diff --git a/design/mvp/CanonicalABI.md b/design/mvp/CanonicalABI.md index 7e9d7e779..1d26ad2ed 100644 --- a/design/mvp/CanonicalABI.md +++ b/design/mvp/CanonicalABI.md @@ -2200,11 +2200,15 @@ memory) and a number of [code units]. There are three supported string encodings in [`canonopt`]: [UTF-8], [UTF-16] and `latin1+utf16`. This last option allows a *dynamic* choice between [Latin-1] and UTF-16, indicated by the high bit of the second pointer-sized value (32nd bit on 32-bit memories, 64th bit on 64-bit -memories). String values include their original encoding and length in tagged -code units as a "hint" that enables `store_string` (defined below) to make -better up-front allocation size choices in many cases. Thus, the value produced -by `load_string` isn't simply a Python `str`, but a *tuple* containing a `str`, -the original encoding and the number of source code units. +memories). String values include a "hint" that enables `store_string` (defined +below) to make better up-front allocation size choices in many cases. Thus, the +value produced by `load_string` isn't simply a Python `str`, but a *tuple* +containing a `str`, a `StringHint` and the number of source code units (with +the UTF-16 tag removed). `StringHint` records the source encoding, splitting +`latin1+utf16` into `DYNAMIC_LATIN1` and `DYNAMIC_UTF16` according to the +dynamic choice. Because the hint is independent of the source memory's pointer +size, it can be stored into a memory with a different pointer size (and thus a +different UTF-16 tag bit). The `MAX_STRING_BYTE_LENGTH` constant limits the byte length of a string when loading. This limit is low enough to ensure that when storing the string via @@ -2214,7 +2218,13 @@ short enough to be stored in 32-bit components. The worst case inflation in string byte length is by a factor of 2 (e.g. this could occur when converting from [UTF-8] to [UTF-16]). ```python -String = tuple[str, str, int] +class StringHint(Enum): + UTF8 = 0 + UTF16 = 1 + DYNAMIC_LATIN1 = 2 + DYNAMIC_UTF16 = 3 + +String = tuple[str, StringHint, int] def load_string(cx, ptr) -> String: ptr_size = cx.opts.memory.ptr_size() @@ -2234,20 +2244,28 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String: match cx.opts.string_encoding: case 'utf8': alignment = 1 - byte_length = tagged_code_units + code_units = tagged_code_units + byte_length = code_units encoding = 'utf-8' + hint = StringHint.UTF8 case 'utf16': alignment = 2 - byte_length = 2 * tagged_code_units + code_units = tagged_code_units + byte_length = 2 * code_units encoding = 'utf-16-le' + hint = StringHint.UTF16 case 'latin1+utf16': alignment = 2 if bool(tagged_code_units & tag): - byte_length = 2 * (tagged_code_units ^ tag) + code_units = tagged_code_units ^ tag + byte_length = 2 * code_units encoding = 'utf-16-le' + hint = StringHint.DYNAMIC_UTF16 else: - byte_length = tagged_code_units + code_units = tagged_code_units + byte_length = code_units encoding = 'latin-1' + hint = StringHint.DYNAMIC_LATIN1 trap_if(byte_length > MAX_STRING_BYTE_LENGTH) trap_if(ptr != align_to(ptr, alignment)) @@ -2257,7 +2275,7 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String: except UnicodeError: trap() - return (s, cx.opts.string_encoding, tagged_code_units) + return (s, hint, code_units) ``` Error context values are lifted directly from the current component instance's @@ -2533,12 +2551,11 @@ approach to update the allocation size during the single copy. A blind `realloc` approach would normally suffer from multiple reallocations per string (e.g., using the standard doubling-growth strategy). However, as already shown in `load_string` above, string values come with two useful hints: their -original encoding and number of source [code units]. From this hint data, +`StringHint` and number of source [code units]. From this hint data, `store_string` can do a much better job minimizing the number of reallocations. -We start with a case analysis to enumerate all the meaningful encoding -combinations, subdividing the `latin1+utf16` encoding into either `latin1` or -`utf16` based on the `utf16_tag` flag set by `load_string`: +We start with a case analysis to enumerate all the meaningful combinations of +destination encoding and source `StringHint`: ```python def store_string(cx, v: String, ptr): begin, tagged_code_units = store_string_into_range(cx, v) @@ -2547,42 +2564,29 @@ def store_string(cx, v: String, ptr): store_int(cx, tagged_code_units, ptr + ptr_size, ptr_size) def store_string_into_range(cx, v: String): - src, src_encoding, src_tagged_code_units = v - tag = utf16_tag(cx) - - if src_encoding == 'latin1+utf16': - if bool(src_tagged_code_units & tag): - src_simple_encoding = 'utf16' - src_code_units = src_tagged_code_units ^ tag - else: - src_simple_encoding = 'latin1' - src_code_units = src_tagged_code_units - else: - src_simple_encoding = src_encoding - src_code_units = src_tagged_code_units - + src, src_hint, src_code_units = v match cx.opts.string_encoding: case 'utf8': - match src_simple_encoding: - case 'utf8' : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8') - case 'utf16' : return store_utf16_to_utf8(cx, src, src_code_units) - case 'latin1' : return store_latin1_to_utf8(cx, src, src_code_units) + match src_hint: + case StringHint.UTF8 : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8') + case StringHint.UTF16 : return store_utf16_to_utf8(cx, src, src_code_units) + case StringHint.DYNAMIC_LATIN1 : return store_latin1_to_utf8(cx, src, src_code_units) + case StringHint.DYNAMIC_UTF16 : return store_utf16_to_utf8(cx, src, src_code_units) case 'utf16': - match src_simple_encoding: - case 'utf8' : return store_utf8_to_utf16(cx, src, src_code_units) - case 'utf16' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') - case 'latin1' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + match src_hint: + case StringHint.UTF8 : return store_utf8_to_utf16(cx, src, src_code_units) + case StringHint.UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + case StringHint.DYNAMIC_UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') case 'latin1+utf16': - match src_encoding: - case 'utf8' : return store_string_to_latin1_or_utf16(cx, src, src_code_units) - case 'utf16' : return store_string_to_latin1_or_utf16(cx, src, src_code_units) - case 'latin1+utf16' : - match src_simple_encoding: - case 'latin1' : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1') - case 'utf16' : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units) + match src_hint: + case StringHint.UTF8 : return store_string_to_latin1_or_utf16(cx, src, src_code_units) + case StringHint.UTF16 : return store_string_to_latin1_or_utf16(cx, src, src_code_units) + case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1') + case StringHint.DYNAMIC_UTF16 : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units) ``` -The simplest 4 cases above can compute the exact destination size and then copy +The simplest 5 cases above can compute the exact destination size and then copy with a simply loop (that possibly inflates Latin-1 to UTF-16 by injecting a 0 byte after every Latin-1 byte). ```python @@ -4816,7 +4820,7 @@ def canon_error_context_new(opts, ptr, tagged_code_units): inst = current_instance() trap_if(not inst.may_leave) if DETERMINISTIC_PROFILE or random.randint(0,1): - s = String(('', 'utf8', 0)) + s = String(('', StringHint.UTF8, 0)) else: cx = LiftLowerContext('lift', opts, inst) s = load_string_from_range(cx, ptr, tagged_code_units) diff --git a/design/mvp/canonical-abi/definitions.py b/design/mvp/canonical-abi/definitions.py index 2ebf9581e..78d4eaf6d 100644 --- a/design/mvp/canonical-abi/definitions.py +++ b/design/mvp/canonical-abi/definitions.py @@ -1334,7 +1334,13 @@ def convert_i32_to_char(cx, i): trap_if(0xD800 <= i <= 0xDFFF) return chr(i) -String = tuple[str, str, int] +class StringHint(Enum): + UTF8 = 0 + UTF16 = 1 + DYNAMIC_LATIN1 = 2 + DYNAMIC_UTF16 = 3 + +String = tuple[str, StringHint, int] def load_string(cx, ptr) -> String: ptr_size = cx.opts.memory.ptr_size() @@ -1354,20 +1360,28 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String: match cx.opts.string_encoding: case 'utf8': alignment = 1 - byte_length = tagged_code_units + code_units = tagged_code_units + byte_length = code_units encoding = 'utf-8' + hint = StringHint.UTF8 case 'utf16': alignment = 2 - byte_length = 2 * tagged_code_units + code_units = tagged_code_units + byte_length = 2 * code_units encoding = 'utf-16-le' + hint = StringHint.UTF16 case 'latin1+utf16': alignment = 2 if bool(tagged_code_units & tag): - byte_length = 2 * (tagged_code_units ^ tag) + code_units = tagged_code_units ^ tag + byte_length = 2 * code_units encoding = 'utf-16-le' + hint = StringHint.DYNAMIC_UTF16 else: - byte_length = tagged_code_units + code_units = tagged_code_units + byte_length = code_units encoding = 'latin-1' + hint = StringHint.DYNAMIC_LATIN1 trap_if(byte_length > MAX_STRING_BYTE_LENGTH) trap_if(ptr != align_to(ptr, alignment)) @@ -1377,7 +1391,7 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String: except UnicodeError: trap() - return (s, cx.opts.string_encoding, tagged_code_units) + return (s, hint, code_units) def lift_error_context(cx, i): errctx = cx.inst.handles.get(i) @@ -1552,39 +1566,26 @@ def store_string(cx, v: String, ptr): store_int(cx, tagged_code_units, ptr + ptr_size, ptr_size) def store_string_into_range(cx, v: String): - src, src_encoding, src_tagged_code_units = v - tag = utf16_tag(cx) - - if src_encoding == 'latin1+utf16': - if bool(src_tagged_code_units & tag): - src_simple_encoding = 'utf16' - src_code_units = src_tagged_code_units ^ tag - else: - src_simple_encoding = 'latin1' - src_code_units = src_tagged_code_units - else: - src_simple_encoding = src_encoding - src_code_units = src_tagged_code_units - + src, src_hint, src_code_units = v match cx.opts.string_encoding: case 'utf8': - match src_simple_encoding: - case 'utf8' : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8') - case 'utf16' : return store_utf16_to_utf8(cx, src, src_code_units) - case 'latin1' : return store_latin1_to_utf8(cx, src, src_code_units) + match src_hint: + case StringHint.UTF8 : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8') + case StringHint.UTF16 : return store_utf16_to_utf8(cx, src, src_code_units) + case StringHint.DYNAMIC_LATIN1 : return store_latin1_to_utf8(cx, src, src_code_units) + case StringHint.DYNAMIC_UTF16 : return store_utf16_to_utf8(cx, src, src_code_units) case 'utf16': - match src_simple_encoding: - case 'utf8' : return store_utf8_to_utf16(cx, src, src_code_units) - case 'utf16' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') - case 'latin1' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + match src_hint: + case StringHint.UTF8 : return store_utf8_to_utf16(cx, src, src_code_units) + case StringHint.UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') + case StringHint.DYNAMIC_UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le') case 'latin1+utf16': - match src_encoding: - case 'utf8' : return store_string_to_latin1_or_utf16(cx, src, src_code_units) - case 'utf16' : return store_string_to_latin1_or_utf16(cx, src, src_code_units) - case 'latin1+utf16' : - match src_simple_encoding: - case 'latin1' : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1') - case 'utf16' : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units) + match src_hint: + case StringHint.UTF8 : return store_string_to_latin1_or_utf16(cx, src, src_code_units) + case StringHint.UTF16 : return store_string_to_latin1_or_utf16(cx, src, src_code_units) + case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1') + case StringHint.DYNAMIC_UTF16 : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units) def store_string_copy(cx, src, src_code_units, dst_code_unit_size, dst_alignment, dst_encoding): dst_byte_length = dst_code_unit_size * src_code_units @@ -2693,7 +2694,7 @@ def canon_error_context_new(opts, ptr, tagged_code_units): inst = current_instance() trap_if(not inst.may_leave) if DETERMINISTIC_PROFILE or random.randint(0,1): - s = String(('', 'utf8', 0)) + s = String(('', StringHint.UTF8, 0)) else: cx = LiftLowerContext('lift', opts, inst) s = load_string_from_range(cx, ptr, tagged_code_units) diff --git a/design/mvp/canonical-abi/run_tests.py b/design/mvp/canonical-abi/run_tests.py index 73561b290..8d71fa672 100644 --- a/design/mvp/canonical-abi/run_tests.py +++ b/design/mvp/canonical-abi/run_tests.py @@ -72,7 +72,7 @@ def on_cancel(): return func_inst def mk_str(s): - return (s, 'utf8', len(s.encode('utf-8'))) + return (s, StringHint.UTF8, len(s.encode('utf-8'))) def mk_tup(*a): def mk_tup_rec(x): @@ -118,13 +118,15 @@ def test(t, vals_to_lift, v, cx = mk_cx(), dst_encoding = None, lower_t = None, - lower_v = None): + lower_v = None, + dst_addr_type = None, + lowered_vals = None): def test_name(): return "test({},{},{}):".format(t, vals_to_lift, v) error = None def thread_func(): - nonlocal error, cx, dst_encoding, lower_t, lower_v + nonlocal error, cx, dst_encoding, lower_t, lower_v, dst_addr_type try: vi = CoreValueIter(vals_to_lift) @@ -148,10 +150,14 @@ def lift_expecting_trap(): heap = Heap(5*len(cx.opts.memory)) if dst_encoding is None: dst_encoding = cx.opts.string_encoding - cx = mk_cx(MemInst(heap.memory, cx.opts.memory.ptr_type()), dst_encoding, heap.realloc) - lowered_vals = lower_flat(cx, v, lower_t) - - vi = CoreValueIter(lowered_vals) + if dst_addr_type is None: + dst_addr_type = cx.opts.memory.ptr_type() + cx = mk_cx(MemInst(heap.memory, dst_addr_type), dst_encoding, heap.realloc) + got_lowered_vals = lower_flat(cx, v, lower_t) + if lowered_vals is not None and got_lowered_vals != lowered_vals: + fail("{} lower_flat() expected {} but got {}".format(test_name(), lowered_vals, got_lowered_vals)) + + vi = CoreValueIter(got_lowered_vals) got = lift_flat(cx, vi, lower_t) if not equal_modulo_string_encoding(got, lower_v): fail("{} re-lift expected {} but got {}".format(test_name(), lower_v, got)) @@ -266,44 +272,62 @@ def test_nan64(inbits, outbits): test_nan64(0x7ff0000000000000, 0x7ff0000000000000) test_nan64(0x3ff0000000000000, 0x3ff0000000000000) -def test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, addr_type='i32'): +def test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, hint, + src_addr_type='i32', dst_addr_type='i32', lowered_vals=None): heap = Heap(len(encoded)) heap.memory[:] = encoded[:] - cx = mk_cx(MemInst(heap.memory, addr_type), src_encoding) - v = (s, src_encoding, tagged_code_units) - test(StringType(), [0, tagged_code_units], v, cx, dst_encoding) - -def test_string(src_encoding, dst_encoding, s, addr_type='i32'): + cx = mk_cx(MemInst(heap.memory, src_addr_type), src_encoding) + tag = 1 << (ptr_size(src_addr_type) * 8 - 1) + code_units = tagged_code_units & ~tag + v = (s, hint, code_units) + test(StringType(), [0, tagged_code_units], v, cx, dst_encoding, + dst_addr_type = dst_addr_type, lowered_vals = lowered_vals) + +def test_string(src_encoding, dst_encoding, s, src_addr_type='i32', dst_addr_type='i32'): + def run(encoded, tagged_code_units, hint): + test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, hint, + src_addr_type, dst_addr_type) if src_encoding == 'utf8': encoded = s.encode('utf-8') - tagged_code_units = len(encoded) - test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, addr_type) + run(encoded, len(encoded), StringHint.UTF8) elif src_encoding == 'utf16': encoded = s.encode('utf-16-le') - tagged_code_units = int(len(encoded) / 2) - test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, addr_type) + run(encoded, int(len(encoded) / 2), StringHint.UTF16) elif src_encoding == 'latin1+utf16': try: encoded = s.encode('latin-1') - tagged_code_units = len(encoded) - test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, addr_type) + run(encoded, len(encoded), StringHint.DYNAMIC_LATIN1) except UnicodeEncodeError: pass encoded = s.encode('utf-16-le') - tagged_code_units = int(len(encoded) / 2) | (1 << (ptr_size(addr_type) * 8 - 1)) - test_string_internal(src_encoding, dst_encoding, s, encoded, tagged_code_units, addr_type) + tag = 1 << (ptr_size(src_addr_type) * 8 - 1) + run(encoded, int(len(encoded) / 2) | tag, StringHint.DYNAMIC_UTF16) encodings = ['utf8', 'utf16', 'latin1+utf16'] fun_strings = ['', 'a', 'hi', '\x00', 'a\x00b', '\x80', '\x80b', 'ab\xefc', - '\u01ffy', 'xy\u01ff', 'a\ud7ffb', 'a\u02ff\u03ff\u04ffbc', - '\uf123', '\uf123\uf123abc', 'abcdef\uf123'] - -for addr_type in ['i32', 'i64']: - for src_encoding in encodings: - for dst_encoding in encodings: - for s in fun_strings: - test_string(src_encoding, dst_encoding, s, addr_type) + 'ǿy', 'xyǿ', 'a퟿b', 'a˿Ͽӿbc', + '', 'abc', 'abcdef'] + +for src_addr_type in ['i32', 'i64']: + for dst_addr_type in ['i32', 'i64']: + for src_encoding in encodings: + for dst_encoding in encodings: + for s in fun_strings: + test_string(src_encoding, dst_encoding, s, src_addr_type, dst_addr_type) + +# A `latin1+utf16` string's UTF-16 tag is read from the source memory's high +# pointer bit and written to the destination memory's. +for src_addr_type, dst_addr_type in [('i32', 'i64'), ('i64', 'i32')]: + src_tag = 1 << (ptr_size(src_addr_type) * 8 - 1) + dst_tag = 1 << (ptr_size(dst_addr_type) * 8 - 1) + def t(s, dst_encoding, lowered_vals): + test_string_internal('latin1+utf16', dst_encoding, s, s.encode('utf-16-le'), len(s) | src_tag, + StringHint.DYNAMIC_UTF16, src_addr_type, dst_addr_type, lowered_vals) + t('a', 'latin1+utf16', [0, 2 | dst_tag]) + t('ab', 'latin1+utf16', [4, 2]) + t('a', 'utf16', [0, 2]) + t('a', 'utf8', [2, 4]) def test_heap(t, expect, args, byte_array, addr_type='i32'): heap = Heap(byte_array) diff --git a/test/nyi.txt b/test/nyi.txt index ec1ec1d78..4dcc5c205 100644 --- a/test/nyi.txt +++ b/test/nyi.txt @@ -4,3 +4,4 @@ ./async/big-interleaving-test.wast ./async/forward.wast ./async/exit-without-resolving.wast +./values/transcode-memory64.wast diff --git a/test/values/transcode-memory64.wast b/test/values/transcode-memory64.wast new file mode 100644 index 000000000..086337e64 --- /dev/null +++ b/test/values/transcode-memory64.wast @@ -0,0 +1,230 @@ +;; Test latin1+utf16 strings passed between components whose memories have +;; different address types. + +;; i64 caller -> i32 callee: utf16-tagged input whose code points all fit in +;; latin1 must arrive deflated to latin1; wide content stays utf16. +(component + (component $C + (core module $M + (memory (export "mem") 1) + (global $next (mut i32) (i32.const 1024)) + (func (export "realloc") (param $old i32) (param $osize i32) (param $align i32) (param $nsize i32) (result i32) + (local $r i32) + ;; shrinking realloc: keep the (8-aligned) allocation in place + (if (i32.and (i32.ne (local.get $old) (i32.const 0)) + (i32.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + ;; fresh 8-aligned bump allocation + (global.set $next (i32.and (i32.add (global.get $next) (i32.const 7)) (i32.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i32.add (global.get $next) (local.get $nsize))) + ;; growing realloc must preserve the old contents + (if (i32.ne (local.get $old) (i32.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + + ;; response "øk" latin1 = F8 6B (2 code units, untagged) + (data (i32.const 64) "\f8\6b") + ;; response "n☃" utf16 = 006E 2603 (2 code units, tagged) + (data (i32.const 68) "\6e\00\03\26") + + ;; expect "AB" deflated to latin1 = 41 42 (2 code units, untagged) + (func (export "deflate") (param $ptr i32) (param $tagged i32) (result i32) + (if (i32.ne (local.get $tagged) (i32.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x4241)) (then unreachable)) + (i32.store (i32.const 0) (i32.const 64)) + (i32.store (i32.const 4) (i32.const 2)) + (i32.const 0)) + + ;; expect "☃" kept as utf16 = 2603 (1 code unit, i32-tagged) + (func (export "keep-wide") (param $ptr i32) (param $tagged i32) (result i32) + (if (i32.ne (local.get $tagged) (i32.const 0x80000001)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x2603)) (then unreachable)) + (i32.store (i32.const 0) (i32.const 68)) + (i32.store (i32.const 4) (i32.const 0x80000002)) + (i32.const 0)) + ) + (core instance $m (instantiate $M)) + (func (export "deflate") (param "s" string) (result string) + (canon lift (core func $m "deflate") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + (func (export "keep-wide") (param "s" string) (result string) + (canon lift (core func $m "keep-wide") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + ) + + (component $D + (import "deflate" (func $deflate (param "s" string) (result string))) + (import "keep-wide" (func $keep-wide (param "s" string) (result string))) + (core module $Libc + (memory (export "mem") i64 1) + (global $next (mut i64) (i64.const 1024)) + (func (export "realloc") (param $old i64) (param $osize i64) (param $align i64) (param $nsize i64) (result i64) + (local $r i64) + (if (i32.and (i64.ne (local.get $old) (i64.const 0)) + (i64.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i64.and (i64.add (global.get $next) (i64.const 7)) (i64.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i64.add (global.get $next) (local.get $nsize))) + (if (i64.ne (local.get $old) (i64.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + ) + (core instance $libc (instantiate $Libc)) + (core func $deflate' (canon lower (func $deflate) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core func $keep-wide' (canon lower (func $keep-wide) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core module $Main + (import "" "mem" (memory i64 1)) + (import "" "deflate" (func $deflate (param i64 i64 i64))) + (import "" "keep-wide" (func $keep-wide (param i64 i64 i64))) + + ;; argument "AB" over-encoded as utf16 = 0041 0042 (2 code units, tagged) + (data (i64.const 16) "\41\00\42\00") + ;; argument "☃" utf16 = 2603 (1 code unit, tagged) + (data (i64.const 24) "\03\26") + + (func (export "run") (result i32) + (local $p i64) + (call $deflate (i64.const 16) (i64.const 0x8000000000000002) (i64.const 32)) + ;; expect "øk" latin1 = F8 6B (2 code units, untagged) + (local.set $p (i64.load (i64.const 32))) + (if (i64.ne (i64.load (i64.const 40)) (i64.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $p)) (i32.const 0x6BF8)) (then unreachable)) + (call $keep-wide (i64.const 24) (i64.const 0x8000000000000001) (i64.const 32)) + ;; expect "n☃" utf16 = 006E 2603 (2 code units, i64-tagged) + (local.set $p (i64.load (i64.const 32))) + (if (i64.ne (i64.load (i64.const 40)) (i64.const 0x8000000000000002)) (then unreachable)) + (if (i32.ne (i32.load (local.get $p)) (i32.const 0x2603006E)) (then unreachable)) + (i32.const 42)) + ) + (core instance $main (instantiate $Main (with "" (instance + (export "mem" (memory $libc "mem")) + (export "deflate" (func $deflate')) + (export "keep-wide" (func $keep-wide')) + )))) + (func (export "run") (result u32) (canon lift (core func $main "run"))) + ) + + (instance $c (instantiate $C)) + (instance $d (instantiate $D + (with "deflate" (func $c "deflate")) + (with "keep-wide" (func $c "keep-wide")) + )) + (func (export "run") (alias export $d "run")) +) +(assert_return (invoke "run") (u32.const 42)) + +;; i32 caller -> i64 callee: the same strings in the other direction. +(component + (component $C + (core module $M + (memory (export "mem") i64 1) + (global $next (mut i64) (i64.const 1024)) + (func (export "realloc") (param $old i64) (param $osize i64) (param $align i64) (param $nsize i64) (result i64) + (local $r i64) + (if (i32.and (i64.ne (local.get $old) (i64.const 0)) + (i64.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i64.and (i64.add (global.get $next) (i64.const 7)) (i64.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i64.add (global.get $next) (local.get $nsize))) + (if (i64.ne (local.get $old) (i64.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + + ;; response "øk" latin1 = F8 6B (2 code units, untagged) + (data (i64.const 64) "\f8\6b") + ;; response "n☃" utf16 = 006E 2603 (2 code units, tagged) + (data (i64.const 68) "\6e\00\03\26") + + ;; expect "AB" deflated to latin1 = 41 42 (2 code units, untagged) + (func (export "deflate") (param $ptr i64) (param $tagged i64) (result i64) + (if (i64.ne (local.get $tagged) (i64.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x4241)) (then unreachable)) + (i64.store (i64.const 0) (i64.const 64)) + (i64.store (i64.const 8) (i64.const 2)) + (i64.const 0)) + + ;; expect "☃" kept as utf16 = 2603 (1 code unit, i64-tagged) + (func (export "keep-wide") (param $ptr i64) (param $tagged i64) (result i64) + (if (i64.ne (local.get $tagged) (i64.const 0x8000000000000001)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $ptr)) (i32.const 0x2603)) (then unreachable)) + (i64.store (i64.const 0) (i64.const 68)) + (i64.store (i64.const 8) (i64.const 0x8000000000000002)) + (i64.const 0)) + ) + (core instance $m (instantiate $M)) + (func (export "deflate") (param "s" string) (result string) + (canon lift (core func $m "deflate") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + (func (export "keep-wide") (param "s" string) (result string) + (canon lift (core func $m "keep-wide") string-encoding=latin1+utf16 + (memory (core memory $m "mem")) (realloc (core func $m "realloc")))) + ) + + (component $D + (import "deflate" (func $deflate (param "s" string) (result string))) + (import "keep-wide" (func $keep-wide (param "s" string) (result string))) + (core module $Libc + (memory (export "mem") 1) + (global $next (mut i32) (i32.const 1024)) + (func (export "realloc") (param $old i32) (param $osize i32) (param $align i32) (param $nsize i32) (result i32) + (local $r i32) + (if (i32.and (i32.ne (local.get $old) (i32.const 0)) + (i32.le_u (local.get $nsize) (local.get $osize))) + (then (return (local.get $old)))) + (global.set $next (i32.and (i32.add (global.get $next) (i32.const 7)) (i32.const -8))) + (local.set $r (global.get $next)) + (global.set $next (i32.add (global.get $next) (local.get $nsize))) + (if (i32.ne (local.get $old) (i32.const 0)) + (then (memory.copy (local.get $r) (local.get $old) (local.get $osize)))) + (local.get $r)) + ) + (core instance $libc (instantiate $Libc)) + (core func $deflate' (canon lower (func $deflate) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core func $keep-wide' (canon lower (func $keep-wide) string-encoding=latin1+utf16 + (memory (core memory $libc "mem")) (realloc (core func $libc "realloc")))) + (core module $Main + (import "" "mem" (memory 1)) + (import "" "deflate" (func $deflate (param i32 i32 i32))) + (import "" "keep-wide" (func $keep-wide (param i32 i32 i32))) + + ;; argument "AB" over-encoded as utf16 = 0041 0042 (2 code units, tagged) + (data (i32.const 16) "\41\00\42\00") + ;; argument "☃" utf16 = 2603 (1 code unit, tagged) + (data (i32.const 24) "\03\26") + + (func (export "run") (result i32) + (local $p i32) + (call $deflate (i32.const 16) (i32.const 0x80000002) (i32.const 8)) + ;; expect "øk" latin1 = F8 6B (2 code units, untagged) + (local.set $p (i32.load (i32.const 8))) + (if (i32.ne (i32.load (i32.const 12)) (i32.const 2)) (then unreachable)) + (if (i32.ne (i32.load16_u (local.get $p)) (i32.const 0x6BF8)) (then unreachable)) + (call $keep-wide (i32.const 24) (i32.const 0x80000001) (i32.const 8)) + ;; expect "n☃" utf16 = 006E 2603 (2 code units, i32-tagged) + (local.set $p (i32.load (i32.const 8))) + (if (i32.ne (i32.load (i32.const 12)) (i32.const 0x80000002)) (then unreachable)) + (if (i32.ne (i32.load (local.get $p)) (i32.const 0x2603006E)) (then unreachable)) + (i32.const 43)) + ) + (core instance $main (instantiate $Main (with "" (instance + (export "mem" (memory $libc "mem")) + (export "deflate" (func $deflate')) + (export "keep-wide" (func $keep-wide')) + )))) + (func (export "run") (result u32) (canon lift (core func $main "run"))) + ) + + (instance $c (instantiate $C)) + (instance $d (instantiate $D + (with "deflate" (func $c "deflate")) + (with "keep-wide" (func $c "keep-wide")) + )) + (func (export "run") (alias export $d "run")) +) +(assert_return (invoke "run") (u32.const 43))