diff --git a/gosh.go b/gosh.go index 98e317a..12e1caf 100644 --- a/gosh.go +++ b/gosh.go @@ -3,8 +3,8 @@ package gosh import ( "net/http" + "git.kingecg.top/kingecg/gosh/interceptor" "github.com/mattn/anko/env" - "github.com/mattn/anko/vm" ) const ( @@ -26,15 +26,17 @@ type SHMux struct { } func NewSHMux() *SHMux { - return &SHMux{ + s := &SHMux{ ServeMux: http.NewServeMux(), ankoEnv: env.NewEnv(), } + //s.executor = interceptor.NewAnkointerceptor(s.ankoEnv) + return s } func (s *SHMux) RegistFunction(name string, function interface{}) { if s.ankoEnv != nil { - s.ankoEnv.Define(name, function) + _ = s.ankoEnv.Define(name, function) } } @@ -111,10 +113,12 @@ func (s *SHMux) handle(w http.ResponseWriter, r *http.Request, script string) { resHelper := &ResponseHelper{ w: w, } - env := s.ankoEnv.Copy() - env.Define("Req", reqHelper) - env.Define("Res", resHelper) - _, err := vm.Execute(env, nil, script) + renv := s.ankoEnv.Copy() + _ = renv.Define("Req", reqHelper) + _ = renv.Define("Res", resHelper) + rinterceptor := interceptor.NewAnkointerceptor(renv) + + _, err := rinterceptor.ExecContent(script) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/interceptor/dl.go b/interceptor/dl.go index cb19d09..3c1b2ab 100644 --- a/interceptor/dl.go +++ b/interceptor/dl.go @@ -85,7 +85,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func if err != nil { panic("Load library error") } - a.libhabdles = append(a.libhabdles, lib) + a.liberalises = append(a.liberalises, lib) fnmap := make(map[string]interface{}) fileds := make([]reflect.StructField, 0, len(fnmap)) for name, types := range libFuncMap { @@ -122,7 +122,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func // Close 关闭所有已加载的动态库句柄,释放资源。 // 应在不再需要使用动态库时调用此方法。 func (a *Ankointerceptor) Close() { - for _, lib := range a.libhabdles { - purego.Dlclose(lib) + for _, lib := range a.liberalises { + _ = purego.Dlclose(lib) } } diff --git a/interceptor/interceptor.go b/interceptor/interceptor.go index 6fd4e6d..c58a90d 100644 --- a/interceptor/interceptor.go +++ b/interceptor/interceptor.go @@ -10,10 +10,10 @@ import ( ) type Ankointerceptor struct { - importMap map[string]interface{} - libMap map[string]interface{} - libhabdles []uintptr - ankoEnv *env.Env + importMap map[string]interface{} + libMap map[string]interface{} + liberalises []uintptr + ankoEnv *env.Env } func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor { @@ -21,13 +21,13 @@ func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor { ankoEnv = env.NewEnv() } a := &Ankointerceptor{ - importMap: make(map[string]interface{}), - libMap: make(map[string]interface{}), - libhabdles: make([]uintptr, 0), - ankoEnv: ankoEnv, + importMap: make(map[string]interface{}), + libMap: make(map[string]interface{}), + liberalises: make([]uintptr, 0), + ankoEnv: ankoEnv, } - a.ankoEnv.Define("println", fmt.Println) - a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} { + _ = a.ankoEnv.Define("println", fmt.Println) + _ = a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} { return a.loadLibrary(libPath, libFuncMap) }) a.importMap["fs"] = &FileModule{} @@ -96,9 +96,9 @@ func (a *Ankointerceptor) exec(scriptContent string, scriptPath string) (interfa cdir, _ := os.Getwd() scriptPath = filepath.Join(cdir, "tmp") } - e.Define("Require", a.genRequireMethod(scriptPath)) - e.Define("__filename", scriptPath) - e.Define("__dirname", filepath.Dir(scriptPath)) + _ = e.Define("Require", a.genRequireMethod(scriptPath)) + _ = e.Define("__filename", scriptPath) + _ = e.Define("__dirname", filepath.Dir(scriptPath)) // Execute the script code in the prepared environment result, err := vm.Execute(e, nil, scriptContent)