Created
April 13, 2014 07:56
-
-
Save jagt/10573875 to your computer and use it in GitHub Desktop.
small lua ext for playing around.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// small lua ext for playing around. | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "lua.h" | |
#include "lualib.h" | |
#include "lauxlib.h" | |
#define err(msg) do {fprintf(stderr, "error: %s\n", (msg)); exit(1);} while (0) | |
int super_bind(lua_State *L) { | |
return 0; | |
} | |
int part2(lua_State *L); | |
int part1(lua_State *L) { | |
puts("part1, yielding.."); | |
return lua_yieldk(L, 0, 253, part2); | |
} | |
int part2(lua_State *L) { | |
int ctx; | |
lua_getctx(L, &ctx); | |
printf("part2!, ctx:%d\n", ctx); | |
return 0; | |
} | |
int super_ccoroutine_test(lua_State *L) { | |
int status; | |
lua_State *co = lua_newthread(L); | |
lua_pushcfunction(co, part1); | |
status = lua_resume(co, NULL, 0); | |
printf("resume1: %d\n", status); | |
status = lua_resume(co, NULL, 0); | |
printf("resume2: %d\n", status); | |
return 0; | |
} | |
static const luaL_Reg superlib[] = { | |
{"bind", super_bind}, | |
{"ccoroutine_test", super_ccoroutine_test}, | |
{NULL, NULL} | |
}; | |
int luaopen_super(lua_State *L) { | |
luaL_newlib(L, superlib); | |
return 1; | |
} | |
int main(int argc, char* argv[]) { | |
lua_State *L = luaL_newstate(); | |
if (argc < 2) { | |
puts("usage: superlua foo.lua"); | |
exit(0); | |
} | |
if (L == NULL) | |
err("failed to create lua state"); | |
luaL_checkversion(L); | |
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | |
luaL_openlibs(L); /* open libraries */ | |
// open super lib | |
luaL_requiref(L, "super", luaopen_super, 1); | |
lua_pop(L, 1); | |
lua_gc(L, LUA_GCRESTART, 0); | |
if (luaL_dofile(L, argv[1])) { | |
// report the error | |
err(lua_tolstring(L, -1, 0)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment