1 | /* |
2 | * Copyright (C) 2017-2018 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 | #include "JSWebAssemblyInstance.h" |
35 | #include "WasmSignatureInlines.h" |
36 | |
37 | namespace JSC { |
38 | |
39 | const ClassInfo WebAssemblyWrapperFunction::s_info = { "WebAssemblyWrapperFunction" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(WebAssemblyWrapperFunction) }; |
40 | |
41 | static EncodedJSValue JSC_HOST_CALL callWebAssemblyWrapperFunction(JSGlobalObject* globalObject, CallFrame* callFrame) |
42 | { |
43 | VM& vm = globalObject->vm(); |
44 | auto scope = DECLARE_THROW_SCOPE(vm); |
45 | WebAssemblyWrapperFunction* wasmFunction = jsCast<WebAssemblyWrapperFunction*>(callFrame->jsCallee()); |
46 | CallData callData; |
47 | JSObject* function = wasmFunction->function(); |
48 | CallType callType = function->methodTable(vm)->getCallData(function, callData); |
49 | RELEASE_ASSERT(callType != CallType::None); |
50 | RELEASE_AND_RETURN(scope, JSValue::encode(call(globalObject, function, callType, callData, jsUndefined(), ArgList(callFrame)))); |
51 | } |
52 | |
53 | WebAssemblyWrapperFunction::WebAssemblyWrapperFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure, Wasm::WasmToWasmImportableFunction importableFunction) |
54 | : Base(vm, globalObject, structure) |
55 | , m_importableFunction(importableFunction) |
56 | { } |
57 | |
58 | WebAssemblyWrapperFunction* WebAssemblyWrapperFunction::create(VM& vm, JSGlobalObject* globalObject, Structure* structure, JSObject* function, unsigned importIndex, JSWebAssemblyInstance* instance, Wasm::SignatureIndex signatureIndex) |
59 | { |
60 | ASSERT_WITH_MESSAGE(!function->inherits<WebAssemblyWrapperFunction>(vm), "We should never double wrap a wrapper function." ); |
61 | String name = "" ; |
62 | NativeExecutable* executable = vm.getHostFunction(callWebAssemblyWrapperFunction, NoIntrinsic, callHostFunctionAsConstructor, nullptr, name); |
63 | WebAssemblyWrapperFunction* result = new (NotNull, allocateCell<WebAssemblyWrapperFunction>(vm.heap)) WebAssemblyWrapperFunction(vm, globalObject, structure, Wasm::WasmToWasmImportableFunction { signatureIndex, &instance->instance().importFunctionInfo(importIndex)->wasmToEmbedderStub } ); |
64 | const Wasm::Signature& signature = Wasm::SignatureInformation::get(signatureIndex); |
65 | result->finishCreation(vm, executable, signature.argumentCount(), name, function, instance); |
66 | return result; |
67 | } |
68 | |
69 | void WebAssemblyWrapperFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigned length, const String& name, JSObject* function, JSWebAssemblyInstance* instance) |
70 | { |
71 | Base::finishCreation(vm, executable, length, name, instance); |
72 | RELEASE_ASSERT(JSValue(function).isFunction(vm)); |
73 | m_function.set(vm, this, function); |
74 | } |
75 | |
76 | Structure* WebAssemblyWrapperFunction::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
77 | { |
78 | ASSERT(globalObject); |
79 | return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info()); |
80 | } |
81 | |
82 | void WebAssemblyWrapperFunction::visitChildren(JSCell* cell, SlotVisitor& visitor) |
83 | { |
84 | WebAssemblyWrapperFunction* thisObject = jsCast<WebAssemblyWrapperFunction*>(cell); |
85 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
86 | Base::visitChildren(thisObject, visitor); |
87 | |
88 | visitor.append(thisObject->m_function); |
89 | } |
90 | |
91 | } // namespace JSC |
92 | |
93 | #endif // ENABLE(WEBASSEMBLY) |
94 | |