From 6cf85dcc900c71687678bc316164142e76df7385 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy <roberto@inf.puc-rio.br> Date: Thu, 5 May 2005 12:34:03 -0300 Subject: metatables for all types --- lstrlib.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lstrlib.c') diff --git a/lstrlib.c b/lstrlib.c index 1a2e6c29..93f8e776 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.110 2005/03/08 20:10:05 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.111 2005/03/22 16:54:29 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -772,11 +772,26 @@ static const luaL_reg strlib[] = { }; +static void createmetatable (lua_State *L) { + lua_newtable(L); /* create metatable for strings */ + lua_pushliteral(L, ""); /* dummy string */ + lua_pushvalue(L, -2); + lua_setmetatable(L, -2); /* set string metatable */ + lua_pop(L, 1); /* pop dummy string */ + lua_pushvalue(L, -2); /* string library... */ + lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ + lua_getfield(L, -2, "len"); + lua_setfield(L, -2, "__siz"); + lua_pop(L, 1); /* pop metatable */ +} + + /* ** Open string library */ LUALIB_API int luaopen_string (lua_State *L) { luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); + createmetatable(L); return 1; } -- cgit v1.2.3-55-g6feb