-
Notifications
You must be signed in to change notification settings - Fork 0
Emscripten .c File Coding Guide
emcc source.c -o output.wasm
Use __attribute__((import_module("env"), import_name("function"))).
e.g.
__attribute__((import_module("env"), import_name("int")))
extern __externref_t toInt(int ptr);
__attribute__((import_module("env"), import_name("malloc")))
extern void* jmalloc(int v);
From WASM's perspective, all pointers are 32-bit integer, so you can replace void* with int and vice sersa.
Note: __externref_t is a special type that represents a Java Object, it cannot be casted to any normal c type.
Holding a __externref_t will prevent the corresponding Java Object from GCed.
__externref_t can only be hold by local values, and has no pointer or array type (this is a restriction of Emscripten).
The most important host function is 'OC_invoke'.
It takes a __externref_t of Java List and returns a __externref_t of Java List.
You need to convert arguments to Java Objects first, then use 'packN' to convert those Java Objects to a Java List.
the returned Java List can be read by 'get' and 'size', you might want to convert them back to c types before use.
Note: when you convert cstring to Java String, you are requested to pass a malloc function pointer.
You are free to pass the Emscripten implementation, but it's better to use the host funcion malloc, it's implemented in Java.
If you use the host funcion malloc, remember use host funcion free(also Java implemented) to release it rather than Emscripten malloc!