mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:37:37 +00:00
LibWeb: Make GlobalObject the first parameter of WebAssembly AOs
Let's be consistent with the rest of LibJS (and the rest of the file).
This commit is contained in:
parent
b883652a83
commit
a76cd669b1
5 changed files with 18 additions and 18 deletions
|
@ -36,7 +36,7 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object)
|
||||||
[&](const Wasm::FunctionAddress& address) {
|
[&](const Wasm::FunctionAddress& address) {
|
||||||
auto object = cache.function_instances.get(address);
|
auto object = cache.function_instances.get(address);
|
||||||
if (!object.has_value()) {
|
if (!object.has_value()) {
|
||||||
object = create_native_function(address, export_.name(), global_object);
|
object = create_native_function(global_object, address, export_.name());
|
||||||
cache.function_instances.set(address, *object);
|
cache.function_instances.set(address, *object);
|
||||||
}
|
}
|
||||||
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
|
m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes);
|
||||||
|
|
|
@ -194,7 +194,7 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
[&](auto&, auto& arguments) -> Wasm::Result {
|
[&](auto&, auto& arguments) -> Wasm::Result {
|
||||||
JS::MarkedValueList argument_values { vm.heap() };
|
JS::MarkedValueList argument_values { vm.heap() };
|
||||||
for (auto& entry : arguments)
|
for (auto& entry : arguments)
|
||||||
argument_values.append(to_js_value(entry, global_object));
|
argument_values.append(to_js_value(global_object, entry));
|
||||||
|
|
||||||
auto result_or_error = vm.call(function, JS::js_undefined(), move(argument_values));
|
auto result_or_error = vm.call(function, JS::js_undefined(), move(argument_values));
|
||||||
if (result_or_error.is_error()) {
|
if (result_or_error.is_error()) {
|
||||||
|
@ -205,7 +205,7 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
return Wasm::Result { Vector<Wasm::Value> {} };
|
return Wasm::Result { Vector<Wasm::Value> {} };
|
||||||
|
|
||||||
if (type.results().size() == 1) {
|
if (type.results().size() == 1) {
|
||||||
auto value = to_webassembly_value(result_or_error.release_value(), type.results().first(), global_object);
|
auto value = to_webassembly_value(global_object, result_or_error.release_value(), type.results().first());
|
||||||
if (!value.has_value())
|
if (!value.has_value())
|
||||||
return Wasm::Trap {};
|
return Wasm::Trap {};
|
||||||
|
|
||||||
|
@ -238,7 +238,7 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
|
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto cast_value = to_webassembly_value(import_, type.type(), global_object);
|
auto cast_value = to_webassembly_value(global_object, import_, type.type());
|
||||||
if (!cast_value.has_value())
|
if (!cast_value.has_value())
|
||||||
return;
|
return;
|
||||||
address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value.release_value());
|
address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value.release_value());
|
||||||
|
@ -358,7 +358,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object)
|
JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value)
|
||||||
{
|
{
|
||||||
switch (wasm_value.type().kind()) {
|
switch (wasm_value.type().kind()) {
|
||||||
case Wasm::ValueType::I64:
|
case Wasm::ValueType::I64:
|
||||||
|
@ -371,7 +371,7 @@ JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object)
|
||||||
return JS::Value(static_cast<double>(wasm_value.to<float>().value()));
|
return JS::Value(static_cast<double>(wasm_value.to<float>().value()));
|
||||||
case Wasm::ValueType::FunctionReference:
|
case Wasm::ValueType::FunctionReference:
|
||||||
// FIXME: What's the name of a function reference that isn't exported?
|
// FIXME: What's the name of a function reference that isn't exported?
|
||||||
return create_native_function(wasm_value.to<Wasm::Reference::Func>().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled", global_object);
|
return create_native_function(global_object, wasm_value.to<Wasm::Reference::Func>().value().address, "FIXME_IHaveNoIdeaWhatThisShouldBeCalled");
|
||||||
case Wasm::ValueType::NullFunctionReference:
|
case Wasm::ValueType::NullFunctionReference:
|
||||||
return JS::js_null();
|
return JS::js_null();
|
||||||
case Wasm::ValueType::ExternReference:
|
case Wasm::ValueType::ExternReference:
|
||||||
|
@ -381,7 +381,7 @@ JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object)
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueType& type, JS::GlobalObject& global_object)
|
Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type)
|
||||||
{
|
{
|
||||||
static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64);
|
static ::Crypto::SignedBigInteger two_64 = "1"_sbigint.shift_left(64);
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
@ -432,7 +432,7 @@ Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueTyp
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object)
|
JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name)
|
||||||
{
|
{
|
||||||
Optional<Wasm::FunctionType> type;
|
Optional<Wasm::FunctionType> type;
|
||||||
WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](const auto& value) { type = value.type(); });
|
WebAssemblyObject::s_abstract_machine.store().get(address)->visit([&](const auto& value) { type = value.type(); });
|
||||||
|
@ -449,7 +449,7 @@ JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String
|
||||||
// Grab as many values as needed and convert them.
|
// Grab as many values as needed and convert them.
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
for (auto& type : type.parameters()) {
|
for (auto& type : type.parameters()) {
|
||||||
auto result = to_webassembly_value(vm.argument(index++), type, global_object);
|
auto result = to_webassembly_value(global_object, vm.argument(index++), type);
|
||||||
if (result.has_value())
|
if (result.has_value())
|
||||||
values.append(result.release_value());
|
values.append(result.release_value());
|
||||||
else
|
else
|
||||||
|
@ -465,11 +465,11 @@ JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
|
|
||||||
if (result.values().size() == 1)
|
if (result.values().size() == 1)
|
||||||
return to_js_value(result.values().first(), global_object);
|
return to_js_value(global_object, result.values().first());
|
||||||
|
|
||||||
Vector<JS::Value> result_values;
|
Vector<JS::Value> result_values;
|
||||||
for (auto& entry : result.values())
|
for (auto& entry : result.values())
|
||||||
result_values.append(to_js_value(entry, global_object));
|
result_values.append(to_js_value(global_object, entry));
|
||||||
|
|
||||||
return JS::Value(JS::Array::create_from(global_object, result_values));
|
return JS::Value(JS::Array::create_from(global_object, result_values));
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,9 +15,9 @@ namespace Web::Bindings {
|
||||||
|
|
||||||
class WebAssemblyMemoryObject;
|
class WebAssemblyMemoryObject;
|
||||||
Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Object* buffer);
|
Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Object* buffer);
|
||||||
JS::NativeFunction* create_native_function(Wasm::FunctionAddress address, String name, JS::GlobalObject& global_object);
|
JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name);
|
||||||
JS::Value to_js_value(Wasm::Value& wasm_value, JS::GlobalObject& global_object);
|
JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value);
|
||||||
Optional<Wasm::Value> to_webassembly_value(JS::Value value, const Wasm::ValueType& type, JS::GlobalObject& global_object);
|
Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type);
|
||||||
|
|
||||||
class WebAssemblyObject final : public JS::Object {
|
class WebAssemblyObject final : public JS::Object {
|
||||||
JS_OBJECT(WebAssemblyObject, JS::Object);
|
JS_OBJECT(WebAssemblyObject, JS::Object);
|
||||||
|
|
|
@ -66,7 +66,7 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
|
||||||
if (value_value.is_undefined())
|
if (value_value.is_undefined())
|
||||||
return Wasm::Value(*reference_type, 0ull);
|
return Wasm::Value(*reference_type, 0ull);
|
||||||
|
|
||||||
return to_webassembly_value(value_value, *reference_type, global_object);
|
return to_webassembly_value(global_object, value_value, *reference_type);
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (auto* exception = vm.exception())
|
if (auto* exception = vm.exception())
|
||||||
|
|
|
@ -40,7 +40,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
|
||||||
if (value_value.is_undefined())
|
if (value_value.is_undefined())
|
||||||
return Wasm::Value(table->type().element_type(), 0ull);
|
return Wasm::Value(table->type().element_type(), 0ull);
|
||||||
|
|
||||||
return to_webassembly_value(value_value, table->type().element_type(), global_object);
|
return to_webassembly_value(global_object, value_value, table->type().element_type());
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (!reference_value.has_value())
|
if (!reference_value.has_value())
|
||||||
|
@ -81,7 +81,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
|
|
||||||
Wasm::Value wasm_value { ref.value() };
|
Wasm::Value wasm_value { ref.value() };
|
||||||
return to_js_value(wasm_value, global_object);
|
return to_js_value(global_object, wasm_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
||||||
|
@ -109,7 +109,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
||||||
if (value_value.is_undefined())
|
if (value_value.is_undefined())
|
||||||
return Wasm::Value(table->type().element_type(), 0ull);
|
return Wasm::Value(table->type().element_type(), 0ull);
|
||||||
|
|
||||||
return to_webassembly_value(value_value, table->type().element_type(), global_object);
|
return to_webassembly_value(global_object, value_value, table->type().element_type());
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (!reference_value.has_value())
|
if (!reference_value.has_value())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue