cc-require: Standards compatible require f...
Oddstr13 17 Jul 2015
cc-require
A standards compatible implementation of require,
and part of the package library, as specified by the Lua Manual
Allows for compatability with many non-CC Lua programs and API.
Examples
Standards compatible:
The ComputerCraft way:
The main difference is that with standard require, APIs return a table
containing the API, where as in ComputerCraft's os.loadAPI, the global
environment of the API file is put into _G[api_filename].
This implementation of require allows you to import either type API.
It will however not place it in the global environment.
The search paths, where require looks for the API, can be found at the top of require.lua
Contribute
Download and installation
Resource pack can be downloaded from the Bitbucket download section.
Put the downloaded zip into the directory resourcepacks/, innside your minecraft directory, creating it if needed.
A standards compatible implementation of require,
and part of the package library, as specified by the Lua Manual
Allows for compatability with many non-CC Lua programs and API.
Examples
Standards compatible:
local api = {} function api.hello(what) print("Hello, " .. what .. "!") end return api
The ComputerCraft way:
function hello(what) print("Hello, " .. what .. "!") end
The main difference is that with standard require, APIs return a table
containing the API, where as in ComputerCraft's os.loadAPI, the global
environment of the API file is put into _G[api_filename].
This implementation of require allows you to import either type API.
It will however not place it in the global environment.
-- API file: /lib/api.lua local api = require("api") api.hello("World")
-- API file: /lib/test/api.lua local api = require("test.api") api.hello("ComputerCraft")
The search paths, where require looks for the API, can be found at the top of require.lua
Contribute
Download and installation
Resource pack can be downloaded from the Bitbucket download section.
Put the downloaded zip into the directory resourcepacks/, innside your minecraft directory, creating it if needed.
MKlegoman357 17 Jul 2015
Very nice! You should probably add compatibility with Lua 5.2 though, because CC is already starting to convert to it and it will be updated in later CC versions. I'd recommend supporting both: Lua 5.1 and Lua 5.2.
Exerro 17 Jul 2015
Very nice, I've always wanted require() in computercraft. My one suggestion is that you don't require people to download the resource pack, maybe make a single file that can be downloaded that adds the necessary globals when run? If I were on a server, or wanted to make a program that used this and distribute it, needing a custom resource pack wouldn't work.
Oddstr13 18 Jul 2015
MKlegoman357, on 17 July 2015 - 07:55 PM, said:
Very nice! You should probably add compatibility with Lua 5.2 though, because CC is already starting to convert to it and it will be updated in later CC versions. I'd recommend supporting both: Lua 5.1 and Lua 5.2.
If there is anything missing, please create a issue on Bitbucket, with details outlining what the problem is, and how to correct it.
awsumben13, on 17 July 2015 - 07:57 PM, said:
Very nice, I've always wanted require() in computercraft. My one suggestion is that you don't require people to download the resource pack, maybe make a single file that can be downloaded that adds the necessary globals when run? If I were on a server, or wanted to make a program that used this and distribute it, needing a custom resource pack wouldn't work.
You can allways just download and run the require.lua file (raw file).
I'll consider making a pastebin that downloads and installs the function into the global environment.
Exerro 18 Jul 2015
Oddstr13, on 18 July 2015 - 01:38 AM, said:
I'd assume it's just the setfenv and loadfile calls that wouldn't be compatible. In lua 5.2, you need to use load( string, name, nil (something to do with bytecode, ignore this), env ).
Oddstr13, on 18 July 2015 - 01:38 AM, said:
awsumben13, on 17 July 2015 - 07:57 PM, said:
-snip-
You can allways just download and run the require.lua file (raw file).
I'll consider making a pastebin that downloads and installs the function into the global environment.
Ah, silly me, didn't see that link to the file in the post (just looked in the downloads section when I wanted to download it ), might be worth making a mention of the single file in the downloads section too. Pastebin is always good because of its integration into CC: being able to install a file with a simple line in the shell.