1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "WebAssemblyWrapperFunction.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "Error.h"
32#include "FunctionPrototype.h"
33#include "JSCInlines.h"
34
35namespace JSC {
36
37const ClassInfo WebAssemblyWrapperFunction::s_info = { "WebAssemblyWrapperFunction", &Base::s_info, nullptr, CREATE_METHOD_TABLE(WebAssemblyWrapperFunction) };
38
39static EncodedJSValue JSC_HOST_CALL callWebAssemblyWrapperFunction(ExecState* exec)
40{
41 VM& vm = exec->vm();
42 auto scope = DECLARE_THROW_SCOPE(vm);
43 WebAssemblyWrapperFunction* wasmFunction = jsDynamicCast<WebAssemblyWrapperFunction*>(vm, exec->jsCallee());
44 if (!wasmFunction)
45 return JSValue::encode(throwException(exec, scope, createTypeError(exec, "expected a WebAssembly function")));
46
47 CallData callData;
48 JSObject* function = wasmFunction->function();
49 CallType callType = function->methodTable(vm)->getCallData(function, callData);
50 RELEASE_ASSERT(callType != CallType::None);
51 scope.release();
52 return JSValue::encode(call(exec, function, callType, callData, jsUndefined(), ArgList(exec)));
53}
54
55WebAssemblyWrapperFunction::WebAssemblyWrapperFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure, Wasm::SignatureIndex signatureIndex, void* wasmEntrypointCode)
56 : Base(vm, globalObject, structure)
57 , m_wasmEntrypointCode(wasmEntrypointCode)
58 , m_signatureIndex(signatureIndex)
59{ }
60
61WebAssemblyWrapperFunction* WebAssemblyWrapperFunction::create(VM& vm, JSGlobalObject* globalObject, JSObject* function, unsigned importIndex, JSWebAssemblyCodeBlock* codeBlock, Wasm::SignatureIndex signatureIndex)
62{
63 ASSERT_WITH_MESSAGE(!function->inherits(vm, WebAssemblyWrapperFunction::info()), "We should never double wrap a wrapper function.");
64 String name = "";
65 NativeExecutable* executable = vm.getHostFunction(callWebAssemblyWrapperFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name);
66 WebAssemblyWrapperFunction* result = new (NotNull, allocateCell<WebAssemblyWrapperFunction>(vm.heap)) WebAssemblyWrapperFunction(vm, globalObject, globalObject->webAssemblyWrapperFunctionStructure(), signatureIndex, codeBlock->wasmToJsCallStubForImport(importIndex));
67 const Wasm::Signature* signature = Wasm::SignatureInformation::get(&vm, signatureIndex);
68 result->finishCreation(vm, executable, signature->argumentCount(), name, function, codeBlock);
69 return result;
70}
71
72void WebAssemblyWrapperFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigned length, const String& name, JSObject* function, JSWebAssemblyCodeBlock* codeBlock)
73{
74 Base::finishCreation(vm, executable, length, name);
75 RELEASE_ASSERT(JSValue(function).isFunction());
76 m_function.set(vm, this, function);
77 m_codeBlock.set(vm, this, codeBlock);
78}
79
80Structure* WebAssemblyWrapperFunction::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
81{
82 ASSERT(globalObject);
83 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
84}
85
86void WebAssemblyWrapperFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
87{
88 WebAssemblyWrapperFunction* thisObject = jsCast<WebAssemblyWrapperFunction*>(cell);
89 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
90 Base::visitChildren(thisObject, visitor);
91
92 visitor.append(thisObject->m_codeBlock);
93 visitor.append(thisObject->m_function);
94}
95
96} // namespace JSC
97
98#endif // ENABLE(WEBASSEMBLY)