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 RangeError.range(
      args.length,
      argTypes.length,
      argTypes.length,
      'args',
      'Wrong number of message arguments',
    );
  }
  final typeTable = TypeTable();
  for (final t in argTypes) {
    t.buildTypeTable(typeTable);
  }
  final magic = _magicNumber.plainToU8a(useDartEncode: true);
  final table = typeTable.encode();
  final len = lebEncode(args.length);
  final types = u8aConcat(
    argTypes.map((t) => t.encodeType(typeTable)).toList(),
  );
  final vals = u8aConcat(
    zipWith<CType<dynamic>, dynamic, dynamic>(argTypes, args, (t, x) {
      final buf = tryToJson(t, x);
      if (buf != null) {
        return buf;
      }
      if (!t.covariant(x)) {
        throw ArgumentError.value(
          t,
          'type',
          'Error in covariant types ${t.display()} : ${toReadableString(x)}',
        );
      }
      return t.encodeValue(x);
    }),
  );
  return u8aConcat([magic, table, len, types, vals]);
}