diff --git a/compiler/compiler_module.go b/compiler/compiler_module.go index eda394e..b221636 100644 --- a/compiler/compiler_module.go +++ b/compiler/compiler_module.go @@ -77,6 +77,10 @@ func (c *Compiler) doCompileModule(moduleName string, src []byte) (*objects.Comp } symbolTable := NewSymbolTable() + for idx, fn := range objects.Builtins { + symbolTable.DefineBuiltin(idx, fn.Name) + } + globals := make(map[string]int) moduleCompiler := c.fork(moduleName, symbolTable) diff --git a/docs/interoperability.md b/docs/interoperability.md index 3cf7007..badfa38 100644 --- a/docs/interoperability.md +++ b/docs/interoperability.md @@ -145,6 +145,8 @@ s.DisableBuiltinFunction("print") _, 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) 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 ``` +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) 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 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. diff --git a/runtime/vm_module_test.go b/runtime/vm_module_test.go index e059968..4157ea5 100644 --- a/runtime/vm_module_test.go +++ b/runtime/vm_module_test.go @@ -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{ "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) }()`, + }) }