update
This commit is contained in:
parent
72bdcfd535
commit
12006ca82c
@ -20,16 +20,32 @@ func createModule(c *Core) *js.Object {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadModule(c *Core, p string) *js.Program {
|
func compileModule(p string) *js.Program {
|
||||||
|
code, _ := ioutil.ReadFile(p)
|
||||||
|
text := moduleTemplate(string(code))
|
||||||
|
prg, _ := js.Compile(p, text, false)
|
||||||
|
|
||||||
|
return prg
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadModule(c *Core, p string) js.Value {
|
||||||
p = filepath.Clean(p)
|
p = filepath.Clean(p)
|
||||||
pkg := c.Pkg[p]
|
pkg := c.Pkg[p]
|
||||||
if pkg == nil {
|
if pkg != nil {
|
||||||
code, _ := ioutil.ReadFile(p)
|
return pkg
|
||||||
text := moduleTemplate(string(code))
|
|
||||||
pkg, _ = js.Compile(p, text, false)
|
|
||||||
c.Pkg[p] = pkg
|
|
||||||
}
|
}
|
||||||
return pkg
|
|
||||||
|
prg := compileModule(p)
|
||||||
|
|
||||||
|
r := c.GetRts()
|
||||||
|
f, _ := r.RunProgram(prg)
|
||||||
|
g, _ := js.AssertFunction(f)
|
||||||
|
|
||||||
|
m := createModule(c)
|
||||||
|
jsExports := m.Get("exports")
|
||||||
|
g(jsExports, m, jsExports)
|
||||||
|
|
||||||
|
return m.Get("exports")
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterLoader register a simple commonjs style loader to runtime
|
// RegisterLoader register a simple commonjs style loader to runtime
|
||||||
@ -38,15 +54,6 @@ func RegisterLoader(c *Core) {
|
|||||||
|
|
||||||
r.Set("require", func(call js.FunctionCall) js.Value {
|
r.Set("require", func(call js.FunctionCall) js.Value {
|
||||||
p := call.Argument(0).String()
|
p := call.Argument(0).String()
|
||||||
prg := loadModule(c, p)
|
return loadModule(c, p)
|
||||||
|
|
||||||
f, _ := r.RunProgram(prg)
|
|
||||||
g, _ := js.AssertFunction(f)
|
|
||||||
|
|
||||||
m := createModule(c)
|
|
||||||
jsExports := m.Get("exports")
|
|
||||||
g(jsExports, m, jsExports)
|
|
||||||
|
|
||||||
return m.Get("exports")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,13 @@ import (
|
|||||||
// Core is the basic struct of gode
|
// Core is the basic struct of gode
|
||||||
type Core struct {
|
type Core struct {
|
||||||
Rts *js.Runtime
|
Rts *js.Runtime
|
||||||
Pkg map[string]*js.Program
|
Pkg map[string]js.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// New create a *Core
|
// New create a *Core
|
||||||
func New() *Core {
|
func New() *Core {
|
||||||
vm := js.New()
|
vm := js.New()
|
||||||
pkg := make(map[string]*js.Program)
|
pkg := make(map[string]js.Value)
|
||||||
|
|
||||||
return &Core{
|
return &Core{
|
||||||
Rts: vm,
|
Rts: vm,
|
||||||
|
Loading…
Reference in New Issue
Block a user