idlEncode function

BinaryBlob idlEncode(
  1. List<CType> argTypes,
  2. List args
)

Encode a array of values @returns {Buffer} serialised value

Implementation

BinaryBlob idlEncode(List<CType> argTypes, List args) {
  if (args.length < argTypes.length) {
    throw 'Wrong number of message arguments';
  }

  final typeTable = TypeTable();
  for (var t in argTypes) {
    t.buildTypeTable(typeTable);
  }

  final magic = magicNumber.plainToU8a(useDartEncode: true);
  final table = typeTable.encode();
  final len = lebEncode(args.length);
  final typs = u8aConcat(argTypes.map((t) => t.encodeType(typeTable)).toList());
  final vals = u8aConcat(
    zipWith<CType<dynamic>, dynamic, dynamic>(argTypes, args, (t, x) {
      var buf = tryToJson(t, x);
      if (buf != null) {
        return buf;
      }
      if (!t.covariant(x)) {
        throw "Invalid ${t.display()} argument: ${toReadableString(x)}";
      }
      return t.encodeValue(x);
    }),
  );

  return u8aConcat([magic, table, len, typs, vals]);
}