Commit b36e346a authored by sandholm@chromium.org's avatar sandholm@chromium.org

Use the PushIfAbsent function for the JSON stringify stack.

Optimize ConvertToString.
Review URL: http://codereview.chromium.org/5614004

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5943 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 059eece4
......@@ -159,25 +159,23 @@ function Join(array, length, separator, convert) {
}
function ConvertToString(e) {
if (e == null) return '';
else return ToString(e);
function ConvertToString(x) {
if (IS_STRING(x)) return x;
if (IS_NUMBER(x)) return %_NumberToString(x);
if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x));
}
function ConvertToLocaleString(e) {
if (e == null) {
return '';
} else {
// e_obj's toLocaleString might be overwritten, check if it is a function.
// Call ToString if toLocaleString is not a function.
// See issue 877615.
var e_obj = ToObject(e);
if (IS_FUNCTION(e_obj.toLocaleString))
return ToString(e_obj.toLocaleString());
else
return ToString(e);
}
// e_obj's toLocaleString might be overwritten, check if it is a function.
// Call ConvertToString if toLocaleString is not a function.
// See issue 877615.
var e_obj = ToObject(e);
if (IS_FUNCTION(e_obj.toLocaleString))
return ToString(e_obj.toLocaleString());
else
return ConvertToString(e);
}
......@@ -365,14 +363,13 @@ function ArrayJoin(separator) {
if (IS_UNDEFINED(separator)) {
separator = ',';
} else if (!IS_STRING(separator)) {
separator = ToString(separator);
separator = NonStringToString(separator);
}
var result = %_FastAsciiArrayJoin(this, separator);
if (!IS_UNDEFINED(result)) return result;
var length = TO_UINT32(this.length);
return Join(this, length, separator, ConvertToString);
return Join(this, TO_UINT32(this.length), separator, ConvertToString);
}
......
......@@ -66,21 +66,10 @@ function JSONParse(text, reviver) {
}
}
function StackContains(stack, val) {
var length = stack.length;
for (var i = 0; i < length; i++) {
if (stack[i] === val) {
return true;
}
}
return false;
}
function SerializeArray(value, replacer, stack, indent, gap) {
if (StackContains(stack, value)) {
if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []);
}
stack.push(value);
var stepback = indent;
indent += gap;
var partial = [];
......@@ -108,10 +97,9 @@ function SerializeArray(value, replacer, stack, indent, gap) {
}
function SerializeObject(value, replacer, stack, indent, gap) {
if (StackContains(stack, value)) {
if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []);
}
stack.push(value);
var stepback = indent;
indent += gap;
var partial = [];
......@@ -197,10 +185,9 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
function BasicSerializeArray(value, stack, builder) {
if (StackContains(stack, value)) {
if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []);
}
stack.push(value);
builder.push("[");
var len = value.length;
for (var i = 0; i < len; i++) {
......@@ -220,10 +207,9 @@ function BasicSerializeArray(value, stack, builder) {
function BasicSerializeObject(value, stack, builder) {
if (StackContains(stack, value)) {
if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []);
}
stack.push(value);
builder.push("{");
for (var p in value) {
if (%HasLocalProperty(value, p)) {
......
......@@ -7671,13 +7671,13 @@ static MaybeObject* Runtime_AllocateInNewSpace(Arguments args) {
}
// Push an array unto an array of arrays if it is not already in the
// Push an object unto an array of objects if it is not already in the
// array. Returns true if the element was pushed on the stack and
// false otherwise.
static MaybeObject* Runtime_PushIfAbsent(Arguments args) {
ASSERT(args.length() == 2);
CONVERT_CHECKED(JSArray, array, args[0]);
CONVERT_CHECKED(JSArray, element, args[1]);
CONVERT_CHECKED(JSObject, element, args[1]);
RUNTIME_ASSERT(array->HasFastElements());
int length = Smi::cast(array->length())->value();
FixedArray* elements = FixedArray::cast(array->elements());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment