33from typing import TypeVar , TypeAlias , Any
44
55from .lib import lib
6- from .types import BinaryenType
6+ from .types import BinaryenType , BinaryenAuto , NULL , BinaryenNone , none
77from .expression import Expression , Block
88from ._binaryen_cffi import ffi , _cffi_backend
99
@@ -19,6 +19,11 @@ def _none_to_null(possibly_none: T | None) -> _cffi_backend.FFI.CData | T:
1919 return ffi .NULL
2020 return possibly_none
2121
22+ def _none_to_binaryen (possibly_none : T | None ) -> BinaryenNone | T :
23+ if possibly_none is None :
24+ return none
25+ return possibly_none
26+
2227
2328def _change_file_extension (filename : str , extension : str ) -> str :
2429 # TODO: Change this to actual file handling
@@ -61,7 +66,7 @@ def block(
6166 self ,
6267 name : bytes | None ,
6368 children : list [Expression ],
64- binaryen_type : BinaryenType ,
69+ binaryen_type : BinaryenType | BinaryenAuto ,
6570 ) -> Block :
6671 # Convert children object list to list of children pointers
6772 children_refs = list (map (lambda x : x .ref , children ))
@@ -167,18 +172,46 @@ def unreachable(self) -> Expression:
167172
168173 # NOTE: Done - expression getId to BlockRemoveChildAt
169174
175+ # TODO: Carry on from here
176+
177+ # Imports
178+
179+ def add_function_import (
180+ self ,
181+ internalName : bytes ,
182+ externalModuleName : bytes ,
183+ externalBaseName : bytes ,
184+ params : BinaryenType ,
185+ results : BinaryenType ,
186+ ) -> None :
187+ lib .BinaryenAddFunctionImport (
188+ self .ref ,
189+ internalName ,
190+ externalModuleName ,
191+ externalBaseName ,
192+ params ,
193+ results ,
194+ )
195+
170196 # EXTRA:
171197
172198 def add_function (
173199 self ,
174200 name : bytes ,
175- params : BinaryenType ,
176- results : BinaryenType ,
177- var_types : list [BinaryenType ],
201+ params : BinaryenType | None ,
202+ results : BinaryenType | None ,
203+ var_types : list [BinaryenType ] | None ,
178204 body : Expression ,
179205 ) -> None :
206+ if var_types is None or (len (var_types ) == 0 ):
207+ length = 0
208+ c_var_types = _none_to_null (var_types )
209+ else :
210+ length = len (var_types )
211+ c_var_types = var_types
212+
180213 return lib .BinaryenAddFunction (
181- self .ref , name , params , results , var_types , len ( var_types ) , body .ref
214+ self .ref , name , _none_to_binaryen ( params ), _none_to_binaryen ( results ), c_var_types , length , body .ref
182215 )
183216
184217 def call (
@@ -192,6 +225,9 @@ def call(
192225 self .ref , target , operand_refs , len (operand_refs ), return_type
193226 )
194227 return Expression (ref )
228+
229+ def set_memory ():
230+ raise NotImplementedError
195231
196232 def add_function_export (
197233 self , internal_name : bytes , external_name : bytes
0 commit comments