script

package
v0.0.0-...-8e12698 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package script runs TypeScript/JavaScript numerical design scripts inside the process — no Node.js install required.

Pipeline: TS/JS source → esbuild (transpile only) → goja VM. Host APIs are injected as the global `gnd`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transpile

func Transpile(name, source string) (string, error)

Transpile turns TS (or JS) into plain JS via esbuild. Identity for pure JS.

Types

type HostAPI

type HostAPI interface {
	// Curve evaluates a growth curve. kind: linear|exponential|logarithmic|power|sigmoid|quadratic|piecewise-log
	Curve(kind string, x float64, params map[string]float64) (float64, error)
	// Damage expected damage.
	Damage(in map[string]float64, typ string) (float64, error)
	// SimBattle runs n 1v1 fights, returns map with win_rate, avg_turns, ...
	SimBattle(attacker, defender map[string]any, n int, seed int64) (map[string]any, error)
	// LoadTable reads csv/tsv/json/yaml/xlsx into {headers, rows}.
	LoadTable(path string, sheet string) (map[string]any, error)
	// ReadJSON reads a JSON file into a JS value.
	ReadJSON(path string) (any, error)
	// WriteJSON writes a value as pretty JSON.
	WriteJSON(path string, v any) error
	// Log prints to CLI stdout.
	Log(args ...any)
}

HostAPI is implemented by the CLI to expose numerical tools to scripts.

type Options

type Options struct {
	// TimeoutMS for the whole script (0 = default 30s).
	TimeoutMS int
	// WorkingDir for relative file access inside host APIs.
	WorkingDir string
	// Globals extra key→value injected before run (JSON-serializable).
	Globals map[string]any
	// Host injects native functions onto the `gnd` object.
	Host HostAPI
}

Options tune execution.

type Result

type Result struct {
	// Value is the script's completion value (last expression / explicit return).
	Value any
	// Logs collected from gnd.log
	Logs []string
}

Result of a script run.

func Run

func Run(name, source string, opt Options) (*Result, error)

Run compiles and executes source (TS or JS detected by extension or content).

func RunFile

func RunFile(path string, opt Options) (*Result, error)

RunFile loads and runs a .ts/.js/.mjs file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL