equal
deleted
inserted
replaced
66 struct lua_thread *thread; |
66 struct lua_thread *thread; |
67 const char *chunk; |
67 const char *chunk; |
68 }; |
68 }; |
69 |
69 |
70 /** |
70 /** |
71 * Setup the given newthread's state |
|
72 */ |
|
73 static int lua_thread_setup (lua_State *L) |
|
74 { |
|
75 struct lua_thread_start_ctx *ctx; |
|
76 |
|
77 // read the ctx argument off the stack |
|
78 if ((ctx = lua_touserdata(L, 1)) == NULL) |
|
79 return luaL_error(L, "lua_touserdata"); |
|
80 |
|
81 // push the thread state as the key |
|
82 lua_pushthread(L); |
|
83 |
|
84 // push the lua_thread as the value |
|
85 lua_pushlightuserdata(L, ctx->thread); |
|
86 |
|
87 // store L -> lua_thread mapping in the registry |
|
88 lua_settable(L, LUA_REGISTRYINDEX); |
|
89 |
|
90 // clean up the stack |
|
91 lua_pop(L, 1); |
|
92 |
|
93 // ok |
|
94 return 0; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Create a new thread, set it up, and run the initial resume, returning the boolean result of that |
71 * Create a new thread, set it up, and run the initial resume, returning the boolean result of that |
99 * (true for yielded, false for done). |
72 * (true for yielded, false for done). |
100 */ |
73 */ |
101 static int _lua_thread_start (lua_State *L) |
74 static int _lua_thread_start (lua_State *L) |
102 { |
75 { |
114 // check |
87 // check |
115 assert(!ctx->thread->L); |
88 assert(!ctx->thread->L); |
116 |
89 |
117 // create new thread |
90 // create new thread |
118 TL = lua_newthread(L); |
91 TL = lua_newthread(L); |
119 |
92 |
120 // create thread and do setup on it |
93 // push the lua_thread as the value |
121 if (nexus_lua_error(TL, lua_cpcall(TL, lua_thread_setup, ctx), &err)) |
94 lua_pushlightuserdata(L, ctx->thread); |
122 goto error; |
95 |
|
96 // store L -> lua_thread mapping in the registry |
|
97 lua_settable(L, LUA_REGISTRYINDEX); |
123 |
98 |
124 // load the chunk as a lua function into the thread's state |
99 // load the chunk as a lua function into the thread's state |
125 if (nexus_lua_error(TL, luaL_loadstring(TL, ctx->chunk), &err)) |
100 if (nexus_lua_error(TL, luaL_loadstring(TL, ctx->chunk), &err)) |
126 goto error; |
101 goto error; |
127 |
102 |