ECMAScriptFunctionObject.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Debug.h>
  9. #include <AK/Function.h>
  10. #include <LibJS/AST.h>
  11. #include <LibJS/Bytecode/BasicBlock.h>
  12. #include <LibJS/Bytecode/Generator.h>
  13. #include <LibJS/Bytecode/Interpreter.h>
  14. #include <LibJS/Interpreter.h>
  15. #include <LibJS/Runtime/AbstractOperations.h>
  16. #include <LibJS/Runtime/Array.h>
  17. #include <LibJS/Runtime/AsyncFunctionDriverWrapper.h>
  18. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  19. #include <LibJS/Runtime/Error.h>
  20. #include <LibJS/Runtime/ExecutionContext.h>
  21. #include <LibJS/Runtime/FunctionEnvironment.h>
  22. #include <LibJS/Runtime/GeneratorObject.h>
  23. #include <LibJS/Runtime/GlobalObject.h>
  24. #include <LibJS/Runtime/NativeFunction.h>
  25. #include <LibJS/Runtime/PromiseCapability.h>
  26. #include <LibJS/Runtime/PromiseConstructor.h>
  27. #include <LibJS/Runtime/Value.h>
  28. namespace JS {
  29. NonnullGCPtr<ECMAScriptFunctionObject> ECMAScriptFunctionObject::create(Realm& realm, DeprecatedFlyString name, DeprecatedString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
  30. {
  31. Object* prototype = nullptr;
  32. switch (kind) {
  33. case FunctionKind::Normal:
  34. prototype = realm.intrinsics().function_prototype();
  35. break;
  36. case FunctionKind::Generator:
  37. prototype = realm.intrinsics().generator_function_prototype();
  38. break;
  39. case FunctionKind::Async:
  40. prototype = realm.intrinsics().async_function_prototype();
  41. break;
  42. case FunctionKind::AsyncGenerator:
  43. prototype = realm.intrinsics().async_generator_function_prototype();
  44. break;
  45. }
  46. return realm.heap().allocate<ECMAScriptFunctionObject>(realm, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_environment, private_environment, *prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name)).release_allocated_value_but_fixme_should_propagate_errors();
  47. }
  48. NonnullGCPtr<ECMAScriptFunctionObject> ECMAScriptFunctionObject::create(Realm& realm, DeprecatedFlyString name, Object& prototype, DeprecatedString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind kind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
  49. {
  50. return realm.heap().allocate<ECMAScriptFunctionObject>(realm, move(name), move(source_text), ecmascript_code, move(parameters), m_function_length, parent_environment, private_environment, prototype, kind, is_strict, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(class_field_initializer_name)).release_allocated_value_but_fixme_should_propagate_errors();
  51. }
  52. ECMAScriptFunctionObject::ECMAScriptFunctionObject(DeprecatedFlyString name, DeprecatedString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> formal_parameters, i32 function_length, Environment* parent_environment, PrivateEnvironment* private_environment, Object& prototype, FunctionKind kind, bool strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name)
  53. : FunctionObject(prototype)
  54. , m_name(move(name))
  55. , m_function_length(function_length)
  56. , m_environment(parent_environment)
  57. , m_private_environment(private_environment)
  58. , m_formal_parameters(move(formal_parameters))
  59. , m_ecmascript_code(ecmascript_code)
  60. , m_realm(&prototype.shape().realm())
  61. , m_source_text(move(source_text))
  62. , m_class_field_initializer_name(move(class_field_initializer_name))
  63. , m_strict(strict)
  64. , m_might_need_arguments_object(might_need_arguments_object)
  65. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  66. , m_is_arrow_function(is_arrow_function)
  67. , m_kind(kind)
  68. {
  69. // NOTE: This logic is from OrdinaryFunctionCreate, https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
  70. // 9. If thisMode is lexical-this, set F.[[ThisMode]] to lexical.
  71. if (m_is_arrow_function)
  72. m_this_mode = ThisMode::Lexical;
  73. // 10. Else if Strict is true, set F.[[ThisMode]] to strict.
  74. else if (m_strict)
  75. m_this_mode = ThisMode::Strict;
  76. else
  77. // 11. Else, set F.[[ThisMode]] to global.
  78. m_this_mode = ThisMode::Global;
  79. // 15. Set F.[[ScriptOrModule]] to GetActiveScriptOrModule().
  80. m_script_or_module = vm().get_active_script_or_module();
  81. // 15.1.3 Static Semantics: IsSimpleParameterList, https://tc39.es/ecma262/#sec-static-semantics-issimpleparameterlist
  82. m_has_simple_parameter_list = all_of(m_formal_parameters, [&](auto& parameter) {
  83. if (parameter.is_rest)
  84. return false;
  85. if (parameter.default_value)
  86. return false;
  87. if (!parameter.binding.template has<DeprecatedFlyString>())
  88. return false;
  89. return true;
  90. });
  91. }
  92. ThrowCompletionOr<void> ECMAScriptFunctionObject::initialize(Realm& realm)
  93. {
  94. auto& vm = this->vm();
  95. MUST_OR_THROW_OOM(Base::initialize(realm));
  96. // Note: The ordering of these properties must be: length, name, prototype which is the order
  97. // they are defined in the spec: https://tc39.es/ecma262/#sec-function-instances .
  98. // This is observable through something like: https://tc39.es/ecma262/#sec-ordinaryownpropertykeys
  99. // which must give the properties in chronological order which in this case is the order they
  100. // are defined in the spec.
  101. MUST(define_property_or_throw(vm.names.length, { .value = Value(m_function_length), .writable = false, .enumerable = false, .configurable = true }));
  102. MUST(define_property_or_throw(vm.names.name, { .value = PrimitiveString::create(vm, m_name.is_null() ? "" : m_name), .writable = false, .enumerable = false, .configurable = true }));
  103. if (!m_is_arrow_function) {
  104. Object* prototype = nullptr;
  105. switch (m_kind) {
  106. case FunctionKind::Normal:
  107. prototype = MUST_OR_THROW_OOM(vm.heap().allocate<Object>(realm, *realm.intrinsics().new_ordinary_function_prototype_object_shape()));
  108. MUST(prototype->define_property_or_throw(vm.names.constructor, { .value = this, .writable = true, .enumerable = false, .configurable = true }));
  109. break;
  110. case FunctionKind::Generator:
  111. // prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
  112. prototype = Object::create(realm, realm.intrinsics().generator_function_prototype_prototype());
  113. break;
  114. case FunctionKind::Async:
  115. break;
  116. case FunctionKind::AsyncGenerator:
  117. prototype = Object::create(realm, realm.intrinsics().async_generator_function_prototype_prototype());
  118. break;
  119. }
  120. // 27.7.4 AsyncFunction Instances, https://tc39.es/ecma262/#sec-async-function-instances
  121. // AsyncFunction instances do not have a prototype property as they are not constructible.
  122. if (m_kind != FunctionKind::Async)
  123. define_direct_property(vm.names.prototype, prototype, Attribute::Writable);
  124. }
  125. return {};
  126. }
  127. // 10.2.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  128. ThrowCompletionOr<Value> ECMAScriptFunctionObject::internal_call(Value this_argument, MarkedVector<Value> arguments_list)
  129. {
  130. auto& vm = this->vm();
  131. // 1. Let callerContext be the running execution context.
  132. // NOTE: No-op, kept by the VM in its execution context stack.
  133. ExecutionContext callee_context(heap());
  134. // Non-standard
  135. callee_context.arguments.extend(move(arguments_list));
  136. if (auto* interpreter = vm.interpreter_if_exists())
  137. callee_context.current_node = interpreter->current_node();
  138. // 2. Let calleeContext be PrepareForOrdinaryCall(F, undefined).
  139. // NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
  140. TRY(prepare_for_ordinary_call(callee_context, nullptr));
  141. // 3. Assert: calleeContext is now the running execution context.
  142. VERIFY(&vm.running_execution_context() == &callee_context);
  143. // 4. If F.[[IsClassConstructor]] is true, then
  144. if (m_is_class_constructor) {
  145. // a. Let error be a newly created TypeError object.
  146. // b. NOTE: error is created in calleeContext with F's associated Realm Record.
  147. auto throw_completion = vm.throw_completion<TypeError>(ErrorType::ClassConstructorWithoutNew, m_name);
  148. // c. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  149. vm.pop_execution_context();
  150. // d. Return ThrowCompletion(error).
  151. return throw_completion;
  152. }
  153. // 5. Perform OrdinaryCallBindThis(F, calleeContext, thisArgument).
  154. ordinary_call_bind_this(callee_context, this_argument);
  155. // 6. Let result be Completion(OrdinaryCallEvaluateBody(F, argumentsList)).
  156. auto result = ordinary_call_evaluate_body();
  157. // 7. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  158. vm.pop_execution_context();
  159. // 8. If result.[[Type]] is return, return result.[[Value]].
  160. if (result.type() == Completion::Type::Return)
  161. return *result.value();
  162. // 9. ReturnIfAbrupt(result).
  163. if (result.is_abrupt()) {
  164. VERIFY(result.is_error());
  165. return result;
  166. }
  167. // 10. Return undefined.
  168. return js_undefined();
  169. }
  170. // 10.2.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
  171. ThrowCompletionOr<NonnullGCPtr<Object>> ECMAScriptFunctionObject::internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target)
  172. {
  173. auto& vm = this->vm();
  174. // 1. Let callerContext be the running execution context.
  175. // NOTE: No-op, kept by the VM in its execution context stack.
  176. // 2. Let kind be F.[[ConstructorKind]].
  177. auto kind = m_constructor_kind;
  178. GCPtr<Object> this_argument;
  179. // 3. If kind is base, then
  180. if (kind == ConstructorKind::Base) {
  181. // a. Let thisArgument be ? OrdinaryCreateFromConstructor(newTarget, "%Object.prototype%").
  182. this_argument = TRY(ordinary_create_from_constructor<Object>(vm, new_target, &Intrinsics::object_prototype, ConstructWithPrototypeTag::Tag));
  183. }
  184. ExecutionContext callee_context(heap());
  185. // Non-standard
  186. callee_context.arguments.extend(move(arguments_list));
  187. if (auto* interpreter = vm.interpreter_if_exists())
  188. callee_context.current_node = interpreter->current_node();
  189. // 4. Let calleeContext be PrepareForOrdinaryCall(F, newTarget).
  190. // NOTE: We throw if the end of the native stack is reached, so unlike in the spec this _does_ need an exception check.
  191. TRY(prepare_for_ordinary_call(callee_context, &new_target));
  192. // 5. Assert: calleeContext is now the running execution context.
  193. VERIFY(&vm.running_execution_context() == &callee_context);
  194. // 6. If kind is base, then
  195. if (kind == ConstructorKind::Base) {
  196. // a. Perform OrdinaryCallBindThis(F, calleeContext, thisArgument).
  197. ordinary_call_bind_this(callee_context, this_argument);
  198. // b. Let initializeResult be Completion(InitializeInstanceElements(thisArgument, F)).
  199. auto initialize_result = this_argument->initialize_instance_elements(*this);
  200. // c. If initializeResult is an abrupt completion, then
  201. if (initialize_result.is_throw_completion()) {
  202. // i. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  203. vm.pop_execution_context();
  204. // ii. Return ? initializeResult.
  205. return initialize_result.throw_completion();
  206. }
  207. }
  208. // 7. Let constructorEnv be the LexicalEnvironment of calleeContext.
  209. auto* constructor_env = callee_context.lexical_environment;
  210. // 8. Let result be Completion(OrdinaryCallEvaluateBody(F, argumentsList)).
  211. auto result = ordinary_call_evaluate_body();
  212. // 9. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  213. vm.pop_execution_context();
  214. // 10. If result.[[Type]] is return, then
  215. if (result.type() == Completion::Type::Return) {
  216. // FIXME: This is leftover from untangling the call/construct mess - doesn't belong here in any way, but removing it breaks derived classes.
  217. // Likely fixed by making ClassDefinitionEvaluation fully spec compliant.
  218. if (kind == ConstructorKind::Derived && result.value()->is_object()) {
  219. auto prototype = TRY(new_target.get(vm.names.prototype));
  220. if (prototype.is_object())
  221. TRY(result.value()->as_object().internal_set_prototype_of(&prototype.as_object()));
  222. }
  223. // EOF (End of FIXME)
  224. // a. If Type(result.[[Value]]) is Object, return result.[[Value]].
  225. if (result.value()->is_object())
  226. return result.value()->as_object();
  227. // b. If kind is base, return thisArgument.
  228. if (kind == ConstructorKind::Base)
  229. return *this_argument;
  230. // c. If result.[[Value]] is not undefined, throw a TypeError exception.
  231. if (!result.value()->is_undefined())
  232. return vm.throw_completion<TypeError>(ErrorType::DerivedConstructorReturningInvalidValue);
  233. }
  234. // 11. Else, ReturnIfAbrupt(result).
  235. else if (result.is_abrupt()) {
  236. VERIFY(result.is_error());
  237. return result;
  238. }
  239. // 12. Let thisBinding be ? constructorEnv.GetThisBinding().
  240. auto this_binding = TRY(constructor_env->get_this_binding(vm));
  241. // 13. Assert: Type(thisBinding) is Object.
  242. VERIFY(this_binding.is_object());
  243. // 14. Return thisBinding.
  244. return this_binding.as_object();
  245. }
  246. void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
  247. {
  248. Base::visit_edges(visitor);
  249. visitor.visit(m_environment);
  250. visitor.visit(m_private_environment);
  251. visitor.visit(m_realm);
  252. visitor.visit(m_home_object);
  253. for (auto& field : m_fields) {
  254. if (auto* property_key_ptr = field.name.get_pointer<PropertyKey>(); property_key_ptr && property_key_ptr->is_symbol())
  255. visitor.visit(property_key_ptr->as_symbol());
  256. }
  257. m_script_or_module.visit(
  258. [](Empty) {},
  259. [&](auto& script_or_module) {
  260. visitor.visit(script_or_module.ptr());
  261. });
  262. }
  263. // 10.2.7 MakeMethod ( F, homeObject ), https://tc39.es/ecma262/#sec-makemethod
  264. void ECMAScriptFunctionObject::make_method(Object& home_object)
  265. {
  266. // 1. Set F.[[HomeObject]] to homeObject.
  267. m_home_object = &home_object;
  268. // 2. Return unused.
  269. }
  270. // 10.2.11 FunctionDeclarationInstantiation ( func, argumentsList ), https://tc39.es/ecma262/#sec-functiondeclarationinstantiation
  271. ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantiation(Interpreter* interpreter)
  272. {
  273. auto& vm = this->vm();
  274. auto& realm = *vm.current_realm();
  275. auto& callee_context = vm.running_execution_context();
  276. // Needed to extract declarations and functions
  277. ScopeNode const* scope_body = nullptr;
  278. if (is<ScopeNode>(*m_ecmascript_code))
  279. scope_body = static_cast<ScopeNode const*>(m_ecmascript_code.ptr());
  280. bool has_parameter_expressions = false;
  281. // FIXME: Maybe compute has duplicates at parse time? (We need to anyway since it's an error in some cases)
  282. bool has_duplicates = false;
  283. HashTable<DeprecatedFlyString> parameter_names;
  284. for (auto& parameter : m_formal_parameters) {
  285. if (parameter.default_value)
  286. has_parameter_expressions = true;
  287. parameter.binding.visit(
  288. [&](DeprecatedFlyString const& name) {
  289. if (parameter_names.set(name) != AK::HashSetResult::InsertedNewEntry)
  290. has_duplicates = true;
  291. },
  292. [&](NonnullRefPtr<BindingPattern const> const& pattern) {
  293. if (pattern->contains_expression())
  294. has_parameter_expressions = true;
  295. // NOTE: Nothing in the callback throws an exception.
  296. MUST(pattern->for_each_bound_name([&](auto& name) {
  297. if (parameter_names.set(name) != AK::HashSetResult::InsertedNewEntry)
  298. has_duplicates = true;
  299. }));
  300. });
  301. }
  302. auto arguments_object_needed = m_might_need_arguments_object;
  303. if (this_mode() == ThisMode::Lexical)
  304. arguments_object_needed = false;
  305. if (parameter_names.contains(vm.names.arguments.as_string()))
  306. arguments_object_needed = false;
  307. HashTable<DeprecatedFlyString> function_names;
  308. Vector<FunctionDeclaration const&> functions_to_initialize;
  309. if (scope_body) {
  310. // NOTE: Nothing in the callback throws an exception.
  311. MUST(scope_body->for_each_var_function_declaration_in_reverse_order([&](FunctionDeclaration const& function) {
  312. if (function_names.set(function.name()) == AK::HashSetResult::InsertedNewEntry)
  313. functions_to_initialize.append(function);
  314. }));
  315. auto const& arguments_name = vm.names.arguments.as_string();
  316. if (!has_parameter_expressions && function_names.contains(arguments_name))
  317. arguments_object_needed = false;
  318. if (!has_parameter_expressions && arguments_object_needed) {
  319. // NOTE: Nothing in the callback throws an exception.
  320. MUST(scope_body->for_each_lexically_declared_name([&](auto const& name) {
  321. if (name == arguments_name)
  322. arguments_object_needed = false;
  323. }));
  324. }
  325. } else {
  326. arguments_object_needed = false;
  327. }
  328. GCPtr<Environment> environment;
  329. if (is_strict_mode() || !has_parameter_expressions) {
  330. environment = callee_context.lexical_environment;
  331. } else {
  332. environment = new_declarative_environment(*callee_context.lexical_environment);
  333. VERIFY(callee_context.variable_environment == callee_context.lexical_environment);
  334. callee_context.lexical_environment = environment;
  335. }
  336. for (auto const& parameter_name : parameter_names) {
  337. if (MUST(environment->has_binding(parameter_name)))
  338. continue;
  339. MUST(environment->create_mutable_binding(vm, parameter_name, false));
  340. if (has_duplicates)
  341. MUST(environment->initialize_binding(vm, parameter_name, js_undefined(), Environment::InitializeBindingHint::Normal));
  342. }
  343. if (arguments_object_needed) {
  344. Object* arguments_object;
  345. if (is_strict_mode() || !has_simple_parameter_list())
  346. arguments_object = create_unmapped_arguments_object(vm, vm.running_execution_context().arguments);
  347. else
  348. arguments_object = create_mapped_arguments_object(vm, *this, formal_parameters(), vm.running_execution_context().arguments, *environment);
  349. if (is_strict_mode())
  350. MUST(environment->create_immutable_binding(vm, vm.names.arguments.as_string(), false));
  351. else
  352. MUST(environment->create_mutable_binding(vm, vm.names.arguments.as_string(), false));
  353. MUST(environment->initialize_binding(vm, vm.names.arguments.as_string(), arguments_object, Environment::InitializeBindingHint::Normal));
  354. parameter_names.set(vm.names.arguments.as_string());
  355. }
  356. // We now treat parameterBindings as parameterNames.
  357. // The spec makes an iterator here to do IteratorBindingInitialization but we just do it manually
  358. auto& execution_context_arguments = vm.running_execution_context().arguments;
  359. size_t default_parameter_index = 0;
  360. for (size_t i = 0; i < m_formal_parameters.size(); ++i) {
  361. auto& parameter = m_formal_parameters[i];
  362. if (parameter.default_value)
  363. ++default_parameter_index;
  364. TRY(parameter.binding.visit(
  365. [&](auto const& param) -> ThrowCompletionOr<void> {
  366. Value argument_value;
  367. if (parameter.is_rest) {
  368. auto array = MUST(Array::create(realm, 0));
  369. for (size_t rest_index = i; rest_index < execution_context_arguments.size(); ++rest_index)
  370. array->indexed_properties().append(execution_context_arguments[rest_index]);
  371. argument_value = array;
  372. } else if (i < execution_context_arguments.size() && !execution_context_arguments[i].is_undefined()) {
  373. argument_value = execution_context_arguments[i];
  374. } else if (parameter.default_value) {
  375. if (auto* bytecode_interpreter = Bytecode::Interpreter::current()) {
  376. auto value_and_frame = bytecode_interpreter->run_and_return_frame(*m_default_parameter_bytecode_executables[default_parameter_index - 1], nullptr);
  377. if (value_and_frame.value.is_error())
  378. return value_and_frame.value.release_error();
  379. // Resulting value is in the accumulator.
  380. argument_value = value_and_frame.frame->registers.at(0);
  381. } else if (interpreter) {
  382. argument_value = TRY(parameter.default_value->execute(*interpreter)).release_value();
  383. }
  384. } else {
  385. argument_value = js_undefined();
  386. }
  387. Environment* used_environment = has_duplicates ? nullptr : environment;
  388. if constexpr (IsSame<DeprecatedFlyString const&, decltype(param)>) {
  389. Reference reference = TRY(vm.resolve_binding(param, used_environment));
  390. // Here the difference from hasDuplicates is important
  391. if (has_duplicates)
  392. return reference.put_value(vm, argument_value);
  393. else
  394. return reference.initialize_referenced_binding(vm, argument_value);
  395. }
  396. if constexpr (IsSame<NonnullRefPtr<BindingPattern const> const&, decltype(param)>) {
  397. // Here the difference from hasDuplicates is important
  398. return vm.binding_initialization(param, argument_value, used_environment);
  399. }
  400. }));
  401. }
  402. GCPtr<Environment> var_environment;
  403. HashTable<DeprecatedFlyString> instantiated_var_names;
  404. if (scope_body)
  405. instantiated_var_names.ensure_capacity(scope_body->var_declaration_count());
  406. if (!has_parameter_expressions) {
  407. if (scope_body) {
  408. // NOTE: Due to the use of MUST with `create_mutable_binding` and `initialize_binding` below,
  409. // an exception should not result from `for_each_var_declared_name`.
  410. MUST(scope_body->for_each_var_declared_name([&](auto const& name) {
  411. if (!parameter_names.contains(name) && instantiated_var_names.set(name) == AK::HashSetResult::InsertedNewEntry) {
  412. MUST(environment->create_mutable_binding(vm, name, false));
  413. MUST(environment->initialize_binding(vm, name, js_undefined(), Environment::InitializeBindingHint::Normal));
  414. }
  415. }));
  416. }
  417. var_environment = environment;
  418. } else {
  419. var_environment = new_declarative_environment(*environment);
  420. callee_context.variable_environment = var_environment;
  421. if (scope_body) {
  422. // NOTE: Due to the use of MUST with `create_mutable_binding`, `get_binding_value` and `initialize_binding` below,
  423. // an exception should not result from `for_each_var_declared_name`.
  424. MUST(scope_body->for_each_var_declared_name([&](auto const& name) {
  425. if (instantiated_var_names.set(name) != AK::HashSetResult::InsertedNewEntry)
  426. return;
  427. MUST(var_environment->create_mutable_binding(vm, name, false));
  428. Value initial_value;
  429. if (!parameter_names.contains(name) || function_names.contains(name))
  430. initial_value = js_undefined();
  431. else
  432. initial_value = MUST(environment->get_binding_value(vm, name, false));
  433. MUST(var_environment->initialize_binding(vm, name, initial_value, Environment::InitializeBindingHint::Normal));
  434. }));
  435. }
  436. }
  437. // B.3.2.1 Changes to FunctionDeclarationInstantiation, https://tc39.es/ecma262/#sec-web-compat-functiondeclarationinstantiation
  438. if (!m_strict && scope_body) {
  439. // NOTE: Due to the use of MUST with `create_mutable_binding` and `initialize_binding` below,
  440. // an exception should not result from `for_each_function_hoistable_with_annexB_extension`.
  441. MUST(scope_body->for_each_function_hoistable_with_annexB_extension([&](FunctionDeclaration& function_declaration) {
  442. auto& function_name = function_declaration.name();
  443. if (parameter_names.contains(function_name))
  444. return;
  445. // The spec says 'initializedBindings' here but that does not exist and it then adds it to 'instantiatedVarNames' so it probably means 'instantiatedVarNames'.
  446. if (!instantiated_var_names.contains(function_name) && function_name != vm.names.arguments.as_string()) {
  447. MUST(var_environment->create_mutable_binding(vm, function_name, false));
  448. MUST(var_environment->initialize_binding(vm, function_name, js_undefined(), Environment::InitializeBindingHint::Normal));
  449. instantiated_var_names.set(function_name);
  450. }
  451. function_declaration.set_should_do_additional_annexB_steps();
  452. }));
  453. }
  454. GCPtr<Environment> lex_environment;
  455. // 30. If strict is false, then
  456. if (!is_strict_mode()) {
  457. // Optimization: We avoid creating empty top-level declarative environments in non-strict mode, if both of these conditions are true:
  458. // 1. there is no direct call to eval() within this function
  459. // 2. there are no lexical declarations that would go into the environment
  460. bool can_elide_declarative_environment = !m_contains_direct_call_to_eval && (!scope_body || !scope_body->has_lexical_declarations());
  461. if (can_elide_declarative_environment) {
  462. lex_environment = var_environment;
  463. } else {
  464. // a. Let lexEnv be NewDeclarativeEnvironment(varEnv).
  465. // b. NOTE: Non-strict functions use a separate Environment Record for top-level lexical declarations so that a direct eval
  466. // can determine whether any var scoped declarations introduced by the eval code conflict with pre-existing top-level
  467. // lexically scoped declarations. This is not needed for strict functions because a strict direct eval always places
  468. // all declarations into a new Environment Record.
  469. lex_environment = new_declarative_environment(*var_environment);
  470. }
  471. } else {
  472. // 31. Else, let lexEnv be varEnv.
  473. lex_environment = var_environment;
  474. }
  475. // 32. Set the LexicalEnvironment of calleeContext to lexEnv.
  476. callee_context.lexical_environment = lex_environment;
  477. if (!scope_body)
  478. return {};
  479. if (!Bytecode::Interpreter::current()) {
  480. // NOTE: Due to the use of MUST in the callback, an exception should not result from `for_each_lexically_scoped_declaration`.
  481. MUST(scope_body->for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
  482. // NOTE: Due to the use of MUST with `create_immutable_binding` and `create_mutable_binding` below,
  483. // an exception should not result from `for_each_bound_name`.
  484. MUST(declaration.for_each_bound_name([&](auto const& name) {
  485. if (declaration.is_constant_declaration())
  486. MUST(lex_environment->create_immutable_binding(vm, name, true));
  487. else
  488. MUST(lex_environment->create_mutable_binding(vm, name, false));
  489. }));
  490. }));
  491. }
  492. auto* private_environment = callee_context.private_environment;
  493. for (auto& declaration : functions_to_initialize) {
  494. auto function = ECMAScriptFunctionObject::create(realm, declaration.name(), declaration.source_text(), declaration.body(), declaration.parameters(), declaration.function_length(), lex_environment, private_environment, declaration.kind(), declaration.is_strict_mode(), declaration.might_need_arguments_object(), declaration.contains_direct_call_to_eval());
  495. MUST(var_environment->set_mutable_binding(vm, declaration.name(), function, false));
  496. }
  497. if (is<DeclarativeEnvironment>(*lex_environment))
  498. static_cast<DeclarativeEnvironment*>(lex_environment.ptr())->shrink_to_fit();
  499. if (is<DeclarativeEnvironment>(*var_environment))
  500. static_cast<DeclarativeEnvironment*>(var_environment.ptr())->shrink_to_fit();
  501. return {};
  502. }
  503. // 10.2.1.1 PrepareForOrdinaryCall ( F, newTarget ), https://tc39.es/ecma262/#sec-prepareforordinarycall
  504. ThrowCompletionOr<void> ECMAScriptFunctionObject::prepare_for_ordinary_call(ExecutionContext& callee_context, Object* new_target)
  505. {
  506. auto& vm = this->vm();
  507. // Non-standard
  508. callee_context.is_strict_mode = m_strict;
  509. // 1. Let callerContext be the running execution context.
  510. // 2. Let calleeContext be a new ECMAScript code execution context.
  511. // NOTE: In the specification, PrepareForOrdinaryCall "returns" a new callee execution context.
  512. // To avoid heap allocations, we put our ExecutionContext objects on the C++ stack instead.
  513. // Whoever calls us should put an ExecutionContext on their stack and pass that as the `callee_context`.
  514. // 3. Set the Function of calleeContext to F.
  515. callee_context.function = this;
  516. callee_context.function_name = m_name;
  517. // 4. Let calleeRealm be F.[[Realm]].
  518. auto* callee_realm = m_realm;
  519. // NOTE: This non-standard fallback is needed until we can guarantee that literally
  520. // every function has a realm - especially in LibWeb that's sometimes not the case
  521. // when a function is created while no JS is running, as we currently need to rely on
  522. // that (:acid2:, I know - see set_event_handler_attribute() for an example).
  523. // If there's no 'current realm' either, we can't continue and crash.
  524. if (!callee_realm)
  525. callee_realm = vm.current_realm();
  526. VERIFY(callee_realm);
  527. // 5. Set the Realm of calleeContext to calleeRealm.
  528. callee_context.realm = callee_realm;
  529. // 6. Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].
  530. callee_context.script_or_module = m_script_or_module;
  531. // 7. Let localEnv be NewFunctionEnvironment(F, newTarget).
  532. auto local_environment = new_function_environment(*this, new_target);
  533. // 8. Set the LexicalEnvironment of calleeContext to localEnv.
  534. callee_context.lexical_environment = local_environment;
  535. // 9. Set the VariableEnvironment of calleeContext to localEnv.
  536. callee_context.variable_environment = local_environment;
  537. // 10. Set the PrivateEnvironment of calleeContext to F.[[PrivateEnvironment]].
  538. callee_context.private_environment = m_private_environment;
  539. // 11. If callerContext is not already suspended, suspend callerContext.
  540. // FIXME: We don't have this concept yet.
  541. // 12. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  542. TRY(vm.push_execution_context(callee_context, {}));
  543. // 13. NOTE: Any exception objects produced after this point are associated with calleeRealm.
  544. // 14. Return calleeContext.
  545. // NOTE: See the comment after step 2 above about how contexts are allocated on the C++ stack.
  546. return {};
  547. }
  548. // 10.2.1.2 OrdinaryCallBindThis ( F, calleeContext, thisArgument ), https://tc39.es/ecma262/#sec-ordinarycallbindthis
  549. void ECMAScriptFunctionObject::ordinary_call_bind_this(ExecutionContext& callee_context, Value this_argument)
  550. {
  551. auto& vm = this->vm();
  552. // 1. Let thisMode be F.[[ThisMode]].
  553. auto this_mode = m_this_mode;
  554. // If thisMode is lexical, return unused.
  555. if (this_mode == ThisMode::Lexical)
  556. return;
  557. // 3. Let calleeRealm be F.[[Realm]].
  558. auto* callee_realm = m_realm;
  559. // NOTE: This non-standard fallback is needed until we can guarantee that literally
  560. // every function has a realm - especially in LibWeb that's sometimes not the case
  561. // when a function is created while no JS is running, as we currently need to rely on
  562. // that (:acid2:, I know - see set_event_handler_attribute() for an example).
  563. // If there's no 'current realm' either, we can't continue and crash.
  564. if (!callee_realm)
  565. callee_realm = vm.current_realm();
  566. VERIFY(callee_realm);
  567. // 4. Let localEnv be the LexicalEnvironment of calleeContext.
  568. auto* local_env = callee_context.lexical_environment;
  569. Value this_value;
  570. // 5. If thisMode is strict, let thisValue be thisArgument.
  571. if (this_mode == ThisMode::Strict) {
  572. this_value = this_argument;
  573. }
  574. // 6. Else,
  575. else {
  576. // a. If thisArgument is undefined or null, then
  577. if (this_argument.is_nullish()) {
  578. // i. Let globalEnv be calleeRealm.[[GlobalEnv]].
  579. // ii. Assert: globalEnv is a global Environment Record.
  580. auto& global_env = callee_realm->global_environment();
  581. // iii. Let thisValue be globalEnv.[[GlobalThisValue]].
  582. this_value = &global_env.global_this_value();
  583. }
  584. // b. Else,
  585. else {
  586. // i. Let thisValue be ! ToObject(thisArgument).
  587. this_value = MUST(this_argument.to_object(vm));
  588. // ii. NOTE: ToObject produces wrapper objects using calleeRealm.
  589. VERIFY(vm.current_realm() == callee_realm);
  590. }
  591. }
  592. // 7. Assert: localEnv is a function Environment Record.
  593. // 8. Assert: The next step never returns an abrupt completion because localEnv.[[ThisBindingStatus]] is not initialized.
  594. // 9. Perform ! localEnv.BindThisValue(thisValue).
  595. MUST(verify_cast<FunctionEnvironment>(local_env)->bind_this_value(vm, this_value));
  596. // 10. Return unused.
  597. }
  598. // 27.7.5.1 AsyncFunctionStart ( promiseCapability, asyncFunctionBody ), https://tc39.es/ecma262/#sec-async-functions-abstract-operations-async-function-start
  599. void ECMAScriptFunctionObject::async_function_start(PromiseCapability const& promise_capability)
  600. {
  601. auto& vm = this->vm();
  602. // 1. Let runningContext be the running execution context.
  603. auto& running_context = vm.running_execution_context();
  604. // 2. Let asyncContext be a copy of runningContext.
  605. auto async_context = running_context.copy();
  606. // 3. NOTE: Copying the execution state is required for AsyncBlockStart to resume its execution. It is ill-defined to resume a currently executing context.
  607. // 4. Perform AsyncBlockStart(promiseCapability, asyncFunctionBody, asyncContext).
  608. async_block_start(vm, m_ecmascript_code, promise_capability, async_context);
  609. // 5. Return unused.
  610. }
  611. // 27.7.5.2 AsyncBlockStart ( promiseCapability, asyncBody, asyncContext ), https://tc39.es/ecma262/#sec-asyncblockstart
  612. void async_block_start(VM& vm, NonnullRefPtr<Statement const> const& async_body, PromiseCapability const& promise_capability, ExecutionContext& async_context)
  613. {
  614. auto& realm = *vm.current_realm();
  615. // 1. Assert: promiseCapability is a PromiseCapability Record.
  616. // 2. Let runningContext be the running execution context.
  617. auto& running_context = vm.running_execution_context();
  618. // 3. Set the code evaluation state of asyncContext such that when evaluation is resumed for that execution context the following steps will be performed:
  619. auto execution_steps = NativeFunction::create(realm, "", [&async_body, &promise_capability, &async_context](auto& vm) -> ThrowCompletionOr<Value> {
  620. // a. Let result be the result of evaluating asyncBody.
  621. auto result = async_body->execute(vm.interpreter());
  622. // b. Assert: If we return here, the async function either threw an exception or performed an implicit or explicit return; all awaiting is done.
  623. // c. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
  624. vm.pop_execution_context();
  625. // d. Let env be asyncContext's LexicalEnvironment.
  626. auto* env = async_context.lexical_environment;
  627. VERIFY(is<DeclarativeEnvironment>(env));
  628. // e. Set result to DisposeResources(env, result).
  629. result = dispose_resources(vm, static_cast<DeclarativeEnvironment*>(env), result);
  630. // f. If result.[[Type]] is normal, then
  631. if (result.type() == Completion::Type::Normal) {
  632. // i. Perform ! Call(promiseCapability.[[Resolve]], undefined, « undefined »).
  633. MUST(call(vm, *promise_capability.resolve(), js_undefined(), js_undefined()));
  634. }
  635. // g. Else if result.[[Type]] is return, then
  636. else if (result.type() == Completion::Type::Return) {
  637. // i. Perform ! Call(promiseCapability.[[Resolve]], undefined, « result.[[Value]] »).
  638. MUST(call(vm, *promise_capability.resolve(), js_undefined(), *result.value()));
  639. }
  640. // h. Else,
  641. else {
  642. // i. Assert: result.[[Type]] is throw.
  643. VERIFY(result.type() == Completion::Type::Throw);
  644. // ii. Perform ! Call(promiseCapability.[[Reject]], undefined, « result.[[Value]] »).
  645. MUST(call(vm, *promise_capability.reject(), js_undefined(), *result.value()));
  646. }
  647. // i. Return unused.
  648. // NOTE: We don't support returning an empty/optional/unused value here.
  649. return js_undefined();
  650. });
  651. // 4. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  652. auto push_result = vm.push_execution_context(async_context, {});
  653. if (push_result.is_error())
  654. return;
  655. // 5. Resume the suspended evaluation of asyncContext. Let result be the value returned by the resumed computation.
  656. auto result = call(vm, *execution_steps, async_context.this_value.is_empty() ? js_undefined() : async_context.this_value);
  657. // 6. Assert: When we return here, asyncContext has already been removed from the execution context stack and runningContext is the currently running execution context.
  658. VERIFY(&vm.running_execution_context() == &running_context);
  659. // 7. Assert: result is a normal completion with a value of unused. The possible sources of this value are Await or, if the async function doesn't await anything, step 3.g above.
  660. VERIFY(result.has_value() && result.value().is_undefined());
  661. // 8. Return unused.
  662. }
  663. // 10.2.1.4 OrdinaryCallEvaluateBody ( F, argumentsList ), https://tc39.es/ecma262/#sec-ordinarycallevaluatebody
  664. // 15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody, https://tc39.es/ecma262/#sec-runtime-semantics-evaluatefunctionbody
  665. Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
  666. {
  667. auto& vm = this->vm();
  668. auto& realm = *vm.current_realm();
  669. if (m_kind == FunctionKind::AsyncGenerator)
  670. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, "Async Generator function execution");
  671. auto* bytecode_interpreter = Bytecode::Interpreter::current();
  672. // The bytecode interpreter can execute generator functions while the AST interpreter cannot.
  673. // This simply makes it create a new bytecode interpreter when one doesn't exist when executing a generator function.
  674. // Doing so makes it automatically switch to the bytecode interpreter to execute any future code until it exits the generator. See below.
  675. // This allows us to keep all of the existing functionality that works in AST while adding generator support on top of it.
  676. // However, this does cause an awkward situation with features not supported in bytecode, where features that work outside of generators with AST
  677. // suddenly stop working inside of generators.
  678. // This is a stop gap until bytecode mode becomes the default.
  679. OwnPtr<Bytecode::Interpreter> temp_bc_interpreter;
  680. if (m_kind == FunctionKind::Generator && !bytecode_interpreter) {
  681. temp_bc_interpreter = make<Bytecode::Interpreter>(realm);
  682. bytecode_interpreter = temp_bc_interpreter.ptr();
  683. }
  684. if (bytecode_interpreter) {
  685. if (!m_bytecode_executable) {
  686. auto compile = [&](auto& node, auto kind, auto name) -> ThrowCompletionOr<NonnullOwnPtr<Bytecode::Executable>> {
  687. auto executable_result = Bytecode::Generator::generate(node, kind);
  688. if (executable_result.is_error())
  689. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, TRY_OR_THROW_OOM(vm, executable_result.error().to_string()));
  690. auto bytecode_executable = executable_result.release_value();
  691. bytecode_executable->name = name;
  692. auto& passes = Bytecode::Interpreter::optimization_pipeline();
  693. passes.perform(*bytecode_executable);
  694. if constexpr (JS_BYTECODE_DEBUG) {
  695. dbgln("Optimisation passes took {}us", passes.elapsed());
  696. dbgln("Compiled Bytecode::Block for function '{}':", m_name);
  697. }
  698. if (Bytecode::g_dump_bytecode)
  699. bytecode_executable->dump();
  700. return bytecode_executable;
  701. };
  702. m_bytecode_executable = TRY(compile(*m_ecmascript_code, m_kind, m_name));
  703. size_t default_parameter_index = 0;
  704. for (auto& parameter : m_formal_parameters) {
  705. if (!parameter.default_value)
  706. continue;
  707. auto executable = TRY(compile(*parameter.default_value, FunctionKind::Normal, DeprecatedString::formatted("default parameter #{} for {}", default_parameter_index, m_name)));
  708. m_default_parameter_bytecode_executables.append(move(executable));
  709. }
  710. }
  711. TRY(function_declaration_instantiation(nullptr));
  712. auto result_and_frame = bytecode_interpreter->run_and_return_frame(*m_bytecode_executable, nullptr);
  713. VERIFY(result_and_frame.frame != nullptr);
  714. if (result_and_frame.value.is_error())
  715. return result_and_frame.value.release_error();
  716. auto result = result_and_frame.value.release_value();
  717. // NOTE: Running the bytecode should eventually return a completion.
  718. // Until it does, we assume "return" and include the undefined fallback from the call site.
  719. if (m_kind == FunctionKind::Normal)
  720. return { Completion::Type::Return, result.value_or(js_undefined()), {} };
  721. auto generator_object = TRY(GeneratorObject::create(realm, result, this, vm.running_execution_context().copy(), move(*result_and_frame.frame)));
  722. // NOTE: Async functions are entirely transformed to generator functions, and wrapped in a custom driver that returns a promise
  723. // See AwaitExpression::generate_bytecode() for the transformation.
  724. if (m_kind == FunctionKind::Async)
  725. return { Completion::Type::Return, TRY(AsyncFunctionDriverWrapper::create(realm, generator_object)), {} };
  726. VERIFY(m_kind == FunctionKind::Generator);
  727. return { Completion::Type::Return, generator_object, {} };
  728. } else {
  729. if (m_kind == FunctionKind::Generator)
  730. return vm.throw_completion<InternalError>(ErrorType::NotImplemented, "Generator function execution in AST interpreter");
  731. OwnPtr<Interpreter> local_interpreter;
  732. Interpreter* ast_interpreter = vm.interpreter_if_exists();
  733. if (!ast_interpreter) {
  734. local_interpreter = Interpreter::create_with_existing_realm(realm);
  735. ast_interpreter = local_interpreter.ptr();
  736. }
  737. VM::InterpreterExecutionScope scope(*ast_interpreter);
  738. // FunctionBody : FunctionStatementList
  739. if (m_kind == FunctionKind::Normal) {
  740. // 1. Perform ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  741. TRY(function_declaration_instantiation(ast_interpreter));
  742. // 2. Let result be result of evaluating FunctionStatementList.
  743. auto result = m_ecmascript_code->execute(*ast_interpreter);
  744. // 3. Let env be the running execution context's LexicalEnvironment.
  745. auto* env = vm.running_execution_context().lexical_environment;
  746. VERIFY(is<DeclarativeEnvironment>(env));
  747. // 4. Return ? DisposeResources(env, result).
  748. return dispose_resources(vm, static_cast<DeclarativeEnvironment*>(env), result);
  749. }
  750. // AsyncFunctionBody : FunctionBody
  751. else if (m_kind == FunctionKind::Async) {
  752. // 1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  753. auto promise_capability = MUST(new_promise_capability(vm, realm.intrinsics().promise_constructor()));
  754. // 2. Let declResult be Completion(FunctionDeclarationInstantiation(functionObject, argumentsList)).
  755. auto declaration_result = function_declaration_instantiation(ast_interpreter);
  756. // 3. If declResult is an abrupt completion, then
  757. if (declaration_result.is_throw_completion()) {
  758. // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
  759. MUST(call(vm, *promise_capability->reject(), js_undefined(), *declaration_result.throw_completion().value()));
  760. }
  761. // 4. Else,
  762. else {
  763. // a. Perform AsyncFunctionStart(promiseCapability, FunctionBody).
  764. async_function_start(promise_capability);
  765. }
  766. // 5. Return Completion Record { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.
  767. return Completion { Completion::Type::Return, promise_capability->promise(), {} };
  768. }
  769. }
  770. VERIFY_NOT_REACHED();
  771. }
  772. void ECMAScriptFunctionObject::set_name(DeprecatedFlyString const& name)
  773. {
  774. VERIFY(!name.is_null());
  775. auto& vm = this->vm();
  776. m_name = name;
  777. MUST(define_property_or_throw(vm.names.name, { .value = PrimitiveString::create(vm, m_name), .writable = false, .enumerable = false, .configurable = true }));
  778. }
  779. }