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