Skip to content

Commit

Permalink
Call asc app exported new/pin/unpin APIs to align with its latest com…
Browse files Browse the repository at this point in the history
…piler (#577)

AssemblyScript's latest compiler 0.18 exports  __new__/__pin__/unpin APIs to allocate/retain/free memory instead of __new/__retain/__release APIs in older version, we lookup functions of both version to make it compatible for both version.
  • Loading branch information
JavanZhu committed Mar 18, 2021
1 parent fda3a26 commit ca67af9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
12 changes: 6 additions & 6 deletions assembly-script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:request_handler": "asc samples/request_handler.ts -b build/request_handler.wasm -t build/request_handler.wat --sourceMap --optimize --use abort=",
"build:request_sender": "asc samples/request_sender.ts -b build/request_sender.wasm -t build/request_sender.wat --sourceMap --optimize --use abort=",
"build:timer": "asc samples/timer.ts -b build/timer.wasm -t build/timer.wat --sourceMap --optimize --use abort=",
"build:publisher": "asc samples/event_publisher.ts -b build/event_publisher.wasm -t build/event_publisher.wat --sourceMap --optimize --use abort=",
"build:subscriber": "asc samples/event_subscriber.ts -b build/event_subscriber.wasm -t build/event_subscriber.wat --sourceMap --optimize --use abort=",
"build:request_handler": "asc samples/request_handler.ts -b build/request_handler.wasm -t build/request_handler.wat --sourceMap --optimize --exportRuntime --use abort=",
"build:request_sender": "asc samples/request_sender.ts -b build/request_sender.wasm -t build/request_sender.wat --sourceMap --optimize --exportRuntime --use abort=",
"build:timer": "asc samples/timer.ts -b build/timer.wasm -t build/timer.wat --sourceMap --optimize --exportRuntime --use abort=",
"build:publisher": "asc samples/event_publisher.ts -b build/event_publisher.wasm -t build/event_publisher.wat --sourceMap --optimize --exportRuntime --use abort=",
"build:subscriber": "asc samples/event_subscriber.ts -b build/event_subscriber.wasm -t build/event_subscriber.wat --sourceMap --optimize --exportRuntime --use abort=",
"build:all": "npm run build:request_handler; npm run build:request_sender; npm run build:timer; npm run build:subscriber; npm run build:publisher"
},
"author": "",
"license": "ISC",
"devDependencies": {
"assemblyscript": "^0.17.4"
"assemblyscript": "^0.18.15"
}
}
6 changes: 4 additions & 2 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,8 @@ load_from_sections(AOTModule *module, AOTSection *sections,
export_tmp = module->exports;
for (j = 0; j < module->export_count; j++, export_tmp++) {
if ((export_tmp->kind == EXPORT_KIND_FUNC)
&& (!strcmp(export_tmp->name, "__retain"))) {
&& (!strcmp(export_tmp->name, "__retain")
|| !strcmp(export_tmp->name, "__pin"))) {
func_index = export_tmp->index
- module->import_func_count;
func_type_index =
Expand Down Expand Up @@ -1988,7 +1989,8 @@ load_from_sections(AOTModule *module, AOTSection *sections,
}
}
else if ((!strcmp(exports[i].name, "free"))
|| (!strcmp(exports[i].name, "__release"))) {
|| (!strcmp(exports[i].name, "__release"))
|| (!strcmp(exports[i].name, "__unpin"))) {
func_index = exports[i].index - module->import_func_count;
func_type_index = module->func_type_indexes[func_index];
func_type = module->func_types[func_type_index];
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,8 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
malloc_func_sig = "(ii)i";
retain_func =
aot_lookup_function(module_inst, "__retain", "(i)i");
if (!retain_func)
retain_func = aot_lookup_function(module_inst, "__pin", "(i)i");
bh_assert(retain_func);
}
else {
Expand Down Expand Up @@ -1573,6 +1575,8 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
}
free_func =
aot_lookup_function(module_inst, free_func_name, "(i)i");
if (!free_func && module->retain_func_index != (uint32)-1)
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");

bh_assert(free_func);
execute_free_function(module_inst, free_func, ptr);
Expand Down
8 changes: 5 additions & 3 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2768,7 +2768,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
}
else if (!strcmp(export->name, "__new")
&& export->index >= module->import_function_count) {
/* __new && __retain for AssemblyScript */
/* __new && __pin for AssemblyScript */
func_index = export->index - module->import_function_count;
func_type = module->functions[func_index]->func_type;
if (func_type->param_count == 2
Expand All @@ -2789,7 +2789,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
export_tmp = module->exports;
for (j = 0; j < module->export_count; j++, export_tmp++) {
if ((export_tmp->kind == EXPORT_KIND_FUNC)
&& (!strcmp(export_tmp->name, "__retain"))
&& (!strcmp(export_tmp->name, "__retain")
|| (!strcmp(export_tmp->name, "__pin")))
&& (export_tmp->index
>= module->import_function_count)) {
func_index = export_tmp->index
Expand Down Expand Up @@ -2818,7 +2819,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
}
}
else if (((!strcmp(export->name, "free"))
|| (!strcmp(export->name, "__release")))
|| (!strcmp(export->name, "__release"))
|| (!strcmp(export->name, "__unpin")))
&& export->index >= module->import_function_count) {
func_index = export->index - module->import_function_count;
func_type = module->functions[func_index]->func_type;
Expand Down
8 changes: 5 additions & 3 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
}
else if (!strcmp(export->name, "__new")
&& export->index >= module->import_function_count) {
/* __new && __retain for AssemblyScript */
/* __new && __pin for AssemblyScript */
func_index = export->index - module->import_function_count;
func_type = module->functions[func_index]->func_type;
if (func_type->param_count == 2
Expand All @@ -1685,7 +1685,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
export_tmp = module->exports;
for (j = 0; j < module->export_count; j++, export_tmp++) {
if ((export_tmp->kind == EXPORT_KIND_FUNC)
&& (!strcmp(export_tmp->name, "__retain"))
&& (!strcmp(export_tmp->name, "__retain")
|| !strcmp(export_tmp->name, "__pin"))
&& (export_tmp->index
>= module->import_function_count)) {
func_index = export_tmp->index
Expand Down Expand Up @@ -1714,7 +1715,8 @@ load_from_sections(WASMModule *module, WASMSection *sections,
}
}
else if (((!strcmp(export->name, "free"))
|| (!strcmp(export->name, "__release")))
|| (!strcmp(export->name, "__release"))
|| (!strcmp(export->name, "__unpin")))
&& export->index >= module->import_function_count) {
func_index = export->index - module->import_function_count;
func_type = module->functions[func_index]->func_type;
Expand Down

0 comments on commit ca67af9

Please sign in to comment.