42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSWebAssembly);
43
44 EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
45 EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
46 EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
47
48 EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
49 {
50 VM& vm = exec->vm();
51 auto catchScope = DECLARE_CATCH_SCOPE(vm);
52
53 JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec->lexicalGlobalObject());
54 RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
55
56 // FIXME: Make this truly asynchronous:
57 // https://bugs.webkit.org/show_bug.cgi?id=166016
58 JSValue module = WebAssemblyModuleConstructor::createModule(exec, exec->argument(0), exec->lexicalGlobalObject()->WebAssemblyModuleStructure());
59 if (Exception* exception = catchScope.exception()) {
60 catchScope.clearException();
61 promise->reject(exec, exception->value());
62 return JSValue::encode(promise->promise());
63 }
64
65 promise->resolve(exec, module);
66 return JSValue::encode(promise->promise());
67 }
68
69 EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState* exec)
70 {
71 VM& vm = exec->vm();
72 auto catchScope = DECLARE_CATCH_SCOPE(vm);
73
74 // FIXME: Make this API truly asynchronous: https://bugs.webkit.org/show_bug.cgi?id=169187
75
76 JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec->lexicalGlobalObject());
77 RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
78
79 auto reject = [&] () {
80 Exception* exception = catchScope.exception();
81 ASSERT(exception);
82 catchScope.clearException();
83 promise->reject(exec, exception->value());
84 return JSValue::encode(promise->promise());
85 };
86
87 JSValue importArgument = exec->argument(1);
88 JSObject* importObject = importArgument.getObject();
89 if (!importArgument.isUndefined() && !importObject) {
90 promise->reject(exec, createTypeError(exec,
91 ASCIILiteral("second argument to WebAssembly.instantiate must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument)));
92 return JSValue::encode(promise->promise());
93 }
94
95 JSValue firstArgument = exec->argument(0);
96 JSValue module;
97 bool firstArgumentIsModule = false;
98 if (firstArgument.inherits(vm, JSWebAssemblyModule::info())) {
99 firstArgumentIsModule = true;
100 module = firstArgument;
101 } else {
102 module = WebAssemblyModuleConstructor::createModule(exec, firstArgument, exec->lexicalGlobalObject()->WebAssemblyModuleStructure());
103 if (catchScope.exception())
104 return reject();
105 }
106
107 JSWebAssemblyInstance* instance = WebAssemblyInstanceConstructor::createInstance(exec, jsCast<JSWebAssemblyModule*>(module), importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure());
108 if (catchScope.exception())
109 return reject();
110
111 if (firstArgumentIsModule)
112 promise->resolve(exec, instance);
113 else {
114 JSObject* result = constructEmptyObject(exec);
115 result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("module")), module);
116 result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("instance")), instance);
117 promise->resolve(exec, result);
118 }
119
120 return JSValue::encode(promise->promise());
121 }
122
123 EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
124 {
125 VM& vm = exec->vm();
126 auto scope = DECLARE_THROW_SCOPE(vm);
127
128 size_t byteOffset;
129 size_t byteSize;
130 uint8_t* base = getWasmBufferFromValue(exec, exec->argument(0), byteOffset, byteSize);
131 RETURN_IF_EXCEPTION(scope, encodedJSValue());
132 Wasm::Plan plan(&vm, base + byteOffset, byteSize);
133 // FIXME: We might want to throw an OOM exception here if we detect that something will OOM.
134 // https://bugs.webkit.org/show_bug.cgi?id=166015
135 return JSValue::encode(jsBoolean(plan.parseAndValidateModule()));
136 }
137