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
undefined variable
should not be syntax error
#1
Comments
you are right, mooncake was very strict about global variable usage, when using core library's loadstring to transpile moocscript to Lua. I think this is an advantage of mooncake, there will be less global variable pollution when code base becoming larger. there's two type of error in this code, I try to figure out what's happening: assert(require"moocscript.core".loadstring("print(a)"))
➜ /tmp moocscript -v
moocscript v0.8.20221204, LuaJIT 2.1.0-beta3
➜ /tmp cat a.mooc
assert(require"moocscript.core".loadstring("print(a)"))
➜ /tmp moocscript a.mooc
Error: invalid token 'string', when expected ')'
File: a.mooc
Line: 1 (Pos: 14)
Source: assert(require"moocscript.core".loadstring("print(a)"))
^
assert(require("moocscript.core").loadstring("print(a)")) and mooncake can transpile it to Lua, this time only treat "print(a)" as a string. ➜ /tmp cat b.mooc
assert(require("moocscript.core").loadstring("print(a)"))
➜ /tmp moocscript -s b.mooc
assert(require("moocscript.core").loadstring("print(a)")) 3. when really running, core library's loadstring will raise an error for transpile "print(a)" to Lua, for variable `a` undefined in source string all level scopes. ➜ /tmp moocscript b.mooc
~/rocks/bin/luajit: [string "b.mooc"]:1: Error: undefined variable
File: _
Line: 1 (Pos: 6)
Source: print(a)
^
stack traceback:
[C]: in function 'assert'
[string "b.mooc"]:1: in function 'f'
...uarocks/rocks-5.1/mooncake/0.8.20221204-2/bin/moocscript:119: in function 'main'
...uarocks/rocks-5.1/mooncake/0.8.20221204-2/bin/moocscript:243: in main chunk
[C]: at 0x0104b67a70 4. if you really know it will running well for bring pre-defined `a` from `setfenv` or `_ENV`, you can using `export` keyword like assert(require("moocscript.core").loadstring("export a; print(a)")) or you can using Lua's loadstring directly assert(loadstring("print(a)")) |
4a. With 4b. Sure we use Lua's So I have no other idea how to deal with it, except asking you to add some option to skip 'undefined variable' checks. |
|
Unfortunately I need it because the function meant to be executed in rich environment, with a lot of variables predefined.
Sorry, I have not got what you meant. |
Unfortunately I did not know why you want an undefined right-value, but mooncake is very strict to right-value to be defined first. Sorry, may be I don't know what you want. Sure I insist on you should read docs, including sources first for your task, which is completely irrelevant to me. Whish the MIT lincesed sources will help you. |
Because it is actually defined, not in the source yourself, but in environment. local ENV = {some_predefined_function=function (x)
-- ...
end}
local mcore = require("mooncake.core")
local fn = assert(mcore.loadstring([[
a = some_predefined_function(12345)
-- ...
]]))
setfenv(fn, ENV)
f() |
you can local ENV = {some_predefined_function=function (x)
-- ...
end}
setmetatable(ENV, { __index = _G })
local mcore = require("moocscript.core")
local fn = assert(mcore.loadstring('export *' .. [[
a = some_predefined_function(12345)
]]))
setfenv(fn, ENV)
fn() |
The problem is that
And using global by default rule I get environment polluted with unneeded identifiers |
you can using like Lua this way, or using more strict the other way, anyway you have all the sources. |
This is breaking change, as Lua's std
loadstring
is not so strict.And this is in many cases just wrong, because it is just impossible to make such assumption not taking into account function's ENV.
The text was updated successfully, but these errors were encountered: