[json] support replacer function in BasicJsonStringifier.
R=cbruni@chromium.org Committed: https://crrev.com/53d4594b849bed40d96389f00b6ce0ca1fff05bb Cr-Commit-Position: refs/heads/master@{#36499} Review-Url: https://codereview.chromium.org/2010533002 Cr-Commit-Position: refs/heads/master@{#36514}
This commit is contained in:
parent
863d015e57
commit
abdd1077e3
@ -2779,7 +2779,7 @@ MaybeLocal<String> JSON::Stringify(Local<Context> context,
|
||||
? isolate->factory()->empty_string()
|
||||
: Utils::OpenHandle(*gap);
|
||||
i::Handle<i::Object> maybe;
|
||||
has_pending_exception = !i::BasicJsonStringifier(isolate)
|
||||
has_pending_exception = !i::JsonStringifier(isolate)
|
||||
.Stringify(object, replacer, gap_string)
|
||||
.ToHandle(&maybe);
|
||||
RETURN_ON_FAILED_EXECUTION(String);
|
||||
|
@ -1594,6 +1594,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
|
||||
Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
|
||||
DCHECK(json_object->IsJSObject());
|
||||
JSObject::AddProperty(global, name, json_object, DONT_ENUM);
|
||||
SimpleInstallFunction(json_object, "stringify", Builtins::kJsonStringify, 3,
|
||||
true);
|
||||
}
|
||||
|
||||
{ // -- M a t h
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "src/ic/handler-compiler.h"
|
||||
#include "src/ic/ic.h"
|
||||
#include "src/isolate-inl.h"
|
||||
#include "src/json-stringifier.h"
|
||||
#include "src/messages.h"
|
||||
#include "src/profiler/cpu-profiler.h"
|
||||
#include "src/property-descriptor.h"
|
||||
@ -2179,6 +2180,16 @@ BUILTIN(GlobalEval) {
|
||||
Execution::Call(isolate, function, target_global_proxy, 0, nullptr));
|
||||
}
|
||||
|
||||
// ES6 section 24.3.2 JSON.stringify.
|
||||
BUILTIN(JsonStringify) {
|
||||
HandleScope scope(isolate);
|
||||
JsonStringifier stringifier(isolate);
|
||||
Handle<Object> object = args.atOrUndefined(isolate, 1);
|
||||
Handle<Object> replacer = args.atOrUndefined(isolate, 2);
|
||||
Handle<Object> indent = args.atOrUndefined(isolate, 3);
|
||||
RETURN_RESULT_OR_FAILURE(isolate,
|
||||
stringifier.Stringify(object, replacer, indent));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// ES6 section 20.2.2 Function Properties of the Math Object
|
||||
|
@ -121,6 +121,8 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) {
|
||||
\
|
||||
V(GlobalEval, kTarget) \
|
||||
\
|
||||
V(JsonStringify, kNone) \
|
||||
\
|
||||
V(MathAcos, kNone) \
|
||||
V(MathAsin, kNone) \
|
||||
V(MathAtan, kNone) \
|
||||
|
@ -1784,10 +1784,6 @@ utils.Export(function(to) {
|
||||
to.InnerArraySort = InnerArraySort;
|
||||
to.InnerArrayToLocaleString = InnerArrayToLocaleString;
|
||||
to.PackedArrayReverse = PackedArrayReverse;
|
||||
to.Stack = Stack;
|
||||
to.StackHas = StackHas;
|
||||
to.StackPush = StackPush;
|
||||
to.StackPop = StackPop;
|
||||
});
|
||||
|
||||
%InstallToContext([
|
||||
|
151
src/js/json.js
151
src/js/json.js
@ -15,27 +15,8 @@ var GlobalDate = global.Date;
|
||||
var GlobalJSON = global.JSON;
|
||||
var GlobalSet = global.Set;
|
||||
var InternalArray = utils.InternalArray;
|
||||
var MakeTypeError;
|
||||
var MaxSimple;
|
||||
var MinSimple;
|
||||
var ObjectHasOwnProperty;
|
||||
var Stack;
|
||||
var StackHas;
|
||||
var StackPop;
|
||||
var StackPush;
|
||||
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
|
||||
|
||||
utils.Import(function(from) {
|
||||
MakeTypeError = from.MakeTypeError;
|
||||
MaxSimple = from.MaxSimple;
|
||||
MinSimple = from.MinSimple;
|
||||
ObjectHasOwnProperty = from.ObjectHasOwnProperty;
|
||||
Stack = from.Stack;
|
||||
StackHas = from.StackHas;
|
||||
StackPop = from.StackPop;
|
||||
StackPush = from.StackPush;
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
function CreateDataProperty(o, p, v) {
|
||||
@ -84,137 +65,6 @@ function JSONParse(text, reviver) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function SerializeArray(value, replacer, stack, indent, gap) {
|
||||
if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure);
|
||||
StackPush(stack, value);
|
||||
var stepback = indent;
|
||||
indent += gap;
|
||||
var partial = new InternalArray();
|
||||
var len = TO_LENGTH(value.length);
|
||||
for (var i = 0; i < len; i++) {
|
||||
var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack,
|
||||
indent, gap);
|
||||
if (IS_UNDEFINED(strP)) {
|
||||
strP = "null";
|
||||
}
|
||||
partial.push(strP);
|
||||
}
|
||||
var final;
|
||||
if (gap == "") {
|
||||
final = "[" + partial.join(",") + "]";
|
||||
} else if (partial.length > 0) {
|
||||
var separator = ",\n" + indent;
|
||||
final = "[\n" + indent + partial.join(separator) + "\n" +
|
||||
stepback + "]";
|
||||
} else {
|
||||
final = "[]";
|
||||
}
|
||||
StackPop(stack);
|
||||
return final;
|
||||
}
|
||||
|
||||
|
||||
function SerializeObject(value, replacer, stack, indent, gap) {
|
||||
if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure);
|
||||
StackPush(stack, value);
|
||||
var stepback = indent;
|
||||
indent += gap;
|
||||
var partial = new InternalArray();
|
||||
var keys = %object_keys(value);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var p = keys[i];
|
||||
var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
|
||||
if (!IS_UNDEFINED(strP)) {
|
||||
var member = %QuoteJSONString(p) + ":";
|
||||
if (gap != "") member += " ";
|
||||
member += strP;
|
||||
partial.push(member);
|
||||
}
|
||||
}
|
||||
var final;
|
||||
if (gap == "") {
|
||||
final = "{" + partial.join(",") + "}";
|
||||
} else if (partial.length > 0) {
|
||||
var separator = ",\n" + indent;
|
||||
final = "{\n" + indent + partial.join(separator) + "\n" +
|
||||
stepback + "}";
|
||||
} else {
|
||||
final = "{}";
|
||||
}
|
||||
StackPop(stack);
|
||||
return final;
|
||||
}
|
||||
|
||||
|
||||
function JSONSerialize(key, holder, replacer, stack, indent, gap) {
|
||||
var value = holder[key];
|
||||
if (IS_RECEIVER(value)) {
|
||||
var toJSON = value.toJSON;
|
||||
if (IS_CALLABLE(toJSON)) {
|
||||
value = %_Call(toJSON, value, key);
|
||||
}
|
||||
}
|
||||
if (IS_CALLABLE(replacer)) {
|
||||
value = %_Call(replacer, holder, key, value);
|
||||
}
|
||||
if (IS_STRING(value)) {
|
||||
return %QuoteJSONString(value);
|
||||
} else if (IS_NUMBER(value)) {
|
||||
return JSON_NUMBER_TO_STRING(value);
|
||||
} else if (IS_BOOLEAN(value)) {
|
||||
return value ? "true" : "false";
|
||||
} else if (IS_NULL(value)) {
|
||||
return "null";
|
||||
} else if (IS_RECEIVER(value) && !IS_CALLABLE(value)) {
|
||||
// Non-callable object. If it's a primitive wrapper, it must be unwrapped.
|
||||
if (%is_arraylike(value)) {
|
||||
return SerializeArray(value, replacer, stack, indent, gap);
|
||||
} else if (IS_NUMBER_WRAPPER(value)) {
|
||||
value = TO_NUMBER(value);
|
||||
return JSON_NUMBER_TO_STRING(value);
|
||||
} else if (IS_STRING_WRAPPER(value)) {
|
||||
return %QuoteJSONString(TO_STRING(value));
|
||||
} else if (IS_BOOLEAN_WRAPPER(value)) {
|
||||
return %_ValueOf(value) ? "true" : "false";
|
||||
} else {
|
||||
return SerializeObject(value, replacer, stack, indent, gap);
|
||||
}
|
||||
}
|
||||
// Undefined or a callable object.
|
||||
return UNDEFINED;
|
||||
}
|
||||
|
||||
|
||||
function JSONStringify(value, replacer, space) {
|
||||
if (arguments.length === 1) return %BasicJSONStringify(value, UNDEFINED, "");
|
||||
if (!IS_CALLABLE(replacer)) {
|
||||
return %BasicJSONStringify(value, replacer, space);
|
||||
}
|
||||
if (IS_OBJECT(space)) {
|
||||
// Unwrap 'space' if it is wrapped
|
||||
if (IS_NUMBER_WRAPPER(space)) {
|
||||
space = TO_NUMBER(space);
|
||||
} else if (IS_STRING_WRAPPER(space)) {
|
||||
space = TO_STRING(space);
|
||||
}
|
||||
}
|
||||
var gap;
|
||||
if (IS_NUMBER(space)) {
|
||||
space = MaxSimple(0, MinSimple(TO_INTEGER(space), 10));
|
||||
gap = %_SubString(" ", 0, space);
|
||||
} else if (IS_STRING(space)) {
|
||||
if (space.length > 10) {
|
||||
gap = %_SubString(space, 0, 10);
|
||||
} else {
|
||||
gap = space;
|
||||
}
|
||||
} else {
|
||||
gap = "";
|
||||
}
|
||||
return JSONSerialize('', {'': value}, replacer, new Stack(), "", gap);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
%AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM);
|
||||
@ -222,7 +72,6 @@ function JSONStringify(value, replacer, space) {
|
||||
// Set up non-enumerable properties of the JSON object.
|
||||
utils.InstallFunctions(GlobalJSON, DONT_ENUM, [
|
||||
"parse", JSONParse,
|
||||
"stringify", JSONStringify
|
||||
]);
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
@ -15,7 +15,7 @@ namespace internal {
|
||||
|
||||
// Translation table to escape Latin1 characters.
|
||||
// Table entries start at a multiple of 8 and are null-terminated.
|
||||
const char* const BasicJsonStringifier::JsonEscapeTable =
|
||||
const char* const JsonStringifier::JsonEscapeTable =
|
||||
"\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 "
|
||||
"\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 "
|
||||
"\\b\0 \\t\0 \\n\0 \\u000b\0 "
|
||||
@ -81,15 +81,15 @@ const char* const BasicJsonStringifier::JsonEscapeTable =
|
||||
"\370\0 \371\0 \372\0 \373\0 "
|
||||
"\374\0 \375\0 \376\0 \377\0 ";
|
||||
|
||||
BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
|
||||
JsonStringifier::JsonStringifier(Isolate* isolate)
|
||||
: isolate_(isolate), builder_(isolate), gap_(nullptr), indent_(0) {
|
||||
tojson_string_ = factory()->toJSON_string();
|
||||
stack_ = factory()->NewJSArray(8);
|
||||
}
|
||||
|
||||
MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object,
|
||||
Handle<Object> replacer,
|
||||
Handle<Object> gap) {
|
||||
MaybeHandle<Object> JsonStringifier::Stringify(Handle<Object> object,
|
||||
Handle<Object> replacer,
|
||||
Handle<Object> gap) {
|
||||
if (!InitializeReplacer(replacer)) return MaybeHandle<Object>();
|
||||
if (!gap->IsUndefined() && !InitializeGap(gap)) return MaybeHandle<Object>();
|
||||
Result result = SerializeObject(object);
|
||||
@ -108,8 +108,9 @@ bool IsInList(Handle<String> key, List<Handle<String> >* list) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BasicJsonStringifier::InitializeReplacer(Handle<Object> replacer) {
|
||||
bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) {
|
||||
DCHECK(property_list_.is_null());
|
||||
DCHECK(replacer_function_.is_null());
|
||||
Maybe<bool> is_array = Object::IsArray(replacer);
|
||||
if (is_array.IsNothing()) return false;
|
||||
if (is_array.FromJust()) {
|
||||
@ -144,11 +145,13 @@ bool BasicJsonStringifier::InitializeReplacer(Handle<Object> replacer) {
|
||||
property_list_->set(i, *list[i]);
|
||||
}
|
||||
property_list_ = handle_scope.CloseAndEscape(property_list_);
|
||||
} else if (replacer->IsCallable()) {
|
||||
replacer_function_ = Handle<JSReceiver>::cast(replacer);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BasicJsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
bool JsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
DCHECK_NULL(gap_);
|
||||
HandleScope scope(isolate_);
|
||||
if (gap->IsJSValue()) {
|
||||
@ -188,49 +191,9 @@ bool BasicJsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
return true;
|
||||
}
|
||||
|
||||
MaybeHandle<Object> BasicJsonStringifier::StringifyString(
|
||||
Isolate* isolate, Handle<String> object) {
|
||||
static const int kJsonQuoteWorstCaseBlowup = 6;
|
||||
static const int kSpaceForQuotes = 2;
|
||||
int worst_case_length =
|
||||
object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes;
|
||||
|
||||
if (worst_case_length > 32 * KB) { // Slow path if too large.
|
||||
BasicJsonStringifier stringifier(isolate);
|
||||
Handle<Object> undefined = isolate->factory()->undefined_value();
|
||||
return stringifier.Stringify(object, undefined, undefined);
|
||||
}
|
||||
|
||||
object = String::Flatten(object);
|
||||
DCHECK(object->IsFlat());
|
||||
Handle<SeqString> result;
|
||||
if (object->IsOneByteRepresentationUnderneath()) {
|
||||
result = isolate->factory()
|
||||
->NewRawOneByteString(worst_case_length)
|
||||
.ToHandleChecked();
|
||||
IncrementalStringBuilder::NoExtendString<uint8_t> no_extend(
|
||||
result, worst_case_length);
|
||||
no_extend.Append('\"');
|
||||
SerializeStringUnchecked_(object->GetFlatContent().ToOneByteVector(),
|
||||
&no_extend);
|
||||
no_extend.Append('\"');
|
||||
return no_extend.Finalize();
|
||||
} else {
|
||||
result = isolate->factory()
|
||||
->NewRawTwoByteString(worst_case_length)
|
||||
.ToHandleChecked();
|
||||
IncrementalStringBuilder::NoExtendString<uc16> no_extend(result,
|
||||
worst_case_length);
|
||||
no_extend.Append('\"');
|
||||
SerializeStringUnchecked_(object->GetFlatContent().ToUC16Vector(),
|
||||
&no_extend);
|
||||
no_extend.Append('\"');
|
||||
return no_extend.Finalize();
|
||||
}
|
||||
}
|
||||
|
||||
MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction(
|
||||
Handle<Object> object, Handle<Object> key) {
|
||||
MaybeHandle<Object> JsonStringifier::ApplyToJsonFunction(Handle<Object> object,
|
||||
Handle<Object> key) {
|
||||
HandleScope scope(isolate_);
|
||||
LookupIterator it(object, tojson_string_,
|
||||
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
|
||||
Handle<Object> fun;
|
||||
@ -240,15 +203,39 @@ MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction(
|
||||
// Call toJSON function.
|
||||
if (key->IsSmi()) key = factory()->NumberToString(key);
|
||||
Handle<Object> argv[] = {key};
|
||||
HandleScope scope(isolate_);
|
||||
ASSIGN_RETURN_ON_EXCEPTION(isolate_, object,
|
||||
Execution::Call(isolate_, fun, object, 1, argv),
|
||||
Object);
|
||||
return scope.CloseAndEscape(object);
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
|
||||
Handle<Object> object) {
|
||||
MaybeHandle<Object> JsonStringifier::ApplyReplacerFunction(
|
||||
Handle<Object> object, Handle<Object> key) {
|
||||
HandleScope scope(isolate_);
|
||||
if (key->IsSmi()) key = factory()->NumberToString(key);
|
||||
Handle<Object> argv[] = {key, object};
|
||||
Handle<JSReceiver> holder = CurrentHolder(object);
|
||||
ASSIGN_RETURN_ON_EXCEPTION(
|
||||
isolate_, object,
|
||||
Execution::Call(isolate_, replacer_function_, holder, 2, argv), Object);
|
||||
return scope.CloseAndEscape(object);
|
||||
}
|
||||
|
||||
Handle<JSReceiver> JsonStringifier::CurrentHolder(Handle<Object> value) {
|
||||
int length = Smi::cast(stack_->length())->value();
|
||||
if (length == 0) {
|
||||
Handle<JSObject> holder =
|
||||
factory()->NewJSObject(isolate_->object_function());
|
||||
JSObject::AddProperty(holder, factory()->empty_string(), value, NONE);
|
||||
return holder;
|
||||
} else {
|
||||
FixedArray* elements = FixedArray::cast(stack_->elements());
|
||||
return Handle<JSReceiver>(JSReceiver::cast(elements->get(length - 1)),
|
||||
isolate_);
|
||||
}
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::StackPush(Handle<Object> object) {
|
||||
StackLimitCheck check(isolate_);
|
||||
if (check.HasOverflowed()) {
|
||||
isolate_->StackOverflow();
|
||||
@ -274,18 +261,23 @@ BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void BasicJsonStringifier::StackPop() {
|
||||
void JsonStringifier::StackPop() {
|
||||
int length = Smi::cast(stack_->length())->value();
|
||||
stack_->set_length(Smi::FromInt(length - 1));
|
||||
}
|
||||
|
||||
template <bool deferred_string_key>
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::Serialize_(
|
||||
Handle<Object> object, bool comma, Handle<Object> key) {
|
||||
JsonStringifier::Result JsonStringifier::Serialize_(Handle<Object> object,
|
||||
bool comma,
|
||||
Handle<Object> key) {
|
||||
if (object->IsJSReceiver()) {
|
||||
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
||||
isolate_, object, ApplyToJsonFunction(object, key), EXCEPTION);
|
||||
}
|
||||
if (!replacer_function_.is_null()) {
|
||||
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
||||
isolate_, object, ApplyReplacerFunction(object, key), EXCEPTION);
|
||||
}
|
||||
|
||||
if (object->IsSmi()) {
|
||||
if (deferred_string_key) SerializeDeferredKey(comma, key);
|
||||
@ -344,7 +336,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::Serialize_(
|
||||
return UNCHANGED;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue(
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSValue(
|
||||
Handle<JSValue> object) {
|
||||
String* class_name = object->class_name();
|
||||
if (class_name == isolate_->heap()->String_string()) {
|
||||
@ -369,7 +361,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
|
||||
JsonStringifier::Result JsonStringifier::SerializeSmi(Smi* object) {
|
||||
static const int kBufferSize = 100;
|
||||
char chars[kBufferSize];
|
||||
Vector<char> buffer(chars, kBufferSize);
|
||||
@ -377,8 +369,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
|
||||
double number) {
|
||||
JsonStringifier::Result JsonStringifier::SerializeDouble(double number) {
|
||||
if (std::isinf(number) || std::isnan(number)) {
|
||||
builder_.AppendCString("null");
|
||||
return SUCCESS;
|
||||
@ -390,7 +381,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSArray(
|
||||
Handle<JSArray> object) {
|
||||
HandleScope handle_scope(isolate_);
|
||||
Result stack_push = StackPush(object);
|
||||
@ -400,59 +391,65 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
|
||||
DCHECK(!object->IsAccessCheckNeeded());
|
||||
builder_.AppendCharacter('[');
|
||||
Indent();
|
||||
switch (object->GetElementsKind()) {
|
||||
case FAST_SMI_ELEMENTS: {
|
||||
Handle<FixedArray> elements(FixedArray::cast(object->elements()),
|
||||
isolate_);
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
Separator(i == 0);
|
||||
SerializeSmi(Smi::cast(elements->get(i)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FAST_DOUBLE_ELEMENTS: {
|
||||
// Empty array is FixedArray but not FixedDoubleArray.
|
||||
if (length == 0) break;
|
||||
Handle<FixedDoubleArray> elements(
|
||||
FixedDoubleArray::cast(object->elements()), isolate_);
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
Separator(i == 0);
|
||||
SerializeDouble(elements->get_scalar(i));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FAST_ELEMENTS: {
|
||||
Handle<Object> old_length(object->length(), isolate_);
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
if (object->length() != *old_length ||
|
||||
object->GetElementsKind() != FAST_ELEMENTS) {
|
||||
Result result = SerializeArrayLikeSlow(object, i, length);
|
||||
if (result != SUCCESS) return result;
|
||||
break;
|
||||
}
|
||||
Separator(i == 0);
|
||||
Result result = SerializeElement(
|
||||
isolate_,
|
||||
Handle<Object>(FixedArray::cast(object->elements())->get(i),
|
||||
isolate_),
|
||||
i);
|
||||
if (result == SUCCESS) continue;
|
||||
if (result == UNCHANGED) {
|
||||
builder_.AppendCString("null");
|
||||
} else {
|
||||
return result;
|
||||
uint32_t i = 0;
|
||||
if (replacer_function_.is_null()) {
|
||||
switch (object->GetElementsKind()) {
|
||||
case FAST_SMI_ELEMENTS: {
|
||||
Handle<FixedArray> elements(FixedArray::cast(object->elements()),
|
||||
isolate_);
|
||||
while (i < length) {
|
||||
Separator(i == 0);
|
||||
SerializeSmi(Smi::cast(elements->get(i)));
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// The FAST_HOLEY_* cases could be handled in a faster way. They resemble
|
||||
// the non-holey cases except that a lookup is necessary for holes.
|
||||
default: {
|
||||
Result result = SerializeArrayLikeSlow(object, 0, length);
|
||||
if (result != SUCCESS) return result;
|
||||
break;
|
||||
case FAST_DOUBLE_ELEMENTS: {
|
||||
// Empty array is FixedArray but not FixedDoubleArray.
|
||||
if (length == 0) break;
|
||||
Handle<FixedDoubleArray> elements(
|
||||
FixedDoubleArray::cast(object->elements()), isolate_);
|
||||
while (i < length) {
|
||||
Separator(i == 0);
|
||||
SerializeDouble(elements->get_scalar(i));
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FAST_ELEMENTS: {
|
||||
Handle<Object> old_length(object->length(), isolate_);
|
||||
while (i < length) {
|
||||
if (object->length() != *old_length ||
|
||||
object->GetElementsKind() != FAST_ELEMENTS) {
|
||||
// Fall back to slow path.
|
||||
break;
|
||||
}
|
||||
Separator(i == 0);
|
||||
Result result = SerializeElement(
|
||||
isolate_,
|
||||
Handle<Object>(FixedArray::cast(object->elements())->get(i),
|
||||
isolate_),
|
||||
i);
|
||||
if (result == UNCHANGED) {
|
||||
builder_.AppendCString("null");
|
||||
} else if (result != SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// The FAST_HOLEY_* cases could be handled in a faster way. They resemble
|
||||
// the non-holey cases except that a lookup is necessary for holes.
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i < length) {
|
||||
// Slow path for non-fast elements and fall-back in edge case.
|
||||
Result result = SerializeArrayLikeSlow(object, i, length);
|
||||
if (result != SUCCESS) return result;
|
||||
}
|
||||
Unindent();
|
||||
if (length > 0) NewLine();
|
||||
builder_.AppendCharacter(']');
|
||||
@ -460,7 +457,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeArrayLikeSlow(
|
||||
JsonStringifier::Result JsonStringifier::SerializeArrayLikeSlow(
|
||||
Handle<JSReceiver> object, uint32_t start, uint32_t length) {
|
||||
for (uint32_t i = start; i < length; i++) {
|
||||
Separator(i == 0);
|
||||
@ -468,22 +465,18 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeArrayLikeSlow(
|
||||
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
||||
isolate_, element, JSReceiver::GetElement(isolate_, object, i),
|
||||
EXCEPTION);
|
||||
if (element->IsUndefined()) {
|
||||
Result result = SerializeElement(isolate_, element, i);
|
||||
if (result == SUCCESS) continue;
|
||||
if (result == UNCHANGED) {
|
||||
builder_.AppendCString("null");
|
||||
} else {
|
||||
Result result = SerializeElement(isolate_, element, i);
|
||||
if (result == SUCCESS) continue;
|
||||
if (result == UNCHANGED) {
|
||||
builder_.AppendCString("null");
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSObject(
|
||||
Handle<JSObject> object) {
|
||||
HandleScope handle_scope(isolate_);
|
||||
Result stack_push = StackPush(object);
|
||||
@ -512,12 +505,8 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
|
||||
Handle<Object> property;
|
||||
if (details.type() == DATA && *map == js_obj->map()) {
|
||||
FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
|
||||
if (js_obj->IsUnboxedDoubleField(field_index)) {
|
||||
double value = js_obj->RawFastDoublePropertyAt(field_index);
|
||||
property = isolate_->factory()->NewHeapNumber(value);
|
||||
} else {
|
||||
property = handle(js_obj->RawFastPropertyAt(field_index), isolate_);
|
||||
}
|
||||
property = JSObject::FastPropertyAt(js_obj, details.representation(),
|
||||
field_index);
|
||||
} else {
|
||||
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
||||
isolate_, property, Object::GetPropertyOrElement(js_obj, key),
|
||||
@ -538,7 +527,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSReceiverSlow(
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSReceiverSlow(
|
||||
Handle<JSReceiver> object) {
|
||||
Handle<FixedArray> contents = property_list_;
|
||||
if (contents.is_null()) {
|
||||
@ -580,7 +569,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSReceiverSlow(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy(
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSProxy(
|
||||
Handle<JSProxy> object) {
|
||||
Result stack_push = StackPush(object);
|
||||
if (stack_push != SUCCESS) return stack_push;
|
||||
@ -615,7 +604,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy(
|
||||
}
|
||||
|
||||
template <typename SrcChar, typename DestChar>
|
||||
void BasicJsonStringifier::SerializeStringUnchecked_(
|
||||
void JsonStringifier::SerializeStringUnchecked_(
|
||||
Vector<const SrcChar> src,
|
||||
IncrementalStringBuilder::NoExtend<DestChar>* dest) {
|
||||
// Assert that uc16 character is not truncated down to 8 bit.
|
||||
@ -633,7 +622,7 @@ void BasicJsonStringifier::SerializeStringUnchecked_(
|
||||
}
|
||||
|
||||
template <typename SrcChar, typename DestChar>
|
||||
void BasicJsonStringifier::SerializeString_(Handle<String> string) {
|
||||
void JsonStringifier::SerializeString_(Handle<String> string) {
|
||||
int length = string->length();
|
||||
builder_.Append<uint8_t, DestChar>('"');
|
||||
// We make a rough estimate to find out if the current string can be
|
||||
@ -663,35 +652,35 @@ void BasicJsonStringifier::SerializeString_(Handle<String> string) {
|
||||
}
|
||||
|
||||
template <>
|
||||
bool BasicJsonStringifier::DoNotEscape(uint8_t c) {
|
||||
bool JsonStringifier::DoNotEscape(uint8_t c) {
|
||||
return c >= '#' && c <= '~' && c != '\\';
|
||||
}
|
||||
|
||||
template <>
|
||||
bool BasicJsonStringifier::DoNotEscape(uint16_t c) {
|
||||
bool JsonStringifier::DoNotEscape(uint16_t c) {
|
||||
return c >= '#' && c != '\\' && c != 0x7f;
|
||||
}
|
||||
|
||||
void BasicJsonStringifier::NewLine() {
|
||||
void JsonStringifier::NewLine() {
|
||||
if (gap_ == nullptr) return;
|
||||
builder_.AppendCharacter('\n');
|
||||
for (int i = 0; i < indent_; i++) builder_.AppendCString(gap_);
|
||||
}
|
||||
|
||||
void BasicJsonStringifier::Separator(bool first) {
|
||||
void JsonStringifier::Separator(bool first) {
|
||||
if (!first) builder_.AppendCharacter(',');
|
||||
NewLine();
|
||||
}
|
||||
|
||||
void BasicJsonStringifier::SerializeDeferredKey(bool deferred_comma,
|
||||
Handle<Object> deferred_key) {
|
||||
void JsonStringifier::SerializeDeferredKey(bool deferred_comma,
|
||||
Handle<Object> deferred_key) {
|
||||
Separator(!deferred_comma);
|
||||
SerializeString(Handle<String>::cast(deferred_key));
|
||||
builder_.AppendCharacter(':');
|
||||
if (gap_ != nullptr) builder_.AppendCharacter(' ');
|
||||
}
|
||||
|
||||
void BasicJsonStringifier::SerializeString(Handle<String> object) {
|
||||
void JsonStringifier::SerializeString(Handle<String> object) {
|
||||
object = String::Flatten(object);
|
||||
if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) {
|
||||
if (object->IsOneByteRepresentationUnderneath()) {
|
||||
|
@ -11,19 +11,16 @@
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
class BasicJsonStringifier BASE_EMBEDDED {
|
||||
class JsonStringifier BASE_EMBEDDED {
|
||||
public:
|
||||
explicit BasicJsonStringifier(Isolate* isolate);
|
||||
explicit JsonStringifier(Isolate* isolate);
|
||||
|
||||
~BasicJsonStringifier() { DeleteArray(gap_); }
|
||||
~JsonStringifier() { DeleteArray(gap_); }
|
||||
|
||||
MUST_USE_RESULT MaybeHandle<Object> Stringify(Handle<Object> object,
|
||||
Handle<Object> replacer,
|
||||
Handle<Object> gap);
|
||||
|
||||
MUST_USE_RESULT static MaybeHandle<Object> StringifyString(
|
||||
Isolate* isolate, Handle<String> object);
|
||||
|
||||
private:
|
||||
enum Result { UNCHANGED, SUCCESS, EXCEPTION };
|
||||
|
||||
@ -33,6 +30,8 @@ class BasicJsonStringifier BASE_EMBEDDED {
|
||||
MUST_USE_RESULT MaybeHandle<Object> ApplyToJsonFunction(
|
||||
Handle<Object> object,
|
||||
Handle<Object> key);
|
||||
MUST_USE_RESULT MaybeHandle<Object> ApplyReplacerFunction(
|
||||
Handle<Object> object, Handle<Object> key);
|
||||
|
||||
// Entry point to serialize the object.
|
||||
INLINE(Result SerializeObject(Handle<Object> obj)) {
|
||||
@ -100,6 +99,8 @@ class BasicJsonStringifier BASE_EMBEDDED {
|
||||
INLINE(void Unindent() { indent_--; });
|
||||
INLINE(void Separator(bool first));
|
||||
|
||||
Handle<JSReceiver> CurrentHolder(Handle<Object> value);
|
||||
|
||||
Result StackPush(Handle<Object> object);
|
||||
void StackPop();
|
||||
|
||||
@ -110,6 +111,7 @@ class BasicJsonStringifier BASE_EMBEDDED {
|
||||
Handle<String> tojson_string_;
|
||||
Handle<JSArray> stack_;
|
||||
Handle<FixedArray> property_list_;
|
||||
Handle<JSReceiver> replacer_function_;
|
||||
uc16* gap_;
|
||||
int indent_;
|
||||
|
||||
|
@ -14,24 +14,6 @@
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
RUNTIME_FUNCTION(Runtime_QuoteJSONString) {
|
||||
HandleScope scope(isolate);
|
||||
CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
|
||||
DCHECK(args.length() == 1);
|
||||
RETURN_RESULT_OR_FAILURE(
|
||||
isolate, BasicJsonStringifier::StringifyString(isolate, string));
|
||||
}
|
||||
|
||||
RUNTIME_FUNCTION(Runtime_BasicJSONStringify) {
|
||||
HandleScope scope(isolate);
|
||||
DCHECK(args.length() == 3);
|
||||
CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
|
||||
CONVERT_ARG_HANDLE_CHECKED(Object, replacer, 1);
|
||||
CONVERT_ARG_HANDLE_CHECKED(Object, gap, 2);
|
||||
RETURN_RESULT_OR_FAILURE(
|
||||
isolate, BasicJsonStringifier(isolate).Stringify(object, replacer, gap));
|
||||
}
|
||||
|
||||
RUNTIME_FUNCTION(Runtime_ParseJson) {
|
||||
HandleScope scope(isolate);
|
||||
DCHECK_EQ(1, args.length());
|
||||
|
@ -327,8 +327,6 @@ namespace internal {
|
||||
F(OrdinaryHasInstance, 2, 1)
|
||||
|
||||
#define FOR_EACH_INTRINSIC_JSON(F) \
|
||||
F(QuoteJSONString, 1, 1) \
|
||||
F(BasicJSONStringify, 3, 1) \
|
||||
F(ParseJson, 1, 1)
|
||||
|
||||
#define FOR_EACH_INTRINSIC_LITERALS(F) \
|
||||
|
@ -520,3 +520,6 @@ reviver = function(p, v) {
|
||||
return p === "" ? v : 42;
|
||||
}
|
||||
assertEquals({a: 0, b: 1}, JSON.parse('{"a":0,"b":1}', reviver));
|
||||
|
||||
reviver = (k, v) => (v === Infinity) ? "inf" : v;
|
||||
assertEquals('{"":"inf"}', JSON.stringify({"":Infinity}, reviver));
|
||||
|
Loading…
Reference in New Issue
Block a user