Skip to content

Commit

Permalink
Add example of using externref in assembly code (#15913)
Browse files Browse the repository at this point in the history
See #15878
  • Loading branch information
sbc100 authored Jan 7, 2022
1 parent 7485f6e commit 8988358
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/core/test_externref.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

void log_externref();
void get_externref();

int main() {
printf("in main\n");
log_externref();
get_externref();
printf("extenref stored\n");
log_externref();
printf("extenref logged\n");
return 0;
}

10 changes: 10 additions & 0 deletions tests/core/test_externref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mergeInto(LibraryManager.library, {
get_externref_js: function () {
var test_object = { foo: 1 };
return test_object;
},

log_externref_js: function (ref) {
console.log('log_externref_js: ' + JSON.stringify(ref));
},
});
5 changes: 5 additions & 0 deletions tests/core/test_externref.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
in main
log_externref_js: null
extenref stored
log_externref_js: {"foo":1}
extenref logged
28 changes: 28 additions & 0 deletions tests/core/test_externref.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Define a global of type `externref`

.globaltype my_global, externref
my_global:

# Define a function that will call out to JS, passing the
# externref to the JS function

.functype log_externref_js (externref) -> ()

.globl log_externref
log_externref:
.functype log_externref () -> ()
global.get my_global
call log_externref_js
end_function

# Define a function that will call out to JS to grab an
# externref

.functype get_externref_js () -> (externref)

.globl get_externref
get_externref:
.functype get_externref () -> ()
call get_externref_js
global.set my_global
end_function
8 changes: 8 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8835,6 +8835,14 @@ def test_em_async_js(self):
self.maybe_closure()
self.do_core_test('test_em_async_js.c')

@require_v8
@no_wasm2js('wasm2js does not support reference types')
def test_externref(self):
self.run_process([EMCC, '-c', test_file('core/test_externref.s'), '-o', 'asm.o'])
self.emcc_args += ['--js-library', test_file('core/test_externref.js')]
self.emcc_args += ['-mreference-types']
self.do_core_test('test_externref.c', libraries=['asm.o'])


# Generate tests for everything
def make_run(name, emcc_args, settings=None, env=None, node_args=None):
Expand Down

0 comments on commit 8988358

Please sign in to comment.