[wasm] Bind native API

This commit is contained in:
Behdad Esfahbod 2023-02-23 09:18:25 -07:00
parent e79a7318c4
commit 9f4dc2e103
4 changed files with 39 additions and 26 deletions

View File

@ -48,6 +48,9 @@ HB_WASM_BEGIN_DECLS
#ifndef HB_WASM_API #ifndef HB_WASM_API
#define HB_WASM_API(x) x #define HB_WASM_API(x) x
#endif #endif
#ifndef HB_WASM_EXEC_ENV
#define HB_WASM_EXEC_ENV
#endif
#ifndef bool_t #ifndef bool_t
@ -61,13 +64,16 @@ typedef uint32_t buffer_t;
/* font */ /* font */
face_t face_t
HB_WASM_API (font_get_face) (font_t); HB_WASM_API (font_get_face) (HB_WASM_EXEC_ENV
font_t);
/* shape interface */ /* shape interface */
bool_t bool_t
HB_WASM_API (shape) (font_t, buffer_t); HB_WASM_API (shape) (HB_WASM_EXEC_ENV
font_t,
buffer_t);
HB_WASM_END_DECLS HB_WASM_END_DECLS

View File

@ -27,11 +27,14 @@
#include "hb.h" #include "hb.h"
#include <wasm_export.h>
#define HB_WASM_API(x) HB_INTERNAL x #define HB_WASM_API(x) HB_INTERNAL x
#define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm { #define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm {
#define HB_WASM_END_DECLS }} #define HB_WASM_END_DECLS }}
#define HB_WASM_EXEC_ENV wasm_exec_env_t exec_env,
#include "hb-wasm-api.h" #include "hb-wasm-api.h"
#undef HB_WASM_API #undef HB_WASM_API
@ -41,5 +44,29 @@
#include "hb-wasm-font.hh" #include "hb-wasm-font.hh"
static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) {
printf("%*s", len, the_string);
}
/* Define an array of NativeSymbol for the APIs to be exported.
* Note: the array must be static defined since runtime will keep it after registration
* For the function signature specifications, goto the link:
* https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
*/
#define NATIVE_SYMBOL(signature, name) {#name, (void *) hb::wasm::name, signature, NULL}
static NativeSymbol _hb_wasm_native_symbols[] =
{
NATIVE_SYMBOL ("(r)r", font_get_face),
{
"debugprint",
(void *) debugprint,
"($i)",
NULL
}
};
#undef NATIVE_SYMBOL
#endif /* HB_WASM_API_HH */ #endif /* HB_WASM_API_HH */

View File

@ -32,7 +32,8 @@ namespace wasm {
face_t face_t
font_get_face (font_t) font_get_face (HB_WASM_EXEC_ENV
font_t)
{ {
return 0; return 0;
} }

View File

@ -32,8 +32,6 @@
#include "hb-wasm-api.hh" #include "hb-wasm-api.hh"
#include <wasm_export.h>
/* /*
* shaper face data * shaper face data
@ -46,10 +44,6 @@ struct hb_wasm_face_data_t {
wasm_module_t wasm_module; wasm_module_t wasm_module;
}; };
static void debugprint(wasm_exec_env_t exec_env, char *the_string, uint8_t len) {
printf("%*s", len, the_string);
}
static bool static bool
init_wasm () init_wasm ()
{ {
@ -60,21 +54,6 @@ init_wasm ()
RuntimeInitArgs init_args; RuntimeInitArgs init_args;
memset (&init_args, 0, sizeof (RuntimeInitArgs)); memset (&init_args, 0, sizeof (RuntimeInitArgs));
// Define an array of NativeSymbol for the APIs to be exported.
// Note: the array must be static defined since runtime
// will keep it after registration
// For the function signature specifications, goto the link:
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
static NativeSymbol native_symbols[] = {
{
"debugprint",
(void *)debugprint,
"($i)",
NULL
}
};
init_args.mem_alloc_type = Alloc_With_Allocator; init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc; init_args.mem_alloc_option.allocator.malloc_func = (void *) hb_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc; init_args.mem_alloc_option.allocator.realloc_func = (void *) hb_realloc;
@ -83,9 +62,9 @@ init_wasm ()
init_args.mem_alloc_type = Alloc_With_System_Allocator; init_args.mem_alloc_type = Alloc_With_System_Allocator;
// Native symbols need below registration phase // Native symbols need below registration phase
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol); init_args.n_native_symbols = ARRAY_LENGTH (_hb_wasm_native_symbols);
init_args.native_module_name = "env"; init_args.native_module_name = "env";
init_args.native_symbols = native_symbols; init_args.native_symbols = _hb_wasm_native_symbols;
if (!wasm_runtime_full_init (&init_args)) if (!wasm_runtime_full_init (&init_args))
{ {