diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-05-05 12:34:03 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-05-05 12:34:03 -0300 |
| commit | 6cf85dcc900c71687678bc316164142e76df7385 (patch) | |
| tree | c7d672fc9c68f2cdaffb5e57d1bf8c846091aaa0 /lstrlib.c | |
| parent | 65f4a0f636eede0f026d0205ac929bc5a56f8b9c (diff) | |
| download | lua-6cf85dcc900c71687678bc316164142e76df7385.tar.gz lua-6cf85dcc900c71687678bc316164142e76df7385.tar.bz2 lua-6cf85dcc900c71687678bc316164142e76df7385.zip | |
metatables for all types
Diffstat (limited to 'lstrlib.c')
| -rw-r--r-- | lstrlib.c | 17 |
1 files changed, 16 insertions, 1 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstrlib.c,v 1.110 2005/03/08 20:10:05 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.111 2005/03/22 16:54:29 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 | */ |
| @@ -772,11 +772,26 @@ static const luaL_reg strlib[] = { | |||
| 772 | }; | 772 | }; |
| 773 | 773 | ||
| 774 | 774 | ||
| 775 | static void createmetatable (lua_State *L) { | ||
| 776 | lua_newtable(L); /* create metatable for strings */ | ||
| 777 | lua_pushliteral(L, ""); /* dummy string */ | ||
| 778 | lua_pushvalue(L, -2); | ||
| 779 | lua_setmetatable(L, -2); /* set string metatable */ | ||
| 780 | lua_pop(L, 1); /* pop dummy string */ | ||
| 781 | lua_pushvalue(L, -2); /* string library... */ | ||
| 782 | lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ | ||
| 783 | lua_getfield(L, -2, "len"); | ||
| 784 | lua_setfield(L, -2, "__siz"); | ||
| 785 | lua_pop(L, 1); /* pop metatable */ | ||
| 786 | } | ||
| 787 | |||
| 788 | |||
| 775 | /* | 789 | /* |
| 776 | ** Open string library | 790 | ** Open string library |
| 777 | */ | 791 | */ |
| 778 | LUALIB_API int luaopen_string (lua_State *L) { | 792 | LUALIB_API int luaopen_string (lua_State *L) { |
| 779 | luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); | 793 | luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); |
| 794 | createmetatable(L); | ||
| 780 | return 1; | 795 | return 1; |
| 781 | } | 796 | } |
| 782 | 797 | ||
