Beispiel #1
0
  /**
   * Returns the given {@link LuaValue} as the appropriate type of {@link LuaObjectValue}, directed
   * by the provided {@link LuaObjectMeta} type.
   *
   * <p>If the provided value is not of the appropriate type, a {@link LuaError} is raised.
   *
   * @param value the LuaValue to check
   * @param clazz the target LuaObjectMeta type to check with
   * @return the appropriately typed LuaValue as a LuaObjectValue
   */
  public static <T> LuaObjectValue<T> checkType(
      LuaValue value, Class<? extends LuaObjectMeta> clazz) {
    LuaObjectMeta meta = Lua.getMeta(clazz);
    if (meta == null) {
      throw new IllegalArgumentException(
          "Meta class " + clazz.getSimpleName() + " isn't registered");
    }

    if (value instanceof LuaObjectValue) {
      LuaObjectValue<?> objectValue = ((LuaObjectValue<?>) value);
      if (meta.getTargetObjectClass().isInstance(objectValue.getObject())) {
        return (LuaObjectValue<T>) objectValue;
      }
    }

    throw new LuaError(String.format(INVALID_TYPE_MSG, meta.getTypeName(), value.typename()));
  }
 /**
  * Get the String name of the type of this value.
  *
  * @return name from type name list LuaValue.TYPE_NAMES corresponding to the type of this value:
  *     "nil", "boolean", "number", "string", "table", "function", "userdata", "thread"
  */
 public String typename() {
   return val.typename();
 }