writeField method
If this lua object is a table, then
it writes value to key and returns the field key.
Otherwise if the lua object is not a table, then null is returned.
Alternatively, if the field value is known to be a LuaObject
whose LuaObject.id will be the same as key, then consider
using writeFieldFrom.
Implementation
Object? writeField(Object key, Object? value) {
if (skipSemanitcs) return LuaObject.noSemantics(key.toString());
if (isNil) {
throw 'Tried to add field "$key" on nil "$id".';
}
final k = _keyFrom(key);
Object? result;
if (isRef) {
result = deref().writeField(k, value);
} else if (isTable) {
result = switch (_fields![k]) {
final LuaObject obj => obj.value = value,
null => _fields![k] = LuaObject.variable(k.toString(), value),
};
}
_onWrite?.call(k, value);
return result;
}