mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 16:07:46 +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:
parent
a2af04837e
commit
3926eab3b7
6 changed files with 113 additions and 39 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -10,8 +10,6 @@
|
|||
|
||||
namespace Wasm {
|
||||
|
||||
typedef u64 (*HostFunctionType)(Store&, Vector<Value>&);
|
||||
|
||||
class Configuration {
|
||||
public:
|
||||
explicit Configuration(Store& store)
|
||||
|
|
|
@ -998,6 +998,16 @@ public:
|
|||
|
||||
auto& sections() const { return m_sections; }
|
||||
auto& functions() const { return m_functions; }
|
||||
auto& type(TypeIndex index) const
|
||||
{
|
||||
const FunctionType* type = nullptr;
|
||||
for_each_section_of_type<TypeSection>([&](const TypeSection& section) {
|
||||
type = §ion.types().at(index.value());
|
||||
});
|
||||
|
||||
VERIFY(type != nullptr);
|
||||
return *type;
|
||||
}
|
||||
|
||||
template<typename T, typename Callback>
|
||||
void for_each_section_of_type(Callback&& callback) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue