LibWeb/WebAssembly: Define the hacky 'native' errors given in the spec

These cannot be implemented "correctly" as the set of native errors as
defined by ecma262 is closed, but we can get pretty close.
This commit is contained in:
Ali Mohammad Pur 2025-05-03 13:22:44 +03:30 committed by Andrew Kaster
commit 333ba93d49
Notes: github-actions[bot] 2025-05-08 09:36:39 +00:00
3 changed files with 238 additions and 1 deletions

View file

@ -12,6 +12,7 @@
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PrototypeObject.h>
#include <LibJS/Runtime/Value.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWeb/Forward.h>
@ -20,6 +21,7 @@ namespace Web::WebAssembly {
void visit_edges(JS::Object&, JS::Cell::Visitor&);
void finalize(JS::Object&);
void initialize(JS::Object&, JS::Realm&);
bool validate(JS::VM&, GC::Root<WebIDL::BufferSource>& bytes);
WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> compile(JS::VM&, GC::Root<WebIDL::BufferSource>& bytes);
@ -96,4 +98,69 @@ extern HashMap<GC::Ptr<JS::Object>, WebAssemblyCache> s_caches;
}
// NOTE: This is technically not allowed by ECMA262, as the set of native errors is closed
// our implementation uses this fact in places, but for the purposes of wasm returning
// *some* kind of error, named e.g. 'WebAssembly.RuntimeError', this is sufficient.
#define DECLARE_WASM_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
class ClassName final : public JS::Error { \
JS_OBJECT(ClassName, Error); \
GC_DECLARE_ALLOCATOR(ClassName); \
\
public: \
static GC::Ref<ClassName> create(JS::Realm&); \
static GC::Ref<ClassName> create(JS::Realm&, String message); \
static GC::Ref<ClassName> create(JS::Realm&, StringView message); \
\
explicit ClassName(Object& prototype); \
virtual ~ClassName() override = default; \
};
#define DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) \
class ConstructorName final : public JS::NativeFunction { \
JS_OBJECT(ConstructorName, NativeFunction); \
GC_DECLARE_ALLOCATOR(ConstructorName); \
\
public: \
virtual void initialize(JS::Realm&) override; \
virtual ~ConstructorName() override; \
virtual JS::ThrowCompletionOr<JS::Value> call() override; \
virtual JS::ThrowCompletionOr<GC::Ref<JS::Object>> construct(JS::FunctionObject& new_target) override; \
\
private: \
explicit ConstructorName(JS::Realm&); \
\
virtual bool has_constructor() const override \
{ \
return true; \
} \
};
#define DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(ClassName, snake_name, PrototypeName, ConstructorName) \
class PrototypeName final : public JS::PrototypeObject<PrototypeName, ClassName> { \
JS_PROTOTYPE_OBJECT(PrototypeName, ClassName, ClassName); \
GC_DECLARE_ALLOCATOR(PrototypeName); \
\
public: \
virtual void initialize(JS::Realm&) override; \
virtual ~PrototypeName() override = default; \
\
private: \
explicit PrototypeName(JS::Realm&); \
};
DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(CompilError, compile_error, CompileErrorPrototype, CompileErrorConstructor)
DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor)
DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR(RuntimeError, runtime_error, RuntimeErrorPrototype, RuntimeErrorConstructor)
DECLARE_WASM_NATIVE_ERROR(CompileError, compile_error, CompileErrorPrototype, CompileErrorConstructor)
DECLARE_WASM_NATIVE_ERROR(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor)
DECLARE_WASM_NATIVE_ERROR(RuntimeError, runtime_error, LinkErrorPrototype, LinkErrorConstructor)
DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(CompileError, compile_error, CompileErrorPrototype, CompileErrorConstructor)
DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(LinkError, link_error, LinkErrorPrototype, LinkErrorConstructor)
DECLARE_WASM_NATIVE_ERROR_PROTOTYPE(RuntimeError, runtime_error, RuntimeErrorPrototype, LinkErrorConstructor)
#undef DECLARE_WASM_NATIVE_ERROR
#undef DECLARE_WASM_NATIVE_ERROR_PROTOTYPE
#undef DECLARE_WASM_NATIVE_ERROR_CONSTRUCTOR
}