generator

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package generator creates TypeScript client code for Go WASM functions.

Two modes are supported:

  • Sync mode: class that calls globalThis functions directly
  • Worker mode: class that communicates via Web Worker

Go functions returning (T, error) automatically throw in TypeScript using the __error field convention defined by ErrorFieldName.

Index

Constants

View Source
const ErrorFieldName = "__error"

ErrorFieldName is the JSON field used to pass Go errors through the WASM boundary. When a Go function returns (T, error), the generated binding returns map[string]interface{}{ErrorFieldName: err.Error()} on error. The TypeScript client checks for this field and throws it as a JavaScript Error.

Variables

This section is empty.

Functions

func Generate

func Generate(parsed *parser.ParsedFile) string

Generate creates TypeScript class-based client for sync mode. This generates a class that wraps globalThis function calls.

func GenerateClass

func GenerateClass(functions []parser.GoFunction, className string) string

GenerateClass creates the TypeScript class with sync methods.

func GenerateClassMethod

func GenerateClassMethod(fn parser.GoFunction) string

GenerateClassMethod creates a single instance method that calls globalThis.

func GenerateClient

func GenerateClient(parsed *parser.ParsedFile) string

GenerateClient creates client.ts with a class-based API for worker mode.

func GenerateFunctionParams

func GenerateFunctionParams(params []parser.GoParameter) string

GenerateFunctionParams formats the parameter list as TypeScript.

func GenerateGoBindings

func GenerateGoBindings(parsed *parser.ParsedFile, workerMode bool) string

GenerateGoBindings generates Go wrapper code for WASM export. workerMode determines whether callbacks use postMessage-based invocation (true) or direct JS function invocation (false).

func GenerateHeader

func GenerateHeader(packageName string) string

GenerateHeader creates the file header comment.

func GenerateInterfaceForFunction

func GenerateInterfaceForFunction(fn parser.GoFunction, types map[string]*parser.GoType) string

GenerateInterfaceForFunction creates an exported interface if the function returns a struct. Returns empty string if the function doesn't return a struct type.

func GenerateWorker

func GenerateWorker(wasmPath string) string

GenerateWorker creates worker.js content that runs Go WASM in a Web Worker. The wasmPath parameter specifies the path to the WASM file (e.g., "module.wasm").

func GenerateWorkerClassMethod

func GenerateWorkerClassMethod(fn parser.GoFunction) string

GenerateWorkerClassMethod creates a single async instance method for worker mode.

func InterfaceName

func InterfaceName(funcName string) string

InterfaceName converts a function name to a result interface name. e.g., "formatUser" -> "FormatUserResult", "getInfo" -> "GetInfoResult"

Types

This section is empty.

Jump to

Keyboard shortcuts

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