jsruntime

package
v0.0.0-...-03ad2cf Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package jsruntime is waffle's in-process JavaScript engine: it transpiles a user's JSX/TSX React document and runs it entirely inside the Go process to produce a waffle-tree/v1 JSON document — no Node.js at render time.

The pipeline is pure Go end to end:

esbuild (github.com/evanw/esbuild)  — transpile JSX/TSX + bundle, in memory
goja    (github.com/dop251/goja)    — execute the bundled React program

React itself (react.production.min.js) and the @waffle/react runtime are embedded and resolved by an esbuild plugin from strings, so nothing is read from disk or node_modules. goja is the single JS engine; there is no sidecar.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance is a rendered document on a live goja VM. Unlike Render, it keeps the runtime alive so function render-props (kept as closures in the VM) can be evaluated per page via EvalCallback. It is NOT safe for concurrent use — the underlying goja VM is single-threaded — but layout/pagination is sequential.

func (*Instance) EvalCallback

func (i *Instance) EvalCallback(id string, ctxJSON []byte) (nodes []byte, err error)

EvalCallback evaluates the render-prop callback id with ctxJSON (the page context: pageNumber, totalPages, …) and returns a JSON array of waffle-tree nodes produced by the callback.

func (*Instance) EvalPaint

func (i *Instance) EvalPaint(id string, w, h float64) (ops []byte, err error)

EvalPaint evaluates a Canvas paint callback id against a painter of size w×h and returns the recorded ops as a JSON array ({op,args} objects).

func (*Instance) Tree

func (i *Instance) Tree() []byte

Tree returns the rendered waffle-tree/v1 JSON.

type Options

type Options struct {
	// TypeScript selects the TSX loader for the user source (JSX by default).
	TypeScript bool
	// Filename labels the user source in error messages (default "document.jsx").
	Filename string
}

Options configures how a source is transpiled.

type Program

type Program struct {
	// contains filtered or unexported fields
}

Program is a compiled React document, ready to render with varying props. It holds an esbuild-bundled, goja-compiled program; it is safe to Render many times (each Render runs on a fresh goja VM).

func Compile

func Compile(source []byte, opts Options) (*Program, error)

Compile transpiles and bundles a JSX/TSX React document into an executable program. The source's default export must be a <Document> element or a function returning one (given props).

func (*Program) BundledSource

func (p *Program) BundledSource() string

BundledSource returns the transpiled+bundled JS (for debugging/tests).

func (*Program) Instantiate

func (p *Program) Instantiate(propsJSON []byte) (inst *Instance, err error)

Instantiate runs the program with props and returns a live Instance whose Tree() is the waffle-tree/v1 JSON.

func (*Program) Render

func (p *Program) Render(propsJSON []byte) ([]byte, error)

Render executes the program on a fresh VM with the given props (already JSON-encoded; pass nil for none) and returns the waffle-tree/v1 JSON. It is a one-shot convenience over Instantiate for documents with no render callbacks.

Jump to

Keyboard shortcuts

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