| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | SymbolConstructor::SymbolConstructor(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2021-06-28 03:45:49 +03:00
										 |  |  |     : NativeFunction(vm().names.Symbol.as_string(), *global_object.function_prototype()) | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  | void SymbolConstructor::initialize(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-13 23:49:19 +02:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  |     NativeFunction::initialize(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 20.4.2.9 Symbol.prototype, https://tc39.es/ecma262/#sec-symbol.prototype
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.prototype, 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; | 
					
						
							|  |  |  |     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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SymbolConstructor::~SymbolConstructor() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 20.4.1.1 Symbol ( [ description ] ), https://tc39.es/ecma262/#sec-symbol-description
 | 
					
						
							| 
									
										
										
										
											2020-09-27 17:24:14 +02:00
										 |  |  | Value SymbolConstructor::call() | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-15 02:40:55 +03:00
										 |  |  |     if (vm().argument(0).is_undefined()) | 
					
						
							|  |  |  |         return js_symbol(heap(), {}, false); | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     return js_symbol(heap(), vm().argument(0).to_string(global_object()), 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-06-27 21:48:34 +02:00
										 |  |  | Value SymbolConstructor::construct(FunctionObject&) | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  |     vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "Symbol"); | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 20.4.2.2 Symbol.for ( key ), https://tc39.es/ecma262/#sec-symbol.for
 | 
					
						
							| 
									
										
										
										
											2020-06-20 13:55:34 +02:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::for_) | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-15 02:40:55 +03:00
										 |  |  |     String description = vm.argument(0).to_string(global_object); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2020-06-20 13:55:34 +02: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); | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  |     if (!argument.is_symbol()) { | 
					
						
							| 
									
										
										
										
											2020-10-04 13:55:20 +01:00
										 |  |  |         vm.throw_exception<TypeError>(global_object, ErrorType::NotASymbol, argument.to_string_without_side_effects()); | 
					
						
							| 
									
										
										
										
											2020-04-29 23:25:21 -07:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |