Remove integer keys from lush table, use string lookups (issue #29)
VM now resolves lush functions via luaH_getshortstr through interned TString pointers, so wrapping lush.command = my_wrapper is transparently picked up. The lush table no longer has integer-keyed duplicates.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# Issue 29: Clean Up Integer Keys in lush Global Table
|
# Issue 29: Clean Up Integer Keys in lush Global Table
|
||||||
|
|
||||||
**Status:** open
|
**Status:** done
|
||||||
|
|
||||||
## Problem
|
## Problem
|
||||||
|
|
||||||
|
|||||||
20
lcmd.c
20
lcmd.c
@@ -22,6 +22,7 @@
|
|||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lcmd.h"
|
#include "lcmd.h"
|
||||||
#include "lbuiltin.h"
|
#include "lbuiltin.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
/* ===== argv parser ===== */
|
/* ===== argv parser ===== */
|
||||||
@@ -1203,6 +1204,8 @@ int lushCmd_setenv (lua_State *L) {
|
|||||||
|
|
||||||
/* ===== lush standard library ===== */
|
/* ===== lush standard library ===== */
|
||||||
|
|
||||||
|
TString *lushname[LUSH_OP_COUNT];
|
||||||
|
|
||||||
static const luaL_Reg lushlib[] = {
|
static const luaL_Reg lushlib[] = {
|
||||||
{"command", lushCmd_command},
|
{"command", lushCmd_command},
|
||||||
{"interactive", lushCmd_interactive},
|
{"interactive", lushCmd_interactive},
|
||||||
@@ -1214,17 +1217,12 @@ static const luaL_Reg lushlib[] = {
|
|||||||
|
|
||||||
LUAMOD_API int luaopen_lush (lua_State *L) {
|
LUAMOD_API int luaopen_lush (lua_State *L) {
|
||||||
luaL_newlib(L, lushlib);
|
luaL_newlib(L, lushlib);
|
||||||
/* also store integer-keyed entries for OP_LUSH VM access */
|
/* intern function name strings for OP_LUSH VM access */
|
||||||
lua_pushcfunction(L, lushCmd_command);
|
lushname[LUSH_OP_COMMAND] = luaS_new(L, "command");
|
||||||
lua_rawseti(L, -2, LUSH_OP_COMMAND + 1);
|
lushname[LUSH_OP_INTERACTIVE] = luaS_new(L, "interactive");
|
||||||
lua_pushcfunction(L, lushCmd_interactive);
|
lushname[LUSH_OP_GETENV] = luaS_new(L, "getenv");
|
||||||
lua_rawseti(L, -2, LUSH_OP_INTERACTIVE + 1);
|
lushname[LUSH_OP_SETENV] = luaS_new(L, "setenv");
|
||||||
lua_pushcfunction(L, lushCmd_getenv);
|
lushname[LUSH_OP_SUBCMD] = luaS_new(L, "subcmd");
|
||||||
lua_rawseti(L, -2, LUSH_OP_GETENV + 1);
|
|
||||||
lua_pushcfunction(L, lushCmd_setenv);
|
|
||||||
lua_rawseti(L, -2, LUSH_OP_SETENV + 1);
|
|
||||||
lua_pushcfunction(L, lushCmd_subcmd);
|
|
||||||
lua_rawseti(L, -2, LUSH_OP_SUBCMD + 1);
|
|
||||||
/* store in registry for OP_LUSH access */
|
/* store in registry for OP_LUSH access */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
|
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
|
||||||
|
|||||||
3
lcmd.h
3
lcmd.h
@@ -8,6 +8,7 @@
|
|||||||
#define lcmd_h
|
#define lcmd_h
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
|
||||||
/* OP_LUSH sub-operation indices (B operand selects which function) */
|
/* OP_LUSH sub-operation indices (B operand selects which function) */
|
||||||
#define LUSH_OP_COMMAND 0
|
#define LUSH_OP_COMMAND 0
|
||||||
@@ -23,6 +24,8 @@ int lushCmd_getenv (lua_State *L);
|
|||||||
int lushCmd_setenv (lua_State *L);
|
int lushCmd_setenv (lua_State *L);
|
||||||
int lushCmd_subcmd (lua_State *L);
|
int lushCmd_subcmd (lua_State *L);
|
||||||
|
|
||||||
|
extern TString *lushname[];
|
||||||
|
|
||||||
LUAMOD_API int luaopen_lush (lua_State *L);
|
LUAMOD_API int luaopen_lush (lua_State *L);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
3
lvm.c
3
lvm.c
@@ -30,6 +30,7 @@
|
|||||||
#include "ltable.h"
|
#include "ltable.h"
|
||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
#include "lvm.h"
|
#include "lvm.h"
|
||||||
|
#include "lcmd.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1947,7 +1948,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
|
|||||||
lu_byte tag = luaH_getint(regt, LUA_RIDX_LUSH, &lushtv);
|
lu_byte tag = luaH_getint(regt, LUA_RIDX_LUSH, &lushtv);
|
||||||
if (novariant(tag) == LUA_TTABLE) {
|
if (novariant(tag) == LUA_TTABLE) {
|
||||||
TValue func;
|
TValue func;
|
||||||
luaH_getint(hvalue(&lushtv), idx + 1, &func); /* 1-based */
|
luaH_getshortstr(hvalue(&lushtv), lushname[idx], &func);
|
||||||
setobj2s(L, ra, &func);
|
setobj2s(L, ra, &func);
|
||||||
}
|
}
|
||||||
vmbreak;
|
vmbreak;
|
||||||
|
|||||||
Reference in New Issue
Block a user