Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use Wasm.MODULE | |
| 3 | use Wasm.TAG | |
| 4 | use Wasm.FUNC_TYPE | |
| 5 | use Wasm.VALUE_TYPE | |
| 6 | use Wasm.AbstractHeapType | |
| 7 | use Wasm.FUNCTION | |
| 8 | use Wasm.HEAP_TYPE | |
| 9 | use Wasm.INSTRUCTION | |
| 10 | use Wasm.INDEX_INSTRUCTION | |
| 11 | use Wasm.I32_CONSTANT | |
| 12 | use Wasm.TYPE_INSTRUCTION | |
| 13 | use Wasm.TYPE_FIELD_INSTRUCTION | |
| 14 | use Wasm.HEAP_TYPE_INSTRUCTION | |
| 15 | use Wasm.Opcode | |
| 16 | ||
| 17 | use Collections.LIST | |
| 18 | ||
| 19 | /// The tag every exception is thrown with, whose one value is the | |
| 20 | /// exception, and the types a handler receives it in. Declared the | |
| 21 | /// first time a body throws or catches, so a program that does | |
| 22 | /// neither needs no exception type. | |
| 23 | class WASMGC_EXCEPTIONS is | |
| 24 | _module: MODULE | |
| 25 | _registry: WASMGC_TYPE_REGISTRY | |
| 26 | ||
| 27 | _is_declared: bool | |
| 28 | ||
| 29 | _tag_index: int? | |
| 30 | _exception: VALUE_TYPE? | |
| 31 | _handler_type: int | |
| 32 | ||
| 33 | /// - module: the module the tag is declared in | |
| 34 | /// - registry: the struct types, the exception type's among them | |
| 35 | init(module: MODULE, registry: WASMGC_TYPE_REGISTRY) is | |
| 36 | _module = module | |
| 37 | _registry = registry | |
| 38 | si | |
| 39 | ||
| 40 | /// The index of the tag, or null where the exception type is not | |
| 41 | /// one the target can hold. | |
| 42 | tag_index: int? is | |
| 43 | _declare() | |
| 44 | ||
| 45 | return _tag_index | |
| 46 | si | |
| 47 | ||
| 48 | /// The type the tag carries the exception as: a non-null | |
| 49 | /// reference to the exception type's struct. Null as `tag_index` | |
| 50 | /// is. | |
| 51 | exception: VALUE_TYPE? is | |
| 52 | _declare() | |
| 53 | ||
| 54 | return _exception | |
| 55 | si | |
| 56 | ||
| 57 | /// The block type a handler block has: no parameters, and as | |
| 58 | /// results the exception and the reference that rethrows it. | |
| 59 | handler_type: int is | |
| 60 | _declare() | |
| 61 | ||
| 62 | return _handler_type | |
| 63 | si | |
| 64 | ||
| 65 | /// The index of the tag where a body has thrown or caught, without | |
| 66 | /// declaring it otherwise. | |
| 67 | declared_tag_index: int? => _tag_index | |
| 68 | ||
| 69 | ||
| 70 | /// The function the loader calls on an exception nothing caught, | |
| 71 | /// taking the exception and answering a JavaScript string of its | |
| 72 | /// type's name and its message, as .NET prints an unhandled one. | |
| 73 | /// | |
| 74 | /// - strings: the string helpers the text is built with | |
| 75 | /// - message_getter: the function index of the exception type's | |
| 76 | /// `message` getter | |
| 77 | /// - build: numbers the function and builds it when the module is | |
| 78 | /// finished | |
| 79 | describe(strings: WASMGC_STRINGS, message_getter: int, build: (() -> FUNCTION) -> int) -> int? is | |
| 80 | let exception = _exception | |
| 81 | let descriptor = _registry.string_descriptor_global | |
| 82 | ||
| 83 | if !exception? \/ !descriptor? then | |
| 84 | return null | |
| 85 | fi | |
| 86 | ||
| 87 | let held = VALUE_TYPE.of_reference(exception.heap_type()!, true) | |
| 88 | let separator = strings.literal(": ") | |
| 89 | let concat = strings.concat_function | |
| 90 | let to_host = strings.to_host_function | |
| 91 | ||
| 92 | // parameter 0 the exception as anyref; local 1 the exception | |
| 93 | let body = LIST[INSTRUCTION]([ | |
| 94 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, 0), | |
| 95 | HEAP_TYPE_INSTRUCTION(Opcode.REF_CAST_NULL, exception.heap_type()!), | |
| 96 | INDEX_INSTRUCTION(Opcode.LOCAL_SET, 1), | |
| 97 | INDEX_INSTRUCTION(Opcode.GLOBAL_GET, descriptor), | |
| 98 | I32_CONSTANT(0), | |
| 99 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, 1), | |
| 100 | TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.OBJECT_TYPE_INDEX, 0), | |
| 101 | TYPE_FIELD_INSTRUCTION(Opcode.STRUCT_GET, WASMGC_TYPE_REGISTRY.DESCRIPTOR_TYPE_INDEX, WASMGC_TYPE_REGISTRY.DESCRIPTOR_NAME), | |
| 102 | TYPE_INSTRUCTION(Opcode.STRUCT_NEW, strings.string_type_index), | |
| 103 | INDEX_INSTRUCTION(Opcode.GLOBAL_GET, separator), | |
| 104 | INDEX_INSTRUCTION(Opcode.CALL, concat), | |
| 105 | INDEX_INSTRUCTION(Opcode.LOCAL_GET, 1), | |
| 106 | INDEX_INSTRUCTION(Opcode.CALL, message_getter), | |
| 107 | INDEX_INSTRUCTION(Opcode.CALL, concat), | |
| 108 | INDEX_INSTRUCTION(Opcode.CALL, to_host) | |
| 109 | ]:INSTRUCTION) | |
| 110 | ||
| 111 | let type_index = _module.add_type( | |
| 112 | FUNC_TYPE([VALUE_TYPE.of_abstract(AbstractHeapType.ANY)], [VALUE_TYPE.of_abstract(AbstractHeapType.EXTERN)])) | |
| 113 | ||
| 114 | return build(() => FUNCTION(type_index, [held], body)) | |
| 115 | si | |
| 116 | _declare() is | |
| 117 | if _is_declared then | |
| 118 | return | |
| 119 | fi | |
| 120 | ||
| 121 | _is_declared = true | |
| 122 | ||
| 123 | let type = IoC.CONTAINER.instance.innate_symbol_lookup.get_exception_type() | |
| 124 | ||
| 125 | if let index = _registry.type_index(type) then | |
| 126 | let exception = VALUE_TYPE.of_type_index(index, false) | |
| 127 | ||
| 128 | _exception = exception | |
| 129 | _tag_index = _module.add_tag(TAG(_module.add_type(FUNC_TYPE([exception], [])))) | |
| 130 | ||
| 131 | _handler_type = _module.add_type( | |
| 132 | FUNC_TYPE([], [exception, VALUE_TYPE.of_abstract(AbstractHeapType.EXN)])) | |
| 133 | fi | |
| 134 | si | |
| 135 | si | |
| 136 | si |