at method

dynamic at([
  1. int index = -1
])

Returns the stack value at index and converts it to the equivalent Dart type.

Because of a limitation of Lua, functions and tables cannot be represented in Dart without a deep copy, these values are instead returned as an LuaOpaqueValue which references the stack position it was found in.

Implementation

dynamic at([int index = -1]) {
  assert(index != 0, "Index must not be 0");
  assert(index <= top, "Index overflowed stack");
  assert(index >= -top, "Index overflowed stack");
  if (lua_isnil(L, index)) {
    return null;
  } if (lua_isboolean(L, index)) {
    return lua_toboolean(L, index);
  } else if (lua_isinteger(L, index)) {
    return lua_tointeger(L, index);
  } else if (lua_isstring(L, index)) {
    return to_jsstring(lua_tostring(L, index));
  } else if (lua_isnumber(L, index)) {
    return lua_tonumber(L, index);
  } else if (lua_isuserdata(L, index) || lua_islightuserdata(L, index)) {
    return lua_touserdata(L, index);
  } else if (lua_isthread(L, index)) {
    return LuaState.wrap(lua_tothread(L, index));
  } else {
    if (index < 0) index += top + 1; // Resolve index
    return LuaOpaqueValue(
      lua_type(L, index),
      to_jsstring(lua_tostring(L, index)),
      index,
    );
  }
}