Skip to content

Commit 4e15675

Browse files
authored
Deprecate allocateUTF8 in favor of stringToNewUTF8 (#19089)
Followup to #19064
1 parent bd95d10 commit 4e15675

File tree

19 files changed

+75
-73
lines changed

19 files changed

+75
-73
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.35 (in development)
2222
-----------------------
23+
- `allocateUTF8` library function moved to `library_legacy.js`. Prefer the
24+
more accurately named `stringToNewUTF8`.
2325
- `SDL_image` port was updated to version 2.6.0.
2426
- `-z` arguments are now passed directly to wasm-ld without the need for the
2527
`-Wl,` prefix. This matches the behaviour of both clang and gcc. (#18956)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +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-
return allocateUTF8(jsString);
114+
return stringToNewUTF8(jsString);
115115
});
116116
117117
int main() {
@@ -191,7 +191,7 @@ Defines
191191
var lengthBytes = lengthBytesUTF8(jsString)+1;
192192
// 'jsString.length' would return the length of the string as UTF-16
193193
// units, but Emscripten C strings operate as UTF-8.
194-
return allocateUTF8(jsString);
194+
return stringToNewUTF8(jsString);
195195
});
196196
printf("UTF8 string says: %s\n", str);
197197
free(str); // Each call to _malloc() must be paired with free(), or heap memory will leak!

site/source/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ The parameters you pass to and receive from functions need to be primitive value
235235

236236
- Integer and floating point numbers can be passed as-is.
237237
- Pointers can be passed as-is also, as they are simply integers in the generated code.
238-
- JavaScript string ``someString`` can be converted to a ``char *`` using ``ptr = allocateUTF8(someString)``.
238+
- JavaScript string ``someString`` can be converted to a ``char *`` using ``ptr = stringToNewUTF8(someString)``.
239239

240240
.. note:: The conversion to a pointer allocates memory, which needs to be
241241
freed up via a call to ``free(ptr)`` afterwards (``_free`` in JavaScript side) -

src/library.js

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

