1 /* 2 * Copyright © 2023 Behdad Esfahbod 3 * 4 * This is part of HarfBuzz, a text shaping library. 5 * 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. 11 * 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 16 * DAMAGE. 17 * 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. 23 */ 24 25 #ifndef HB_WASM_API_HH 26 #define HB_WASM_API_HH 27 28 #include "hb.hh" 29 30 #include <wasm_export.h> 31 32 #define HB_WASM_BEGIN_DECLS namespace hb { namespace wasm { 33 #define HB_WASM_END_DECLS }} 34 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 37 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, 40 41 #define ptr_t(type_t) uint32_t 42 #define ptr_d(type_t, name) uint32_t name##ptr 43 44 #include "hb-wasm-api.h" 45 46 #undef HB_WASM_BEGIN_DECLS 47 #undef HB_WASM_END_DECLS 48 49 50 enum { 51 hb_wasm_ref_type_none, 52 hb_wasm_ref_type_face, 53 hb_wasm_ref_type_font, 54 hb_wasm_ref_type_buffer, 55 }; 56 57 HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; 58 59 #define nullref 0 60 61 #define HB_REF2OBJ(obj) \ 62 hb_##obj##_t *obj = nullptr; \ 63 HB_STMT_START { \ 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 (); \ 70 } HB_STMT_END 71 72 #define HB_OBJ2REF(obj) \ 73 uint32_t obj##ref = nullref; \ 74 HB_STMT_START { \ 75 hb_##obj##_set_user_data (obj, &_hb_wasm_ref_type_key, \ 76 (void *) hb_wasm_ref_type_##obj, \ 77 nullptr, false); \ 78 (void) wasm_externref_obj2ref (module_inst, obj, &obj##ref); \ 79 } HB_STMT_END 80 81 #define HB_RETURN_STRUCT(type, name) \ 82 type *_name_ptr = nullptr; \ 83 { \ 84 if (likely (wasm_runtime_validate_app_addr (module_inst, \ 85 retptr, sizeof (type)))) \ 86 { \ 87 _name_ptr = (type *) wasm_runtime_addr_app_to_native (module_inst, retptr); \ 88 if (unlikely (!_name_ptr)) \ 89 return; \ 90 } \ 91 } \ 92 type &name = *_name_ptr 93 94 #define HB_PTR_PARAM(type, name) \ 95 type *name = nullptr; \ 96 HB_STMT_START { \ 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); \ 100 } HB_STMT_END 101 102 #define HB_ARRAY_PARAM(type, name, length) \ 103 type *name = nullptr; \ 104 HB_STMT_START { \ 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); \ 109 } HB_STMT_END 110 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)) 115 116 117 #endif /* HB_WASM_API_HH */ 118