diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5cd75a2..5d5c5a1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,6 +23,5 @@ jobs: run: make wasm - name: Run C++ tests run: make cc - # TODO: C memory management seems to have broken entirely for yet unknown reasons - #- name: Run C tests - # run: make c + - name: Run C tests + run: make c diff --git a/example/hostref.c b/example/hostref.c index ae23584..bb0b831 100644 --- a/example/hostref.c +++ b/example/hostref.c @@ -123,6 +123,8 @@ void check(own wasm_ref_t* actual, const wasm_ref_t* expected) { int main(int argc, const char* argv[]) { + int rc = 0; + // Initialize. printf("Initializing...\n"); wasm_engine_t* engine = wasm_engine_new(); @@ -130,32 +132,33 @@ int main(int argc, const char* argv[]) { // Load binary. printf("Loading binary...\n"); + wasm_byte_vec_t binary = WASM_EMPTY_VEC; FILE* file = fopen("hostref.wasm", "rb"); if (!file) { printf("> Error loading module!\n"); - return 1; + rc = 1; goto cleanup; } fseek(file, 0L, SEEK_END); size_t file_size = ftell(file); fseek(file, 0L, SEEK_SET); - wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { printf("> Error loading module!\n"); - return 1; + fclose(file); + rc = 1; goto cleanup; } fclose(file); // Compile. printf("Compiling module...\n"); own wasm_module_t* module = wasm_module_new(store, &binary); + wasm_byte_vec_delete(&binary); + binary = (wasm_byte_vec_t)WASM_EMPTY_VEC; if (!module) { printf("> Error compiling module!\n"); - return 1; + rc = 1; goto cleanup; } - wasm_byte_vec_delete(&binary); - // Create external callback function. printf("Creating callback...\n"); own wasm_functype_t* callback_type = wasm_functype_new_1_1( @@ -173,7 +176,7 @@ int main(int argc, const char* argv[]) { wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); - return 1; + rc = 1; goto cleanup; } wasm_func_delete(callback_func); @@ -259,12 +262,14 @@ int main(int argc, const char* argv[]) { wasm_extern_vec_delete(&exports); +cleanup: // Shut down. printf("Shutting down...\n"); + wasm_byte_vec_delete(&binary); wasm_store_delete(store); wasm_engine_delete(engine); // All done. - printf("Done.\n"); - return 0; + if (!rc) printf("Done.\n"); + return rc; } diff --git a/example/hostref.wasm b/example/hostref.wasm index 7bfc728..b1786ed 100644 Binary files a/example/hostref.wasm and b/example/hostref.wasm differ diff --git a/example/hostref.wat b/example/hostref.wat index d288d62..9ed43db 100644 --- a/example/hostref.wat +++ b/example/hostref.wat @@ -1,7 +1,7 @@ (module (import "" "f" (func $fun (param externref) (result externref))) - (global $glob (export "global") (mut externref) (ref.null)) + (global $glob (export "global") (mut externref) (ref.null extern)) (table $tab (export "table") 10 externref) (func (export "global.set") (param $r externref) diff --git a/src/wasm-c.cc b/src/wasm-c.cc index fdf2448..313b7d0 100644 --- a/src/wasm-c.cc +++ b/src/wasm-c.cc @@ -29,7 +29,7 @@ struct borrowed_vec { struct wasm_##name##_t : Name {}; \ \ void wasm_##name##_delete(wasm_##name##_t* x) { \ - delete x; \ + if (x) destroyer{}(static_cast(x)); \ } \ \ extern "C++" inline auto hide_##name(Name* x) -> wasm_##name##_t* { \ diff --git a/src/wasm-v8-lowlevel.cc b/src/wasm-v8-lowlevel.cc index 07e8b96..0af7f53 100644 --- a/src/wasm-v8-lowlevel.cc +++ b/src/wasm-v8-lowlevel.cc @@ -30,6 +30,7 @@ namespace wasm { void flags_init() { v8::internal::v8_flags.expose_gc = true; + v8::internal::v8_flags.experimental_wasm_typed_funcref = true; }