Ejemplo n.º 1
0
    // -- JavaFunction methods
    @Override
    public int invoke(LuaState luaState) {
      // Check table
      luaState.checkType(1, LuaType.TABLE);

      // Get interfaces
      int interfaceCount = luaState.getTop() - 1;
      luaState.checkArg(2, interfaceCount > 0, "no interface specified");
      Class<?>[] interfaces = new Class<?>[interfaceCount];
      for (int i = 0; i < interfaceCount; i++) {
        if (luaState.isJavaObject(i + 2, Class.class)) {
          interfaces[i] = luaState.checkJavaObject(i + 2, Class.class);
        } else {
          String interfaceName = luaState.checkString(i + 2);
          interfaces[i] = loadType(luaState, interfaceName);
        }
      }

      // Create proxy
      luaState.pushJavaObjectRaw(luaState.getProxy(1, interfaces));
      return 1;
    }
Ejemplo n.º 2
0
    // -- JavaFunction methods
    @Override
    public int invoke(LuaState luaState) {
      // Find class
      Class<?> clazz;
      if (luaState.isJavaObject(1, Class.class)) {
        clazz = luaState.checkJavaObject(1, Class.class);
      } else {
        String className = luaState.checkString(1);
        clazz = loadType(luaState, className);
      }

      // Instantiate
      Object object;
      int dimensionCount = luaState.getTop() - 1;
      switch (dimensionCount) {
        case 0:
          try {
            object = clazz.newInstance();
          } catch (InstantiationException e) {
            throw new RuntimeException(e);
          } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
          }
          break;
        case 1:
          object = Array.newInstance(clazz, luaState.checkInteger(2));
          break;
        default:
          int[] dimensions = new int[dimensionCount];
          for (int i = 0; i < dimensionCount; i++) {
            dimensions[i] = luaState.checkInteger(i + 2);
          }
          object = Array.newInstance(clazz, dimensions);
      }

      // Return
      luaState.pushJavaObject(object);
      return 1;
    }