Hi,
I'm attaching the two patches I promised: the first, if_lua_5.2_table.diff
just serializes lists and dicts as Lua tables, while if_lua_5.2_udata.diff is
a bit more complex and represents lists and dicts as Lua userdata. I'll talk
about them in more detail below.
<snip>
> After that, this is some little things,
> I wonder why keep using LUAVIM_ENVIRONINDEX keyword yet.
> You have defined two new macros luaV_get/setvalue,
> and just those are using LUAVIM_ENVIRONINDEX only.
> How about rename those to luaV_get/setenvvalue,
> and remove LUAVIM_ENVIRONINDEX keyword.
> Like this:
>
> > #define luaV_getenvvalue(L, v) \
> > lua_pushlightuserdata(L, (void *) (v)); \
> > lua_rawget(L, lua_upvalueindex(1))
> > #define luaV_setenvvalue(L, v) \
> > lua_pushlightuserdata(L, (void *) (v)); \
> > lua_pushvalue(L, -2); \
> > lua_rawset(L, lua_upvalueindex(1))
Good, that's quite reasonable. I've changed luaV_{g,s}etvalue to
luaV_{g,s}etudata and added a few comments on these functions and about the
cache table. Thanks for the suggestions.
<snip>
> > The first solution was in my last patch; I'll try and come up
> > with another patch for the second solution by the end of the day,
> > and we can compare them.
>
> I like the first solution.
> It is simple and enough powerful.
Thanks for the feedback!
Now, back to the patches: the first patch is simpler, but does not intern
lists or dicts; the second patch provides "referential integrity": once
referenced in Lua, lists and dicts are interned and do not need to be
re-evaluated (or updated.) As an example:
:let x = {'n' : 1}
:lua x = vim.eval'x'
:lua print(x, x.n, #x)
--> dict: 0x27d92b8 1 1
:let x.self = x
:lua for k, v in x() do print(k, v) end -- equivalent to items(x)
--> self dict: 0x27d92b8
--> n 1
:let l = [1, 'a', x]
:lua l = vim.eval'l'
:lua print(l, #l, l[0], l[1], l[-1])
--> list: 0x27e2068 3 1 a dict: 0x27d92b8
:let l[2] = l
:lua for item in l() do print(item) end
--> 1
--> a
--> list: 0x27e2068
The other advantage of this patch is to allow a closer integration with Vim
internals. If, eventually, there is a higher demand for using Vim lists and
dicts in Lua, it would be easier to implement, say, list/dict creation --
:lua l = vim.list{1, 'a', x} -- concatenation -- :lua l = l + l2 -- and
modification -- :lua l[2] = l. Actually, due to this integration, it would be
possible to pass values from Lua to Vim using a "bridge" list or dict.
BTW, if we decide to adopt if_lua_5.2_udata.diff, it would be nice to have
list_find from eval.c as not static and defined in proto; right now I have a
simple and less efficient version of it in if_lua.c. Would that be possible?
Unless there are other considerations I'm missing, I'd prefer the second
patch. I'd appreciate feedback from if_lua users. I'll also have to update the
documentation for if_lua depending on which patch we apply.
Cheers,
Luis
--
Computers are useless. They can only give you answers.
-- Pablo Picasso
--
Luis Carvalho (Kozure)
lua -e 'print((("[email protected]"):gsub("(%u+%.)","")))'
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
--- if_lua.c.orig 2012-01-09 09:30:03.302542639 -0500
+++ if_lua.c 2012-01-11 13:42:19.658354683 -0500
@@ -1,4 +1,4 @@
-/* vi:set ts=8 sts=4 sw=4:
+/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
@@ -30,6 +30,17 @@
static const char LUAVIM_WINDOW[] = "window";
static const char LUAVIM_FREE[] = "luaV_free";
+/* most functions are closures with a cache table as first upvalue;
+ * get/setudata manage references to vim userdata in cache table through
+ * object pointers (light userdata) */
+#define luaV_getudata(L, v) \
+ lua_pushlightuserdata((L), (void *) (v)); \
+ lua_rawget((L), lua_upvalueindex(1))
+#define luaV_setudata(L, v) \
+ lua_pushlightuserdata((L), (void *) (v)); \
+ lua_pushvalue((L), -2); \
+ lua_rawset((L), lua_upvalueindex(1))
+/* get field from registry: used by metatables and luaV_free */
#define luaV_getfield(L, s) \
lua_pushlightuserdata((L), (void *)(s)); \
lua_rawget((L), LUA_REGISTRYINDEX)
@@ -38,6 +49,12 @@
#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
+#if LUA_VERSION_NUM <= 501
+#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
+#define luaL_typeerror luaL_typerror
+#else
+#define luaV_openlib luaL_setfuncs
+#endif
#ifdef DYNAMIC_LUA
@@ -54,32 +71,54 @@
#endif
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
#define luaL_register dll_luaL_register
+#define luaL_prepbuffer dll_luaL_prepbuffer
+#define luaL_openlib dll_luaL_openlib
#define luaL_typerror dll_luaL_typerror
+#define luaL_loadfile dll_luaL_loadfile
+#define luaL_loadbuffer dll_luaL_loadbuffer
+#else
+#define luaL_prepbuffsize dll_luaL_prepbuffsize
+#define luaL_setfuncs dll_luaL_setfuncs
+#define luaL_loadfilex dll_luaL_loadfilex
+#define luaL_loadbufferx dll_luaL_loadbufferx
+#define luaL_argerror dll_luaL_argerror
+#endif
#define luaL_checklstring dll_luaL_checklstring
#define luaL_checkinteger dll_luaL_checkinteger
#define luaL_optinteger dll_luaL_optinteger
#define luaL_checktype dll_luaL_checktype
#define luaL_error dll_luaL_error
-#define luaL_loadfile dll_luaL_loadfile
-#define luaL_loadbuffer dll_luaL_loadbuffer
#define luaL_newstate dll_luaL_newstate
#define luaL_buffinit dll_luaL_buffinit
-#define luaL_prepbuffer dll_luaL_prepbuffer
#define luaL_addlstring dll_luaL_addlstring
#define luaL_pushresult dll_luaL_pushresult
/* lua */
+#if LUA_VERSION_NUM <= 501
+#define lua_tonumber dll_lua_tonumber
+#define lua_tointeger dll_lua_tointeger
+#define lua_call dll_lua_call
+#define lua_pcall dll_lua_pcall
+#else
+#define lua_tonumberx dll_lua_tonumberx
+#define lua_tointegerx dll_lua_tointegerx
+#define lua_callk dll_lua_callk
+#define lua_pcallk dll_lua_pcallk
+#define lua_getglobal dll_lua_getglobal
+#define lua_setglobal dll_lua_setglobal
+#define lua_typename dll_lua_typename
+#endif
#define lua_close dll_lua_close
#define lua_gettop dll_lua_gettop
#define lua_settop dll_lua_settop
#define lua_pushvalue dll_lua_pushvalue
#define lua_replace dll_lua_replace
+#define lua_remove dll_lua_remove
#define lua_isnumber dll_lua_isnumber
#define lua_isstring dll_lua_isstring
#define lua_type dll_lua_type
#define lua_rawequal dll_lua_rawequal
-#define lua_tonumber dll_lua_tonumber
-#define lua_tointeger dll_lua_tointeger
#define lua_toboolean dll_lua_toboolean
#define lua_tolstring dll_lua_tolstring
#define lua_touserdata dll_lua_touserdata
@@ -94,16 +133,14 @@
#define lua_pushlightuserdata dll_lua_pushlightuserdata
#define lua_getfield dll_lua_getfield
#define lua_rawget dll_lua_rawget
+#define lua_rawgeti dll_lua_rawgeti
#define lua_createtable dll_lua_createtable
#define lua_newuserdata dll_lua_newuserdata
#define lua_getmetatable dll_lua_getmetatable
#define lua_setfield dll_lua_setfield
#define lua_rawset dll_lua_rawset
#define lua_rawseti dll_lua_rawseti
-#define lua_remove dll_lua_remove
#define lua_setmetatable dll_lua_setmetatable
-#define lua_call dll_lua_call
-#define lua_pcall dll_lua_pcall
/* libs */
#define luaopen_base dll_luaopen_base
#define luaopen_table dll_luaopen_table
@@ -116,32 +153,56 @@
#define luaL_openlibs dll_luaL_openlibs
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
+char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
+void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
+int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
+int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
+#else
+char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
+void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
+int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
+int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
+int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
+#endif
const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
-int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
-int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
lua_State *(*dll_luaL_newstate) (void);
void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
-char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
void (*dll_luaL_pushresult) (luaL_Buffer *B);
/* lua */
+#if LUA_VERSION_NUM <= 501
+lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
+lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
+void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
+int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
+#else
+lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
+lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
+void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
+ lua_CFunction k);
+int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
+ int ctx, lua_CFunction k);
+void (*dll_lua_getglobal) (lua_State *L, const char *var);
+void (*dll_lua_setglobal) (lua_State *L, const char *var);
+const char *(*dll_lua_typename) (lua_State *L, int tp);
+#endif
void (*dll_lua_close) (lua_State *L);
int (*dll_lua_gettop) (lua_State *L);
void (*dll_lua_settop) (lua_State *L, int idx);
void (*dll_lua_pushvalue) (lua_State *L, int idx);
void (*dll_lua_replace) (lua_State *L, int idx);
+void (*dll_lua_remove) (lua_State *L, int idx);
int (*dll_lua_isnumber) (lua_State *L, int idx);
int (*dll_lua_isstring) (lua_State *L, int idx);
int (*dll_lua_type) (lua_State *L, int idx);
int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
-lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
-lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
int (*dll_lua_toboolean) (lua_State *L, int idx);
const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
void *(*dll_lua_touserdata) (lua_State *L, int idx);
@@ -156,16 +217,14 @@
void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
void (*dll_lua_rawget) (lua_State *L, int idx);
+void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
int (*dll_lua_getmetatable) (lua_State *L, int objindex);
void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
void (*dll_lua_rawset) (lua_State *L, int idx);
void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
-void (*dll_lua_remove) (lua_State *L, int idx);
int (*dll_lua_setmetatable) (lua_State *L, int objindex);
-void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
-int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
/* libs */
int (*dll_luaopen_base) (lua_State *L);
int (*dll_luaopen_table) (lua_State *L);
@@ -185,32 +244,54 @@
static const luaV_Reg luaV_dll[] = {
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
{"luaL_register", (luaV_function) &dll_luaL_register},
+ {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
+ {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
{"luaL_typerror", (luaV_function) &dll_luaL_typerror},
+ {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
+ {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
+#else
+ {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
+ {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
+ {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
+ {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
+ {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
+#endif
{"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
{"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
{"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
{"luaL_checktype", (luaV_function) &dll_luaL_checktype},
{"luaL_error", (luaV_function) &dll_luaL_error},
- {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
- {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
{"luaL_newstate", (luaV_function) &dll_luaL_newstate},
{"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
- {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
{"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
{"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
/* lua */
+#if LUA_VERSION_NUM <= 501
+ {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
+ {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
+ {"lua_call", (luaV_function) &dll_lua_call},
+ {"lua_pcall", (luaV_function) &dll_lua_pcall},
+#else
+ {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
+ {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
+ {"lua_callk", (luaV_function) &dll_lua_callk},
+ {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
+ {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
+ {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
+ {"lua_typename", (luaV_function) &dll_lua_typename},
+#endif
{"lua_close", (luaV_function) &dll_lua_close},
{"lua_gettop", (luaV_function) &dll_lua_gettop},
{"lua_settop", (luaV_function) &dll_lua_settop},
{"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
{"lua_replace", (luaV_function) &dll_lua_replace},
+ {"lua_remove", (luaV_function) &dll_lua_remove},
{"lua_isnumber", (luaV_function) &dll_lua_isnumber},
{"lua_isstring", (luaV_function) &dll_lua_isstring},
{"lua_type", (luaV_function) &dll_lua_type},
{"lua_rawequal", (luaV_function) &dll_lua_rawequal},
- {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
- {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
{"lua_toboolean", (luaV_function) &dll_lua_toboolean},
{"lua_tolstring", (luaV_function) &dll_lua_tolstring},
{"lua_touserdata", (luaV_function) &dll_lua_touserdata},
@@ -225,16 +306,14 @@
{"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
{"lua_getfield", (luaV_function) &dll_lua_getfield},
{"lua_rawget", (luaV_function) &dll_lua_rawget},
+ {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
{"lua_createtable", (luaV_function) &dll_lua_createtable},
{"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
{"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
{"lua_setfield", (luaV_function) &dll_lua_setfield},
{"lua_rawset", (luaV_function) &dll_lua_rawset},
{"lua_rawseti", (luaV_function) &dll_lua_rawseti},
- {"lua_remove", (luaV_function) &dll_lua_remove},
{"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
- {"lua_call", (luaV_function) &dll_lua_call},
- {"lua_pcall", (luaV_function) &dll_lua_pcall},
/* libs */
{"luaopen_base", (luaV_function) &dll_luaopen_base},
{"luaopen_table", (luaV_function) &dll_luaopen_table},
@@ -294,6 +373,16 @@
#endif /* DYNAMIC_LUA */
+#if LUA_VERSION_NUM > 501
+ static int
+luaL_typeerror (lua_State *L, int narg, const char *tname)
+{
+ const char *msg = lua_pushfstring(L, "%s expected, got %s",
+ tname, luaL_typename(L, narg));
+ return luaL_argerror(L, narg, msg);
+}
+#endif
+
/* ======= Internal ======= */
@@ -330,7 +419,7 @@
luaV_checkudata(lua_State *L, int ud, const char *tname)
{
void *p = luaV_toudata(L, ud, tname);
- if (p == NULL) luaL_typerror(L, ud, tname);
+ if (p == NULL) luaL_typeerror(L, ud, tname);
return p;
}
@@ -357,22 +446,22 @@
if (l != NULL)
{
/* check cache */
- lua_pushlightuserdata(L, (void *) l);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, l);
if (lua_isnil(L, -1)) /* not interned? */
{
listitem_T *li;
int n = 0;
lua_pop(L, 1); /* nil */
lua_newtable(L);
- lua_pushlightuserdata(L, (void *) l);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX);
+ luaV_setudata(L, l); /* cache l */
for (li = l->lv_first; li != NULL; li = li->li_next)
{
luaV_pushtypval(L, &li->li_tv);
lua_rawseti(L, -2, ++n);
}
+ lua_pushnil(L);
+ luaV_setudata(L, l); /* uncache l */
+ lua_pop(L, 1);
}
}
else lua_pushnil(L);
@@ -384,8 +473,7 @@
if (d != NULL)
{
/* check cache */
- lua_pushlightuserdata(L, (void *) d);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, d);
if (lua_isnil(L, -1)) /* not interned? */
{
hashtab_T *ht = &d->dv_hashtab;
@@ -393,9 +481,7 @@
int n = ht->ht_used; /* remaining items */
lua_pop(L, 1); /* nil */
lua_newtable(L);
- lua_pushlightuserdata(L, (void *) d);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX);
+ luaV_setudata(L, d); /* cache d */
for (hi = ht->ht_array; n > 0; hi++)
{
if (!HASHITEM_EMPTY(hi))
@@ -406,6 +492,9 @@
n--;
}
}
+ lua_pushnil(L);
+ luaV_setudata(L, d); /* uncache d */
+ lua_pop(L, 1);
}
}
else lua_pushnil(L);
@@ -489,14 +578,7 @@
{
luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
*b = buf;
- lua_pushlightuserdata(L, (void *) buf);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
- /* to avoid GC, store as key in env */
- lua_pushvalue(L, -1);
- lua_pushboolean(L, 1);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
- /* set metatable */
+ luaV_setudata(L, buf); /* cache[buf] = udata */
luaV_getfield(L, LUAVIM_BUFFER);
lua_setmetatable(L, -2);
return b;
@@ -509,8 +591,7 @@
if (buf == NULL)
lua_pushnil(L);
else {
- lua_pushlightuserdata(L, (void *) buf);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, buf);
if (lua_isnil(L, -1)) /* not interned? */
{
lua_pop(L, 1);
@@ -702,8 +783,7 @@
luaV_buffer_isvalid(lua_State *L)
{
luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
- lua_pushlightuserdata(L, (void *) (*b));
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, *b);
lua_pushboolean(L, !lua_isnil(L, -1));
return 1;
}
@@ -729,14 +809,7 @@
{
luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
*w = win;
- lua_pushlightuserdata(L, (void *) win);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
- /* to avoid GC, store as key in env */
- lua_pushvalue(L, -1);
- lua_pushboolean(L, 1);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
- /* set metatable */
+ luaV_setudata(L, win); /* cache[win] = udata */
luaV_getfield(L, LUAVIM_WINDOW);
lua_setmetatable(L, -2);
return w;
@@ -749,8 +822,7 @@
if (win == NULL)
lua_pushnil(L);
else {
- lua_pushlightuserdata(L, (void *) win);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, win);
if (lua_isnil(L, -1)) /* not interned? */
{
lua_pop(L, 1);
@@ -880,8 +952,7 @@
luaV_window_isvalid(lua_State *L)
{
luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
- lua_pushlightuserdata(L, (void *) (*w));
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, *w);
lua_pushboolean(L, !lua_isnil(L, -1));
return 1;
}
@@ -1071,15 +1142,8 @@
static int
luaV_free(lua_State *L)
{
- lua_pushvalue(L, 1); /* lightudata */
- lua_rawget(L, LUA_ENVIRONINDEX);
- if (!lua_isnil(L, -1))
- {
- lua_pushnil(L);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
- lua_pushnil(L);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
- }
+ lua_pushnil(L);
+ luaV_setudata(L, lua_touserdata(L, 1));
return 0;
}
@@ -1099,13 +1163,8 @@
static int
luaopen_vim(lua_State *L)
{
- /* set environment */
+ /* set cache table */
lua_newtable(L);
- lua_newtable(L);
- lua_pushliteral(L, "v");
- lua_setfield(L, -2, "__mode");
- lua_setmetatable(L, -2);
- lua_replace(L, LUA_ENVIRONINDEX);
/* print */
lua_pushcfunction(L, luaV_print);
lua_setglobal(L, "print");
@@ -1116,14 +1175,20 @@
lua_pop(L, 1);
/* free */
lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
- lua_pushcfunction(L, luaV_free);
+ lua_pushvalue(L, 1); /* cache table */
+ lua_pushcclosure(L, luaV_free, 1);
lua_rawset(L, LUA_REGISTRYINDEX);
/* register */
luaV_newmetatable(L, LUAVIM_BUFFER);
- luaL_register(L, NULL, luaV_Buffer_mt);
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_Buffer_mt, 1);
luaV_newmetatable(L, LUAVIM_WINDOW);
- luaL_register(L, NULL, luaV_Window_mt);
- luaL_register(L, LUAVIM_NAME, luaV_module);
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_Window_mt, 1);
+ lua_newtable(L); /* vim table */
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_module, 1);
+ lua_setglobal(L, LUAVIM_NAME);
return 0;
}
@@ -1154,7 +1219,7 @@
static lua_State *L = NULL;
static int
-lua_is_open(void)
+lua_isopen(void)
{
return L != NULL;
}
@@ -1162,7 +1227,7 @@
static int
lua_init(void)
{
- if (L == NULL)
+ if (!lua_isopen())
{
#ifdef DYNAMIC_LUA
if (!lua_enabled(TRUE))
@@ -1179,7 +1244,7 @@
void
lua_end(void)
{
- if (L != NULL)
+ if (lua_isopen())
{
lua_close(L);
L = NULL;
@@ -1277,7 +1342,7 @@
void
lua_buffer_free(buf_T *buf)
{
- if (!lua_is_open()) return;
+ if (!lua_isopen()) return;
luaV_getfield(L, LUAVIM_FREE);
lua_pushlightuserdata(L, (void *) buf);
lua_call(L, 1, 0);
@@ -1287,7 +1352,7 @@
void
lua_window_free(win_T *win)
{
- if (!lua_is_open()) return;
+ if (!lua_isopen()) return;
luaV_getfield(L, LUAVIM_FREE);
lua_pushlightuserdata(L, (void *) win);
lua_call(L, 1, 0);
--- if_lua.c.orig 2012-01-09 09:30:03.302542639 -0500
+++ if_lua.c 2012-01-11 13:43:02.009170437 -0500
@@ -1,4 +1,4 @@
-/* vi:set ts=8 sts=4 sw=4:
+/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
@@ -24,12 +24,26 @@
typedef buf_T *luaV_Buffer;
typedef win_T *luaV_Window;
+typedef dict_T *luaV_Dict;
+typedef list_T *luaV_List;
typedef void (*msgfunc_T)(char_u *);
+static const char LUAVIM_DICT[] = "dict";
+static const char LUAVIM_LIST[] = "list";
static const char LUAVIM_BUFFER[] = "buffer";
static const char LUAVIM_WINDOW[] = "window";
static const char LUAVIM_FREE[] = "luaV_free";
+/* most functions are closures with a cache table as first upvalue;
+ * get/setudata manage references to vim userdata in cache table through
+ * object pointers (light userdata) */
+#define luaV_getudata(L, v) \
+ lua_pushlightuserdata((L), (void *) (v)); \
+ lua_rawget((L), lua_upvalueindex(1))
+#define luaV_setudata(L, v) \
+ lua_pushlightuserdata((L), (void *) (v)); \
+ lua_pushvalue((L), -2); \
+ lua_rawset((L), lua_upvalueindex(1))
#define luaV_getfield(L, s) \
lua_pushlightuserdata((L), (void *)(s)); \
lua_rawget((L), LUA_REGISTRYINDEX)
@@ -38,6 +52,15 @@
#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
+static luaV_List *luaV_pushlist (lua_State *L, list_T *lis);
+static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic);
+
+#if LUA_VERSION_NUM <= 501
+#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
+#define luaL_typeerror luaL_typerror
+#else
+#define luaV_openlib luaL_setfuncs
+#endif
#ifdef DYNAMIC_LUA
@@ -54,32 +77,54 @@
#endif
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
#define luaL_register dll_luaL_register
+#define luaL_prepbuffer dll_luaL_prepbuffer
+#define luaL_openlib dll_luaL_openlib
#define luaL_typerror dll_luaL_typerror
+#define luaL_loadfile dll_luaL_loadfile
+#define luaL_loadbuffer dll_luaL_loadbuffer
+#else
+#define luaL_prepbuffsize dll_luaL_prepbuffsize
+#define luaL_setfuncs dll_luaL_setfuncs
+#define luaL_loadfilex dll_luaL_loadfilex
+#define luaL_loadbufferx dll_luaL_loadbufferx
+#define luaL_argerror dll_luaL_argerror
+#endif
#define luaL_checklstring dll_luaL_checklstring
#define luaL_checkinteger dll_luaL_checkinteger
#define luaL_optinteger dll_luaL_optinteger
#define luaL_checktype dll_luaL_checktype
#define luaL_error dll_luaL_error
-#define luaL_loadfile dll_luaL_loadfile
-#define luaL_loadbuffer dll_luaL_loadbuffer
#define luaL_newstate dll_luaL_newstate
#define luaL_buffinit dll_luaL_buffinit
-#define luaL_prepbuffer dll_luaL_prepbuffer
#define luaL_addlstring dll_luaL_addlstring
#define luaL_pushresult dll_luaL_pushresult
/* lua */
+#if LUA_VERSION_NUM <= 501
+#define lua_tonumber dll_lua_tonumber
+#define lua_tointeger dll_lua_tointeger
+#define lua_call dll_lua_call
+#define lua_pcall dll_lua_pcall
+#else
+#define lua_tonumberx dll_lua_tonumberx
+#define lua_tointegerx dll_lua_tointegerx
+#define lua_callk dll_lua_callk
+#define lua_pcallk dll_lua_pcallk
+#define lua_getglobal dll_lua_getglobal
+#define lua_setglobal dll_lua_setglobal
+#define lua_typename dll_lua_typename
+#endif
#define lua_close dll_lua_close
#define lua_gettop dll_lua_gettop
#define lua_settop dll_lua_settop
#define lua_pushvalue dll_lua_pushvalue
#define lua_replace dll_lua_replace
+#define lua_remove dll_lua_remove
#define lua_isnumber dll_lua_isnumber
#define lua_isstring dll_lua_isstring
#define lua_type dll_lua_type
#define lua_rawequal dll_lua_rawequal
-#define lua_tonumber dll_lua_tonumber
-#define lua_tointeger dll_lua_tointeger
#define lua_toboolean dll_lua_toboolean
#define lua_tolstring dll_lua_tolstring
#define lua_touserdata dll_lua_touserdata
@@ -94,16 +139,14 @@
#define lua_pushlightuserdata dll_lua_pushlightuserdata
#define lua_getfield dll_lua_getfield
#define lua_rawget dll_lua_rawget
+#define lua_rawgeti dll_lua_rawgeti
#define lua_createtable dll_lua_createtable
#define lua_newuserdata dll_lua_newuserdata
#define lua_getmetatable dll_lua_getmetatable
#define lua_setfield dll_lua_setfield
#define lua_rawset dll_lua_rawset
#define lua_rawseti dll_lua_rawseti
-#define lua_remove dll_lua_remove
#define lua_setmetatable dll_lua_setmetatable
-#define lua_call dll_lua_call
-#define lua_pcall dll_lua_pcall
/* libs */
#define luaopen_base dll_luaopen_base
#define luaopen_table dll_luaopen_table
@@ -116,32 +159,56 @@
#define luaL_openlibs dll_luaL_openlibs
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
+char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
+void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
+int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
+int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
+#else
+char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
+void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
+int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
+int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
+int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
+#endif
const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
-int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
-int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
lua_State *(*dll_luaL_newstate) (void);
void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
-char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
void (*dll_luaL_pushresult) (luaL_Buffer *B);
/* lua */
+#if LUA_VERSION_NUM <= 501
+lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
+lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
+void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
+int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
+#else
+lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
+lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
+void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
+ lua_CFunction k);
+int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
+ int ctx, lua_CFunction k);
+void (*dll_lua_getglobal) (lua_State *L, const char *var);
+void (*dll_lua_setglobal) (lua_State *L, const char *var);
+const char *(*dll_lua_typename) (lua_State *L, int tp);
+#endif
void (*dll_lua_close) (lua_State *L);
int (*dll_lua_gettop) (lua_State *L);
void (*dll_lua_settop) (lua_State *L, int idx);
void (*dll_lua_pushvalue) (lua_State *L, int idx);
void (*dll_lua_replace) (lua_State *L, int idx);
+void (*dll_lua_remove) (lua_State *L, int idx);
int (*dll_lua_isnumber) (lua_State *L, int idx);
int (*dll_lua_isstring) (lua_State *L, int idx);
int (*dll_lua_type) (lua_State *L, int idx);
int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
-lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
-lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
int (*dll_lua_toboolean) (lua_State *L, int idx);
const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
void *(*dll_lua_touserdata) (lua_State *L, int idx);
@@ -156,16 +223,14 @@
void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
void (*dll_lua_rawget) (lua_State *L, int idx);
+void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
int (*dll_lua_getmetatable) (lua_State *L, int objindex);
void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
void (*dll_lua_rawset) (lua_State *L, int idx);
void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
-void (*dll_lua_remove) (lua_State *L, int idx);
int (*dll_lua_setmetatable) (lua_State *L, int objindex);
-void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
-int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
/* libs */
int (*dll_luaopen_base) (lua_State *L);
int (*dll_luaopen_table) (lua_State *L);
@@ -185,32 +250,54 @@
static const luaV_Reg luaV_dll[] = {
/* lauxlib */
+#if LUA_VERSION_NUM <= 501
{"luaL_register", (luaV_function) &dll_luaL_register},
+ {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
+ {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
{"luaL_typerror", (luaV_function) &dll_luaL_typerror},
+ {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
+ {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
+#else
+ {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
+ {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
+ {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
+ {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
+ {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
+#endif
{"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
{"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
{"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
{"luaL_checktype", (luaV_function) &dll_luaL_checktype},
{"luaL_error", (luaV_function) &dll_luaL_error},
- {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
- {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
{"luaL_newstate", (luaV_function) &dll_luaL_newstate},
{"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
- {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
{"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
{"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
/* lua */
+#if LUA_VERSION_NUM <= 501
+ {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
+ {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
+ {"lua_call", (luaV_function) &dll_lua_call},
+ {"lua_pcall", (luaV_function) &dll_lua_pcall},
+#else
+ {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
+ {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
+ {"lua_callk", (luaV_function) &dll_lua_callk},
+ {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
+ {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
+ {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
+ {"lua_typename", (luaV_function) &dll_lua_typename},
+#endif
{"lua_close", (luaV_function) &dll_lua_close},
{"lua_gettop", (luaV_function) &dll_lua_gettop},
{"lua_settop", (luaV_function) &dll_lua_settop},
{"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
{"lua_replace", (luaV_function) &dll_lua_replace},
+ {"lua_remove", (luaV_function) &dll_lua_remove},
{"lua_isnumber", (luaV_function) &dll_lua_isnumber},
{"lua_isstring", (luaV_function) &dll_lua_isstring},
{"lua_type", (luaV_function) &dll_lua_type},
{"lua_rawequal", (luaV_function) &dll_lua_rawequal},
- {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
- {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
{"lua_toboolean", (luaV_function) &dll_lua_toboolean},
{"lua_tolstring", (luaV_function) &dll_lua_tolstring},
{"lua_touserdata", (luaV_function) &dll_lua_touserdata},
@@ -225,16 +312,14 @@
{"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
{"lua_getfield", (luaV_function) &dll_lua_getfield},
{"lua_rawget", (luaV_function) &dll_lua_rawget},
+ {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
{"lua_createtable", (luaV_function) &dll_lua_createtable},
{"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
{"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
{"lua_setfield", (luaV_function) &dll_lua_setfield},
{"lua_rawset", (luaV_function) &dll_lua_rawset},
{"lua_rawseti", (luaV_function) &dll_lua_rawseti},
- {"lua_remove", (luaV_function) &dll_lua_remove},
{"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
- {"lua_call", (luaV_function) &dll_lua_call},
- {"lua_pcall", (luaV_function) &dll_lua_pcall},
/* libs */
{"luaopen_base", (luaV_function) &dll_luaopen_base},
{"luaopen_table", (luaV_function) &dll_luaopen_table},
@@ -294,6 +379,16 @@
#endif /* DYNAMIC_LUA */
+#if LUA_VERSION_NUM > 501
+ static int
+luaL_typeerror (lua_State *L, int narg, const char *tname)
+{
+ const char *msg = lua_pushfstring(L, "%s expected, got %s",
+ tname, luaL_typename(L, narg));
+ return luaL_argerror(L, narg, msg);
+}
+#endif
+
/* ======= Internal ======= */
@@ -330,7 +425,7 @@
luaV_checkudata(lua_State *L, int ud, const char *tname)
{
void *p = luaV_toudata(L, ud, tname);
- if (p == NULL) luaL_typerror(L, ud, tname);
+ if (p == NULL) luaL_typeerror(L, ud, tname);
return p;
}
@@ -351,66 +446,12 @@
lua_pushnumber(L, (lua_Number) tv->vval.v_float);
break;
#endif
- case VAR_LIST: {
- list_T *l = tv->vval.v_list;
-
- if (l != NULL)
- {
- /* check cache */
- lua_pushlightuserdata(L, (void *) l);
- lua_rawget(L, LUA_ENVIRONINDEX);
- if (lua_isnil(L, -1)) /* not interned? */
- {
- listitem_T *li;
- int n = 0;
- lua_pop(L, 1); /* nil */
- lua_newtable(L);
- lua_pushlightuserdata(L, (void *) l);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX);
- for (li = l->lv_first; li != NULL; li = li->li_next)
- {
- luaV_pushtypval(L, &li->li_tv);
- lua_rawseti(L, -2, ++n);
- }
- }
- }
- else lua_pushnil(L);
+ case VAR_LIST:
+ luaV_pushlist(L, tv->vval.v_list);
break;
- }
- case VAR_DICT: {
- dict_T *d = tv->vval.v_dict;
-
- if (d != NULL)
- {
- /* check cache */
- lua_pushlightuserdata(L, (void *) d);
- lua_rawget(L, LUA_ENVIRONINDEX);
- if (lua_isnil(L, -1)) /* not interned? */
- {
- hashtab_T *ht = &d->dv_hashtab;
- hashitem_T *hi;
- int n = ht->ht_used; /* remaining items */
- lua_pop(L, 1); /* nil */
- lua_newtable(L);
- lua_pushlightuserdata(L, (void *) d);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX);
- for (hi = ht->ht_array; n > 0; hi++)
- {
- if (!HASHITEM_EMPTY(hi))
- {
- dictitem_T *di = dict_lookup(hi);
- luaV_pushtypval(L, &di->di_tv);
- lua_setfield(L, -2, (char *) hi->hi_key);
- n--;
- }
- }
- }
- }
- else lua_pushnil(L);
+ case VAR_DICT:
+ luaV_pushdict(L, tv->vval.v_dict);
break;
- }
default:
luaL_error(L, "invalid type");
}
@@ -482,6 +523,232 @@
}
+/* ======= List type ======= */
+
+ static luaV_List *
+luaV_newlist (lua_State *L, list_T *lis)
+{
+ luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
+ *l = lis;
+ luaV_setudata(L, lis); /* cache[lis] = udata */
+ luaV_getfield(L, LUAVIM_LIST);
+ lua_setmetatable(L, -2);
+ return l;
+}
+
+ static luaV_List *
+luaV_pushlist (lua_State *L, list_T *lis)
+{
+ luaV_List *l = NULL;
+ if (lis == NULL)
+ lua_pushnil(L);
+ else {
+ luaV_getudata(L, lis);
+ if (lua_isnil(L, -1)) /* not interned? */
+ {
+ lua_pop(L, 1);
+ l = luaV_newlist(L, lis);
+ }
+ else
+ l = (luaV_List *) lua_touserdata(L, -1);
+ }
+ return l;
+}
+
+/* List metamethods */
+
+ static int
+luaV_list_gc (lua_State *L)
+{
+ lua_pushnil(L);
+ luaV_setudata(L, *((luaV_List *) lua_touserdata(L, 1)));
+ return 0;
+}
+
+ static int
+luaV_list_tostring (lua_State *L)
+{
+ lua_pushfstring(L, "%s: %p", LUAVIM_LIST, lua_touserdata(L, 1));
+ return 1;
+}
+
+ static int
+luaV_list_len (lua_State *L)
+{
+ list_T *l = *((luaV_List *) lua_touserdata(L, 1));
+ lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
+ return 1;
+}
+
+ static int
+luaV_list_iter (lua_State *L)
+{
+ listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
+ if (li == NULL) return 0;
+ luaV_pushtypval(L, &li->li_tv);
+ lua_pushlightuserdata(L, (void *) li->li_next);
+ lua_replace(L, lua_upvalueindex(2));
+ return 1;
+}
+
+ static int
+luaV_list_call (lua_State *L)
+{
+ list_T *l = *((luaV_List *) lua_touserdata(L, 1));
+ lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
+ lua_pushlightuserdata(L, (void *) l->lv_first);
+ lua_pushcclosure(L, luaV_list_iter, 2);
+ return 1;
+}
+
+ static listitem_T *
+list_find(list_T *l, long n)
+{
+ listitem_T *li;
+ if (l == NULL || n < -l->lv_len || n >= l->lv_len)
+ return NULL;
+ if (n < 0) /* search backward? */
+ for (li = l->lv_last; n < -1; li = li->li_prev)
+ n++;
+ else /* search forward */
+ for (li = l->lv_first; n > 0; li = li->li_next)
+ n--;
+ return li;
+}
+
+ static int
+luaV_list_index (lua_State *L)
+{
+ list_T *l = *((luaV_List *) lua_touserdata(L, 1));
+ long n = (long) luaL_checkinteger(L, 2);
+ listitem_T *li = list_find(l, n);
+ if (li == NULL)
+ lua_pushnil(L);
+ else
+ luaV_pushtypval(L, &li->li_tv);
+ return 1;
+}
+
+static const luaL_Reg luaV_List_mt[] = {
+ {"__gc", luaV_list_gc},
+ {"__tostring", luaV_list_tostring},
+ {"__len", luaV_list_len},
+ {"__call", luaV_list_call},
+ {"__index", luaV_list_index},
+ {NULL, NULL}
+};
+
+
+/* ======= Dict type ======= */
+
+ static luaV_Dict *
+luaV_newdict (lua_State *L, dict_T *dic)
+{
+ luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
+ *d = dic;
+ luaV_setudata(L, dic); /* cache[dic] = udata */
+ luaV_getfield(L, LUAVIM_DICT);
+ lua_setmetatable(L, -2);
+ return d;
+}
+
+ static luaV_Dict *
+luaV_pushdict (lua_State *L, dict_T *dic)
+{
+ luaV_Dict *d = NULL;
+ if (dic == NULL)
+ lua_pushnil(L);
+ else {
+ luaV_getudata(L, dic);
+ if (lua_isnil(L, -1)) /* not interned? */
+ {
+ lua_pop(L, 1);
+ d = luaV_newdict(L, dic);
+ }
+ else
+ d = (luaV_Dict *) lua_touserdata(L, -1);
+ }
+ return d;
+}
+
+/* Dict metamethods */
+
+ static int
+luaV_dict_gc (lua_State *L)
+{
+ lua_pushnil(L);
+ luaV_setudata(L, *((luaV_Dict *) lua_touserdata(L, 1)));
+ return 0;
+}
+
+ static int
+luaV_dict_tostring (lua_State *L)
+{
+ lua_pushfstring(L, "%s: %p", LUAVIM_DICT, lua_touserdata(L, 1));
+ return 1;
+}
+
+ static int
+luaV_dict_len (lua_State *L)
+{
+ dict_T *d = *((luaV_Dict *) lua_touserdata(L, 1));
+ lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
+ return 1;
+}
+
+ static int
+luaV_dict_iter (lua_State *L)
+{
+ hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
+ int n = lua_tointeger(L, lua_upvalueindex(3));
+ dictitem_T *di;
+ if (n <= 0) return 0;
+ while (HASHITEM_EMPTY(hi)) hi++;
+ di = dict_lookup(hi);
+ lua_pushstring(L, (char *) hi->hi_key);
+ luaV_pushtypval(L, &di->di_tv);
+ lua_pushlightuserdata(L, (void *) (hi + 1));
+ lua_replace(L, lua_upvalueindex(2));
+ lua_pushinteger(L, n - 1);
+ lua_replace(L, lua_upvalueindex(3));
+ return 2;
+}
+
+ static int
+luaV_dict_call (lua_State *L)
+{
+ dict_T *d = *((luaV_Dict *) lua_touserdata(L, 1));
+ hashtab_T *ht = &d->dv_hashtab;
+ lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
+ lua_pushlightuserdata(L, (void *) ht->ht_array);
+ lua_pushinteger(L, ht->ht_used); /* # remaining items */
+ lua_pushcclosure(L, luaV_dict_iter, 3);
+ return 1;
+}
+
+ static int
+luaV_dict_index (lua_State *L)
+{
+ dict_T *d = *((luaV_Dict *) lua_touserdata(L, 1));
+ char_u *key = (char_u *) luaL_checkstring(L, 2);
+ dictitem_T *di = dict_find(d, key, -1);
+ if (di == NULL)
+ lua_pushnil(L);
+ else
+ luaV_pushtypval(L, &di->di_tv);
+ return 1;
+}
+
+static const luaL_Reg luaV_Dict_mt[] = {
+ {"__gc", luaV_dict_gc},
+ {"__tostring", luaV_dict_tostring},
+ {"__len", luaV_dict_len},
+ {"__call", luaV_dict_call},
+ {"__index", luaV_dict_index},
+ {NULL, NULL}
+};
+
+
/* ======= Buffer type ======= */
static luaV_Buffer *
@@ -489,14 +756,7 @@
{
luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
*b = buf;
- lua_pushlightuserdata(L, (void *) buf);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
- /* to avoid GC, store as key in env */
- lua_pushvalue(L, -1);
- lua_pushboolean(L, 1);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
- /* set metatable */
+ luaV_setudata(L, buf); /* cache[buf] = udata */
luaV_getfield(L, LUAVIM_BUFFER);
lua_setmetatable(L, -2);
return b;
@@ -509,8 +769,7 @@
if (buf == NULL)
lua_pushnil(L);
else {
- lua_pushlightuserdata(L, (void *) buf);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, buf);
if (lua_isnil(L, -1)) /* not interned? */
{
lua_pop(L, 1);
@@ -702,8 +961,7 @@
luaV_buffer_isvalid(lua_State *L)
{
luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
- lua_pushlightuserdata(L, (void *) (*b));
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, *b);
lua_pushboolean(L, !lua_isnil(L, -1));
return 1;
}
@@ -729,14 +987,7 @@
{
luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
*w = win;
- lua_pushlightuserdata(L, (void *) win);
- lua_pushvalue(L, -2);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
- /* to avoid GC, store as key in env */
- lua_pushvalue(L, -1);
- lua_pushboolean(L, 1);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
- /* set metatable */
+ luaV_setudata(L, win); /* cache[win] = udata */
luaV_getfield(L, LUAVIM_WINDOW);
lua_setmetatable(L, -2);
return w;
@@ -749,8 +1000,7 @@
if (win == NULL)
lua_pushnil(L);
else {
- lua_pushlightuserdata(L, (void *) win);
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, win);
if (lua_isnil(L, -1)) /* not interned? */
{
lua_pop(L, 1);
@@ -880,8 +1130,7 @@
luaV_window_isvalid(lua_State *L)
{
luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
- lua_pushlightuserdata(L, (void *) (*w));
- lua_rawget(L, LUA_ENVIRONINDEX);
+ luaV_getudata(L, *w);
lua_pushboolean(L, !lua_isnil(L, -1));
return 1;
}
@@ -1008,15 +1257,10 @@
break;
}
}
- if (buf == NULL) /* not found? */
- lua_pushnil(L);
- else
- luaV_pushbuffer(L, buf);
}
- else {
+ else
buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
- luaV_pushbuffer(L, buf);
- }
+ luaV_pushbuffer(L, buf);
return 1;
}
@@ -1029,15 +1273,10 @@
int n = lua_tointeger(L, 1);
for (win = firstwin; win != NULL; win = win->w_next, n--)
if (n == 1) break;
- if (win == NULL) /* not found? */
- lua_pushnil(L);
- else
- luaV_pushwindow(L, win);
}
- else {
+ else
win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
- luaV_pushwindow(L, win);
- }
+ luaV_pushwindow(L, win);
return 1;
}
@@ -1071,15 +1310,8 @@
static int
luaV_free(lua_State *L)
{
- lua_pushvalue(L, 1); /* lightudata */
- lua_rawget(L, LUA_ENVIRONINDEX);
- if (!lua_isnil(L, -1))
- {
- lua_pushnil(L);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
- lua_pushnil(L);
- lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
- }
+ lua_pushnil(L);
+ luaV_setudata(L, lua_touserdata(L, 1));
return 0;
}
@@ -1099,13 +1331,8 @@
static int
luaopen_vim(lua_State *L)
{
- /* set environment */
- lua_newtable(L);
+ /* set cache table */
lua_newtable(L);
- lua_pushliteral(L, "v");
- lua_setfield(L, -2, "__mode");
- lua_setmetatable(L, -2);
- lua_replace(L, LUA_ENVIRONINDEX);
/* print */
lua_pushcfunction(L, luaV_print);
lua_setglobal(L, "print");
@@ -1116,14 +1343,26 @@
lua_pop(L, 1);
/* free */
lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
- lua_pushcfunction(L, luaV_free);
+ lua_pushvalue(L, 1); /* cache table */
+ lua_pushcclosure(L, luaV_free, 1);
lua_rawset(L, LUA_REGISTRYINDEX);
/* register */
+ luaV_newmetatable(L, LUAVIM_LIST);
+ lua_pushvalue(L, 1);
+ luaV_openlib(L, luaV_List_mt, 1);
+ luaV_newmetatable(L, LUAVIM_DICT);
+ lua_pushvalue(L, 1);
+ luaV_openlib(L, luaV_Dict_mt, 1);
luaV_newmetatable(L, LUAVIM_BUFFER);
- luaL_register(L, NULL, luaV_Buffer_mt);
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_Buffer_mt, 1);
luaV_newmetatable(L, LUAVIM_WINDOW);
- luaL_register(L, NULL, luaV_Window_mt);
- luaL_register(L, LUAVIM_NAME, luaV_module);
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_Window_mt, 1);
+ lua_newtable(L); /* vim table */
+ lua_pushvalue(L, 1); /* cache table */
+ luaV_openlib(L, luaV_module, 1);
+ lua_setglobal(L, LUAVIM_NAME);
return 0;
}
@@ -1154,7 +1393,7 @@
static lua_State *L = NULL;
static int
-lua_is_open(void)
+lua_isopen(void)
{
return L != NULL;
}
@@ -1162,7 +1401,7 @@
static int
lua_init(void)
{
- if (L == NULL)
+ if (!lua_isopen())
{
#ifdef DYNAMIC_LUA
if (!lua_enabled(TRUE))
@@ -1179,7 +1418,7 @@
void
lua_end(void)
{
- if (L != NULL)
+ if (lua_isopen())
{
lua_close(L);
L = NULL;
@@ -1277,7 +1516,7 @@
void
lua_buffer_free(buf_T *buf)
{
- if (!lua_is_open()) return;
+ if (!lua_isopen()) return;
luaV_getfield(L, LUAVIM_FREE);
lua_pushlightuserdata(L, (void *) buf);
lua_call(L, 1, 0);
@@ -1287,7 +1526,7 @@
void
lua_window_free(win_T *win)
{
- if (!lua_is_open()) return;
+ if (!lua_isopen()) return;
luaV_getfield(L, LUAVIM_FREE);
lua_pushlightuserdata(L, (void *) win);
lua_call(L, 1, 0);