core

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package core provides internal utilities for the TypeGo bridge layer.

This package contains low-level primitives used by the bridge to convert data between Go and JavaScript. It is not intended for direct use by TypeGo applications.

Index

Constants

This section is empty.

Variables

View Source
var GlobalTypes []byte

Functions

func BindStruct

func BindStruct(vm *goja.Runtime, name string, s interface{}) error

BindStruct exposes a Go struct to JavaScript with full field and method access. Supports nested structs (converted recursively) and callback arguments.

func InitAll

func InitAll(vm *goja.Runtime, el *eventloop.EventLoop)

InitAll initializes all registered modules. Called once during engine startup.

func MapSharedBuffer

func MapSharedBuffer(vm *goja.Runtime, name string, data []byte)

MapSharedBuffer exposes a Go byte slice as a global JavaScript TypedArray. The backing memory is shared between Go and JavaScript, meaning modifications from either side are immediately visible to the other.

The buffer is registered as a global variable with the given name, accessible as a Uint8Array in JavaScript. This is commonly used for inter-worker communication and zero-copy data sharing.

Example:

data := make([]byte, 1024)
core.MapSharedBuffer(vm, "sharedBuffer", data)
// In JS: sharedBuffer[0] = 42
// In Go: data[0] == 42

func RegisterConsole

func RegisterConsole(vm *goja.Runtime)

RegisterConsole injects the console object into the runtime

func RegisterGlobals

func RegisterGlobals(vm *goja.Runtime)

RegisterGlobals injects global helper functions into the runtime

func RegisterModule

func RegisterModule(m Module)

RegisterModule adds a module to the global registry. Modules typically call this in their init() function.

func ToArrayBuffer

func ToArrayBuffer(vm *goja.Runtime, data []byte) goja.Value

ToArrayBuffer converts a Go byte slice to a JavaScript ArrayBuffer. The returned ArrayBuffer is a copy of the original data, so modifications in JavaScript will not affect the Go slice.

For shared memory scenarios where modifications should be visible to both Go and JavaScript, use MapSharedBuffer instead.

Types

type Binding

type Binding struct {
	Name   string
	Target interface{}
}

Binding represents a Go struct that has been bound to the JavaScript runtime.

type Console

type Console struct{}

Console implements the JS console object

func (*Console) Error

func (c *Console) Error(call goja.FunctionCall) goja.Value

Error prints arguments to stderr

func (*Console) Log

func (c *Console) Log(call goja.FunctionCall) goja.Value

Log prints arguments to stdout

type Module

type Module interface {
	// Name returns the module's import path (e.g., "go:fmt", "typego:memory")
	Name() string
	// Register injects the module's bindings into the JavaScript runtime
	Register(vm *goja.Runtime, el *eventloop.EventLoop)
}

Module represents a registerable TypeGo module.

func GetModules

func GetModules() []Module

GetModules returns all registered modules (for debugging/introspection).

Jump to

Keyboard shortcuts

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