mozjs

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 7 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 mozjs 经 cgo 嵌入 SpiderMonkey,提供线程绑定的上下文、根表句柄、求值、Go 原生函数、微任务与中断

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CPU added in v0.3.11

func CPU() float64

CPU 返回当前线程已用的 CPU 毫秒数

func Clock added in v0.3.10

func Clock() float64

Clock 返回当前线程 VM 时钟的毫秒数

func Gzip added in v0.3.10

func Gzip(data []byte) []byte

Gzip 返回 Windows 版 Firefox CompressionStream("gzip") 对 data 输出的字节:以 Firefox 所用的 zlib-rs 按其默认级别与调用顺序压缩,gzip 头的操作系统字节取 Windows 版写入的 10

func Init

func Init() error

Init 初始化进程内的 SpiderMonkey,只执行一次

func Now

func Now() float64

Now 返回单调时钟的毫秒数,精度为微秒

func SetCamouLocale

func SetCamouLocale(locale string)

SetCamouLocale 设置当前线程 mozilla::intl::Locale 的默认区域为 Camoufox 的 navigator.language

func SetClock added in v0.3.10

func SetClock(base, pace, limit float64)

SetClock 在任务开始时把当前线程 VM 时钟设为 base 毫秒,此后按当前线程 CPU 时间的 pace 倍走,走过 limit 毫秒后停住,只在持有 JSContext 的线程调用

Types

type Call

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

Call 是一次原生函数调用

func (*Call) Arg

func (call *Call) Arg(index int) Value

Arg 返回第 index 个参数,对象等值的句柄由调用方释放

func (*Call) ArgBytes

func (call *Call) ArgBytes(index int) ([]byte, bool)

ArgBytes 返回第 index 个参数(ArrayBuffer 或视图)的字节副本

func (*Call) ArgNumber

func (call *Call) ArgNumber(index int) float64

ArgNumber 返回第 index 个参数的数值,不是数值时为 0

func (*Call) ArgString

func (call *Call) ArgString(index int) string

ArgString 返回第 index 个参数的字符串,不是字符串时为空串

func (*Call) Return

func (call *Call) Return(value Value)

Return 设置返回值

func (*Call) ReturnBytes

func (call *Call) ReturnBytes(data []byte)

ReturnBytes 以字节新建 ArrayBuffer 作为返回值

type Context

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

Context 是绑定到创建它的 OS 线程的 JSContext,除 Interrupt 外只能在该线程上使用

func NewContext

func NewContext(options Options) (*Context, error)

NewContext 在当前 OS 线程创建上下文,调用方需已锁定线程

func (*Context) Call

func (cx *Context) Call(fn Handle, this Handle, args ...Value) (Value, error)

Call 以 this 调用函数 fn,this 为 0 时为 undefined

func (*Context) ClearKeptObjects

func (cx *Context) ClearKeptObjects()

ClearKeptObjects 结束当前任务的 WeakRef 存活期

func (*Context) Close

func (cx *Context) Close()

Close 销毁上下文

func (*Context) Evaluate

func (cx *Context) Evaluate(global Handle, source, filename string, line int) (Value, error)

Evaluate 在 global 的 Realm 中执行脚本

func (*Context) FlushRejections

func (cx *Context) FlushRejections(fn Handle) error

FlushRejections 按发生顺序以 (promise, handled, global) 调用 fn 并清空 Promise 拒绝记录

func (*Context) GC

func (cx *Context) GC()

GC 执行一次完整 GC,保留 JIT 代码

func (*Context) Get

func (cx *Context) Get(object Handle, name string) (Value, error)

Get 读取对象属性

func (*Context) InstallHostNatives

func (cx *Context) InstallHostNatives(target Handle) error

InstallHostNatives 在 target 上定义 now、kindOf、throwLocation 与 incumbent

func (*Context) InstallWindowNatives

func (cx *Context) InstallWindowNatives(global Handle) error

InstallWindowNatives 在全局对象上定义 queueMicrotask、atob 与 btoa

func (*Context) Interrupt

func (cx *Context) Interrupt()

