Skip to content

Instantly share code, notes, and snippets.

@jagt
Created April 13, 2014 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagt/10573875 to your computer and use it in GitHub Desktop.
Save jagt/10573875 to your computer and use it in GitHub Desktop.
small lua ext for playing around.
// 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