CommonImplementations.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/CommonImplementations.h>
  7. #include <LibJS/Bytecode/Interpreter.h>
  8. #include <LibJS/Bytecode/Op.h>
  9. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  10. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  11. #include <LibJS/Runtime/GlobalEnvironment.h>
  12. #include <LibJS/Runtime/ObjectEnvironment.h>
  13. namespace JS::Bytecode {
  14. ThrowCompletionOr<NonnullGCPtr<Object>> base_object_for_get(Bytecode::Interpreter& interpreter, Value base_value)
  15. {
  16. auto& vm = interpreter.vm();
  17. if (base_value.is_object())
  18. return base_value.as_object();
  19. // OPTIMIZATION: For various primitives we can avoid actually creating a new object for them.
  20. if (base_value.is_string())
  21. return vm.current_realm()->intrinsics().string_prototype();
  22. if (base_value.is_number())
  23. return vm.current_realm()->intrinsics().number_prototype();
  24. if (base_value.is_boolean())
  25. return vm.current_realm()->intrinsics().boolean_prototype();
  26. return base_value.to_object(vm);
  27. }
  28. ThrowCompletionOr<Value> get_by_id(Bytecode::Interpreter& interpreter, IdentifierTableIndex property, Value base_value, Value this_value, u32 cache_index)
  29. {
  30. auto& vm = interpreter.vm();
  31. auto const& name = interpreter.current_executable().get_identifier(property);
  32. auto& cache = interpreter.current_executable().property_lookup_caches[cache_index];
  33. if (base_value.is_string()) {
  34. auto string_value = TRY(base_value.as_string().get(vm, name));
  35. if (string_value.has_value())
  36. return *string_value;
  37. }
  38. auto base_obj = TRY(base_object_for_get(interpreter, base_value));
  39. // OPTIMIZATION: If the shape of the object hasn't changed, we can use the cached property offset.
  40. // NOTE: Unique shapes don't change identity, so we compare their serial numbers instead.
  41. auto& shape = base_obj->shape();
  42. if (&shape == cache.shape
  43. && (!shape.is_unique() || shape.unique_shape_serial_number() == cache.unique_shape_serial_number)) {
  44. return base_obj->get_direct(cache.property_offset.value());
  45. }
  46. CacheablePropertyMetadata cacheable_metadata;
  47. auto value = TRY(base_obj->internal_get(name, this_value, &cacheable_metadata));
  48. if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) {
  49. cache.shape = shape;
  50. cache.property_offset = cacheable_metadata.property_offset.value();
  51. cache.unique_shape_serial_number = shape.unique_shape_serial_number();
  52. }
  53. return value;
  54. }
  55. ThrowCompletionOr<Value> get_by_value(Bytecode::Interpreter& interpreter, Value base_value, Value property_key_value)
  56. {
  57. auto& vm = interpreter.vm();
  58. auto object = TRY(base_object_for_get(interpreter, base_value));
  59. // OPTIMIZATION: Fast path for simple Int32 indexes in array-like objects.
  60. if (property_key_value.is_int32()
  61. && property_key_value.as_i32() >= 0
  62. && !object->may_interfere_with_indexed_property_access()
  63. && object->indexed_properties().has_index(property_key_value.as_i32())) {
  64. auto value = object->indexed_properties().get(property_key_value.as_i32())->value;
  65. if (!value.is_accessor())
  66. return value;
  67. }
  68. auto property_key = TRY(property_key_value.to_property_key(vm));
  69. if (base_value.is_string()) {
  70. auto string_value = TRY(base_value.as_string().get(vm, property_key));
  71. if (string_value.has_value())
  72. return *string_value;
  73. }
  74. return TRY(object->internal_get(property_key, base_value));
  75. }
  76. ThrowCompletionOr<Value> get_global(Bytecode::Interpreter& interpreter, IdentifierTableIndex identifier, u32 cache_index)
  77. {
  78. auto& vm = interpreter.vm();
  79. auto& realm = *vm.current_realm();
  80. auto& cache = interpreter.current_executable().global_variable_caches[cache_index];
  81. auto& binding_object = realm.global_environment().object_record().binding_object();
  82. auto& declarative_record = realm.global_environment().declarative_record();
  83. // OPTIMIZATION: If the shape of the object hasn't changed, we can use the cached property offset.
  84. // NOTE: Unique shapes don't change identity, so we compare their serial numbers instead.
  85. auto& shape = binding_object.shape();
  86. if (cache.environment_serial_number == declarative_record.environment_serial_number()
  87. && &shape == cache.shape
  88. && (!shape.is_unique() || shape.unique_shape_serial_number() == cache.unique_shape_serial_number)) {
  89. return binding_object.get_direct(cache.property_offset.value());
  90. }
  91. cache.environment_serial_number = declarative_record.environment_serial_number();
  92. auto const& name = interpreter.current_executable().get_identifier(identifier);
  93. if (vm.running_execution_context().script_or_module.has<NonnullGCPtr<Module>>()) {
  94. // NOTE: GetGlobal is used to access variables stored in the module environment and global environment.
  95. // The module environment is checked first since it precedes the global environment in the environment chain.
  96. auto& module_environment = *vm.running_execution_context().script_or_module.get<NonnullGCPtr<Module>>()->environment();
  97. if (TRY(module_environment.has_binding(name))) {
  98. // TODO: Cache offset of binding value
  99. return TRY(module_environment.get_binding_value(vm, name, vm.in_strict_mode()));
  100. }
  101. }
  102. if (TRY(declarative_record.has_binding(name))) {
  103. // TODO: Cache offset of binding value
  104. return TRY(declarative_record.get_binding_value(vm, name, vm.in_strict_mode()));
  105. }
  106. if (TRY(binding_object.has_property(name))) {
  107. CacheablePropertyMetadata cacheable_metadata;
  108. auto value = TRY(binding_object.internal_get(name, js_undefined(), &cacheable_metadata));
  109. if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) {
  110. cache.shape = shape;
  111. cache.property_offset = cacheable_metadata.property_offset.value();
  112. cache.unique_shape_serial_number = shape.unique_shape_serial_number();
  113. }
  114. return value;
  115. }
  116. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  117. }
  118. ThrowCompletionOr<void> put_by_property_key(VM& vm, Value base, Value this_value, Value value, PropertyKey name, Op::PropertyKind kind)
  119. {
  120. auto object = TRY(base.to_object(vm));
  121. if (kind == Op::PropertyKind::Getter || kind == Op::PropertyKind::Setter) {
  122. // The generator should only pass us functions for getters and setters.
  123. VERIFY(value.is_function());
  124. }
  125. switch (kind) {
  126. case Op::PropertyKind::Getter: {
  127. auto& function = value.as_function();
  128. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  129. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(DeprecatedString::formatted("get {}", name));
  130. object->define_direct_accessor(name, &function, nullptr, Attribute::Configurable | Attribute::Enumerable);
  131. break;
  132. }
  133. case Op::PropertyKind::Setter: {
  134. auto& function = value.as_function();
  135. if (function.name().is_empty() && is<ECMAScriptFunctionObject>(function))
  136. static_cast<ECMAScriptFunctionObject*>(&function)->set_name(DeprecatedString::formatted("set {}", name));
  137. object->define_direct_accessor(name, nullptr, &function, Attribute::Configurable | Attribute::Enumerable);
  138. break;
  139. }
  140. case Op::PropertyKind::KeyValue: {
  141. bool succeeded = TRY(object->internal_set(name, value, this_value));
  142. if (!succeeded && vm.in_strict_mode())
  143. return vm.throw_completion<TypeError>(ErrorType::ReferenceNullishSetProperty, name, base.to_string_without_side_effects());
  144. break;
  145. }
  146. case Op::PropertyKind::DirectKeyValue:
  147. object->define_direct_property(name, value, Attribute::Enumerable | Attribute::Writable | Attribute::Configurable);
  148. break;
  149. case Op::PropertyKind::Spread:
  150. TRY(object->copy_data_properties(vm, value, {}));
  151. break;
  152. case Op::PropertyKind::ProtoSetter:
  153. if (value.is_object() || value.is_null())
  154. MUST(object->internal_set_prototype_of(value.is_object() ? &value.as_object() : nullptr));
  155. break;
  156. }
  157. return {};
  158. }
  159. template<typename InstructionType>
  160. ThrowCompletionOr<Value> perform_call(Interpreter& interpreter, InstructionType const& call, Value callee, MarkedVector<Value> argument_values)
  161. {
  162. auto& vm = interpreter.vm();
  163. auto this_value = interpreter.reg(call.this_value());
  164. auto& function = callee.as_function();
  165. Value return_value;
  166. if (call.call_type() == Op::CallType::DirectEval) {
  167. if (callee == interpreter.realm().intrinsics().eval_function())
  168. return_value = TRY(perform_eval(vm, !argument_values.is_empty() ? argument_values[0].value_or(JS::js_undefined()) : js_undefined(), vm.in_strict_mode() ? CallerMode::Strict : CallerMode::NonStrict, EvalMode::Direct));
  169. else
  170. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  171. } else if (call.call_type() == Op::CallType::Call)
  172. return_value = TRY(JS::call(vm, function, this_value, move(argument_values)));
  173. else
  174. return_value = TRY(construct(vm, function, move(argument_values)));
  175. return return_value;
  176. }
  177. template ThrowCompletionOr<Value> perform_call(Interpreter&, Op::Call const&, Value, MarkedVector<Value>);
  178. template ThrowCompletionOr<Value> perform_call(Interpreter&, Op::CallWithArgumentArray const&, Value, MarkedVector<Value>);
  179. }