Skip to content

Commit fe539a5

Browse files
author
Apprentice Alchemist
committed
Initial impl, hxcpp more or less working
1 parent 93e64d8 commit fe539a5

File tree

5 files changed

+383
-0
lines changed

5 files changed

+383
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lua51.*
2+
out/*
3+
Main.hx
4+
script.lua

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "LuaJIT"]
2+
path = LuaJIT
3+
url = https://github.com/LuaJIT/LuaJIT

LuaJIT

Submodule LuaJIT added at e957737

haxelib.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "hxlua",
3+
"classPath": "src",
4+
"license": "MIT",
5+
"contributors": ["Apprentice-Alchemist"],
6+
"dependencies": {},
7+
"description": "Haxe bindings for LuaJIT",
8+
"releasenote": "N/A",
9+
"version": "0.0.0",
10+
"tags": ["lua","luajit","native"],
11+
"url": "https://github.com/Apprentice-Alchemist/hxlua"
12+
}

src/Lua.hx

Lines changed: 363 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,363 @@
1+
package;
2+
3+
private abstract IntBool(Int) from Int to Int {
4+
@:to function toBool():Bool {
5+
return this != 0;
6+
}
7+
8+
@:from static function fromBool(b:Bool):IntBool {
9+
return b ? 1 : 0;
10+
}
11+
}
12+
13+
#if hl
14+
abstract State(hl.Abstract<"lua_State">) {}
15+
16+
private abstract CString(hl.Bytes) from hl.Bytes to hl.Bytes {
17+
@:from static inline function fromString(s:String) {
18+
return @:privateAccess s.toUTF8();
19+
}
20+
21+
@:to inline function toString():String {
22+
return @:privateAccess String.fromUTF8(this);
23+
}
24+
}
25+
26+
private typedef Ref<T> = hl.Ref<T>;
27+
private typedef Bytes = hl.Bytes;
28+
private typedef LuaNumber = Float;
29+
private typedef LuaInteger = Int;
30+
#elseif cpp
31+
@:include("lua.hpp")
32+
@:native("lua_State *")
33+
extern class State {}
34+
35+
private abstract CString(cpp.ConstCharStar) from cpp.ConstCharStar to cpp.ConstCharStar {
36+
@:from static inline function fromString(s:String):CString {
37+
return (s : cpp.ConstCharStar);
38+
}
39+
40+
@:to inline function toString():String {
41+
return (this : String);
42+
}
43+
}
44+
45+
private typedef Ref<T> = cpp.Star<T>;
46+
47+
private abstract Bytes(cpp.Star<Void>) {
48+
@:from static function fromBytes(b:haxe.io.Bytes) {
49+
return cast @:privateAccess b.b;
50+
}
51+
}
52+
53+
private typedef LuaNumber = Float;
54+
private typedef LuaInteger = cpp.Int64;
55+
#end
56+
private typedef Reader = cpp.Callable<(state:State, size:Ref<Int>) -> Bytes>;
57+
private typedef Writer = cpp.Callable<(state:State, data:Bytes, size:Int) -> Int>;
58+
typedef LuaCFunction = cpp.Callable<State->Int>;
59+
60+
enum abstract LuaType(Int) {
61+
var LUA_TNONE = -1;
62+
var LUA_TNIL = 0;
63+
var LUA_TBOOLEAN = 1;
64+
var LUA_TLIGHTUSERDATA = 2;
65+
var LUA_TNUMBER = 3;
66+
var LUA_TSTRING = 4;
67+
var LUA_TTABLE = 5;
68+
var LUA_TFUNCTION = 6;
69+
var LUA_TUSERDATA = 7;
70+
var LUA_TTHREAD = 8;
71+
}
72+
73+
enum abstract ThreadStatus(Int) {
74+
var LUA_SOMETHING = -1;
75+
var LUA_OK;
76+
var LUA_YIELD;
77+
var LUA_ERRRUN;
78+
var LUA_ERRSYNTAX;
79+
var LUA_ERRMEM;
80+
var LUA_ERRERR;
81+
}
82+
83+
enum abstract GcOptions(Int) {
84+
var LUA_GCSTOP = 0;
85+
var LUA_GCRESTART = 1;
86+
var LUA_GCCOLLECT = 2;
87+
var LUA_GCCOUNT = 3;
88+
var LUA_GCCOUNTB = 4;
89+
var LUA_GCSTEP = 5;
90+
var LUA_GCSETPAUSE = 6;
91+
var LUA_GCSETSTEPMUL = 7;
92+
var LUA_GCISRUNNING = 9;
93+
}
94+
95+
#if hl
96+
@:hlNative("lua")
97+
#elseif cpp
98+
@:include("lua.hpp")
99+
@:buildXml("
100+
<files id='haxe'>
101+
<compilerflag value='-I${haxelib:hxlua}/LuaJIT/src'/>
102+
</files>
103+
<target id='haxe'>
104+
<libpath name='${haxelib:hxlua}/lib/${BINDIR}'/>
105+
<lib base='lua51' if='windows'/>
106+
<lib base='luajit-5.1' unless='windows'/>
107+
</target>
108+
")
109+
#end
110+
extern class Lua {
111+
static inline var MINSTACK = 20;
112+
static inline var MULTRET = -1;
113+
static inline var REGISTRYINDEX = -10000;
114+
static inline var ENVIRONINDEX = -10001;
115+
static inline var GLOBALSINDEX = -10002;
116+
static inline function upvalueindex(i:Int):Int {
117+
return GLOBALSINDEX - i;
118+
}
119+
@:native("luaL_newstate")
120+
static function newstate():State;
121+
@:native("lua_close")
122+
static function close(L:State):Void;
123+
@:native("lua_newthread")
124+
static function newthread(L:State):State;
125+
@:native("lua_atpanic")
126+
static function atpanic(L:State, panicf:LuaCFunction):LuaCFunction;
127+
@:native("lua_gettop")
128+
static function gettop(L:State):Int;
129+
@:native("lua_settop")
130+
static function settop(L:State, idx:Int):Void;
131+
@:native("lua_pushvalue")
132+
static function pushvalue(L:State, idx:Int):Void;
133+
@:native("lua_remove")
134+
static function remove(L:State, idx:Int):Void;
135+
@:native("lua_insert")
136+
static function insert(l:State, idx:Int):Void;
137+
@:native("lua_replace")
138+
static function replace(L:State, idx:Int):Void;
139+
@:native("lua_checkstack")
140+
static function checkstack(L:State, sz:Int):Int;
141+
@:native("lua_xmove")
142+
static function xmove(from:State, to:State, n:Int):Void;
143+
@:native("lua_isnumber")
144+
static function isnumber(L:State, idx:Int):IntBool;
145+
@:native("lua_isstring")
146+
static function isstring(L:State, idx:Int):IntBool;
147+
@:native("lua_iscfunction")
148+
static function iscfunction(L:State, idx:Int):IntBool;
149+
@:native("lua_isuserdata")
150+
static function isuserdata(L:State, idx:Int):IntBool;
151+
@:native("lua_type")
152+
static function type(L:State, idx:Int):LuaType;
153+
@:native("lua_typename")
154+
static function typename(L:State, tp:Int):CString;
155+
@:native("lua_equal")
156+
static function equal(L:State, idx1:Int, idx2:Int):IntBool;
157+
@:native("lua_rawequal")
158+
static function rawequal(L:State, idx1:Int, idx2:Int):IntBool;
159+
@:native("lua_lessthan")
160+
static function lessthan(L:State, idx1:Int, idx2:Int):IntBool;
161+
@:native("lua_tonumber")
162+
static function tonumber(L:State, idx:Int):LuaNumber;
163+
@:native("lua_tointeger")
164+
static function tointeger(L:State, idx:Int):LuaInteger;
165+
@:native("lua_toboolean")
166+
static function toboolean(L:State, idx:Int):IntBool;
167+
@:native("lua_tolstring")
168+
static function tolstring(L:State, idx:Int, len:Ref<#if cpp cpp.SizeT #elseif hl Int #end>):CString;
169+
@:native("lua_objlen")
170+
static function objlen(L:State, idx:Int):#if cpp cpp.SizeT #else Int #end;
171+
@:native("lua_tocfunction")
172+
static function tocfunction(L:State, idx:Int):LuaCFunction;
173+
@:native("lua_touserdata")
174+
static function touserdata<T:Dynamic>(L:State, idx:Int):T;
175+
@:native("lua_tothread")
176+
static function tothread(L:State, idx:Int):State;
177+
@:native("lua_topointer")
178+
static function topointer<T:Dynamic>(L:State, idx:Int):T;
179+
@:native("lua_pushnil")
180+
static function pushnil(L:State):Void;
181+
@:native("lua_pushnumber")
182+
static function pushnumber(L:State, n:LuaNumber):Void;
183+
@:native("lua_pushinteger")
184+
static function pushinteger(L:State, n:LuaInteger):Void;
185+
@:native("lua_pushlstring")
186+
static function pushlstring(L:State, s:Bytes, len:#if cpp cpp.SizeT #else Int #end):Void;
187+
@:native("lua_pushstring")
188+
static function pushstring(L:State, s:CString):Void;
189+
@:native("lua_pushcclosure")
190+
static function pushcclosure(L:State, fn:LuaCFunction, n:Int):Void;
191+
@:native("lua_pushlightuserdata")
192+
static function pushlightuserdata<T:Dynamic>(L:State, p:T):Void;
193+
@:native("lua_pushthread")
194+
static function pushthread(L:State):Int;
195+
@:native("lua_gettable")
196+
static function gettable(L:State, idx:Int):Void;
197+
@:native("lua_getfield")
198+
static function getfield(L:State, idx:Int, name:CString):Void;
199+
@:native("lua_rawget")
200+
static function rawget(L:State, idx:Int):Void;
201+
@:native("lua_rawgeti")
202+
static function rawgeti(L:State, idx:Int, n:Int):Void;
203+
@:native("lua_createtable")
204+
static function createtable(L:State, narr:Int, nrec:Int):Void;
205+
@:native("lua_newuserdata")
206+
static function newuserdata<T:Dynamic>(L:State, sz:#if cpp cpp.SizeT #else Int #end):T;
207+
@:native("lua_getmetatable")
208+
static function getmetatable(L:State, objindex:Int):Int;
209+
@:native("lua_getfenv")
210+
static function getfenv(L:State, idx:Int):Void;
211+
@:native("lua_settable")
212+
static function settable(L:State, idx:Int):Void;
213+
@:native("lua_setfield")
214+
static function setfield(L:State, idx:Int, k:CString):Void;
215+
@:native("lua_rawset")
216+
static function rawset(L:State, idx:Int):Void;
217+
@:native("lua_rawseti")
218+
static function rawseti(L:State, idx:Int, n:Int):Void;
219+
@:native("lua_setmetatable")
220+
static function setmetatable(L:State, objindex:Int):Void;
221+
@:native("lua_setfenv")
222+
static function setfenv(L:State, idx:Int):Int;
223+
224+
@:native("lua_call")
225+
static function call(L:State, nargs:Int, nresults:Int):Void;
226+
@:native("lua_pcall")
227+
static function pcall(L:State, nargs:Int, nresults:Int, errfunc:Int):Int;
228+
@:native("lua_cpcall")
229+
static function cpcall<T:Dynamic>(L:State, func:LuaCFunction, ud:T):Int;
230+
@:native("lua_load")
231+
static function load(L:State, reader:Reader, dt:Bytes, chunkname:CString):Int;
232+
@:native("lua_dump")
233+
static function dump(L:State, writer:Writer, data:Bytes):Int;
234+
@:native("lua_yield")
235+
static function yield(L:State, nresults:Int):ThreadStatus;
236+
@:native("lua_resume")
237+
static function resume(L:State, narg:Int):ThreadStatus;
238+
@:native("lua_status")
239+
static function status(L:State):ThreadStatus;
240+
@:native("lua_gc")
241+
static function gc(L:State, what:Int, data:Int):Int;
242+
@:native("lua_error")
243+
static function error(L:State):Int;
244+
@:native("lua_next")
245+
static function next(L:State, idx:Int):Int;
246+
@:native("lua_concat")
247+
static function concat(L:State, n:Int):Void;
248+
249+
static inline function pop(L:State, n:Int):Void {
250+
settop(L, -n - 1);
251+
}
252+
253+
static inline function newtable(L:State):Void {
254+
createtable(L, 0, 0);
255+
}
256+
257+
static inline function register(L:State, n:CString, f:LuaCFunction):Void {
258+
pushcfunction(L, (f));
259+
setglobal(L, (n));
260+
}
261+
262+
static inline function strlen(L:State, i:Int):Int {
263+
return objlen(L, i);
264+
}
265+
static inline function isfunction(L:State, n:Int):Bool {
266+
return type(L, (n)) == LUA_TFUNCTION;
267+
}
268+
static inline function istable(L:State, n:Int):Bool {
269+
return type(L, (n)) == LUA_TTABLE;
270+
}
271+
static inline function islightuserdata(L:State, n:Int):Bool {
272+
return type(L, (n)) == LUA_TLIGHTUSERDATA;
273+
}
274+
static inline function isnil(L:State, n:Int):Bool {
275+
return type(L, (n)) == LUA_TNIL;
276+
}
277+
static inline function isboolean(L:State, n:Int):Bool {
278+
return type(L, (n)) == LUA_TBOOLEAN;
279+
}
280+
static inline function isthread(L:State, n:Int):Bool {
281+
return type(L, (n)) == LUA_TTHREAD;
282+
}
283+
static inline function isnone(L:State, n:Int):Bool {
284+
return type(L, (n)) == LUA_TNONE;
285+
}
286+
static inline function isnoneornil(L:State, n:Int):Bool {
287+
return (cast type(L, (n)) : Int) <= 0;
288+
}
289+
290+
static inline function pushliteral(L:State, s:String):Void {
291+
pushlstring(L, haxe.io.Bytes.ofString(s), s.length);
292+
}
293+
294+
static inline function pushcfunction(L:State, f:LuaCFunction):Void {
295+
pushcclosure(L, f, 0);
296+
}
297+
298+
static inline function setglobal(L:State, s:CString):Void {
299+
setfield(L, GLOBALSINDEX, s);
300+
}
301+
302+
static inline function getglobal(L:State, s:CString):Void {
303+
getfield(L, GLOBALSINDEX, s);
304+
}
305+
306+
static inline function tostring(L:State, i:Int):Void {
307+
tolstring(L, i, null);
308+
}
309+
310+
@:native("lua_setlevel")
311+
static function setlevel(from:State, to:State):Void;
312+
@:native("luaL_openlibs")
313+
static function openlibs(L:State):Void;
314+
@:native("luaL_loadfile")
315+
static function loadfile(L:State, filename:CString):Int;
316+
317+
static inline function dofile(L:State,f:String):Bool {
318+
return loadfile(L,f) == 1 && pcall(L,0,MULTRET,0) == 1;
319+
}
320+
321+
}
322+
323+
@:hlNative("lua","hl_open_")
324+
@:include("lua.hpp")
325+
extern class LuaOpen {
326+
static inline var LUA_COLIBNAME = "coroutine";
327+
static inline var LUA_MATHLIBNAME = "math";
328+
static inline var LUA_STRLIBNAME = "string";
329+
static inline var LUA_TABLIBNAME = "table";
330+
static inline var LUA_IOLIBNAME = "io";
331+
static inline var LUA_OSLIBNAME = "os";
332+
static inline var LUA_LOADLIBNAME = "package";
333+
static inline var LUA_DBLIBNAME = "debug";
334+
static inline var LUA_BITLIBNAME = "bit";
335+
static inline var LUA_JITLIBNAME = "jit";
336+
static inline var LUA_FFILIBNAME = "ffi";
337+
338+
@:native("luaopen_base")
339+
static function base(L:State):Void;
340+
341+
@:native("luaopen_math")
342+
static function math(L:State):Void;
343+
@:native("luaopen_string")
344+
static function string(L:State):Void;
345+
@:native("luaopen_table")
346+
static function table(L:State):Void;
347+
@:native("luaopen_io")
348+
static function io(L:State):Void;
349+
@:native("luaopen_os")
350+
static function os(L:State):Void;
351+
@:native("luaopen_package")
352+
static function _package(L:State):Void;
353+
@:native("luaopen_debug")
354+
static function debug(L:State):Void;
355+
@:native("luaopen_bit")
356+
static function bit(L:State):Void;
357+
@:native("luaopen_jit")
358+
static function jit(L:State):Void;
359+
@:native("luaopen_ffi")
360+
static function ffi(L:State):Void;
361+
@:native("luaopen_string_buffer")
362+
static function string_buffer(L:State):Void;
363+
}

0 commit comments

Comments
 (0)