This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CoolisTheName007's profile picture

loadreq API - require implemented in CC+some goodies!

Started by CoolisTheName007, 22 November 2012 - 12:35 PM
CoolisTheName007 #1
Posted 22 November 2012 - 01:35 PM
Code and Readme:
Github

In a nutshell,require it's a function that will probably simplify loading several APIS, and enables loading modules, without too much hassle writing the exact location of the files, polluting the global namespace, or unnecessarily reloading code.

Here is a copy-paste of the Github readme (you would better read it on github, at least it has some indentation there):


loadreq is a CC API that emulates the require Lua function behavior, like so:


require(s,paths,…)
@paths is a string of paths separated by ';' where there can be '?'
-acquires @paths variable, by the following order;
0-arg @paths
Example (FILE_PATH='myFolder/myFolder2/myAPI.lua'):
myAPI=require('myFolder2.myAPI','myFolder/?.lua')
1-REQUIRE_PATH in the caller's path, if existent
Example (FILE_PATH='myFolder/myFolder2/myAPI.lua'):
REQUIRE_PATH='myFolder/?.lua'
myAPI=require'myFolder2.myAPI'
2-directory named PACKAGE_NAME in FILE_PATH, if defined in the caller's environment
with sufixes appended by @sufix and concatenated with @vars.paths.
FILE_PATH is set, for instance, by lua_loader in the files it loads.
Example (FILE_PATH='myFolder/myFolder3/myFolder/runningFile'):
PACKAGE_NAME='myFolder'
myAPI=require'myAPI' –@paths is 'myFolder/?;myFolder/?.lua;myFolder/?/init.lua;myFolder/?/?.lua;myFolder/?/?;myFolder'
3-directory of FILE_PATH, if defined
with sufixes appended by @sufix and concatenated with @vars.paths.
Example (FILE_PATH='myFolder/runningFile'):
myAPI=require'myAPI' –@paths is 'myFolder/?;myFolder/?.lua;myFolder/?/init.lua;myFolder/?/?.lua;myFolder/?/?;myFolder'
4-@vars.paths as set in loadreq.vars.paths
-replaces '.' in @s by '/' and '..' by '.'
–for all search_path in @paths
- for all iterators in loadreq.vars.finders, iterates over the paths returned;
default iterator:
@direct: replaces '?' in the search_path by @s and returns the resulting path if it is a file.
-for the first valid path, calls the loaders in loadreq.vars.requirers sequentially until one succeds,
in which case it returns the first value that the loader returns, else if it returns nil,e it accumulates e as an error message
if all loaders fail, errors immediatly, printing all error messages
-in case of failure finding the path, errors with the searched paths.

It is easy to define custom search functions and path loaders (which I call requirers), by altering loadreq.vars.finders and loadreq.vars.requirers .
For instance, one could add a .json requirer. Also, if you also install my search API (see signature), loadreq uses the glob search function from it as default.
loadreq comes with a lua_requirer function, that handles lua files:

lua_requirer(path,cenv,env,renv,rerun,args)
Accepts empty or .lua extensions.
if the rerun flag is true, reloads the file even if it done it before;
if the file has been loaded already returns previous value;
if the file is being loaded returns nil, error_message
else:
loads file in @path;
sets it's env to @env, default {} with metatable with __index set to @renv, default _G;
calls the function with unpack(@args) and returns and saves either
the function return value;
if the function returns nil, a shallow copy of the functions environment.
CoolisTheName007 #2
Posted 01 December 2012 - 03:41 PM
Bumping for general update:
-new functionality;
-cleaner documentation.

Btw, forum editor still does not keeps indentation…
bjornir90 #3
Posted 01 December 2012 - 07:04 PM
Bumping for general update:
-new functionality;
-cleaner documentation.

Btw, forum editor still does not keeps indentation…
To keep indentation, use (code) and (/code) but in square bracket, like this :

This has
   Indentation !
       You see ?
CoolisTheName007 #4
Posted 02 December 2012 - 03:13 AM
snip
Is that a workaround or is it supposed to be that way? 'Cause if it is, it means I can't paste text with indentation, which is weird…I didn't use the code brackets because the text turns awfully small, but I suppose the indents are worth it. Thanks.
bjornir90 #5
Posted 02 December 2012 - 04:23 AM
snip
Is that a workaround or is it supposed to be that way? 'Cause if it is, it means I can't paste text with indentation, which is weird…I didn't use the code brackets because the text turns awfully small, but I suppose the indents are worth it. Thanks.
I think you can do something like that :


Hello
   I have indentation
      And I have a size of 4

Edit : it doesn't work, sorry
toxicwolf #6
Posted 05 January 2013 - 04:25 AM
Hello, I was wondering if you would be so kind as to allow me to use this in my OS, WolfOS 2.0.0?
Really good work, well done!
CoolisTheName007 #7
Posted 05 January 2013 - 05:21 AM
Hello, I was wondering if you would be so kind as to allow me to use this in my OS, WolfOS 2.0.0?
Really good work, well done!
Sure thing!! I've updated a few things, for basic functionality the loadreq github repository works fine, for the glob search you now have to download the search module (same github).