Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported method-to-closure and method-to-function #54

Open
cosmos72 opened this issue Dec 21, 2018 · 3 comments
Open

unsupported method-to-closure and method-to-function #54

cosmos72 opened this issue Dec 21, 2018 · 3 comments

Comments

@cosmos72
Copy link

cosmos72 commented Dec 21, 2018

Probably I reported this already, but I cannot find it among closed issues:

gi> import "time"

elapsed: '283.382µs'
gi> time.Now
userdata: 0x7f9d70786518

elapsed: '157.187µs'
gi> t := time.Now()

elapsed: '227.014µs'
gi> t.After(t)
// I was expecting 'false', but nothing was shown. Is it intentional?

elapsed: '510.565µs'
gi> t.After
problem in golua_callgofunction, panic happened: '[string "--..."]:611: no field named `__methodVals` for type time.Time' at
goroutine 1 [running, locked to thread]:
runtime/debug.Stack(0xc4202ef368, 0xe34100, 0xc4204d7f50)
	/usr/local/go/src/runtime/debug/stack.go:24 +0xa7
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.golua_callgofunction.func1()
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/golua.go:149 +0x6e
panic(0xe34100, 0xc4204d7f50)
	/usr/local/go/src/runtime/panic.go:502 +0x229
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.(*State).RaiseError(0xc4202c5480, 0xc420036270, 0x30)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/lua.go:760 +0x1fc
github.com/gijit/gi/vendor/github.com/glycerine/luar.struct__newindex(0xc4202c5480, 0x47)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/luar/proxymm.go:555 +0x523
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.golua_callgofunction(0x7f9d70785ee8, 0x0, 0x1, 0x7f9d7802d378, 0x3d, 0x0)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/golua.go:191 +0x5af
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua._cgoexpwrap_ffe5ce162ef2_golua_callgofunction(0x7f9d70785ee8, 0x0, 0x1, 0x7f9d7802d378, 0x3d, 0x3ff0000000000000)
	_cgo_gotypes.go:1714 +0x53
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua._Cfunc_lua_pcall(0x7f9d7802d378, 0x1, 0x1, 0x0)
	_cgo_gotypes.go:1215 +0x4d
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.(*State).pcall.func1(0x7f9d7802d378, 0x1, 0x7f9d00000001, 0xc4204e19e0)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/lua.go:179 +0x6e
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.(*State).pcall(0xc4201a6e40, 0x1, 0x0, 0x1, 0x2)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/lua.go:179 +0x49
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.(*State).callEx(0xc4201a6e40, 0x1, 0x0, 0x1c56701, 0x0, 0x0)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/lua.go:203 +0x18c
github.com/gijit/gi/vendor/github.com/glycerine/golua/lua.(*State).Call(0xc4201a6e40, 0x1, 0x0, 0x0, 0x1)
	/home/max/go/src/github.com/gijit/gi/vendor/github.com/glycerine/golua/lua/lua.go:220 +0x44
github.com/gijit/gi/pkg/compiler.(*Goro).privateRun(0xc4201e7d50, 0xc4204cadc0, 0x2a2, 0x2c0, 0xc4202efb01, 0x0, 0x0)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/goro.go:301 +0x475
github.com/gijit/gi/pkg/compiler.(*Goro).handleTicket(0xc4201e7d50, 0xc420017080)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/goro.go:217 +0x54e
github.com/gijit/gi/pkg/compiler.(*Goro).do(0xc4201e7d50, 0xc420017080)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/goro.go:253 +0x35
github.com/gijit/gi/pkg/compiler.(*ticket).Do(0xc420017080, 0xc4204cab00, 0x2a2)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/goro.go:259 +0x10e
github.com/gijit/gi/pkg/compiler.LuaRun(0xc4201a3840, 0xc4204cab00, 0x2a2, 0x1, 0xc4202efda8, 0x1)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/luaUtil.go:555 +0x5a
github.com/gijit/gi/pkg/compiler.(*Repl).Eval(0xc4201da360, 0xc4200c4f58, 0x7, 0x0, 0x0)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/repl_luajit.go:549 +0x46f
github.com/gijit/gi/pkg/compiler.(*Repl).Loop(0xc4201da360)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/repl_luajit.go:187 +0x67
github.com/gijit/gi/pkg/compiler.(*GIConfig).LuajitMain(0xc4201a3820)
	/home/max/go/src/github.com/gijit/gi/pkg/compiler/repl_luajit.go:93 +0x64
