some ideas on how it could work:
  • Required scripts are saved as a different type of script: modules, with a new compiler option: compile to "SLua scriopt" or to "Slua module".
  • Modules are executed when required and return a value, with a
    return
    statement, for instance a table with functions and constants.
  • Scripts call modules as
    myModule = require("Module name")
    , the returned value is assigned to the variable
    myModule
    , using the script's memory space.
  • The module must be in the object contents with the script that requires it.
  • If the module is not in the object, its running state is disabled, or it doesn't return a value,
    require()
    will return nil.
  • Once it returns, the module is removed from memory and its variables are garbage-collected.
  • Modules are executed synchronously, the main script is paused until the module returns.
  • Modules don't receive events, any events that arrive while the module is executing are queued in the main script.
  • Modules can't require other modules, only scripts can use
    require()
    .