lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

2015-05-03 21:24 GMT+02:00 Andrew Starks <>:

> I could not bare to triple post, and this idea is different enough
> that I'll take the opportunity to fork...
> sand boxing the string's metamethods is something that would be nice
> to be able to do, but it is hard to imagine that feature existing
> without something being treated as "special".
> I don't know if this mechanism would be general and useful enough for
> consideration, but I did think of a way that I believe could be used
> to solve this problem.

It is trivial to sandbox a string's methods.

$ lua
Lua 5.3.0  Copyright (C) 1994-2015, PUC-Rio
> getmetatable"".__metatable = "Sandboxed!"
> string = nil
> str = "The quick brown fox"
> for k in str:gmatch"%S+" do print(k) end
> for k in string.gmatch(str,"%S+") do print(k) end
stdin:1: attempt to index a nil value (global 'string')
stack traceback:
    stdin:1: in main chunk
    [C]: in ?
> mt = getmetatable""
> mt

Any extra method you care to add to 'string' before setting it to nil will
still be there.