writeWASIComponentCanonicalString function

WASIComponentMemoryString writeWASIComponentCanonicalString(
  1. Memory memory,
  2. WASIComponentCanonicalRealloc realloc,
  3. String value,
  4. WASIComponentCanonicalStringEncoding encoding,
)

Writes a canonical string payload into memory through realloc.

Implementation

WASIComponentMemoryString writeWASIComponentCanonicalString(
  wasm.Memory memory,
  WASIComponentCanonicalRealloc realloc,
  String value,
  WASIComponentCanonicalStringEncoding encoding,
) {
  final encoded = _encodeCanonicalString(value, encoding);
  final pointer = realloc(0, 0, encoded.alignment, encoded.bytes.length);
  RangeError.checkNotNegative(pointer, 'pointer');
  _checkAligned(pointer, encoded.alignment, 'pointer');

  final bytes = Uint8List.view(memory.buffer);
  _checkMemoryRange(bytes, pointer, encoded.bytes.length, 'pointer + length');
  bytes.setRange(pointer, pointer + encoded.bytes.length, encoded.bytes);
  return WASIComponentMemoryString(
    pointer: pointer,
    canonicalLength: encoded.canonicalLength,
    byteLength: encoded.bytes.length,
  );
}