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

lua, overriding bizhawk-reserved variables #724

Closed
mugg91 opened this issue Nov 1, 2016 · 3 comments
Closed

lua, overriding bizhawk-reserved variables #724

mugg91 opened this issue Nov 1, 2016 · 3 comments
Labels
re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request

Comments

@mugg91
Copy link

mugg91 commented Nov 1, 2016

confirmed in 1.11.7 and 1.11.8.2

Not sure if this is something you'll look into but here goes.
I had a script active and was editing/saving it while refreshing over and over. At one point I declared

bit=7

and from that point on, when the script needed to use bit.check or bit.clear, it would crash saying that "bit" is a number value. This would persist even after deleting the offending line (it persists through refreshes). But it fixes itself when closing and reopening the lua client. (Only took me half an hour to figure that out)

There might be more issues related to this one (what happens when overriding other bizhawk-reserved variables such as "userdata", "emu", "client" etc.?)

This is also somewhat related to an issue where two different scripts could override each other's values. But I'm not sure if that issue still exists.

@zeromus
Copy link
Contributor

zeromus commented Nov 9, 2016

the lua philosophy is you can build it yourself, and you should. you can build a script which protects all built-in symbols. look for guidance in more general lua resources on the web

I'm quite sure the "issue" where two different scripts can stomp on each other still exists. Whether you think this is an "issue" depends on whether you prefer your scripts to collaborate or run in isolation.

@iamgreaser
Copy link
Contributor

This might work for your case:

if not global_traps_locked then
    local realg=_G
    local newg=setmetatable({}, {
        __index=function(t,k)
            return realg[k]
        end,
        __newindex=function(t,k,v)
            if realg[k] then
                error("attempted to assign to global variable `"..k.."`")
            else
                rawset(t,k,v)
            end
        end,
    })
    realg.global_traps_locked = true
    setfenv(0, newg)
end

@adelikat
Copy link
Contributor

adelikat commented Nov 9, 2016

I'm going to classify this as a "won't fix". There might be hacks we can do to prevent you from clobbering things. But there is a certain level of expertise expected when scripting for an emulator. And I think this can be safely classified as a thing for the user to avoid. We combat it with having more unlikely names for libraries such as bizstring. In the case of bit I can see how it is more likely to be inadvertent but I think once you know, you know to simply avoid it.

@adelikat adelikat closed this as completed Nov 9, 2016
@YoshiRulz YoshiRulz added re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request labels Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request
Projects
None yet
Development

No branches or pull requests

5 participants