forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
52 lines (42 loc) · 980 Bytes
/
module.go
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
45
46
47
48
49
50
51
52
package process
import (
"os"
"strings"
"github.com/kmcsr/goja"
"github.com/kmcsr/goja/extern/require"
"github.com/kmcsr/goja/extern/events"
)
type Process struct {
*events.EventEmitter
inerr bool
env map[string]string
}
func Require(runtime *goja.Runtime, module *goja.Object){
p := &Process{
EventEmitter: events.NewEventEmitter(runtime),
env: make(map[string]string),
}
for _, e := range os.Environ() {
i := strings.IndexByte(e, '=')
p.env[e[:i]] = e[i+1:]
}
runtime.Onerror = func(err error){
if p.inerr {
panic("Error in error handle")
}
p.inerr = true
p.Emit("error", runtime.ToValue(err))
p.inerr = false
}
o := module.Get("exports").(*goja.Object)
emitter := p.EventEmitter.ToObject()
o.SetPrototype(emitter)
o.Set("events", emitter)
o.Set("env", p.env)
}
func Enable(runtime *goja.Runtime) {
runtime.Set("process", require.Require(runtime, "process"))
}
func init() {
require.RegisterNativeModule("process", Require)
}