getValueAsync method
Asynchronous version of operator [] that awaits any __index
metamethod results. This is needed when metamethods are implemented as
Lua functions which return Futures.
Implementation
Future<dynamic> getValueAsync(Object? key) async {
if (raw is TableStorage) {
final denseIndex = _extractPositiveIndex(key);
if (denseIndex != null) {
final stored = (raw as TableStorage).denseValueAt(denseIndex);
if (stored != null) {
return _wrapRuntimeValue(stored);
}
}
}
if (raw is Map) {
final storageKey = _computeStorageKey(key);
if (raw is TableStorage && key is Value) {
final int? denseIndex = _extractPositiveIndex(key);
if (denseIndex != null) {
final stored = (raw as TableStorage).arrayValueAt(denseIndex);
if (stored != null) {
return _wrapRuntimeValue(stored);
}
}
}
if ((raw as Map).containsKey(storageKey)) {
final result = (raw as Map)[storageKey];
if (raw is VirtualLuaTable) {
return result;
}
if (result is Value) {
final resultRaw = rawLuaSlot(result);
if (resultRaw is Map) {
final existing = _lookupTableIdentity(resultRaw);
if (existing != null && !identical(existing, result)) {
(raw as Map)[storageKey] = existing;
return existing;
}
}
return result;
}
// See synchronous variant: avoid writing back wrapped Value into
// possibly typed maps.
return _wrapRuntimeValue(result);
}
final indexMeta = getMetamethod('__index');
if (indexMeta != null) {
var result = _normalizeIndexMetamethodResult(
await callMetamethodAsync('__index', [this, _wrapRuntimeValue(key)]),
);
if (result is Value) return result;
return _wrapRuntimeValue(result);
}
return _nilValue();
} else {
final indexMeta = getMetamethod('__index');
if (indexMeta != null) {
var result = _normalizeIndexMetamethodResult(
await callMetamethodAsync('__index', [this, _wrapRuntimeValue(key)]),
);
if (result is Value) return result;
final existing = _lookupTableIdentity(result);
if (existing != null) return existing;
return result;
}
final tname = NumberUtils.typeName(raw);
throw LuaError.typeError('attempt to index a $tname value');
}
}