2 * Copyright © 2023 Behdad Esfahbod
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 #ifndef HB_WASM_API_HH
26 #define HB_WASM_API_HH
30 #include <wasm_export.h>
32 #define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm {
33 #define HB_WASM_END_DECLS }}
35 #define HB_WASM_API(ret_t, name) HB_INTERNAL ret_t name
36 #define HB_WASM_API_COMPOUND(ret_t, name) HB_INTERNAL void name
38 #define HB_WASM_EXEC_ENV wasm_exec_env_t exec_env,
39 #define HB_WASM_EXEC_ENV_COMPOUND wasm_exec_env_t exec_env, ptr_t() retptr,
41 #define ptr_t(type_t) uint32_t
42 #define ptr_d(type_t, name) uint32_t name##ptr
44 #include "hb-wasm-api.h"
46 #undef HB_WASM_BEGIN_DECLS
47 #undef HB_WASM_END_DECLS
51 hb_wasm_ref_type_none,
52 hb_wasm_ref_type_face,
53 hb_wasm_ref_type_font,
54 hb_wasm_ref_type_buffer,
57 HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key;
61 #define HB_REF2OBJ(obj) \
62 hb_##obj##_t *obj = nullptr; \
64 (void) wasm_externref_ref2obj (obj##ptr, (void **) &obj); \
65 /* Check object type. */ \
66 /* This works because all our objects have the same hb_object_t layout. */ \
67 if (unlikely (hb_##obj##_get_user_data (obj, &_hb_wasm_ref_type_key) != \
68 (void *) hb_wasm_ref_type_##obj)) \
69 obj = hb_##obj##_get_empty (); \
72 #define HB_OBJ2REF(obj) \
73 uint32_t obj##ref = nullref; \
75 hb_##obj##_set_user_data (obj, &_hb_wasm_ref_type_key, \
76 (void *) hb_wasm_ref_type_##obj, \
78 (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref); \
81 #define HB_RETURN_STRUCT(type, name) \
82 type *_name_ptr = nullptr; \
84 if (likely (wasm_runtime_validate_app_addr (module_inst, \
85 retptr, sizeof (type)))) \
87 _name_ptr = (type *) wasm_runtime_addr_app_to_native (module_inst, retptr); \
88 if (unlikely (!_name_ptr)) \
92 type &name = *_name_ptr
94 #define HB_PTR_PARAM(type, name) \
95 type *name = nullptr; \
97 if (likely (wasm_runtime_validate_app_addr (module_inst, \
98 name##ptr, sizeof (type)))) \
99 name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \
102 #define HB_ARRAY_PARAM(type, name, length) \
103 type *name = nullptr; \
105 if (likely (!hb_unsigned_mul_overflows (length, sizeof (type)) && \
106 wasm_runtime_validate_app_addr (module_inst, \
107 name##ptr, length * sizeof (type)))) \
108 name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \
111 #define HB_ARRAY_APP2NATIVE(type, name, length) \
112 ((type *) (!hb_unsigned_mul_overflows (length, sizeof (type)) && \
113 validate_app_addr (name, (length) * sizeof (type)) ? \
114 addr_app_to_native (name) : nullptr))
117 #endif /* HB_WASM_API_HH */