Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Eduardo Ochs <eduardoochs <at> gmail.com>
Subject: Idea (unsafe): a variant of lua_getstack
Newsgroups: gmane.comp.lang.lua.general
Date: Tuesday 21st February 2012 09:23:15 UTC (over 5 years ago)
Two questions - one technical, one not...

1) Is there a way to flag clearly to the list that a certain idea is
   for a low-level/dangerous hack ONLY, and only meant as a prototype
   and for quick experiments that would be run only a handful of times
   and in very controlled environments? I would like to avoid getting
   answers like "don't do that, it's unsafe", "how would you handle
   GC?", "use userdata", "this won't work in architectures where
   pointers can not be represented faithfully as numbers"... Sometimes
   in a proof of concept it is better to leave all concerns with GC,
   safety, etc to a second moment...

2) I would like to experiment with variants of lua_getstack,
   debug.getinfo, debug.getlocal and debug.setlocal - see:

     http://www.lua.org/source/5.2/ldblib.c.html#dblib
     http://www.lua.org/source/5.2/ldblib.c.html#db_getinfo
     http://www.lua.org/source/5.2/ldblib.c.html#db_getlocal
     http://www.lua.org/source/5.2/ldblib.c.html#db_setlocal
     http://www.lua.org/source/5.2/ldebug.c.html#lua_getstack

     http://www.lua.org/manual/5.2/manual.html#lua_getstack
     http://www.lua.org/manual/5.2/manual.html#lua_getinfo
     http://www.lua.org/manual/5.2/manual.html#lua_getlocal
     http://www.lua.org/manual/5.2/manual.html#lua_setlocal

     http://www.lua.org/manual/5.2/manual.html#pdf-debug.getinfo
     http://www.lua.org/manual/5.2/manual.html#pdf-debug.getlocal
     http://www.lua.org/manual/5.2/manual.html#pdf-debug.setlocal

   in which the "level" parameter could also be given as a string of
   the form "0xhhhhhhhh", meaning the address in memory - without any
   kind of error checking - of an active function. Is there something
   like this around? I haven't started to write the code for this yet,
   and my C skills got quite rusty in the last years, so I'd rather
   start with someone else's code...

   If it is not clear how these variants could be useful, consider
   this:

     inner = function ()
         print(debug.getlocal(2, 1))      --> a 22
         print(debug.setlocal(2, 1, 33))  --> a
       end
     outer = function ()
         local a = 22
         print(a)   --> 22
         inner()
         print(a)   --> 33
       end
     outer()

   and this pseudocode, where _L is a table that gives access to the
   local variables of the function "outer":

     inner = function (_L)
         print(_L.a)   --> 22
         _L.a = 33
       end
     outer = function ()
         local a = 22
         _L = setmetable({}, {__index = ..., __newindex = ...})
         print(a)   --> 22
         inner(_L)
         print(a)   --> 33
       end
     outer()

   It is possible to write the code for "_L" in standard Lua using the
   debug library, but that requires a lot of juggling with the stack
   depth.

     Cheers,
       Eduardo Ochs
       [email protected]
       http://angg.twu.net/
 
CD: 4ms