Skip to content

NonStandardLuaLibs

Vexatos edited this page Jul 13, 2014 · 9 revisions

For those that don't like images: the wiki has moved to a new place, http://ocdoc.cil.li/.
This wiki will no longer be updated.


Most of Lua's standard libraries are available, although some of them may only be available partially or as re-implementations, meaning behavior may differ slightly. Please see the Lua 5.2 manual for documentation on the standard libraries.

This page tries to list all differences to these standard libraries.

Basic Functions

The original functions from the base library are available with the following differences.

  • collectgarbage is not available.
  • dofile and loadfile have been reimplemented to load files from mounted file system components (they use the filesystem API / reimplemented io library).
  • load can only be used to load text, no binary/compiled code by default. Note that bytecode loading can be enabled in the config, but is really not recommended, since it is a major security risk.
  • print has been reimplemented to use the reimplemented io.stdout which uses term.write.

Coroutine Manipulation

The original functions from the coroutine library are available with no observable differences.

Note that the coroutine.resume and coroutine.yield implementations exposed to user code are wrappers that take care of aborting code that does not yield after a certain time (see config), and to allow differentiating system yields from user yields (system yields "bubble", for example this is used for the shutdown command and component API calls). This should not be noticeable from user code, however. If it is, it should be considered a bug.

Modules

The package module got a reimplementation for OpenComputers. It should operate the same as the original, but is lacking the following functions:

  • package.config is missing and not used.
  • package.cpath is missing and not used.
  • package.loadlib is not implemented

The latter two are missing because it is impossible to load C code in OpenComputers. String Manipulation

The original functions from the string library are available without alterations.

Note that the functions of the GPU API work on UTF-8 strings, and, by extension, so does term.write and print. To help you work work with UTF-8 strings, there is an additional library, the Unicode API.

Table Manipulation

The original functions from the table library are available without alteration.

Mathematical Functions

The original functions from the math library are available with minor alterations.

  • math.random uses a separate java.util.Random instance for each Lua state / computer.
  • math.randomseed is applied to that instance.

Bitwise Operations

The original functions from the bit32 library are available without alteration.

Input and Output Facilities

The original functions from the io library have been reimplemented for the most part, and work on mounted filesystem components and term.read / term.write for the standard input / output.
For the most part these should be functionally equivalent to the standard Lua implementation. They may return error strings that differ from vanilla Lua, though, but since that uses C library errors for the most part, which are platform dependent, it's not a very good idea to use these for program logic anyway.

  • io.popen is not available. If someone could be bothered to reimplement that using coroutines that'd be pretty cool.
  • io.open does not support the + modes, i.e. it only supports r, w, a, rb, wb and ab. Binary mode in this implementation determines whether to use UTF-8 aware string functions or not, when reading a number of chars (e.g. via f:read(42)).
  • io.stdin reads data using term.read.
  • io.stdout writes using term.write.
  • io.stderr also writes using term.write, but tries to do so in a red color, if supported by the primary GPU and Screen.
  • io.read does not support the *n format at this point.

Operating System Facilities

The original functions from the os library have been partially reimplemented.

  • os.clock has been reimplemented to return the approximate CPU time, meaning the time the computer has actually been running in an executor thread. This is not the same as the time the computer has been running, for that see [[computer.uptime|API/Computer]].
  • os.date has been reimplemented to use ingame time and supports most formats.
  • os.execute has been reimplemented to start programs from a mounted filesystem via shell.execute. The specified string is parsed the same as commands entered in the shell.
  • os.exit throws an error to try and terminate the current coroutine.
  • os.setenv is added to set shell variables from Lua.
  • os.remove is an alias for filesystem.remove.
  • os.rename is an alias for filesystem.rename.
  • os.setlocale is not available.
  • os.time has been reimplemented to return the ingame time since the world has been created.
    Note that this time is in "in-game seconds". To get the number of game ticks since the world was created, multiply it with 1000/60/60 (since there are 24000 ticks in a day) and subtract 6000. This offset of 6000 is not arbitrary, it ensures that 6 o'clock AM is actually that. Minecraft somehow thinks six o'clock in the morning is zero - probably because that's "when" a new game starts...
  • os.tmpname has been reimplemented to generate an unused name in the /tmp mount.

One additional function has been added:

  • os.sleep(seconds: number) which allows pausing a script for the specified amount of time. Note that signals will still be processed by event handlers while the sleep is active, i.e. you cannot pull signals that were accumulated during the sleep after it ended, since no signals will remain in the queue (or at least not all of them).

Some new functions that kind of fall into this category are available in the computer API.

Debug

Only debug.traceback is implemented.