main.main()
	/home/max/go/src/github.com/gijit/gi/cmd/gi/repl.go:59 +0x21a


elapsed: '1.324371ms'
gi> 

The expected result of t.After is a function with signature func (time.Time) bool

A similar error happens with time.Time.After:

gi> import "time"

elapsed: '235.411µs'
gi> time.Time.After
error! __errHandlerForEval sees err =	[string "--..."]:639: attempt to index field 'prototype' (a nil value)
[string "--..."]:639: attempt to index field 'prototype' (a nil value)
stack traceback:
	[string "--..."]:3262: in function '__index'
	[string "--..."]:639: in function '__methodExpr'
	[string "print(__methodExpr(__type__.time.Time, "After..."]:1: in function 'chunk'
	[string "--..."]:3290: in function <[string "--..."]:3290>
	[C]: in function 'xpcall'
	[string "--..."]:3290: in function '__gijitMainEval'
	[string "--..."]:3322: in function <[string "--..."]:3322>

elapsed: '111.532µs'
gi> 

Playground runs both as expected: https://play.golang.org/p/4DemIg_y87F

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	fmt.Printf("%v %T\n", t.After, t.After)                 // shows 0x.... func(time.Time) bool
	fmt.Printf("%v %T\n", time.Time.After, time.Time.After) // shows 0x.... func(time.Time, time.Time) bool
}

Maybe solving this can also help with issue #53 ?

@glycerine
Copy link
Contributor

Thanks, @cosmos72, for making note of these.

Yes they are bugs.

If the values were entirely constructed at runtime from Go code (translated into Lua code), then all these would work fine. However, the time library is imported as a binary library, so really this is testing the use and interaction with binary Go values. The type system in use for Lua objects isn't as complete for dealing with binary Go values and types. This isn't intentional. It's just that the binary Go library support was added later and I haven't found time to polish it. I am certainly aware that there are rough edges, especially when asking gi for the types that are binary Go values.

The current status is that I've made the binary Go types work inside interpreter defined structs and standalone (https://github.com/gijit/gi/blob/master/pkg/compiler/time_test.go#L37). But there's still a bunch of polish to do.

@cosmos72
Copy link
Author

cosmos72 commented Dec 21, 2018

I can confirm that writing an almost complete Go interpreter is a lot of work...

As per your suggestion, I tested the method-to-closure and method-to-function syntax on a type declared within gi:

gi> type Date struct { val int }

elapsed: '178.44µs'
gi> func (d Date) Less(d2 Date) bool {
return d.val < d2.val
}

elapsed: '90.663µs'
gi> var d Date

elapsed: '253.965µs'
gi> d.Less
function: 0x7ff058e2c9b0

elapsed: '123.71µs'
gi> Date.Less
error! __errHandlerForEval sees err =	[string "--..."]:640: attempt to index local 'method' (a function value)
[string "--..."]:640: attempt to index local 'method' (a function value)
stack traceback:
	[string "--..."]:3262: in function '__index'
	[string "--..."]:640: in function '__methodExpr'
	[string "print(__methodExpr(__type__.Date, "Less"));"]:1: in function 'chunk'
	[string "--..."]:3290: in function <[string "--..."]:3290>
	[C]: in function 'xpcall'
	[string "--..."]:3290: in function '__gijitMainEval'
	[string "--..."]:3322: in function <[string "--..."]:3322>

@glycerine
Copy link
Contributor

yes. You can call Less(), but Date.Less as an expression won't currently work.

gi> type Date struct { val int }
type Date struct { val int }

elapsed: '114.028µs'
gi> func (d Date) Less(d2 Date) bool {
return d.val < d2.val
func (d Date) Less(d2 Date) bool {
return d.val < d2.val
}
}

elapsed: '166.883µs'
gi> var d Date
var d Date

elapsed: '188.144µs'
gi> d.Less(d)
d.Less(d)

elapsed: '131.699µs'
gi> println(d.Less(d))
println(d.Less(d))
false

elapsed: '194.335µs'
gi> d2:= Date{val: 5}
d2:= Date{val: 5}

elapsed: '142.47µs'
gi> println(d.Less(d2))
println(d.Less(d2))
true

elapsed: '107.222µs'
gi> println(d2.Less(d))
println(d2.Less(d))
false

elapsed: '180.006µs'
gi> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants