tableInsert method

LuaObject? tableInsert(
  1. int index,
  2. LuaObject value
)

Convenience method to implement std table methods.

Returns null if there is a problem Otherwise returns a lua object.

Implementation

LuaObject? tableInsert(int index, LuaObject value) {
  uses++;
  if (isTable) {
    final int sz = tableSize;
    if (index < 1 || index > tableSize + 1) return null;

    final List<Object> vals = [];

    int others = index;
    while (hasField(others.toString())) {
      vals.add(readField(others.toString())!);
      others++;
    }

    writeField(index.toString(), value);

    if (others <= sz) {
      while (others > 0) {
        writeField((others + 1).toString(), vals.removeLast());
        others--;
      }
    }

    return LuaObject.nil('ret');
  }

  return null;
}