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

load function fails with nil args #91

Open
technomancy opened this issue Mar 14, 2018 · 9 comments
Open

load function fails with nil args #91

technomancy opened this issue Mar 14, 2018 · 9 comments

Comments

@technomancy
Copy link

This code, as you might expect, prints "2":

print(load("return (1 + 1)")())

However, this code fails, even though it should work exactly the same:

print(load("return (1 + 1)", nil)())
{"init terminating in do_boot",{{lua_error,{badarg,load,[<<"return (1 + 1)">>,nil]},{luerl,{array,13,100,undefined,{{{table,{array,0,10,nil,10},{{{{empty,<<"_G">>,{tref,0},empty,<<"_VERSION">>,<<"Lua 5.2">>,empty},<<"assert">>,{function,#Fun<luerl_lib_basic.1.62990734>},{empty,<<"bit32">>,{tref,5},empty},<<"collectgarbage">>,{function,#Fun<luerl_lib_basic.2.62990734>},{empty,<<"debug">>,{tref,12},empty}},<<"dofile">>,{function,#Fun<luerl_lib_basic.3.62990734>},{{empty,<<"eprint">>,{function,#Fun<luerl_lib_basic.4.62990734>},empty},<<"error">>,{function,#Fun<luerl_lib_basic.0.62990734>},{empty,<<"getmetatable">>,{function,#Fun<luerl_lib_basic.5.62990734>},empty,<<"io">>,{tref,6},empty}}},<<"ipairs">>,{function,#Fun<luerl_lib_basic.6.62990734>},{{{empty,<<"load">>,{function,#Fun<luerl_lib_basic.7.62990734>},empty},<<"loadfile">>,{function,#Fun<luerl_lib_basic.8.62990734>},{empty,<<"loadstring">>,{function,#Fun<luerl_lib_basic.9.62990734>},empty,<<"math">>,{tref,7},empty}},<<"next">>,{function,#Fun<luerl_lib_basic.10.62990734>},{{empty,<<"os">>,{tref,8},empty},<<"package">>,{tref,4},{empty,<<"pairs">>,{function,#Fun<luerl_lib_basic.11.62990734>},empty},<<"pcall">>,{function,#Fun<luerl_lib_basic.12.62990734>},{empty,<<"print">>,{function,#Fun<luerl_lib_basic.13.62990734>},empty}}},<<"rawequal">>,{function,#Fun<luerl_lib_basic.14.62990734>},{{{empty,<<"rawget">>,{function,#Fun<luerl_lib_basic.15.62990734>},empty},<<"rawlen">>,{function,#Fun<luerl_lib_basic.16.62990734>},{empty,<<"rawset">>,{function,#Fun<luerl_lib_basic.17.62990734>},empty,<<"require">>,{function,#Fun<luerl_lib_package.0.46355753>},empty}},<<"select">>,{function,#Fun<luerl_lib_basic.18.62990734>},{{empty,<<"setmetatable">>,{function,#Fun<luerl_lib_basic.19.62990734>},empty},<<"string">>,{tref,9},{empty,<<"table">>,{tref,11},empty}},<<"tonumber">>,{function,#Fun<luerl_lib_basic.20.62990734>},{{empty,<<"tostring">>,{function,#Fun<luerl_lib_basic.21.62990734>},empty},<<"type">>,{function,#Fun<luerl_lib_basic.22.62990734>},{empty,<<"unpack">>,{function,#Fun<luerl_lib_basic.23.62990734>},empty}}}},nil},{table,{array,3,10,nil,{nil,{function,#Fun<luerl_lib_package.2.46355753>},{function,#Fun<luerl_lib_package.3.46355753>},nil,nil,nil,nil,nil,nil,nil}},empty,nil},{table,{array,0,10,nil,10},{{{empty,<<"_G">>,{tref,0},empty},<<"bit32">>,{tref,5},{empty,<<"debug">>,{tref,12},empty},<<"io">>,{tref,6},{empty,<<"math">>,{tref,7},empty}},<<"os">>,{tref,8},{{empty,<<"package">>,{tref,4},empty},<<"string">>,{tref,9},{empty,<<"table">>,{tref,11},empty}}},nil},{table,{array,0,10,nil,10},empty,nil},{table,{array,0,10,nil,10},{{empty,<<"config">>,<<47,10,59,10,63,10,33,10,45,10>>,empty},<<"loaded">>,{tref,2},{empty,<<"path">>,<<"./?.lua;./?/init.lua">>,empty},<<"preload">>,{tref,3},{empty,<<"searchers">>,{tref,1},empty,<<"searchpath">>,{function,#Fun<luerl_lib_package.1.46355753>},empty}},nil},{table,{array,0,10,nil,10},{{{empty,<<"arshift">>,{function,#Fun<luerl_lib_bit32.7.29927074>},empty,<<"band">>,{function,#Fun<luerl_lib_bit32.0.29927074>},empty},<<"bnot">>,{function,#Fun<luerl_lib_bit32.1.29927074>},{empty,<<"bor">>,{function,#Fun<luerl_lib_bit32.2.29927074>},empty}},<<"btest">>,{function,#Fun<luerl_lib_bit32.3.29927074>},{{empty,<<"bxor">>,{function,#Fun<luerl_lib_bit32.4.29927074>},empty},<<"extract">>,{function,#Fun<luerl_lib_bit32.10.29927074>},{empty,<<"lrotate">>,{function,#Fun<luerl_lib_bit32.8.29927074>},empty}},<<"lshift">>,{function,#Fun<luerl_lib_bit32.5.29927074>},{{empty,<<"replace">>,{function,#Fun<luerl_lib_bit32.11.29927074>},empty},<<"rrotate">>,{function,#Fun<luerl_lib_bit32.9.29927074>},{empty,<<"rshift">>,{function,#Fun<luerl_lib_bit32.6.29927074>},empty}}},nil},{table,{array,0,10,nil,10},{empty,<<"flush">>,{function,#Fun<luerl_lib_io.0.101991831>},empty,<<"write">>,{function,#Fun<luerl_lib_io.1.101991831>},empty},nil},{table,{array,0,10,nil,10},{{{{empty,<<"abs">>,{function,#Fun<luerl_lib_math.2.97665772>},empty},<<"acos">>,{function,#Fun<luerl_lib_math.3.97665772>},{empty,<<"asin">>,{function,#Fun<luerl_lib_math.4.97665772>},empty}},<<"atan">>,{function,#Fun<luerl_lib_math.5.97665772>},{{empty,<<"atan2">>,{function,#Fun<luerl_lib_math.6.97665772>},empty},<<"ceil">>,{function,#Fun<luerl_lib_math.7.97665772>},{empty,<<"cos">>,{function,#Fun<luerl_lib_math.8.97665772>},empty}}},<<"cosh">>,{function,#Fun<luerl_lib_math.9.97665772>},{{{empty,<<"deg">>,{function,#Fun<luerl_lib_math.10.97665772>},empty},<<"exp">>,{function,#Fun<luerl_lib_math.11.97665772>},{empty,<<"floor">>,{function,#Fun<luerl_lib_math.12.97665772>},empty}},<<"fmod">>,{function,#Fun<luerl_lib_math.13.97665772>},{{empty,<<"frexp">>,{function,#Fun<luerl_lib_math.14.97665772>},empty},<<"huge">>,1.797693e+308,{empty,<<"ldexp">>,{function,#Fun<luerl_lib_math.15.97665772>},empty}}},<<"log">>,{function,#Fun<luerl_lib_math.16.97665772>},{{{empty,<<"log10">>,{function,#Fun<luerl_lib_math.17.97665772>},empty},<<"max">>,{function,#Fun<luerl_lib_math.0.97665772>},{empty,<<"min">>,{function,#Fun<luerl_lib_math.1.97665772>},empty}},<<"modf">>,{function,#Fun<luerl_lib_math.18.97665772>},{{empty,<<"pi">>,3.141593e+00,empty},<<"pow">>,{function,#Fun<luerl_lib_math.19.97665772>},{empty,<<"rad">>,{function,#Fun<luerl_lib_math.20.97665772>},empty}},<<"random">>,{function,#Fun<luerl_lib_math.21.97665772>},{{empty,<<"randomseed">>,{function,#Fun<luerl_lib_math.22.97665772>},empty},<<"sin">>,{function,#Fun<luerl_lib_math.23.97665772>},{empty,<<"sinh">>,{function,#Fun<luerl_lib_math.24.97665772>},empty},<<"sqrt">>,{function,#Fun<luerl_lib_math.25.97665772>},{empty,<<"tan">>,{function,#Fun<luerl_lib_math.26.97665772>},empty,<<"tanh">>,{function,#Fun<luerl_lib_math.27.97665772>},empty}}}},nil},{table,{array,0,10,nil,10},{{empty,<<"clock">>,{function,#Fun<luerl_lib_os.0.64788999>},empty},<<"date">>,{function,#Fun<luerl_lib_os.1.64788999>},{empty,<<"difftime">>,{function,#Fun<luerl_lib_os.2.64788999>},empty},<<"getenv">>,{function,#Fun<luerl_lib_os.3.64788999>},{empty,<<"time">>,{function,#Fun<luerl_lib_os.4.64788999>},empty}},nil},{table,{array,0,10,nil,10},{{{empty,<<"byte">>,{function,#Fun<luerl_lib_string.0.23292798>},empty},<<"char">>,{function,#Fun<luerl_lib_string.1.23292798>},{empty,<<"dump">>,{function,#Fun<luerl_lib_string.2.23292798>},empty}},<<"find">>,{function,#Fun<luerl_lib_string.3.23292798>},{{empty,<<"format">>,{function,#Fun<luerl_lib_string.4.23292798>},empty},<<"gmatch">>,{function,#Fun<luerl_lib_string.5.23292798>},{empty,<<"gsub">>,{function,#Fun<luerl_lib_string.6.23292798>},empty}},<<"len">>,{function,#Fun<luerl_lib_string.7.23292798>},{{empty,<<"lower">>,{function,#Fun<luerl_lib_string.8.23292798>},empty},<<"match">>,{function,#Fun<luerl_lib_string.9.23292798>},{empty,<<"rep">>,{function,#Fun<luerl_lib_string.10.23292798>},empty},<<"reverse">>,{function,#Fun<luerl_lib_string.11.23292798>},{empty,<<"sub">>,{function,#Fun<luerl_lib_string.12.23292798>},empty,<<"upper">>,{function,#Fun<luerl_lib_string.13.23292798>},empty}}},nil}},{{table,{array,0,10,nil,10},{empty,<<"__index">>,{tref,9},empty},nil},{table,{array,0,10,nil,10},{{empty,<<"concat">>,{function,#Fun<luerl_lib_table.0.83559307>},empty},<<"insert">>,{function,#Fun<luerl_lib_table.1.83559307>},{empty,<<"pack">>,{function,#Fun<luerl_lib_table.2.83559307>},empty},<<"remove">>,{function,#Fun<luerl_lib_table.3.83559307>},{empty,<<"sort">>,{function,#Fun<luerl_lib_table.4.83559307>},empty,<<"unpack">>,{function,#Fun<luerl_lib_table.5.83559307>},empty}},nil},{table,{array,0,10,nil,10},{{empty,<<"getmetatable">>,{function,#Fun<luerl_lib_debug.0.33065617>},empty},<<"getuservalue">>,{function,#Fun<luerl_lib_debug.1.33065617>},{empty,<<"setmetatable">>,{function,#Fun<luerl_lib_debug.2.33065617>},empty,<<"setuservalue">>,{function,#Fun<luerl_lib_debug.3.33065617>},empty}},nil},undefined,undefined,undefined,undefined,undefined,undefined,undefined},10,10,10,10,10,10,10,10,10}},[],13,{array,0,10,undefined,10},[],0,{tref,0},[{call_frame,[{[]}],[]},{function,#Fun<luerl_lib_basic.13.62990734>}],{meta,nil,nil,nil,{tref,10}},#Ref<0.0.2.79>}},[{luerl_lib,lua_error,2,[{file,"src/luerl_lib.erl"},{line,43}]},{luerl_emul,functioncall,4,[{file,"src/luerl_emul.erl"},{line,838}]},{luerl_emul,functioncall,7,[{file,"src/luerl_emul.erl"},{line,741}]},{luerl_emul,functioncall,5,[{file,"src/luerl_emul.erl"},{line,851}]},{luerl_emul,functioncall,4,[{file,"src/luerl_emul.erl"},{line,826}]},{luerl_emul,functioncall,3,[{file,"src/luerl_emul.erl"},{line,731}]},{luerl_emul,call,3,[{file,"src/luerl_emul.erl"},{line,392}]},{init,start_em,1,[]}]}}
@rvirding
Copy link
Owner

How were you calling this?

@technomancy
Copy link
Author

Great question! I'm actually using luerl with Fennel, which is a small compiler which targets Lua: https://github.com/bakpakin/Fennel/blob/master/fennel.lua#L64

I have been investigating some of the less mainstream Lua implementations to see if I could uncover any bugs with Fennel. But I believe Erlang semantics might be leaking here--Lua semantics would dictate that there is basically no way for a function to tell the difference between being called like f(1) vs f(1, nil, nil, nil); unless f calls select on ... these two calls should be equivalent.

That appears to not be the case here, at least with load but possibly with other functions as well?

@rvirding
Copy link
Owner

So load can handle multiple arguments but it is strict on the types of the first 3 arguments. If they are given they must be strings otherwise you get an argument error. The 4 can be anything. Currently only the first argument is used, the others are ignored. This needs some more work. The question is how to interpret nil as an argument here. Is calling load("return 1+1", nil, "bt") legal? The manual doesn't say.

This should only be a problem with functions in the standard libraries and not in functions defined in Lua.

@technomancy
Copy link
Author

technomancy commented Mar 21, 2018 via email

@rvirding
Copy link
Owner

How would it be interpreted? One way would be that all the nil arguments get their default value. Would this be a reasonable interpretation? Would it apply everywhere?

@technomancy
Copy link
Author

technomancy commented Mar 26, 2018 via email

@rvirding
Copy link
Owner

I will have a go and try to fix it. There is one limitation with load which is a bit more difficult to fix and that is that it currently only accepts a file name as first and not a function. This is more difficult to fix as calling lua functions from within erlang is a bit more tricky. We shall see if I can fix that as well.

@rvirding
Copy link
Owner

rvirding commented Apr 8, 2018

I will gradually upgrade the internal functions. If you come across where an explicit nil as an argument does not mean use the default then let me know.

@technomancy
Copy link
Author

technomancy commented Apr 10, 2018 via email

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