v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
builtins-symbol.cc
Go to the documentation of this file.
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
7#include "src/heap/heap-inl.h" // For public_symbol_table().
10
11namespace v8 {
12namespace internal {
13
14// -----------------------------------------------------------------------------
15// ES #sec-symbol-objects
16
17// ES #sec-symbol-constructor
18BUILTIN(SymbolConstructor) {
19 HandleScope scope(isolate);
20 if (!IsUndefined(*args.new_target(), isolate)) { // [[Construct]]
22 isolate, NewTypeError(MessageTemplate::kNotConstructor,
23 isolate->factory()->Symbol_string()));
24 }
25 // [[Call]]
26 DirectHandle<Symbol> result = isolate->factory()->NewSymbol();
27 Handle<Object> description = args.atOrUndefined(isolate, 1);
28 if (!IsUndefined(*description, isolate)) {
29 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, description,
30 Object::ToString(isolate, description));
31 result->set_description(Cast<String>(*description));
32 }
33 return *result;
34}
35
36// ES6 section 19.4.2.1 Symbol.for.
37BUILTIN(SymbolFor) {
38 HandleScope scope(isolate);
39 Handle<Object> key_obj = args.atOrUndefined(isolate, 1);
42 Object::ToString(isolate, key_obj));
43 return *isolate->SymbolFor(RootIndex::kPublicSymbolTable, key, false);
44}
45
46// ES6 section 19.4.2.5 Symbol.keyFor.
47BUILTIN(SymbolKeyFor) {
48 HandleScope scope(isolate);
49 Handle<Object> obj = args.atOrUndefined(isolate, 1);
50 if (!IsSymbol(*obj)) {
52 isolate, NewTypeError(MessageTemplate::kSymbolKeyFor, obj));
53 }
54 auto symbol = Cast<Symbol>(obj);
57 if (symbol->is_in_public_symbol_table()) {
58 result = symbol->description();
59 DCHECK(IsString(result));
60 } else {
61 result = ReadOnlyRoots(isolate).undefined_value();
62 }
63 DCHECK_EQ(isolate->heap()->public_symbol_table()->SlowReverseLookup(*symbol),
64 result);
65 return result;
66}
67
68} // namespace internal
69} // namespace v8
#define BUILTIN(name)
static V8_WARN_UNUSED_RESULT HandleType< String >::MaybeType ToString(Isolate *isolate, HandleType< T > input)
#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:284
#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call)
Definition isolate.h:294
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
ZoneVector< RpoNumber > & result
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485