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

including more advanced libraries

Started by Lemur, 10 September 2014 - 03:52 AM
Lemur #1
Posted 10 September 2014 - 05:52 AM
http://mirven.github.io/underscore.lua/
http://pastebin.com/4UK502Jb

Turtles will flip out if I try and include that at all. Using os.loadAPI for it, and downloading it as '_' as well.

When you mentioned before that there are differences between actual lua and in-game cc-lua, how large are they? Is this something that can be ported at all?

My version of it at least is partially working: http://pastebin.com/4w6sPV9H
Lyqyd #2
Posted 10 September 2014 - 06:08 AM
You'd need to convert it, of course. The normal Lua API/module inclusion system dependent on require() simply doesn't exist in ComputerCraft. It looks like this library is doing some jiggery-pokery to get the returned table into an… interesting configuration. You'd have an interesting time getting it to initialize correctly under os.loadAPI, though it is certainly possible. Your port is probably headed in the wrong direction, if it's truly necessary that this thing get its table into that final shape. I'd have to look into this a bit more to give you an answer for sure, but it should be possible to perform a bit of CC-specific black magic to end up with the right result. Off the top of my head, you're going to want to getfenv(2) and place the resulting Underscore instance there instead of returning it, though you have to actually coerce the contents into the table rather than replacing it outright. Hrm. Perhaps the right solution is to copy the contents of the generated Underscore instance into the table retrieved from getfenv, as a simple shallow copy followed by assigning the correct metatable may do the trick. Perhaps.
Lignum #3
Posted 10 September 2014 - 07:56 PM
dofile will do the job:

local Underscore = dofile("underscore") --# Get an Underscore instance.
local tbl = {
   ["Hello"] = "World",
   ["How"] = "are you today?"
}

--# Print tbl's values.
local values = Underscore.funcs.values(tbl)
print(textutils.serialise(values))

--# Output:
--# {
--#     "World",
--#     "are you today?"
--# }

However, I can't guarantee that this will work for all libraries.