[builtins] Also simplify the Symbol constructor.
authorbmeurer <bmeurer@chromium.org>
Wed, 16 Sep 2015 11:35:15 +0000 (04:35 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 16 Sep 2015 11:35:30 +0000 (11:35 +0000)
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
src/builtins.cc
src/builtins.h
src/symbol.js

index 0346c8c..4505102 100644 (file)
@@ -1147,7 +1147,11 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> global_object,
     // --- S y m b o l ---
     Handle<JSFunction> 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);
   }
 
index ac6c9fa..87f1037 100644 (file)
@@ -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<Symbol> result = isolate->factory()->NewSymbol();
+  Handle<Object> description = args.at<Object>(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
index aff47f2..f2b6b1e 100644 (file)
@@ -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)             \
index c65cda7..600d4fd 100644 (file)
@@ -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, [