Limits bound how much CPU and stack a script may consume, so a host can run
untrusted or buggy code without a crash or an unbounded loop.
go run ./examples/limitsExpected output:
normal completed
deep recursion completed
busy loop stopped: execution step limit exceeded
host still in control after all three
Key points:
MaxCallDepthcaps nested calls. Exceeding it raises a catchableRangeError("Maximum call stack size exceeded") — a normal JS exception the script can catch and recover from (so the "deep recursion" case completes). It also keeps recursion below the point where the Go stack would overflow.MaxStepscaps evaluation steps (statements, loop iterations, calls). Exceeding it aborts with an uncatchable*LimitError:try/catchcannot swallow it, and control returns to the host. This guarantees a busy loop likewhile (true) {}terminates even without a context deadline.- Both default sensibly (
MaxCallDepth6000,MaxStepsunlimited). Combine withWithContext(ctx)(a wall-clock deadline) andWithSecurity(...)for a fully locked-down sandbox — see thesandboxexample.