fix var name bug for MakePluginCode

This commit is contained in:
Star 2024-03-24 12:20:58 +08:00
parent abf7e54f20
commit c036d99899
2 changed files with 16 additions and 13 deletions

View File

@ -89,7 +89,7 @@ func test(t *testing.T, name string, check bool, extArgs ...interface{}) {
func TestTS(t *testing.T) { func TestTS(t *testing.T) {
plg := plugin.Get("obj") plg := plugin.Get("obj")
plgCode := gojs.MakePluginCode(plg) 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) { func TestGlobal(t *testing.T) {
@ -103,42 +103,44 @@ return plus(number,2)
"plus": func(i, j int) int { return i + j }, "plus": func(i, j int) int { return i + j },
} }
r, _, _ := gojs.Run(code, &gojs.RuntimeOption{ r, _ := gojs.Run(code, &gojs.RuntimeOption{
Globals: globals, Globals: globals,
}) })
test(t, "call", u.Int(r) == 11, r) test(t, "call", u.Int(r) == 11, r)
} }
func TestPlugin(t *testing.T) { 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) test(t, "obj.getId()", u.String(r) == "o-00", r)
r, _, _ = gojs.Run(` r, _ = rt.Run(`
o = obj.new('o-01') o = obj.new('o-01')
return o.getId() return o.getId()
`, nil) `)
test(t, "new obj.getId()", u.String(r) == "o-01", r) test(t, "new obj.getId()", u.String(r) == "o-01", r)
t1 := time.Now() t1 := time.Now()
r, _, _ = gojs.Run(` r, _ = rt.Run(`
out = '' out = ''
obj.echo('123', function(text){ obj.echo('123', function(text){
out = text out = text
}, null) }, null)
return out return out
`, nil) `)
t2 := time.Now() t2 := time.Now()
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
test(t, "callback", u.String(r) == "123", r) test(t, "callback", u.String(r) == "123", r)
t1 = time.Now() t1 = time.Now()
r, _, _ = gojs.Run(` r, _ = rt.Run(`
out = '' out = ''
obj.echoTimes(function(text){ obj.echoTimes(function(text){
out += text out += text
}) })
return out return out
`, nil) `)
t2 = time.Now() t2 = time.Now()
fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro()) fmt.Println("time:", t2.UnixMicro()-t1.UnixMicro())
test(t, "callbacks", u.String(r) == "01234", r) test(t, "callbacks", u.String(r) == "01234", r)
@ -170,6 +172,7 @@ func BenchmarkCallback(tb *testing.B) {
tb.StartTimer() tb.StartTimer()
for i := 0; i < tb.N; i++ { for i := 0; i < tb.N; i++ {
gojs.Run(` gojs.Run(`
import obj from 'obj'
out = '' out = ''
obj.echoTimes(function(text){ obj.echoTimes(function(text){
out += text out += text

8
ts.go
View File

@ -279,10 +279,10 @@ func makeFuncInOutArgs(t reflect.Type, existsClasses *map[string]bool, classes *
//if arg.Kind() == reflect.Pointer { //if arg.Kind() == reflect.Pointer {
// arg = arg.Elem() // arg = arg.Elem()
//} //}
//if arg.Kind() == reflect.Func { if arg.Kind() == reflect.Func {
// argInfo.isFunc = true argInfo.isFunc = true
// argInfo.funcInArgs, argInfo.funcOutArgs = makeFuncInOutArgs(arg, existsClasses, classes) argInfo.funcInArgs, argInfo.funcOutArgs = makeFuncInOutArgs(arg, existsClasses, classes)
//} }
//if !argInfo.isSkip && arg.Kind() == reflect.Struct { //if !argInfo.isSkip && arg.Kind() == reflect.Struct {
// makeClass(originArg, existsClasses, classes) // makeClass(originArg, existsClasses, classes)
//} //}