mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +00:00
LibWeb: Convert WebAssemblyObject AOs to ThrowCompletionOr
This commit is contained in:
parent
a76cd669b1
commit
3e8c76d5ab
6 changed files with 62 additions and 102 deletions
|
@ -36,12 +36,8 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(Fun
|
||||||
if (!is<WebAssemblyModuleObject>(module_argument))
|
if (!is<WebAssemblyModuleObject>(module_argument))
|
||||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
|
||||||
auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
|
auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
|
||||||
auto result = WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object);
|
auto result = TRY(WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object));
|
||||||
if (result.is_error()) {
|
return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result);
|
||||||
vm.throw_exception(global_object, result.error());
|
|
||||||
return JS::throw_completion(result.error());
|
|
||||||
}
|
|
||||||
return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)
|
void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)
|
||||||
|
|
|
@ -34,13 +34,9 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(Funct
|
||||||
auto& global_object = this->global_object();
|
auto& global_object = this->global_object();
|
||||||
|
|
||||||
auto* buffer_object = TRY(vm.argument(0).to_object(global_object));
|
auto* buffer_object = TRY(vm.argument(0).to_object(global_object));
|
||||||
auto result = parse_module(global_object, buffer_object);
|
auto result = TRY(parse_module(global_object, buffer_object));
|
||||||
if (result.is_error()) {
|
|
||||||
vm.throw_exception(global_object, result.error());
|
|
||||||
return JS::throw_completion(result.error());
|
|
||||||
}
|
|
||||||
|
|
||||||
return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value());
|
return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)
|
void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)
|
||||||
|
|
|
@ -94,8 +94,10 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyObject::validate)
|
||||||
return JS::Value { true };
|
return JS::Value { true };
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Object* buffer_object)
|
JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::Object* buffer_object)
|
||||||
{
|
{
|
||||||
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
ReadonlyBytes data;
|
ReadonlyBytes data;
|
||||||
if (is<JS::ArrayBuffer>(buffer_object)) {
|
if (is<JS::ArrayBuffer>(buffer_object)) {
|
||||||
auto& buffer = static_cast<JS::ArrayBuffer&>(*buffer_object);
|
auto& buffer = static_cast<JS::ArrayBuffer&>(*buffer_object);
|
||||||
|
@ -107,8 +109,7 @@ Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Obje
|
||||||
auto& buffer = static_cast<JS::DataView&>(*buffer_object);
|
auto& buffer = static_cast<JS::DataView&>(*buffer_object);
|
||||||
data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length());
|
data = buffer.viewed_array_buffer()->buffer().span().slice(buffer.byte_offset(), buffer.byte_length());
|
||||||
} else {
|
} else {
|
||||||
auto error = JS::TypeError::create(global_object, "Not a BufferSource");
|
return vm.throw_completion<JS::TypeError>(global_object, "Not a BufferSource");
|
||||||
return JS::Value { error };
|
|
||||||
}
|
}
|
||||||
InputMemoryStream stream { data };
|
InputMemoryStream stream { data };
|
||||||
auto module_result = Wasm::Module::parse(stream);
|
auto module_result = Wasm::Module::parse(stream);
|
||||||
|
@ -119,8 +120,7 @@ Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Obje
|
||||||
};
|
};
|
||||||
if (module_result.is_error()) {
|
if (module_result.is_error()) {
|
||||||
// FIXME: Throw CompileError instead.
|
// FIXME: Throw CompileError instead.
|
||||||
auto error = JS::TypeError::create(global_object, Wasm::parse_error_to_string(module_result.error()));
|
return vm.throw_completion<JS::TypeError>(global_object, Wasm::parse_error_to_string(module_result.error()));
|
||||||
return JS::Value { error };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebAssemblyObject::s_compiled_modules.append(make<WebAssemblyObject::CompiledWebAssemblyModule>(module_result.release_value()));
|
WebAssemblyObject::s_compiled_modules.append(make<WebAssemblyObject::CompiledWebAssemblyModule>(module_result.release_value()));
|
||||||
|
@ -145,26 +145,19 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyObject::compile)
|
||||||
auto* buffer = buffer_or_error.release_value();
|
auto* buffer = buffer_or_error.release_value();
|
||||||
auto result = parse_module(global_object, buffer);
|
auto result = parse_module(global_object, buffer);
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
promise->reject(result.error());
|
promise->reject(result.release_error().value());
|
||||||
else
|
else
|
||||||
promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.value()));
|
promise->fulfill(vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value()));
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module const& module, JS::VM& vm, JS::GlobalObject& global_object)
|
JS::ThrowCompletionOr<size_t> WebAssemblyObject::instantiate_module(Wasm::Module const& module, JS::VM& vm, JS::GlobalObject& global_object)
|
||||||
{
|
{
|
||||||
Wasm::Linker linker { module };
|
Wasm::Linker linker { module };
|
||||||
HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
|
HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
|
||||||
auto import_argument = vm.argument(1);
|
auto import_argument = vm.argument(1);
|
||||||
if (!import_argument.is_undefined()) {
|
if (!import_argument.is_undefined()) {
|
||||||
auto import_object_or_error = import_argument.to_object(global_object);
|
auto* import_object = TRY(import_argument.to_object(global_object));
|
||||||
if (import_object_or_error.is_error()) {
|
|
||||||
vm.clear_exception();
|
|
||||||
vm.stop_unwind();
|
|
||||||
return import_object_or_error.throw_completion().value();
|
|
||||||
}
|
|
||||||
[[maybe_unused]] auto* import_object = import_object_or_error.release_value();
|
|
||||||
|
|
||||||
dbgln("Trying to resolve stuff because import object was specified");
|
dbgln("Trying to resolve stuff because import object was specified");
|
||||||
for (const Wasm::Linker::Name& import_name : linker.unresolved_imports()) {
|
for (const Wasm::Linker::Name& import_name : linker.unresolved_imports()) {
|
||||||
dbgln("Trying to resolve {}::{}", import_name.module, import_name.name);
|
dbgln("Trying to resolve {}::{}", import_name.module, import_name.name);
|
||||||
|
@ -180,13 +173,13 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
if (import_or_error.is_error())
|
if (import_or_error.is_error())
|
||||||
break;
|
break;
|
||||||
auto import_ = import_or_error.release_value();
|
auto import_ = import_or_error.release_value();
|
||||||
import_name.type.visit(
|
TRY(import_name.type.visit(
|
||||||
[&](Wasm::TypeIndex index) {
|
[&](Wasm::TypeIndex index) -> JS::ThrowCompletionOr<void> {
|
||||||
dbgln("Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value());
|
dbgln("Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value());
|
||||||
auto& type = module.type(index);
|
auto& type = module.type(index);
|
||||||
// FIXME: IsCallable()
|
// FIXME: IsCallable()
|
||||||
if (!import_.is_function())
|
if (!import_.is_function())
|
||||||
return;
|
return {};
|
||||||
auto& function = import_.as_function();
|
auto& function = import_.as_function();
|
||||||
// FIXME: If this is a function created by create_native_function(),
|
// FIXME: If this is a function created by create_native_function(),
|
||||||
// just extract its address and resolve to that.
|
// just extract its address and resolve to that.
|
||||||
|
@ -205,11 +198,11 @@ 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(global_object, result_or_error.release_value(), type.results().first());
|
auto value_or_error = to_webassembly_value(global_object, result_or_error.release_value(), type.results().first());
|
||||||
if (!value.has_value())
|
if (value_or_error.is_error())
|
||||||
return Wasm::Trap {};
|
return Wasm::Trap {};
|
||||||
|
|
||||||
return Wasm::Result { Vector<Wasm::Value> { value.release_value() } };
|
return Wasm::Result { Vector<Wasm::Value> { value_or_error.release_value() } };
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Multiple returns
|
// FIXME: Multiple returns
|
||||||
|
@ -223,67 +216,57 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
VERIFY(address.has_value());
|
VERIFY(address.has_value());
|
||||||
|
|
||||||
resolved_imports.set(import_name, Wasm::ExternValue { Wasm::FunctionAddress { *address } });
|
resolved_imports.set(import_name, Wasm::ExternValue { Wasm::FunctionAddress { *address } });
|
||||||
|
return {};
|
||||||
},
|
},
|
||||||
[&](Wasm::GlobalType const& type) {
|
[&](Wasm::GlobalType const& type) -> JS::ThrowCompletionOr<void> {
|
||||||
Optional<Wasm::GlobalAddress> address;
|
Optional<Wasm::GlobalAddress> address;
|
||||||
// https://webassembly.github.io/spec/js-api/#read-the-imports step 5.1
|
// https://webassembly.github.io/spec/js-api/#read-the-imports step 5.1
|
||||||
if (import_.is_number() || import_.is_bigint()) {
|
if (import_.is_number() || import_.is_bigint()) {
|
||||||
if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) {
|
if (import_.is_number() && type.type().kind() == Wasm::ValueType::I64) {
|
||||||
// FIXME: Throw a LinkError instead.
|
// FIXME: Throw a LinkError instead.
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a Number to a BigInteger");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a Number to a BigInteger");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) {
|
if (import_.is_bigint() && type.type().kind() != Wasm::ValueType::I64) {
|
||||||
// FIXME: Throw a LinkError instead.
|
// FIXME: Throw a LinkError instead.
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Import resolution attempted to cast a BigInteger to a Number");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
auto cast_value = to_webassembly_value(global_object, import_, type.type());
|
auto cast_value = TRY(to_webassembly_value(global_object, import_, type.type()));
|
||||||
if (!cast_value.has_value())
|
address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value);
|
||||||
return;
|
|
||||||
address = s_abstract_machine.store().allocate({ type.type(), false }, cast_value.release_value());
|
|
||||||
} else {
|
} else {
|
||||||
// FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2
|
// FIXME: https://webassembly.github.io/spec/js-api/#read-the-imports step 5.2
|
||||||
// if v implements Global
|
// if v implements Global
|
||||||
// let globaladdr be v.[[Global]]
|
// let globaladdr be v.[[Global]]
|
||||||
|
|
||||||
// FIXME: Throw a LinkError instead
|
// FIXME: Throw a LinkError instead
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Invalid value for global type");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Invalid value for global type");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolved_imports.set(import_name, Wasm::ExternValue { *address });
|
resolved_imports.set(import_name, Wasm::ExternValue { *address });
|
||||||
|
return {};
|
||||||
},
|
},
|
||||||
[&](Wasm::MemoryType const&) {
|
[&](Wasm::MemoryType const&) -> JS::ThrowCompletionOr<void> {
|
||||||
if (!import_.is_object() || !is<WebAssemblyMemoryObject>(import_.as_object())) {
|
if (!import_.is_object() || !is<WebAssemblyMemoryObject>(import_.as_object())) {
|
||||||
// FIXME: Throw a LinkError instead
|
// FIXME: Throw a LinkError instead
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Memory for a memory import");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Memory for a memory import");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
auto address = static_cast<WebAssemblyMemoryObject const&>(import_.as_object()).address();
|
auto address = static_cast<WebAssemblyMemoryObject const&>(import_.as_object()).address();
|
||||||
resolved_imports.set(import_name, Wasm::ExternValue { address });
|
resolved_imports.set(import_name, Wasm::ExternValue { address });
|
||||||
|
return {};
|
||||||
},
|
},
|
||||||
[&](Wasm::TableType const&) {
|
[&](Wasm::TableType const&) -> JS::ThrowCompletionOr<void> {
|
||||||
if (!import_.is_object() || !is<WebAssemblyTableObject>(import_.as_object())) {
|
if (!import_.is_object() || !is<WebAssemblyTableObject>(import_.as_object())) {
|
||||||
// FIXME: Throw a LinkError instead
|
// FIXME: Throw a LinkError instead
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Table for a table import");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Expected an instance of WebAssembly.Table for a table import");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
auto address = static_cast<WebAssemblyTableObject const&>(import_.as_object()).address();
|
auto address = static_cast<WebAssemblyTableObject const&>(import_.as_object()).address();
|
||||||
resolved_imports.set(import_name, Wasm::ExternValue { address });
|
resolved_imports.set(import_name, Wasm::ExternValue { address });
|
||||||
|
return {};
|
||||||
},
|
},
|
||||||
[&](const auto&) {
|
[&](const auto&) -> JS::ThrowCompletionOr<void> {
|
||||||
// FIXME: Implement these.
|
// FIXME: Implement these.
|
||||||
dbgln("Unimplemented import of non-function attempted");
|
dbgln("Unimplemented import of non-function attempted");
|
||||||
vm.throw_exception<JS::TypeError>(global_object, "LinkError: Not Implemented");
|
return vm.throw_completion<JS::TypeError>(global_object, "LinkError: Not Implemented");
|
||||||
});
|
}));
|
||||||
if (vm.exception())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto exception = vm.exception()) {
|
|
||||||
vm.clear_exception();
|
|
||||||
return exception->value();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,13 +277,13 @@ Result<size_t, JS::Value> WebAssemblyObject::instantiate_module(Wasm::Module con
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append("LinkError: Missing ");
|
builder.append("LinkError: Missing ");
|
||||||
builder.join(' ', link_result.error().missing_imports);
|
builder.join(' ', link_result.error().missing_imports);
|
||||||
return JS::Value(JS::TypeError::create(global_object, builder.build()));
|
return vm.throw_completion<JS::TypeError>(global_object, builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value());
|
auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value());
|
||||||
if (instance_result.is_error()) {
|
if (instance_result.is_error()) {
|
||||||
// FIXME: Throw a LinkError instead.
|
// FIXME: Throw a LinkError instead.
|
||||||
return JS::Value(JS::TypeError::create(global_object, instance_result.error().error));
|
return vm.throw_completion<JS::TypeError>(global_object, instance_result.error().error);
|
||||||
}
|
}
|
||||||
|
|
||||||
s_instantiated_modules.append(instance_result.release_value());
|
s_instantiated_modules.append(instance_result.release_value());
|
||||||
|
@ -327,10 +310,10 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
|
||||||
if (is<JS::ArrayBuffer>(buffer) || is<JS::TypedArrayBase>(buffer)) {
|
if (is<JS::ArrayBuffer>(buffer) || is<JS::TypedArrayBase>(buffer)) {
|
||||||
auto result = parse_module(global_object, buffer);
|
auto result = parse_module(global_object, buffer);
|
||||||
if (result.is_error()) {
|
if (result.is_error()) {
|
||||||
promise->reject(result.error());
|
promise->reject(result.release_error().value());
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
module = &WebAssemblyObject::s_compiled_modules.at(result.value()).module;
|
module = &WebAssemblyObject::s_compiled_modules.at(result.release_value()).module;
|
||||||
should_return_module = true;
|
should_return_module = true;
|
||||||
} else if (is<WebAssemblyModuleObject>(buffer)) {
|
} else if (is<WebAssemblyModuleObject>(buffer)) {
|
||||||
module = &static_cast<WebAssemblyModuleObject*>(buffer)->module();
|
module = &static_cast<WebAssemblyModuleObject*>(buffer)->module();
|
||||||
|
@ -343,9 +326,9 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyObject::instantiate)
|
||||||
|
|
||||||
auto result = instantiate_module(*module, vm, global_object);
|
auto result = instantiate_module(*module, vm, global_object);
|
||||||
if (result.is_error()) {
|
if (result.is_error()) {
|
||||||
promise->reject(result.release_error());
|
promise->reject(result.release_error().value());
|
||||||
} else {
|
} else {
|
||||||
auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.value());
|
auto instance_object = vm.heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.release_value());
|
||||||
if (should_return_module) {
|
if (should_return_module) {
|
||||||
auto object = JS::Object::create(global_object, nullptr);
|
auto object = JS::Object::create(global_object, nullptr);
|
||||||
object->define_direct_property("module", vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, s_compiled_modules.size() - 1), JS::default_attributes);
|
object->define_direct_property("module", vm.heap().allocate<WebAssemblyModuleObject>(global_object, global_object, s_compiled_modules.size() - 1), JS::default_attributes);
|
||||||
|
@ -381,14 +364,14 @@ JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value)
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type)
|
JS::ThrowCompletionOr<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();
|
||||||
|
|
||||||
switch (type.kind()) {
|
switch (type.kind()) {
|
||||||
case Wasm::ValueType::I64: {
|
case Wasm::ValueType::I64: {
|
||||||
auto bigint = TRY_OR_DISCARD(value.to_bigint(global_object));
|
auto bigint = TRY(value.to_bigint(global_object));
|
||||||
auto value = bigint->big_integer().divided_by(two_64).remainder;
|
auto value = bigint->big_integer().divided_by(two_64).remainder;
|
||||||
VERIFY(value.unsigned_value().trimmed_length() <= 2);
|
VERIFY(value.unsigned_value().trimmed_length() <= 2);
|
||||||
i64 integer = static_cast<i64>(value.unsigned_value().to_u64());
|
i64 integer = static_cast<i64>(value.unsigned_value().to_u64());
|
||||||
|
@ -397,15 +380,15 @@ Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::
|
||||||
return Wasm::Value { integer };
|
return Wasm::Value { integer };
|
||||||
}
|
}
|
||||||
case Wasm::ValueType::I32: {
|
case Wasm::ValueType::I32: {
|
||||||
auto _i32 = TRY_OR_DISCARD(value.to_i32(global_object));
|
auto _i32 = TRY(value.to_i32(global_object));
|
||||||
return Wasm::Value { static_cast<i32>(_i32) };
|
return Wasm::Value { static_cast<i32>(_i32) };
|
||||||
}
|
}
|
||||||
case Wasm::ValueType::F64: {
|
case Wasm::ValueType::F64: {
|
||||||
auto number = TRY_OR_DISCARD(value.to_double(global_object));
|
auto number = TRY(value.to_double(global_object));
|
||||||
return Wasm::Value { static_cast<double>(number) };
|
return Wasm::Value { static_cast<double>(number) };
|
||||||
}
|
}
|
||||||
case Wasm::ValueType::F32: {
|
case Wasm::ValueType::F32: {
|
||||||
auto number = TRY_OR_DISCARD(value.to_double(global_object));
|
auto number = TRY(value.to_double(global_object));
|
||||||
return Wasm::Value { static_cast<float>(number) };
|
return Wasm::Value { static_cast<float>(number) };
|
||||||
}
|
}
|
||||||
case Wasm::ValueType::FunctionReference:
|
case Wasm::ValueType::FunctionReference:
|
||||||
|
@ -421,8 +404,7 @@ Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Exported function");
|
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Exported function");
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
case Wasm::ValueType::ExternReference:
|
case Wasm::ValueType::ExternReference:
|
||||||
case Wasm::ValueType::NullExternReference:
|
case Wasm::ValueType::NullExternReference:
|
||||||
|
@ -448,13 +430,8 @@ JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm
|
||||||
|
|
||||||
// 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(global_object, vm.argument(index++), type);
|
values.append(TRY(to_webassembly_value(global_object, vm.argument(index++), type)));
|
||||||
if (result.has_value())
|
|
||||||
values.append(result.release_value());
|
|
||||||
else
|
|
||||||
return JS::throw_completion(vm.exception()->value());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values));
|
auto result = WebAssemblyObject::s_abstract_machine.invoke(address, move(values));
|
||||||
// FIXME: Use the convoluted mapping of errors defined in the spec.
|
// FIXME: Use the convoluted mapping of errors defined in the spec.
|
||||||
|
|
|
@ -14,10 +14,10 @@
|
||||||
namespace Web::Bindings {
|
namespace Web::Bindings {
|
||||||
|
|
||||||
class WebAssemblyMemoryObject;
|
class WebAssemblyMemoryObject;
|
||||||
Result<size_t, JS::Value> parse_module(JS::GlobalObject& global_object, JS::Object* buffer);
|
JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::Object* buffer);
|
||||||
JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name);
|
JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name);
|
||||||
JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value);
|
JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value);
|
||||||
Optional<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type);
|
JS::ThrowCompletionOr<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);
|
||||||
|
@ -29,7 +29,7 @@ public:
|
||||||
|
|
||||||
virtual void visit_edges(Cell::Visitor&) override;
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
|
||||||
static Result<size_t, JS::Value> instantiate_module(Wasm::Module const&, JS::VM&, JS::GlobalObject&);
|
static JS::ThrowCompletionOr<size_t> instantiate_module(Wasm::Module const&, JS::VM&, JS::GlobalObject&);
|
||||||
|
|
||||||
struct CompiledWebAssemblyModule {
|
struct CompiledWebAssemblyModule {
|
||||||
explicit CompiledWebAssemblyModule(Wasm::Module&& module)
|
explicit CompiledWebAssemblyModule(Wasm::Module&& module)
|
||||||
|
|
|
@ -62,17 +62,14 @@ JS::ThrowCompletionOr<JS::Object*> WebAssemblyTableConstructor::construct(Functi
|
||||||
return vm.throw_completion<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
|
return vm.throw_completion<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
|
||||||
|
|
||||||
auto value_value = TRY(descriptor->get("value"));
|
auto value_value = TRY(descriptor->get("value"));
|
||||||
auto reference_value = [&]() -> Optional<Wasm::Value> {
|
auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
|
||||||
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(global_object, value_value, *reference_type);
|
return to_webassembly_value(global_object, value_value, *reference_type);
|
||||||
}();
|
}());
|
||||||
|
|
||||||
if (auto* exception = vm.exception())
|
auto& reference = reference_value.value().get<Wasm::Reference>();
|
||||||
return JS::throw_completion(exception->value());
|
|
||||||
|
|
||||||
auto& reference = reference_value->value().get<Wasm::Reference>();
|
|
||||||
|
|
||||||
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
|
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::TableType { *reference_type, Wasm::Limits { initial, maximum } });
|
||||||
if (!address.has_value())
|
if (!address.has_value())
|
||||||
|
|
|
@ -36,17 +36,14 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
|
||||||
|
|
||||||
auto initial_size = table->elements().size();
|
auto initial_size = table->elements().size();
|
||||||
auto value_value = vm.argument(1);
|
auto value_value = vm.argument(1);
|
||||||
auto reference_value = [&]() -> Optional<Wasm::Value> {
|
auto reference_value = TRY_OR_DISCARD([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
|
||||||
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(global_object, value_value, table->type().element_type());
|
return to_webassembly_value(global_object, value_value, table->type().element_type());
|
||||||
}();
|
}());
|
||||||
|
|
||||||
if (!reference_value.has_value())
|
auto& reference = reference_value.value().get<Wasm::Reference>();
|
||||||
return {};
|
|
||||||
|
|
||||||
auto& reference = reference_value->value().get<Wasm::Reference>();
|
|
||||||
|
|
||||||
if (!table->grow(delta, reference)) {
|
if (!table->grow(delta, reference)) {
|
||||||
vm.throw_exception<JS::RangeError>(global_object, "Failed to grow table");
|
vm.throw_exception<JS::RangeError>(global_object, "Failed to grow table");
|
||||||
|
@ -105,17 +102,14 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto value_value = vm.argument(1);
|
auto value_value = vm.argument(1);
|
||||||
auto reference_value = [&]() -> Optional<Wasm::Value> {
|
auto reference_value = TRY_OR_DISCARD([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
|
||||||
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(global_object, value_value, table->type().element_type());
|
return to_webassembly_value(global_object, value_value, table->type().element_type());
|
||||||
}();
|
}());
|
||||||
|
|
||||||
if (!reference_value.has_value())
|
auto& reference = reference_value.value().get<Wasm::Reference>();
|
||||||
return {};
|
|
||||||
|
|
||||||
auto& reference = reference_value->value().get<Wasm::Reference>();
|
|
||||||
table->elements()[index] = reference;
|
table->elements()[index] = reference;
|
||||||
|
|
||||||
return JS::js_undefined();
|
return JS::js_undefined();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue