summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.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/funcs.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/funcs.go')
-rw-r--r--vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go
new file mode 100644
index 000000000..a809ff120
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-ffmpreg/wasm/funcs.go
@@ -0,0 +1,74 @@
+package wasm
+
+import (
+ "context"
+
+ "github.com/tetratelabs/wazero/api"
+ "github.com/tetratelabs/wazero/experimental"
+)
+
+type snapshotskey struct{}
+
+// withSetjmpLongjmp updates the context to contain wazero/experimental.Snapshotter{} support,
+// and embeds the necessary snapshots map required for later calls to Setjmp() / Longjmp().
+func withSetjmpLongjmp(ctx context.Context) context.Context {
+ snapshots := make(map[uint32]experimental.Snapshot, 10)
+ ctx = experimental.WithSnapshotter(ctx)
+ ctx = context.WithValue(ctx, snapshotskey{}, snapshots)
+ return ctx
+}
+
+func getSnapshots(ctx context.Context) map[uint32]experimental.Snapshot {
+ v, _ := ctx.Value(snapshotskey{}).(map[uint32]experimental.Snapshot)
+ return v
+}
+
+// setjmp implements the C function: setjmp(env jmp_buf)
+func setjmp(ctx context.Context, mod api.Module, stack []uint64) {
+
+ // Input arguments.
+ envptr := api.DecodeU32(stack[0])
+
+ // Take snapshot of current execution environment.
+ snapshotter := experimental.GetSnapshotter(ctx)
+ snapshot := snapshotter.Snapshot()
+
+ // Get stored snapshots map.
+ snapshots := getSnapshots(ctx)
+ if snapshots == nil {
+ panic("setjmp / longjmp not supported")
+ }
+
+ // Set latest snapshot in map.
+ snapshots[envptr] = snapshot
+
+ // Set return.
+ stack[0] = 0
+}
+
+// longjmp implements the C function: int longjmp(env jmp_buf, value int)
+func longjmp(ctx context.Context, mod api.Module, stack []uint64) {
+
+ // Input arguments.
+ envptr := api.DecodeU32(stack[0])
+ // val := stack[1]
+
+ // Get stored snapshots map.
+ snapshots := getSnapshots(ctx)
+ if snapshots == nil {
+ panic("setjmp / longjmp not supported")
+ }
+
+ // Get snapshot stored in map.
+ snapshot := snapshots[envptr]
+ if snapshot == nil {
+ panic("must first call setjmp")
+ }
+
+ // Set return.
+ stack[0] = 0
+
+ // Restore execution and
+ // return passed value arg.
+ snapshot.Restore(stack[1:])
+}