Jump to content




cc-require: Standards compatible require for ComputerCraft

api lua

7 replies to this topic

#1 Oddstr13

  • Members
  • 25 posts
  • LocationNorway

Posted 17 July 2015 - 07: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.

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 July 2015 - 07: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.

#3 Exerro

  • Members
  • 801 posts

Posted 17 July 2015 - 07: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.

#4 Oddstr13

  • Members
  • 25 posts
  • LocationNorway

Posted 18 July 2015 - 01:38 AM

View PostMKlegoman357, 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.
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.



View Postawsumben13, 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.
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.

#5 Exerro

  • Members
  • 801 posts

Posted 18 July 2015 - 10:08 AM

View PostOddstr13, on 18 July 2015 - 01:38 AM, said:

View PostMKlegoman357, on 17 July 2015 - 07:55 PM, said:

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

View PostOddstr13, on 18 July 2015 - 01:38 AM, said:

View Postawsumben13, on 17 July 2015 - 07:57 PM, said:

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

#6 Yarillo

  • Members
  • 81 posts
  • LocationFrance

Posted 20 June 2016 - 02:11 PM

Extremely underrated post

It was very useful to me, thank you very much.

#7 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 21 June 2016 - 05:34 PM

I agree, this is very good.

#8 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 21 June 2016 - 06:08 PM

This should be the way to go for APIs.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users