github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/worker/wavm-c.h (about) 1 // WebAssembly C API 2 3 #ifndef __WASM_H 4 #define __WASM_H 5 6 #include <assert.h> 7 #include <stdbool.h> 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #ifndef WASM_C_API 12 #ifdef _WIN32 13 #define WASM_C_API __declspec(dllimport) 14 #else 15 #define WASM_C_API 16 #endif 17 #endif 18 19 // Opaque types 20 21 #ifndef WASM_OPAQUE_TYPES_DEFINED 22 typedef struct wasm_config_t wasm_config_t; 23 typedef struct wasm_engine_t wasm_engine_t; 24 typedef struct wasm_compartment_t wasm_compartment_t; 25 typedef struct wasm_store_t wasm_store_t; 26 typedef struct wasm_valtype_t wasm_valtype_t; 27 typedef struct wasm_functype_t wasm_functype_t; 28 typedef struct wasm_tabletype_t wasm_tabletype_t; 29 typedef struct wasm_memorytype_t wasm_memorytype_t; 30 typedef struct wasm_globaltype_t wasm_globaltype_t; 31 typedef struct wasm_externtype_t wasm_externtype_t; 32 typedef struct wasm_ref_t wasm_ref_t; 33 typedef struct wasm_trap_t wasm_trap_t; 34 typedef struct wasm_foreign_t wasm_foreign_t; 35 typedef struct wasm_module_t wasm_module_t; 36 typedef struct wasm_func_t wasm_func_t; 37 typedef struct wasm_table_t wasm_table_t; 38 typedef struct wasm_memory_t wasm_memory_t; 39 typedef struct wasm_global_t wasm_global_t; 40 typedef struct wasm_extern_t wasm_extern_t; 41 typedef struct wasm_instance_t wasm_instance_t; 42 43 typedef struct wasm_shared_module_t wasm_shared_module_t; 44 typedef struct wasm_shared_func_t wasm_shared_func_t; 45 typedef struct wasm_shared_table_t wasm_shared_table_t; 46 typedef struct wasm_shared_memory_t wasm_shared_memory_t; 47 typedef struct wasm_shared_foreign_t wasm_shared_foreign_t; 48 #endif 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 /////////////////////////////////////////////////////////////////////////////// 55 // Auxiliaries 56 57 typedef float wasm_float32_t; 58 typedef double wasm_float64_t; 59 60 // Ownership 61 62 #define own 63 64 // The qualifier `own` is used to indicate ownership of data in this API. 65 // It is intended to be interpreted similar to a `const` qualifier: 66 // 67 // - `own wasm_xxx_t*` owns the pointed-to data 68 // - `own wasm_xxx_t` distributes to all fields of a struct or union `xxx` 69 // - `own wasm_xxx_vec_t` owns the vector as well as its elements(!) 70 // - an `own` function parameter passes ownership from caller to callee 71 // - an `own` function result passes ownership from callee to caller 72 // - an exception are `own` pointer parameters named `out`, which are copy-back 73 // output parameters passing back ownership from callee to caller 74 // 75 // Own data is created by `wasm_xxx_new` functions and some others. 76 // It must be released with the corresponding `wasm_xxx_delete` function. 77 // 78 // Deleting a reference does not necessarily delete the underlying object, 79 // it merely indicates that this owner no longer uses it. 80 // 81 // For vectors, `const wasm_xxx_vec_t` is used informally to indicate that 82 // neither the vector nor its elements should be modified. 83 // TODO: introduce proper `wasm_xxx_const_vec_t`? 84 85 #define WASM_DECLARE_OWN(name) WASM_C_API void wasm_##name##_delete(own wasm_##name##_t*); 86 87 /////////////////////////////////////////////////////////////////////////////// 88 // Runtime Environment 89 90 // Configuration 91 92 WASM_DECLARE_OWN(config) 93 94 WASM_C_API own wasm_config_t* wasm_config_new(); 95 96 #define WASM_DECLARE_FEATURE(feature) \ 97 WASM_C_API void wasm_config_feature_set_##feature(wasm_config_t* config, bool enable); 98 99 // Standardized, or mature proposed standard extensions that are expected to be standardized without 100 // breaking backward compatibility: enabled by default. 101 WASM_DECLARE_FEATURE(import_export_mutable_globals) 102 WASM_DECLARE_FEATURE(nontrapping_float_to_int) 103 WASM_DECLARE_FEATURE(sign_extension) 104 WASM_DECLARE_FEATURE(bulk_memory_ops) 105 106 // Proposed standard extensions: disabled by default. 107 WASM_DECLARE_FEATURE(simd) 108 WASM_DECLARE_FEATURE(atomics) 109 WASM_DECLARE_FEATURE(exception_handling) 110 WASM_DECLARE_FEATURE(multivalue) 111 WASM_DECLARE_FEATURE(reference_types) 112 WASM_DECLARE_FEATURE(extended_name_section) 113 WASM_DECLARE_FEATURE(multimemory) 114 115 // Non-standard extensions. 116 WASM_DECLARE_FEATURE(shared_tables) 117 WASM_DECLARE_FEATURE(allow_legacy_inst_names) 118 WASM_DECLARE_FEATURE(any_extern_kind_elems) 119 WASM_DECLARE_FEATURE(wat_quoted_names) 120 WASM_DECLARE_FEATURE(wat_custom_sections) 121 122 #undef WASM_DECLARE_FEATURE 123 124 // Engine 125 126 WASM_DECLARE_OWN(engine) 127 128 WASM_C_API own wasm_engine_t* wasm_engine_new(); 129 WASM_C_API own wasm_engine_t* wasm_engine_new_with_config(own wasm_config_t*); 130 131 // Compartment 132 133 WASM_DECLARE_OWN(compartment) 134 135 WASM_C_API own wasm_compartment_t* wasm_compartment_new(wasm_engine_t* engine, 136 const char* debug_name); 137 WASM_C_API own wasm_compartment_t* wasm_compartment_clone(const wasm_compartment_t*); 138 139 WASM_C_API bool wasm_compartment_contains(const wasm_compartment_t*, const wasm_ref_t*); 140 141 // Store 142 143 WASM_DECLARE_OWN(store) 144 145 WASM_C_API own wasm_store_t* wasm_store_new(wasm_compartment_t*, const char* debug_name); 146 147 /////////////////////////////////////////////////////////////////////////////// 148 // Type Representations 149 150 // Type attributes 151 152 typedef uint8_t wasm_mutability_t; 153 enum wasm_mutability_enum 154 { 155 WASM_CONST, 156 WASM_VAR, 157 }; 158 159 typedef uint8_t wasm_shared_t; 160 enum wasm_shared_enum 161 { 162 WASM_NOTSHARED, 163 WASM_SHARED, 164 }; 165 166 typedef uint8_t wasm_index_t; 167 enum wasm_index_enum 168 { 169 WASM_INDEX_I32, 170 WASM_INDEX_I64, 171 }; 172 173 typedef struct wasm_limits_t 174 { 175 uint32_t min; 176 uint32_t max; 177 } wasm_limits_t; 178 179 static const uint32_t wasm_limits_max_default = 0xffffffff; 180 181 // Generic 182 183 #define WASM_DECLARE_TYPE(name) \ 184 WASM_DECLARE_OWN(name) \ 185 \ 186 WASM_C_API own wasm_##name##_t* wasm_##name##_copy(wasm_##name##_t*); 187 188 // Value Types 189 190 WASM_DECLARE_TYPE(valtype) 191 192 typedef uint8_t wasm_valkind_t; 193 enum wasm_valkind_enum 194 { 195 WASM_I32, 196 WASM_I64, 197 WASM_F32, 198 WASM_F64, 199 WASM_V128, 200 WASM_ANYREF = 128, 201 WASM_FUNCREF, 202 }; 203 204 WASM_C_API own wasm_valtype_t* wasm_valtype_new(wasm_valkind_t); 205 206 WASM_C_API wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t*); 207 208 static inline bool wasm_valkind_is_num(wasm_valkind_t k) { return k < WASM_ANYREF; } 209 static inline bool wasm_valkind_is_ref(wasm_valkind_t k) { return k >= WASM_ANYREF; } 210 211 static inline bool wasm_valtype_is_num(const wasm_valtype_t* t) 212 { 213 return wasm_valkind_is_num(wasm_valtype_kind(t)); 214 } 215 static inline bool wasm_valtype_is_ref(const wasm_valtype_t* t) 216 { 217 return wasm_valkind_is_ref(wasm_valtype_kind(t)); 218 } 219 220 // Function Types 221 222 WASM_DECLARE_TYPE(functype) 223 224 WASM_C_API own wasm_functype_t* wasm_functype_new(own wasm_valtype_t** params, 225 size_t num_params, 226 own wasm_valtype_t** results, 227 size_t num_results); 228 229 WASM_C_API size_t wasm_functype_num_params(const wasm_functype_t* type); 230 WASM_C_API wasm_valtype_t* wasm_functype_param(const wasm_functype_t* type, size_t index); 231 232 WASM_C_API size_t wasm_functype_num_results(const wasm_functype_t* type); 233 WASM_C_API wasm_valtype_t* wasm_functype_result(const wasm_functype_t* type, size_t index); 234 235 // Global Types 236 237 WASM_DECLARE_TYPE(globaltype) 238 239 WASM_C_API own wasm_globaltype_t* wasm_globaltype_new(own wasm_valtype_t*, wasm_mutability_t); 240 241 WASM_C_API const wasm_valtype_t* wasm_globaltype_content(const wasm_globaltype_t*); 242 WASM_C_API wasm_mutability_t wasm_globaltype_mutability(const wasm_globaltype_t*); 243 244 // Table Types 245 246 WASM_DECLARE_TYPE(tabletype) 247 248 WASM_C_API own wasm_tabletype_t* wasm_tabletype_new(own wasm_valtype_t*, 249 const wasm_limits_t*, 250 wasm_shared_t, 251 wasm_index_t); 252 253 WASM_C_API const wasm_valtype_t* wasm_tabletype_element(const wasm_tabletype_t*); 254 WASM_C_API const wasm_limits_t* wasm_tabletype_limits(const wasm_tabletype_t*); 255 WASM_C_API wasm_shared_t wasm_tabletype_shared(const wasm_tabletype_t*); 256 WASM_C_API wasm_index_t wasm_tabletype_index(const wasm_tabletype_t*); 257 258 // Memory Types 259 260 WASM_DECLARE_TYPE(memorytype) 261 262 WASM_C_API own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*, 263 wasm_shared_t, 264 wasm_index_t); 265 266 WASM_C_API const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t*); 267 WASM_C_API wasm_shared_t wasm_memorytype_shared(const wasm_memorytype_t*); 268 WASM_C_API wasm_index_t wasm_memorytype_index(const wasm_memorytype_t*); 269 270 // Extern Types 271 272 WASM_DECLARE_TYPE(externtype) 273 274 typedef uint8_t wasm_externkind_t; 275 enum wasm_externkind_enum 276 { 277 WASM_EXTERN_FUNC, 278 WASM_EXTERN_GLOBAL, 279 WASM_EXTERN_TABLE, 280 WASM_EXTERN_MEMORY, 281 }; 282 283 WASM_C_API wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*); 284 285 WASM_C_API wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t*); 286 WASM_C_API wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t*); 287 WASM_C_API wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t*); 288 WASM_C_API wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t*); 289 290 WASM_C_API wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*); 291 WASM_C_API wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t*); 292 WASM_C_API wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t*); 293 WASM_C_API wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t*); 294 295 WASM_C_API const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t*); 296 WASM_C_API const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t*); 297 WASM_C_API const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t*); 298 WASM_C_API const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t*); 299 300 WASM_C_API const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t*); 301 WASM_C_API const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t*); 302 WASM_C_API const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t*); 303 WASM_C_API const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t*); 304 305 // Imports 306 307 typedef struct wasm_import_t 308 { 309 const char* module; 310 size_t num_module_bytes; 311 const char* name; 312 size_t num_name_bytes; 313 wasm_externtype_t* type; 314 } wasm_import_t; 315 316 // Exports 317 318 typedef struct wasm_export_t 319 { 320 const char* name; 321 size_t num_name_bytes; 322 wasm_externtype_t* type; 323 } wasm_export_t; 324 325 /////////////////////////////////////////////////////////////////////////////// 326 // Runtime Objects 327 328 // Values 329 330 // NOTE: not 128-bit aligned 331 typedef struct wasm_v128_t 332 { 333 uint64_t u64x2[2]; 334 } wasm_v128_t; 335 336 typedef union wasm_val_t 337 { 338 int32_t i32; 339 int64_t i64; 340 wasm_float32_t f32; 341 wasm_float64_t f64; 342 wasm_v128_t v128; 343 wasm_ref_t* ref; 344 } wasm_val_t; 345 346 WASM_C_API void wasm_val_delete(wasm_valkind_t kind, own wasm_val_t* v); 347 WASM_C_API void wasm_val_copy(wasm_valkind_t kind, own wasm_val_t* out, const wasm_val_t*); 348 349 // References 350 351 #define WASM_DECLARE_REF_BASE(name) \ 352 WASM_DECLARE_OWN(name) \ 353 \ 354 WASM_C_API own wasm_##name##_t* wasm_##name##_copy(const wasm_##name##_t*); \ 355 WASM_C_API bool wasm_##name##_same(const wasm_##name##_t*, const wasm_##name##_t*); \ 356 \ 357 WASM_C_API void* wasm_##name##_get_host_info(const wasm_##name##_t*); \ 358 WASM_C_API void wasm_##name##_set_host_info(wasm_##name##_t*, void*); \ 359 WASM_C_API void wasm_##name##_set_host_info_with_finalizer( \ 360 wasm_##name##_t*, void*, void (*)(void*)); \ 361 \ 362 WASM_C_API own wasm_##name##_t* wasm_##name##_remap_to_cloned_compartment( \ 363 const wasm_##name##_t*, const wasm_compartment_t*); \ 364 \ 365 WASM_C_API const char* wasm_##name##_name(const wasm_##name##_t*); 366 367 #define WASM_DECLARE_REF(name) \ 368 WASM_DECLARE_REF_BASE(name) \ 369 \ 370 WASM_C_API wasm_ref_t* wasm_##name##_as_ref(wasm_##name##_t*); \ 371 WASM_C_API wasm_##name##_t* wasm_ref_as_##name(wasm_ref_t*); \ 372 WASM_C_API const wasm_ref_t* wasm_##name##_as_ref_const(const wasm_##name##_t*); \ 373 WASM_C_API const wasm_##name##_t* wasm_ref_as_##name##_const(const wasm_ref_t*); 374 375 #define WASM_DECLARE_SHAREABLE_REF(name) \ 376 WASM_DECLARE_REF(name) \ 377 WASM_DECLARE_OWN(shared_##name) \ 378 \ 379 WASM_C_API own wasm_shared_##name##_t* wasm_##name##_share(const wasm_##name##_t*); \ 380 WASM_C_API own wasm_##name##_t* wasm_##name##_obtain(wasm_store_t*, \ 381 const wasm_shared_##name##_t*); 382 383 WASM_DECLARE_REF_BASE(ref) 384 385 // Frames 386 387 typedef struct wasm_frame_t 388 { 389 wasm_func_t* function; 390 size_t instr_index; 391 } wasm_frame_t; 392 393 // Traps 394 395 WASM_DECLARE_REF(trap) 396 397 WASM_C_API own wasm_trap_t* wasm_trap_new(wasm_compartment_t*, 398 const char* message, 399 size_t num_message_bytes); 400 401 WASM_C_API bool wasm_trap_message(const wasm_trap_t*, 402 char* out_message, 403 size_t* inout_num_message_bytes); 404 WASM_C_API size_t wasm_trap_stack_num_frames(const wasm_trap_t*); 405 WASM_C_API void wasm_trap_stack_frame(const wasm_trap_t*, 406 size_t index, 407 own wasm_frame_t* out_frame); 408 409 // Foreign Objects 410 411 WASM_DECLARE_SHAREABLE_REF(foreign) 412 413 WASM_C_API own wasm_foreign_t* wasm_foreign_new(wasm_compartment_t*, const char* debug_name); 414 415 // Modules 416 417 WASM_DECLARE_TYPE(module) 418 419 WASM_C_API own wasm_module_t* wasm_module_new(wasm_engine_t*, 420 const char* binary, 421 size_t num_binary_bytes); 422 423 WASM_C_API own wasm_module_t* wasm_module_new_text(wasm_engine_t*, 424 const char* text, 425 size_t num_text_chars); 426 427 WASM_C_API own char* wasm_module_print(const wasm_module_t* module, size_t* out_num_chars); 428 429 WASM_C_API bool wasm_module_validate(const char* binary, size_t num_binary_bytes); 430 431 WASM_C_API size_t wasm_module_num_imports(const wasm_module_t* module); 432 WASM_C_API void wasm_module_import(const wasm_module_t* module, 433 size_t index, 434 own wasm_import_t* out_import); 435 WASM_C_API size_t wasm_module_num_exports(const wasm_module_t* module); 436 WASM_C_API void wasm_module_export(const wasm_module_t* module, 437 size_t index, 438 own wasm_export_t* out_export); 439 440 WASM_C_API char* wasm_module_compile(wasm_engine_t* engine, 441 const char* wasmBytes, 442 uintptr_t numWASMBytes, 443 uintptr_t* outBytes); 444 445 WASM_C_API char* wasm_module_compile_object(wasm_engine_t* engine, 446 const char* wasmBytes, 447 uintptr_t numWASMBytes, 448 uintptr_t* outBytes); 449 450 // Function Instances 451 452 WASM_DECLARE_SHAREABLE_REF(func) 453 454 typedef own wasm_trap_t* (*wasm_func_callback_t)(const wasm_val_t args[], wasm_val_t results[]); 455 typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(void* env, 456 const wasm_val_t args[], 457 wasm_val_t results[]); 458 459 WASM_C_API own wasm_func_t* wasm_func_new(wasm_compartment_t*, 460 const wasm_functype_t*, 461 wasm_func_callback_t, 462 const char* debug_name); 463 WASM_C_API own wasm_func_t* wasm_func_new_with_env(wasm_compartment_t*, 464 const wasm_functype_t* type, 465 wasm_func_callback_with_env_t, 466 void* env, 467 void (*finalizer)(void*), 468 const char* debug_name); 469 470 WASM_C_API own wasm_functype_t* wasm_func_type(const wasm_func_t*); 471 WASM_C_API size_t wasm_func_param_arity(const wasm_func_t*); 472 WASM_C_API size_t wasm_func_result_arity(const wasm_func_t*); 473 474 WASM_C_API own wasm_trap_t* wasm_func_call(wasm_store_t*, 475 const wasm_func_t*, 476 const wasm_val_t args[], 477 wasm_val_t results[]); 478 479 // Global Instances 480 481 WASM_DECLARE_REF(global) 482 483 WASM_C_API own wasm_global_t* wasm_global_new(wasm_compartment_t*, 484 const wasm_globaltype_t*, 485 const wasm_val_t*, 486 const char* debug_name); 487 488 WASM_C_API own wasm_globaltype_t* wasm_global_type(const wasm_global_t*); 489 490 WASM_C_API void wasm_global_get(wasm_store_t*, const wasm_global_t*, own wasm_val_t* out); 491 WASM_C_API void wasm_global_set(wasm_global_t*, const wasm_val_t*); 492 493 // Table Instances 494 495 WASM_DECLARE_SHAREABLE_REF(table) 496 497 typedef uint32_t wasm_table_size_t; 498 499 static const wasm_table_size_t WASM_TABLE_SIZE_MAX = UINT32_MAX; 500 501 WASM_C_API own wasm_table_t* wasm_table_new(wasm_compartment_t*, 502 const wasm_tabletype_t*, 503 wasm_ref_t* init, 504 const char* debug_name); 505 506 WASM_C_API own wasm_tabletype_t* wasm_table_type(const wasm_table_t*); 507 508 WASM_C_API own wasm_ref_t* wasm_table_get(const wasm_table_t* table, wasm_table_size_t index); 509 WASM_C_API bool wasm_table_set(wasm_table_t* table, wasm_table_size_t index, wasm_ref_t* value); 510 511 WASM_C_API wasm_table_size_t wasm_table_size(const wasm_table_t* table); 512 WASM_C_API bool wasm_table_grow(wasm_table_t* table, 513 wasm_table_size_t delta, 514 wasm_ref_t* init, 515 wasm_table_size_t* out_previous_size); 516 517 // Memory Instances 518 519 WASM_DECLARE_SHAREABLE_REF(memory) 520 521 typedef uint32_t wasm_memory_pages_t; 522 523 static const wasm_memory_pages_t WASM_MEMORY_PAGES_MAX = UINT32_MAX; 524 525 static const size_t MEMORY_PAGE_SIZE = 0x10000; 526 527 WASM_C_API own wasm_memory_t* wasm_memory_new(wasm_compartment_t*, 528 const wasm_memorytype_t*, 529 const char* debug_name); 530 531 WASM_C_API own wasm_memorytype_t* wasm_memory_type(const wasm_memory_t*); 532 533 WASM_C_API char* wasm_memory_data(wasm_memory_t*); 534 WASM_C_API size_t wasm_memory_data_size(const wasm_memory_t*); 535 536 WASM_C_API wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*); 537 WASM_C_API bool wasm_memory_grow(wasm_memory_t*, 538 wasm_memory_pages_t delta, 539 wasm_memory_pages_t* out_previous_size); 540 541 // Externals 542 543 WASM_DECLARE_REF(extern) 544 545 WASM_C_API wasm_externkind_t wasm_extern_kind(const wasm_extern_t*); 546 WASM_C_API own wasm_externtype_t* wasm_extern_type(const wasm_extern_t*); 547 548 WASM_C_API wasm_extern_t* wasm_func_as_extern(wasm_func_t*); 549 WASM_C_API wasm_extern_t* wasm_global_as_extern(wasm_global_t*); 550 WASM_C_API wasm_extern_t* wasm_table_as_extern(wasm_table_t*); 551 WASM_C_API wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*); 552 553 WASM_C_API wasm_func_t* wasm_extern_as_func(wasm_extern_t*); 554 WASM_C_API wasm_global_t* wasm_extern_as_global(wasm_extern_t*); 555 WASM_C_API wasm_table_t* wasm_extern_as_table(wasm_extern_t*); 556 WASM_C_API wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*); 557 558 WASM_C_API const wasm_extern_t* wasm_func_as_extern_const(const wasm_func_t*); 559 WASM_C_API const wasm_extern_t* wasm_global_as_extern_const(const wasm_global_t*); 560 WASM_C_API const wasm_extern_t* wasm_table_as_extern_const(const wasm_table_t*); 561 WASM_C_API const wasm_extern_t* wasm_memory_as_extern_const(const wasm_memory_t*); 562 563 WASM_C_API const wasm_func_t* wasm_extern_as_func_const(const wasm_extern_t*); 564 WASM_C_API const wasm_global_t* wasm_extern_as_global_const(const wasm_extern_t*); 565 WASM_C_API const wasm_table_t* wasm_extern_as_table_const(const wasm_extern_t*); 566 WASM_C_API const wasm_memory_t* wasm_extern_as_memory_const(const wasm_extern_t*); 567 568 // Module Instances 569 570 WASM_DECLARE_REF(instance) 571 572 WASM_C_API own wasm_instance_t* wasm_instance_new(wasm_store_t*, 573 const wasm_module_t*, 574 const wasm_extern_t* const imports[], 575 own wasm_trap_t**, 576 const char* debug_name); 577 578 WASM_C_API size_t wasm_instance_num_exports(const wasm_instance_t*); 579 WASM_C_API wasm_extern_t* wasm_instance_export(const wasm_instance_t*, size_t index); 580 581 /////////////////////////////////////////////////////////////////////////////// 582 // Convenience 583 584 // Value Type construction short-hands 585 586 static inline own wasm_valtype_t* wasm_valtype_new_i32() { return wasm_valtype_new(WASM_I32); } 587 static inline own wasm_valtype_t* wasm_valtype_new_i64() { return wasm_valtype_new(WASM_I64); } 588 static inline own wasm_valtype_t* wasm_valtype_new_f32() { return wasm_valtype_new(WASM_F32); } 589 static inline own wasm_valtype_t* wasm_valtype_new_f64() { return wasm_valtype_new(WASM_F64); } 590 static inline own wasm_valtype_t* wasm_valtype_new_v128() { return wasm_valtype_new(WASM_V128); } 591 592 static inline own wasm_valtype_t* wasm_valtype_new_externref() 593 { 594 return wasm_valtype_new(WASM_ANYREF); 595 } 596 static inline own wasm_valtype_t* wasm_valtype_new_funcref() 597 { 598 return wasm_valtype_new(WASM_FUNCREF); 599 } 600 601 // Function Types construction short-hands 602 603 static inline own wasm_functype_t* wasm_functype_new_0_0() 604 { 605 return wasm_functype_new((wasm_valtype_t**)0, 0, (wasm_valtype_t**)0, 0); 606 } 607 608 static inline own wasm_functype_t* wasm_functype_new_1_0(own wasm_valtype_t* p) 609 { 610 wasm_valtype_t* ps[1] = {p}; 611 return wasm_functype_new(ps, 1, (wasm_valtype_t**)0, 0); 612 } 613 614 static inline own wasm_functype_t* wasm_functype_new_2_0(own wasm_valtype_t* p1, 615 own wasm_valtype_t* p2) 616 { 617 wasm_valtype_t* ps[2] = {p1, p2}; 618 return wasm_functype_new(ps, 2, (wasm_valtype_t**)0, 0); 619 } 620 621 static inline own wasm_functype_t* wasm_functype_new_3_0(own wasm_valtype_t* p1, 622 own wasm_valtype_t* p2, 623 own wasm_valtype_t* p3) 624 { 625 wasm_valtype_t* ps[3] = {p1, p2, p3}; 626 return wasm_functype_new(ps, 3, (wasm_valtype_t**)0, 0); 627 } 628 629 static inline own wasm_functype_t* wasm_functype_new_0_1(own wasm_valtype_t* r) 630 { 631 wasm_valtype_t* rs[1] = {r}; 632 return wasm_functype_new((wasm_valtype_t**)0, 0, rs, 1); 633 } 634 635 static inline own wasm_functype_t* wasm_functype_new_1_1(own wasm_valtype_t* p, 636 own wasm_valtype_t* r) 637 { 638 wasm_valtype_t* ps[1] = {p}; 639 wasm_valtype_t* rs[1] = {r}; 640 return wasm_functype_new(ps, 1, rs, 1); 641 } 642 643 static inline own wasm_functype_t* wasm_functype_new_2_1(own wasm_valtype_t* p1, 644 own wasm_valtype_t* p2, 645 own wasm_valtype_t* r) 646 { 647 wasm_valtype_t* ps[2] = {p1, p2}; 648 wasm_valtype_t* rs[1] = {r}; 649 return wasm_functype_new(ps, 2, rs, 1); 650 } 651 652 static inline own wasm_functype_t* wasm_functype_new_3_1(own wasm_valtype_t* p1, 653 own wasm_valtype_t* p2, 654 own wasm_valtype_t* p3, 655 own wasm_valtype_t* r) 656 { 657 wasm_valtype_t* ps[3] = {p1, p2, p3}; 658 wasm_valtype_t* rs[1] = {r}; 659 return wasm_functype_new(ps, 3, rs, 1); 660 } 661 662 static inline own wasm_functype_t* wasm_functype_new_0_2(own wasm_valtype_t* r1, 663 own wasm_valtype_t* r2) 664 { 665 wasm_valtype_t* rs[2] = {r1, r2}; 666 return wasm_functype_new((wasm_valtype_t**)0, 0, rs, 2); 667 } 668 669 static inline own wasm_functype_t* wasm_functype_new_1_2(own wasm_valtype_t* p, 670 own wasm_valtype_t* r1, 671 own wasm_valtype_t* r2) 672 { 673 wasm_valtype_t* ps[1] = {p}; 674 wasm_valtype_t* rs[2] = {r1, r2}; 675 return wasm_functype_new(ps, 1, rs, 2); 676 } 677 678 static inline own wasm_functype_t* wasm_functype_new_2_2(own wasm_valtype_t* p1, 679 own wasm_valtype_t* p2, 680 own wasm_valtype_t* r1, 681 own wasm_valtype_t* r2) 682 { 683 wasm_valtype_t* ps[2] = {p1, p2}; 684 wasm_valtype_t* rs[2] = {r1, r2}; 685 return wasm_functype_new(ps, 2, rs, 2); 686 } 687 688 static inline own wasm_functype_t* wasm_functype_new_3_2(own wasm_valtype_t* p1, 689 own wasm_valtype_t* p2, 690 own wasm_valtype_t* p3, 691 own wasm_valtype_t* r1, 692 own wasm_valtype_t* r2) 693 { 694 wasm_valtype_t* ps[3] = {p1, p2, p3}; 695 wasm_valtype_t* rs[2] = {r1, r2}; 696 return wasm_functype_new(ps, 3, rs, 2); 697 } 698 699 /////////////////////////////////////////////////////////////////////////////// 700 701 #undef own 702 703 #ifdef __cplusplus 704 } // extern "C" 705 #endif 706 707 #endif // #ifdef __WASM_H