Skip to content

Conversation

@tlively
Copy link
Member

@tlively tlively commented Aug 21, 2024

The best way to lower strings is via the "magic imports" API that uses
the names of imported string globals as their values. This approach only
works for valid UTF-8 strings, though. The existing
string-lowering-magic-imports pass falls back to putting non-UTF-8
strings in a JSON custom section, but this requires the runtime to
support that custom section for correctness. To help catch errors early
when runtimes do not support the strings custom section, add a new pass
that uses magic imports and raises an error if there are any invalid
strings.

The best way to lower strings is via the "magic imports" API that uses
the names of imported string globals as their values. This approach only
works for valid UTF-8 strings, though. The existing
string-lowering-magic-imports pass falls back to putting non-UTF-8
strings in a JSON custom section, but this requires the runtime to
support that custom section for correctness. To help catch errors early
when runtimes do not support the strings custom section, add a new pass
that uses magic imports and raises an error if there are any invalid
strings.
@tlively tlively requested a review from kripken August 21, 2024 18:41
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

lgtm % suggestion to make the new mode depend on magic imports being set, which seems a bit simpler.

StringLowering(bool useMagicImports = false)
: useMagicImports(useMagicImports) {}
// Whether to throw a fatal error on non-UTF8 strings that would not be able
// to use the "magic import" mechanism.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// to use the "magic import" mechanism.
// to use the "magic import" mechanism. This is only relevant when useMagicImports.

bool assertUTF8;

StringLowering(bool useMagicImports = false, bool assertUTF8 = false)
: useMagicImports(useMagicImports), assertUTF8(assertUTF8) {}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
: useMagicImports(useMagicImports), assertUTF8(assertUTF8) {}
: useMagicImports(useMagicImports), assertUTF8(assertUTF8) {
// if we assert UTF8, we must have enabled magic imports.
assert(!assertUTF8 || useMagicImports);
}

global->module = "'";
global->base = Name(utf8.str());
} else {
if (useMagicImports && assertUTF8) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (useMagicImports && assertUTF8) {
if (assertUTF8) {

@tlively tlively merged commit 692e55c into main Aug 21, 2024
@tlively tlively deleted the strings-magic-import-assert-valid branch August 21, 2024 22:05
@gkdn gkdn mentioned this pull request Aug 31, 2024
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.

3 participants