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 ¶
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}]