fmt

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 16 Imported by: ? 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 fmt gives a beastjs.VM some of Go's fmt: printf, println, sprint and sprintf.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(obj beastjs.Value, opts ...Option) error

Register sets obj.fmt, on the object obj, normally the global object, to an object with the functions:

printf(format, ...args)  writes fmt.Sprintf(format, args...), returns [bytes written, error or null]
println(...args)         writes fmt.Sprintln(args...), returns the same
sprint(...args)          returns fmt.Sprint(args...)
sprintf(format, ...args) returns fmt.Sprintf(format, args...)

They are also the global functions __fmt_printf__, __fmt_println__, __fmt_sprint__ and __fmt_sprintf__. The arguments convert to Go as the arguments of a function of RegisterFunc do. An output whose bound is more than the memory limit of the VM allows, or more than 16 MiB in a VM with no limit, ends the call with the out of memory error.

Register takes obj over and frees it.

Example (Printf)
vm, err := beastjs.NewVM()
if err != nil {
	panic(err)
}

defer vm.Close()

g := vm.GlobalObject()

defer g.Free()

if err := Register(g.Dup()); err != nil {
	panic(err)
}

v, err := vm.Eval(`fmt.printf('Math.PI=%v\n', Math.PI)`, beastjs.EvalGlobal)
if err != nil {
	panic(err)
}

fmt.Print(v)
Output:
Math.PI=3.141592653589793
[26,null]
Example (Sprint)
vm, err := beastjs.NewVM()
if err != nil {
	panic(err)
}

defer vm.Close()

g := vm.GlobalObject()

defer g.Free()

if err := Register(g.Dup()); err != nil {
	panic(err)
}

v, err := vm.Eval(`fmt.sprint([2*21, 'foo', {bar: Math.exp(1)}])`, beastjs.EvalGlobal)
if err != nil {
	panic(err)
}

fmt.Print(v)
Output:
[42,"foo",{"bar":2.718281828459045}]

Types

type Option

type Option func(*options)

An Option configures Register.

func Writer

func Writer(w io.Writer) Option

Writer makes printf and println write to w. Without it they write to os.Stdout, as it is at the time of each call.

Jump to

Keyboard shortcuts

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