summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Bytecode/Op.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-06-05 15:53:36 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-07 18:11:59 +0200
commit80b1604b0a90b85f1fbdfecfcffcbf73e5b485bc (patch)
tree9f5a39f3b47842905c62bc3eb3096f04582937c2 /Userland/Libraries/LibJS/Bytecode/Op.cpp
parentb609fc6d516f0215a2df92f17c6137afde513c72 (diff)
downloadserenity-80b1604b0a90b85f1fbdfecfcffcbf73e5b485bc.zip
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! :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Bytecode/Op.cpp')
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Op.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp
index d40584067e..869dd169ce 100644
--- a/Userland/Libraries/LibJS/Bytecode/Op.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp
@@ -133,6 +133,12 @@ void EnterScope::execute(Bytecode::Interpreter& interpreter) const
// FIXME: Whatever else JS::Interpreter::enter_scope() does.
}
+void Return::execute(Bytecode::Interpreter& interpreter) const
+{
+ auto return_value = m_argument.has_value() ? interpreter.reg(m_argument.value()) : js_undefined();
+ interpreter.do_return(return_value);
+}
+
String Load::to_string() const
{
return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
@@ -228,4 +234,11 @@ String EnterScope::to_string() const
return "EnterScope";
}
+String Return::to_string() const
+{
+ if (m_argument.has_value())
+ return String::formatted("Return {}", m_argument.value());
+ return "Return";
+}
+
}