writeOut static method

void writeOut(
  1. Pointer<QjsResult> out,
  2. Object? v
)

将 Dart 对象写入 QjsResult (准备传给 C 侧)

Implementation

static void writeOut(Pointer<QjsResult> out, Object? v) {
  out.ref.error = 0;
  if (v is String) {
    out.ref.type = qjsTypeString;
    out.ref.s = v.toNativeUtf8();
  } else if (v == null) {
    out.ref.type = qjsTypeNull;
  } else if (v is bool) {
    out.ref.type = qjsTypeBool;
    out.ref.b = v ? 1 : 0;
  } else if (v is int) {
    out.ref.type = qjsTypeInt64;
    out.ref.i64 = v;
  } else if (v is double) {
    out.ref.type = qjsTypeFloat64;
    out.ref.f64 = v;
  } else {
    // 其他复杂类型根据协议选择序列化方式
    out.ref.type = qjsTypeObject;
    if (_useBinaryProtocol) {
      final writer = _BinaryWriter();
      writer.write(v);
      final bytes = writer.takeBytes();
      final ptr = ffi.malloc<Uint8>(bytes.length);
      ptr.asTypedList(bytes.length).setAll(0, bytes);
      out.ref.data = ptr;
      out.ref.dataLen = bytes.length;
    } else {
      final jsonStr = json.encode(v);
      final bytes = utf8.encode(jsonStr);
      final ptr = ffi.malloc<Uint8>(bytes.length);
      ptr.asTypedList(bytes.length).setAll(0, bytes);
      out.ref.data = ptr;
      out.ref.dataLen = bytes.length;
    }
  }
}