seqlua

view seqlua.c @ 57:da4b9d6a5b7e

Fixed bug due to problem combining luaL_buffinit with seqlua_iterinit
author jbe
date Tue Jan 05 04:26:49 2016 +0100 (2016-01-05)
parents 92ce3958aca7
children
line source
1 #include <lua.h>
2 #include <lauxlib.h>
3 #include "seqlualib.h"
4 #include <string.h>
6 static int seqlua_ipairsaux_raw(lua_State *L) {
7 lua_Integer i;
8 luaL_checktype(L, 1, LUA_TTABLE);
9 i = luaL_checkinteger(L, 2) + 1;
10 lua_pushinteger(L, i);
11 lua_rawgeti(L, 1, i); // TODO: Lua 5.3 returns type
12 return lua_isnil(L, -1) ? 1 : 2;
13 }
15 static int seqlua_ipairsaux_index(lua_State *L) {
16 lua_Integer i = luaL_checkinteger(L, 2) + 1;
17 lua_pushinteger(L, i);
18 lua_pushinteger(L, i);
19 lua_gettable(L, 1); // TODO: Lua 5.3 returns type
20 return lua_isnil(L, -1) ? 1 : 2;
21 }
23 static int seqlua_ipairsaux_call(lua_State *L) {
24 lua_pushinteger(L, luaL_checkinteger(L, 2) + 1);
25 lua_insert(L, 1); // integer on stack index 1
26 lua_settop(L, 2); // function on stack index 2
27 lua_call(L, 0, LUA_MULTRET);
28 if (lua_isnoneornil(L, 2)) {
29 lua_settop(L, 0);
30 lua_pushnil(L);
31 return 1;
32 } else {
33 return lua_gettop(L);
34 }
35 }
37 static int seqlua_ipairs(lua_State *L) {
38 if (luaL_getmetafield(L, 1, "__ipairs")) {
39 lua_pushvalue(L, 1);
40 lua_call(L, 1, 3);
41 if (lua_type(L, -3) == LUA_TSTRING) {
42 const char *method = lua_tostring(L, -3);
43 if (!strcmp(method, "raw")) {
44 lua_pushcfunction(L, seqlua_ipairsaux_raw);
45 } else if (!strcmp(method, "index")) {
46 lua_pushcfunction(L, seqlua_ipairsaux_index);
47 } else if (!strcmp(method, "call")) {
48 lua_pushcfunction(L, seqlua_ipairsaux_call);
49 } else {
50 luaL_error(L, "Unexpected string returned by __ipairs metamethod");
51 }
52 lua_pushvalue(L, -3);
53 lua_pushinteger(L, 0);
54 }
55 } else {
56 int t = lua_type(L, 1);
57 if (t == LUA_TFUNCTION) {
58 lua_pushcfunction(L, seqlua_ipairsaux_call);
59 } else if (luaL_getmetafield(L, 1, "__index")) {
60 lua_pushcfunction(L, seqlua_ipairsaux_index);
61 } else {
62 luaL_checktype(L, 1, LUA_TTABLE);
63 lua_pushcfunction(L, seqlua_ipairsaux_raw);
64 }
65 lua_pushvalue(L, 1);
66 lua_pushinteger(L, 0);
67 }
68 return 3;
69 }
71 static int seqlua_concat(lua_State *L) {
72 const char *sep;
73 size_t seplen;
74 seqlua_Iterator iter;
75 // NOTE: the following implementation is inefficient (Lua buffer would
76 // prohibit use of stack by seqlua_iterloop)
77 sep = luaL_checklstring(L, 1, &seplen);
78 luaL_checkany(L, 2);
79 lua_settop(L, 2);
80 lua_pushliteral(L, "");
81 seqlua_iterloop(L, &iter, 2) {
82 if (seqlua_itercount(&iter) > 1) {
83 lua_pushvalue(L, 3);
84 lua_pushlstring(L, sep, seplen);
85 lua_pushvalue(L, -3);
86 lua_concat(L, 3);
87 lua_replace(L, 3);
88 lua_pop(L, 1);
89 } else {
90 lua_replace(L, 3);
91 }
92 }
93 return 1;
94 }
96 int luaopen_seqlua(lua_State *L) {
97 lua_pushcfunction(L, seqlua_ipairs);
98 lua_setglobal(L, "ipairs");
99 lua_getglobal(L, "string");
100 lua_pushcfunction(L, seqlua_concat);
101 lua_setfield(L, -2, "concat");
102 return 0;
103 }

Impressum / About Us