readNativeUICommandToDart function

List<UICommand> readNativeUICommandToDart(
  1. Pointer<Uint64> nativeCommandItems,
  2. int commandLength,
  3. int contextId
)

Implementation

List<UICommand> readNativeUICommandToDart(
    Pointer<Uint64> nativeCommandItems, int commandLength, int contextId) {
  List<int> rawMemory = nativeCommandItems
      .cast<Int64>()
      .asTypedList(commandLength * nativeCommandSize)
      .toList(growable: false);
  List<UICommand> results = List.generate(commandLength, (int _i) {
    int i = _i * nativeCommandSize;
    UICommand command = UICommand();

    int typeIdCombine = rawMemory[i + typeAndIdMemOffset];

    // int32_t  int32_t
    // +-------+-------+
    // |  id   | type  |
    // +-------+-------+
    int id = (typeIdCombine >> 32).toSigned(32);
    int type = (typeIdCombine ^ (id << 32)).toSigned(32);

    command.type = UICommandType.values[type];
    command.id = id;
    int nativePtrValue = rawMemory[i + nativePtrMemOffset];
    command.nativePtr = nativePtrValue != 0
        ? Pointer.fromAddress(rawMemory[i + nativePtrMemOffset])
        : nullptr;
    command.args = List.empty(growable: true);

    int args01And02Length = rawMemory[i + args01And02LengthMemOffset];
    int args01Length;
    int args02Length;

    if (args01And02Length == 0) {
      args01Length = args02Length = 0;
    } else {
      args02Length = (args01And02Length >> 32).toSigned(32);
      args01Length = (args01And02Length ^ (args02Length << 32)).toSigned(32);
    }

    int args01StringMemory = rawMemory[i + args01StringMemOffset];
    if (args01StringMemory != 0) {
      Pointer<Uint16> args_01 = Pointer.fromAddress(args01StringMemory);
      command.args.add(uint16ToString(args_01, args01Length));

      int args02StringMemory = rawMemory[i + args02StringMemOffset];
      if (args02StringMemory != 0) {
        Pointer<Uint16> args_02 = Pointer.fromAddress(args02StringMemory);
        command.args.add(uint16ToString(args_02, args02Length));
      }
    }

    if (isEnabledLog) {
      String printMsg = '${command.type}, id: ${command.id}';
      for (int i = 0; i < command.args.length; i++) {
        printMsg += ' args[$i]: ${command.args[i]}';
      }
      printMsg += ' nativePtr: ${command.nativePtr}';
      print(printMsg);
    }
    return command;
  }, growable: false);

  // Clear native command.
  _clearUICommandItems(contextId);

  return results;
}