-
Notifications
You must be signed in to change notification settings - Fork 793
[Strings] Add a string lowering pass using magic imports #6497
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,7 +213,8 @@ std::optional<uint16_t> takeWTF16CodeUnit(std::string_view& str) { | |
return u; | ||
} | ||
|
||
std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str) { | ||
std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str, | ||
bool allowWTF = true) { | ||
auto u = takeWTF16CodeUnit(str); | ||
if (!u) { | ||
return std::nullopt; | ||
|
@@ -228,7 +229,13 @@ std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str) { | |
uint16_t highBits = *u - 0xD800; | ||
uint16_t lowBits = *low - 0xDC00; | ||
return 0x10000 + ((highBits << 10) | lowBits); | ||
} else if (!allowWTF) { | ||
// Unpaired high surrogate. | ||
return std::nullopt; | ||
} | ||
} else if (!allowWTF && 0xDC00 <= *u && *u < 0xE000) { | ||
// Unpaired low surrogate. | ||
return std::nullopt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these bugfixes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is newly necessary to catch strings that are valid WTF-16 but not valid UTF-16. |
||
} | ||
|
||
return *u; | ||
|
@@ -242,6 +249,23 @@ void writeWTF16CodeUnit(std::ostream& os, uint16_t u) { | |
|
||
constexpr uint32_t replacementCharacter = 0xFFFD; | ||
|
||
bool doConvertWTF16ToWTF8(std::ostream& os, | ||
std::string_view str, | ||
bool allowWTF) { | ||
bool valid = true; | ||
|
||
while (str.size()) { | ||
auto u = takeWTF16CodePoint(str, allowWTF); | ||
if (!u) { | ||
valid = false; | ||
u = replacementCharacter; | ||
} | ||
writeWTF8CodePoint(os, *u); | ||
} | ||
|
||
return valid; | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
std::ostream& writeWTF8CodePoint(std::ostream& os, uint32_t u) { | ||
|
@@ -308,18 +332,11 @@ bool convertWTF8ToWTF16(std::ostream& os, std::string_view str) { | |
} | ||
|
||
bool convertWTF16ToWTF8(std::ostream& os, std::string_view str) { | ||
bool valid = true; | ||
|
||
while (str.size()) { | ||
auto u = takeWTF16CodePoint(str); | ||
if (!u) { | ||
valid = false; | ||
u = replacementCharacter; | ||
} | ||
writeWTF8CodePoint(os, *u); | ||
} | ||
return doConvertWTF16ToWTF8(os, str, true); | ||
} | ||
|
||
return valid; | ||
bool convertUTF16ToUTF8(std::ostream& os, std::string_view str) { | ||
return doConvertWTF16ToWTF8(os, str, false); | ||
} | ||
|
||
std::ostream& printEscapedJSON(std::ostream& os, std::string_view str) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. | ||
|
||
;; RUN: wasm-opt %s -all --string-lowering-magic-imports --remove-unused-module-elements -S -o - | filecheck %s | ||
;; RUN: wasm-opt %s -all --string-lowering-magic-imports --remove-unused-module-elements --roundtrip -S -o - | filecheck %s --check-prefix=RTRIP | ||
|
||
(module | ||
;; CHECK: (type $0 (func)) | ||
|
||
;; CHECK: (import "\'" "bar" (global $string.const_bar (ref extern))) | ||
|
||
;; CHECK: (import "\'" "foo" (global $string.const_foo (ref extern))) | ||
|
||
;; CHECK: (import "\'" "needs\tescaping\00.\'#%- .\r\n\\08\0c\n\r\t.\ea\99\ae" (global $"string.const_needs\tescaping\00.\'#%- .\r\n\\08\0c\n\r\t.\ea\99\ae" (ref extern))) | ||
|
||
;; CHECK: (import "string.const" "0" (global $"string.const_unpaired high surrogate \ed\a0\80 " (ref extern))) | ||
|
||
;; CHECK: (import "string.const" "1" (global $"string.const_unpaired low surrogate \ed\bd\88 " (ref extern))) | ||
|
||
;; CHECK: (export "consts" (func $consts)) | ||
|
||
;; CHECK: (func $consts (type $0) | ||
;; CHECK-NEXT: (drop | ||
;; CHECK-NEXT: (global.get $string.const_foo) | ||
;; CHECK-NEXT: ) | ||
;; CHECK-NEXT: (drop | ||
;; CHECK-NEXT: (global.get $string.const_bar) | ||
;; CHECK-NEXT: ) | ||
;; CHECK-NEXT: (drop | ||
;; CHECK-NEXT: (global.get $"string.const_needs\tescaping\00.\'#%- .\r\n\\08\0c\n\r\t.\ea\99\ae") | ||
;; CHECK-NEXT: ) | ||
;; CHECK-NEXT: (drop | ||
;; CHECK-NEXT: (global.get $"string.const_unpaired high surrogate \ed\a0\80 ") | ||
;; CHECK-NEXT: ) | ||
;; CHECK-NEXT: (drop | ||
;; CHECK-NEXT: (global.get $"string.const_unpaired low surrogate \ed\bd\88 ") | ||
;; CHECK-NEXT: ) | ||
;; CHECK-NEXT: ) | ||
;; RTRIP: (type $0 (func)) | ||
|
||
;; RTRIP: (import "\'" "bar" (global $gimport$0 (ref extern))) | ||
|
||
;; RTRIP: (import "\'" "foo" (global $gimport$1 (ref extern))) | ||
|
||
;; RTRIP: (import "\'" "needs\tescaping\00.\'#%- .\r\n\\08\0c\n\r\t.\ea\99\ae" (global $gimport$2 (ref extern))) | ||
|
||
;; RTRIP: (import "string.const" "0" (global $gimport$3 (ref extern))) | ||
|
||
;; RTRIP: (import "string.const" "1" (global $gimport$4 (ref extern))) | ||
|
||
;; RTRIP: (export "consts" (func $consts)) | ||
|
||
;; RTRIP: (func $consts (type $0) | ||
;; RTRIP-NEXT: (drop | ||
;; RTRIP-NEXT: (global.get $gimport$1) | ||
;; RTRIP-NEXT: ) | ||
;; RTRIP-NEXT: (drop | ||
;; RTRIP-NEXT: (global.get $gimport$0) | ||
;; RTRIP-NEXT: ) | ||
;; RTRIP-NEXT: (drop | ||
;; RTRIP-NEXT: (global.get $gimport$2) | ||
;; RTRIP-NEXT: ) | ||
;; RTRIP-NEXT: (drop | ||
;; RTRIP-NEXT: (global.get $gimport$3) | ||
;; RTRIP-NEXT: ) | ||
;; RTRIP-NEXT: (drop | ||
;; RTRIP-NEXT: (global.get $gimport$4) | ||
;; RTRIP-NEXT: ) | ||
;; RTRIP-NEXT: ) | ||
(func $consts (export "consts") | ||
(drop | ||
(string.const "foo") | ||
) | ||
(drop | ||
(string.const "bar") | ||
) | ||
(drop | ||
(string.const "needs\tescaping\00.'#%- .\r\n\\08\0C\0A\0D\09.ꙮ") | ||
) | ||
(drop | ||
(string.const "unpaired high surrogate \ED\A0\80 ") | ||
) | ||
(drop | ||
(string.const "unpaired low surrogate \ED\BD\88 ") | ||
) | ||
) | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/jk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🪄 ✨