jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 17ae490 | 2021-02-19 15:51:15 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2017-2021 Apple Inc. All rights reserved. |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 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 "WasmFormat.h" |
| 31 | #include "WasmLimits.h" |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 32 | #include "WriteBarrier.h" |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 33 | #include <wtf/MallocPtr.h> |
| 34 | #include <wtf/Optional.h> |
| 35 | #include <wtf/Ref.h> |
| 36 | #include <wtf/ThreadSafeRefCounted.h> |
| 37 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 38 | namespace JSC { namespace Wasm { |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 39 | |
jfbastien@apple.com | 89177d3 | 2017-10-26 15:33:55 +0000 | [diff] [blame] | 40 | class Instance; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 41 | class FuncRefTable; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 42 | |
| 43 | class Table : public ThreadSafeRefCounted<Table> { |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 44 | WTF_MAKE_NONCOPYABLE(Table); |
| 45 | WTF_MAKE_FAST_ALLOCATED(Table); |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 46 | public: |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 47 | static RefPtr<Table> tryCreate(uint32_t initial, Optional<uint32_t> maximum, TableElementType); |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 48 | |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 49 | JS_EXPORT_PRIVATE ~Table() = default; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 50 | |
cdumez@apple.com | 8b7a022 | 2018-12-20 04:41:11 +0000 | [diff] [blame] | 51 | Optional<uint32_t> maximum() const { return m_maximum; } |
jfbastien@apple.com | c384fff | 2018-01-09 07:10:36 +0000 | [diff] [blame] | 52 | uint32_t length() const { return m_length; } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 53 | |
jfbastien@apple.com | c384fff | 2018-01-09 07:10:36 +0000 | [diff] [blame] | 54 | static ptrdiff_t offsetOfLength() { return OBJECT_OFFSETOF(Table, m_length); } |
| 55 | static ptrdiff_t offsetOfMask() { return OBJECT_OFFSETOF(Table, m_mask); } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 56 | |
jfbastien@apple.com | c384fff | 2018-01-09 07:10:36 +0000 | [diff] [blame] | 57 | static uint32_t allocatedLength(uint32_t length); |
| 58 | uint32_t mask() const { return m_mask; } |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 59 | |
justin_michaud@apple.com | 6cae935 | 2019-06-18 22:01:02 +0000 | [diff] [blame] | 60 | template<typename T> T* owner() const { return reinterpret_cast<T*>(m_owner); } |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 61 | void setOwner(JSObject* owner) |
| 62 | { |
| 63 | ASSERT(!m_owner); |
| 64 | ASSERT(owner); |
| 65 | m_owner = owner; |
| 66 | } |
| 67 | |
| 68 | TableElementType type() const { return m_type; } |
commit-queue@webkit.org | c53450d | 2020-11-07 01:10:50 +0000 | [diff] [blame] | 69 | bool isExternrefTable() const { return m_type == TableElementType::Externref; } |
commit-queue@webkit.org | 91d305a | 2021-01-08 18:18:06 +0000 | [diff] [blame] | 70 | bool isFuncrefTable() const { return m_type == TableElementType::Funcref; } |
commit-queue@webkit.org | 2b24f92 | 2021-01-29 19:26:36 +0000 | [diff] [blame] | 71 | Type wasmType() const; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 72 | FuncRefTable* asFuncrefTable(); |
| 73 | |
jfbastien@apple.com | c384fff | 2018-01-09 07:10:36 +0000 | [diff] [blame] | 74 | static bool isValidLength(uint32_t length) { return length < maxTableEntries; } |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 75 | |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 76 | void clear(uint32_t); |
| 77 | void set(uint32_t, JSValue); |
| 78 | JSValue get(uint32_t) const; |
| 79 | |
commit-queue@webkit.org | 91d305a | 2021-01-08 18:18:06 +0000 | [diff] [blame] | 80 | Optional<uint32_t> grow(uint32_t delta, JSValue defaultValue); |
commit-queue@webkit.org | 0198ce7 | 2020-12-07 22:26:48 +0000 | [diff] [blame] | 81 | void copy(const Table* srcTable, uint32_t dstIndex, uint32_t srcIndex); |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 82 | |
mark.lam@apple.com | 17ae490 | 2021-02-19 15:51:15 +0000 | [diff] [blame^] | 83 | DECLARE_VISIT_AGGREGATE; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 84 | |
| 85 | protected: |
commit-queue@webkit.org | c53450d | 2020-11-07 01:10:50 +0000 | [diff] [blame] | 86 | Table(uint32_t initial, Optional<uint32_t> maximum, TableElementType = TableElementType::Externref); |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 87 | |
jfbastien@apple.com | c384fff | 2018-01-09 07:10:36 +0000 | [diff] [blame] | 88 | void setLength(uint32_t); |
| 89 | |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 90 | uint32_t m_length; |
| 91 | uint32_t m_mask; |
| 92 | const TableElementType m_type; |
| 93 | const Optional<uint32_t> m_maximum; |
| 94 | |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 95 | MallocPtr<WriteBarrier<Unknown>, VMMalloc> m_jsValues; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 96 | JSObject* m_owner; |
| 97 | }; |
| 98 | |
| 99 | class FuncRefTable : public Table { |
| 100 | public: |
| 101 | JS_EXPORT_PRIVATE ~FuncRefTable() = default; |
| 102 | |
| 103 | void setFunction(uint32_t, JSObject*, WasmToWasmImportableFunction, Instance*); |
tzagallo@apple.com | bf01be2 | 2019-10-31 22:32:52 +0000 | [diff] [blame] | 104 | const WasmToWasmImportableFunction& function(uint32_t) const; |
| 105 | Instance* instance(uint32_t) const; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 106 | |
commit-queue@webkit.org | 0198ce7 | 2020-12-07 22:26:48 +0000 | [diff] [blame] | 107 | void copyFunction(const FuncRefTable* srcTable, uint32_t dstIndex, uint32_t srcIndex); |
| 108 | |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 109 | static ptrdiff_t offsetOfFunctions() { return OBJECT_OFFSETOF(FuncRefTable, m_importableFunctions); } |
| 110 | static ptrdiff_t offsetOfInstances() { return OBJECT_OFFSETOF(FuncRefTable, m_instances); } |
| 111 | |
| 112 | private: |
| 113 | FuncRefTable(uint32_t initial, Optional<uint32_t> maximum); |
| 114 | |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 115 | MallocPtr<WasmToWasmImportableFunction, VMMalloc> m_importableFunctions; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 116 | // call_indirect needs to do an Instance check to potentially context switch when calling a function to another instance. We can hold raw pointers to Instance here because the embedder ensures that Table keeps all the instances alive. We couldn't hold a Ref here because it would cause cycles. |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 117 | MallocPtr<Instance*, VMMalloc> m_instances; |
justin_michaud@apple.com | 1f3b521 | 2019-06-06 03:14:47 +0000 | [diff] [blame] | 118 | |
| 119 | friend class Table; |
jfbastien@apple.com | d9f999e | 2017-10-20 02:23:29 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } } // namespace JSC::Wasm |
| 123 | |
| 124 | #endif // ENABLE(WEBASSEMBLY) |