Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | #include "liba.h" |
| 2 | |
| 3 | #include "lua.h" |
| 4 | #include "lualib.h" |
| 5 | #include "lauxlib.h" |
| 6 | |
| 7 | |
| 8 | //glue the luaVm and the actually libary |
| 9 | |
| 10 | static int lib_a_f_1_wrapper(lua_State * luaVM) |
| 11 | { |
| 12 | // no parameters , just call |
| 13 | lib_a_f_1(); |
| 14 | return 0; |
| 15 | } |
| 16 | |
| 17 | static int lib_a_f_2_wrapper(lua_State * L) |
| 18 | { |
| 19 | // no parameters , just call |
| 20 | int n = lua_gettop(L); |
| 21 | //error checking & return back to lua scripter |
| 22 | int a = lua_tonumber(L,1); |
| 23 | int b = lua_tonumber(L,2); |
| 24 | |
| 25 | int r = lib_a_f_2(a ,b ); |
| 26 | |
| 27 | lua_pushnumber(L,r); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | static int lib_a_f_3_wrapper(lua_State * L) |
| 33 | { |
| 34 | // no parameters , just call |
| 35 | int n = lua_gettop(L); |
| 36 | |
| 37 | //error checking & return back to lua scripter |
| 38 | const char *s = lua_tostring(L,1); |
| 39 | |
| 40 | int r = lib_a_f_3(s); |
| 41 | |
| 42 | lua_pushnumber(L,r); |
| 43 | |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | const char *metaname = "mine.Point"; // associated with userdata of type Point* |
| 48 | |
| 49 | static int lib_a_f_4_wrapper(lua_State *L) { |
| 50 | Point *t = luaL_checkudata(L, 1, metaname); // check argument type |
| 51 | lua_pushnumber(L, (lua_Number)lib_a_f_4(t)); |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static int point_new_wrapper(lua_State *L) { // get Lua to allocate an initialize a Point* |
| 56 | int a = luaL_checkint(L, 1); |
| 57 | int b = luaL_checkint(L, 2); |
| 58 | //create user data and associate metable with it |
| 59 | Point *t = lua_newuserdata(L, sizeof(*t)); |
| 60 | luaL_getmetatable(L, metaname); |
| 61 | lua_setmetatable(L, -2); |
| 62 | t->a = a; |
| 63 | t->b = b; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | static int point_geta_wrapper(lua_State *L) |
| 68 | { |
| 69 | Point *t = luaL_checkudata(L, 1, metaname); |
| 70 | lua_pushnumber(L,t->a); |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static int point_getb_wrapper(lua_State *L) |
| 76 | { |
| 77 | Point *t = luaL_checkudata(L, 1, metaname); |
| 78 | lua_pushnumber(L,t->b); |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | static int lib_a_f_5_wrapper(lua_State *L) |
| 84 | { |
| 85 | // Point *t = luaL_checkudata(L, 1, metaname); // check argument type |
| 86 | // lua_pushnumber(L, (lua_Number)lib_a_f_4(t)); |
| 87 | |
| 88 | Point t ; |
| 89 | lib_a_f_5(&t); |
| 90 | Point *r = lua_newuserdata(L,sizeof(*r)); |
| 91 | luaL_getmetatable(L, metaname); |
| 92 | //It will Pops a table from the stack and |
| 93 | //sets it as the new metatable for the value at the given acceptable index. |
| 94 | //so , after this function , it is the userdata at the top of the stack. |
| 95 | lua_setmetatable(L, -2); |
| 96 | r->a = t.a; |
| 97 | r->b = t.b; |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static const struct luaL_reg functions[] = { |
| 103 | {"lib_a_f_1", lib_a_f_1_wrapper}, |
| 104 | {"lib_a_f_2", lib_a_f_2_wrapper}, |
| 105 | {"lib_a_f_3", lib_a_f_3_wrapper}, |
| 106 | {"lib_a_f_4", lib_a_f_4_wrapper}, |
| 107 | {"lib_a_f_5", lib_a_f_5_wrapper}, |
| 108 | {"point_new", point_new_wrapper}, |
| 109 | {"point_geta", point_geta_wrapper}, |
| 110 | {"point_getb", point_getb_wrapper}, |
| 111 | { NULL, NULL} |
| 112 | }; |
| 113 | |
| 114 | //This is the init function that will be called when you require 'mylib' |
| 115 | |
| 116 | int luaopen_wrap(lua_State *L) { |
| 117 | |
| 118 | |
| 119 | luaL_newmetatable(L, metaname); |
| 120 | //pop 1 elements from the statck .. why?? to pop the newmetatable that is useless. |
| 121 | // |
| 122 | //lua_pop(L, 1); |
| 123 | //replace luaL_openlib |
| 124 | { |
| 125 | lua_pop(L, 1); |
| 126 | luaL_newmetatable(L,metaname); |
| 127 | return 1; |
| 128 | } |
| 129 | luaL_register(L, "wrap", functions); |
| 130 | return 1; |
| 131 | } |
| 132 |
Branches:
master
