Skip to content

Commit d071875

Browse files
committed
feat: Added support for globals
1 parent 2c4e903 commit d071875

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

binaryen/__global.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from ._binaryen import lib
2+
from .__expression import Expression
3+
4+
5+
class Global:
6+
def __init__(self, ref):
7+
self.ref = ref
8+
9+
def get_name(self):
10+
return str(lib.BinaryenGlobalGetName(self.ref))
11+
12+
def get_type(self):
13+
return lib.BinaryenGlobalGetType(self.ref)
14+
15+
def is_mutable(self):
16+
return bool(lib.BinaryenGlobalIsMutable(self.ref))
17+
18+
def get_init_expr(self):
19+
return Expression(lib.BinaryenGlobalGetInitExpr(self.ref))

binaryen/__module.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from . import literal
66
from .__expression import Block, Expression
7+
from .__global import Global
78
from .__feature import Feature
89
from .__functionref import FunctionRef
910
from ._binaryen import ffi, lib
1011
from .internals import (
11-
BinaryenGlobalRef,
1212
BinaryenHeapType,
1313
BinaryenLiteral,
1414
BinaryenOp,
@@ -385,11 +385,25 @@ def add_function_export(
385385

386386
def add_global(
387387
self, name: bytes, global_type: BinaryenType, mutable: bool, init: Expression
388-
) -> BinaryenGlobalRef:
388+
):
389389
ref = lib.BinaryenAddGlobal(self.ref, name, global_type, mutable, init.ref)
390-
return ref
390+
return Global(ref)
391+
392+
def get_global(self, name: bytes):
393+
ref = lib.BinaryenGetGlobal(self.ref, name)
394+
if ref == ffi.NULL:
395+
return None
396+
return Global(ref)
397+
398+
def remove_global(self, name: bytes):
399+
lib.BinaryenRemoveGlobal(self.ref, name)
400+
401+
def get_num_globals(self):
402+
return int(lib.BinaryenGetNumGlobals(self.ref))
391403

392-
# TODO: GetGlobal, RemoveGlobal, GetNumGlobals, GetGlobalByIndex
404+
def get_global_by_index(self, index: int):
405+
ref = lib.BinaryenGetGlobalByIndex(self.ref, index)
406+
return Global(ref)
393407

394408
# TODO: AddTag, GetTag, RemoveTag
395409

binaryen/internals.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ class __BaseType:
2222
BinaryenExternalKind = __NewType("BinaryenExternalKind", __BaseType)
2323
BinaryenOp = __NewType("BinaryenOp", __BaseType)
2424
BinaryenLiteral = __NewType("BinaryenLiteral", __BaseType)
25-
BinaryenGlobalRef = __NewType("BinaryenGlobalRef", __BaseType)

0 commit comments

Comments
 (0)