2 * Copyright (C) 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebAssemblyFunction.h"
29 #if ENABLE(WEBASSEMBLY)
31 #include "B3Compilation.h"
32 #include "JSCInlines.h"
33 #include "JSFunctionInlines.h"
35 #include "JSWebAssemblyCallee.h"
36 #include "JSWebAssemblyInstance.h"
37 #include "JSWebAssemblyMemory.h"
38 #include "LLIntThunks.h"
39 #include "ProtoCallFrame.h"
41 #include "WasmFormat.h"
42 #include "WasmMemory.h"
46 const ClassInfo WebAssemblyFunction::s_info = { "WebAssemblyFunction", &Base::s_info, nullptr, CREATE_METHOD_TABLE(WebAssemblyFunction) };
48 static EncodedJSValue JSC_HOST_CALL callWebAssemblyFunction(ExecState* exec)
51 auto scope = DECLARE_THROW_SCOPE(vm);
52 WebAssemblyFunction* wasmFunction = jsDynamicCast<WebAssemblyFunction*>(exec->jsCallee());
54 return JSValue::encode(throwException(exec, scope, createTypeError(exec, "expected a WebAssembly function", defaultSourceAppender, runtimeTypeForValue(exec->jsCallee()))));
55 const Wasm::Signature* signature = wasmFunction->signature();
57 // FIXME is this the right behavior? https://bugs.webkit.org/show_bug.cgi?id=164876
58 if (exec->argumentCount() != signature->arguments.size())
59 return JSValue::encode(throwException(exec, scope, createNotEnoughArgumentsError(exec, defaultSourceAppender)));
61 // FIXME is this boxing correct? https://bugs.webkit.org/show_bug.cgi?id=164876
62 Vector<JSValue> boxedArgs;
63 for (unsigned argIndex = 0; argIndex < exec->argumentCount(); ++argIndex) {
64 JSValue arg = exec->uncheckedArgument(argIndex);
65 switch (signature->arguments[argIndex]) {
67 arg = JSValue::decode(arg.toInt32(exec));
70 arg = JSValue::decode(bitwise_cast<uint32_t>(arg.toFloat(exec)));
73 arg = JSValue::decode(bitwise_cast<uint64_t>(arg.toNumber(exec)));
79 RELEASE_ASSERT_NOT_REACHED();
81 RETURN_IF_EXCEPTION(scope, { });
82 boxedArgs.append(arg);
85 JSValue firstArgument = JSValue();
87 JSValue* remainingArgs = nullptr;
88 if (boxedArgs.size()) {
89 remainingArgs = boxedArgs.data();
90 firstArgument = *remainingArgs;
92 argCount = boxedArgs.size();
95 // Note: we specifically use the WebAssemblyFunction as the callee to begin with in the ProtoCallFrame.
96 // The reason for this is that calling into the llint may stack overflow, and the stack overflow
97 // handler might read the global object from the callee. The JSWebAssemblyCallee doesn't have a
98 // global object, but the WebAssemblyFunction does.
99 ProtoCallFrame protoCallFrame;
100 protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
102 return wasmFunction->call(vm, &protoCallFrame);
105 EncodedJSValue WebAssemblyFunction::call(VM& vm, ProtoCallFrame* protoCallFrame)
107 // Setup the memory that the entrance loads.
108 if (JSWebAssemblyMemory* memory = instance()->memory()) {
109 Wasm::Memory* wasmMemory = memory->memory();
110 vm.topWasmMemoryPointer = wasmMemory->memory();
111 vm.topWasmMemorySize = wasmMemory->size();
113 vm.topWasmMemoryPointer = nullptr;
114 vm.topWasmMemorySize = 0;
117 JSWebAssemblyInstance* prevJSWebAssemblyInstance = vm.topJSWebAssemblyInstance;
118 vm.topJSWebAssemblyInstance = instance();
119 EncodedJSValue rawResult = vmEntryToWasm(webAssemblyCallee()->jsToWasmEntryPoint(), &vm, protoCallFrame);
120 vm.topJSWebAssemblyInstance = prevJSWebAssemblyInstance;
122 // FIXME is this correct? https://bugs.webkit.org/show_bug.cgi?id=164876
123 switch (signature()->returnType) {
125 return JSValue::encode(jsUndefined());
127 return JSValue::encode(JSValue(static_cast<int32_t>(rawResult)));
129 return JSValue::encode(JSValue(bitwise_cast<float>(static_cast<int32_t>(rawResult))));
131 return JSValue::encode(JSValue(bitwise_cast<double>(rawResult)));
138 RELEASE_ASSERT_NOT_REACHED();
139 return EncodedJSValue();
142 WebAssemblyFunction* WebAssemblyFunction::create(VM& vm, JSGlobalObject* globalObject, unsigned length, const String& name, JSWebAssemblyInstance* instance, JSWebAssemblyCallee* callee, Wasm::Signature* signature)
144 NativeExecutable* executable = vm.getHostFunction(callWebAssemblyFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name);
145 Structure* structure = globalObject->webAssemblyFunctionStructure();
146 WebAssemblyFunction* function = new (NotNull, allocateCell<WebAssemblyFunction>(vm.heap)) WebAssemblyFunction(vm, globalObject, structure);
147 function->finishCreation(vm, executable, length, name, instance, callee, signature);
151 Structure* WebAssemblyFunction::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
153 ASSERT(globalObject);
154 return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info());
157 WebAssemblyFunction::WebAssemblyFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure)
158 : Base(vm, globalObject, structure)
162 void WebAssemblyFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
164 WebAssemblyFunction* thisObject = jsCast<WebAssemblyFunction*>(cell);
165 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
166 Base::visitChildren(thisObject, visitor);
167 visitor.append(&thisObject->m_instance);
168 visitor.append(&thisObject->m_wasmCallee);
171 void WebAssemblyFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigned length, const String& name, JSWebAssemblyInstance* instance, JSWebAssemblyCallee* wasmCallee, Wasm::Signature* signature)
173 Base::finishCreation(vm, executable, length, name);
174 ASSERT(inherits(info()));
175 m_instance.set(vm, this, instance);
176 m_wasmCallee.set(vm, this, wasmCallee);
177 m_signature = signature;
182 #endif // ENABLE(WEBASSEMBLY)