elgopher [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=81157 require function for loading Lua modules <p> <table><tr><td> <a href="/bbs/?pid=143480#p"> <img src="/bbs/thumbs/pico64_require-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=143480#p"> require</a><br><br> by <a href="/bbs/?uid=81157"> elgopher</a> <br><br><br> <a href="/bbs/?pid=143480#p"> [Click to Play]</a> </td></tr></table> </p> <p>I wrote Lua <code>require</code> function based on the <code>include</code> function found in <code>/system/lib/head.lua:791</code>.</p> <p>This function loads Lua modules similar to standard require found in Lua:</p> <ul> <li>it loads the file</li> <li>executes the file</li> <li>returns the module returned by included file</li> <li>caches the module, so next call to require will return already loaded module</li> </ul> <p>How to use:</p> <ul> <li>put the source code of <code>require</code> function in your <code>main.lua</code>:</li> </ul> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function require(name) if _modules == nil then _modules={} end local already_imported = _modules[name] if already_imported ~= nil then return already_imported end local filename = fullpath(name..'.lua') local src = fetch(filename) if (type(src) ~= &quot;string&quot;) then notify(&quot;could not include &quot;..filename) stop() return end -- https://www.lua.org/manual/5.4/manual.html#pdf-load -- chunk name (for error reporting), mode (&quot;t&quot; for text only -- no binary chunk loading), _ENV upvalue -- @ is a special character that tells debugger the string is a filename local func,err = load(src, &quot;@&quot;..filename, &quot;t&quot;, _ENV) -- syntax error while loading if (not func) then send_message(3, {event=&quot;report_error&quot;, content = &quot;*syntax error&quot;}) send_message(3, {event=&quot;report_error&quot;, content = tostr(err)}) stop() return end local module = func() _modules[name]=module return module end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <ul> <li>write modules, for example create file named <code>module.lua</code>:</li> </ul> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>M = {} function M.func() print(&quot;inside module.func()&quot;) end return M</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <ul> <li>import modules:</li> </ul> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>local module = require('module') -- will load and execute module.lua module.func()</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=140784 https://www.lexaloffle.com/bbs/?tid=140784 Sun, 17 Mar 2024 11:21:24 UTC