feat: Add memory.dataUTF8() built-in function#2974
feat: Add memory.dataUTF8() built-in function#2974geraintluff wants to merge 4 commits intoAssemblyScript:mainfrom
memory.dataUTF8() built-in function#2974Conversation
| let arrayNullTerminated = new Uint8Array(array.length + 1); | ||
| arrayNullTerminated.set(array); | ||
| offset = compiler.addAlignedMemorySegment(arrayNullTerminated, 1).offset; | ||
| // FIXME: what if recompiles happen? recompiles are bad. |
There was a problem hiding this comment.
This FIXME is copied from the memory.data<T> implementation just above. Tbh I don't understand it, but if it's a problem there, then it's a problem here.
|
If you want to see why I proposed this: here's a diff |
memory.dataUTF8() built-in functionmemory.dataUTF8() built-in function
|
@dcodeIO I wrote a It silently encodes unpaired surrogates (WTF16) as code-points, which is the same behaviour as |
|
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! |
Implements #2972, providing a way to embed UTF-8 string constants.
Rationale
UTF-16 strings are naturally embedded in the data section if you use a string constant (
"...") in your code.If what you need is a UTF-8 string, then storing as WTF-16 wastes binary space and execution time.
Current workaround
You can currently achieve the same result by using
memory.data<u8>([..., 0])with the appropriate UTF-8 bytes, but reading/changing/reviewing this code is more difficult.Changes
memory.dataUTF8(<string constant>)function in standard librarytests/compiler/memory.tsnpm run checkshows no errors for the files I touched. (Existing errors intests/import/index.tsare unrelated.)