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