iLua Core Functions

The core module keeps the functionality of the following standard Lua libraries:

  • Basic - all functions except dofile;
  • coroutine - replaced with co library to resolve conflicts with the multithreading engine;
  • package - not implemented, use require;
  • string - all funcions;
  • table - all funcions;
  • math - all funcions;
  • bit32 - all funcions;
  • io - replaced with file module;
  • os - replaced with similar functions and file module;
  • debug - not implemented, use graphical interface.

Global functions

after(t, func)
Execute function func in a new thread after t seconds. Returns the new (paused) thread.
clock()
Returns the time since system started, in seconds.
dump(...)
Prints the arguments in the output log in verbose form (tables are expanded).
exit([code])
Terminate the program.
include(filename)
Load and execute a Lua file in the current thread.
iskindof(v, name)
Checks if v is a userdata of type name or a type derived from it. Returns the actual type or nil.
keepalive()
Prevent the program from exiting when all threads have finished executing.
load(filename)
Load a Lua file and return it as a function.
objtype(v)
Returns the type of userdata v.
print(...)
Prints the arguments in the output log as strings.
require(name)
Load a DLL module.
sleep(t)
Pause the current thread for t seconds (can be a real number). Paused threads have higher priority so calling sleep(0) in a loop can block other threads.
wipe(t)
Removes all entries of table t (in-place).

System library

os.execute(cmd)
Execute a console (shell) command or run an external program.
os.execfile(filename[, action[, args]])
Execute an action (such as "open") on a file.
os.getenv(name)
os.setenv(name, value)
Get/set an environment variable.
os.environ
A table containing all environment variables.

Time library

Time structure contains the following fields:
  • year - 4 digits;
  • month - 1-12;
  • day - 1-31;
  • wday - day of week, 1-7 (7 for Sunday);
  • hour - 0-23;
  • min - 0-59;
  • sec - 0-59;
  • ms - 0-999.
time.time()
Time in seconds since January 1, 1601 (UTC).
time.tolocal([t])
Converts specified or current time (in seconds) to a time structure in local time zone.
time.toutc([t])
Converts specified or current time (in seconds) to a time structure in UTC time zone.
time.fromlocal(ts)
Converts local time structure to seconds as defined by time.time().
time.fromutc(ts)
Converts UTC time structure to seconds as defined by time.time().
time.format([fmt, [t]])
Formats local time (in seconds, a time structure, or current time) according to format specifications of strftime.
time.formatutc([fmt, [t]])
Formats UTC time (in seconds, a time structure, or current time) according to format specifications of strftime.

Thread library

thread.create(func, ...)
Run a function with given arguments in a new thread. Returns the thread object.
thread.current()
Returns the current thread object.
thread.lock()
thread.unlock()
Prevents the current thread from yielding. Prefer to use synchronization primitives instead.
thread.yield()
Pass control to the next thread in queue.
thread:state()
Returns true if the thread has finished executing or an error occured, false if it is still running.
thread:suspend()
thread:resume()
Suspend/resume a thread.
thread:terminate()
Terminate a thread.

Coroutine library

These functions replace the original coroutine module to avoid conflicts with the multithreading engine. The library is intended for making python-like generator functions.

co.create(func, ...)
Returns a coroutine created from function func, optionally saving arguments for the first call. The coroutine is initially suspended.
co.resume(state, ...)
Starts or resumes the couroutine. The arguments are either passed as the function arguments (appended after arguments specified in co.create, if any), or passed as return values from co.yield call. Returns the values passed to the next co.yield call or values returned from the function if it finishes execution. Returns nil immediately if the coroutine is already completed.
co.yield(...)
Yields a coroutine. The arguments are passed as return values from the co.resume call. Returns the values passed to the next co.resume call.
co.wrap(func, ...)
Creates a coroutine from func and optional initial arguments, and returns a wrapper function that is equivalent to calling co.resume on the coroutine.
co.status(state)
Returns coroutine status: "running" if called from inside the coroutine, "suspended" if the coroutine is in suspended state (has not been started yet or yielded, or if it is running in a different thread from the one that called co.status), "normal" if the coroutine is is active but not running (it called co.resume), or "dead" if it finished execution or an error occured.

