Skip to content

Commit 198d3c8

Browse files
authored
Change wasm-c-api vec initialization (#77)
Vectors for wasm-c-api seem to need to either be created with _new or by = <MACRO> to be portable. The macro form only works with arrays whos size are known at compile time. Here we use _new instead, which is less efficient but more portable.
1 parent a174328 commit 198d3c8

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/wasi-shim.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,13 @@ namespace godot {
197197
auto r_types = new std::vector<wasm_valtype_t*>;
198198
for (auto &it: std::get<0>(signature)) p_types->push_back(wasm_valtype_new(it));
199199
for (auto &it: std::get<1>(signature)) r_types->push_back(wasm_valtype_new(it));
200-
wasm_valtype_vec_t params = { p_types->size(), p_types->data() };
201-
wasm_valtype_vec_t results = { r_types->size(), r_types->data() };
200+
201+
wasm_valtype_vec_t params;
202+
wasm_valtype_vec_new(&params, p_types->size(), p_types->data());
203+
204+
wasm_valtype_vec_t results;
205+
wasm_valtype_vec_new(&results, r_types->size(), r_types->data());
206+
202207
wasm_functype_t* functype = wasm_functype_new(&params, &results);
203208
DEFER(wasm_functype_delete(functype));
204209
return wasm_func_new_with_env(store, functype, std::get<2>(signature), wasm, NULL);

src/wasm.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ namespace godot {
301301
// Load binary
302302
wasm_byte_vec_t wasm_bytes;
303303
DEFER(wasm_byte_vec_delete(&wasm_bytes));
304-
wasm_byte_vec_new_uninitialized(&wasm_bytes, bytecode.size());
305-
memcpy(wasm_bytes.data, BYTE_ARRAY_POINTER(bytecode), bytecode.size());
304+
wasm_byte_vec_new(&wasm_bytes, bytecode.size(), (const wasm_byte_t *)bytecode.ptr());
306305

307306
// Validate binary
308307
FAIL_IF(!wasm_module_validate(STORE, &wasm_bytes), "Invalid binary", ERR_INVALID_DATA);
@@ -320,7 +319,6 @@ namespace godot {
320319
godot_error Wasm::instantiate(const Dictionary import_map) {
321320
// Prepare module externs
322321
std::map<uint16_t, wasm_extern_t*> extern_map;
323-
DEFER(for (auto &it: extern_map) wasm_extern_delete(it.second));
324322

325323
// Construct import functions
326324
const Dictionary& functions = dict_safe_get(import_map, "functions", Dictionary());
@@ -356,7 +354,10 @@ namespace godot {
356354
// Sort imports by index
357355
std::vector<wasm_extern_t*> extern_list;
358356
for (auto &it: extern_map) extern_list.push_back(it.second); // Maps iterate over sorted keys
359-
wasm_extern_vec_t imports = { extern_list.size(), extern_list.data() };
357+
358+
wasm_extern_vec_t imports;
359+
DEFER(wasm_extern_vec_delete(&imports));
360+
wasm_extern_vec_new(&imports, extern_list.size(), extern_list.data());
360361

361362
// Instantiate with imports
362363
instance = wasm_instance_new(STORE, module, &imports, NULL);
@@ -452,20 +453,23 @@ namespace godot {
452453
FAIL_IF(value.kind == WASM_EXTERNREF, "Invalid argument type", NULL_VARIANT);
453454
args_vec.push_back(value);
454455
}
455-
wasm_val_vec_t f_args = { args_vec.size(), args_vec.data() };
456+
wasm_val_vec_t f_args;
457+
DEFER(wasm_val_vec_delete(&f_args));
458+
wasm_val_vec_new(&f_args, args_vec.size(), args_vec.data());
456459

457460
// Construct return values
458-
std::vector<wasm_val_t> results_vec(context.return_count);
459-
wasm_val_vec_t f_results = { results_vec.size(), results_vec.data() };
461+
wasm_val_vec_t f_results;
462+
DEFER(wasm_val_vec_delete(&f_results));
463+
wasm_val_vec_new_uninitialized(&f_results, context.return_count);
460464

461465
// Call function
462466
FAIL_IF(wasm_func_call(func, &f_args, &f_results), "Failed calling function " + name, NULL_VARIANT);
463467

464468
// Extract result(s)
465469
if (context.return_count == 0) return NULL_VARIANT;
466-
if (context.return_count == 1) return decode_variant(results_vec[0]);
470+
if (context.return_count == 1) return decode_variant(f_results.data[0]);
467471
Array results = Array();
468-
for (uint16_t i = 0; i < context.return_count; i++) results.append(decode_variant(results_vec[i]));
472+
for (uint16_t i = 0; i < context.return_count; i++) results.append(decode_variant(f_results.data[i]));
469473
return results;
470474
}
471475

0 commit comments

Comments
 (0)