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 | #include "config.h" |
27 | #include "JSWebAssemblyTable.h" |
28 | |
29 | #if ENABLE(WEBASSEMBLY) |
30 | |
31 | #include "JSCInlines.h" |
32 | #include "JSWebAssemblyInstance.h" |
33 | #include <wtf/CheckedArithmetic.h> |
34 | |
35 | namespace JSC { |
36 | |
37 | const ClassInfo JSWebAssemblyTable::s_info = { "WebAssembly.Table" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyTable) }; |
38 | |
39 | JSWebAssemblyTable* JSWebAssemblyTable::create(JSGlobalObject* globalObject, VM& vm, Structure* structure, Ref<Wasm::Table>&& table) |
40 | { |
41 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
42 | |
43 | if (!globalObject->webAssemblyEnabled()) { |
44 | throwException(globalObject, throwScope, createEvalError(globalObject, globalObject->webAssemblyDisabledErrorMessage())); |
45 | return nullptr; |
46 | } |
47 | |
48 | auto* instance = new (NotNull, allocateCell<JSWebAssemblyTable>(vm.heap)) JSWebAssemblyTable(vm, structure, WTFMove(table)); |
49 | instance->table()->setOwner(instance); |
50 | instance->finishCreation(vm); |
51 | return instance; |
52 | } |
53 | |
54 | Structure* JSWebAssemblyTable::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
55 | { |
56 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
57 | } |
58 | |
59 | JSWebAssemblyTable::JSWebAssemblyTable(VM& vm, Structure* structure, Ref<Wasm::Table>&& table) |
60 | : Base(vm, structure) |
61 | , m_table(WTFMove(table)) |
62 | { |
63 | } |
64 | |
65 | void JSWebAssemblyTable::finishCreation(VM& vm) |
66 | { |
67 | Base::finishCreation(vm); |
68 | ASSERT(inherits(vm, info())); |
69 | } |
70 | |
71 | void JSWebAssemblyTable::destroy(JSCell* cell) |
72 | { |
73 | static_cast<JSWebAssemblyTable*>(cell)->JSWebAssemblyTable::~JSWebAssemblyTable(); |
74 | } |
75 | |
76 | void JSWebAssemblyTable::visitChildren(JSCell* cell, SlotVisitor& visitor) |
77 | { |
78 | JSWebAssemblyTable* thisObject = jsCast<JSWebAssemblyTable*>(cell); |
79 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
80 | |
81 | Base::visitChildren(thisObject, visitor); |
82 | thisObject->table()->visitAggregate(visitor); |
83 | } |
84 | |
85 | bool JSWebAssemblyTable::grow(uint32_t delta) |
86 | { |
87 | if (delta == 0) |
88 | return true; |
89 | return !!m_table->grow(delta); |
90 | } |
91 | |
92 | JSValue JSWebAssemblyTable::get(uint32_t index) |
93 | { |
94 | RELEASE_ASSERT(index < length()); |
95 | return m_table->get(index); |
96 | } |
97 | |
98 | void JSWebAssemblyTable::set(uint32_t index, JSValue value) |
99 | { |
100 | RELEASE_ASSERT(index < length()); |
101 | RELEASE_ASSERT(m_table->isAnyrefTable()); |
102 | m_table->set(index, value); |
103 | } |
104 | |
105 | void JSWebAssemblyTable::set(uint32_t index, WebAssemblyFunction* function) |
106 | { |
107 | RELEASE_ASSERT(index < length()); |
108 | RELEASE_ASSERT(m_table->asFuncrefTable()); |
109 | auto& subThis = *static_cast<Wasm::FuncRefTable*>(&m_table.get()); |
110 | subThis.setFunction(index, function, function->importableFunction(), &function->instance()->instance()); |
111 | } |
112 | |
113 | void JSWebAssemblyTable::set(uint32_t index, WebAssemblyWrapperFunction* function) |
114 | { |
115 | RELEASE_ASSERT(index < length()); |
116 | RELEASE_ASSERT(m_table->asFuncrefTable()); |
117 | auto& subThis = *static_cast<Wasm::FuncRefTable*>(&m_table.get()); |
118 | subThis.setFunction(index, function, function->importableFunction(), &function->instance()->instance()); |
119 | } |
120 | |
121 | void JSWebAssemblyTable::clear(uint32_t index) |
122 | { |
123 | RELEASE_ASSERT(index < length()); |
124 | m_table->clear(index); |
125 | } |
126 | |
127 | } // namespace JSC |
128 | |
129 | #endif // ENABLE(WEBASSEMBLY) |
130 | |