Skip to content

Conversation

@geraintluff
Copy link

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

  • Declare memory.dataUTF8(<string constant>) function in standard library
  • Implement the built-in in the compiler
  • Add test to tests/compiler/memory.ts

npm run check shows no errors for the files I touched. (Existing errors in tests/import/index.ts are unrelated.)

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

let arrayNullTerminated = new Uint8Array(array.length + 1);
arrayNullTerminated.set(array);
offset = compiler.addAlignedMemorySegment(arrayNullTerminated, 1).offset;
// FIXME: what if recompiles happen? recompiles are bad.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@geraintluff
Copy link
Author

If you want to see why I proposed this: here's a diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant