teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.02k stars 101 forks source link

debug.getlocal -- fixes to signature and return values #713

Closed JLPLabs closed 8 months ago

JLPLabs commented 8 months ago

The code below, using debug.getlocal([thread,] f, local), generates warnings and errors, despite using getlocal as specified in [1] (and all previous versions through and including 5.1). Quoting from [1], This function returns the name and the value of the local variable with index local of the function at level f of the stack.

-- getlocal.tl
local name: string
local val: string
name, val = debug.getlocal(2, 1)

tl check generates...

tmp> tl check getlocal.tl 
========================================
2 warnings:
getlocal.tl:3:1: only 0 values are returned by the function
getlocal.tl:3:7: only 0 values are returned by the function
========================================
3 errors:
getlocal.tl:3:1: variable is not being assigned a value
getlocal.tl:3:7: variable is not being assigned a value
getlocal.tl:3:28: argument 1: got integer, expected function(...: <any type>): <any type>...

getlocal is defined at line 5313 in tl.tl.

            ["getlocal"] = a_type {
               typename = "poly",
               types = {
                  a_type { typename = "function", args = TUPLE { THREAD, FUNCTION, NUMBER }, rets = TUPLE {} },
                  a_type { typename = "function", args = TUPLE { FUNCTION, NUMBER }, rets = TUPLE {} },
               },
            },

Perhaps it is appropriate to insert another definition to types{} table, like this:

                  a_type { typename = "function", args = TUPLE { NUMBER, NUMBER }, rets = TUPLE { STRING, STRING } },

I'd be happy to do a pull request if that is your preference.


[1] Lua 5.4 Reference Manual; https://www.lua.org/manual/5.4/manual.html#pdf-debug.getlocal

hishamhm commented 8 months ago

A PR is definitely welcome!

On Mon, Oct 30, 2023, 19:34 JLPLabs @.***> wrote:

The code below, using debug.getlocal([thread,] f, local), generates warnings and errors, despite using getlocal as specified in [1] (and all previous versions through and including 5.1). Quoting from [1], This function returns the name and the value of the local variable with index local of the function at level f of the stack.

-- getlocal.tl local name: string local val: string name, val = debug.getlocal(2, 1)

tl check generates...

tmp> tl check getlocal.tl

2 warnings: getlocal.tl:3:1: only 0 values are returned by the function getlocal.tl:3:7: only 0 values are returned by the function

3 errors: getlocal.tl:3:1: variable is not being assigned a value getlocal.tl:3:7: variable is not being assigned a value getlocal.tl:3:28: argument 1: got integer, expected function(...: ): ...

getlocal is defined at line 5313 in tl.tl.

        ["getlocal"] = a_type {
           typename = "poly",
           types = {
              a_type { typename = "function", args = TUPLE { THREAD, FUNCTION, NUMBER }, rets = TUPLE {} },
              a_type { typename = "function", args = TUPLE { FUNCTION, NUMBER }, rets = TUPLE {} },
           },
        },

Perhaps it is appropriate to insert another definition to types{} table, like this:

              a_type { typename = "function", args = TUPLE { NUMBER, NUMBER }, rets = TUPLE { STRING, STRING } },

I'd be happy to do a pull request if that is your preference.

[1] Lua 5.4 Reference Manual; https://www.lua.org/manual/5.4/manual.html#pdf-debug.getlocal

— Reply to this email directly, view it on GitHub https://github.com/teal-language/tl/issues/713, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB365MQUKCYBOO6MKXU4XLYCATPJAVCNFSM6AAAAAA6WYFTK6VHI2DSMVQWIX3LMV43ASLTON2WKOZRHE3DSMZSHE3DKNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

hishamhm commented 8 months ago

it's weird that the other cases of the definition are also missing rets... Also, in the case you're adding, the rets tuple should probably be something like STRING, ANY?

(Replying via email from the phone, so sorry for any bad formatting!)

On Mon, Oct 30, 2023, 19:45 Hisham Muhammad @.***> wrote:

A PR is definitely welcome!

On Mon, Oct 30, 2023, 19:34 JLPLabs @.***> wrote:

The code below, using debug.getlocal([thread,] f, local), generates warnings and errors, despite using getlocal as specified in [1] (and all previous versions through and including 5.1). Quoting from [1], This function returns the name and the value of the local variable with index local of the function at level f of the stack.

-- getlocal.tl local name: string local val: string name, val = debug.getlocal(2, 1)

tl check generates...

tmp> tl check getlocal.tl

2 warnings: getlocal.tl:3:1: only 0 values are returned by the function getlocal.tl:3:7: only 0 values are returned by the function

3 errors: getlocal.tl:3:1: variable is not being assigned a value getlocal.tl:3:7: variable is not being assigned a value getlocal.tl:3:28: argument 1: got integer, expected function(...: ): ...

getlocal is defined at line 5313 in tl.tl.

        ["getlocal"] = a_type {
           typename = "poly",
           types = {
              a_type { typename = "function", args = TUPLE { THREAD, FUNCTION, NUMBER }, rets = TUPLE {} },
              a_type { typename = "function", args = TUPLE { FUNCTION, NUMBER }, rets = TUPLE {} },
           },
        },

Perhaps it is appropriate to insert another definition to types{} table, like this:

              a_type { typename = "function", args = TUPLE { NUMBER, NUMBER }, rets = TUPLE { STRING, STRING } },

I'd be happy to do a pull request if that is your preference.

[1] Lua 5.4 Reference Manual; https://www.lua.org/manual/5.4/manual.html#pdf-debug.getlocal

— Reply to this email directly, view it on GitHub https://github.com/teal-language/tl/issues/713, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB365MQUKCYBOO6MKXU4XLYCATPJAVCNFSM6AAAAAA6WYFTK6VHI2DSMVQWIX3LMV43ASLTON2WKOZRHE3DSMZSHE3DKNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

JLPLabs commented 8 months ago

Will do. Good point about STRING, ANY.

JLPLabs commented 8 months ago

to your point about the other cases needing 'rets'... I could do a PR for them such that the entire branch looks like this

types = {
              a_type { typename = "function", args = TUPLE { THREAD, FUNCTION, NUMBER }, rets = STRING },
              a_type { typename = "function", args = TUPLE { THREAD, NUMBER, NUMBER }, rets = TUPLE { STRING, ANY } },
              a_type { typename = "function", args = TUPLE { FUNCTION, NUMBER }, rets = STRING },
              a_type { typename = "function", args = TUPLE { NUMBER, NUMBER }, rets = TUPLE { STRING, ANY } },
},

NOTE: calling getlocal with a function does NOT return a tuple, rather it returns a string holding the function arg, like this:

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
> function f1 (a, b) return a + b end
> f1(2, 40)
42
> debug.getlocal(f1,1)
a
> debug.getlocal(f1,2)
b