Skip to content

Commit 79a7ca9

Browse files
authored
Make more use of allocateUTF8 helper. NFC (#19064)
1 parent 30c720a commit 79a7ca9

File tree

9 files changed

+31
-53
lines changed

9 files changed

+31
-53
lines changed

site/source/docs/api_reference/emscripten.h.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ Defines
111111
var jsString = 'Hello with some exotic Unicode characters: Tässä on yksi lumiukko: ☃, ole hyvä.';
112112
// 'jsString.length' would return the length of the string as UTF-16
113113
// units, but Emscripten C strings operate as UTF-8.
114-
var lengthBytes = lengthBytesUTF8(jsString)+1;
115-
var stringOnWasmHeap = _malloc(lengthBytes);
116-
stringToUTF8(jsString, stringOnWasmHeap, lengthBytes);
117-
return stringOnWasmHeap;
114+
return allocateUTF8(jsString);
118115
});
119116
120117
int main() {
@@ -194,9 +191,7 @@ Defines
194191
var lengthBytes = lengthBytesUTF8(jsString)+1;
195192
// 'jsString.length' would return the length of the string as UTF-16
196193
// units, but Emscripten C strings operate as UTF-8.
197-
var stringOnWasmHeap = _malloc(lengthBytes);
198-
stringToUTF8(jsString, stringOnWasmHeap, lengthBytes);
199-
return stringOnWasmHeap;
194+
return allocateUTF8(jsString);
200195
});
201196
printf("UTF8 string says: %s\n", str);
202197
free(str); // Each call to _malloc() must be paired with free(), or heap memory will leak!

