From c036d99899cccd6f0ffa0e4dff77657d6411d9fc Mon Sep 17 00:00:00 2001 From: Star <> Date: Sun, 24 Mar 2024 12:20:58 +0800 Subject: [PATCH] fix var name bug for MakePluginCode --- gojs_test.go | 21 ++++++++++++--------- ts.go | 8 ++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/gojs_test.go b/gojs_test.go index 68435be..034ce37 100644 --- a/gojs_test.go +++ b/gojs_test.go @@ -89,7 +89,7 @@ func test(t *testing.T, name string, check bool, extArgs ...interface{}) { func TestTS(t *testing.T) { plg := plugin.Get("obj") plgCode := gojs.MakePluginCode(plg) - test(t, "ts code", strings.Contains(plgCode, "(echoFunc: (text: string) => void)")) + test(t, "ts code", strings.Contains(plgCode, "(echoFunc: (text: string) => void)"), plgCode) } func TestGlobal(t *testing.T) { @@ -103,42 +103,44 @@ return plus(number,2) "plus": func(i, j int) int { return i + j }, } - r, _, _ := gojs.Run(code, &gojs.RuntimeOption{ + r, _ := gojs.Run(code, &gojs.RuntimeOption{ Globals: globals, }) test(t, "call", u.Int(r) == 11, r) } func TestPlugin(t *testing.T) { - r, _, _ := gojs.Run("return obj.getId()", nil) + rt := gojs.New(nil) + rt.Exec("import obj from 'obj'") + r, _ := rt.Run("return obj.getId()") test(t, "obj.getId()", u.String(r) == "o-00", r) - r, _, _ = gojs.Run(` + r, _ = rt.Run(` o = obj.new('o-01') return o.getId() -`, nil) +`) test(t, "new obj.getId()", u.String(r) == "o-01", r) t1 := time.Now() - r, _, _ = gojs.Run(` + r, _ = rt.Run(` out = '' obj.echo('123', function(text){ out = text }, null) return out -`, nil) +`) t2 := time.Now() fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) test(t, "callback", u.String(r) == "123", r) t1 = time.Now() - r, _, _ = gojs.Run(` + r, _ = rt.Run(` out = '' obj.echoTimes(function(text){ out += text }) return out -`, nil) +`) t2 = time.Now() fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) test(t, "callbacks", u.String(r) == "01234", r) @@ -170,6 +172,7 @@ func BenchmarkCallback(tb *testing.B) { tb.StartTimer() for i := 0; i < tb.N; i++ { gojs.Run(` +import obj from 'obj' out = '' obj.echoTimes(function(text){ out += text diff --git a/ts.go b/ts.go index a54af18..7f663d3 100644 --- a/ts.go +++ b/ts.go @@ -279,10 +279,10 @@ func makeFuncInOutArgs(t reflect.Type, existsClasses *map[string]bool, classes * //if arg.Kind() == reflect.Pointer { // arg = arg.Elem() //} - //if arg.Kind() == reflect.Func { - // argInfo.isFunc = true - // argInfo.funcInArgs, argInfo.funcOutArgs = makeFuncInOutArgs(arg, existsClasses, classes) - //} + if arg.Kind() == reflect.Func { + argInfo.isFunc = true + argInfo.funcInArgs, argInfo.funcOutArgs = makeFuncInOutArgs(arg, existsClasses, classes) + } //if !argInfo.isSkip && arg.Kind() == reflect.Struct { // makeClass(originArg, existsClasses, classes) //}