js

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package js defines the JavaScript API TSX shares between moejs and the browser.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCleanup

func AddCleanup[S any](o *Object, cleanup func(S), arg S)

AddCleanup runs cleanup with arg once o is collected.

func Caller

func Caller(r *Runtime) (file string, line int, ok bool)

Caller is the line of JS code r runs now, through the source maps the code carries, as file and line; ok is false while r runs none.

func IsNull

func IsNull(v Value) bool

func IsUndefined

func IsUndefined(v Value) bool

func IsolatedFrameAvailable

func IsolatedFrameAvailable() bool

IsolatedFrameAvailable reports whether this host can create an opaque-origin sandboxed DOM iframe with MessageChannel.

func SetMemoryLimit

func SetMemoryLimit(r *Runtime, limit int64, onLimit func() error)

SetMemoryLimit stops the realm's calls once its JavaScript keeps and allocates more than limit bytes beyond what the realm keeps now: they fail with an *InterruptedError of what onLimit returns. A limit of zero or less removes it.

func TrackCallers

func TrackCallers(*Runtime)

TrackCallers does nothing: the engine captures the current call stack.

Types

type ArrayBuffer

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

ArrayBuffer is a JS ArrayBuffer, or the bytes Go gives one.

func (ArrayBuffer) Bytes

func (a ArrayBuffer) Bytes() []byte

Bytes are the buffer's bytes, not a copy.

type AsyncContextTracker

type AsyncContextTracker interface {
	Grab() any
	Resumed(any)
	Exited()
}

AsyncContextTracker captures and restores context around promise reactions.

type Callable

type Callable func(this Value, args ...Value) (Value, error)

Callable is a JS function as Go calls it.

func AssertFunction

func AssertFunction(v Value) (Callable, bool)

AssertFunction returns v as a function Go calls.

type Constructor

type Constructor func(newTarget *Object, args ...Value) (*Object, error)

Constructor is a JS constructor as Go calls it with new.

func AssertConstructor

func AssertConstructor(v Value) (Constructor, bool)

AssertConstructor returns v as a constructor Go calls with new.

type ConstructorCall

type ConstructorCall struct {
	This      *Object
	Arguments []Value
	NewTarget *Object
}

ConstructorCall is a call of a Go constructor with new.

func (ConstructorCall) Argument

func (f ConstructorCall) Argument(i int) Value

Argument returns argument i, undefined when missing.

type EngineProperties

type EngineProperties struct {
	CanInterrupt         bool // The host can stop a running JS slice.
	ReactionsAfterCommit bool // Promise reactions run on a later page turn.
	HasNativeIntl        bool // Keep the engine's own internationalization APIs.
	SharesPageAuthority  bool // The realm can reach the containing page's APIs.
	TracksReactions      bool // A promise reaction runs under the context of the call that registered it.
}

EngineProperties describes differences the TSX runtime relies on.

func Properties

func Properties() EngineProperties

Properties returns the properties of the engine selected for this build.

type Exception

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

Exception is what JS threw, with its stack read through the scripts' source maps.

func (*Exception) Error

func (e *Exception) Error() string

func (*Exception) String

func (e *Exception) String() string

func (*Exception) Unwrap

func (e *Exception) Unwrap() error

Unwrap is the Go error a host function failed with, which the exception carries.

func (*Exception) Value

func (e *Exception) Value() Value

type FieldNameMapper

type FieldNameMapper interface {
	FieldName(reflect.Type, reflect.StructField) string
	MethodName(reflect.Type, reflect.Method) string
}

FieldNameMapper names fields and methods when a Go value enters JS.

func UncapFieldNameMapper

func UncapFieldNameMapper() FieldNameMapper

UncapFieldNameMapper lowercases the first letter of Go fields and methods.

type Flag

type Flag int

Flag is a property attribute's setting.

const (
	FLAG_NOT_SET Flag = iota
	FLAG_FALSE
	FLAG_TRUE
)

type FunctionCall

type FunctionCall struct {
	This      Value
	Arguments []Value
}

FunctionCall is a call of a Go function from JS.

func (FunctionCall) Argument

func (f FunctionCall) Argument(i int) Value

Argument returns argument i, undefined when missing.

type InterruptedError

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

InterruptedError stops a call the host interrupted.

func (*InterruptedError) CallError

func (*InterruptedError) CallError() error

CallError is nil: moejs drops the jobs an interrupt cut, the calls' among them.

