jsToCString function
Implementation
String jsToCString(Pointer<JSContext> ctx, Pointer<JSValue> val) {
final ptr = _jsToCString(ctx, val, _cStringLength);
if (ptr.address == 0) throw Exception('JSValue cannot convert to string');
final length = _cStringLength.value;
final bytes = ptr.cast<Uint8>();
// Utf8Decoder silently drops a leading BOM: decode past it and restore it.
final str =
length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF
? '\uFEFF${(bytes + 3).cast<Utf8>().toDartString(length: length - 3)}'
: ptr.toDartString(length: length);
jsFreeCString(ctx, ptr);
return str;
}