loadreq API - require implemented in CC+so...
CoolisTheName007 22 Nov 2012
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'
[email protected] 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.
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'
[email protected] 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 01 Dec 2012
Bumping for general update:
-new functionality;
-cleaner documentation.
Btw, forum editor still does not keeps indentation...
-new functionality;
-cleaner documentation.
Btw, forum editor still does not keeps indentation...
bjornir90 01 Dec 2012
CoolisTheName007 02 Dec 2012
bjornir90, on 01 December 2012 - 07:04 PM, said:
snip
bjornir90 02 Dec 2012
CoolisTheName007, on 02 December 2012 - 03:13 AM, said:
bjornir90, on 01 December 2012 - 07:04 PM, said:
snip
Hello I have indentation And I have a size of 4
Edit : it doesn't work, sorry
toxicwolf 05 Jan 2013
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!
Really good work, well done!
CoolisTheName007 05 Jan 2013
toxicwolf, on 05 January 2013 - 04:25 AM, said:
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!
Really good work, well done!