Skip to content

Commit 1ff9d33

Browse files
committed
LibJS: Make Function::call() not require an Interpreter&
This makes a difference inside ScriptFunction::call(), which will now instantiate a temporary Interpreter if one is not attached to the VM.
1 parent be31805 commit 1ff9d33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+167
-142
lines changed

Libraries/LibJS/Interpreter.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444

4545
namespace JS {
4646

47+
NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
48+
{
49+
DeferGC defer_gc(global_object.heap());
50+
auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
51+
interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
52+
return interpreter;
53+
}
54+
4755
Interpreter::Interpreter(VM& vm)
4856
: m_vm(vm)
4957
, m_console(*this)
@@ -84,28 +92,6 @@ const GlobalObject& Interpreter::global_object() const
8492
return static_cast<const GlobalObject&>(*m_global_object.cell());
8593
}
8694

87-
Value Interpreter::call_internal(Function& function, Value this_value, Optional<MarkedValueList> arguments)
88-
{
89-
ASSERT(!exception());
90-
91-
VM::InterpreterExecutionScope scope(*this);
92-
93-
auto& call_frame = vm().push_call_frame();
94-
call_frame.function_name = function.name();
95-
call_frame.this_value = function.bound_this().value_or(this_value);
96-
call_frame.arguments = function.bound_arguments();
97-
if (arguments.has_value())
98-
call_frame.arguments.append(arguments.value().values());
99-
call_frame.environment = function.create_environment();
100-
101-
ASSERT(call_frame.environment->this_binding_status() == LexicalEnvironment::ThisBindingStatus::Uninitialized);
102-
call_frame.environment->bind_this_value(call_frame.this_value);
103-
104-
auto result = function.call(*this);
105-
vm().pop_call_frame();
106-
return result;
107-
}
108-
10995
void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type, GlobalObject& global_object)
11096
{
11197
for (auto& declaration : scope_node.functions()) {

Libraries/LibJS/Interpreter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class Interpreter : public Weakable<Interpreter> {
5858
return interpreter;
5959
}
6060

61+
static NonnullOwnPtr<Interpreter> create_with_existing_global_object(GlobalObject&);
62+
6163
template<typename... Args>
6264
[[nodiscard]] ALWAYS_INLINE Value call(Function& function, Value this_value, Args... args)
6365
{
@@ -110,7 +112,10 @@ class Interpreter : public Weakable<Interpreter> {
110112
private:
111113
explicit Interpreter(VM&);
112114

113-
[[nodiscard]] Value call_internal(Function&, Value this_value, Optional<MarkedValueList>);
115+
[[nodiscard]] Value call_internal(Function& function, Value this_value, Optional<MarkedValueList> arguments)
116+
{
117+
return vm().call(function, this_value, move(arguments));
118+
}
114119

115120
NonnullRefPtr<VM> m_vm;
116121

Libraries/LibJS/Runtime/ArrayConstructor.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ void ArrayConstructor::initialize(GlobalObject& global_object)
5959
define_native_function("of", of, 0, attr);
6060
}
6161

62-
Value ArrayConstructor::call(Interpreter& interpreter)
62+
Value ArrayConstructor::call()
6363
{
64-
if (interpreter.argument_count() <= 0)
64+
if (vm().argument_count() <= 0)
6565
return Array::create(global_object());
6666

67-
if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
68-
auto array_length_value = interpreter.argument(0);
67+
if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
68+
auto array_length_value = vm().argument(0);
6969
if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
70-
interpreter.vm().throw_exception<TypeError>(global_object(), ErrorType::ArrayInvalidLength);
70+
vm().throw_exception<TypeError>(global_object(), ErrorType::ArrayInvalidLength);
7171
return {};
7272
}
7373
auto* array = Array::create(global_object());
@@ -76,14 +76,14 @@ Value ArrayConstructor::call(Interpreter& interpreter)
7676
}
7777

7878
auto* array = Array::create(global_object());
79-
for (size_t i = 0; i < interpreter.argument_count(); ++i)
80-
array->indexed_properties().append(interpreter.argument(i));
79+
for (size_t i = 0; i < vm().argument_count(); ++i)
80+
array->indexed_properties().append(vm().argument(i));
8181
return array;
8282
}
8383

84-
Value ArrayConstructor::construct(Interpreter& interpreter, Function&)
84+
Value ArrayConstructor::construct(Interpreter&, Function&)
8585
{
86-
return call(interpreter);
86+
return call();
8787
}
8888

8989
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)