func (*InterruptedError) Error

func (e *InterruptedError) Error() string

func (*InterruptedError) String

func (e *InterruptedError) String() string

func (*InterruptedError) Unwrap

func (e *InterruptedError) Unwrap() error

func (*InterruptedError) Value

func (e *InterruptedError) Value() any

Value is what the host interrupted the call with.

type IsolatedChannel

type IsolatedChannel struct{}

IsolatedChannel is a private MessagePort channel to an opaque-origin sandboxed iframe on the web.

func OpenIsolatedFrame

func OpenIsolatedFrame(srcdoc, initToken string, onMessage func(map[string]any)) *IsolatedChannel

OpenIsolatedFrame is only available on the web (GOOS=js).

func (*IsolatedChannel) Close

func (*IsolatedChannel) Close()

func (*IsolatedChannel) Post

func (*IsolatedChannel) Post(map[string]any)

func (*IsolatedChannel) Ready

func (*IsolatedChannel) Ready() <-chan struct{}

type Object

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

Object is a JS object or function of a realm.

func (*Object) ClassName

func (o *Object) ClassName() string

ClassName is the object's class: an array's, a function's and an error's by their kind, others by their constructor's.

func (*Object) DefineAccessorProperty

func (o *Object) DefineAccessorProperty(name string, getter, setter Value, configurable, enumerable Flag) error

func (*Object) DefineDataProperty

func (o *Object) DefineDataProperty(name string, value Value, writable, configurable, enumerable Flag) error

func (*Object) DefineDataPropertySymbol

func (o *Object) DefineDataPropertySymbol(s *Symbol, value Value, writable, configurable, enumerable Flag) error

func (*Object) Export

func (o *Object) Export() any

Export gives the object as Go reads it: a Go value as it is, an array as []any, a function as func(FunctionCall) Value, a promise as *Promise, a date as time.Time, and other objects as map[string]any.

func (*Object) ExportType

func (o *Object) ExportType() reflect.Type

func (*Object) Get

func (o *Object) Get(name string) Value

Get returns the property name, nil when neither the object nor its prototypes have it.

func (*Object) GetSymbol

func (o *Object) GetSymbol(s *Symbol) Value

func (*Object) Keys

func (o *Object) Keys() []string

Keys are the object's own enumerable string keys.

func (*Object) MarshalJSON

func (o *Object) MarshalJSON() ([]byte, error)

MarshalJSON writes the object as JSON.stringify does.

func (*Object) Prototype

func (o *Object) Prototype() *Object

func (*Object) Set

func (o *Object) Set(name string, value any) error

func (*Object) SetPrototype

func (o *Object) SetPrototype(proto *Object) error

func (*Object) SetSymbol

func (o *Object) SetSymbol(s *Symbol, value any) error

func (*Object) StrictEquals

func (o *Object) StrictEquals(v Value) bool

func (*Object) String

func (o *Object) String() string

func (*Object) ToBoolean

func (o *Object) ToBoolean() bool

func (*Object) ToFloat

func (o *Object) ToFloat() float64

func (*Object) ToInteger

func (o *Object) ToInteger() int64

func (*Object) ToNumber

func (o *Object) ToNumber() Value

func (*Object) ToString

func (o *Object) ToString() Value

type Program

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

Program is a script compiled for RunProgram.

func MustCompile

func MustCompile(name, src string, strict bool) *Program

MustCompile compiles src, as strict mode code when strict is set.

type Promise

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

Promise is a JS promise.

func (*Promise) Result

func (p *Promise) Result() Value

func (*Promise) State

func (p *Promise) State() PromiseState

type PromiseState

type PromiseState int

PromiseState is a promise's state.

const (
	PromiseStatePending PromiseState = iota
	PromiseStateFulfilled
	PromiseStateRejected
)

type Runtime

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

Runtime is a JavaScript realm backed by moejs.

func New

func New() *Runtime

func (*Runtime) ClearInterrupt

func (r *Runtime) ClearInterrupt()

func (*Runtime) Close

func (r *Runtime) Close()

Close lets go of what the realm keeps of finished calls.

func (*Runtime) DrainPromiseJobs

func (r *Runtime) DrainPromiseJobs() error

DrainPromiseJobs has nothing to run: the outermost call runs the jobs.

func (*Runtime) ExportTo

func (r *Runtime) ExportTo(v Value, target any) error

func (*Runtime) Get

