1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

LibJS: Compile ScriptFunctions into bytecode and run them that way :^)

If there's a current Bytecode::Interpreter in action, ScriptFunction
will now compile itself into bytecode and execute in that context.

This patch also adds the Return bytecode instruction so that we can
actually return values from called functions. :^)

Return values are propagated from callee to caller via the caller's
$0 register. Bytecode::Interpreter now keeps a stack of register
"windows". These are not very efficient, but it should be pretty
straightforward to convert them to e.g a sliding register window
architecture later on.

This is pretty dang cool! :^)
This commit is contained in:
Andreas Kling 2021-06-05 15:53:36 +02:00
parent b609fc6d51
commit 80b1604b0a
7 changed files with 150 additions and 49 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Register.h>
#include <LibJS/Forward.h>
@ -14,25 +15,34 @@
namespace JS::Bytecode {
using RegisterWindow = Vector<Value>;
class Interpreter {
public:
explicit Interpreter(GlobalObject&);
~Interpreter();
// FIXME: Remove this thing once we don't need it anymore!
static Interpreter* current();
GlobalObject& global_object() { return m_global_object; }
VM& vm() { return m_vm; }
void run(Bytecode::Block const&);
Value run(Bytecode::Block const&);
Value& reg(Register const& r) { return m_registers[r.index()]; }
Value& reg(Register const& r) { return registers()[r.index()]; }
void jump(Label const& label) { m_pending_jump = label.address(); }
void do_return(Value return_value) { m_return_value = return_value; }
private:
RegisterWindow& registers() { return m_register_windows.last(); }
VM& m_vm;
GlobalObject& m_global_object;
Vector<Value> m_registers;
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
Optional<size_t> m_pending_jump;
Value m_return_value;
};
}