@@ -398,11 +398,18 @@ def __init__(self, namespace, name, func, user_data):
398
398
arg_names = [arg for arg in hints .keys () if arg != "return" ]
399
399
returns = hints .pop ("return" , None )
400
400
401
+ uses_current_plugin = False
402
+ if len (arg_names ) > 0 and hints .get (arg_names [0 ], None ) == CurrentPlugin :
403
+ uses_current_plugin = True
404
+ arg_names = arg_names [1 :]
405
+
401
406
args = [_map_arg (arg , hints [arg ]) for arg in arg_names ]
407
+
402
408
returns = [] if returns is None else _map_ret (returns )
403
409
404
410
def inner_func (plugin , inputs , outputs , * user_data ):
405
- inner_args = [
411
+ first_arg = [plugin ] if uses_current_plugin else []
412
+ inner_args = first_arg + [
406
413
extract (plugin , slot ) for ((_ , extract ), slot ) in zip (args , inputs )
407
414
]
408
415
@@ -523,6 +530,7 @@ def call(
523
530
function_name : str ,
524
531
data : Union [str , bytes ],
525
532
parse : Callable [[Any ], Any ] = lambda xs : bytes (xs ),
533
+ host_context : Any = None ,
526
534
) -> Any :
527
535
"""
528
536
Call a function by name with the provided input data
@@ -533,11 +541,13 @@ def call(
533
541
:raises: An :py:class:`extism.Error <.extism.Error>` if the guest function call was unsuccessful.
534
542
:returns: The returned bytes from the guest function as interpreted by the ``parse`` parameter.
535
543
"""
544
+
545
+ host_context = _ffi .new_handle (host_context )
536
546
if isinstance (data , str ):
537
547
data = data .encode ()
538
548
self ._check_error (
539
- _lib .extism_plugin_call (
540
- self .plugin , function_name .encode (), data , len (data )
549
+ _lib .extism_plugin_call_with_host_context (
550
+ self .plugin , function_name .encode (), data , len (data ), host_context
541
551
)
542
552
)
543
553
out_len = _lib .extism_plugin_output_length (self .plugin )
@@ -608,6 +618,12 @@ def memory(self, mem: Memory) -> _ffi.buffer:
608
618
return None
609
619
return _ffi .buffer (p + mem .offset , mem .length )
610
620
621
+ def host_context (self ) -> Any :
622
+ result = _lib .extism_current_plugin_host_context (self .pointer )
623
+ if result == 0 :
624
+ return None
625
+ return _ffi .from_handle (result )
626
+
611
627
def alloc (self , size : int ) -> Memory :
612
628
"""
613
629
Allocate a new block of memory.
0 commit comments