func (r *Runtime) Get(name string) Value

func (*Runtime) GlobalObject

func (r *Runtime) GlobalObject() *Object

func (*Runtime) Interrupt

func (r *Runtime) Interrupt(value any)

func (*Runtime) New

func (r *Runtime) New(constructor Value, args ...Value) (*Object, error)

func (*Runtime) NewArray

func (r *Runtime) NewArray(items ...any) *Object

func (*Runtime) NewArrayBuffer

func (r *Runtime) NewArrayBuffer(data []byte) ArrayBuffer

func (*Runtime) NewGoError

func (r *Runtime) NewGoError(err error) *Object

NewGoError makes an Error of err, which an exception carrying it unwraps to.

func (*Runtime) NewObject

func (r *Runtime) NewObject() *Object

func (*Runtime) NewPromise

func (r *Runtime) NewPromise() (*Promise, func(any) error, func(any) error)

func (*Runtime) NewTypeError

func (r *Runtime) NewTypeError(format string, args ...any) *Object

func (*Runtime) RunProgram

func (r *Runtime) RunProgram(p *Program) (Value, error)

func (*Runtime) RunScript

func (r *Runtime) RunScript(name, source string) (Value, error)

func (*Runtime) RunString

func (r *Runtime) RunString(source string) (Value, error)

func (*Runtime) Set

func (r *Runtime) Set(name string, value any) error

func (*Runtime) SetAsyncContextTracker

func (r *Runtime) SetAsyncContextTracker(tracker AsyncContextTracker)

func (*Runtime) SetFieldNameMapper

func (r *Runtime) SetFieldNameMapper(mapper FieldNameMapper)

func (*Runtime) SetPromiseJobFilter

func (r *Runtime) SetPromiseJobFilter(func(any) bool)

SetPromiseJobFilter does nothing: moejs drops the queued jobs when it interrupts a call.

func (*Runtime) SetTimeSource

func (r *Runtime) SetTimeSource(now func() time.Time)

func (*Runtime) ToObject

func (r *Runtime) ToObject(v Value) *Object

ToObject boxes v using the realm's prototypes and returns objects unchanged.

func (*Runtime) ToValue

func (r *Runtime) ToValue(i any) Value

ToValue makes a JS value of a Go one: primitives as themselves, functions as JS functions converting their arguments and results, slices and maps as arrays and objects of their items, and other values as objects whose prototype holds their fields and methods, exported back as the value.

type StackOverflowError

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

StackOverflowError is kept for the shared API: moejs throws a RangeError past its call depth, which JS can catch.

func (*StackOverflowError) Error

func (e *StackOverflowError) Error() string

func (*StackOverflowError) String

func (e *StackOverflowError) String() string

func (*StackOverflowError) Unwrap

func (*StackOverflowError) Unwrap() error

type Symbol

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

Symbol is a JavaScript symbol, which any realm may use.

func NewSymbol

func NewSymbol(s string) *Symbol

func (*Symbol) Export

func (s *Symbol) Export() any

func (*Symbol) ExportType

func (s *Symbol) ExportType() reflect.Type

func (*Symbol) StrictEquals

func (s *Symbol) StrictEquals(v Value) bool

func (*Symbol) String

func (s *Symbol) String() string

func (*Symbol) ToBoolean

func (s *Symbol) ToBoolean() bool

func (*Symbol) ToFloat

func (s *Symbol) ToFloat() float64

func (*Symbol) ToInteger

func (s *Symbol) ToInteger() int64

func (*Symbol) ToNumber

func (s *Symbol) ToNumber() Value

func (*Symbol) ToString

func (s *Symbol) ToString() Value

type Value

type Value interface {
	Export() any
	ExportType() reflect.Type
	ToInteger() int64
	ToFloat() float64
	ToNumber() Value
	ToString() Value
	ToBoolean() bool
	String() string
	StrictEquals(Value) bool
}

Value is a JavaScript primitive or object. A nil Value denotes a missing property, distinct from the JavaScript values null and undefined.

func Null

func Null() Value

func Undefined

func Undefined() Value

type WeakObject

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

WeakObject holds an object without keeping it alive.

func MakeWeak

func MakeWeak(o *Object) WeakObject

MakeWeak holds o weakly.

func (WeakObject) Value

func (w WeakObject) Value() *Object

Value is the object, nil once it is collected.

Jump to

Keyboard shortcuts

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