From 04087a7e45648a41b791e335ca852e53676f11f8 Mon Sep 17 00:00:00 2001 From: bmeurer Date: Wed, 16 Sep 2015 04:35:15 -0700 Subject: [PATCH] [builtins] Also simplify the Symbol constructor. No need to rely on the %_IsConstructCall magic here, we can just implement the Symbol constructor in C++ altogether (it was just a stupid wrapper around %CreateSymbol anyway). R=mstarzinger@chromium.org Review URL: https://codereview.chromium.org/1349643002 Cr-Commit-Position: refs/heads/master@{#30762} --- src/bootstrapper.cc | 6 +++++- src/builtins.cc | 30 +++++++++++++++++++++++++----- src/builtins.h | 3 +++ src/symbol.js | 10 ---------- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc index 0346c8c..4505102 100644 --- a/src/bootstrapper.cc +++ b/src/bootstrapper.cc @@ -1147,7 +1147,11 @@ void Genesis::InitializeGlobal(Handle global_object, // --- S y m b o l --- Handle symbol_fun = InstallFunction( global, "Symbol", JS_VALUE_TYPE, JSValue::kSize, - isolate->initial_object_prototype(), Builtins::kIllegal); + isolate->initial_object_prototype(), Builtins::kSymbolConstructor); + symbol_fun->shared()->set_construct_stub(isolate->builtins()->builtin( + Builtins::kSymbolConstructor_ConstructStub)); + symbol_fun->shared()->set_internal_formal_parameter_count(1); + symbol_fun->shared()->set_length(1); native_context()->set_symbol_function(*symbol_fun); } diff --git a/src/builtins.cc b/src/builtins.cc index ac6c9fa..87f1037 100644 --- a/src/builtins.cc +++ b/src/builtins.cc @@ -1445,11 +1445,7 @@ BUILTIN(ArrayConcat) { } -// ----------------------------------------------------------------------------- -// - - -// 20.3.4.45 Date.prototype [ @@toPrimitive ] ( hint ) +// ES6 section 20.3.4.45 Date.prototype [ @@toPrimitive ] ( hint ) BUILTIN(DateToPrimitive) { HandleScope scope(isolate); DCHECK_EQ(2, args.length()); @@ -1469,6 +1465,30 @@ BUILTIN(DateToPrimitive) { } +// ES6 section 19.4.1.1 Symbol ( [ description ] ) for the [[Call]] case. +BUILTIN(SymbolConstructor) { + HandleScope scope(isolate); + DCHECK_EQ(2, args.length()); + Handle result = isolate->factory()->NewSymbol(); + Handle description = args.at(1); + if (!description->IsUndefined()) { + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, description, + Object::ToString(isolate, description)); + result->set_name(*description); + } + return *result; +} + + +// ES6 section 19.4.1.1 Symbol ( [ description ] ) for the [[Construct]] case. +BUILTIN(SymbolConstructor_ConstructStub) { + HandleScope scope(isolate); + THROW_NEW_ERROR_RETURN_FAILURE( + isolate, NewTypeError(MessageTemplate::kNotConstructor, + isolate->factory()->Symbol_string())); +} + + // ----------------------------------------------------------------------------- // Throwers for restricted function properties and strict arguments object // properties diff --git a/src/builtins.h b/src/builtins.h index aff47f2..f2b6b1e 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -59,6 +59,9 @@ enum BuiltinExtraArguments { \ V(DateToPrimitive, NO_EXTRA_ARGUMENTS) \ \ + V(SymbolConstructor, NO_EXTRA_ARGUMENTS) \ + V(SymbolConstructor_ConstructStub, NO_EXTRA_ARGUMENTS) \ + \ V(HandleApiCall, NEEDS_CALLED_FUNCTION) \ V(HandleApiCallConstruct, NEEDS_CALLED_FUNCTION) \ V(HandleApiCallAsFunction, NO_EXTRA_ARGUMENTS) \ diff --git a/src/symbol.js b/src/symbol.js index c65cda7..600d4fd 100644 --- a/src/symbol.js +++ b/src/symbol.js @@ -20,24 +20,15 @@ var isRegExpSymbol = utils.ImportNow("is_regexp_symbol"); var iteratorSymbol = utils.ImportNow("iterator_symbol"); var ObjectGetOwnPropertyKeys; var toPrimitiveSymbol = utils.ImportNow("to_primitive_symbol"); -var ToString; var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); var unscopablesSymbol = utils.ImportNow("unscopables_symbol"); utils.Import(function(from) { ObjectGetOwnPropertyKeys = from.ObjectGetOwnPropertyKeys; - ToString = from.ToString; }); // ------------------------------------------------------------------- -function SymbolConstructor(x) { - if (%_IsConstructCall()) throw MakeTypeError(kNotConstructor, "Symbol"); - // NOTE: Passing in a Symbol value will throw on ToString(). - return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); -} - - // 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint ) function SymbolToPrimitive(hint) { if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { @@ -95,7 +86,6 @@ function ObjectGetOwnPropertySymbols(obj) { // ------------------------------------------------------------------- -%SetCode(GlobalSymbol, SymbolConstructor); %FunctionSetPrototype(GlobalSymbol, new GlobalObject()); utils.InstallConstants(GlobalSymbol, [ -- 2.7.4