Esempio n. 1
0
  public int invoke(LuaState vm) {
    switch (id) {

        /* Load the table library dynamically
         */
      case INSTALL:
        install(vm._G);
        return 0;

        /* table.concat (table [, sep [, i [, j]]])
         *
         * Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j].
         * The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table.
         * If i is greater than j, returns the empty string.
         */
      case CONCAT:
        {
          // int n = vm.gettop();
          LTable table = vm.checktable(1);
          LString sep = vm.optlstring(2, null);
          int i = vm.optint(3, 1);
          int j = vm.optint(4, -1);
          if (j == -1) j = table.luaLength();
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          for (int k = i; k <= j; k++) {
            LValue v = table.get(k);
            if (!v.isString()) vm.argerror(1, "table contains non-strings");
            v.luaConcatTo(baos);
            if (k < j && sep != null) sep.luaConcatTo(baos);
          }
          vm.pushlstring(baos.toByteArray());
          return 1;
        }

        /* table.getn (table)
         *
         * Get length of table t.
         */
      case FOREACH:
      case FOREACHI:
        {
          LTable table = vm.checktable(1);
          LFunction function = vm.checkfunction(2);
          vm.pushlvalue(table.foreach(vm, function, id == FOREACHI));
          return 1;
        }

        /* table.getn (table)
         *
         * Get length of table t.
         */
      case GETN:
        {
          LTable table = vm.checktable(1);
          vm.pushinteger(table.luaLength());
          return 1;
        }

        /* table.insert (table, [pos,] value)
         *
         * Inserts element value at position pos in table, shifting up other elements to open space, if necessary.
         * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call
         * table.insert(t,x) inserts x at the end of table t.
         */
      case INSERT:
        {
          LTable table = vm.checktable(1);
          int pos = 0;
          switch (vm.gettop()) {
            case 2:
              break;
            case 3:
              pos = vm.checkint(2);
              break;
            default:
              vm.error("wrong number of arguments to 'insert'");
          }
          table.luaInsertPos(pos, vm.topointer(-1));
          return 0;
        }

        /* table.maxn (table)
         *
         * Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
         * indices. (To do its job this function does a linear traversal of the whole table.)
         */
      case MAXN:
        {
          LTable table = vm.checktable(1);
          vm.pushlvalue(table.luaMaxN());
          return 1;
        }

        /* table.remove (table [, pos])
         *
         * Removes from table the element at position pos, shifting down other elements to close the space, if necessary.
         * Returns the value of the removed element. The default value for pos is n, where n is the length of the table,
         * so that a call table.remove(t) removes the last element of table t.
         */
      case REMOVE:
        {
          LTable table = vm.checktable(1);
          int pos = vm.optint(2, 0);
          LValue v = table.luaRemovePos(pos);
          vm.pushlvalue(v);
          return v.isNil() ? 0 : 1;
        }

        /* table.sort (table [, comp])
         *
         * Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table.
         * If comp is given, then it must be a function that receives two table elements, and returns true when the first
         * is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given,
         * then the standard Lua operator &lt; is used instead.
         *
         * The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
         */
      case SORT:
        {
          LTable table = vm.checktable(1);
          LValue compare = (vm.isnoneornil(2) ? (LValue) LNil.NIL : (LValue) vm.checkfunction(2));
          table.luaSort(vm, compare);
          return 0;
        }

      default:
        LuaState.vmerror("bad table id");
        return 0;
    }
  }
Esempio n. 2
0
 public static void install(LTable globals) {
   LTable table = new LTable();
   for (int i = 1; i < NAMES.length; i++) table.put(NAMES[i], new TableLib(i));
   globals.put("table", table);
   PackageLib.setIsLoaded("table", table);
 }