Skip to content

Commit 12396df

Browse files
authored
Simplify test_utf. NFC (#24274)
- Use stringToUTFOnStack to avoid malloc and free - Use separate params to `out` rather than using `join()` - Use correct printf args to simplify C printf call - Format JS to its more readable
1 parent 4698834 commit 12396df

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

test/core/test_utf.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111

1212
int main() {
1313
char *c = "μ†ℱ ╋ℯ╳╋ 😇";
14-
printf("%d %d %d %d %s\n", c[0] & 0xff, c[1] & 0xff, c[2] & 0xff, c[3] & 0xff,
15-
c);
14+
printf("%hhu %hhu %hhu %hhu %s\n", c[0], c[1], c[2], c[3], c);
1615
emscripten_run_script(
17-
"var cheez = _malloc(100);"
18-
"Module.stringToUTF8(\"μ†ℱ ╋ℯ╳╋ 😇\", cheez, 100);"
19-
"out([UTF8ToString(cheez), Module.getValue(cheez, "
20-
"'i8')&0xff, Module.getValue(cheez+1, 'i8')&0xff, "
21-
"Module.getValue(cheez+2, 'i8')&0xff, Module.getValue(cheez+3, "
22-
"'i8')&0xff].join(','));"
23-
"_free(cheez);"
24-
);
16+
"var cheez = Module.stringToUTF8OnStack(\"μ†ℱ ╋ℯ╳╋ 😇\");"
17+
"out(UTF8ToString(cheez), Module.getValue(cheez+0, 'i8')&0xff, "
18+
" Module.getValue(cheez+1, 'i8')&0xff, "
19+
" Module.getValue(cheez+2, 'i8')&0xff, "
20+
" Module.getValue(cheez+3, 'i8')&0xff);");
2521
}

test/core/test_utf.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
206 188 226 128 μ†ℱ ╋ℯ╳╋ 😇
2-
μ†ℱ ╋ℯ╳╋ 😇,206,188,226,128
2+
μ†ℱ ╋ℯ╳╋ 😇 206 188 226 128

test/test_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,8 +5618,7 @@ def test_futimens(self):
56185618
@no_minimal_runtime('MINIMAL_RUNTIME does not have getValue() and setValue() (TODO add it to a JS library function to get it in)')
56195619
@requires_node # only node handles utf well
56205620
def test_utf(self):
5621-
self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_malloc', '_free'])
5622-
self.set_setting('EXPORTED_RUNTIME_METHODS', ['getValue', 'setValue', 'UTF8ToString', 'stringToUTF8'])
5621+
self.set_setting('EXPORTED_RUNTIME_METHODS', ['getValue', 'setValue', 'UTF8ToString', 'stringToUTF8OnStack'])
56235622
self.do_core_test('test_utf.c')
56245623

56255624
def test_utf32(self):

0 commit comments

Comments
 (0)