Skip to content

Commit

Permalink
call with host context
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Dec 12, 2024
1 parent 7c43251 commit d284a87
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/CurrentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ public function __construct($lib, \FFI\CData $handle)
$this->lib = $lib;
}

/**
* Get *a copy* of the current plugin call's associated host context data. Returns null if call was made without host context.
*
* @return mixed|null Returns a copy of the host context data or null if none was provided
*/
public function getCallHostContext()
{
$serialized = $this->lib->extism_current_plugin_host_context($this->handle);
if ($serialized === null) {
return null;
}

return unserialize($serialized);
}

/**
* Reads a string from the plugin's memory at the given offset.
*
Expand Down
31 changes: 30 additions & 1 deletion src/Internal/LibExtism.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,23 @@ public function extism_plugin_config(\FFI\CData $plugin, string $json, int $json
public function extism_plugin_call_with_host_context(\FFI\CData $plugin, string $func_name, string $data, int $data_len, $host_context): int
{
$dataPtr = $this->owned("uint8_t", $data);
return $this->ffi->extism_plugin_call_with_host_context($plugin, $func_name, $dataPtr, $data_len, $host_context);

if ($host_context === null) {
return $this->ffi->extism_plugin_call_with_host_context($plugin, $func_name, $dataPtr, $data_len, null);
}

$serialized = serialize($host_context);
$contextPtr = $this->ffi->new("char*");
$contextArray = $this->ownedZero($serialized);
$contextPtr = \FFI::addr($contextArray);

return $this->ffi->extism_plugin_call_with_host_context(
$plugin,
$func_name,
$dataPtr,
$data_len,
$contextPtr
);
}

/**
Expand All @@ -219,6 +235,19 @@ public function extism_plugin_call(\FFI\CData $plugin, string $func_name, string
return $this->ffi->extism_plugin_call($plugin, $func_name, $dataPtr, $data_len);
}

/**
* Get the current plugin's associated host context data
*/
public function extism_current_plugin_host_context(\FFI\CData $plugin): ?string
{
$ptr = $this->ffi->extism_current_plugin_host_context($plugin);
if ($ptr === null || \FFI::isNull($ptr)) {
return null;
}

return \FFI::string($this->ffi->cast("char *", $ptr));
}

public function extism_error(\FFI\CData $plugin): ?string
{
return $this->ffi->extism_error($plugin);
Expand Down
33 changes: 33 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ public function testHostFunctions(): void
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}


public function testCallWithHostContext(): void
{
$multiUserKvStore = [[]];

$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$multiUserKvStore) {
$ctx = $p->getCallHostContext(); // get a copy of the host context data
$userId = $ctx["userId"];
$kvStore = $multiUserKvStore[$userId] ?? [];

return $kvStore[$key] ?? "\0\0\0\0";
});

$kvWrite = new HostFunction("kv_write", [ExtismValType::I64, ExtismValType::I64], [], function (CurrentPlugin $p, string $key, string $value) use (&$multiUserKvStore) {
$ctx = $p->getCallHostContext(); // get a copy of the host context data
$userId = $ctx["userId"];
$kvStore = $multiUserKvStore[$userId] ?? [];

$kvStore[$key] = $value;
$multiUserKvStore[$userId] = $kvStore;
});

$plugin = self::loadPlugin("count_vowels_kvstore.wasm", [$kvRead, $kvWrite]);

$ctx = ["userId" => 1];

$response = $plugin->callWithContext("count_vowels", "Hello World!", $ctx);
$this->assertEquals('{"count":3,"total":3,"vowels":"aeiouAEIOU"}', $response);

$response = $plugin->callWithContext("count_vowels", "Hello World!", $ctx);
$this->assertEquals('{"count":3,"total":6,"vowels":"aeiouAEIOU"}', $response);
}

public function testHostFunctionManual(): void
{
$kvstore = [];
Expand Down

0 comments on commit d284a87

Please sign in to comment.