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)) {
      vals.add(readField(others)!);
      others++;
    }

    writeField(index, value);

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

    return LuaObject.nil('field');
  }

  return null;
}