2020-03-12 19:53:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-12 19:53:31 +01:00
|
|
|
*/
|
|
|
|
|
2021-09-11 21:42:01 +01:00
|
|
|
#include <LibJS/Interpreter.h>
|
2021-09-22 12:44:56 +02:00
|
|
|
#include <LibJS/Runtime/FunctionEnvironment.h>
|
2020-04-17 19:59:32 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-12 19:53:31 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-06-27 22:38:42 +02:00
|
|
|
NativeFunction* NativeFunction::create(GlobalObject& global_object, const FlyString& name, Function<Value(VM&, GlobalObject&)> function)
|
2020-04-17 19:59:32 +02:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
return global_object.heap().allocate<NativeFunction>(global_object, name, move(function), *global_object.function_prototype());
|
2020-04-17 19:59:32 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 21:42:01 +01:00
|
|
|
// FIXME: m_realm is supposed to be the realm argument of CreateBuiltinFunction, or the current
|
|
|
|
// Realm Record. The former is not something that's commonly used or we support, the
|
|
|
|
// latter is impossible as no ExecutionContext exists when most NativeFunctions are created...
|
|
|
|
|
2020-04-17 19:59:32 +02:00
|
|
|
NativeFunction::NativeFunction(Object& prototype)
|
2021-06-27 21:48:34 +02:00
|
|
|
: FunctionObject(prototype)
|
2021-09-12 12:52:37 +01:00
|
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
2020-04-17 19:59:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-28 03:45:49 +03:00
|
|
|
NativeFunction::NativeFunction(FlyString name, Function<Value(VM&, GlobalObject&)> native_function, Object& prototype)
|
2021-06-27 21:48:34 +02:00
|
|
|
: FunctionObject(prototype)
|
2021-06-28 03:45:49 +03:00
|
|
|
, m_name(move(name))
|
2020-04-11 12:56:20 +01:00
|
|
|
, m_native_function(move(native_function))
|
2021-09-12 12:52:37 +01:00
|
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
2020-03-12 19:53:31 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-28 03:45:49 +03:00
|
|
|
NativeFunction::NativeFunction(FlyString name, Object& prototype)
|
2021-06-27 21:48:34 +02:00
|
|
|
: FunctionObject(prototype)
|
2021-06-28 03:45:49 +03:00
|
|
|
, m_name(move(name))
|
2021-09-12 12:52:37 +01:00
|
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
2020-04-13 01:07:31 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-12 19:53:31 +01:00
|
|
|
NativeFunction::~NativeFunction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:24:14 +02:00
|
|
|
Value NativeFunction::call()
|
2020-03-13 10:08:52 +01:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
return m_native_function(vm(), global_object());
|
2020-03-13 10:08:52 +01:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
Value NativeFunction::construct(FunctionObject&)
|
2020-04-01 18:31:24 +01:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:44:56 +02:00
|
|
|
FunctionEnvironment* NativeFunction::new_function_environment(Object* new_target)
|
2020-06-08 13:31:21 -05:00
|
|
|
{
|
2021-09-22 12:44:56 +02:00
|
|
|
// Simplified version of 9.1.2.4 NewFunctionEnvironment ( F, newTarget )
|
|
|
|
Environment* parent_scope = nullptr;
|
|
|
|
if (!vm().execution_context_stack().is_empty())
|
|
|
|
parent_scope = vm().lexical_environment();
|
|
|
|
|
|
|
|
auto* environment = heap().allocate<FunctionEnvironment>(global_object(), parent_scope);
|
|
|
|
environment->set_new_target(new_target ? new_target : js_undefined());
|
|
|
|
|
|
|
|
return environment;
|
2020-06-08 13:31:21 -05:00
|
|
|
}
|
|
|
|
|
2020-10-04 13:54:44 +02:00
|
|
|
bool NativeFunction::is_strict_mode() const
|
|
|
|
{
|
2021-09-22 12:44:56 +02:00
|
|
|
return true;
|
2020-10-04 13:54:44 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 19:53:31 +01:00
|
|
|
}
|