Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improvements from erlmon

  • Loading branch information...
commit 44a55514f1e5c90014694fe5ed1fb0a50b1ad5f3 1 parent 578cf8a
@darrikmazey darrikmazey authored
View
28 Makefile
@@ -0,0 +1,28 @@
+ERL_LIB=/usr/lib/erlang/lib/erl_interface-3.6.3
+CFLAGS=-Wall -I/usr/local/include -I$(ERL_LIB)/include -I/usr/lib/erlang/usr/include
+LDFLAGS=-shared -L. -L$(ERL_LIB)/lib
+LDLIBS=-llua
+STATICLIBS=$(ERL_LIB)/lib/libei.a $(ERL_LIB)/lib/liberl_interface.a
+GCC=/usr/bin/gcc
+ERL=/usr/bin/erl
+
+LIB=lib/liberlua.so
+
+ERLMODS=$(patsubst src/%.erl,ebin/%.beam,$(wildcard src/*.erl))
+OBJECTS=$(patsubst c_src/%.c,build/%.o,$(wildcard c_src/*.c))
+
+all: $(LIB) $(ERLMODS)
+
+build/%.o: c_src/%.c
+ $(GCC) $(CFLAGS) -o $@ -c $<
+
+ebin/%.beam: src/%.erl
+ $(ERL) -make
+
+$(LIB): $(OBJECTS)
+ $(GCC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $(LIB) $(OBJECTS) $(STATICLIBS)
+
+clean:
+ rm -f ebin/*.beam
+ rm -f build/*.o
+ rm -f lib/liberlua.so
View
1  build/.gitignore
@@ -0,0 +1 @@
+*.o
View
32 c_src/Rakefile
@@ -1,32 +0,0 @@
-CFLAGS = "-I /usr/local/lib/erlang/erts-5.7/include -I /usr/local/lib/erlang/lib/erl_interface-3.6/include -fPIC -g -O2 -Wall"
-if ENV['platform'] == 'linux'
- LDFLAGS = "-shared"
-else
- LDFLAGS = "-dynamic -bundle -undefined suppress -flat_namespace"
-end
-EXTLIBS = "/usr/local/lib/erlang/lib/erl_interface-3.6/lib/libei.a /usr/local/lib/liblua.a"
-
-FILES = ["commands", "lua_drv"]
-
-desc "Compile driver to a shared lib"
-task :compile => ["../priv/lua_drv.so"]
-
-FILES.each do |f|
- file "#{f}.o" => ["#{f}.c"] do
- # puts "gcc #{CFLAGS} -c -o #{f}.o #{f}.c"
- sh "gcc #{CFLAGS} -c -o #{f}.o #{f}.c"
- end
-end
-
-file "../priv/lua_drv.so" => FILES.map {|f| "#{f}.o"} do
- sh "mkdir -p ../priv"
- # puts "gcc #{CFLAGS} #{LDFLAGS} -o ../priv/lua_drv.so #{FILES.join('.o ')}.o #{EXTLIBS}"
- print "Compiling lua_drv.so..."
- sh "gcc #{CFLAGS} #{LDFLAGS} -o ../priv/lua_drv.so #{FILES.join('.o ')}.o #{EXTLIBS}"
- print " done\n"
- sh "rm #{FILES.join('.o ')}.o"
-end
-
-task :clean do
- sh "rm ../priv/*.so"
-end
View
14 c_src/commands.c
@@ -4,7 +4,7 @@
#include <lauxlib.h>
#include <string.h>
-#include "lua_drv.h"
+#include "erlua.h"
#include "commands.h"
static void reply_ok(lua_drv_t *driver_data);
@@ -318,6 +318,18 @@ erl_lual_dostring(lua_drv_t *driver_data, char *buf, int index)
reply_error(driver_data);
}
+void
+erl_lual_dofile(lua_drv_t *driver_data, char *buf, int index)
+{
+ char *code;
+
+ code = decode_string(buf, &index);
+
+ if (!luaL_dofile(driver_data->L, code))
+ reply_ok(driver_data);
+ else
+ reply_error(driver_data);
+}
void
erl_lua_no_command(lua_drv_t *driver_data)
View
8 c_src/lua_drv.c → c_src/erlua.c
@@ -5,7 +5,7 @@
#include <string.h>
#include <stdio.h>
-#include "lua_drv.h"
+#include "erlua.h"
#include "commands.h"
static ErlDrvData start (ErlDrvPort port, char* cmd);
@@ -19,7 +19,7 @@ static ErlDrvEntry lua_driver_entry = {
NULL, /* output */
NULL, /* ready_input */
NULL, /* ready_output */
- "lua_drv", /* the name of the driver */
+ "liberlua", /* the name of the driver */
NULL, /* finish */
NULL, /* handle */
NULL, /* control */
@@ -136,6 +136,10 @@ process(ErlDrvData handle, ErlIOVec *ev)
case ERL_LUAL_DOSTRING:
erl_lual_dostring(driver_data, buf, index);
break;
+
+ case ERL_LUAL_DOFILE:
+ erl_lual_dofile(driver_data, buf, index);
+ break;
default:
erl_lua_no_command(driver_data);
View
0  c_src/lua_drv.h → c_src/erlua.h
File renamed without changes
View
1  lib/.gitignore
@@ -0,0 +1 @@
+*.so
View
125 src/lua.erl
@@ -4,9 +4,14 @@
close/1,
call/3,
concat/2,
+ dostring/2,
+ dofile/2,
+ dump_table/2,
getfield/3,
getglobal/2,
gettop/1,
+ pop/1,
+ push/2,
pushboolean/2,
pushinteger/2,
pushstring/2,
@@ -19,7 +24,9 @@
tointeger/2,
tolstring/2,
tonumber/2,
- type/2]).
+ type/2,
+ type_atom/2
+ ]).
-include("lua.hrl").
-include("lua_api.hrl").
@@ -30,6 +37,14 @@ new_state() ->
close(L) ->
lua_driver:close(L).
+dostring(#lua{port=Port}, Code) ->
+ port_command(Port, term_to_binary({?ERL_LUAL_DOSTRING, Code})),
+ receive_simple_response().
+
+dofile(#lua{port=Port}, Filename) ->
+ port_command(Port, term_to_binary({?ERL_LUAL_DOFILE, Filename})),
+ receive_simple_response().
+
call(L, Args, Results) ->
command(L, {?ERL_LUA_CALL, Args, Results}),
receive_simple_response().
@@ -37,6 +52,41 @@ call(L, Args, Results) ->
concat(L, N) ->
command(L, {?ERL_LUA_CONCAT, N}).
+dump_table(L, N) ->
+ dump_table(L, N, none).
+
+dump_table(L, N, NextKey) ->
+ io:format("dump_table(~p, ~p)~n", [N, NextKey]),
+ case NextKey of
+ none ->
+ Str=lists:concat(["tmpkey, tmpval = pairs(", N, ")(", N, ")"]);
+ NK ->
+ Str=lists:concat(["tmpkey, tmpval = pairs(", N, ")(", N, ", \"", NK, "\")"])
+ end,
+ io:format("Str == ~p~n", [Str]),
+ lual:dostring(L, Str),
+ lua:getglobal(L, "tmpkey"),
+ {ok, T} = lua:type(L, -1),
+ case T of
+ ?LUA_TNIL -> [];
+ _ ->
+ {ok, K} = lua:tolstring(L, -1),
+ lua:remove(L, -1),
+ lua:getglobal(L, "tmpval"),
+ {ok, VT} = lua:type(L, -1),
+ io:format("type == ~p~n", [VT]),
+ case VT of
+ ?LUA_TNUMBER ->
+ {ok, V} = lua:tonumber(L, -1);
+ ?LUA_TTABLE ->
+ V = dump_table(L, lists:concat([N, ".", K]));
+ _ ->
+ {ok, V} = lua:tolstring(L, -1)
+ end,
+ lua:remove(L, -1),
+ [{list_to_atom(K),V}|dump_table(L, N, K)]
+ end.
+
getfield(L, global, Name) ->
getglobal(L, Name);
getfield(L, Index, Name) ->
@@ -51,6 +101,43 @@ gettop(L) ->
command(L, {?ERL_LUA_GETTOP}),
receive_valued_response().
+pop(L) ->
+ {ok, R} = gettop(L),
+ if
+ R < 1 ->
+ nil;
+ true ->
+ {ok, T} = type_atom(L, R),
+ case T of
+ number ->
+ {ok, N} = tonumber(L, R),
+ remove(L, R),
+ {ok, number, N};
+ string ->
+ {ok, N} = tolstring(L, R),
+ remove(L, R),
+ {ok, string, N};
+ boolean ->
+ {other, N} = toboolean(L, R),
+ remove(L, R),
+ {ok, boolean, N};
+ _ ->
+ {ok, N} = tolstring(L, R),
+ remove(L, R),
+ {ok, unknown, N}
+ end
+ end.
+
+push(L, Term) when is_number(Term) ->
+ pushnumber(L, Term);
+push(L, Term) when is_list(Term) ->
+ pushstring(L, Term);
+push(L, true) ->
+ pushboolean(L, true);
+push(L, false) ->
+ pushboolean(L, false);
+push(L, Term) when is_atom(Term) ->
+ pushstring(L, atom_to_list(Term)).
pushboolean(L, Bool) ->
command(L, {?ERL_LUA_PUSHBOOLEAN, Bool}),
@@ -108,7 +195,41 @@ type(L, Index) ->
command(L, {?ERL_LUA_TYPE, Index}),
receive_valued_response().
+type_atom(L, Index) ->
+ command(L, {?ERL_LUA_TYPE, Index}),
+ R = receive_valued_response(),
+ case R of
+ {ok, Str} ->
+ {ok, type_int_to_atom(Str)};
+ _ ->
+ R
+ end.
+type_int_to_atom(TypeInt) when is_integer(TypeInt) ->
+ case TypeInt of
+ 0 ->
+ Atom = nil;
+ 1 ->
+ Atom = boolean;
+ 2 ->
+ Atom = light_user_data;
+ 3 ->
+ Atom = number;
+ 4 ->
+ Atom = string;
+ 5 ->
+ Atom = table;
+ 6 ->
+ Atom = function;
+ 7 ->
+ Atom = user_data;
+ 8 ->
+ Atom = thread;
+ _ ->
+ Atom = unknown
+ end,
+ Atom.
+
command(#lua{port=Port}, Data) ->
port_command(Port, term_to_binary(Data)).
@@ -136,4 +257,4 @@ receive_valued_response() ->
{other, Other}
after ?STD_TIMEOUT ->
{error, timeout}
- end.
+ end.
View
6 src/lua_driver.erl
@@ -14,10 +14,10 @@ close(#lua{port=Port}) ->
%% Private functions
load_driver() ->
- SearchDir = filename:join([filename:dirname(code:which(lua_driver)), "..", "priv"]),
- case erl_ddll:load(SearchDir, "lua_drv") of
+ SearchDir = filename:join([filename:dirname(code:which(lua_driver)), "..", "lib"]),
+ case erl_ddll:load(SearchDir, liberlua) of
ok ->
- {ok, open_port({spawn, 'lua_drv'}, [binary])};
+ {ok, open_port({spawn, 'liberlua'}, [binary])};
Error ->
Error
end.
View
42 src/lua_erl.erl
@@ -1,42 +0,0 @@
--module(lua_erl).
-
--export([call/3, call/4]).
-
--include("lua.hrl").
--include("lua_api.hrl").
-
-call(L, Func, Args) ->
- call(L, Func, Args, 1).
-
-call(L, Func, Args, Returns) when is_atom(Func) ->
- call(L, atom_to_list(Func), Args, Returns);
-call(L, Func, Args, Returns) when is_list(Args) ->
- [FirstFunc | FuncList] = string:tokens(Func, "."),
- lua:getfield(L, global, FirstFunc),
- lists:foreach(fun(F) ->
- lua:getfield(L, -1, F),
- lua:remove(L, -2)
- end, FuncList),
- lists:foreach(fun(Arg) ->
- push(L, Arg)
- end, Args),
- lua:call(L, length(Args), Returns),
- list_to_tuple(lists:map(fun(I) ->
- R = case lua:type(L, -I) of
- {ok, ?LUA_TNIL} ->
- nil;
- {ok, ?LUA_TNUMBER} ->
- {ok, N} = lua:tonumber(L, -I),
- N;
- {ok, ?LUA_TSTRING} ->
- {ok, S} = lua:tolstring(L, -I),
- S
- end,
- lua:remove(L, -I),
- R
- end, lists:seq(Returns, 1, -1))).
-
-push(L, Value) when is_number(Value) ->
- lua:pushnumber(L, Value);
-push(L, Value) when is_list(Value) ->
- lua:pushstring(L, Value).
View
24 src/lual.erl
@@ -1,24 +0,0 @@
--module(lual).
-
--export([dostring/2]).
-
--include("lua.hrl").
--include("lua_api.hrl").
-
-dostring(#lua{port=Port}, Code) ->
- port_command(Port, term_to_binary({?ERL_LUAL_DOSTRING, Code})),
- recieve_simple_response().
-
-recieve_simple_response() ->
- receive
- ok ->
- ok;
- error ->
- {error, lua_error};
- {error, Reason} ->
- {error, Reason};
- Other ->
- {other, Other}
- after ?STD_TIMEOUT ->
- {error, timeout}
- end.
Please sign in to comment.
Something went wrong with that request. Please try again.