|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pykwasm.kwasm_ast as wast |
| 6 | + |
| 7 | +from .integers import u32 |
| 8 | +from .utils import WasmParseError, read_byte |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from pyk.kast.inner import KInner |
| 12 | + |
| 13 | + from .utils import InputStream |
| 14 | + |
| 15 | + |
| 16 | +def typeidx(s: InputStream) -> int: |
| 17 | + return u32(s) |
| 18 | + |
| 19 | + |
| 20 | +def funcidx(s: InputStream) -> int: |
| 21 | + return u32(s) |
| 22 | + |
| 23 | + |
| 24 | +def tableidx(s: InputStream) -> int: |
| 25 | + return u32(s) |
| 26 | + |
| 27 | + |
| 28 | +# TODO multi-memory support is future work; the K semantics models a single memory, |
| 29 | +# so any reference to a memory other than 0 is rejected instead of mis-executing. |
| 30 | +def memidx(s: InputStream) -> int: |
| 31 | + x = u32(s) |
| 32 | + if x != 0: |
| 33 | + raise WasmParseError(f'Multi-memory is not supported. Expected memory index 0, got: {x}') |
| 34 | + return x |
| 35 | + |
| 36 | + |
| 37 | +def globalidx(s: InputStream) -> int: |
| 38 | + return u32(s) |
| 39 | + |
| 40 | + |
| 41 | +def tagidx(s: InputStream) -> int: |
| 42 | + return u32(s) |
| 43 | + |
| 44 | + |
| 45 | +def elemidx(s: InputStream) -> int: |
| 46 | + return u32(s) |
| 47 | + |
| 48 | + |
| 49 | +def dataidx(s: InputStream) -> int: |
| 50 | + return u32(s) |
| 51 | + |
| 52 | + |
| 53 | +def localidx(s: InputStream) -> int: |
| 54 | + return u32(s) |
| 55 | + |
| 56 | + |
| 57 | +def labelidx(s: InputStream) -> int: |
| 58 | + return u32(s) |
| 59 | + |
| 60 | + |
| 61 | +def externidx(s: InputStream) -> KInner: |
| 62 | + match read_byte(s): |
| 63 | + case 0x00: |
| 64 | + return wast.externidx_func(funcidx(s)) |
| 65 | + case 0x01: |
| 66 | + return wast.externidx_table(tableidx(s)) |
| 67 | + case 0x02: |
| 68 | + return wast.externidx_memory(memidx(s)) |
| 69 | + case 0x03: |
| 70 | + return wast.externidx_global(globalidx(s)) |
| 71 | + case 0x04: |
| 72 | + return wast.externidx_tag(tagidx(s)) |
| 73 | + case x: |
| 74 | + raise WasmParseError(f'Invalid externidx descriptor: {x}') |
0 commit comments