Commit 60afed46 authored by yangguo's avatar yangguo Committed by Commit bot

[json] replace remaining json.js code with C++ builtins.

R=bmeurer@chromium.org

Review-Url: https://codereview.chromium.org/2026643003
Cr-Commit-Position: refs/heads/master@{#36610}
parent a43b732e
...@@ -359,7 +359,6 @@ action("js2c") { ...@@ -359,7 +359,6 @@ action("js2c") {
"src/js/collection-iterator.js", "src/js/collection-iterator.js",
"src/js/promise.js", "src/js/promise.js",
"src/js/messages.js", "src/js/messages.js",
"src/js/json.js",
"src/js/array-iterator.js", "src/js/array-iterator.js",
"src/js/string-iterator.js", "src/js/string-iterator.js",
"src/js/templates.js", "src/js/templates.js",
...@@ -1389,7 +1388,6 @@ v8_source_set("v8_base") { ...@@ -1389,7 +1388,6 @@ v8_source_set("v8_base") {
"src/runtime/runtime-i18n.cc", "src/runtime/runtime-i18n.cc",
"src/runtime/runtime-internal.cc", "src/runtime/runtime-internal.cc",
"src/runtime/runtime-interpreter.cc", "src/runtime/runtime-interpreter.cc",
"src/runtime/runtime-json.cc",
"src/runtime/runtime-literals.cc", "src/runtime/runtime-literals.cc",
"src/runtime/runtime-liveedit.cc", "src/runtime/runtime-liveedit.cc",
"src/runtime/runtime-maths.cc", "src/runtime/runtime-maths.cc",
......
...@@ -1475,6 +1475,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -1475,6 +1475,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
0, true); 0, true);
SimpleInstallFunction(prototype, "setYear", Builtins::kDatePrototypeSetYear, SimpleInstallFunction(prototype, "setYear", Builtins::kDatePrototypeSetYear,
1, false); 1, false);
SimpleInstallFunction(prototype, "toJSON", Builtins::kDatePrototypeToJson,
1, false);
// Install i18n fallback functions. // Install i18n fallback functions.
SimpleInstallFunction(prototype, "toLocaleString", SimpleInstallFunction(prototype, "toLocaleString",
...@@ -1601,8 +1603,13 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -1601,8 +1603,13 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED); Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
DCHECK(json_object->IsJSObject()); DCHECK(json_object->IsJSObject());
JSObject::AddProperty(global, name, json_object, DONT_ENUM); JSObject::AddProperty(global, name, json_object, DONT_ENUM);
SimpleInstallFunction(json_object, "parse", Builtins::kJsonParse, 2, true);
SimpleInstallFunction(json_object, "stringify", Builtins::kJsonStringify, 3, SimpleInstallFunction(json_object, "stringify", Builtins::kJsonStringify, 3,
true); true);
JSObject::AddProperty(
json_object, factory->to_string_tag_symbol(),
factory->NewStringFromAsciiChecked("JSON"),
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
} }
{ // -- M a t h { // -- M a t h
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "src/ic/handler-compiler.h" #include "src/ic/handler-compiler.h"
#include "src/ic/ic.h" #include "src/ic/ic.h"
#include "src/isolate-inl.h" #include "src/isolate-inl.h"
#include "src/json-parser.h"
#include "src/json-stringifier.h" #include "src/json-stringifier.h"
#include "src/messages.h" #include "src/messages.h"
#include "src/profiler/cpu-profiler.h" #include "src/profiler/cpu-profiler.h"
...@@ -2220,6 +2221,20 @@ BUILTIN(GlobalEval) { ...@@ -2220,6 +2221,20 @@ BUILTIN(GlobalEval) {
Execution::Call(isolate, function, target_global_proxy, 0, nullptr)); Execution::Call(isolate, function, target_global_proxy, 0, nullptr));
} }
// ES6 section 24.3.1 JSON.parse.
BUILTIN(JsonParse) {
HandleScope scope(isolate);
Handle<Object> source = args.atOrUndefined(isolate, 1);
Handle<Object> reviver = args.atOrUndefined(isolate, 2);
Handle<String> string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
Object::ToString(isolate, source));
RETURN_RESULT_OR_FAILURE(
isolate, string->IsSeqOneByteString()
? JsonParser<true>::Parse(isolate, string, reviver)
: JsonParser<false>::Parse(isolate, string, reviver));
}
// ES6 section 24.3.2 JSON.stringify. // ES6 section 24.3.2 JSON.stringify.
BUILTIN(JsonStringify) { BUILTIN(JsonStringify) {
HandleScope scope(isolate); HandleScope scope(isolate);
...@@ -3927,6 +3942,33 @@ BUILTIN(DatePrototypeSetYear) { ...@@ -3927,6 +3942,33 @@ BUILTIN(DatePrototypeSetYear) {
return SetLocalDateValue(date, time_val); return SetLocalDateValue(date, time_val);
} }
// ES6 section 20.3.4.37 Date.prototype.toJSON ( key )
BUILTIN(DatePrototypeToJson) {
HandleScope scope(isolate);
Handle<Object> receiver = args.atOrUndefined(isolate, 0);
Handle<JSReceiver> receiver_obj;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver_obj,
Object::ToObject(isolate, receiver));
Handle<Object> primitive;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, primitive,
Object::ToPrimitive(receiver_obj, ToPrimitiveHint::kNumber));
if (primitive->IsNumber() && !std::isfinite(primitive->Number())) {
return isolate->heap()->null_value();
} else {
Handle<String> name =
isolate->factory()->NewStringFromAsciiChecked("toISOString");
Handle<Object> function;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, function,
Object::GetProperty(receiver_obj, name));
if (!function->IsCallable()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kCalledNonCallable, name));
}
RETURN_RESULT_OR_FAILURE(
isolate, Execution::Call(isolate, function, receiver_obj, 0, NULL));
}
}
// static // static
void Builtins::Generate_DatePrototypeGetDate(MacroAssembler* masm) { void Builtins::Generate_DatePrototypeGetDate(MacroAssembler* masm) {
......
...@@ -108,6 +108,7 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) { ...@@ -108,6 +108,7 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) {
V(DatePrototypeValueOf, kNone) \ V(DatePrototypeValueOf, kNone) \
V(DatePrototypeGetYear, kNone) \ V(DatePrototypeGetYear, kNone) \
V(DatePrototypeSetYear, kNone) \ V(DatePrototypeSetYear, kNone) \
V(DatePrototypeToJson, kNone) \
\ \
V(FunctionConstructor, kTargetAndNewTarget) \ V(FunctionConstructor, kTargetAndNewTarget) \
V(FunctionPrototypeBind, kNone) \ V(FunctionPrototypeBind, kNone) \
...@@ -123,6 +124,7 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) { ...@@ -123,6 +124,7 @@ inline bool operator&(BuiltinExtraArguments lhs, BuiltinExtraArguments rhs) {
\ \
V(GlobalEval, kTarget) \ V(GlobalEval, kTarget) \
\ \
V(JsonParse, kNone) \
V(JsonStringify, kNone) \ V(JsonStringify, kNone) \
\ \
V(MathAcos, kNone) \ V(MathAcos, kNone) \
......
// Copyright 2009 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(function(global, utils) {
"use strict";
%CheckIsBootstrapping();
// -------------------------------------------------------------------
// Imports
var GlobalDate = global.Date;
var GlobalJSON = global.JSON;
var GlobalSet = global.Set;
var InternalArray = utils.InternalArray;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
// -------------------------------------------------------------------
function JSONParse(text, reviver) {
return %ParseJson(text, reviver);
}
// -------------------------------------------------------------------
%AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM);
// Set up non-enumerable properties of the JSON object.
utils.InstallFunctions(GlobalJSON, DONT_ENUM, [
"parse", JSONParse,
]);
// -------------------------------------------------------------------
// Date.toJSON
// 20.3.4.37 Date.prototype.toJSON ( key )
function DateToJSON(key) {
var o = TO_OBJECT(this);
var tv = TO_PRIMITIVE_NUMBER(o);
if (IS_NUMBER(tv) && !NUMBER_IS_FINITE(tv)) {
return null;
}
return o.toISOString();
}
// Set up non-enumerable functions of the Date prototype object.
utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [
"toJSON", DateToJSON
]);
})
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/runtime/runtime-utils.h"
#include "src/arguments.h"
#include "src/char-predicates-inl.h"
#include "src/isolate-inl.h"
#include "src/json-parser.h"
#include "src/json-stringifier.h"
#include "src/objects-inl.h"
namespace v8 {
namespace internal {
RUNTIME_FUNCTION(Runtime_ParseJson) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
CONVERT_ARG_HANDLE_CHECKED(Object, reviver, 1);
Handle<String> source;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, source,
Object::ToString(isolate, object));
source = String::Flatten(source);
// Optimized fast case where we only have Latin1 characters.
RETURN_RESULT_OR_FAILURE(
isolate, source->IsSeqOneByteString()
? JsonParser<true>::Parse(isolate, source, reviver)
: JsonParser<false>::Parse(isolate, source, reviver));
}
} // namespace internal
} // namespace v8
...@@ -327,8 +327,6 @@ namespace internal { ...@@ -327,8 +327,6 @@ namespace internal {
F(OrdinaryHasInstance, 2, 1) \ F(OrdinaryHasInstance, 2, 1) \
F(IsWasmObject, 1, 1) F(IsWasmObject, 1, 1)
#define FOR_EACH_INTRINSIC_JSON(F) F(ParseJson, 2, 1)
#define FOR_EACH_INTRINSIC_LITERALS(F) \ #define FOR_EACH_INTRINSIC_LITERALS(F) \
F(CreateRegExpLiteral, 4, 1) \ F(CreateRegExpLiteral, 4, 1) \
F(CreateObjectLiteral, 4, 1) \ F(CreateObjectLiteral, 4, 1) \
...@@ -989,7 +987,6 @@ namespace internal { ...@@ -989,7 +987,6 @@ namespace internal {
FOR_EACH_INTRINSIC_GENERATOR(F) \ FOR_EACH_INTRINSIC_GENERATOR(F) \
FOR_EACH_INTRINSIC_I18N(F) \ FOR_EACH_INTRINSIC_I18N(F) \
FOR_EACH_INTRINSIC_INTERNAL(F) \ FOR_EACH_INTRINSIC_INTERNAL(F) \
FOR_EACH_INTRINSIC_JSON(F) \
FOR_EACH_INTRINSIC_LITERALS(F) \ FOR_EACH_INTRINSIC_LITERALS(F) \
FOR_EACH_INTRINSIC_LIVEEDIT(F) \ FOR_EACH_INTRINSIC_LIVEEDIT(F) \
FOR_EACH_INTRINSIC_MATHS(F) \ FOR_EACH_INTRINSIC_MATHS(F) \
......
...@@ -1055,7 +1055,6 @@ ...@@ -1055,7 +1055,6 @@
'runtime/runtime-i18n.cc', 'runtime/runtime-i18n.cc',
'runtime/runtime-internal.cc', 'runtime/runtime-internal.cc',
'runtime/runtime-interpreter.cc', 'runtime/runtime-interpreter.cc',
'runtime/runtime-json.cc',
'runtime/runtime-literals.cc', 'runtime/runtime-literals.cc',
'runtime/runtime-liveedit.cc', 'runtime/runtime-liveedit.cc',
'runtime/runtime-maths.cc', 'runtime/runtime-maths.cc',
...@@ -2062,7 +2061,6 @@ ...@@ -2062,7 +2061,6 @@
'js/collection-iterator.js', 'js/collection-iterator.js',
'js/promise.js', 'js/promise.js',
'js/messages.js', 'js/messages.js',
'js/json.js',
'js/array-iterator.js', 'js/array-iterator.js',
'js/string-iterator.js', 'js/string-iterator.js',
'js/templates.js', 'js/templates.js',
......
...@@ -84,11 +84,6 @@ var math_script = Debug.findScript('native math.js'); ...@@ -84,11 +84,6 @@ var math_script = Debug.findScript('native math.js');
assertEquals('native math.js', math_script.name); assertEquals('native math.js', math_script.name);
assertEquals(Debug.ScriptType.Native, math_script.type); assertEquals(Debug.ScriptType.Native, math_script.type);
// Test a builtins delay loaded script.
var date_delay_script = Debug.findScript('native json.js');
assertEquals('native json.js', date_delay_script.name);
assertEquals(Debug.ScriptType.Native, date_delay_script.type);
// Test a debugger script. // Test a debugger script.
var debug_delay_script = Debug.findScript('native debug.js'); var debug_delay_script = Debug.findScript('native debug.js');
assertEquals('native debug.js', debug_delay_script.name); assertEquals('native debug.js', debug_delay_script.name);
......
...@@ -83,7 +83,7 @@ PASS tests[i](nativeJSON) is tests[i](JSON) ...@@ -83,7 +83,7 @@ PASS tests[i](nativeJSON) is tests[i](JSON)
function (jsonObject){ function (jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON}); return jsonObject.stringify({toJSON: Date.prototype.toJSON});
} }
PASS tests[i](nativeJSON) threw exception TypeError: (var).toISOString is not a function. PASS tests[i](nativeJSON) threw exception TypeError: toISOString is not a function.
function (jsonObject){ function (jsonObject){
return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }}); return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }});
} }
...@@ -101,7 +101,7 @@ function (jsonObject){ ...@@ -101,7 +101,7 @@ function (jsonObject){
d.toISOString = null; d.toISOString = null;
return jsonObject.stringify(d); return jsonObject.stringify(d);
} }
PASS tests[i](nativeJSON) threw exception TypeError: (var).toISOString is not a function. PASS tests[i](nativeJSON) threw exception TypeError: toISOString is not a function.
function (jsonObject){ function (jsonObject){
var d = new Date(0); var d = new Date(0);
d.toJSON = undefined; d.toJSON = undefined;
......
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