Commit 1df70e27 authored by yangguo's avatar yangguo Committed by Commit bot

Wrap JSON and generator implementation in functions.

R=jkummerow@chromium.org

Review URL: https://codereview.chromium.org/1097703002

Cr-Commit-Position: refs/heads/master@{#27918}
parent 5a9a0b20
...@@ -2,14 +2,15 @@ ...@@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
(function() {
"use strict"; "use strict";
// This file relies on the fact that the following declarations have been made %CheckIsBootstrapping();
// in runtime.js:
// var $Function = global.Function;
// ---------------------------------------------------------------------------- var GlobalFunction = global.Function;
// ----------------------------------------------------------------------------
// Generator functions and objects are specified by ES6, sections 15.19.3 and // Generator functions and objects are specified by ES6, sections 15.19.3 and
// 15.19.4. // 15.19.4.
...@@ -39,6 +40,7 @@ function GeneratorObjectNext(value) { ...@@ -39,6 +40,7 @@ function GeneratorObjectNext(value) {
} }
} }
function GeneratorObjectThrow(exn) { function GeneratorObjectThrow(exn) {
if (!IS_GENERATOR(this)) { if (!IS_GENERATOR(this)) {
throw MakeTypeError(kIncompatibleMethodReceiver, throw MakeTypeError(kIncompatibleMethodReceiver,
...@@ -63,10 +65,12 @@ function GeneratorObjectThrow(exn) { ...@@ -63,10 +65,12 @@ function GeneratorObjectThrow(exn) {
} }
} }
function GeneratorObjectIterator() { function GeneratorObjectIterator() {
return this; return this;
} }
function GeneratorFunctionConstructor(arg1) { // length == 1 function GeneratorFunctionConstructor(arg1) { // length == 1
var source = NewFunctionString(arguments, 'function*'); var source = NewFunctionString(arguments, 'function*');
var global_proxy = %GlobalProxy(global); var global_proxy = %GlobalProxy(global);
...@@ -77,36 +81,33 @@ function GeneratorFunctionConstructor(arg1) { // length == 1 ...@@ -77,36 +81,33 @@ function GeneratorFunctionConstructor(arg1) { // length == 1
return f; return f;
} }
// ----------------------------------------------------------------------------
function SetUpGenerators() { // Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by
%CheckIsBootstrapping(); // neither Crankshaft nor TurboFan, disable optimization of wrappers here.
%NeverOptimizeFunction(GeneratorObjectNext);
// Both Runtime_GeneratorNext and Runtime_GeneratorThrow are supported by %NeverOptimizeFunction(GeneratorObjectThrow);
// neither Crankshaft nor TurboFan, disable optimization of wrappers here.
%NeverOptimizeFunction(GeneratorObjectNext);
%NeverOptimizeFunction(GeneratorObjectThrow);
// Set up non-enumerable functions on the generator prototype object. // Set up non-enumerable functions on the generator prototype object.
var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
InstallFunctions(GeneratorObjectPrototype, InstallFunctions(GeneratorObjectPrototype,
DONT_ENUM, DONT_ENUM,
["next", GeneratorObjectNext, ["next", GeneratorObjectNext,
"throw", GeneratorObjectThrow]); "throw", GeneratorObjectThrow]);
%FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]'); %FunctionSetName(GeneratorObjectIterator, '[Symbol.iterator]');
%AddNamedProperty(GeneratorObjectPrototype, symbolIterator, %AddNamedProperty(GeneratorObjectPrototype, symbolIterator,
GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY); GeneratorObjectIterator, DONT_ENUM | DONT_DELETE | READ_ONLY);
%AddNamedProperty(GeneratorObjectPrototype, "constructor", %AddNamedProperty(GeneratorObjectPrototype, "constructor",
GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY); GeneratorFunctionPrototype, DONT_ENUM | READ_ONLY);
%AddNamedProperty(GeneratorObjectPrototype, %AddNamedProperty(GeneratorObjectPrototype,
symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY); symbolToStringTag, "Generator", DONT_ENUM | READ_ONLY);
%InternalSetPrototype(GeneratorFunctionPrototype, $Function.prototype); %InternalSetPrototype(GeneratorFunctionPrototype, GlobalFunction.prototype);
%AddNamedProperty(GeneratorFunctionPrototype, %AddNamedProperty(GeneratorFunctionPrototype,
symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY); symbolToStringTag, "GeneratorFunction", DONT_ENUM | READ_ONLY);
%AddNamedProperty(GeneratorFunctionPrototype, "constructor", %AddNamedProperty(GeneratorFunctionPrototype, "constructor",
GeneratorFunction, DONT_ENUM | READ_ONLY); GeneratorFunction, DONT_ENUM | READ_ONLY);
%InternalSetPrototype(GeneratorFunction, $Function); %InternalSetPrototype(GeneratorFunction, GlobalFunction);
%SetCode(GeneratorFunction, GeneratorFunctionConstructor); %SetCode(GeneratorFunction, GeneratorFunctionConstructor);
}
SetUpGenerators(); })();
...@@ -361,8 +361,9 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric( ...@@ -361,8 +361,9 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric(
bool deferred_comma, bool deferred_comma,
bool deferred_key) { bool deferred_key) {
Handle<JSObject> builtins(isolate_->native_context()->builtins(), isolate_); Handle<JSObject> builtins(isolate_->native_context()->builtins(), isolate_);
Handle<JSFunction> builtin = Handle<JSFunction>::cast(Object::GetProperty( Handle<JSFunction> builtin = Handle<JSFunction>::cast(
isolate_, builtins, "JSONSerializeAdapter").ToHandleChecked()); Object::GetProperty(isolate_, builtins, "$jsonSerializeAdapter")
.ToHandleChecked());
Handle<Object> argv[] = { key, object }; Handle<Object> argv[] = { key, object };
Handle<Object> result; Handle<Object> result;
......
...@@ -2,14 +2,15 @@ ...@@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var $jsonSerializeAdapter;
(function() {
"use strict"; "use strict";
// This file relies on the fact that the following declarations have been made %CheckIsBootstrapping();
// in runtime.js:
// var $Array = global.Array;
// var $String = global.String;
var $JSON = global.JSON; var GlobalJSON = global.JSON;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
...@@ -19,7 +20,7 @@ function Revive(holder, name, reviver) { ...@@ -19,7 +20,7 @@ function Revive(holder, name, reviver) {
if (IS_ARRAY(val)) { if (IS_ARRAY(val)) {
var length = val.length; var length = val.length;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var newElement = Revive(val, $String(i), reviver); var newElement = Revive(val, %_NumberToString(i), reviver);
val[i] = newElement; val[i] = newElement;
} }
} else { } else {
...@@ -38,6 +39,7 @@ function Revive(holder, name, reviver) { ...@@ -38,6 +39,7 @@ function Revive(holder, name, reviver) {
return %_CallFunction(holder, name, val, reviver); return %_CallFunction(holder, name, val, reviver);
} }
function JSONParse(text, reviver) { function JSONParse(text, reviver) {
var unfiltered = %ParseJson(TO_STRING_INLINE(text)); var unfiltered = %ParseJson(TO_STRING_INLINE(text));
if (IS_SPEC_FUNCTION(reviver)) { if (IS_SPEC_FUNCTION(reviver)) {
...@@ -47,6 +49,7 @@ function JSONParse(text, reviver) { ...@@ -47,6 +49,7 @@ function JSONParse(text, reviver) {
} }
} }
function SerializeArray(value, replacer, stack, indent, gap) { function SerializeArray(value, replacer, stack, indent, gap) {
if (!%PushIfAbsent(stack, value)) { if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []); throw MakeTypeError('circular_structure', []);
...@@ -56,7 +59,7 @@ function SerializeArray(value, replacer, stack, indent, gap) { ...@@ -56,7 +59,7 @@ function SerializeArray(value, replacer, stack, indent, gap) {
var partial = new InternalArray(); var partial = new InternalArray();
var len = value.length; var len = value.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var strP = JSONSerialize($String(i), value, replacer, stack, var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack,
indent, gap); indent, gap);
if (IS_UNDEFINED(strP)) { if (IS_UNDEFINED(strP)) {
strP = "null"; strP = "null";
...@@ -77,6 +80,7 @@ function SerializeArray(value, replacer, stack, indent, gap) { ...@@ -77,6 +80,7 @@ function SerializeArray(value, replacer, stack, indent, gap) {
return final; return final;
} }
function SerializeObject(value, replacer, stack, indent, gap) { function SerializeObject(value, replacer, stack, indent, gap) {
if (!%PushIfAbsent(stack, value)) { if (!%PushIfAbsent(stack, value)) {
throw MakeTypeError('circular_structure', []); throw MakeTypeError('circular_structure', []);
...@@ -125,6 +129,7 @@ function SerializeObject(value, replacer, stack, indent, gap) { ...@@ -125,6 +129,7 @@ function SerializeObject(value, replacer, stack, indent, gap) {
return final; return final;
} }
function JSONSerialize(key, holder, replacer, stack, indent, gap) { function JSONSerialize(key, holder, replacer, stack, indent, gap) {
var value = holder[key]; var value = holder[key];
if (IS_SPEC_OBJECT(value)) { if (IS_SPEC_OBJECT(value)) {
...@@ -214,30 +219,24 @@ function JSONStringify(value, replacer, space) { ...@@ -214,30 +219,24 @@ function JSONStringify(value, replacer, space) {
return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap);
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
function SetUpJSON() { %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM);
%CheckIsBootstrapping();
%AddNamedProperty($JSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM);
// Set up non-enumerable properties of the JSON object. // Set up non-enumerable properties of the JSON object.
InstallFunctions($JSON, DONT_ENUM, [ InstallFunctions(GlobalJSON, DONT_ENUM, [
"parse", JSONParse, "parse", JSONParse,
"stringify", JSONStringify "stringify", JSONStringify
]); ]);
}
SetUpJSON();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// JSON Builtins // JSON Builtins
function JSONSerializeAdapter(key, object) { $jsonSerializeAdapter = function(key, object) {
var holder = {}; var holder = {};
holder[key] = object; holder[key] = object;
// No need to pass the actual holder since there is no replacer function. // No need to pass the actual holder since there is no replacer function.
return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", "");
} }
})();
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