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