55-
$stringToNewUTF8: '$allocateUTF8',
56-
5755
#if !MINIMAL_RUNTIME
5856
$exitJS__docs: '/** @param {boolean|number=} implicit */',
5957
$exitJS__deps: ['proc_exit'],
@@ -569,7 +567,7 @@ mergeInto(LibraryManager.library, {
569567

570568
// TODO: Initialize these to defaults on startup from system settings.
571569
// Note: glibc has one fewer underscore for all of these. Also used in other related functions (timegm)
572-
_tzset_js__deps: ['$allocateUTF8'],
570+
_tzset_js__deps: ['$stringToNewUTF8'],
573571
_tzset_js__internal: true,
574572
_tzset_js: function(timezone, daylight, tzname) {
575573
// TODO: Use (malleable) environment variables instead of system settings.
@@ -599,8 +597,8 @@ mergeInto(LibraryManager.library, {
599597
};
600598
var winterName = extractZone(winter);
601599
var summerName = extractZone(summer);
602-
var winterNamePtr = allocateUTF8(winterName);
603-
var summerNamePtr = allocateUTF8(summerName);
600+
var winterNamePtr = stringToNewUTF8(winterName);
601+
var summerNamePtr = stringToNewUTF8(summerName);
604602
if (summerOffset < winterOffset) {
605603
// Northern hemisphere
606604
{{{ makeSetValue('tzname', '0', 'winterNamePtr', POINTER_TYPE) }}};
@@ -1827,11 +1825,11 @@ mergeInto(LibraryManager.library, {
18271825
return getHostByName(UTF8ToString(name));
18281826
},
18291827

1830-
$getHostByName__deps: ['malloc', '$allocateUTF8', '$DNS', '$inetPton4'],
1828+
$getHostByName__deps: ['malloc', '$stringToNewUTF8', '$DNS', '$inetPton4'],
18311829
$getHostByName: function(name) {
18321830
// generate hostent
18331831
var ret = _malloc({{{ C_STRUCTS.hostent.__size__ }}}); // XXX possibly leaked, as are others here
1834-
var nameBuf = allocateUTF8(name);
1832+
var nameBuf = stringToNewUTF8(name);
18351833
{{{ makeSetValue('ret', C_STRUCTS.hostent.h_name, 'nameBuf', POINTER_TYPE) }}};
18361834
var aliasesBuf = _malloc(4);
18371835
{{{ makeSetValue('aliasesBuf', '0', '0', POINTER_TYPE) }}};
@@ -2622,7 +2620,7 @@ mergeInto(LibraryManager.library, {
26222620
// using builtin_malloc to avoid LSan reporting these as leaks.
26232621
emscripten_get_compiler_setting__noleakcheck: true,
26242622
#if RETAIN_COMPILER_SETTINGS
2625-
emscripten_get_compiler_setting__deps: ['$allocateUTF8'],
2623+
emscripten_get_compiler_setting__deps: ['$stringToNewUTF8'],
26262624
#endif
26272625
emscripten_get_compiler_setting: function(name) {
26282626
#if RETAIN_COMPILER_SETTINGS
@@ -2635,7 +2633,7 @@ mergeInto(LibraryManager.library, {
26352633
var cache = _emscripten_get_compiler_setting.cache;
26362634
var fullret = cache[name];
26372635
if (fullret) return fullret;
2638-
return cache[name] = allocateUTF8(ret);
2636+
return cache[name] = stringToNewUTF8(ret);
26392637
#else
26402638
throw 'You must build with -sRETAIN_COMPILER_SETTINGS for getCompilerSetting or emscripten_get_compiler_setting to work';
26412639
#endif
@@ -2803,7 +2801,7 @@ mergeInto(LibraryManager.library, {
28032801

28042802
// Look up the function name from our stack frame cache with our PC representation.
28052803
#if USE_OFFSET_CONVERTER
2806-
emscripten_pc_get_function__deps: ['$UNWIND_CACHE', 'free', '$allocateUTF8'],
2804+
emscripten_pc_get_function__deps: ['$UNWIND_CACHE', 'free', '$stringToNewUTF8'],
28072805
// Don't treat allocation of _emscripten_pc_get_function.ret as a leak
28082806
emscripten_pc_get_function__noleakcheck: true,
28092807
#endif
@@ -2829,7 +2827,7 @@ mergeInto(LibraryManager.library, {
28292827
name = wasmOffsetConverter.getName(pc);
28302828
}
28312829
if (_emscripten_pc_get_function.ret) _free(_emscripten_pc_get_function.ret);
2832-
_emscripten_pc_get_function.ret = allocateUTF8(name);
2830+
_emscripten_pc_get_function.ret = stringToNewUTF8(name);
28332831
return _emscripten_pc_get_function.ret;
28342832
#endif
28352833
},
@@ -2863,15 +2861,15 @@ mergeInto(LibraryManager.library, {
28632861
},
28642862

28652863
// Look up the file name from our stack frame cache with our PC representation.
2866-
emscripten_pc_get_file__deps: ['$convertPCtoSourceLocation', 'free', '$allocateUTF8'],
2864+
emscripten_pc_get_file__deps: ['$convertPCtoSourceLocation', 'free', '$stringToNewUTF8'],
28672865
// Don't treat allocation of _emscripten_pc_get_file.ret as a leak
28682866
emscripten_pc_get_file__noleakcheck: true,
28692867
emscripten_pc_get_file: function (pc) {
28702868
var result = convertPCtoSourceLocation(pc);
28712869
if (!result) return 0;
28722870

28732871
if (_emscripten_pc_get_file.ret) _free(_emscripten_pc_get_file.ret);
2874-
_emscripten_pc_get_file.ret = allocateUTF8(result.file);
2872+
_emscripten_pc_get_file.ret = stringToNewUTF8(result.file);
28752873
return _emscripten_pc_get_file.ret;
28762874
},
28772875

@@ -3714,7 +3712,7 @@ DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push(
37143712
'$UTF32ToString',
37153713
'$stringToUTF32',
37163714
'$lengthBytesUTF32',
3717-
'$allocateUTF8',
3715+
'$stringToNewUTF8',
37183716
'$allocateUTF8OnStack',
37193717
'$writeStringToMemory',
37203718
'$writeArrayToMemory',

src/library_browser.js

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

805805
emscripten_run_preload_plugins_data__proxy: 'sync',
806-
emscripten_run_preload_plugins_data__deps: ['$allocateUTF8'],
806+
emscripten_run_preload_plugins_data__deps: ['$stringToNewUTF8'],
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 cname = allocateUTF8(name);
813+
var cname = stringToNewUTF8(name);
814814
FS.createPreloadedFile(
815815
'/',
816816
name,

src/library_egl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ var LibraryEGL = {
520520
},
521521

522522
// EGLAPI const char * EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name);
523-
eglQueryString__deps: ['$allocateUTF8'],
523+
eglQueryString__deps: ['$stringToNewUTF8'],
524524
eglQueryString__proxy: 'sync',
525525
eglQueryString__sig: 'iii',
526526
eglQueryString: function(display, name) {
@@ -533,10 +533,10 @@ var LibraryEGL = {
533533
if (EGL.stringCache[name]) return EGL.stringCache[name];
534534
var ret;
535535
switch (name) {
536-
case 0x3053 /* EGL_VENDOR */: ret = allocateUTF8("Emscripten"); break;
537-
case 0x3054 /* EGL_VERSION */: ret = allocateUTF8("1.4 Emscripten EGL"); break;
538-
case 0x3055 /* EGL_EXTENSIONS */: ret = allocateUTF8(""); break; // Currently not supporting any EGL extensions.
539-
case 0x308D /* EGL_CLIENT_APIS */: ret = allocateUTF8("OpenGL_ES"); break;
536+
case 0x3053 /* EGL_VENDOR */: ret = stringToNewUTF8("Emscripten"); break;
537+
case 0x3054 /* EGL_VERSION */: ret = stringToNewUTF8("1.4 Emscripten EGL"); break;
538+
case 0x3055 /* EGL_EXTENSIONS */: ret = stringToNewUTF8(""); break; // Currently not supporting any EGL extensions.
539+
case 0x308D /* EGL_CLIENT_APIS */: ret = stringToNewUTF8("OpenGL_ES"); break;
540540
default:
541541
EGL.setErrorCode(0x300C /* EGL_BAD_PARAMETER */);
542542
return 0;

src/library_glew.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
var LibraryGLEW = {
23-
$GLEW__deps: ['glGetString', '$allocateUTF8'],
23+
$GLEW__deps: ['glGetString', '$stringToNewUTF8'],
2424
$GLEW: {
2525
isLinaroFork: 1,
2626
extensions: null,
@@ -73,7 +73,7 @@ var LibraryGLEW = {
7373
string = "Unknown error";
7474
error = 8; // prevent array from growing more than this
7575
}
76-
GLEW.error[error] = allocateUTF8(string);
76+
GLEW.error[error] = stringToNewUTF8(string);
7777
}
7878
return GLEW.error[error];
7979
},
@@ -93,7 +93,7 @@ var LibraryGLEW = {
9393
var string = GLEW.versionStringConstantFromCode(name);
9494
if (!string)
9595
return 0;
96-
GLEW.version[name] = allocateUTF8(string);
96+
GLEW.version[name] = stringToNewUTF8(string);
9797
}
9898
return GLEW.version[name];
9999
},

src/library_glfw.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var LibraryGLFW = {
8282
},
8383

8484
$GLFW__deps: ['emscripten_get_now', '$GL', '$Browser', '$GLFW_Window',
85-
'$allocateUTF8',
85+
'$stringToNewUTF8',
8686
#if FILESYSTEM
8787
'$FS',
8888
#endif
@@ -662,7 +662,7 @@ var LibraryGLFW = {
662662
if (!GLFW.joys[joy]) {
663663
out('glfw joystick connected:',joy);
664664
GLFW.joys[joy] = {
665-
id: allocateUTF8(gamepad.id),
665+
id: stringToNewUTF8(gamepad.id),
666666
buttonsCount: gamepad.buttons.length,
667667
axesCount: gamepad.axes.length,
668668
buttons: _malloc(gamepad.buttons.length),
@@ -789,7 +789,7 @@ var LibraryGLFW = {
789789
};
790790
reader.readAsArrayBuffer(file);
791791

792-
var filename = allocateUTF8(path);
792+
var filename = stringToNewUTF8(path);
793793
filenamesArray.push(filename);
794794
{{{ makeSetValue('filenames + i*4', '0', 'filename', POINTER_TYPE) }}};
795795
}
@@ -1251,7 +1251,7 @@ var LibraryGLFW = {
12511251
glfwGetVersionString__sig: 'i',
12521252
glfwGetVersionString: function() {
12531253
if (!GLFW.versionString) {
1254-
GLFW.versionString = allocateUTF8("3.2.1 JS WebGL Emscripten");
1254+
GLFW.versionString = stringToNewUTF8("3.2.1 JS WebGL Emscripten");
12551255
}
12561256
return GLFW.versionString;
12571257
},
@@ -1318,7 +1318,7 @@ var LibraryGLFW = {
13181318
glfwGetMonitorName__sig: 'ii',
13191319
glfwGetMonitorName: function(mon) {
13201320
if (!GLFW.monitorString) {
1321-
GLFW.monitorString = allocateUTF8("HTML5 WebGL Canvas");
1321+
GLFW.monitorString = stringToNewUTF8("HTML5 WebGL Canvas");
13221322
}
13231323
return GLFW.monitorString;
13241324
},

src/library_legacy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ mergeInto(LibraryManager.library, {
3737
HEAPU8.set(slab, ret);
3838
return ret;
3939
},
40+
41+
$allocateUTF8: '$stringToNewUTF8',
4042
});

src/library_openal.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,7 @@ var LibraryOpenAL = {
23762376
},
23772377

23782378
alcGetString__proxy: 'sync',
2379-
alcGetString__deps: ['$allocateUTF8'],
2379+
alcGetString__deps: ['$stringToNewUTF8'],
23802380
alcGetString: function(deviceId, param) {
23812381
if (AL.alcStringCache[param]) {
23822382
return AL.alcStringCache[param];
@@ -2450,7 +2450,7 @@ var LibraryOpenAL = {
24502450
return 0;
24512451
}
24522452

2453-
ret = allocateUTF8(ret);
2453+
ret = stringToNewUTF8(ret);
24542454
AL.alcStringCache[param] = ret;
24552455
return ret;
24562456
},
@@ -2631,7 +2631,7 @@ var LibraryOpenAL = {
26312631

26322632
emscripten_alcGetStringiSOFT__proxy: 'sync',
26332633
emscripten_alcGetStringiSOFT__sig: 'iiii',
2634-
emscripten_alcGetStringiSOFT__deps: ['alcGetString', '$allocateUTF8'],
2634+
emscripten_alcGetStringiSOFT__deps: ['alcGetString', '$stringToNewUTF8'],
26352635
emscripten_alcGetStringiSOFT: function(deviceId, param, index) {
26362636
if (!(deviceId in AL.deviceRefCounts)) {
26372637
#if OPENAL_DEBUG
@@ -2669,7 +2669,7 @@ var LibraryOpenAL = {
26692669
return _alcGetString(deviceId, param);
26702670
}
26712671

2672-
ret = allocateUTF8(ret);
2672+
ret = stringToNewUTF8(ret);
26732673
AL.alcStringCache[param] = ret;
26742674
return ret;
26752675
},
@@ -3018,7 +3018,7 @@ var LibraryOpenAL = {
30183018
},
30193019

30203020
alGetString__proxy: 'sync',
3021-
alGetString__deps: ['$allocateUTF8'],
3021+
alGetString__deps: ['$stringToNewUTF8'],
30223022
alGetString: function(param) {
30233023
if (AL.stringCache[param]) {
30243024
return AL.stringCache[param];
@@ -3072,7 +3072,7 @@ var LibraryOpenAL = {
30723072
return 0;
30733073
}
30743074

3075-
ret = allocateUTF8(ret);
3075+
ret = stringToNewUTF8(ret);
30763076
AL.stringCache[param] = ret;
30773077
return ret;
30783078
},

src/library_sdl.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,10 +1762,10 @@ var LibrarySDL = {
17621762
},
17631763

17641764
SDL_GetKeyName__proxy: 'sync',
1765-
SDL_GetKeyName__deps: ['$allocateUTF8'],
1765+
SDL_GetKeyName__deps: ['$stringToNewUTF8'],
17661766
SDL_GetKeyName: function(key) {
17671767
if (!SDL.keyName) {
1768-
SDL.keyName = allocateUTF8('unknown key');
1768+
SDL.keyName = stringToNewUTF8('unknown key');
17691769
}
17701770
return SDL.keyName;
17711771
},
@@ -1817,10 +1817,10 @@ var LibrarySDL = {
18171817
},
18181818

18191819
SDL_GetError__proxy: 'sync',
1820-
SDL_GetError__deps: ['$allocateUTF8'],
1820+
SDL_GetError__deps: ['$stringToNewUTF8'],
18211821
SDL_GetError: function() {
18221822
if (!SDL.errorMessage) {
1823-
SDL.errorMessage = allocateUTF8("unknown SDL-emscripten error");
1823+
SDL.errorMessage = stringToNewUTF8("unknown SDL-emscripten error");
18241824
}
18251825
return SDL.errorMessage;
18261826
},
@@ -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', '$allocateUTF8'],
2188+
IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW', '$PATH_FS', 'malloc', '$stringToNewUTF8'],
21892189
IMG_Load_RW__proxy: 'sync',
21902190
IMG_Load_RW: function(rwopsID, freeSrc) {
21912191
try {
@@ -2245,7 +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 name = allocateUTF8(filename);
2248+
var name = stringToNewUTF8(filename);
22492249
addCleanup(function() {
22502250
_free(name);
22512251
});
@@ -3447,15 +3447,15 @@ var LibrarySDL = {
34473447
},
34483448

34493449
SDL_JoystickName__proxy: 'sync',
3450-
SDL_JoystickName__deps: ['$allocateUTF8'],
3450+
SDL_JoystickName__deps: ['$stringToNewUTF8'],
34513451
SDL_JoystickName: function(deviceIndex) {
34523452
var gamepad = SDL.getGamepad(deviceIndex);
34533453
if (gamepad) {
34543454
var name = gamepad.id;
34553455
if (SDL.joystickNamePool.hasOwnProperty(name)) {
34563456
return SDL.joystickNamePool[name];
34573457
}
3458-
return SDL.joystickNamePool[name] = allocateUTF8(name);
3458+
return SDL.joystickNamePool[name] = stringToNewUTF8(name);
34593459
}
34603460
return 0;
34613461
},
@@ -3575,9 +3575,9 @@ var LibrarySDL = {
35753575
},
35763576

35773577
SDL_GetNumAudioDrivers: function() { return 1 },
3578-
SDL_GetCurrentAudioDriver__deps: ['$allocateUTF8'],
3578+
SDL_GetCurrentAudioDriver__deps: ['$stringToNewUTF8'],
35793579
SDL_GetCurrentAudioDriver: function() {
3580-
return allocateUTF8('Emscripten Audio');
3580+
return stringToNewUTF8('Emscripten Audio');
35813581
},
35823582
SDL_GetScancodeFromKey: function (key) {
35833583
return SDL.scanCodes[key];

0 commit comments

Comments
 (0)