Added JSString for gc-able UNICODE strings, changed toString(), toNumber() to return JSValue rather than JSValue*.

This commit is contained in:
beard%netscape.com 2000-05-09 03:10:04 +00:00
Родитель f97cc95de0
Коммит 0327df07be
4 изменённых файлов: 96 добавлений и 52 удалений

Просмотреть файл

@ -72,10 +72,10 @@ Formatter& operator<<(Formatter& f, const JSValue& value)
case JSValue::object_tag:
case JSValue::array_tag:
case JSValue::function_tag:
printFormat(stdOut, "0x%08X", value.object);
printFormat(f, "0x%08X", value.object);
break;
case JSValue::string_tag:
f << *value.string;
printString(f, value.string->begin(), value.string->end());
break;
default:
f << "undefined";
@ -85,16 +85,16 @@ Formatter& operator<<(Formatter& f, const JSValue& value)
}
JSValue *JSValue::valueToString(JSValue *v) // can assume v is not a string
JSValue JSValue::valueToString(const JSValue& value) // can assume value is not a string
{
char *chrp;
char buf[dtosStandardBufferSize];
switch (v->tag) {
switch (value.tag) {
case JSValue::i32_tag:
chrp = doubleToStr(buf, dtosStandardBufferSize, v->i32, dtosStandard, 0);
chrp = doubleToStr(buf, dtosStandardBufferSize, value.i32, dtosStandard, 0);
break;
case JSValue::f64_tag:
chrp = doubleToStr(buf, dtosStandardBufferSize, v->f64, dtosStandard, 0);
chrp = doubleToStr(buf, dtosStandardBufferSize, value.f64, dtosStandard, 0);
break;
case JSValue::object_tag:
chrp = "object";
@ -106,27 +106,26 @@ JSValue *JSValue::valueToString(JSValue *v) // can assume v is not a string
chrp = "function";
break;
case JSValue::string_tag:
return v;
return value;
default:
chrp = "undefined";
break;
}
return new JSValue(new String(widenCString(chrp)));
return JSValue(new JSString(chrp));
}
JSValue *JSValue::valueToNumber(JSValue *v)// can assume v is not a number
JSValue JSValue::valueToNumber(const JSValue& value) // can assume value is not a number
{
switch (v->tag) {
switch (value.tag) {
case JSValue::i32_tag:
case JSValue::f64_tag:
return v;
return value;
case JSValue::string_tag:
{
int length = v->string->length();
const char16 *s = v->string->c_str();
JSString* str = value.string;
const char16 *numEnd;
double d = stringToDouble(s, s + length, numEnd);
return new JSValue(d);
double d = stringToDouble(str->begin(), str->end(), numEnd);
return JSValue(d);
}
case JSValue::object_tag:
case JSValue::array_tag:
@ -134,9 +133,22 @@ JSValue *JSValue::valueToNumber(JSValue *v)// can assume v is not a number
default:
break;
}
return new JSValue();
return kUndefinedValue;
}
JSString::JSString(const String* str)
{
size_t n = str->size();
resize(n);
traits_type::copy(begin(), str->begin(), n);
}
JSString::JSString(const char* str)
{
size_t n = ::strlen(str);
resize(n);
std::transform(str, str + n, begin(), JavaScript::widen);
}
} /* namespace JSTypes */
} /* namespace JavaScript */

Просмотреть файл

