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