NativeFunction.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/LexicalEnvironment.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, AK::Function<Value(VM&, GlobalObject&)> function)
  12. {
  13. return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype());
  14. }
  15. NativeFunction::NativeFunction(Object& prototype)
  16. : Function(prototype)
  17. {
  18. }
  19. NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(VM&, GlobalObject&)> native_function, Object& prototype)
  20. : Function(prototype)
  21. , m_name(name)
  22. , m_native_function(move(native_function))
  23. {
  24. }
  25. NativeFunction::NativeFunction(const FlyString& name, Object& prototype)
  26. : Function(prototype)
  27. , m_name(name)
  28. {
  29. }
  30. NativeFunction::~NativeFunction()
  31. {
  32. }
  33. Value NativeFunction::call()
  34. {
  35. return m_native_function(vm(), global_object());
  36. }
  37. Value NativeFunction::construct(Function&)
  38. {
  39. return {};
  40. }
  41. LexicalEnvironment* NativeFunction::create_environment()
  42. {
  43. return heap().allocate<LexicalEnvironment>(global_object(), LexicalEnvironment::EnvironmentRecordType::Function);
  44. }
  45. bool NativeFunction::is_strict_mode() const
  46. {
  47. return vm().in_strict_mode();
  48. }
  49. }