Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.148
0.1.149
2 changes: 1 addition & 1 deletion pykwasm/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "pykwasm"
version = "0.1.148"
version = "0.1.149"
description = ""
readme = "README.md"
requires-python = "~=3.10"
Expand Down
Empty file.
53 changes: 53 additions & 0 deletions pykwasm/src/pykwasm/binary/combinators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from .integers import u32
from .utils import WasmParseError, reset

if TYPE_CHECKING:
from .utils import A, InputStream, Parser


def iterate(f: Parser[A], s: InputStream) -> list[A]:
results = []
while True:
try:
results.append(f(s))
except (WasmParseError, IndexError, ValueError):
break
return results


def sized(p: Parser[A], s: InputStream) -> A:
size = u32(s)
start_pos = s.tell()
res = p(s)
end_pos = s.tell()
if end_pos - start_pos != size:
raise WasmParseError('Size mismatch')
return res


def parse_n(p: Parser[A], n: int, s: InputStream) -> list[A]:
results = []
for _ in range(n):
x = p(s)
results.append(x)
return results


def list_of(p: Parser[A], s: InputStream) -> list[A]:
n = u32(s)
return parse_n(p, n, s)


def either(ps: list[Parser[A]], s: InputStream) -> A:
for p in ps:
pos = s.tell()
try:
return p(s)
except WasmParseError:
reset(pos, s)
continue
raise WasmParseError('None of the alternatives succeeded')
21 changes: 21 additions & 0 deletions pykwasm/src/pykwasm/binary/floats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import struct
from typing import TYPE_CHECKING

from .utils import read_bytes

if TYPE_CHECKING:
from .utils import InputStream


def f32(s: InputStream) -> float:
bs = read_bytes(4, s)
f = struct.unpack('<f', bs)[0]
return f


def f64(s: InputStream) -> float:
bs = read_bytes(8, s)
f = struct.unpack('<d', bs)[0]
return f
69 changes: 69 additions & 0 deletions pykwasm/src/pykwasm/binary/indices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pykwasm.kwasm_ast as wast

from .integers import u32
from .utils import WasmParseError, read_byte

if TYPE_CHECKING:
from pyk.kast.inner import KInner

from .utils import InputStream


def typeidx(s: InputStream) -> int:
return u32(s)


def funcidx(s: InputStream) -> int:
return u32(s)


def tableidx(s: InputStream) -> int:
return u32(s)


def memidx(s: InputStream) -> int:
return u32(s)


def globalidx(s: InputStream) -> int:
return u32(s)


def tagidx(s: InputStream) -> int:
return u32(s)


def elemidx(s: InputStream) -> int:
return u32(s)


def dataidx(s: InputStream) -> int:
return u32(s)


def localidx(s: InputStream) -> int:
return u32(s)


def labelidx(s: InputStream) -> int:
return u32(s)


def externidx(s: InputStream) -> KInner:
match read_byte(s):
case 0x00:
return wast.externidx_func(funcidx(s))
case 0x01:
return wast.externidx_table(tableidx(s))
case 0x02:
return wast.externidx_memory(memidx(s))
case 0x03:
return wast.externidx_global(globalidx(s))
case 0x04:
return wast.externidx_tag(tagidx(s))
case x:
raise WasmParseError(f'Invalid externidx descriptor: {x}')
Loading
Loading