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

cc-require: Standards compatible require for ComputerCraft

Started by Oddstr13, 17 July 2015 - 06:43 PM
Oddstr13 #1
Posted 17 July 2015 - 08:43 PM
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:

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 #2
Posted 17 July 2015 - 08:55 PM
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 #3
Posted 17 July 2015 - 08:57 PM
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 #4
Posted 18 July 2015 - 02:38 AM
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.
What exactly is missing before this is Lua 5.2 compatable?
If there is anything missing, please create a issue on Bitbucket, with details outlining what the problem is, and how to correct it.



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.
The resource pack is provided as a convinience, as it is needed for server(and client)-wide install of the require function.
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 #5
Posted 18 July 2015 - 11:08 AM
-snip-
What exactly is missing before this is Lua 5.2 compatable?
If there is anything missing, please create a issue on Bitbucket, with details outlining what the problem is, and how to correct it.

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 ).

-snip-
The resource pack is provided as a convinience, as it is needed for server(and client)-wide install of the require function.
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 :P/>), 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.
Yarillo #6
Posted 20 June 2016 - 03:11 PM
Extremely underrated post

It was very useful to me, thank you very much.
CrazedProgrammer #7
Posted 21 June 2016 - 06:34 PM
I agree, this is very good.
Creator #8
Posted 21 June 2016 - 07:08 PM
This should be the way to go for APIs.