mirror of
https://github.com/hanzoai/zip.git
synced 2026-10-30 20:11:40 +00:00
Add Runner — a registry of language → Engine that executes arbitrary-
language source in goroutine-scoped, context.Context-bound tasks. Each
Run spawns a worker (engine.Eval) and a watcher (ctx.Done() → engine
interrupt), joining both before return so a borrowed VM is never handed
back to its pool while still interrupted. This generalizes the settled
JSRuntime.EvalContext cancellation contract (one worker, one watcher,
joined, engine-specific interrupt) across every backend a host registers.
zip ships exactly one in-tree engine: the pure-Go goja engine, surfaced
via (*JSRuntime).Engine() and registered as "js". It self-manages ctx
inside EvalContext (per-call interrupt on the exact borrowed VM), so the
Runner's watcher is a benign no-op for it. base's backends (pyvm, v8vm,
wasmvm, starkvm) plug in through the same Engine seam via thin extruntime
adapters at app startup — the registry is the seam, not an import edge.
zip never imports base; the dependency direction stays base → zip.
Engine is the zip-side projection of base's extruntime backends: any
value implementing Eval(ctx, src, args) satisfies it. Optional
Interruptible lets the watcher abort a runaway eval out-of-band.
Wire the unified runner into examples/express-in-zip: POST /runtime/:lang
reads the body as source, dispatches to the engine for :lang, returns
{result} or 404 {error:"unknown language"}.
Tests (go test -race, all green):
- JS happy path: Run(ctx, "js", "1+2") → int64(3)
- JS function call: define + invoke in one eval
- JS ctx deadline: while(true){} interrupted, DeadlineExceeded, ~50ms
- unknown language: ErrUnknownLanguage sentinel, no panic
- concurrent: 100 goroutines, registry RWMutex + per-call VM isolation
- example: POST /runtime/js "40+2" → {"result":42}; cobol → 404
No cgo. No base import. README documents the Runner + SPI registration.
140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// newJSRunner builds a Runner with zip's real goja engine registered as
|
|
// "js". No stubs: this is the same *JSRuntime base/gojavm wraps.
|
|
func newJSRunner(t *testing.T) Runner {
|
|
t.Helper()
|
|
rt, err := NewJSRuntime(JSOptions{PoolSize: 8})
|
|
if err != nil {
|
|
t.Fatalf("NewJSRuntime: %v", err)
|
|
}
|
|
r := NewRunner()
|
|
if err := r.Register("js", rt.Engine()); err != nil {
|
|
t.Fatalf("Register js: %v", err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// TestRunnerJSHappyPath: real goja, real arithmetic, real Export() result.
|
|
func TestRunnerJSHappyPath(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
got, err := r.Run(context.Background(), "js", []byte("1+2"))
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if got != int64(3) {
|
|
t.Fatalf("1+2 = %#v (%T), want int64(3)", got, got)
|
|
}
|
|
}
|
|
|
|
// TestRunnerJSFunctionCall defines a function and returns the result of
|
|
// calling it via a top-level expression — proving a defined function is
|
|
// reachable and invocable in the same eval.
|
|
func TestRunnerJSFunctionCall(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
got, err := r.Run(context.Background(), "js",
|
|
[]byte("function add(a,b){return a+b}; add(40,2)"))
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if got != int64(42) {
|
|
t.Fatalf("add(40,2) = %#v (%T), want int64(42)", got, got)
|
|
}
|
|
|
|
// And capture the function value itself, then confirm it round-trips
|
|
// as a callable (Export of a goja function is a Go func wrapper).
|
|
fnVal, err := r.Run(context.Background(), "js",
|
|
[]byte("function add(a,b){return a+b}; add"))
|
|
if err != nil {
|
|
t.Fatalf("Run capture fn: %v", err)
|
|
}
|
|
if fnVal == nil {
|
|
t.Fatalf("captured function is nil")
|
|
}
|
|
}
|
|
|
|
// TestRunnerJSDeadline: an infinite loop must be interrupted by ctx
|
|
// deadline. Asserts the error is context.DeadlineExceeded and the wall
|
|
// time is bounded (interrupt actually fired, not a hung worker).
|
|
func TestRunnerJSDeadline(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
start := time.Now()
|
|
_, err := r.Run(ctx, "js", []byte("while(true){}"))
|
|
elapsed := time.Since(start)
|
|
|
|
t.Logf("deadline test elapsed: %v", elapsed)
|
|
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("err = %v, want context.DeadlineExceeded", err)
|
|
}
|
|
if elapsed > 200*time.Millisecond {
|
|
t.Fatalf("elapsed %v > 200ms: interrupt did not fire promptly", elapsed)
|
|
}
|
|
}
|
|
|
|
// TestRunnerUnknownLanguage: dispatch to an unregistered language returns
|
|
// the typed sentinel, not a panic.
|
|
func TestRunnerUnknownLanguage(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
_, err := r.Run(context.Background(), "cobol", []byte("DISPLAY 'HI'."))
|
|
if !errors.Is(err, ErrUnknownLanguage) {
|
|
t.Fatalf("err = %v, want ErrUnknownLanguage", err)
|
|
}
|
|
}
|
|
|
|
// TestRunnerConcurrent: 100 goroutines each evaluate a distinct random
|
|
// expression a+b and assert the result. Run under -race this proves the
|
|
// registry RWMutex and the per-call VM isolation are sound.
|
|
func TestRunnerConcurrent(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
|
|
const n = 100
|
|
var wg sync.WaitGroup
|
|
errs := make(chan error, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
a := rand.Intn(10000)
|
|
b := rand.Intn(10000)
|
|
src := fmt.Sprintf("%d+%d", a, b)
|
|
got, err := r.Run(context.Background(), "js", []byte(src))
|
|
if err != nil {
|
|
errs <- fmt.Errorf("%s: %w", src, err)
|
|
return
|
|
}
|
|
if got != int64(a+b) {
|
|
errs <- fmt.Errorf("%s = %#v, want %d", src, got, a+b)
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(errs)
|
|
for err := range errs {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
// TestRunnerLanguages: registry reports registered languages, sorted.
|
|
func TestRunnerLanguages(t *testing.T) {
|
|
r := newJSRunner(t)
|
|
langs := r.Languages()
|
|
if len(langs) != 1 || langs[0] != "js" {
|
|
t.Fatalf("Languages() = %v, want [js]", langs)
|
|
}
|
|
}
|