From e94a046a548795b8675bc109c87413de6bca53e7 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 12 Jul 2024 22:39:20 +0900 Subject: feat: add Linker class --- src/WebAssembly/Execution/Linker.php | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/WebAssembly/Execution/Linker.php (limited to 'src/WebAssembly/Execution/Linker.php') diff --git a/src/WebAssembly/Execution/Linker.php b/src/WebAssembly/Execution/Linker.php new file mode 100644 index 0000000..3c9c1d0 --- /dev/null +++ b/src/WebAssembly/Execution/Linker.php @@ -0,0 +1,49 @@ +> + */ + private array $symbols = []; + + public function __construct( + private Store $store, + ) { + } + + public function register(string $namespace, string $name, Extern $extern): void + { + $externVal = $this->store->register($extern); + $this->symbols[$namespace][$name] = $externVal; + } + + public function registerNamespace(string $namespace, ExporterInterface $exporter): void + { + foreach ($exporter->exports() as $export) { + $this->symbols[$namespace][$export->name] = $export->value; + } + } + + /** + * @return list + */ + public function resolve(Module $module): array + { + $externVals = []; + foreach ($module->imports as $import) { + $externVal = $this->symbols[$import->module][$import->name] ?? null; + if ($externVal === null) { + throw new LinkErrorException("import not found: {$import->module}::{$import->name}"); + } + $externVals[] = $externVal; + } + return $externVals; + } +} -- cgit v1.2.3-70-g09d2