Fix a bug where user modules couldn't resolve builtin functions

This commit is contained in:
Daniel Kang 2019-02-01 18:13:29 -08:00
parent c57a7f8f98
commit 9c21c8a804
3 changed files with 15 additions and 0 deletions

View file

@ -77,6 +77,10 @@ func (c *Compiler) doCompileModule(moduleName string, src []byte) (*objects.Comp
} }
symbolTable := NewSymbolTable() symbolTable := NewSymbolTable()
for idx, fn := range objects.Builtins {
symbolTable.DefineBuiltin(idx, fn.Name)
}
globals := make(map[string]int) globals := make(map[string]int)
moduleCompiler := c.fork(moduleName, symbolTable) moduleCompiler := c.fork(moduleName, symbolTable)

View file

@ -145,6 +145,8 @@ s.DisableBuiltinFunction("print")
_, err := s.Run() // compile error _, err := s.Run() // compile error
``` ```
Note that when a script is being added to another script as a module (via `Script.AddModule`), it does not inherit the disabled builtin function list from the main script.
#### Script.DisableStdModule(name string) #### Script.DisableStdModule(name string)
DisableStdModule disables a [standard library](https://github.com/d5/tengo/blob/master/docs/stdlib.md) module. Compile will report a compile-time error if the code tries to import the module with the given name. DisableStdModule disables a [standard library](https://github.com/d5/tengo/blob/master/docs/stdlib.md) module. Compile will report a compile-time error if the code tries to import the module with the given name.
@ -157,6 +159,8 @@ s.DisableStdModule("exec")
_, err := s.Run() // compile error _, err := s.Run() // compile error
``` ```
Note that when a script is being added to another script as a module (via `Script.AddModule`), it does not inherit the disabled standard module list from the main script.
#### Script.SetUserModuleLoader(loader compiler.ModuleLoader) #### Script.SetUserModuleLoader(loader compiler.ModuleLoader)
SetUserModuleLoader replaces the default user-module loader of the compiler, which tries to read the source from a local file. SetUserModuleLoader replaces the default user-module loader of the compiler, which tries to read the source from a local file.
@ -173,6 +177,8 @@ s.SetUserModuleLoader(func(moduleName string) ([]byte, error) {
}) })
``` ```
Note that when a script is being added to another script as a module (via `Script.AddModule`), it does not inherit the module loader from the main script.
## Compiler and VM ## Compiler and VM
Although it's not recommended, you can directly create and run the Tengo [Parser](https://godoc.org/github.com/d5/tengo/compiler/parser#Parser), [Compiler](https://godoc.org/github.com/d5/tengo/compiler#Compiler), and [VM](https://godoc.org/github.com/d5/tengo/runtime#VM) for yourself instead of using Scripts and Script Variables. It's a bit more involved as you have to manage the symbol tables and global variables between them, but, basically that's what Script and Script Variable is doing internally. Although it's not recommended, you can directly create and run the Tengo [Parser](https://godoc.org/github.com/d5/tengo/compiler/parser#Parser), [Compiler](https://godoc.org/github.com/d5/tengo/compiler#Compiler), and [VM](https://godoc.org/github.com/d5/tengo/runtime#VM) for yourself instead of using Scripts and Script Variables. It's a bit more involved as you have to manage the symbol tables and global variables between them, but, basically that's what Script and Script Variable is doing internally.

View file

@ -126,4 +126,9 @@ if !is_error(cmd) {
expectWithUserModules(t, `m1 := import("mod1"); m1.a.b = 5; out = m1.a.b`, 5, map[string]string{ expectWithUserModules(t, `m1 := import("mod1"); m1.a.b = 5; out = m1.a.b`, 5, map[string]string{
"mod1": `a := {b: 3}`, "mod1": `a := {b: 3}`,
}) })
// make sure module has same builtin functions
expectWithUserModules(t, `out = import("mod1").a`, "int", map[string]string{
"mod1": `a := func() { return type_name(0) }()`,
})
} }