Libraries/LibJS/Runtime/ArrayConstructor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ArrayConstructor final : public NativeFunction {
3838
virtual void initialize(GlobalObject&) override;
3939
virtual ~ArrayConstructor() override;
4040

41-
virtual Value call(Interpreter&) override;
41+
virtual Value call() override;
4242
virtual Value construct(Interpreter&, Function& new_target) override;
4343

4444
private:

Libraries/LibJS/Runtime/BigIntConstructor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ BigIntConstructor::~BigIntConstructor()
5454
{
5555
}
5656

57-
Value BigIntConstructor::call(Interpreter& interpreter)
57+
Value BigIntConstructor::call()
5858
{
59-
auto primitive = interpreter.argument(0).to_primitive(interpreter, Value::PreferredType::Number);
60-
if (interpreter.exception())
59+
auto primitive = vm().argument(0).to_primitive(Value::PreferredType::Number);
60+
if (vm().exception())
6161
return {};
6262
if (primitive.is_number()) {
6363
if (!primitive.is_integer()) {
64-
interpreter.vm().throw_exception<RangeError>(global_object(), ErrorType::BigIntIntArgument);
64+
vm().throw_exception<RangeError>(global_object(), ErrorType::BigIntIntArgument);
6565
return {};
6666
}
67-
return js_bigint(interpreter, Crypto::SignedBigInteger { primitive.as_i32() });
67+
return js_bigint(heap(), Crypto::SignedBigInteger { primitive.as_i32() });
6868
}
69-
auto* bigint = interpreter.argument(0).to_bigint(interpreter);
70-
if (interpreter.exception())
69+
auto* bigint = vm().argument(0).to_bigint(global_object());
70+
if (vm().exception())
7171
return {};
7272
return bigint;
7373
}

Libraries/LibJS/Runtime/BigIntConstructor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BigIntConstructor final : public NativeFunction {
3838
virtual void initialize(GlobalObject&) override;
3939
virtual ~BigIntConstructor() override;
4040

41-
virtual Value call(Interpreter&) override;
41+
virtual Value call() override;
4242
virtual Value construct(Interpreter&, Function& new_target) override;
4343

4444
private:

Libraries/LibJS/Runtime/BooleanConstructor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ BooleanConstructor::~BooleanConstructor()
4949
{
5050
}
5151

52-
Value BooleanConstructor::call(Interpreter& interpreter)
52+
Value BooleanConstructor::call()
5353
{
54-
return Value(interpreter.argument(0).to_boolean());
54+
return Value(vm().argument(0).to_boolean());
5555
}
5656

5757
Value BooleanConstructor::construct(Interpreter& interpreter, Function&)

Libraries/LibJS/Runtime/BooleanConstructor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BooleanConstructor final : public NativeFunction {
3838
virtual void initialize(GlobalObject&) override;
3939
virtual ~BooleanConstructor() override;
4040

41-
virtual Value call(Interpreter&) override;
41+
virtual Value call() override;
4242
virtual Value construct(Interpreter&, Function& new_target) override;
4343

4444
private:

Libraries/LibJS/Runtime/BoundFunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ BoundFunction::~BoundFunction()
4949
{
5050
}
5151

52-
Value BoundFunction::call(Interpreter& interpreter)
52+
Value BoundFunction::call()
5353
{
54-
return m_target_function->call(interpreter);
54+
return m_target_function->call();
5555
}
5656

5757
Value BoundFunction::construct(Interpreter& interpreter, Function& new_target)

Libraries/LibJS/Runtime/BoundFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BoundFunction final : public Function {
3838
virtual void initialize(GlobalObject&) override;
3939
virtual ~BoundFunction();
4040

41-
virtual Value call(Interpreter& interpreter) override;
41+
virtual Value call() override;
4242

4343
virtual Value construct(Interpreter&, Function& new_target) override;
4444

0 commit comments

Comments
 (0)