| File lib/felix/lib/lua.flx |
GODI Package
apps-felix |
#line 5 "lpsrc/flx_luademo.ipk"
include 'std';
header lua_h = '#include "flx_lua.h"';
header lua_h = '#include "flx_lauxlib.h"';
header lua_h = '#include "flx_lualib.h"';
publish "Interface to Lua 5.0 CAPI"
module Lua
{
requires lua_h;
open C_hack;
comment "Event masks";
const LUA_MASKCALL : int; // 0b0001
const LUA_MASKRET : int; // 0b0010
const LUA_MASKLINE : int; // 0b0100
const LUA_MASKCOUNT : int; // 0b1000
comment "Event codes";
const LUA_HOOKCALL : int; // 0
const LUA_HOOKRET : int; // 1
const LUA_HOOKLINE : int; // 2
const LUA_HOOKCOUNT : int; // 3
const LUA_HOOKTAILRET : int; // 4
comment "Lua basic typecodes";
const LUA_TNONE : int; // -1
const LUA_TNIL : int; // 0
const LUA_TBOOLEAN : int; // 1
const LUA_TLIGHTUSERDATA : int; // 2
const LUA_TNUMBER : int; // 3
const LUA_TSTRING : int; // 4
const LUA_TTABLE : int; // 5
const LUA_TFUNCTION : int; // 6
const LUA_TUSERDATA : int; // 7
const LUA_TTHREAD : int; // 8
comment "error codes for `lua_load' and `lua_pcall'";
const LUA_ERRRUN : int; // 1
const LUA_ERRFILE : int; // 2
const LUA_ERRSYNTAX : int; // 3
const LUA_ERRMEM : int; // 4
const LUA_ERRERR : int; // 5
const LUA_IDSIZE : int;
const LUA_NUMBER_FMT : charp;
const LUA_NUMBER_SCAN : charp;
const LUA_REFNIL : int;
const LUA_NOREF : int;
const LUA_MINSTACK : int;
const LUA_GLOBALSINDEX :int;
const LUA_REGISTRYINDEX : int;
const LUA_MULTRET :int;
comment "Lua version";
const LUA_AUTHORS :charp;
const LUA_COPYRIGHT :charp;
const LUA_VERSION :charp;
//CSTRUCTS
cstruct luaL_reg {
name: cptr[char];
func: lua_CFunction;
}
cstruct luaL_Buffer {
p: ptr[char];
lvl: int;
L: lua_t;
buffer: ptr[char];
}
//C FUNCTION POINTER TYPES
header '''typedef int (*lua_lua_h_cft_3)(lua_State *, void const *, size_t, void *);''';
type lua_lua_h_cft_3 = 'lua_lua_h_cft_3';
header '''typedef void (*lua_lua_h_cft_4)(lua_State *, lua_Debug *);''';
type lua_lua_h_cft_4 = 'lua_lua_h_cft_4';
ctypes lua_CFunction;
type lua_t = "lua_State*";
//TYPE ALIASES
typedef lua_Chunkwriter = lua_lua_h_cft_3;
typedef lua_Hook = lua_lua_h_cft_4;
typedef _struct_lua_Debug = lua_Debug;
typedef lua_Number = double;
//#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
//#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
//#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
//#define lua_getglobal(L,s) \
//#define lua_setglobal(L,s) \
//#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
//#define lua_pushliteral(L, s) \
comment "3.1- States";
fun lua_open: 1 -> lua_t;
proc lua_close: lua_t;
comment "3.2- The stack and indicies";
publish "Reserve space on the Lua stack for subsequent operations"
fun lua_checkstack: lua_t * int -> int;
publish "Get the index of the top of the lua stack"
fun lua_gettop: lua_t -> int;
comment "3.3 - Generic stack operations";
publish "Copy element onto top of stack"
proc lua_pushvalue: lua_t * int;
publish "Move top element into stack"
proc lua_insert: lua_t * int;
publish "Discard top element"
proc lua_pop: lua_t * int;
publish "Remove element from stack"
proc lua_remove: lua_t * int;
publish "Replace element with top of stack"
proc lua_replace: lua_t * int;
comment "3.4 - Querying the Stack";
fun lua_isnoneornil: lua_t * int -> bool;
fun lua_isnone: lua_t * int -> bool;
fun lua_isboolean: lua_t * int -> bool;
fun lua_isnil: lua_t * int -> bool;
fun lua_islightuserdata: lua_t * int -> bool;
fun lua_istable: lua_t * int -> bool;
fun lua_isfunction: lua_t * int -> bool;
fun lua_iscfunction: lua_t * int -> int;
fun lua_isnumber: lua_t * int -> int;
fun lua_isstring: lua_t * int -> int;
fun lua_isuserdata: lua_t * int -> int;
publish "Typecode of top element"
fun lua_type: lua_t * int -> int;
publish "String of typecode"
fun lua_typename: lua_t * int -> cptr[char];
publish "Equality without metamethods"
fun lua_rawequal: lua_t * int * int -> int;
publish "Equality"
fun lua_equal: lua_t * int * int -> int;
publish "Less Than"
fun lua_lessthan: lua_t * int * int -> int;
comment "3.5 - Getting Values from the Stack";
fun lua_toboolean: lua_t * int -> int;
fun lua_tonumber: lua_t * int -> double;
fun lua_tocfunction: lua_t * int -> lua_CFunction;
fun lua_topointer: lua_t * int -> caddress;
fun lua_tostring: lua_t * int -> cptr[char];
fun lua_tothread: lua_t * int -> lua_t;
fun lua_touserdata: lua_t * int -> address;
fun lua_strlen: lua_t * int -> size;
comment "3.6 Pushing Values onto the Stack";
proc lua_pushboolean: lua_t * int;
proc lua_pushcfunction: lua_t * lua_CFunction;
proc lua_pushcclosure: lua_t * lua_CFunction * int;
proc lua_pushlightuserdata: lua_t * address;
proc lua_pushlstring: lua_t * cptr[char] * size;
proc lua_pushnil: lua_t;
proc lua_pushnumber: lua_t * double;
proc lua_pushstring: lua_t * cptr[char];
publish " Push formatted string (like sprintf)"
fun lua_pushfstring[t]: t -> cptr[char] = 'lua_pushfstring($a)';
publish "Push formatted string (like vsprintf)"
fun lua_pushvfstring: lua_t * cptr[char] * va_list -> cptr[char];
publish "Concatenate strings on stack"
proc lua_concat: lua_t * int;
comment "3.7- GC control";
proc lua_setgcthreshold: lua_t * int;
fun lua_getgccount: lua_t -> int;
fun lua_getgcthreshold: lua_t -> int;
comment "3.8- Userdata";
fun lua_newuserdata: lua_t * size -> address;
comment "3.9- Metatables";
fun lua_getmetatable: lua_t * int -> int;
fun lua_setmetatable: lua_t * int -> int;
comment "3.10- Loading chunks";
publish "Raw C chunkreader function type"
type lua_Chunkreader_p = "lua_Chunkreader*";
publish "Load chunk using C chunkreader function"
fun lua_load: lua_t * lua_Chunkreader_p * address * cptr[char] -> int;
comment """
Felix callback wrapper for lua chunkreader type.
This target callback type just returns a string.
""";
// the wrapper for the Felix callback, has type lua_Chunkreader
header fcbw_chrd_dcl =
'''
char const *_fcbw_chrd(lua_State *a1, void *a2, size_t *a3);
''';
body fcbw_chrd_def =
'''
char const *_fcbw_chrd(lua_State *a1, void *a2, size_t *a3)
{
// cast client data to exported Felix function type and apply it
// the result is a string
std::string result = ((_fcbt_chrd_t)a2)->apply(a1);
// get the size of the string and store it
std::size_t n = (size_t)result.size();
*a3 = n;
// we take an empty string as end of data
if (n == 0) return NULL;
// malloc the string contents so Lua can own it
char const * data = (char const*)std::malloc(n);
std::memcpy(data,result.data(),n);
// return the data to Lua
return data;
}
'''
requires fcbw_chrd_dcl;
// make the C wrapper available to Felix
const _fcbw_chrd: lua_Chunkreader_p = "_fcbw_chrd"
requires fcbw_chrd_def;
// The wrapper for load accepts a Felix callback
// It calls lua_load with the fixed callback wrapper
// passing the Felix function in the client data pointer
publish "Felix wrapper for lua_load"
fun wrapper_lua_load
(
a1: lua_t,
a2: lua_t -> string,
a4: string
): int=
{
return lua_load(a1, _fcbw_chrd, C_hack::cast[address]a2, C_hack::enconst (cstr a4));
}
comment "3.11- Manipulating Tables";
proc lua_gettable: lua_t * int;
proc lua_newtable: lua_t;
proc lua_rawget: lua_t * int;
proc lua_settable: lua_t * int;
proc lua_rawset: lua_t * int;
fun lua_next: lua_t * int -> int;
comment "3.12- Manipulating Environments";
proc lua_getfenv: lua_t * int;
fun lua_setfenv: lua_t * int -> int;
comment "3.13- Using Tables as Arrays";
proc lua_rawgeti: lua_t * int * int;
proc lua_rawseti: lua_t * int * int;
comment "3.14- Calling Functions";
proc lua_call: lua_t * int * int;
comment "3.15- Protected Calls";
fun lua_pcall: lua_t * int * int * int -> int;
comment "3.16- Defining C functions";
proc lua_register: lua_t * int * lua_CFunction;
comment "3.17- Defining C Closures";
proc lua_pushcclosure: lua_t * lua_CFunction* int;
comment "3.18- Registry";
comment "3.19- Error Handling in C";
fun lua_atpanic: lua_t * lua_CFunction -> lua_CFunction;
fun lua_cpcall: lua_t * lua_CFunction * address -> int;
fun lua_error: lua_t -> int;
comment "3.20- Threads";
fun lua_newthread: lua_t -> lua_t;
fun lua_resume: lua_t * int -> int;
fun lua_yield: lua_t * int -> int;
publish "Move n stack items from thread 1 to thread 2"
proc lua_xmove: lua_t * lua_t * int;
comment "4- The Debug Interface";
comment "4.1 Stack and Function Information";
fun lua_getstack: lua_t * int * ptr[lua_Debug] -> int;
cstruct lua_Debug {
event: int;
name: cptr[char];
namewhat: cptr[char];
what: cptr[char];
source: cptr[char];
currentline: int;
nups: int;
linedefined: int;
short_src: ptr[char];
i_ci: int;
}
fun lua_getinfo: lua_t * cptr[char] * ptr[lua_Debug] -> int;
comment "4.2- Manipulating Local Variables and Upvalues";
fun lua_getlocal: lua_t * cptr[lua_Debug] * int -> cptr[char];
fun lua_setlocal: lua_t * cptr[lua_Debug] * int -> cptr[char];
fun lua_getupvalue: lua_t * int * int -> cptr[char];
fun lua_setupvalue: lua_t * int * int -> cptr[char];
comment "4.3- Hooks";
fun lua_sethook: lua_t * lua_lua_h_cft_4 * int * int -> int;
fun lua_gethook: lua_t -> lua_Hook;
fun lua_gethookmask: lua_t -> int;
fun lua_gethookcount: lua_t -> int;
comment "open standard libraries";
fun luaopen_base: lua_t -> int;
fun luaopen_debug: lua_t -> int;
fun luaopen_io: lua_t -> int;
fun luaopen_loadlib: lua_t -> int;
fun luaopen_math: lua_t -> int;
fun luaopen_string: lua_t -> int;
fun luaopen_table: lua_t -> int;
comment "Verson";
fun lua_version: 1 -> cptr[char];
comment "Undocumented";
publish "Reset top of stack position"
proc lua_settop: lua_t * int;
fun lua_dump: lua_t * lua_lua_h_cft_3 * address -> int;
fun lua_pushupvalues: lua_t -> int;
//#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
//#define lua_boxpointer(L,u) \
fun lua_upvalueindex:int->int;
comment "LuaL";
fun luaL_argerror: lua_t * int * cptr[char] -> int;
fun luaL_callmeta: lua_t * int * cptr[char] -> int;
proc luaL_addlstring: ptr[luaL_Buffer] * cptr[char] * size;
proc luaL_addstring: ptr[luaL_Buffer] * cptr[char];
proc luaL_addvalue: ptr[luaL_Buffer];
proc luaL_buffinit: lua_t * ptr[luaL_Buffer];
fun luaL_error[t]: t -> int = 'luaL_error($a)';
fun luaL_findstring: cptr[char] * cptr[cptr[char]] -> int;
fun luaL_getmetafield: lua_t * int * cptr[char] -> int;
fun luaL_getn: lua_t * int -> int;
fun luaL_loadbuffer: lua_t * cptr[char] * size * cptr[char] -> int;
fun luaL_loadfile: lua_t * cptr[char] -> int;
fun luaL_newmetatable: lua_t * cptr[char] -> int;
fun luaL_optlstring: lua_t * int * cptr[char] * ptr[size] -> cptr[char];
fun luaL_optnumber: lua_t * int * double -> double;
fun luaL_prepbuffer: ptr[luaL_Buffer] -> ptr[char];
fun luaL_ref: lua_t * int -> int;
fun luaL_typerror: lua_t * int * cptr[char] -> int;
comment "Integrity Check";
fun luaL_checklstring: lua_t * int * ptr[size] -> cptr[char];
fun luaL_checknumber: lua_t * int -> double;
fun luaL_checkudata: lua_t * int * cptr[char] -> address;
proc luaL_checkany: lua_t * int;
proc luaL_checkstack: lua_t * int * cptr[char];
proc luaL_checktype: lua_t * int * int;
proc luaL_getmetatable: lua_t * cptr[char];
proc luaL_openlib: lua_t * cptr[char] * cptr[luaL_reg] * int;
proc luaL_pushresult: ptr[luaL_Buffer];
proc luaL_setn: lua_t * int * int;
proc luaL_unref: lua_t * int * int;
proc luaL_where: lua_t * int;
}