The best way to control Claude Code, Codex and more from any computer or phone

main
inc 90 lines 3.33 KB
Raw
1 /*
2 * Copyright 2018 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // This file is used as a template to generate code for table operations for
18 // funcref or externref. For this, the file must be included after defining
19 // either WASM_RT_TABLE_OPS_FUNCREF or WASM_RT_TABLE_OPS_EXTERNREF
20
21 #if defined(WASM_RT_TABLE_OPS_FUNCREF) + \
22 defined(WASM_RT_TABLE_OPS_EXTERNREF) + \
23 defined(WASM_RT_TABLE_OPS_EXNREF) > \
24 1
25 #error \
26 "Expected only one of { WASM_RT_TABLE_OPS_FUNCREF, WASM_RT_TABLE_OPS_EXTERNREF, WASM_RT_TABLE_OPS_EXNREF } to be defined"
27 #elif defined(WASM_RT_TABLE_OPS_FUNCREF) + \
28 defined(WASM_RT_TABLE_OPS_EXTERNREF) + \
29 defined(WASM_RT_TABLE_OPS_EXNREF) < \
30 1
31 #error \
32 "Expected one of { WASM_RT_TABLE_OPS_FUNCREF, WASM_RT_TABLE_OPS_EXTERNREF, WASM_RT_TABLE_OPS_EXNREF } to be defined"
33 #endif
34
35 #if defined(WASM_RT_TABLE_OPS_FUNCREF)
36 #define WASM_RT_TABLE_TYPE wasm_rt_funcref_table_t
37 #define WASM_RT_TABLE_ELEMENT_TYPE wasm_rt_funcref_t
38 #define WASM_RT_TABLE_APINAME(name) name##_funcref_table
39 #elif defined(WASM_RT_TABLE_OPS_EXTERNREF)
40 #define WASM_RT_TABLE_TYPE wasm_rt_externref_table_t
41 #define WASM_RT_TABLE_ELEMENT_TYPE wasm_rt_externref_t
42 #define WASM_RT_TABLE_APINAME(name) name##_externref_table
43 #elif defined(WASM_RT_TABLE_OPS_EXNREF)
44 #define WASM_RT_TABLE_TYPE wasm_rt_exnref_table_t
45 #define WASM_RT_TABLE_ELEMENT_TYPE wasm_rt_exnref_t
46 #define WASM_RT_TABLE_APINAME(name) name##_exnref_table
47 #endif
48
49 void WASM_RT_TABLE_APINAME(wasm_rt_allocate)(WASM_RT_TABLE_TYPE* table,
50 uint32_t elements,
51 uint32_t max_elements) {
52 table->size = elements;
53 table->max_size = max_elements;
54 table->data = calloc(table->size, sizeof(WASM_RT_TABLE_ELEMENT_TYPE));
55 }
56
57 void WASM_RT_TABLE_APINAME(wasm_rt_free)(WASM_RT_TABLE_TYPE* table) {
58 free(table->data);
59 }
60
61 uint32_t WASM_RT_TABLE_APINAME(wasm_rt_grow)(WASM_RT_TABLE_TYPE* table,
62 uint32_t delta,
63 WASM_RT_TABLE_ELEMENT_TYPE init) {
64 uint32_t old_elems = table->size;
65 uint64_t new_elems = (uint64_t)table->size + delta;
66 if (new_elems == 0) {
67 return 0;
68 }
69 if ((new_elems < old_elems) || (new_elems > table->max_size)) {
70 return (uint32_t)-1;
71 }
72 if (new_elems > SIZE_MAX / sizeof(WASM_RT_TABLE_ELEMENT_TYPE)) {
73 return (uint32_t)-1;
74 }
75 void* new_data =
76 realloc(table->data, new_elems * sizeof(WASM_RT_TABLE_ELEMENT_TYPE));
77 if (!new_data) {
78 return (uint32_t)-1;
79 }
80 table->data = new_data;
81 table->size = new_elems;
82 for (uint32_t i = old_elems; i < new_elems; i++) {
83 table->data[i] = init;
84 }
85 return old_elems;
86 }
87
88 #undef WASM_RT_TABLE_APINAME
89 #undef WASM_RT_TABLE_ELEMENT_TYPE
90 #undef WASM_RT_TABLE_TYPE