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 "JSWebAssemblyCodeBlock.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "JSCInlines.h"
32#include "JSWebAssemblyLinkError.h"
33#include "JSWebAssemblyMemory.h"
34#include "WasmModuleInformation.h"
35#include "WasmToJS.h"
36
37
38namespace JSC {
39
40const ClassInfo JSWebAssemblyCodeBlock::s_info = { "WebAssemblyCodeBlock", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyCodeBlock) };
41
42JSWebAssemblyCodeBlock* JSWebAssemblyCodeBlock::create(VM& vm, Ref<Wasm::CodeBlock> codeBlock, const Wasm::ModuleInformation& moduleInformation)
43{
44 auto* result = new (NotNull, allocateCell<JSWebAssemblyCodeBlock>(vm.heap)) JSWebAssemblyCodeBlock(vm, WTFMove(codeBlock), moduleInformation);
45 result->finishCreation(vm);
46 return result;
47}
48
49JSWebAssemblyCodeBlock::JSWebAssemblyCodeBlock(VM& vm, Ref<Wasm::CodeBlock>&& codeBlock, const Wasm::ModuleInformation& moduleInformation)
50 : Base(vm, vm.webAssemblyCodeBlockStructure.get())
51 , m_codeBlock(WTFMove(codeBlock))
52{
53 // FIXME: We should not need to do this synchronously.
54 // https://bugs.webkit.org/show_bug.cgi?id=170567
55 m_wasmToJSExitStubs.reserveCapacity(m_codeBlock->functionImportCount());
56 for (unsigned importIndex = 0; importIndex < m_codeBlock->functionImportCount(); ++importIndex) {
57 Wasm::SignatureIndex signatureIndex = moduleInformation.importFunctionSignatureIndices.at(importIndex);
58 auto binding = Wasm::wasmToJS(vm, m_callLinkInfos, signatureIndex, importIndex);
59 if (UNLIKELY(!binding)) {
60 switch (binding.error()) {
61 case Wasm::BindingFailure::OutOfMemory:
62 m_errorMessage = "Out of executable memory"_s;
63 return;
64 }
65 RELEASE_ASSERT_NOT_REACHED();
66 }
67 m_wasmToJSExitStubs.uncheckedAppend(binding.value());
68 }
69}
70
71void JSWebAssemblyCodeBlock::finishCreation(VM& vm)
72{
73 Base::finishCreation(vm);
74}
75
76void JSWebAssemblyCodeBlock::destroy(JSCell* cell)
77{
78 static_cast<JSWebAssemblyCodeBlock*>(cell)->JSWebAssemblyCodeBlock::~JSWebAssemblyCodeBlock();
79}
80
81void JSWebAssemblyCodeBlock::clearJSCallICs(VM& vm)
82{
83 for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter)
84 (*iter)->unlink(vm);
85}
86
87void JSWebAssemblyCodeBlock::visitChildren(JSCell* cell, SlotVisitor& visitor)
88{
89 JSWebAssemblyCodeBlock* thisObject = jsCast<JSWebAssemblyCodeBlock*>(cell);
90 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
91
92 Base::visitChildren(thisObject, visitor);
93}
94
95void JSWebAssemblyCodeBlock::finalizeUnconditionally(VM& vm)
96{
97 for (auto iter = m_callLinkInfos.begin(); !!iter; ++iter)
98 (*iter)->visitWeak(vm);
99}
100
101} // namespace JSC
102
103#endif // ENABLE(WEBASSEMBLY)
104