Skip to content

Commit 0804c5b

Browse files
committed
Make more use of allocateUTF8 helper. NFC
1 parent b79bf7b commit 0804c5b

File tree

9 files changed

+24
-48
lines changed

9 files changed

+24
-48
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 */',
@@ -1854,12 +1848,11 @@ mergeInto(LibraryManager.library, {
18541848
return getHostByName(UTF8ToString(name));
18551849
},
18561850

1857-
$getHostByName__deps: ['malloc', '$DNS', '$inetPton4'],
1851+
$getHostByName__deps: ['malloc', '$allocateUTF8', '$DNS', '$inetPton4'],
18581852
$getHostByName: function(name) {
18591853
// generate hostent
18601854
var ret = _malloc({{{ C_STRUCTS.hostent.__size__ }}}); // XXX possibly leaked, as are others here
1861-
var nameBuf = {{{ makeMalloc('getHostByName', 'name.length+1') }}};
1862-
stringToUTF8(name, nameBuf, name.length+1);
1855+
var nameBuf = allocateUTF8(name);
18631856
{{{ makeSetValue('ret', C_STRUCTS.hostent.h_name, 'nameBuf', POINTER_TYPE) }}};
18641857
var aliasesBuf = _malloc(4);
18651858
{{{ makeSetValue('aliasesBuf', '0', '0', POINTER_TYPE) }}};
@@ -2662,6 +2655,9 @@ mergeInto(LibraryManager.library, {
26622655
// using builtin_malloc to avoid LSan reporting these as leaks.
26632656
emscripten_get_compiler_setting__noleakcheck: true,
26642657
emscripten_get_compiler_setting__sig: 'pp',
2658+
#if RETAIN_COMPILER_SETTINGS
2659+
emscripten_get_compiler_setting__deps: ['$allocateUTF8'],
2660+
#endif
26652661
emscripten_get_compiler_setting: function(name) {
26662662
#if RETAIN_COMPILER_SETTINGS
26672663
name = UTF8ToString(name);
@@ -2673,9 +2669,7 @@ mergeInto(LibraryManager.library, {
26732669
var cache = _emscripten_get_compiler_setting.cache;
26742670
var fullret = cache[name];
26752671
if (fullret) return fullret;
2676-
cache[name] = _malloc(ret.length + 1);
2677-
stringToUTF8(ret + '', cache[name], ret.length + 1);
2678-
return cache[name];
2672+
return cache[name] = allocateUTF8(ret);
26792673
#else
26802674
throw 'You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work';
26812675
#endif

src/library_browser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,15 @@ var LibraryBrowser = {
804804
},
805805

806806
emscripten_run_preload_plugins_data__proxy: 'sync',
807-
emscripten_run_preload_plugins_data__deps: ['malloc'],
807+
emscripten_run_preload_plugins_data__deps: ['$allocateUTF8'],
808808
emscripten_run_preload_plugins_data__sig: 'vpipppp',
809809
emscripten_run_preload_plugins_data: function(data, size, suffix, arg, onload, onerror) {
810810
{{{ runtimeKeepalivePush() }}}
811811

812812
var _suffix = UTF8ToString(suffix);
813813
if (!Browser.asyncPrepareDataCounter) Browser.asyncPrepareDataCounter = 0;
814814
var name = 'prepare_data_' + (Browser.asyncPrepareDataCounter++) + '.' + _suffix;
815-
var lengthAsUTF8 = lengthBytesUTF8(name);
816-
var cname = _malloc(lengthAsUTF8+1);
817-
stringToUTF8(name, cname, lengthAsUTF8+1);
815+
var cname = allocateUTF8(name);
818816
FS.createPreloadedFile(
819817
'/',
820818
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
@@ -2518,7 +2518,7 @@ var LibraryHTML5 = {
25182518
},
25192519
#endif
25202520

2521-
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$withStackSave'],
2521+
$setCanvasElementSize__deps: ['emscripten_set_canvas_element_size', '$withStackSave', '$allocateUTF8OnStack'],
25222522
$setCanvasElementSize: function(target, width, height) {
25232523
#if GL_DEBUG
25242524
dbg('setCanvasElementSize(target='+target+',width='+width+',height='+height);
@@ -2530,8 +2530,7 @@ var LibraryHTML5 = {
25302530
// This function is being called from high-level JavaScript code instead of asm.js/Wasm,
25312531
// and it needs to synchronously proxy over to another thread, so marshal the string onto the heap to do the call.
25322532
withStackSave(function() {
2533-
var targetInt = stackAlloc(target.id.length+1);
2534-
stringToUTF8(target.id, targetInt, target.id.length+1);
2533+
var targetInt = allocateUTF8OnStack(target.id);
25352534
_emscripten_set_canvas_element_size(targetInt, width, height);
25362535
});
25372536
}
@@ -2595,14 +2594,13 @@ var LibraryHTML5 = {
25952594
#endif
25962595

25972596
// JavaScript-friendly API, returns pair [width, height]
2598-
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$withStackSave'],
2597+
$getCanvasElementSize__deps: ['emscripten_get_canvas_element_size', '$withStackSave', '$allocateUTF8OnStack'],
25992598
$getCanvasElementSize: function(target) {
26002599
return withStackSave(function() {
26012600
var w = stackAlloc(8);
26022601
var h = w + 4;
26032602

2604-
var targetInt = stackAlloc(target.id.length+1);
2605-
stringToUTF8(target.id, targetInt, target.id.length+1);
2603+
var targetInt = allocateUTF8OnStack(target.id);
26062604
var ret = _emscripten_get_canvas_element_size(targetInt, w, h);
26072605
var size = [{{{ makeGetValue('w', 0, 'i32')}}}, {{{ makeGetValue('h', 0, 'i32')}}}];
26082606
return size;

src/library_sdl.js

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

2229-
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', 'malloc'],
2229+
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', 'malloc', '$allocateUTF8'],
22302230
IMG_Load_RW__proxy: 'sync',
22312231
IMG_Load_RW__sig: 'ppi',
22322232
IMG_Load_RW: function(rwopsID, freeSrc) {
@@ -2287,9 +2287,7 @@ var LibrarySDL = {
22872287
if (!raw) {
22882288
if (raw === null) err('Trying to reuse preloaded image, but freePreloadedMediaOnUse is set!');
22892289
#if STB_IMAGE
2290-
var lengthBytes = lengthBytesUTF8(filename)+1;
2291-
var name = _malloc(lengthBytes);
2292-
stringToUTF8(filename, name, lengthBytes);
2290+
var name = allocateUTF8(filename);
22932291
addCleanup(function() {
22942292
_free(name);
22952293
});

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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,9 @@ var LibraryWebSocket = {
249249
#endif
250250
HEAPU32[WS.socketEvent>>2] = socketId;
251251
if (typeof e.data == 'string') {
252-
var len = lengthBytesUTF8(e.data)+1;
253-
var buf = _malloc(len);
254-
stringToUTF8(e.data, buf, len);
252+
var buf = allocateUTF8(e.data);
255253
#if WEBSOCKET_DEBUG
254+
var len = lengthBytesUTF8(e.data)+1;
256255
var s = (e.data.length < 256) ? e.data : (e.data.substr(0, 256) + ' (' + (e.data.length-256) + ' more characters)');
257256
dbg('WebSocket onmessage, received data: "' + e.data + '", ' + e.data.length + ' chars, ' + len + ' bytes encoded as UTF-8: "' + s + '"');
258257
#endif

src/library_wget.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ var LibraryWget = {
179179
if (onerror) {
180180
var statusText = 0;
181181
if (http.statusText) {
182-
var len = lengthBytesUTF8(http.statusText) + 1;
183-
statusText = stackAlloc(len);
184-
stringToUTF8(http.statusText, statusText, len);
182+
statusText = allocateUTF8OnStack(http.statusText);
185183
}
186184
{{{ makeDynCall('viiii', 'onerror') }}}(handle, arg, http.status, statusText);
187185
}

0 commit comments

Comments
 (0)