File: compile.go

package info (click to toggle)
golang-github-evilsocket-islazy 1.10.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 296 kB
  • sloc: sh: 20; javascript: 8; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 970 bytes parent folder | download | duplicates (3)
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
}