Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stringref operations #2687

Closed
wants to merge 10 commits into from
Closed

Implement stringref operations #2687

wants to merge 10 commits into from

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented Apr 19, 2023

Implements the current state of the Reference-Typed Strings proposal (currently phase 1) as supported by Binaryen, for now as built-ins to enable experimentation. A constant reference-typed string can be created using a string literal in a ref_string context.

Enable with --enable stringref in AS, --experimental-wasm-stringref in V8.

Instruction Binaryen status
string.const ✔️
string.new_utf8 ⏳ Assumes single memory
string.new_utf8_array ✔️
string.new_lossy_utf8 ⏳ Assumes single memory
string.new_lossy_utf8_array ✔️
string.new_wtf8 ⏳ Assumes single memory
string.new_wtf8_array ✔️
string.new_wtf16 ⏳ Assumes single memory
string.new_wtf16_array ✔️
string.from_code_point ✔️
string.hash ✔️
string.measure_utf8 ✔️
string.measure_wtf8 ✔️
string.measure_wtf16 ✔️
string.is_usv_sequence ✔️
string.encode_utf8 ⏳ Assumes single memory
string.encode_utf8_array ✔️
string.encode_lossy_utf8
string.encode_lossy_utf8_array
string.encode_wtf8 ⏳ Assumes single memory
string.encode_wtf8_array ✔️
string.encode_wtf16 ⏳ Assumes single memory
string.encode_wtf16_array ✔️
string.concat ✔️
string.eq ✔️
string.compare ✔️
string.as_wtf8 ✔️
string.as_wtf16 ✔️
string.as_iter ✔️
stringview_wtf8.advance ✔️
stringview_wtf8.encode_utf8
stringview_wtf8.encode_lossy_utf8
stringview_wtf8.encode_wtf8
stringview_wtf8.slice ✔️
stringview_wtf16.length ✔️
stringview_wtf16.slice ✔️
stringview_wtf16.get_codeunit ✔️
stringview_wtf16.encode
stringview_iter.next ✔️
stringview_iter.advance ✔️
stringview_iter.rewind ✔️
stringview_iter.slice ✔️

✔️ Available   ⏳ Available but incomplete   ❌ Not available

Possible follow-up work in case there is interest: Populate the RefString wrapper class using the instructions to mimic String, so it can eventually replace String. So far this PR implements a few string functions as a POC.

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

@dcodeIO
Copy link
Member Author

dcodeIO commented Apr 22, 2023

With #2689 in place, instructions that technically accept but would throw when provided a stringref that is null now take ref_string (non-nullable stringref), effectively eliminating the possibility of such traps statically per the type system. Similarly, instructions that cannot produce null use ref_string as their return type, with tests showing that V8 materializes a non-nullable type there as well, i.e. respective assignments succeeding. One exception is string.eq, as it can deal with null inputs.

@dcodeIO
Copy link
Member Author

dcodeIO commented Apr 23, 2023

There is some proof of concept code now, showing how RefString can be populated with functionality utilizing the stringref instruction set. Basically, in a ref_string type context, a string literal compiles to a string.const, making a reference-typed string. Such a value of type ref_string is backed by the RefString wrapper class (in std/assembly/references.ts) providing the JS-like API that mostly vanishes after optimizations, leaving primarily the string instructions. So the underlying Wasm type in AS is simply (ref string) that can be exchanged with JS and Web APIs by reference.

@dcodeIO
Copy link
Member Author

dcodeIO commented Aug 1, 2023

Quick update: There has been a more general string discussion in CG context meanwhile. Iiuc, an alternative to stringref that instead uses (non-portable?) compile time imports for common string operations has been discussed. Also noteworthy:

The Wasmtime project would prefer not to implement stringref. We don't have a managed host string to expose to Wasm guests, and we don't want to have to implement one. We would rather that Wasm guests implement their own strings on top of the GC proposal, where they can build exactly what they need, which can be very different across languages. As far as interoperability of strings across different languages goes, we believe this should be addressed at the component model and interface types layer. For tight integration with JS strings on the Web, string imports seem like a good solution.

Translated: Our thing that intentionally does not address the thing should be off-label responsible for the thing so we can not address the thing and eventually the Web becomes the special case - it's BusinessWasmtime!

@github-actions
Copy link

This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

@github-actions github-actions bot added the stale label Oct 17, 2023
@github-actions
Copy link

This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards.

@github-actions github-actions bot closed this Oct 24, 2023
@CountBleck CountBleck reopened this Oct 24, 2023
@github-actions github-actions bot removed the stale label Oct 25, 2023
Copy link

This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

@github-actions github-actions bot added the stale label Dec 25, 2023
Copy link

github-actions bot commented Jan 2, 2024

This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards.

@github-actions github-actions bot closed this Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants