• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/builtins/builtins-utils.h"
6 #include "src/builtins/builtins.h"
7 #include "src/code-stub-assembler.h"
8 #include "src/counters.h"
9 #include "src/objects-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // -----------------------------------------------------------------------------
15 // ES6 section 19.4 Symbol Objects
16 
17 // ES6 section 19.4.1.1 Symbol ( [ description ] ) for the [[Call]] case.
BUILTIN(SymbolConstructor)18 BUILTIN(SymbolConstructor) {
19   HandleScope scope(isolate);
20   Handle<Symbol> result = isolate->factory()->NewSymbol();
21   Handle<Object> description = args.atOrUndefined(isolate, 1);
22   if (!description->IsUndefined(isolate)) {
23     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, description,
24                                        Object::ToString(isolate, description));
25     result->set_name(*description);
26   }
27   return *result;
28 }
29 
30 // ES6 section 19.4.1.1 Symbol ( [ description ] ) for the [[Construct]] case.
BUILTIN(SymbolConstructor_ConstructStub)31 BUILTIN(SymbolConstructor_ConstructStub) {
32   HandleScope scope(isolate);
33   THROW_NEW_ERROR_RETURN_FAILURE(
34       isolate, NewTypeError(MessageTemplate::kNotConstructor,
35                             isolate->factory()->Symbol_string()));
36 }
37 
38 // ES6 section 19.4.2.1 Symbol.for.
BUILTIN(SymbolFor)39 BUILTIN(SymbolFor) {
40   HandleScope scope(isolate);
41   Handle<Object> key_obj = args.atOrUndefined(isolate, 1);
42   Handle<String> key;
43   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key,
44                                      Object::ToString(isolate, key_obj));
45   return *isolate->SymbolFor(Heap::kPublicSymbolTableRootIndex, key, false);
46 }
47 
48 // ES6 section 19.4.2.5 Symbol.keyFor.
BUILTIN(SymbolKeyFor)49 BUILTIN(SymbolKeyFor) {
50   HandleScope scope(isolate);
51   Handle<Object> obj = args.atOrUndefined(isolate, 1);
52   if (!obj->IsSymbol()) {
53     THROW_NEW_ERROR_RETURN_FAILURE(
54         isolate, NewTypeError(MessageTemplate::kSymbolKeyFor, obj));
55   }
56   Handle<Symbol> symbol = Handle<Symbol>::cast(obj);
57   DisallowHeapAllocation no_gc;
58   Object* result;
59   if (symbol->is_public()) {
60     result = symbol->name();
61     DCHECK(result->IsString());
62   } else {
63     result = isolate->heap()->undefined_value();
64   }
65   DCHECK_EQ(isolate->heap()->public_symbol_table()->SlowReverseLookup(*symbol),
66             result);
67   return result;
68 }
69 
70 // ES6 section 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint )
Generate_SymbolPrototypeToPrimitive(compiler::CodeAssemblerState * state)71 void Builtins::Generate_SymbolPrototypeToPrimitive(
72     compiler::CodeAssemblerState* state) {
73   typedef compiler::Node Node;
74   CodeStubAssembler assembler(state);
75 
76   Node* receiver = assembler.Parameter(0);
77   Node* context = assembler.Parameter(4);
78 
79   Node* result =
80       assembler.ToThisValue(context, receiver, PrimitiveType::kSymbol,
81                             "Symbol.prototype [ @@toPrimitive ]");
82   assembler.Return(result);
83 }
84 
85 // ES6 section 19.4.3.2 Symbol.prototype.toString ( )
Generate_SymbolPrototypeToString(compiler::CodeAssemblerState * state)86 void Builtins::Generate_SymbolPrototypeToString(
87     compiler::CodeAssemblerState* state) {
88   typedef compiler::Node Node;
89   CodeStubAssembler assembler(state);
90 
91   Node* receiver = assembler.Parameter(0);
92   Node* context = assembler.Parameter(3);
93 
94   Node* value = assembler.ToThisValue(context, receiver, PrimitiveType::kSymbol,
95                                       "Symbol.prototype.toString");
96   Node* result =
97       assembler.CallRuntime(Runtime::kSymbolDescriptiveString, context, value);
98   assembler.Return(result);
99 }
100 
101 // ES6 section 19.4.3.3 Symbol.prototype.valueOf ( )
Generate_SymbolPrototypeValueOf(compiler::CodeAssemblerState * state)102 void Builtins::Generate_SymbolPrototypeValueOf(
103     compiler::CodeAssemblerState* state) {
104   typedef compiler::Node Node;
105   CodeStubAssembler assembler(state);
106 
107   Node* receiver = assembler.Parameter(0);
108   Node* context = assembler.Parameter(3);
109 
110   Node* result = assembler.ToThisValue(
111       context, receiver, PrimitiveType::kSymbol, "Symbol.prototype.valueOf");
112   assembler.Return(result);
113 }
114 
115 }  // namespace internal
116 }  // namespace v8
117