@ -43,7 +43,6 @@
/* forward declare classes from JavaScript::ICG */
namespace JavaScript {
class StringAtom;
namespace ICG {
class ICodeModule;
} /* namespace ICG */
@ -56,6 +55,7 @@ namespace JSTypes {
class JSObject;
class JSArray;
class JSFunction;
class JSString;
/**
* All JavaScript data types.
@ -75,7 +75,7 @@ namespace JSTypes {
JSObject* object;
JSArray* array;
JSFunction *function;
String *string;
JSString *string;
};
enum {
@ -94,23 +94,23 @@ namespace JSTypes {
explicit JSValue(JSObject* object) : object(object), tag(object_tag) {}
explicit JSValue(JSArray* array) : array(array), tag(array_tag) {}
explicit JSValue(JSFunction* function) : function(function), tag(function_tag) {}
explicit JSValue(String* string) : string(string), tag(string_tag) {}
explicit JSValue(JSString* string) : string(string), tag(string_tag) {}
int32& operator=(int32 i32) { return (tag = i32_tag, this->i32 = i32); }
float64& operator=(float64 f64) { return (tag = f64_tag, this->f64 = f64); }
JSObject*& operator=(JSObject* object) { return (tag = object_tag, this->object = object); }
JSArray*& operator=(JSArray* array) { return (tag = array_tag, this->array = array); }
JSFunction*& operator=(JSFunction* function) { return (tag = function_tag, this->function = function); }
String*& operator=(String* string) { return (tag = string_tag, this->string = string); }
JSString*& operator=(JSString* string) { return (tag = string_tag, this->string = string); }
bool isString() { return (tag == string_tag); }
bool isNumber() { return ((tag == f64_tag) || (tag == i32_tag)); }
bool isString() const { return (tag == string_tag); }
bool isNumber() const { return ((tag == f64_tag) || (tag == i32_tag)); }
JSValue *toString() { if (isString()) return this; else return valueToString(this); }
JSValue *toNumber() { if (isNumber()) return this; else return valueToNumber(this); }
JSValue toString() { return (isString() ? *this : valueToString(*this)); }
JSValue toNumber() { return (isNumber() ? *this : valueToNumber(*this)); }
static JSValue *valueToString(JSValue *v);
static JSValue *valueToNumber(JSValue *v);
static JSValue valueToString(const JSValue& value);
static JSValue valueToNumber(const JSValue& value);
int operator==(const JSValue& value) const;
};
@ -266,6 +266,16 @@ namespace JSTypes {
JSValue value;
};
/**
* Garbage collectable UNICODE string.
*/
class JSString : public std::basic_string<char16, std::char_traits<char16>, gc_allocator<char16> >, public gc_base {
public:
JSString() {}
explicit JSString(const String* str);
explicit JSString(const char* str);
};
/**
* Provides a set of nested scopes.
*/

Просмотреть файл

@ -72,10 +72,10 @@ Formatter& operator<<(Formatter& f, const JSValue& value)
case JSValue::object_tag:
case JSValue::array_tag:
case JSValue::function_tag:
printFormat(stdOut, "0x%08X", value.object);
printFormat(f, "0x%08X", value.object);
break;
case JSValue::string_tag:
f << *value.string;
printString(f, value.string->begin(), value.string->end());
break;
default:
f << "undefined";
@ -85,16 +85,16 @@ Formatter& operator<<(Formatter& f, const JSValue& value)
}
JSValue *JSValue::valueToString(JSValue *v) // can assume v is not a string
JSValue JSValue::valueToString(const JSValue& value) // can assume value is not a string
{
char *chrp;
char buf[dtosStandardBufferSize];
switch (v->tag) {
switch (value.tag) {
case JSValue::i32_tag:
chrp = doubleToStr(buf, dtosStandardBufferSize, v->i32, dtosStandard, 0);
chrp = doubleToStr(buf, dtosStandardBufferSize, value.i32, dtosStandard, 0);
break;
case JSValue::f64_tag:
chrp = doubleToStr(buf, dtosStandardBufferSize, v->f64, dtosStandard, 0);
chrp = doubleToStr(buf, dtosStandardBufferSize, value.f64, dtosStandard, 0);
break;
case JSValue::object_tag:
chrp = "object";
@ -106,27 +106,26 @@ JSValue *JSValue::valueToString(JSValue *v) // can assume v is not a string
chrp = "function";
break;
case JSValue::string_tag:
return v;
return value;
default:
chrp = "undefined";
break;
}
return new JSValue(new String(widenCString(chrp)));
return JSValue(new JSString(chrp));
}
JSValue *JSValue::valueToNumber(JSValue *v)// can assume v is not a number
JSValue JSValue::valueToNumber(const JSValue& value) // can assume value is not a number
{
switch (v->tag) {
switch (value.tag) {
case JSValue::i32_tag:
case JSValue::f64_tag:
return v;
return value;
case JSValue::string_tag:
{
int length = v->string->length();
const char16 *s = v->string->c_str();
JSString* str = value.string;
const char16 *numEnd;
double d = stringToDouble(s, s + length, numEnd);
return new JSValue(d);
double d = stringToDouble(str->begin(), str->end(), numEnd);
return JSValue(d);
}
case JSValue::object_tag:
case JSValue::array_tag:
@ -134,9 +133,22 @@ JSValue *JSValue::valueToNumber(JSValue *v)// can assume v is not a number
default:
break;
}
return new JSValue();
return kUndefinedValue;
}
JSString::JSString(const String* str)
{
size_t n = str->size();
resize(n);
traits_type::copy(begin(), str->begin(), n);
}
JSString::JSString(const char* str)
{
size_t n = ::strlen(str);
resize(n);
std::transform(str, str + n, begin(), JavaScript::widen);
}
} /* namespace JSTypes */
} /* namespace JavaScript */

Просмотреть файл

@ -43,7 +43,6 @@
/* forward declare classes from JavaScript::ICG */
namespace JavaScript {
class StringAtom;
namespace ICG {
class ICodeModule;
} /* namespace ICG */
@ -56,6 +55,7 @@ namespace JSTypes {
class JSObject;
class JSArray;
class JSFunction;
class JSString;
/**
* All JavaScript data types.
@ -75,7 +75,7 @@ namespace JSTypes {
JSObject* object;
JSArray* array;
JSFunction *function;
String *string;
JSString *string;
};
enum {
@ -94,23 +94,23 @@ namespace JSTypes {
explicit JSValue(JSObject* object) : object(object), tag(object_tag) {}
explicit JSValue(JSArray* array) : array(array), tag(array_tag) {}
explicit JSValue(JSFunction* function) : function(function), tag(function_tag) {}
explicit JSValue(String* string) : string(string), tag(string_tag) {}
explicit JSValue(JSString* string) : string(string), tag(string_tag) {}
int32& operator=(int32 i32) { return (tag = i32_tag, this->i32 = i32); }
float64& operator=(float64 f64) { return (tag = f64_tag, this->f64 = f64); }
JSObject*& operator=(JSObject* object) { return (tag = object_tag, this->object = object); }
JSArray*& operator=(JSArray* array) { return (tag = array_tag, this->array = array); }
JSFunction*& operator=(JSFunction* function) { return (tag = function_tag, this->function = function); }
String*& operator=(String* string) { return (tag = string_tag, this->string = string); }
JSString*& operator=(JSString* string) { return (tag = string_tag, this->string = string); }
bool isString() { return (tag == string_tag); }
bool isNumber() { return ((tag == f64_tag) || (tag == i32_tag)); }
bool isString() const { return (tag == string_tag); }
bool isNumber() const { return ((tag == f64_tag) || (tag == i32_tag)); }
JSValue *toString() { if (isString()) return this; else return valueToString(this); }
JSValue *toNumber() { if (isNumber()) return this; else return valueToNumber(this); }
JSValue toString() { return (isString() ? *this : valueToString(*this)); }
JSValue toNumber() { return (isNumber() ? *this : valueToNumber(*this)); }
static JSValue *valueToString(JSValue *v);
static JSValue *valueToNumber(JSValue *v);
static JSValue valueToString(const JSValue& value);
static JSValue valueToNumber(const JSValue& value);
int operator==(const JSValue& value) const;
};
@ -266,6 +266,16 @@ namespace JSTypes {
JSValue value;
};
/**
* Garbage collectable UNICODE string.
*/
class JSString : public std::basic_string<char16, std::char_traits<char16>, gc_allocator<char16> >, public gc_base {
public:
JSString() {}
explicit JSString(const String* str);
explicit JSString(const char* str);
};
/**
* Provides a set of nested scopes.
*/