1/*
2 * Copyright (C) 2016-2019 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#pragma once
27
28#if ENABLE(WEBASSEMBLY)
29
30#include "JSDestructibleObject.h"
31#include "JSObject.h"
32#include "JSWebAssemblyCodeBlock.h"
33#include "JSWebAssemblyMemory.h"
34#include "JSWebAssemblyTable.h"
35#include "WasmCreationMode.h"
36#include "WasmInstance.h"
37#include <wtf/Ref.h>
38
39namespace JSC {
40
41class JSModuleNamespaceObject;
42class JSWebAssemblyModule;
43class WebAssemblyToJSCallee;
44
45namespace Wasm {
46class CodeBlock;
47}
48
49class JSWebAssemblyInstance final : public JSDestructibleObject {
50public:
51 typedef JSDestructibleObject Base;
52
53 static Identifier createPrivateModuleKey();
54
55 static JSWebAssemblyInstance* create(VM&, JSGlobalObject*, const Identifier& moduleKey, JSWebAssemblyModule*, JSObject* importObject, Structure*, Ref<Wasm::Module>&&, Wasm::CreationMode);
56 static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
57
58 DECLARE_EXPORT_INFO;
59
60 void finalizeCreation(VM&, JSGlobalObject*, Ref<Wasm::CodeBlock>&&, JSObject* importObject, Wasm::CreationMode);
61
62 Wasm::Instance& instance() { return m_instance.get(); }
63 JSModuleNamespaceObject* moduleNamespaceObject() { return m_moduleNamespaceObject.get(); }
64 WebAssemblyToJSCallee* webAssemblyToJSCallee() { return m_callee.get(); }
65
66 JSWebAssemblyMemory* memory() { return m_memory.get(); }
67 void setMemory(VM& vm, JSWebAssemblyMemory* value) {
68 ASSERT(!memory());
69 m_memory.set(vm, this, value);
70 instance().setMemory(makeRef(memory()->memory()));
71 }
72 Wasm::MemoryMode memoryMode() { return memory()->memory().mode(); }
73
74 JSWebAssemblyTable* table(unsigned i) { return m_tables[i].get(); }
75 void setTable(VM& vm, uint32_t index, JSWebAssemblyTable* value)
76 {
77 ASSERT(index < m_tables.size());
78 ASSERT(!table(index));
79 m_tables[index].set(vm, this, value);
80 instance().setTable(index, makeRef(*table(index)->table()));
81 }
82
83 JSGlobalObject* globalObject() const { return m_globalObject.get(); }
84 JSWebAssemblyModule* module() const { return m_module.get(); }
85
86 static size_t offsetOfInstance() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_instance); }
87 static size_t offsetOfGlobalObject() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_globalObject); }
88 static size_t offsetOfCallee() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_callee); }
89 static size_t offsetOfVM() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_vm); }
90
91protected:
92 JSWebAssemblyInstance(VM&, Structure*, Ref<Wasm::Instance>&&);
93 void finishCreation(VM&, JSWebAssemblyModule*, JSModuleNamespaceObject*);
94 static void destroy(JSCell*);
95 static void visitChildren(JSCell*, SlotVisitor&);
96
97private:
98 Ref<Wasm::Instance> m_instance;
99 VM* m_vm;
100
101 WriteBarrier<JSGlobalObject> m_globalObject;
102 WriteBarrier<JSWebAssemblyModule> m_module;
103 WriteBarrier<JSWebAssemblyCodeBlock> m_codeBlock;
104 WriteBarrier<JSModuleNamespaceObject> m_moduleNamespaceObject;
105 WriteBarrier<JSWebAssemblyMemory> m_memory;
106 Vector<WriteBarrier<JSWebAssemblyTable>> m_tables;
107 WriteBarrier<WebAssemblyToJSCallee> m_callee;
108};
109
110} // namespace JSC
111
112#endif // ENABLE(WEBASSEMBLY)
113