api - Lua inner conflicts with C/Delphi functions? -


i made timer implementation project (for lua 5.1, full sources, dll , test available @ http://wintarif.narod.ru/products.htm skip full sources in question). timer creates objects , implements createtimerqueuetimer. made 3 tests different behavior: shared part of test script

require('timer')  -- params same createtimerqueuetimer: duetime, period, flags -- flag wt_executeonlyonce = 8, timer stops, enabled set false local mt = timer(1000, 1000, 0)  local = 0; function myontimer()     print('wow!')     if < 5         = + 1     else         print("stopping timer")         mt:stoptimer()     end end  mt:setevent('ontimer', myontimer) mt:starttimer() 

when using

while mt:getenabled() --more buggy way end 

there "unhandled exception", prints wow! every second nonstop.
with

while true --buggy way, stack conflict during callback?     local enabled = mt:getenabled()     if not(enabled)         break     end end 

errors 5: bad argument #-2 'getenabled' (attempt concatenatetimerloadlib: table valuetimerstring) or 5: bad argument #-2 'getenabled' (timer expected, got table) or can work until first event dll , stops without errors.

only

function waitfortimer()     while true         local is_enabled = mt:getenabled()         if not(is_enabled)             print("not enabled")             return coroutine.yield()         end     end end  co = coroutine.create(waitfortimer) coroutine.resume(co) 

works without errors.

dll implementation getenabled() quite simple, static cdecl function

function staticthunk(l: plua_state): integer; cdecl; var   o: tluawrapper; begin   o := tluawrapper(lua_topointer(l, lua_upvalueindex(1)));   result := o.thunk(l); end; 

that extracts object, object's thunk

function tluawrapper.thunk(l: plua_state): integer; var   i: integer;   pobj: ptrt; begin   { redirect method call real thing }   := lua_tointeger(l, lua_upvalueindex(2)); // function's index, index 2 since 1 self ptr   lua_pushnumber(l, 0);   lua_gettable(l, 1); // class table (i.e, self)    pobj := ptrt(lual_checkudata(l, -1, pansichar(reg_name)));   lua_remove(l, -1); // remove userdata stack   lua_remove(l, 1); // remove object stack    try     result := classapiarray[i].func(l, pobj^); // execute thunk   except       result := 0;   end; end; 

and exact

function tluawrapper.getenabled(l: plua_state; obj: tqueuedtimer): integer; begin   // lua_settop(l, 0);   lua_pushboolean(l, obj.enabled);   result := 1; end; 

what happens inside lua? why have conflicts?

more info: script executed under lua.exe luaforwindows.

your callback function passed createtimerqueuetimer() executed windows asynchronously in thread.
not allowed work same lua state 2 different threads simultaneously.
thus, can't execute callback function in lua state while main script running in lua state.
using lua state multiple threads lead unpredictable behavior , weird error messages.
code coroutines, works without errors, errors once callback function become more complex.

lua state not thread-safe.
so, functionality of createtimerqueuetimer not bindable lua. Пичалька :-(


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -