1 /** 2 * \file wasmtime/types/extern.hh 3 */ 4 5 #ifndef WASMTIME_TYPES_EXTERN_HH 6 #define WASMTIME_TYPES_EXTERN_HH 7 8 #include <variant> 9 #include <wasm.h> 10 #include <wasmtime/types/export.hh> 11 #include <wasmtime/types/func.hh> 12 #include <wasmtime/types/global.hh> 13 #include <wasmtime/types/import.hh> 14 #include <wasmtime/types/memory.hh> 15 #include <wasmtime/types/table.hh> 16 #include <wasmtime/types/tag.hh> 17 18 namespace wasmtime { 19 20 /** 21 * \brief Generic type of a WebAssembly item. 22 */ 23 class ExternType { 24 friend class ExportType; 25 friend class ImportType; 26 27 public: 28 /// \typedef Ref 29 /// \brief Non-owning reference to an item's type 30 /// 31 /// This cannot be used after the original owner has been deleted, and 32 /// otherwise this is used to determine what the actual type of the outer item 33 /// is. 34 typedef std::variant<FuncType::Ref, GlobalType::Ref, TableType::Ref, 35 MemoryType::Ref, TagType::Ref> 36 Ref; 37 38 /// Extract the type of the item imported by the provided type. from_import(ImportType::Ref ty)39 static Ref from_import(ImportType::Ref ty) { 40 // TODO: this would ideally be some sort of implicit constructor, unsure how 41 // to do that though... 42 return ref_from_c(ty.raw_type()); 43 } 44 45 /// Extract the type of the item exported by the provided type. from_export(ExportType::Ref ty)46 static Ref from_export(ExportType::Ref ty) { 47 // TODO: this would ideally be some sort of implicit constructor, unsure how 48 // to do that though... 49 return ref_from_c(ty.raw_type()); 50 } 51 52 private: ref_from_c(const wasm_externtype_t * ptr)53 static Ref ref_from_c(const wasm_externtype_t *ptr) { 54 switch (wasm_externtype_kind(ptr)) { 55 case WASM_EXTERN_FUNC: 56 return wasm_externtype_as_functype_const(ptr); 57 case WASM_EXTERN_GLOBAL: 58 return wasm_externtype_as_globaltype_const(ptr); 59 case WASM_EXTERN_TABLE: 60 return wasm_externtype_as_tabletype_const(ptr); 61 case WASM_EXTERN_MEMORY: 62 return wasm_externtype_as_memorytype_const(ptr); 63 case WASM_EXTERN_TAG: 64 return wasm_externtype_as_tagtype_const(ptr); 65 } 66 std::abort(); 67 } 68 }; 69 70 }; // namespace wasmtime 71 72 #endif // WASMTIME_TYPES_EXTERN_HH 73