NativeFunction.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Interpreter.h>
  8. #include <LibJS/Runtime/FunctionEnvironment.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/NativeFunction.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. NativeFunction* NativeFunction::create(GlobalObject& global_object, const FlyString& name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> function)
  14. {
  15. return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype());
  16. }
  17. // FIXME: m_realm is supposed to be the realm argument of CreateBuiltinFunction, or the current
  18. // Realm Record. The former is not something that's commonly used or we support, the
  19. // latter is impossible as no ExecutionContext exists when most NativeFunctions are created...
  20. NativeFunction::NativeFunction(Object& prototype)
  21. : FunctionObject(prototype)
  22. , m_realm(global_object().associated_realm())
  23. {
  24. }
  25. NativeFunction::NativeFunction(FlyString name, Function<ThrowCompletionOr<Value>(VM&, GlobalObject&)> native_function, Object& prototype)
  26. : FunctionObject(prototype)
  27. , m_name(move(name))
  28. , m_native_function(move(native_function))
  29. , m_realm(global_object().associated_realm())
  30. {
  31. }
  32. NativeFunction::NativeFunction(FlyString name, Object& prototype)
  33. : FunctionObject(prototype)
  34. , m_name(move(name))
  35. , m_realm(global_object().associated_realm())
  36. {
  37. }
  38. NativeFunction::~NativeFunction()
  39. {
  40. }
  41. // NOTE: Do not attempt to DRY these, it's not worth it. The difference in return types (Value vs Object*),
  42. // called functions (call() vs construct(FunctionObject&)), and this value (passed vs uninitialized) make
  43. // these good candidates for a bit of code duplication :^)
  44. // 10.3.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-built-in-function-objects-call-thisargument-argumentslist
  45. ThrowCompletionOr<Value> NativeFunction::internal_call(Value this_argument, MarkedValueList arguments_list)
  46. {
  47. auto& vm = this->vm();
  48. auto& global_object = this->global_object();
  49. // 1. Let callerContext be the running execution context.
  50. auto& caller_context = vm.running_execution_context();
  51. // 2. If callerContext is not already suspended, suspend callerContext.
  52. // NOTE: We don't support this concept yet.
  53. // 3. Let calleeContext be a new execution context.
  54. ExecutionContext callee_context(heap());
  55. // 4. Set the Function of calleeContext to F.
  56. callee_context.function = this;
  57. callee_context.function_name = m_name;
  58. // 5. Let calleeRealm be F.[[Realm]].
  59. auto* callee_realm = m_realm;
  60. // NOTE: This non-standard fallback is needed until we can guarantee that literally
  61. // every function has a realm - especially in LibWeb that's sometimes not the case
  62. // when a function is created while no JS is running, as we currently need to rely on
  63. // that (:acid2:, I know - see set_event_handler_attribute() for an example).
  64. // If there's no 'current realm' either, we can't continue and crash.
  65. if (!callee_realm)
  66. callee_realm = vm.current_realm();
  67. VERIFY(callee_realm);
  68. // 6. Set the Realm of calleeContext to calleeRealm.
  69. callee_context.realm = callee_realm;
  70. // 7. Set the ScriptOrModule of calleeContext to null.
  71. // FIXME: Our execution context struct currently does not track this item.
  72. // 8. Perform any necessary implementation-defined initialization of calleeContext.
  73. callee_context.this_value = this_argument;
  74. callee_context.arguments.extend(move(arguments_list));
  75. callee_context.lexical_environment = caller_context.lexical_environment;
  76. callee_context.variable_environment = caller_context.variable_environment;
  77. // NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
  78. callee_context.is_strict_mode = vm.in_strict_mode();
  79. if (auto* interpreter = vm.interpreter_if_exists())
  80. callee_context.current_node = interpreter->current_node();
  81. // </8.> --------------------------------------------------------------------------
  82. // 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  83. vm.push_execution_context(callee_context, global_object);
  84. // 10. Let result be the Completion Record that is the result of evaluating F in a manner that conforms to the specification of F. thisArgument is the this value, argumentsList provides the named parameters, and the NewTarget value is undefined.
  85. auto result = call();
  86. // 11. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  87. vm.pop_execution_context();
  88. // 12. Return result.
  89. return result;
  90. }
  91. // 10.3.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-built-in-function-objects-construct-argumentslist-newtarget
  92. ThrowCompletionOr<Object*> NativeFunction::internal_construct(MarkedValueList arguments_list, FunctionObject& new_target)
  93. {
  94. auto& vm = this->vm();
  95. auto& global_object = this->global_object();
  96. // 1. Let callerContext be the running execution context.
  97. auto& caller_context = vm.running_execution_context();
  98. // 2. If callerContext is not already suspended, suspend callerContext.
  99. // NOTE: We don't support this concept yet.
  100. // 3. Let calleeContext be a new execution context.
  101. ExecutionContext callee_context(heap());
  102. // 4. Set the Function of calleeContext to F.
  103. callee_context.function = this;
  104. callee_context.function_name = m_name;
  105. // 5. Let calleeRealm be F.[[Realm]].
  106. auto* callee_realm = m_realm;
  107. // NOTE: This non-standard fallback is needed until we can guarantee that literally
  108. // every function has a realm - especially in LibWeb that's sometimes not the case
  109. // when a function is created while no JS is running, as we currently need to rely on
  110. // that (:acid2:, I know - see set_event_handler_attribute() for an example).
  111. // If there's no 'current realm' either, we can't continue and crash.
  112. if (!callee_realm)
  113. callee_realm = vm.current_realm();
  114. VERIFY(callee_realm);
  115. // 6. Set the Realm of calleeContext to calleeRealm.
  116. callee_context.realm = callee_realm;
  117. // 7. Set the ScriptOrModule of calleeContext to null.
  118. // FIXME: Our execution context struct currently does not track this item.
  119. // 8. Perform any necessary implementation-defined initialization of calleeContext.
  120. callee_context.arguments.extend(move(arguments_list));
  121. callee_context.lexical_environment = caller_context.lexical_environment;
  122. callee_context.variable_environment = caller_context.variable_environment;
  123. // NOTE: This is a LibJS specific hack for NativeFunction to inherit the strictness of its caller.
  124. callee_context.is_strict_mode = vm.in_strict_mode();
  125. if (auto* interpreter = vm.interpreter_if_exists())
  126. callee_context.current_node = interpreter->current_node();
  127. // </8.> --------------------------------------------------------------------------
  128. // 9. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.
  129. vm.push_execution_context(callee_context, global_object);
  130. // 10. Let result be the Completion Record that is the result of evaluating F in a manner that conforms to the specification of F. The this value is uninitialized, argumentsList provides the named parameters, and newTarget provides the NewTarget value.
  131. auto result = construct(new_target);
  132. // 11. Remove calleeContext from the execution context stack and restore callerContext as the running execution context.
  133. vm.pop_execution_context();
  134. // 12. Return result.
  135. return result;
  136. }
  137. ThrowCompletionOr<Value> NativeFunction::call()
  138. {
  139. return m_native_function(vm(), global_object());
  140. }
  141. ThrowCompletionOr<Object*> NativeFunction::construct(FunctionObject&)
  142. {
  143. // Needs to be overridden if [[Construct]] is needed.
  144. VERIFY_NOT_REACHED();
  145. }
  146. bool NativeFunction::is_strict_mode() const
  147. {
  148. return true;
  149. }
  150. }