Commit 7012babb authored by yangguo's avatar yangguo Committed by Commit bot

Revert of [json] support replacer function in BasicJsonStringifier. (patchset...

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}
parent dd609a5d
......@@ -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([
......
......@@ -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
]);
// -------------------------------------------------------------------
......
This diff is collapsed.
......@@ -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) \
......
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