[experiment] script state metatable #436
Conversation
|
oh, this is clever. by changing the as you say, it doesn't prevent a script from smashing contents of existing global tables. maybe its sufficient to cover that by convention. |
i'm wondering that too. also curious how this gotcha would come up and impact folks in practice. i haven't written enough lua myself to know but my gut says this wouldn't happen very often when writing scripts for making music but dunno! |
|
re: trampling on globals and conventions. are we still possibly interested in renaming all of the c functions to include a preceding underscore? |
|
i think its fine. this covers the two practical problems: scripts accidentally clobbering global names used by the norns system, and stumbling over global state left over from another script. |
|
@tehn : not sure i'm following. (sorry, late back to the conversation!) are you thinking about user state that should persist between script runs? if so, what is that state? (if not, sorry if i'm muddying the waters!) |
|
@pq apologies perhaps i'm likewise totally confused. my assumption of the goal:
so mainly i have no idea what the code in this does :) what i'm looking for is some sort of drop-in replacement for my lvm_reset work-- instead of resetting the LVM i'd just like to restore the original state of globals |
achievement unlocked! this little patch should ensure script-written globals only stick until the next script is run which is meant to handle cases 1 and 2. in your example, as for how well this would stand-in for the lvm-reset idea, we should enumerate the issues and do some experimenting. (down the road, this might be a good seed for some docs -- see e.g., #437.) |
|
well then, this seems perfect then? i'm curious how we'd police the meta-table? |
briefly discussed in #426 but the basic idea is to be opinionated in the __newindex = function(t,k,v)
if (isDisallowed(k)) then
warn('writing to '..k..' is a bad idea!')
else
state[k] = v
end, |
|
just tested this.
in general, this is likely a huge improvement as-is, particularly during development when globals can make a huge mess. |
|
thanks for nudging this. i've been meaning for like forever to cycle back to it.
ok, yeah. that's because those writes are to a table -- there's a kind of veiled caveat above about how this works "shallowly". could you give me an example of what you're doing? are you using
same thing. we'll need to go another level deeper for those table entries. to help me get my head back into this, could you enumerate some of the globals we want to have stick and those we don't? (aside: this would be good fodder for #437.) i have a few ideas and concrete examples will help me test 'em out. |
|
i think there are two issues at hand:
for example, in the REPL:
and then reset a script, and it's still nil. but again, this isn't the most pressing issue. also suggested above is renaming a whole ton of the c function calls with a preceding underscore to clear up usable namespace with a naming rule (ie, don't use leading underscores) |
|
further testing shows this as a good solution to the first problem. perhaps we merge/close and create a secondary issue for the other parts? or makes some checkboxes for #425 ? |
|
@tehn |
another script environment management experiment.
sets up a metatable for script state allowing scripts to shadow but not clobber state in
_G[1]. script state is zeroed on script runs so scripts don't accumulate surprising state.unlike #426 which introduces a fresh env that scripts are loaded into, this continues to use
_Gand so the repl "just works".(tested on grid apps; not midi.)
see also: #258
/cc @catfact @tehn @ngwese
[1] qualification: "shallow" state or something; writing into (pre)existing tables in
_Gwill stick.