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 // Setup the memory that the entrance loads.
96 if (JSWebAssemblyMemory* memory = wasmFunction->instance()->memory()) {
97 Wasm::Memory* wasmMemory = memory->memory();
98 vm.topWasmMemoryPointer = wasmMemory->memory();
99 vm.topWasmMemorySize = wasmMemory->size();
101 vm.topWasmMemoryPointer = nullptr;
102 vm.topWasmMemorySize = 0;
105 // Note: we specifically use the WebAsseblyFunction as the callee to begin with in the ProtoCallFrame.
106 // The reason for this is that calling into the llint may stack overflow, and the stack overflow
107 // handler might read the global object from the callee. The JSWebAssemblyCallee doesn't have a
108 // global object, but the WebAssemblyFunction does.
109 ProtoCallFrame protoCallFrame;
110 protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
112 JSWebAssemblyInstance* prevJSWebAssemblyInstance = vm.topJSWebAssemblyInstance;
113 vm.topJSWebAssemblyInstance = wasmFunction->instance();
114 EncodedJSValue rawResult = vmEntryToWasm(wasmFunction->webAssemblyCallee()->jsToWasmEntryPoint(), &vm, &protoCallFrame);
115 vm.topJSWebAssemblyInstance = prevJSWebAssemblyInstance;
117 // FIXME is this correct? https://bugs.webkit.org/show_bug.cgi?id=164876
118 switch (signature->returnType) {
120 return JSValue::encode(jsUndefined());
122 return JSValue::encode(JSValue(static_cast<int32_t>(rawResult)));
124 return JSValue::encode(JSValue(bitwise_cast<float>(static_cast<int32_t>(rawResult))));
126 return JSValue::encode(JSValue(bitwise_cast<double>(rawResult)));
133 RELEASE_ASSERT_NOT_REACHED();
134 return EncodedJSValue();
137 WebAssemblyFunction* WebAssemblyFunction::create(VM& vm, JSGlobalObject* globalObject, unsigned length, const String& name, JSWebAssemblyInstance* instance, JSWebAssemblyCallee* callee, Wasm::Signature* signature)
139 NativeExecutable* executable = vm.getHostFunction(callWebAssemblyFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name);
140 Structure* structure = globalObject->webAssemblyFunctionStructure();
141 WebAssemblyFunction* function = new (NotNull, allocateCell<WebAssemblyFunction>(vm.heap)) WebAssemblyFunction(vm, globalObject, structure);
142 function->finishCreation(vm, executable, length, name, instance, callee, signature);
146 Structure* WebAssemblyFunction::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
148 ASSERT(globalObject);
149 return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info());
152 WebAssemblyFunction::WebAssemblyFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure)
153 : Base(vm, globalObject, structure)
157 void WebAssemblyFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
159 WebAssemblyFunction* thisObject = jsCast<WebAssemblyFunction*>(cell);
160 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
161 Base::visitChildren(thisObject, visitor);
162 visitor.append(&thisObject->m_instance);
163 visitor.append(&thisObject->m_wasmCallee);
166 void WebAssemblyFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigned length, const String& name, JSWebAssemblyInstance* instance, JSWebAssemblyCallee* wasmCallee, Wasm::Signature* signature)
168 Base::finishCreation(vm, executable, length, name);
169 ASSERT(inherits(info()));
170 m_instance.set(vm, this, instance);
171 m_wasmCallee.set(vm, this, wasmCallee);
172 m_signature = signature;
177 #endif // ENABLE(WEBASSEMBLY)