Synchronization library

sync.wait(obj1, ..., objn[, t])
Wait until all objk:state() return true (obj can be any type with a state() method, such as sync.event or thread). Optionally specify a timeout in seconds. Returns true unless the timeout was exceeded.
sync.newsection()
Create a new sync.section object.
sync.newevent([state])
Create a new sync.event object with a specified initial state (default is false)
sync.section:enter()
sync.section:leave()
Enter/leave a section object. Only one thread can enter a section object at any given time. Calls can be nested within a single thread.
sync.event:state()
Returns current event state.
sync.event:set()
sync.event:reset()
Set/reset event state.

String library

string.concat(...)
Concatenate arguments as strings.
string.hex(str)
Convert a binary string to hexadecimal.
string.pad(str, len)
Return str resized to len characters, shortening the string or appending nil characters as required.
string.unpack(str)
Return a list of characters in a string.
utf8.byte(s[, i[, j]])
Return one or more characters of a UTF-8 string as integers (offsets as in string.byte, but are specified in UTF-8 characters).
utf8.char(...)
Convert a list of integers to UTF-8 characters and return their concatenation as a string.
utf8.fromoffset(str, pos)
Index of a UTF-8 character containing position pos.
utf8.len(str)
Return a string length in UTF-8 characters.
utf8.lower(str)
Return a UTF-8 string converted to lowercase.
utf8.offset(str, pos)
Get an offset of UTF-8 character at index pos.
utf8.reverse(str)
Return a reversed UTF-8 string.
utf8.sub(str, i[, j])
Return a substring with offsets specified in UTF-8 characters.
utf8.unpack(str)
Return a list of UTF-8 characters in a string.
utf8.upper(str)
Return a UTF-8 string converted to uppercase.
utf8.valid(str)
Check if a string is valid UTF-8.

Regular expression library

Library closely follows Python re module, using PCRE as underlying library. Newline can be any combination of linefeed and carriage return symbols. All strings are treated as UTF-8.
re.compile(pattern[, flags])
Return a regular expression object compiled from a pattern. Flags can be a combination (sum) of the following flags, defaulting to zero. For the following functions, strings can be specified in place of a compiled expression, in which case they will compiled with flags defaulting to re.LIST. Compilation results are cached (lifetime defined by garbage collection).
re.I
re.IGNORECASE
Regular expression is case-insensitive.
re.M
re.MULTILINE
^ and $ metacharacters match beginning/end of line instead of just the beginning/end of string.
re.S
re.DOTALL
Dot (.) metacharacter matches all characters, including newline (by default, newline is not matched).
re.X
re.VERBOSE
Unescaped whitespace in patterns is ignored, and patterns may contain comments spanning from a # symbol until the end of line.
re.L
re.LIST
Matching functions will return a list of captures (including the entire match) instead of a match object.
re.search(pattern, string[, flags])
Return the first match of a pattern in a string, or nil if no matches were found.
re.match(pattern, string[, flags])
Match zero or more characters at the beginning of string; return a match object or nil.
re.split(pattern, string[, maxsplit[, flags]])
Split a string by non-empty occurences of pattern, return a list of strings. If pattern matches the beginning/end of string, the list will start/end with an empty string. If pattern contains capturing groups, they will be included in the list. Optionally, maxsplit limits the number of splits made.
re.findall(pattern, string[, flags])
Return a list of all occurences of pattern in string. If the pattern conains captures, return captures instead of the entire matches. If more than one capture group is present, return a list of tables instead.
re.finditer(pattern, string[, flags])
Return an iterator pair for use in a for loop that will iterate through all matches of pattern in the string. Each iteration returns a match object or a list of captures if re.LIST flag was specified.
re.sub(pattern, repl, string[, count[, flags]])
Replace all occurences of pattern in string with repl, which can be either a string or a function. The repl string may contain references to capture groups in the form of \4 or \g<name>. Optionally, up to count replacements are made. Returns the resulting string and number of replacements made.
re.escape(string)
Returns string with all non-alphanumerics escaped (resulting string is a pattern equivalent to searching for string).
re.expand(string, match)
Return string with capture references expanded according to a given match object or a list of captures.
re.prog:search(string[, pos])
Same as re.search but allows specifying starting position to search from.
re.prog:match(string[, pos])
Same as re.match but allows specifying starting position to match from.
re.prog:split(string[, maxsplit])
Same as re.split.
re.prog:findall(string[, pos])
Same as re.findall but allows specifying starting position to search from.
re.prog:finditer(string[, pos])
Same as re.finditer but allows specifying starting position to search from.
re.prog:sub(repl, string[, count])
Same as re.sub.
re.prog.flags
A combination of flags passed to re.compile and inline flags in the pattern.
re.prog.groups
Number of capture groups in the pattern.
re.prog.groupindex
A table mapping named groups to group indices.
re.prog.pattern
Original pattern passed to re.compile.
re.match:expand(string)
Returns string with capture references expanded.
re.match:group(...)
Returns a list of strings corresponding to specified groups (by index or by name). If no groups are specified, returns the whole match.
re.match:groups([default])
Returns the list of all captured groups; if a group wasn't matched, its value is replaced with default.
re.match:groupdict([default])
Returns a table mapping all named groups to their values. If a group wasn't matched, its value is replaced with default.
re.match:left([group])
re.match:right([group])
Returns the index of the first/last character in a matched group, or -1 if the group wasn't matched.
re.match:span([group])
Returns the indices of the first and last character in a matched group, or -1,-1 if the group wasn't matched.
re.match.pos
The original position passed to re.prog:search() or re.prog:match().
re.match.re
The expression object used for matching.
re.match.string
The string the match was found in.

