@@ -516,6 +516,73 @@ Return the number of codepoints that were actually consumed.
516516Return a substring of * ` view ` * , starting at the current position of
517517* ` view ` * and continuing for at most * ` codepoints ` * codepoints.
518518
519+ ### GC integration
520+
521+ Though this proposal does not have a dependency on the [ GC
522+ proposal] ( https://github.com/WebAssembly/gc/blob/master/proposals/gc/MVP.md ) ,
523+ compiler authors that target GC will likely want to be able to encode
524+ the contents of a stringref to a GC array, and vice versa.
525+
526+ The primary use cases are:
527+
528+ 1 . String-builder interfaces, which will likely use a WTF-8 or WTF-16
529+ array as intermediate storage, depending on the language being
530+ compiled. We will need to be able to create strings from arrays.
531+ When the string contents are ready, we will almost always decode
532+ from array offset 0 and continue to some offset before the end of
533+ the array. We'll also need to be able to append a string's contents
534+ to an array at a given offset.
535+ 2 . Communicating strings with another process, possibly over the
536+ network. Here, UTF-8 and WTF-8 are the important encodings, and we
537+ need to be able to read and write to arbitrary slices of arrays.
538+
539+ The instructions below shall be available in WebAssembly implementations
540+ that support both GC and stringrefs.
541+
542+ ```
543+ (string.new_wtf8_array $wtf8_policy codeunits:$t start:i32 end:i32)
544+ if expand($t) => array i8
545+ -> str:stringref
546+ ```
547+ Create a new string from a subsequence of the * ` codeunits ` * WTF-8 bytes
548+ in a GC-managed array, starting from offset * ` start ` * and continuing to
549+ but not including * ` end ` * . If * ` end ` * is less than * ` start ` * or is
550+ greater than the array length, trap. The bytes are decoded according to
551+ ` $wtf8_policy ` , as in ` string.new_wtf8 ` . The maximum value for
552+ * ` end ` * –* ` start ` * is 2<sup >31</sup >–1; passing a higher value traps.
553+
554+ ```
555+ (string.new_wtf16_array codeunits:$t start:i32 end:i32)
556+ if expand($t) => array i16
557+ -> str:stringref
558+ ```
559+ Create a new string from a subsequence of the * ` codeunits ` * WTF-16 code
560+ units in a GC-managed array, starting from offset * ` start ` * and
561+ continuing to but not including * ` end ` * . If * ` end ` * is less than
562+ * ` start ` * or is greater than the array length, trap. The maximum value
563+ for * ` end ` * –* ` start ` * is 2<sup >30</sup >–1; passing a higher value
564+ traps.
565+
566+ ```
567+ (string.encode_wtf8_array $wtf8_policy str:stringref array:$t start:i32)
568+ if expand($t) => array (mut i8)
569+ -> codeunits:i32
570+ (string.encode_wtf16_array str:stringref array:$t start:i32)
571+ if expand($t) => array (mut i16)
572+ -> codeunits:i32
573+ ```
574+ Encode the contents of the string * ` str ` * as WTF-8 or WTF-16,
575+ respectively, to the GC-managed array * ` array ` * , starting at offset
576+ * ` start ` * . Return the number of code units written, which will be the
577+ same as the result of a the corresponding ` string.measure_wtf8 ` or
578+ ` string.measure_wtf16 ` , respectively. If there is not space for the
579+ code units in the array, trap. Note that no ` NUL ` terminator is ever
580+ written.
581+
582+ For ` string.encode_wtf8_array ` , if an isolated surrogate is seen, the
583+ behavior depends on the * ` $wtf8_policy ` * immediate, in the same way as
584+ ` string.encode_wtf8 ` .
585+
519586## Binary encoding
520587
521588```
@@ -554,6 +621,10 @@ instr ::= ...
554621 | 0xfb 0xa2 ⇒ stringview_iter.advance
555622 | 0xfb 0xa3 ⇒ stringview_iter.rewind
556623 | 0xfb 0xa4 ⇒ stringview_iter.slice
624+ | 0xfb 0xb0 $policy:u32 [gc] ⇒ string.new_wtf8_array $policy
625+ | 0xfb 0xb1 [gc] ⇒ string.new_wtf16_array
626+ | 0xfb 0xb2 $policy:u32 [gc] ⇒ string.encode_wtf8_array $policy
627+ | 0xfb 0xb3 [gc] ⇒ string.encode_wtf16_array
557628
558629;; New section. If present, must be present only once, and right before
559630;; the globals section (or where the globals section would be). Each
0 commit comments