initStdMetatables method
void
initStdMetatables()
Defines the following global functions:
- setmetatable(t, mt)
- getmetatable(t)
- rawset(t,k,v)
- rawget(t,k)
Implementation
void initStdMetatables() {
final defSetMetatable = LuaFuncBuilder.create('setmetatable')
.arg('t')
.arg('mt')
.exec(
call: () {
final t = findVar('t');
final mt = findVar('mt');
if (t == null || t.isNotTable) {
throw 'Expected lua table as first argument.';
}
if (mt == null || mt.isNotTable) {
throw 'Expected lua table as second argument.';
}
t.setMetatable(mt);
},
);
defGlobal(defSetMetatable).doc = LuaDoc(
category: catRuntime,
html: '''
Given lua table <code>t</code> and a table of functions <code>mt</code>,
sets <code>t</code>'s metatable to <code>mt</code>.
''',
);
final defGetMetatable = LuaFuncBuilder.create('getmetatable')
.arg('t')
.exec(
call: () {
final t = findVar('t');
if (t == null || t.isNotTable) {
throw 'Expected lua table as first argument.';
}
return t.getMetatable();
},
);
defGlobal(defGetMetatable).doc = LuaDoc(
category: catRuntime,
html: '''
Given lua table <code>t</code>, returns the metatable used
by <code>t</code> or <code>nil</code> if no metatable is set.
''',
);
final defRawSet = LuaFuncBuilder.create('rawset')
.arg('t')
.arg('k')
.arg('v')
.exec(
call: () {
final t = findVar('t');
if (t?.isNotTable ?? true) {
throw 'Expected lua table as first argument.';
}
final k = findVar('k');
if (k?.isNil ?? true) {
throw 'Expected non-null key for second argument';
}
final v = findVar('v');
t!.writeField(k ?? LuaObject.nil('key'), v);
return t;
},
);
defGlobal(defRawSet).doc = LuaDoc(
category: catRuntime,
html: '''
Given lua table <code>t</code>, sets the value <code>v</code>
to the table's <code>k</code> key index. This does not trigger
<code>__newindex</code>.<br/>Returns <code>t</code>.
''',
);
final defRawGet = LuaFuncBuilder.create('rawget')
.arg('t')
.arg('k')
.exec(
call: () {
final t = findVar('t');
if (t?.isNotTable ?? true) {
throw 'Expected lua table as first argument.';
}
final k = findVar('k');
if (k?.isNil ?? true) {
throw 'Expected non-null key for second argument';
}
return t!.readField(k!);
},
);
defGlobal(defRawGet).doc = LuaDoc(
category: catRuntime,
html: '''
Given lua table <code>t</code>, returns the value in the table
by key <code>k</code>. This does not trigger
<code>__index</code>.
''',
);
}