From a087c2737441aad781be7e3d88775e688152ad4e Mon Sep 17 00:00:00 2001 From: moteus Date: Fri, 27 Dec 2013 18:25:00 +0400 Subject: Add. pass cfunctions to child thread. --- .travis.yml | 1 + README.md | 2 +- src/llthread.c | 5 +++++ test/test_pass_cfunction.lua | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/test_pass_cfunction.lua diff --git a/.travis.yml b/.travis.yml index 7cadb6a..d02d26a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,6 +52,7 @@ script: - lua$LUA_SFX test_join_detach.lua - lua$LUA_SFX test_register_ffi.lua - lua$LUA_SFX test_logger.lua + - lua$LUA_SFX test_pass_cfunction.lua notifications: email: diff --git a/README.md b/README.md index d384055..b18459e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ This is full dropin replacement for [llthreads](https://github.com/Neopallium/lu * does not support ffi interface (use Lua C API for LuaJIT) * returns nil instead of false on error * start method returns self instead of true on success -* does not open all standart libraries (set LLTHREAD_REGISTER_STD_LIBRARY to on this feature) * register loaders for llthreads library itself ##Additional @@ -17,6 +16,7 @@ This is full dropin replacement for [llthreads](https://github.com/Neopallium/lu * thread:join() method support arbitrary timeout on Windows threads * set_logger function allow logging errors (crash Lua VM) in current llthread's threads * thread:start() has additional parameter which control in which thread child Lua VM will be destroyed +* allow pass cfunctions to child thread (e.g. to initialize Lua state) (experemental) ##Usage diff --git a/src/llthread.c b/src/llthread.c index 668a8c8..88498a8 100644 --- a/src/llthread.c +++ b/src/llthread.c @@ -228,6 +228,11 @@ static int llthread_copy_value(llthread_copy_state *state, int depth, int idx) { } break; case LUA_TFUNCTION: + if(lua_iscfunction(state->from_L, idx)){ + lua_CFunction fn = lua_tocfunction(state->from_L, idx); + lua_pushcfunction(state->to_L, fn); + break; + } case LUA_TUSERDATA: case LUA_TTHREAD: default: diff --git a/test/test_pass_cfunction.lua b/test/test_pass_cfunction.lua new file mode 100644 index 0000000..86fcd3d --- /dev/null +++ b/test/test_pass_cfunction.lua @@ -0,0 +1,17 @@ +local llthreads = require"llthreads" +local utils = require"utils" + +local thread = llthreads.new(utils.thread_init .. [[ + require "llthreads" + local fn = ... + + if type(fn) ~= 'function' then + print("ERROR! No function : ", fn, type(fn)) + os.exit(-2) + end + + fn("print('Done!'); require'os'.exit(0)"):start():join() +]], llthreads.new) + +print(thread:start():join()) +os.exit(-1) \ No newline at end of file -- cgit v1.2.3-55-g6feb