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) {
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

8
ts.go
View File

@ -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)
//}