Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
ingie

Reloading required libraries

Recommended Posts

Just a quick note on something that may confuse a beginner [like me, until i found out what to do]

 

 

When prototyping a lua library, i tend to have a lua prompt open for testing and evaluating things, and an offline editor for the code - in mycase ZerobraneStudio - and have the config option "bufferChanges=false" instead of true, so that the file system on actual disk, is always in sync with the virtual game HDD
 

[ note, see the warnings and notice in the config if you're changing this yourself, you've been warned - but unless your game crashes, it should be safe - it will often be safe even if the game does crash, but... YMMMYRYHO* ]

 

 

However, you probably find that you're making "on-the-fly" changes to your library, and want to see them take effect in your lua prompt.

 

so, imagine in my editor i type 

local lib = {}

lib.test = function()

  print("Hello, Lua")

end

return lib

i save this as "testlib.lib" in my home/lib folder and at my lua prompt i type

 

lua> t = require("testlib") 

lua> t.test()

Hello, Lua

lua> 

 

i then make a change to my testlib.lib

local lib = {}

lib.test = function(name)

  print("Hello, "..tostring(name))

end

return lib

and save that over the testlib.lua file. 

still at my lua prompt, i want to test this change, so i naturally type:

 

lua> t = require("testlib") -- to reload the library changes

lua> t.test("ingie")

Hello, Lua

lua> 

 

hold on, i say, that's not my changed library...  a first thought might be, ah, i know:

 

lua> t2 = require("testlib") 

lua> t2.test("ingie")

Hello, Lua

lua> 

 

to fool lua into creating a new library... but no, lua is too clever for that... 

so, how do i rectify this?   well, lua uses a table packages.loaded to tell whether it should bother loading a library or not.
we can check this.

 

lua> for p in pairs(packages.loaded) do print p end 

... other loaded packages ...

testlib

... other loaded packages ...

lua> 

 

aha!

 

so, to reload our library, we can just nillify [if that's a word] the entry for our library

note that this name is the name of the filename, less extension, i.e. the bit we "required" it with, not the name of the variable we assigned it to 

 

lua> packages.loaded.testlib = nil

 

and then as before 
 

lua> t = require("testlib") -- to reload the library changes

lua> t.test("ingie")

Hello, ingie

lua> 

 

 

this caught me out for a while, before i read this

 

note also that the same is true even if you're exiting the lua prompt, using edit within OC to change the file, then going back into the lua prompt to test... the shell environment will still hold your library from before. 

 

 

* your mileage may make you rip your hair out

Link to post
Share on other sites

I'm not sure if this is due to a change in more-recent versions, but you'll want to use "package.loaded", rather than packages.loaded, for this to work.

 

This also works from within programs:  Simply set the package.loaded table to nil for any libraries you frequently edit just before you require them, and any changes made to those libraries will be reflected on a soft reboot (rather than having to manually turn the computer/server off-then-on).  

 

Here's a C&P from the top of my program:

--========= DECLARING TOP-SCOPE VARIABLES ============================================================================
local component, dbCard, cb
local str, event, term
local A, D, H
local CLASS, HEXCLASS
local CONSOLE, GRID
local DB, STATUS

do --========= INITIALIZING TOP-LEVEL COMPONENTS, CLASSES & VARIABLES ================================================
  do --*** IMPORTING LIBRARIES & INITIALIZING TABLE VARIABLES ***
    component = require("component")
    dbCard = component.debug
    cb = component.command_block

    str = require("unicode")
    event = require("event")
    term = require("term")

    package.loaded.ArtLib = nil
    package.loaded.DataLib = nil
    package.loaded.HexConfig = nil
    A = require("ArtLib")
    D = require("DataLib")
    H = require("HexConfig")

    --// snip // ... and so on

 

Link to post
Share on other sites

If OpenOS require is anything like standard Lua's, then you can force a reload for all future `require` calls by `table.remove`-ing the first entry in `package.loaders`. That is the one that queries `package.loaded`.

That's a bad idea, there probably is some code that relies on require not reloading every module every time. It's safer to remove the modules that you want to reload from package.loaded instead.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.