Jump to content




libloader: Lua require support (5.3)

lua utility api

3 replies to this topic

#1 Janne

  • Members
  • 4 posts

Posted 31 May 2018 - 11:50 PM

libloader

Decided to write a small library that adds support for lua require. I see now after writing this utility that it has been done before by Oddstr13 thought this one is a bit different. Instead of being based on 5.1 docs I based it on 5.3 docs, the only difference afaik being that 5.3 renamed package.loaders to package.searchers.

One thing this library supports which Oddstr13 didn't is the possibility of loading libraries from the program's working directory by using "./" in the package.path string

Basic usage
local _libloader = dofile("path/to/libloader")
local mylibrary = require("mylibrary")

mylibrary.doSomething()
By default libloader is set up to look for libraries in the program's working directory (including <workdir>/lib and <workdir>/bin)
if it doesn't find anything there it will look in /lib and /bin
By default it will not find non lua files when looking in <workdir> due to the possibility of accidentally loading
a non library like a config file.

package.path is set to "./?.lua;./lib/?.lua;./lib/?;./bin/?.lua;./bin/?;/lib/?.lua;/lib/?;/bin/?.lua;/bin/?"
but it can be changed by calling setPath
local _libloader = dofile("path/to/libloader")

_libloader.setPath("./?.lua")

local mylibrary = require("mylibrary")

mylibrary.doSomething()


Lua 5.3 doc: http://www.lua.org/m...al.html#Modules
Pastebin: https://pastebin.com/tt8eQALP

Moonscript source:
Spoiler

Transpiled lua:
Spoiler

Edited by Janne, 01 June 2018 - 12:44 PM.


#2 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 01 June 2018 - 11:25 AM

I know this one is 5.3, but would it not be better off if the user just updated to 1.8? If they aren't using 1.12.2, it can be obtained here

#3 Janne

  • Members
  • 4 posts

Posted 01 June 2018 - 12:49 PM

Not quite sure I understand, does 1.8 support require? If it does that is great!

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 June 2018 - 01:54 PM

Yep.

https://github.com/d...27c644f311a886c





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users