Line data Source code
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
17 41124 : BUILTIN(SymbolConstructor) {
18 : HandleScope scope(isolate);
19 30843 : if (!args.new_target()->IsUndefined(isolate)) { // [[Construct]]
20 126 : THROW_NEW_ERROR_RETURN_FAILURE(
21 : isolate, NewTypeError(MessageTemplate::kNotConstructor,
22 : isolate->factory()->Symbol_string()));
23 : }
24 : // [[Call]]
25 10218 : Handle<Symbol> result = isolate->factory()->NewSymbol();
26 : Handle<Object> description = args.atOrUndefined(isolate, 1);
27 20436 : if (!description->IsUndefined(isolate)) {
28 3723 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, description,
29 : Object::ToString(isolate, description));
30 1848 : result->set_name(*description);
31 : }
32 10209 : return *result;
33 : }
34 :
35 : // ES6 section 19.4.2.1 Symbol.for.
36 528 : BUILTIN(SymbolFor) {
37 : HandleScope scope(isolate);
38 132 : Handle<Object> key_obj = args.atOrUndefined(isolate, 1);
39 : Handle<String> key;
40 264 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key,
41 : Object::ToString(isolate, key_obj));
42 264 : return *isolate->SymbolFor(RootIndex::kPublicSymbolTable, key, false);
43 : }
44 :
45 : // ES6 section 19.4.2.5 Symbol.keyFor.
46 396 : BUILTIN(SymbolKeyFor) {
47 : HandleScope scope(isolate);
48 : Handle<Object> obj = args.atOrUndefined(isolate, 1);
49 198 : if (!obj->IsSymbol()) {
50 36 : THROW_NEW_ERROR_RETURN_FAILURE(
51 : isolate, NewTypeError(MessageTemplate::kSymbolKeyFor, obj));
52 : }
53 81 : Handle<Symbol> symbol = Handle<Symbol>::cast(obj);
54 : DisallowHeapAllocation no_gc;
55 : Object result;
56 81 : if (symbol->is_public()) {
57 54 : 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 81 : return result;
65 : }
66 :
67 : } // namespace internal
68 183867 : } // namespace v8
|