lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

Below code can be compiled but says an error "attempt to call a nil value"

```
print"hello"

(function() print"world" end)()
```

luac creates this instructions:

```
main <foo.lua:0,0> (7 instructions at 0x6a5a50)
0+ params, 2 slots, 1 upvalue, 0 locals, 2 constants, 1 function
        1       [1]     GETTABUP        0 0 -1  ; _ENV "print"
        2       [1]     LOADK           1 -2    ; "hello"
        3       [1]     CALL            0 2 2
        4       [3]     CLOSURE         1 0     ; 0x6a5d30
        5       [1]     CALL            0 2 2
        6       [1]     CALL            0 1 1
        7       [3]     RETURN          0 1
constants (2) for 0x6a5a50:
        1       "print"
        2       "hello"
locals (0) for 0x6a5a50:
upvalues (1) for 0x6a5a50:
        0       _ENV    1       0

function <foo.lua:3,3> (4 instructions at 0x6a5d30)
0 params, 2 slots, 1 upvalue, 0 locals, 2 constants, 0 functions
        1       [3]     GETTABUP        0 0 -1  ; _ENV "print"
        2       [3]     LOADK           1 -2    ; "world"
        3       [3]     CALL            0 2 1
        4       [3]     RETURN          0 1
constants (2) for 0x6a5d30:
        1       "print"
        2       "world"
locals (0) for 0x6a5d30:
upvalues (1) for 0x6a5d30:
        0       _ENV    0       0

```

Look at the 4th and 5th instructions of main chunk.
At 4th instruction, it registers the annonymous function to register 1, and 5th instruction calls a function from register 0, which is empty.
That is why the code says "attempt to call a nil value".

Generally speaking, the code makes a invalid instruction like this:

```
funcall()
(exp)
```

This code can't be compiled with lua5.1, but CAN be compiled with 5.2, 5.3

It is not a problem that `exp`  is function definition or variables (, even if the type is not function), but it can write number, string, and so on.
I think it should not be compiled.

And invalid instruction insertion IS a bug.

--
Kawahara Satoru
College of Information Science
University of Tsukuba