• 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-inl.h"
6 #include "src/builtins/builtins.h"
7 #include "src/counters.h"
8 #include "src/objects-inl.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 // -----------------------------------------------------------------------------
14 // ES #sec-symbol-objects
15 
16 // ES #sec-symbol-constructor
BUILTIN(SymbolConstructor)17 BUILTIN(SymbolConstructor) {
18   HandleScope scope(isolate);
19   if (!args.new_target()->IsUndefined(isolate)) {  // [[Construct]]
20     THROW_NEW_ERROR_RETURN_FAILURE(
21         isolate, NewTypeError(MessageTemplate::kNotConstructor,
22                               isolate->factory()->Symbol_string()));
23   }
24   // [[Call]]
25   Handle<Symbol> result = isolate->factory()->NewSymbol();
26   Handle<Object> description = args.atOrUndefined(isolate, 1);
27   if (!description->IsUndefined(isolate)) {
28     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, description,
29                                        Object::ToString(isolate, description));
30     result->set_name(*description);
31   }
32   return *result;
33 }
34 
35 // ES6 section 19.4.2.1 Symbol.for.
BUILTIN(SymbolFor)36 BUILTIN(SymbolFor) {
37   HandleScope scope(isolate);
38   Handle<Object> key_obj = args.atOrUndefined(isolate, 1);
39   Handle<String> key;
40   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key,
41                                      Object::ToString(isolate, key_obj));
42   return *isolate->SymbolFor(Heap::kPublicSymbolTableRootIndex, key, false);
43 }
44 
45 // ES6 section 19.4.2.5 Symbol.keyFor.
BUILTIN(SymbolKeyFor)46 BUILTIN(SymbolKeyFor) {
47   HandleScope scope(isolate);
48   Handle<Object> obj = args.atOrUndefined(isolate, 1);
49   if (!obj->IsSymbol()) {
50     THROW_NEW_ERROR_RETURN_FAILURE(
51         isolate, NewTypeError(MessageTemplate::kSymbolKeyFor, obj));
52   }
53   Handle<Symbol> symbol = Handle<Symbol>::cast(obj);
54   DisallowHeapAllocation no_gc;
55   Object* result;
56   if (symbol->is_public()) {
57     result = symbol->name();
58     DCHECK(result->IsString());
59   } else {
60     result = ReadOnlyRoots(isolate).undefined_value();
61   }
62   DCHECK_EQ(isolate->heap()->public_symbol_table()->SlowReverseLookup(*symbol),
63             result);
64   return result;
65 }
66 
67 }  // namespace internal
68 }  // namespace v8
69