Stream library

stream:read(...)
Receives a list of lengths, reads a binary chunk for each specified length and returns them as strings.
stream:write(...)
Writes a list of strings as binary chunks.
stream:seek(pos[, orig])
Changes the current position to offset pos from orig, which can be stream.SEEK_SET (absolute position), stream.SEEK_CUR (current position) or stream.SEEK_END (end of data). String values can be used instead: "set", "cur", "end".
stream:tell()
Returns the current position.
stream:size()
Returns the total stream size in bytes.
stream:resize()
Resize the stream, trimming contents or appending zeroes.
stream:eof()
Checks if the current position is at the end of stream (equivalent to tell() >= size()).
stream:read8()
stream:read16([big])
stream:read32([big])
stream:readfloat([big])
stream:readdouble([big])
Reads a 8/16/32-bit integer, a 32-bit float or a 64-bit double value, optionally swapping byte order.
stream:write8(val)
stream:write16(val[, big])
stream:write32(val[, big])
stream:writefloat(val[, big])
stream:writedouble(val[, big])
Writes a 8/16/32-bit integer, a 32-bit float or a 64-bit double value, optionally swapping byte order.
stream:readstr()
stream:writestr(str)
Read/write a null-terminated string.
stream:readbuf()
Read a 32-bit integer N, followed by N bytes of raw data, and returns the data as string.
stream:writebuf(buf)
Write a buffer/string as 32-bit integer length followed by contents as raw data.
stream:serialize(...)
stream:deserialize([count])
Writen given Lua objects in binary form/read count objects. Accepts a nil, number, string, Lua function or a table containing listed types. Supports recursive tables and metatables. Note that it does not check Lua functions for validity, do not use on untrusted data (see Lua manual).
stream:copy(source[, length])
Copy up to length bytes from source stream (or all remaining bytes).
stream:printf(fmt, ...)
Format data as text according to format specifiers.
stream:getline()
Returns a line of text, not including the newline character.
stream:lines()
Returns an interable for use in for loop, iterating over all lines in a stream.
stream:flush()
Flush all buffered operations.
stream.SEEK_SET
stream.SEEK_CUR
stream.SEEK_END
Origin specifiers for use in stream.seek.
buffer.create([str])
Creates a buffer object from a string. Buffers are a mutable version of string that is derived from stream. Buffers support the concatenation (..) and length (#) operations.
buffer:tostring()
Returns the string representation of a buffer.

RivSoft.net by Riv (riv@rivsoft.net) - All rights reserved