2020-04-30 03:25:21 -03:00
|
|
|
/*
|
2021-04-22 20:53:07 -03:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-04-30 03:25:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-30 03:25:21 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibJS/Runtime/SymbolConstructor.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
SymbolConstructor::SymbolConstructor(Realm& realm)
|
2022-09-14 20:10:27 -03:00
|
|
|
: NativeFunction(realm.vm().names.Symbol.as_string(), *realm.intrinsics().function_prototype())
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2020-06-20 10:40:48 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
void SymbolConstructor::initialize(Realm& realm)
|
2020-06-20 10:40:48 -03:00
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2022-08-15 20:20:49 -03:00
|
|
|
NativeFunction::initialize(realm);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
|
|
|
// 20.4.2.9 Symbol.prototype, https://tc39.es/ecma262/#sec-symbol.prototype
|
2022-08-26 20:54:55 -03:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().symbol_prototype(), 0);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
2021-06-12 20:58:25 -03:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 17:47:35 -03:00
|
|
|
define_native_function(realm, vm.names.for_, for_, 1, attr);
|
|
|
|
|
define_native_function(realm, vm.names.keyFor, key_for, 1, attr);
|
2020-04-30 03:25:21 -03:00
|
|
|
|
2020-07-09 19:34:20 -03:00
|
|
|
#define __JS_ENUMERATE(SymbolName, snake_name) \
|
2021-07-05 20:15:08 -03:00
|
|
|
define_direct_property(vm.names.SymbolName, vm.well_known_symbol_##snake_name(), 0);
|
2020-07-09 19:34:20 -03:00
|
|
|
JS_ENUMERATE_WELL_KNOWN_SYMBOLS
|
|
|
|
|
#undef __JS_ENUMERATE
|
2021-07-07 20:49:53 -03:00
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
2020-04-30 03:25:21 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
|
2021-10-20 17:16:30 -03:00
|
|
|
ThrowCompletionOr<Value> SymbolConstructor::call()
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2022-08-21 10:00:56 -03:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
if (vm.argument(0).is_undefined())
|
|
|
|
|
return js_symbol(vm, {}, false);
|
|
|
|
|
return js_symbol(vm, TRY(vm.argument(0).to_string(vm)), false);
|
2020-04-30 03:25:21 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
|
2021-10-20 17:16:30 -03:00
|
|
|
ThrowCompletionOr<Object*> SymbolConstructor::construct(FunctionObject&)
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2022-08-16 16:33:17 -03:00
|
|
|
return vm().throw_completion<TypeError>(ErrorType::NotAConstructor, "Symbol");
|
2020-04-30 03:25:21 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
|
2021-10-28 18:16:59 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_)
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2022-08-21 10:00:56 -03:00
|
|
|
auto description = TRY(vm.argument(0).to_string(vm));
|
2022-08-22 07:48:08 -03:00
|
|
|
return vm.get_global_symbol(description);
|
2020-04-30 03:25:21 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 20.4.2.6 Symbol.keyFor ( sym ), https://tc39.es/ecma262/#sec-symbol.keyfor
|
2021-10-28 18:16:59 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto argument = vm.argument(0);
|
2021-10-28 18:16:59 -03:00
|
|
|
if (!argument.is_symbol())
|
2022-08-16 16:33:17 -03:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
|
2020-04-30 03:25:21 -03:00
|
|
|
|
|
|
|
|
auto& symbol = argument.as_symbol();
|
|
|
|
|
if (symbol.is_global())
|
2020-09-27 13:36:49 -03:00
|
|
|
return js_string(vm, symbol.description());
|
2020-04-30 03:25:21 -03:00
|
|
|
|
|
|
|
|
return js_undefined();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|