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&, ExecState*, 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&, ExecState*, 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() { return m_table.get(); }
75 void setTable(VM& vm, JSWebAssemblyTable* value) {
76 ASSERT(!table());
77 m_table.set(vm, this, value);
78 instance().setTable(makeRef(*table()->table()));
79 }
80
81 JSWebAssemblyModule* module() const { return m_module.get(); }
82
83 static size_t offsetOfInstance() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_instance); }
84 static size_t offsetOfCallee() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_callee); }
85 static size_t offsetOfVM() { return OBJECT_OFFSETOF(JSWebAssemblyInstance, m_vm); }
86
87protected:
88 JSWebAssemblyInstance(VM&, Structure*, Ref<Wasm::Instance>&&);
89 void finishCreation(VM&, JSWebAssemblyModule*, JSModuleNamespaceObject*);
90 static void destroy(JSCell*);
91 static void visitChildren(JSCell*, SlotVisitor&);
92
93private:
94 Ref<Wasm::Instance> m_instance;
95 VM* m_vm;
96
97 WriteBarrier<JSWebAssemblyModule> m_module;
98 WriteBarrier<JSWebAssemblyCodeBlock> m_codeBlock;
99 WriteBarrier<JSModuleNamespaceObject> m_moduleNamespaceObject;
100 WriteBarrier<JSWebAssemblyMemory> m_memory;
101 WriteBarrier<JSWebAssemblyTable> m_table;
102 WriteBarrier<WebAssemblyToJSCallee> m_callee;
103};
104
105} // namespace JSC
106
107#endif // ENABLE(WEBASSEMBLY)
108