1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
package plugin
import (
// "unicode"
"github.com/robertkrimen/otto"
)
func (p *Plugin) compile() (err error) {
// create a new vm
p.vm = otto.New()
// track objects already defined by Otto
predefined := map[string]bool{}
for name := range p.vm.Context().Symbols {
predefined[name] = true
}
// define symbols
for name, val := range Defines {
if err := p.vm.Set(name, val); err != nil {
return err
}
}
// run the code once in order to define all the functions
// and validate the syntax, then get the callbacks
if _, err = p.vm.Run(p.Code); err != nil {
return
}
// every uppercase object is considered exported
for name, sym := range p.vm.Context().Symbols {
// ignore predefined objects
if _, found := predefined[name]; !found {
// ignore lowercase global objects
// if unicode.IsUpper(rune(name[0])) {
if sym.IsFunction() {
p.callbacks[name] = sym
} else {
p.objects[name] = sym
}
// }
}
}
return nil
}
|