src/library.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ mergeInto(LibraryManager.library, {
5252
// JavaScript <-> C string interop
5353
// ==========================================================================
5454

55-
$stringToNewUTF8__deps: ['malloc'],
56-
$stringToNewUTF8: function(jsString) {
57-
var length = lengthBytesUTF8(jsString)+1;
58-
var cString = _malloc(length);
59-
stringToUTF8(jsString, cString, length);
60-
return cString;
61-
},
55+
$stringToNewUTF8: '$allocateUTF8',
6256

6357
#if !MINIMAL_RUNTIME
6458
$exitJS__docs: '/** @param {boolean|number=} implicit */',
@@ -1833,12 +1827,11 @@ mergeInto(LibraryManager.library, {
18331827
return getHostByName(UTF8ToString(name));
18341828
},
18351829

1836-
$getHostByName__deps: ['malloc', '$DNS', '$inetPton4'],
1830+
$getHostByName__deps: ['malloc', '$allocateUTF8', '$DNS', '$inetPton4'],
18371831
$getHostByName: function(name) {
18381832
// generate hostent
18391833
var ret = _malloc({{{ C_STRUCTS.hostent.__size__ }}}); // XXX possibly leaked, as are others here
1840-
var nameBuf = {{{ makeMalloc('getHostByName', 'name.length+1') }}};
1841-
stringToUTF8(name, nameBuf, name.length+1);
1834+
var nameBuf = allocateUTF8(name);
18421835
{{{ makeSetValue('ret', C_STRUCTS.hostent.h_name, 'nameBuf', POINTER_TYPE) }}};
18431836
var aliasesBuf = _malloc(4);
18441837
{{{ makeSetValue('aliasesBuf', '0', '0', POINTER_TYPE) }}};
@@ -2628,6 +2621,9 @@ mergeInto(LibraryManager.library, {
26282621
// We never free the return values of this function so we need to allocate
26292622
// using builtin_malloc to avoid LSan reporting these as leaks.
26302623
emscripten_get_compiler_setting__noleakcheck: true,
2624+
#if RETAIN_COMPILER_SETTINGS
2625+
emscripten_get_compiler_setting__deps: ['$allocateUTF8'],
2626+
#endif
26312627
emscripten_get_compiler_setting: function(name) {
26322628
#if RETAIN_COMPILER_SETTINGS
26332629
name = UTF8ToString(name);
@@ -2639,9 +2635,7 @@ mergeInto(LibraryManager.library, {
26392635
var cache = _emscripten_get_compiler_setting.cache;
26402636
var fullret = cache[name];
26412637
if (fullret) return fullret;
2642-
cache[name] = _malloc(ret.length + 1);
2643-
stringToUTF8(ret + '', cache[name], ret.length + 1);
2644-
return cache[name];
2638+
return cache[name] = allocateUTF8(ret);
26452639
#else
26462640
throw 'You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work';
26472641
#endif

src/library_browser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,16 +803,14 @@ var LibraryBrowser = {
803803
},
804804

805805
emscripten_run_preload_plugins_data__proxy: 'sync',
806-
emscripten_run_preload_plugins_data__deps: ['malloc'],
806+
emscripten_run_preload_plugins_data__deps: ['$allocateUTF8'],
807807
emscripten_run_preload_plugins_data: function(data, size, suffix, arg, onload, onerror) {
808808
{{{ runtimeKeepalivePush() }}}
809809

810810
var _suffix = UTF8ToString(suffix);
811811
if (!Browser.asyncPrepareDataCounter) Browser.asyncPrepareDataCounter = 0;
812812
var name = 'prepare_data_' + (Browser.asyncPrepareDataCounter++) + '.' + _suffix;
813-
var lengthAsUTF8 = lengthBytesUTF8(name);
814-
var cname = _malloc(lengthAsUTF8+1);
815-
stringToUTF8(name, cname, lengthAsUTF8+1);
813+
var cname = allocateUTF8(name);
816814
FS.createPreloadedFile(
817815
'/',
818816
name,

src/library_ccall.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mergeInto(LibraryManager.library, {
1515
},
1616

1717
// C calling interface.
18-
$ccall__deps: ['$getCFunc', '$writeArrayToMemory'],
18+
$ccall__deps: ['$getCFunc', '$writeArrayToMemory', '$allocateUTF8OnStack'],
1919
$ccall__docs: `
2020
/**
2121
* @param {string|null=} returnType
@@ -33,9 +33,7 @@ mergeInto(LibraryManager.library, {
3333
var ret = 0;
3434
if (str !== null && str !== undefined && str !== 0) { // null string
3535
// at most 4 bytes per UTF-8 code point, +1 for the trailing '\0'
36-
var len = (str.length << 2) + 1;
37-
ret = stackAlloc(len);
38-
stringToUTF8(str, ret, len);
36+
ret = allocateUTF8OnStack(str);
3937
}
4038
return {{{ to64('ret') }}};
4139
},

src/library_html5.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,7 +2459,7 @@ var LibraryHTML5 = {
24592459
},
24602460
#endif
24612461

2462-
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$withStackSave'],
2462+
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$withStackSave', '$allocateUTF8OnStack'],
24632463
$setCanvasElementSize: function(target, width, height) {
24642464
#if GL_DEBUG
24652465
dbg('setCanvasElementSize(target='+target+',width='+width+',height='+height);
@@ -2471,8 +2471,7 @@ var LibraryHTML5 = {
24712471
// This function is being called from high-level JavaScript code instead of asm.js/Wasm,
24722472
// and it needs to synchronously proxy over to another thread, so marshal the string onto the heap to do the call.
24732473
withStackSave(function() {
2474-
var targetInt = stackAlloc(target.id.length+1);
2475-
stringToUTF8(target.id, targetInt, target.id.length+1);
2474+
var targetInt = allocateUTF8OnStack(target.id);
24762475
_emscripten_set_canvas_element_size(targetInt, width, height);
24772476
});
24782477
}
@@ -2534,14 +2533,13 @@ var LibraryHTML5 = {
25342533
#endif
25352534

25362535
// JavaScript-friendly API, returns pair [width, height]
2537-
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$withStackSave'],
2536+
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$withStackSave', '$allocateUTF8OnStack'],
25382537
$getCanvasElementSize: function(target) {
25392538
return withStackSave(function() {
25402539
var w = stackAlloc(8);
25412540
var h = w + 4;
25422541

2543-
var targetInt = stackAlloc(target.id.length+1);
2544-
stringToUTF8(target.id, targetInt, target.id.length+1);
2542+
var targetInt = allocateUTF8OnStack(target.id);
25452543
var ret = _emscripten_get_canvas_element_size(targetInt, w, h);
25462544
var size = [{{{ makeGetValue('w', 0, 'i32')}}}, {{{ makeGetValue('h', 0, 'i32')}}}];
25472545
return size;

src/library_sdl.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ var LibrarySDL = {
21852185
return flags; // We support JPG, PNG, TIF because browsers do
21862186
},
21872187

2188-
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', 'malloc'],
2188+
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', 'malloc', '$allocateUTF8'],
21892189
IMG_Load_RW__proxy: 'sync',
21902190
IMG_Load_RW: function(rwopsID, freeSrc) {
21912191
try {
@@ -2245,9 +2245,7 @@ var LibrarySDL = {
22452245
if (!raw) {
22462246
if (raw === null) err('Trying to reuse preloaded image, but freePreloadedMediaOnUse is set!');
22472247
#if STB_IMAGE
2248-
var lengthBytes = lengthBytesUTF8(filename)+1;
2249-
var name = _malloc(lengthBytes);
2250-
stringToUTF8(filename, name, lengthBytes);
2248+
var name = allocateUTF8(filename);
22512249
addCleanup(function() {
22522250
_free(name);
22532251
});

src/library_stack_trace.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
var LibraryStackTrace = {
88
#if DEMANGLE_SUPPORT
9-
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free'],
9+
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free', '$allocateUTF8OnStack'],
1010
#endif
1111
$demangle: function(func) {
1212
#if DEMANGLE_SUPPORT
@@ -19,9 +19,7 @@ var LibraryStackTrace = {
1919
var s = func;
2020
if (s.startsWith('__Z'))
2121
s = s.substr(1);
22-
var len = lengthBytesUTF8(s)+1;
23-
var buf = stackAlloc(len);
24-
stringToUTF8(s, buf, len);
22+
var buf = allocateUTF8OnStack(s);
2523
var status = stackAlloc(4);
2624
var ret = ___cxa_demangle(buf, 0, 0, status);
2725
if ({{{ makeGetValue('status', '0', 'i32') }}} === 0 && ret) {

src/library_websocket.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ var LibraryWebSocket = {
215215
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
216216
},
217217

218-
emscripten_websocket_set_onmessage_callback_on_thread__deps: ['$WS'],
218+
emscripten_websocket_set_onmessage_callback_on_thread__deps: ['$WS', '$allocateUTF8'],
219219
emscripten_websocket_set_onmessage_callback_on_thread__proxy: 'sync',
220220
emscripten_websocket_set_onmessage_callback_on_thread: function(socketId, userData, callbackFunc, thread) {
221221
if (!WS.socketEvent) WS.socketEvent = _malloc(1024); // TODO: sizeof(EmscriptenWebSocketCloseEvent), which is the largest event struct
@@ -237,10 +237,9 @@ var LibraryWebSocket = {
237237
#endif
238238
HEAPU32[WS.socketEvent>>2] = socketId;
239239
if (typeof e.data == 'string') {
240-
var len = lengthBytesUTF8(e.data)+1;
241-
var buf = _malloc(len);
242-
stringToUTF8(e.data, buf, len);
240+
var buf = allocateUTF8(e.data);
243241
#if WEBSOCKET_DEBUG
242+
var len = lengthBytesUTF8(e.data)+1;
244243
var s = (e.data.length < 256) ? e.data : (e.data.substr(0, 256) + ' (' + (e.data.length-256) + ' more characters)');
245244
dbg('WebSocket onmessage, received data: "' + e.data + '", ' + e.data.length + ' chars, ' + len + ' bytes encoded as UTF-8: "' + s + '"');
246245
#endif

src/library_wget.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ var LibraryWget = {
173173

174174
function onerrorjs() {
175175
if (onerror) {
176-
var statusText = 0;
177-
if (http.statusText) {
178-
var len = lengthBytesUTF8(http.statusText) + 1;
179-
statusText = stackAlloc(len);
180-
stringToUTF8(http.statusText, statusText, len);
181-
}
182-
{{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, statusText);
176+
withStackSave(() => {
177+
var statusText = 0;
178+
if (http.statusText) {
179+
statusText = allocateUTF8OnStack(http.statusText);
180+
}
181+
{{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, statusText);
182+
});
183183
}
184184
}
185185

0 commit comments

Comments
 (0)