NativeFunction.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. #include <LibJS/Runtime/Value.h>
  10. namespace JS {
  11. NativeFunction* NativeFunction::create(GlobalObject& global_object, const FlyString& name, Function<Value(VM&, GlobalObject&)> function)
  12. {
  13. return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype());
  14. }
  15. // FIXME: m_realm is supposed to be the realm argument of CreateBuiltinFunction, or the current
  16. // Realm Record. The former is not something that's commonly used or we support, the
  17. // latter is impossible as no ExecutionContext exists when most NativeFunctions are created...
  18. NativeFunction::NativeFunction(Object& prototype)
  19. : FunctionObject(prototype)
  20. , m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
  21. {
  22. }
  23. NativeFunction::NativeFunction(FlyString name, Function<Value(VM&, GlobalObject&)> native_function, Object& prototype)
  24. : FunctionObject(prototype)
  25. , m_name(move(name))
  26. , m_native_function(move(native_function))
  27. , m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
  28. {
  29. }
  30. NativeFunction::NativeFunction(FlyString name, Object& prototype)
  31. : FunctionObject(prototype)
  32. , m_name(move(name))
  33. , m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
  34. {
  35. }
  36. NativeFunction::~NativeFunction()
  37. {
  38. }
  39. Value NativeFunction::call()
  40. {
  41. return m_native_function(vm(), global_object());
  42. }
  43. Value NativeFunction::construct(FunctionObject&)
  44. {
  45. return {};
  46. }
  47. FunctionEnvironment* NativeFunction::create_environment(FunctionObject&)
  48. {
  49. return nullptr;
  50. }
  51. bool NativeFunction::is_strict_mode() const
  52. {
  53. return vm().in_strict_mode();
  54. }
  55. }