lua-users home
lua-l archive

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


On Sun, Apr 18, 2010 at 2:11 PM, Ralph Hempel <rhempel@bmts.com> wrote:
> OK, I've been breaking my head for hours on this and
> cannot see the purpose of the createmetatable() call
> in luaopen_string()
>
> The reason I'm fooling around in this code is an attempt
> to make even the standard libraries unloadable to save
> precious RAM in my pbLua for the LEGO MINDSTORMS NXT.
>
> I've had to modify my main() to create the _LOADED registry
> table with weak values so that when a particular library
> like "table" is set to nil, the table can be garbage collected.
>
> But string eludes me.
>
> So, long story short, if someone can explain to me what's going
> on then the string library is created, I'd appreciate it :-)
>
> Ralph
>

The purpose of that function is to set a metatable for the base
"string" type, so that method calls on a string instance will call
functions in the string library. For example:

  a = "Blah blah blah"
  b = a:upper()

...becomes equivalent to:

  a = "Blah blah blah"
  b = string.upper(a)

Unlike tables (and full userdata), which can each individually have
their own metatable, the base types (string, number, boolean, etc.)
each have a shared metatable that is used by all values of that type.

-Duncan