Interrupt 请求终止正在执行的脚本,可在任意 goroutine 调用,上下文已销毁时不做任何事

func (*Context) NewFunction

func (cx *Context) NewFunction(global Handle, name string, nargs int, fn NativeFunc) (Handle, error)

NewFunction 在 global 的 Realm 中新建调用 fn 的函数

func (*Context) NewGlobal

func (cx *Context) NewGlobal(same Handle, timezone string) (Handle, error)

NewGlobal 新建 Window 全局对象,same 非 0 时与其共用 compartment,timezone 为该 Realm 的时区

func (*Context) NewObject

func (cx *Context) NewObject(global Handle) (Handle, error)

NewObject 在 global 的 Realm 中新建普通对象

func (*Context) ParseJSON

func (cx *Context) ParseJSON(global Handle, text string) (Value, error)

ParseJSON 在 global 的 Realm 中解析 JSON

func (*Context) Release

func (cx *Context) Release(handle Handle)

Release 释放句柄

func (*Context) ReleaseValue

func (cx *Context) ReleaseValue(value Value)

ReleaseValue 释放值持有的句柄

func (*Context) RunJobs

func (cx *Context) RunJobs()

RunJobs 执行微任务检查点

func (*Context) Set

func (cx *Context) Set(object Handle, name string, value Value) error

Set 写入对象属性

func (*Context) SetMicrotaskReporter

func (cx *Context) SetMicrotaskReporter(fn Handle)

SetMicrotaskReporter 设置以 (global, exception) 报告微任务回调异常的函数

func (*Context) SetThrowLocation

func (cx *Context) SetThrowLocation(filename string, line, column int)

SetThrowLocation 设置宿主 throwLocation 返回的抛出位置

func (*Context) SetTrustedTypes

func (cx *Context) SetTrustedTypes(registry Handle, block bool)

SetTrustedTypes 设置 TrustedScript 源码登记表与是否拒绝以字符串编译代码

func (*Context) ShrinkGC added in v0.3.0

func (cx *Context) ShrinkGC()

ShrinkGC 执行一次收缩的完整 GC,归还空闲内存并丢弃 JIT 代码

type Error

type Error struct {
	Message  string
	Stack    string
	Filename string
	Line     int
	Column   int
	Value    Handle
}

Error 是 JS 抛出的异常,Value 为异常值的句柄

func (*Error) Error

func (err *Error) Error() string

type Handle

type Handle uint32

Handle 是上下文根表中一个 JS 值的句柄,0 表示 undefined

type Kind

type Kind int32

Kind 是 Value 的种类

const (
	KindUndefined Kind = C.MZ_UNDEFINED
	KindNull      Kind = C.MZ_NULL
	KindBool      Kind = C.MZ_BOOL
	KindNumber    Kind = C.MZ_NUMBER
	KindString    Kind = C.MZ_STRING
	KindHandle    Kind = C.MZ_HANDLE
)

Kind 的取值与 mozjs.h 的 MZ_* 一致

type NativeFunc

type NativeFunc func(call *Call) error

NativeFunc 是由 JS 调用的 Go 函数,返回错误时在 JS 中抛出

type Options

type Options struct {
	HeapBytes uint32
	Locale    string
}

Options 是新建上下文的参数

type ThrowError

type ThrowError struct {
	Type    string
	Message string
}

ThrowError 让原生函数以当前全局对象中名为 Type 的错误构造器抛出 Message

func (*ThrowError) Error

func (err *ThrowError) Error() string

type Value

type Value struct {
	Kind   Kind
	Bool   bool
	Number float64
	String string
	Handle Handle
}

Value 是跨边界的 JS 值:原始值按种类保存,其余值为句柄

func Bool

func Bool(value bool) Value

Bool 返回布尔值

func Null

func Null() Value

Null 返回 null

func Number

func Number(value float64) Value

Number 返回数值

func Object

func Object(handle Handle) Value

Object 返回句柄引用的值

func String

func String(value string) Value

String 返回字符串

func Undefined

func Undefined() Value

Undefined 返回 undefined

Jump to

Keyboard shortcuts

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