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