diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-28 14:13:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-28 14:13:52 -0300 |
| commit | 17526ed2fe81c52dd6cf22d90fea7fea62d07229 (patch) | |
| tree | 5091eb31cce94a2ae028832b190889778fc7ecce | |
| parent | f180822fa6f99a27d3c01bb166af53788681d861 (diff) | |
| download | lua-17526ed2fe81c52dd6cf22d90fea7fea62d07229.tar.gz lua-17526ed2fe81c52dd6cf22d90fea7fea62d07229.tar.bz2 lua-17526ed2fe81c52dd6cf22d90fea7fea62d07229.zip | |
no more errors on accesses to 'string' and strings
| -rw-r--r-- | lstrlib.c | 26 |
1 files changed, 7 insertions, 19 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.168 2011/06/09 18:22:47 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.169 2011/06/16 14:14:05 roberto Exp roberto $ |
| 3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -917,15 +917,6 @@ static int str_format (lua_State *L) { | |||
| 917 | /* }====================================================== */ | 917 | /* }====================================================== */ |
| 918 | 918 | ||
| 919 | 919 | ||
| 920 | static int noindex (lua_State *L) { | ||
| 921 | const char *f = lua_tostring(L, 2); | ||
| 922 | if (f) | ||
| 923 | return luaL_error(L, "no field '%s' in strings", f); | ||
| 924 | else | ||
| 925 | return luaL_error(L, "no such field in strings"); | ||
| 926 | } | ||
| 927 | |||
| 928 | |||
| 929 | static const luaL_Reg strlib[] = { | 920 | static const luaL_Reg strlib[] = { |
| 930 | {"byte", str_byte}, | 921 | {"byte", str_byte}, |
| 931 | {"char", str_char}, | 922 | {"char", str_char}, |
| @@ -941,22 +932,19 @@ static const luaL_Reg strlib[] = { | |||
| 941 | {"reverse", str_reverse}, | 932 | {"reverse", str_reverse}, |
| 942 | {"sub", str_sub}, | 933 | {"sub", str_sub}, |
| 943 | {"upper", str_upper}, | 934 | {"upper", str_upper}, |
| 944 | {"__index", noindex}, | ||
| 945 | {NULL, NULL} | 935 | {NULL, NULL} |
| 946 | }; | 936 | }; |
| 947 | 937 | ||
| 948 | 938 | ||
| 949 | static void createmetatable (lua_State *L) { | 939 | static void createmetatable (lua_State *L) { |
| 950 | /* setmetatable("", {__index = string}) */ | 940 | lua_createtable(L, 0, 1); /* table to be metatable for strings */ |
| 951 | lua_pushliteral(L, ""); /* dummy string */ | 941 | lua_pushliteral(L, ""); /* dummy string */ |
| 952 | lua_createtable(L, 0, 1); /* create metatable for strings */ | 942 | lua_pushvalue(L, -2); /* copy table */ |
| 953 | lua_pushvalue(L, -3); /* set the string library... */ | 943 | lua_setmetatable(L, -2); /* set table as metatable for strings */ |
| 954 | lua_setfield(L, -2, "__index"); /* ...as the __index metamethod */ | ||
| 955 | lua_setmetatable(L, -2); /* set metatable for strings */ | ||
| 956 | lua_pop(L, 1); /* pop dummy string */ | 944 | lua_pop(L, 1); /* pop dummy string */ |
| 957 | /* setmetatable(string, string) */ | 945 | lua_pushvalue(L, -2); /* get string library */ |
| 958 | lua_pushvalue(L, -1); /* push string library */ | 946 | lua_setfield(L, -2, "__index"); /* metatable.__index = string */ |
| 959 | lua_setmetatable(L, -2); /* set it as its own metatable */ | 947 | lua_pop(L, 1); /* pop metatable */ |
| 960 | } | 948 | } |
| 961 | 949 | ||
| 962 | 950 | ||
