summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-10-28 10:55:48 +0000
committerLibravatar GitHub <noreply@github.com>2024-10-28 10:55:48 +0000
commite86592bc320ba0c127bb47bb6b0028ffe69e77e3 (patch)
tree87309bc74b9c59fe7c56ed25b4e96b76280369b8 /vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go
parent[chore]: Bump github.com/tdewolff/minify/v2 from 2.21.0 to 2.21.1 (#3489) (diff)
downloadgotosocial-e86592bc320ba0c127bb47bb6b0028ffe69e77e3.tar.xz
[chore] pull in go-ffmpreg v0.4.1 (#3485)
* pull in go-ffmpreg v0.4.1 * bring back GTS_WAZERO_COMPILATION_CACHE
Diffstat (limited to 'vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go')
-rw-r--r--vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go
new file mode 100644
index 000000000..328a26193
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/runtime.go
@@ -0,0 +1,67 @@
+package wasm
+
+import (
+ "context"
+
+ "github.com/tetratelabs/wazero"
+ "github.com/tetratelabs/wazero/api"
+ "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
+)
+
+// CoreFeatures are the WebAssembly Core specification
+// features our embedded binaries are compiled with.
+const CoreFeatures = api.CoreFeatureSIMD |
+ api.CoreFeatureBulkMemoryOperations |
+ api.CoreFeatureNonTrappingFloatToIntConversion |
+ api.CoreFeatureMutableGlobal |
+ api.CoreFeatureReferenceTypes |
+ api.CoreFeatureSignExtensionOps
+
+// NewRuntime returns a new WebAssembly wazero.Runtime compatible with go-ffmpreg.
+func NewRuntime(ctx context.Context, cfg wazero.RuntimeConfig) (wazero.Runtime, error) {
+ var err error
+
+ if cfg == nil {
+ // Ensure runtime config is set.
+ cfg = wazero.NewRuntimeConfig()
+ }
+
+ // Set core features ffmpeg compiled with.
+ cfg = cfg.WithCoreFeatures(CoreFeatures)
+
+ // Instantiate runtime with prepared config.
+ rt := wazero.NewRuntimeWithConfig(ctx, cfg)
+
+ // Prepare default "env" host module.
+ env := rt.NewHostModuleBuilder("env")
+
+ // Register setjmp host function.
+ env = env.NewFunctionBuilder().
+ WithGoModuleFunction(
+ api.GoModuleFunc(setjmp),
+ []api.ValueType{api.ValueTypeI32},
+ []api.ValueType{api.ValueTypeI32},
+ ).Export("setjmp")
+
+ // Register longjmp host function.
+ env = env.NewFunctionBuilder().
+ WithGoModuleFunction(
+ api.GoModuleFunc(longjmp),
+ []api.ValueType{api.ValueTypeI32, api.ValueTypeI32},
+ []api.ValueType{},
+ ).Export("longjmp")
+
+ // Instantiate "env" module.
+ _, err = env.Instantiate(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ // Instantiate the wasi snapshot preview 1 in runtime.
+ _, err = wasi_snapshot_preview1.Instantiate(ctx, rt)
+ if err != nil {
+ return nil, err
+ }
+
+ return rt, nil
+}