Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining custom builtins #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func runVM(

start := time.Now()

v := tengo.NewVM(bytecode, globals, -1)
v := tengo.NewVM(bytecode, nil, globals, -1)
if err := v.Run(); err != nil {
return time.Since(start), nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/tengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func CompileAndRun(
return
}

machine := tengo.NewVM(bytecode, nil, -1)
machine := tengo.NewVM(bytecode, nil, nil, -1)
err = machine.Run()
return
}
Expand All @@ -153,7 +153,7 @@ func RunCompiled(modules *tengo.ModuleMap, data []byte) (err error) {
return
}

machine := tengo.NewVM(bytecode, nil, -1)
machine := tengo.NewVM(bytecode, nil, nil, -1)
err = machine.Run()
return
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) {
}

bytecode := c.Bytecode()
machine := tengo.NewVM(bytecode, globals, -1)
machine := tengo.NewVM(bytecode, nil, globals, -1)
if err := machine.Run(); err != nil {
_, _ = fmt.Fprintln(out, err.Error())
continue
Expand Down
4 changes: 2 additions & 2 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (c *Compiled) Run() error {
c.lock.Lock()
defer c.lock.Unlock()

v := NewVM(c.bytecode, c.globals, c.maxAllocs)
v := NewVM(c.bytecode, nil, c.globals, c.maxAllocs)
return v.Run()
}

Expand All @@ -216,7 +216,7 @@ func (c *Compiled) RunContext(ctx context.Context) (err error) {
c.lock.Lock()
defer c.lock.Unlock()

v := NewVM(c.bytecode, c.globals, c.maxAllocs)
v := NewVM(c.bytecode, nil, c.globals, c.maxAllocs)
ch := make(chan error, 1)
go func() {
defer func() {
Expand Down
8 changes: 7 additions & 1 deletion vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type VM struct {
constants []Object
stack [StackSize]Object
sp int
builtins []*BuiltinFunction
globals []Object
fileSet *parser.SourceFileSet
frames [MaxFrames]frame
Expand All @@ -37,15 +38,20 @@ type VM struct {
// NewVM creates a VM.
func NewVM(
bytecode *Bytecode,
builtins []*BuiltinFunction,
globals []Object,
maxAllocs int64,
) *VM {
if builtins == nil {
builtins = GetAllBuiltinFunctions()
}
if globals == nil {
globals = make([]Object, GlobalsSize)
}
v := &VM{
constants: bytecode.Constants,
sp: 0,
builtins: builtins,
globals: globals,
fileSet: bytecode.FileSet,
framesIndex: 1,
Expand Down Expand Up @@ -739,7 +745,7 @@ func (v *VM) run() {
case parser.OpGetBuiltin:
v.ip++
builtinIndex := int(v.curInsts[v.ip])
v.stack[v.sp] = builtinFuncs[builtinIndex]
v.stack[v.sp] = v.builtins[builtinIndex]
v.sp++
case parser.OpClosure:
v.ip += 3
Expand Down
2 changes: 1 addition & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3852,7 +3852,7 @@ func traceCompileRun(
trace = append(trace, fmt.Sprintf("\n[Compiled Instructions]\n\n%s\n",
strings.Join(bytecode.FormatInstructions(), "\n")))

v = tengo.NewVM(bytecode, globals, maxAllocs)
v = tengo.NewVM(bytecode, nil, globals, maxAllocs)

err = v.Run()
{
Expand Down