Revert of [json] support replacer function in BasicJsonStringifier. (patchset #4 id:60001 of https://codereview.chromium.org/2010533002/ )
Reason for revert: Layout test failure: https://build.chromium.org/p/client.v8.fyi/builders/V8-Blink%20Linux%2064/builds/7029 Original issue's description: > [json] support replacer function in BasicJsonStringifier. > > R=cbruni@chromium.org > > Committed: https://crrev.com/53d4594b849bed40d96389f00b6ce0ca1fff05bb > Cr-Commit-Position: refs/heads/master@{#36499} TBR=cbruni@chromium.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review-Url: https://codereview.chromium.org/2008293002 Cr-Commit-Position: refs/heads/master@{#36512}
This commit is contained in:
parent
dd609a5d3d
commit
7012babb9d
@ -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::JsonStringifier(isolate)
|
||||
has_pending_exception = !i::BasicJsonStringifier(isolate)
|
||||
.Stringify(object, replacer, gap_string)
|
||||
.ToHandle(&maybe);
|
||||
RETURN_ON_FAILED_EXECUTION(String);
|
||||
|
@ -1594,8 +1594,6 @@ 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,7 +18,6 @@
|
||||
#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"
|
||||
@ -2180,16 +2179,6 @@ 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,8 +121,6 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) {
|
||||
\
|
||||
V(GlobalEval, kTarget) \
|
||||
\
|
||||
V(JsonStringify, kNone) \
|
||||
\
|
||||
V(MathAcos, kNone) \
|
||||
V(MathAsin, kNone) \
|
||||
V(MathAtan, kNone) \
|
||||
|
@ -1784,6 +1784,10 @@ 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,8 +15,27 @@ 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) {
|
||||
@ -65,6 +84,137 @@ 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);
|
||||
@ -72,6 +222,7 @@ function JSONParse(text, reviver) {
|
||||
// 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 JsonStringifier::JsonEscapeTable =
|
||||
const char* const BasicJsonStringifier::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 JsonStringifier::JsonEscapeTable =
|
||||
"\370\0 \371\0 \372\0 \373\0 "
|
||||
"\374\0 \375\0 \376\0 \377\0 ";
|
||||
|
||||
JsonStringifier::JsonStringifier(Isolate* isolate)
|
||||
BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
|
||||
: isolate_(isolate), builder_(isolate), gap_(nullptr), indent_(0) {
|
||||
tojson_string_ = factory()->toJSON_string();
|
||||
stack_ = factory()->NewJSArray(8);
|
||||
}
|
||||
|
||||
MaybeHandle<Object> JsonStringifier::Stringify(Handle<Object> object,
|
||||
Handle<Object> replacer,
|
||||
Handle<Object> gap) {
|
||||
MaybeHandle<Object> BasicJsonStringifier::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,9 +108,8 @@ bool IsInList(Handle<String> key, List<Handle<String> >* list) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) {
|
||||
bool BasicJsonStringifier::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()) {
|
||||
@ -145,13 +144,11 @@ bool JsonStringifier::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 JsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
bool BasicJsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
DCHECK_NULL(gap_);
|
||||
HandleScope scope(isolate_);
|
||||
if (gap->IsJSValue()) {
|
||||
@ -191,9 +188,49 @@ bool JsonStringifier::InitializeGap(Handle<Object> gap) {
|
||||
return true;
|
||||
}
|
||||
|
||||
MaybeHandle<Object> JsonStringifier::ApplyToJsonFunction(Handle<Object> object,
|
||||
Handle<Object> key) {
|
||||
HandleScope scope(isolate_);
|
||||
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) {
|
||||
LookupIterator it(object, tojson_string_,
|
||||
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
|
||||
Handle<Object> fun;
|
||||
@ -203,39 +240,15 @@ MaybeHandle<Object> JsonStringifier::ApplyToJsonFunction(Handle<Object> object,
|
||||
// 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);
|
||||
}
|
||||
|
||||
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) {
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
|
||||
Handle<Object> object) {
|
||||
StackLimitCheck check(isolate_);
|
||||
if (check.HasOverflowed()) {
|
||||
isolate_->StackOverflow();
|
||||
@ -261,23 +274,18 @@ JsonStringifier::Result JsonStringifier::StackPush(Handle<Object> object) {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void JsonStringifier::StackPop() {
|
||||
void BasicJsonStringifier::StackPop() {
|
||||
int length = Smi::cast(stack_->length())->value();
|
||||
stack_->set_length(Smi::FromInt(length - 1));
|
||||
}
|
||||
|
||||
template <bool deferred_string_key>
|
||||
JsonStringifier::Result JsonStringifier::Serialize_(Handle<Object> object,
|
||||
bool comma,
|
||||
Handle<Object> key) {
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::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);
|
||||
@ -336,7 +344,7 @@ JsonStringifier::Result JsonStringifier::Serialize_(Handle<Object> object,
|
||||
return UNCHANGED;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSValue(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue(
|
||||
Handle<JSValue> object) {
|
||||
String* class_name = object->class_name();
|
||||
if (class_name == isolate_->heap()->String_string()) {
|
||||
@ -361,7 +369,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSValue(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeSmi(Smi* object) {
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
|
||||
static const int kBufferSize = 100;
|
||||
char chars[kBufferSize];
|
||||
Vector<char> buffer(chars, kBufferSize);
|
||||
@ -369,7 +377,8 @@ JsonStringifier::Result JsonStringifier::SerializeSmi(Smi* object) {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeDouble(double number) {
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
|
||||
double number) {
|
||||
if (std::isinf(number) || std::isnan(number)) {
|
||||
builder_.AppendCString("null");
|
||||
return SUCCESS;
|
||||
@ -381,7 +390,7 @@ JsonStringifier::Result JsonStringifier::SerializeDouble(double number) {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSArray(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
|
||||
Handle<JSArray> object) {
|
||||
HandleScope handle_scope(isolate_);
|
||||
Result stack_push = StackPush(object);
|
||||
@ -391,64 +400,58 @@ JsonStringifier::Result JsonStringifier::SerializeJSArray(
|
||||
DCHECK(!object->IsAccessCheckNeeded());
|
||||
builder_.AppendCharacter('[');
|
||||
Indent();
|
||||
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;
|
||||
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)));
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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();
|
||||
@ -457,7 +460,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSArray(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeArrayLikeSlow(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeArrayLikeSlow(
|
||||
Handle<JSReceiver> object, uint32_t start, uint32_t length) {
|
||||
for (uint32_t i = start; i < length; i++) {
|
||||
Separator(i == 0);
|
||||
@ -465,18 +468,22 @@ JsonStringifier::Result JsonStringifier::SerializeArrayLikeSlow(
|
||||
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
||||
isolate_, element, JSReceiver::GetElement(isolate_, object, i),
|
||||
EXCEPTION);
|
||||
Result result = SerializeElement(isolate_, element, i);
|
||||
if (result == SUCCESS) continue;
|
||||
if (result == UNCHANGED) {
|
||||
if (element->IsUndefined()) {
|
||||
builder_.AppendCString("null");
|
||||
} else {
|
||||
return result;
|
||||
Result result = SerializeElement(isolate_, element, i);
|
||||
if (result == SUCCESS) continue;
|
||||
if (result == UNCHANGED) {
|
||||
builder_.AppendCString("null");
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSObject(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
|
||||
Handle<JSObject> object) {
|
||||
HandleScope handle_scope(isolate_);
|
||||
Result stack_push = StackPush(object);
|
||||
@ -531,7 +538,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSObject(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSReceiverSlow(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSReceiverSlow(
|
||||
Handle<JSReceiver> object) {
|
||||
Handle<FixedArray> contents = property_list_;
|
||||
if (contents.is_null()) {
|
||||
@ -573,7 +580,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSReceiverSlow(
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
JsonStringifier::Result JsonStringifier::SerializeJSProxy(
|
||||
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy(
|
||||
Handle<JSProxy> object) {
|
||||
Result stack_push = StackPush(object);
|
||||
if (stack_push != SUCCESS) return stack_push;
|
||||
@ -608,7 +615,7 @@ JsonStringifier::Result JsonStringifier::SerializeJSProxy(
|
||||
}
|
||||
|
||||
template <typename SrcChar, typename DestChar>
|
||||
void JsonStringifier::SerializeStringUnchecked_(
|
||||
void BasicJsonStringifier::SerializeStringUnchecked_(
|
||||
Vector<const SrcChar> src,
|
||||
IncrementalStringBuilder::NoExtend<DestChar>* dest) {
|
||||
// Assert that uc16 character is not truncated down to 8 bit.
|
||||
@ -626,7 +633,7 @@ void JsonStringifier::SerializeStringUnchecked_(
|
||||
}
|
||||
|
||||
template <typename SrcChar, typename DestChar>
|
||||
void JsonStringifier::SerializeString_(Handle<String> string) {
|
||||
void BasicJsonStringifier::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
|
||||
@ -656,35 +663,35 @@ void JsonStringifier::SerializeString_(Handle<String> string) {
|
||||
}
|
||||
|
||||
template <>
|
||||
bool JsonStringifier::DoNotEscape(uint8_t c) {
|
||||
bool BasicJsonStringifier::DoNotEscape(uint8_t c) {
|
||||
return c >= '#' && c <= '~' && c != '\\';
|
||||
}
|
||||
|
||||
template <>
|
||||
bool JsonStringifier::DoNotEscape(uint16_t c) {
|
||||
bool BasicJsonStringifier::DoNotEscape(uint16_t c) {
|
||||
return c >= '#' && c != '\\' && c != 0x7f;
|
||||
}
|
||||
|
||||
void JsonStringifier::NewLine() {
|
||||
void BasicJsonStringifier::NewLine() {
|
||||
if (gap_ == nullptr) return;
|
||||
builder_.AppendCharacter('\n');
|
||||
for (int i = 0; i < indent_; i++) builder_.AppendCString(gap_);
|
||||
}
|
||||
|
||||
void JsonStringifier::Separator(bool first) {
|
||||
void BasicJsonStringifier::Separator(bool first) {
|
||||
if (!first) builder_.AppendCharacter(',');
|
||||
NewLine();
|
||||
}
|
||||
|
||||
void JsonStringifier::SerializeDeferredKey(bool deferred_comma,
|
||||
Handle<Object> deferred_key) {
|
||||
void BasicJsonStringifier::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 JsonStringifier::SerializeString(Handle<String> object) {
|
||||
void BasicJsonStringifier::SerializeString(Handle<String> object) {
|
||||
object = String::Flatten(object);
|
||||
if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) {
|
||||
if (object->IsOneByteRepresentationUnderneath()) {
|
||||
|
@ -11,16 +11,19 @@
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
class JsonStringifier BASE_EMBEDDED {
|
||||
class BasicJsonStringifier BASE_EMBEDDED {
|
||||
public:
|
||||
explicit JsonStringifier(Isolate* isolate);
|
||||
explicit BasicJsonStringifier(Isolate* isolate);
|
||||
|
||||
~JsonStringifier() { DeleteArray(gap_); }
|
||||
~BasicJsonStringifier() { 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 };
|
||||
|
||||
@ -30,8 +33,6 @@ class JsonStringifier 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)) {
|
||||
@ -99,8 +100,6 @@ class JsonStringifier BASE_EMBEDDED {
|
||||
INLINE(void Unindent() { indent_--; });
|
||||
INLINE(void Separator(bool first));
|
||||
|
||||
Handle<JSReceiver> CurrentHolder(Handle<Object> value);
|
||||
|
||||
Result StackPush(Handle<Object> object);
|
||||
void StackPop();
|
||||
|
||||
@ -111,7 +110,6 @@ class JsonStringifier BASE_EMBEDDED {
|
||||
Handle<String> tojson_string_;
|
||||
Handle<JSArray> stack_;
|
||||
Handle<FixedArray> property_list_;
|
||||
Handle<JSReceiver> replacer_function_;
|
||||
uc16* gap_;
|
||||
int indent_;
|
||||
|
||||
|
@ -14,6 +14,24 @@
|
||||
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,6 +327,8 @@ 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) \
|
||||
|
Loading…
Reference in New Issue
Block a user