1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:17:35 +00:00

LibWasm+LibWeb: Implement (a very basic version of) the JS link/import

This allows Wasm code to call javascript functions.
This commit is contained in:
Ali Mohammad Pur 2021-05-17 12:29:44 +04:30 committed by Ali Mohammad Pur
parent a2af04837e
commit 3926eab3b7
6 changed files with 113 additions and 39 deletions

View file

@ -21,10 +21,10 @@ Optional<FunctionAddress> Store::allocate(ModuleInstance& module, const Module::
return address;
}
Optional<FunctionAddress> Store::allocate(const HostFunction& function)
Optional<FunctionAddress> Store::allocate(HostFunction&& function)
{
FunctionAddress address { m_functions.size() };
m_functions.empend(HostFunction { function });
m_functions.empend(HostFunction { move(function) });
return address;
}

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/HashTable.h>
#include <AK/OwnPtr.h>
@ -14,6 +15,8 @@
namespace Wasm {
class Configuration;
struct InstantiationError {
String error { "Unknown error" };
};
@ -235,17 +238,17 @@ private:
class HostFunction {
public:
explicit HostFunction(FlatPtr ptr, const FunctionType& type)
: m_ptr(ptr)
explicit HostFunction(AK::Function<Result(Configuration&, Vector<Value>&)> function, const FunctionType& type)
: m_function(move(function))
, m_type(type)
{
}
auto ptr() const { return m_ptr; }
auto& function() { return m_function; }
auto& type() const { return m_type; }
private:
FlatPtr m_ptr { 0 };
AK::Function<Result(Configuration&, Vector<Value>&)> m_function;
FunctionType m_type;
};
@ -352,7 +355,7 @@ public:
Store() = default;
Optional<FunctionAddress> allocate(ModuleInstance& module, const Module::Function& function);
Optional<FunctionAddress> allocate(const HostFunction&);
Optional<FunctionAddress> allocate(HostFunction&&);
Optional<TableAddress> allocate(const TableType&);
Optional<MemoryAddress> allocate(const MemoryType&);
Optional<GlobalAddress> allocate(const GlobalType&, Value);
@ -464,6 +467,12 @@ public:
// Link a bunch of qualified values, also matches 'module name'.
void link(const HashMap<Name, ExternValue>&);
auto& unresolved_imports()
{
populate();
return m_unresolved_imports;
}
AK::Result<Vector<ExternValue>, LinkError> finish();
private:

View file

@ -47,13 +47,7 @@ Result Configuration::call(FunctionAddress address, Vector<Value> arguments)
// It better be a host function, else something is really wrong.
auto& host_function = function->get<HostFunction>();
auto result = bit_cast<HostFunctionType>(host_function.ptr())(m_store, arguments);
auto count = host_function.type().results().size();
if (count == 0)
return Result { Vector<Value> {} };
if (count == 1)
return Result { Vector<Value> { Value { host_function.type().results().first(), result } } };
TODO();
return host_function.function()(*this, arguments);
}
Result Configuration::execute()

View file

@ -10,8 +10,6 @@
namespace Wasm {
typedef u64 (*HostFunctionType)(Store&, Vector<Value>&);
class Configuration {
public:
explicit Configuration(Store& store)