[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: getmetatable-esque metamethod [Forked from:] metatables for strings?
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 3 May 2015 22:38:45 +0200
2015-05-03 21:24 GMT+02:00 Andrew Starks <andrew.starks@trms.com>:
> 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 Lua.org, PUC-Rio
> getmetatable"".__metatable = "Sandboxed!"
> string = nil
> str = "The quick brown fox"
> for k in str:gmatch"%S+" do print(k) end
The
quick
brown
fox
> 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
Sandboxed!
---
Any extra method you care to add to 'string' before setting it to nil will
still be there.