|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024-2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "dom.h" |
| 14 | +#include "refs.h" |
| 15 | +#include <stdint.h> |
| 16 | + |
| 17 | +static __externref_t table[0]; |
| 18 | + |
| 19 | +typedef __externref_t (*__funcref funcref_t)(__externref_t); |
| 20 | +static funcref_t ftable[0]; |
| 21 | + |
| 22 | +static int nextAvailableTableIndex = 0; |
| 23 | +static const int defaultTableGrowSize = 256; |
| 24 | + |
| 25 | +void freeExternRef(ExternRefIndex ref) { |
| 26 | + __builtin_wasm_table_set(table, ref.index, __builtin_wasm_ref_null_extern()); |
| 27 | +} |
| 28 | + |
| 29 | +ExternRefIndex tableAppend(__externref_t ref) { |
| 30 | + ExternRefIndex idx = { .index = nextAvailableTableIndex++ }; |
| 31 | + |
| 32 | + if (idx.index >= __builtin_wasm_table_size(table)) { |
| 33 | + __builtin_wasm_table_grow(table, __builtin_wasm_ref_null_extern(), defaultTableGrowSize); |
| 34 | + } |
| 35 | + |
| 36 | + __builtin_wasm_table_set(table, idx.index, ref); |
| 37 | + |
| 38 | + return idx; |
| 39 | +} |
| 40 | + |
| 41 | +ExternRefIndex createElement(ExternRefIndex name) { |
| 42 | + return tableAppend(createElementJS(__builtin_wasm_table_get(table, name.index))); |
| 43 | +} |
| 44 | + |
| 45 | +ExternRefIndex getDocument() { |
| 46 | + return tableAppend(getDocumentJS()); |
| 47 | +} |
| 48 | + |
| 49 | +ExternRefIndex getProp(ExternRefIndex self, ExternRefIndex name) { |
| 50 | + return tableAppend(getPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index))); |
| 51 | +} |
| 52 | + |
| 53 | +int getIntProp(ExternRefIndex self, ExternRefIndex name) { |
| 54 | + return getIntPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index)); |
| 55 | +} |
| 56 | + |
| 57 | +void setProp(ExternRefIndex self, ExternRefIndex name, ExternRefIndex val) { |
| 58 | + setPropJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, name.index), __builtin_wasm_table_get(table, val.index)); |
| 59 | +} |
| 60 | + |
| 61 | +void appendChild(ExternRefIndex self, ExternRefIndex child) { |
| 62 | + appendChildJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, child.index)); |
| 63 | +} |
| 64 | + |
| 65 | +ExternRefIndex bridgeString(const uint8_t *str, size_t bytes) { |
| 66 | + return tableAppend(bridgeStringJS(str, bytes)); |
| 67 | +} |
| 68 | + |
| 69 | +ExternRefIndex emptyArray() { |
| 70 | + return tableAppend(emptyArrayJS()); |
| 71 | +} |
| 72 | + |
| 73 | +void arrayPush(ExternRefIndex self, ExternRefIndex element) { |
| 74 | + arrayPushJS(__builtin_wasm_table_get(table, self.index), __builtin_wasm_table_get(table, element.index)); |
| 75 | +} |
0 commit comments