lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


so in the end, i found the same reload() function in the the Lua reference manual.
but your code got me started.
so i wrote a simple/primitive example to exploit your code.

the module i want to reload is named themodule.lua, it has only one simple line code:
--------------------------------------------------
function TheFunction() return '1' end
--------------------------------------------------

and the main file is named main.lua:
-------------------------------------------------
require "themodule"
io.write('the value is ' .. TheFunction()..'\n')
io.write('Edit the file themodule.lua, then press enter to CONTINUE\n')
io.read()
package.loaded['TheModule'] = nil; require 'TheModule'
io.write('the value is ' .. TheFunction()..'\n')
-----------------------------------------------------------

and here is more complex example with a loop that watches for timestamp changes in the themodule.lua and then dynamically reloads it.
----------------------------------------------------------------------------------------------------
require 'lfs';require 'themodule'

thefile='themodule.lua'
print('the value is ' .. TheFunction())

lastmodif= lfs.attributes (thefile).modification
while(true) do
    local currentmodif = lfs.attributes(thefile).modification
    if lastmodif ~= currentmodif then
        package.loaded['TheModule'] = nil; require 'TheModule'
        print('the value is ' .. TheFunction())
    end
    lastmodif = currentmodif
    os.execute('sleep 1')
end
------------------------------------------------------------------------------------------------------

of course, dealing with variables needs to work.
i started to experimet with coroutintes. one for the iup frame and another gui callbacks.
i dont like cooperative multi-tasking so now i am experimenting with Lanes.
http://files.luaforge.net/releases/lanes/lanes/Lanes2011

so again thanks much

> Date: Thu, 21 Jun 2012 16:23:00 -0400
> From: kevin.t.ryan@gmail.com
> To: lua-l@lists.lua.org
> Subject: Re: newbie: how to modify lua code while the program is running like visual basic
>
> Well, depends on how much control you want - if you simply want to
> reload a module, you can create a function to do that for you:
>
> function reload(mod)
> package.loaded[mod] = nil
> require mod
> end
>
> It won't give you "hot swapping" so to speak, but it will reset the
> function to what you want (in your original example). See here for
> more information:
>
> http://lua-users.org/wiki/ModulesTutorial
>
> Hope that helps!
> ---------------------
> Kevin T. Ryan
> (215) 764-6915
>
>
> On Thu, Jun 21, 2012 at 3:41 PM, yoyomeltz yoyomeltz
> <yoyomeltz@hotmail.com> wrote:
> > kevin, thanks you much. it does seem to offer what i wanted but the problem
> > is i would be locked into an ide that does not seem to offer much else.
> >
> > there has to be a simple way to dynamically reload code as needed via a
> > console/command.line or other simple solution.
> > if visual basic from microsoft can do it and python can do it, then why
> > should i be an issue for Lua.
> > even visual basic 6.0 from 10 years ago can do that.
> >
> > it seems to me this such a feature is a pre-requisite for learning any
> > language, espcially a lightweight scripting language.
> > so does anybody have any other suggestions?
> > thanks in advance
> >
> >> Date: Thu, 21 Jun 2012 12:03:35 -0400
> >> From: kevin.t.ryan@gmail.com
> >> To: lua-l@lists.lua.org
> >> Subject: Re: newbie: how to modify lua code while the program is running
> >> like visual basic
> >
> >>
> >> You may want to try ZeroBrane Studio:
> >>
> >>
> >> http://notebook.kulchenko.com/zerobrane/live-coding-in-lua-bret-victor-style
> >>
> >> I haven't tried it (Vim user :)), but the video looks impressive and
> >> sounds like it might be what you want.
> >> ---------------------
> >> Kevin T. Ryan
> >> (215) 764-6915
> >>
> >>
> >> On Thu, Jun 21, 2012 at 11:36 AM, yoyomeltz yoyomeltz
> >> <yoyomeltz@hotmail.com> wrote:
> >> > hi, my name is david and this is my first post, so i do aplogize if i am
> >> > doing something lame....
> >> > and yeah, i know to use 'Lua' and not 'lua' and not 'LUA', lol i already
> >> > made that mistake
> >> >
> >> >
> >> >
> >> > there is a feature called Edit and Continue.
> >> > if i am running a program in the debugger, i am able to modify the code
> >> > of a
> >> > function while in the debugger.
> >> > both Visual Basic for Visual Studio and Visual Basic for Applications
> >> > can do
> >> > that. in fact, VBA can modify a function while the debugger is debugging
> >> > that specific function.
> >> >
> >> > as you can imagine this approach enables me to build program
> >> > 'on-the'fly', i
> >> > can learn iup without having to stop the program, change the code,
> >> > debug,
> >> > fix the issue, run the program again, debug it again, and over and over.
> >> >
> >> > so with Lua, i need that same functionality.
> >> > i am trying to learn Lua and IUP.
> >> > for example, lets say i have used the following code:
> >> > ------------------------------------------
> >> > require("iuplua")
> >> >
> >> > ml = iup.multiline{expand="YES", value="I ignore the 'g' key!",
> >> > border="YES"}
> >> >
> >> > ml.action = "" c, after)
> >> >    if c == iup.K_g then
> >> >      return iup.IGNORE
> >> >   else
> >> >     return iup.DEFAULT;
> >> >   end
> >> > end
> >> >
> >> > dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER"}
> >> > dlg:show()
> >> >
> >> > if (iup.MainLoopLevel()==0) then
> >> >   iup.MainLoop()
> >> > end
> >> > ------------------------------------
> >> >
> >> > i would like to be able to change that ml.action while the program is
> >> > being
> >> > debugged.
> >> > i was not able to get that to work.
> >> > so i tried to place that ml.action in another lua file name second.lua.
> >> > to the main program i added a require('second') to that other .lua file.
> >> > if i change the code in the code in second.lua, while the debugger is
> >> > active, i have the same problem.
> >> >
> >> > i would like to switch from VB to Lua, i am loving Lua!
> >> > this is the only real issue i need to resolve.
> >> > thanks in advance,
> >> > david
> >> >
> >>
>