2021-05-01 01:08:51 +04:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-01-25 20:19:05 +01:00
|
|
|
#include <AK/MemoryStream.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
|
|
#include <LibWasm/AbstractMachine/Interpreter.h>
|
2021-06-04 03:30:09 +04:30
|
|
|
#include <LibWasm/Printer/Printer.h>
|
2021-05-01 01:08:51 +04:30
|
|
|
|
|
|
|
namespace Wasm {
|
|
|
|
|
2021-06-04 03:42:11 +04:30
|
|
|
void Configuration::unwind(Badge<CallFrameHandle>, CallFrameHandle const& frame_handle)
|
2021-05-24 01:28:02 +04:30
|
|
|
{
|
2025-06-06 14:13:28 +02:00
|
|
|
m_frame_stack.take_last();
|
2021-05-24 01:28:02 +04:30
|
|
|
m_depth--;
|
2025-08-02 20:30:44 +02:00
|
|
|
m_ip = frame_handle.ip.value();
|
|
|
|
m_locals_base = m_frame_stack.is_empty() ? nullptr : m_frame_stack.unchecked_last().locals().data();
|
2021-05-24 01:28:02 +04:30
|
|
|
}
|
|
|
|
|
2021-05-24 02:04:58 +04:30
|
|
|
Result Configuration::call(Interpreter& interpreter, FunctionAddress address, Vector<Value> arguments)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
auto* function = m_store.get(address);
|
|
|
|
if (!function)
|
2025-04-22 09:48:26 +02:00
|
|
|
return Trap::from_string("Attempt to call nonexistent function by address");
|
2021-05-01 01:08:51 +04:30
|
|
|
if (auto* wasm_function = function->get_pointer<WasmFunction>()) {
|
2021-08-12 00:59:00 +04:30
|
|
|
Vector<Value> locals = move(arguments);
|
2024-07-27 16:44:36 -07:00
|
|
|
locals.ensure_capacity(locals.size() + wasm_function->code().func().locals().size());
|
|
|
|
for (auto& local : wasm_function->code().func().locals()) {
|
|
|
|
for (size_t i = 0; i < local.n(); ++i)
|
2024-08-17 15:40:21 -07:00
|
|
|
locals.append(Value(local.type()));
|
2024-07-27 16:44:36 -07:00
|
|
|
}
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2021-05-23 21:42:19 +04:30
|
|
|
set_frame(Frame {
|
2021-05-01 01:08:51 +04:30
|
|
|
wasm_function->module(),
|
|
|
|
move(locals),
|
2024-07-27 16:44:36 -07:00
|
|
|
wasm_function->code().func().body(),
|
2021-05-23 21:42:19 +04:30
|
|
|
wasm_function->type().results().size(),
|
|
|
|
});
|
2021-05-24 01:28:02 +04:30
|
|
|
m_ip = 0;
|
2021-05-24 02:04:58 +04:30
|
|
|
return execute(interpreter);
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
// It better be a host function, else something is really wrong.
|
|
|
|
auto& host_function = function->get<HostFunction>();
|
2021-05-17 12:29:44 +04:30
|
|
|
return host_function.function()(*this, arguments);
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
2021-05-24 02:04:58 +04:30
|
|
|
Result Configuration::execute(Interpreter& interpreter)
|
2021-05-01 01:08:51 +04:30
|
|
|
{
|
|
|
|
interpreter.interpret(*this);
|
2021-05-07 09:59:18 +04:30
|
|
|
if (interpreter.did_trap())
|
2025-04-22 09:48:26 +02:00
|
|
|
return interpreter.trap();
|
2021-05-01 01:08:51 +04:30
|
|
|
|
2025-06-06 14:20:07 +02:00
|
|
|
Vector<Value> results { value_stack().span().slice_from_end(frame().arity()) };
|
|
|
|
value_stack().shrink(value_stack().size() - results.size(), true);
|
|
|
|
results.reverse();
|
2024-08-02 20:51:40 -07:00
|
|
|
|
|
|
|
label_stack().take_last();
|
2021-05-23 21:21:17 +04:30
|
|
|
return Result { move(results) };
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|
|
|
|
|
2021-05-01 03:19:01 +04:30
|
|
|
void Configuration::dump_stack()
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> format, Ts... vs) {
|
2023-01-25 20:19:05 +01:00
|
|
|
AllocatingMemoryStream memory_stream;
|
2023-01-21 10:29:21 +01:00
|
|
|
Printer { memory_stream }.print(vs...);
|
2023-01-09 12:06:13 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
|
2023-03-01 15:27:35 +01:00
|
|
|
memory_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
|
2021-09-10 23:43:11 +02:00
|
|
|
dbgln(format.view(), StringView(buffer).trim_whitespace());
|
2021-06-04 03:30:09 +04:30
|
|
|
};
|
2024-08-02 20:51:40 -07:00
|
|
|
for (auto const& value : value_stack()) {
|
|
|
|
print_value(" {}", value);
|
2021-05-01 03:19:01 +04:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:08:51 +04:30
|
|
|
}
|