-
Notifications
You must be signed in to change notification settings - Fork 0
/
goatsejs_require.go
38 lines (36 loc) · 1.07 KB
/
goatsejs_require.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
package main
import (
"github.com/robertkrimen/otto"
"io/ioutil"
)
// Like the nodeJS require. Scope is not clean. Returns module.exports.
// Module and module.exports are objects.
// :SIGJS: (filename string) -> (returnvalue Dynamic||Null)
func goatsejs_requirejs(call otto.FunctionCall) otto.Value {
var (
file_name string
file_contents []byte
source string
script *otto.Script
retval otto.Value
err error
)
file_name, err = call.Argument(0).ToString()
if err != nil {
return returnErrCapsule(call.Otto, err)
}
file_contents, err = ioutil.ReadFile(file_name)
if err != nil {
return returnErrCapsule(call.Otto, err)
}
source = `var module={}; module.exports={};new function(){` + string(file_contents) + `;return module.exports}()`
script, err = call.Otto.Compile("", source)
if err != nil {
return returnErrCapsule(call.Otto, err)
}
retval, err = call.Otto.Run(script)
if err != nil {
return returnErrCapsule(call.Otto, err)
}
return returnCapsule(call.Otto, retval, otto.Value{})
}