Skip to content

Commit 41f442c

Browse files
authored
Move writeStringToMemory/writeAsciiToMemory to library_legacy.js (#19103)
`writeStringToMemory` was already marked as deprecated. We have `stringToAscii` and `stringToUTF8` which we recommend over these function. Also, change `stringToUTF8Array` to `stringToUTF8` in a couple of places here the target is `HEAPU8`.
1 parent cd1aacc commit 41f442c

File tree

9 files changed

+59
-99
lines changed

9 files changed

+59
-99
lines changed

site/source/docs/api_reference/preamble.js.rst

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -252,32 +252,6 @@ Conversion functions — strings, pointers and arrays
252252
:returns: A ``String``, containing the content of ``array``.
253253

254254

255-
256-
.. js:function:: writeStringToMemory(string, buffer, dontAddNull)
257-
258-
Writes a JavaScript string to a specified address in the heap.
259-
260-
.. warning:: This function is deprecated, you should call the function ``stringToUTF8`` instead, which provides a secure bounded version of the same functionality instead.
261-
262-
.. code-block:: javascript
263-
264-
// Allocate space for string and extra '0' at the end
265-
var buffer = Module._malloc(myString.length+1);
266-
267-
// Write the string to memory
268-
Module.writeStringToMemory(myString, buffer);
269-
270-
// We can now send buffer into a C function, it is just a normal char* pointer
271-
272-
:param string: The string to write into memory.
273-
:type string: String
274-
:param buffer: The address (number) where ``string`` is to be written.
275-
:type buffer: Number
276-
:param dontAddNull: If ``true``, the new array is not zero-terminated.
277-
:type dontAddNull: bool
278-
279-
280-
281255
.. js:function:: writeArrayToMemory(array, buffer)
282256

283257
Writes an array to a specified address in the heap. Note that memory should to be allocated for the array before it is written.
@@ -288,29 +262,8 @@ Conversion functions — strings, pointers and arrays
288262

289263

290264

291-
.. js:function:: writeAsciiToMemory(str, buffer, dontAddNull)
292-
293-
Writes an ASCII string to a specified address in the heap. Note that memory should to be allocated for the string before it is written.
294-
295-
The string is assumed to only have characters in the ASCII character set. If ASSERTIONS are enabled and this is not the case, it will fail.
296-
297-
.. code-block:: javascript
298-
299-
// Allocate space for string
300-
var buffer = Module._malloc(myString.length);
301-
302-
// Write the string to memory
303-
Module.writeStringToMemory(myString, buffer);
304-
305-
:param string: The string to write into memory.
306-
:param buffer: The address where ``string`` is to be written.
307-
:param dontAddNull: If ``true``, the new string is not zero-terminated.
308-
:type dontAddNull: bool
309-
310-
311-
312265
Run dependencies
313-
=====================================
266+
================
314267

315268
Note that generally run dependencies are managed by the file packager and other parts of the system. It is rare for developers to use this API directly.
316269

@@ -338,7 +291,7 @@ Note that generally run dependencies are managed by the file packager and other
338291

339292

340293
Stack trace
341-
=====================
294+
===========
342295

343296
.. js:function:: stackTrace()
344297

src/library.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,15 +2078,15 @@ mergeInto(LibraryManager.library, {
20782078
list: [],
20792079
map: {}
20802080
},
2081-
setprotoent__deps: ['$Protocols', '$writeAsciiToMemory', 'malloc'],
2081+
setprotoent__deps: ['$Protocols', '$stringToAscii', 'malloc'],
20822082
setprotoent: function(stayopen) {
20832083
// void setprotoent(int stayopen);
20842084

20852085
// Allocate and populate a protoent structure given a name, protocol number and array of aliases
20862086
function allocprotoent(name, proto, aliases) {
20872087
// write name into buffer
20882088
var nameBuf = _malloc(name.length + 1);
2089-
writeAsciiToMemory(name, nameBuf);
2089+
stringToAscii(name, nameBuf);
20902090

20912091
// write aliases into buffer
20922092
var j = 0;
@@ -2096,7 +2096,7 @@ mergeInto(LibraryManager.library, {
20962096
for (var i = 0; i < length; i++, j += 4) {
20972097
var alias = aliases[i];
20982098
var aliasBuf = _malloc(alias.length + 1);
2099-
writeAsciiToMemory(alias, aliasBuf);
2099+
stringToAscii(alias, aliasBuf);
21002100
{{{ makeSetValue('aliasListBuf', 'j', 'aliasBuf', POINTER_TYPE) }}};
21012101
}
21022102
{{{ makeSetValue('aliasListBuf', 'j', '0', POINTER_TYPE) }}}; // Terminating NULL pointer.

src/library_legacy.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,40 @@ mergeInto(LibraryManager.library, {
3838
return ret;
3939
},
4040

41+
// Deprecated: This function should not be called because it is unsafe and
42+
// does not provide a maximum length limit of how many bytes it is allowed to
43+
// write. Prefer calling the function stringToUTF8Array() instead, which takes
44+
// in a maximum length that can be used to be secure from out of bounds
45+
// writes.
46+
$writeStringToMemory__docs: '/** @deprecated @param {boolean=} dontAddNull */',
47+
$writeStringToMemory: function(string, buffer, dontAddNull) {
48+
warnOnce('writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!');
49+
50+
var /** @type {number} */ lastChar, /** @type {number} */ end;
51+
if (dontAddNull) {
52+
// stringToUTF8Array always appends null. If we don't want to do that, remember the
53+
// character that existed at the location where the null will be placed, and restore
54+
// that after the write (below).
55+
end = buffer + lengthBytesUTF8(string);
56+
lastChar = HEAP8[end];
57+
}
58+
stringToUTF8(string, buffer, Infinity);
59+
if (dontAddNull) HEAP8[end] = lastChar; // Restore the value under the null character.
60+
},
61+
62+
// Deprecated: Use stringToAscii
63+
$writeAsciiToMemory__docs: '/** @param {boolean=} dontAddNull */',
64+
$writeAsciiToMemory: function(str, buffer, dontAddNull) {
65+
for (var i = 0; i < str.length; ++i) {
66+
#if ASSERTIONS
67+
assert(str.charCodeAt(i) === (str.charCodeAt(i) & 0xff));
68+
#endif
69+
{{{ makeSetValue('buffer++', 0, 'str.charCodeAt(i)', 'i8') }}};
70+
}
71+
// Null-terminate the string
72+
if (!dontAddNull) {{{ makeSetValue('buffer', 0, 0, 'i8') }}};
73+
},
74+
4175
$allocateUTF8: '$stringToNewUTF8',
4276
$allocateUTF8OnStack: '$stringToUTF8OnStack',
4377
});

src/library_strings.js

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ mergeInto(LibraryManager.library, {
2929
// Copies the given Javascript String object 'str' to the emscripten HEAP at
3030
// address 'outPtr', null-terminated and encoded in ASCII form. The copy will
3131
// require at most str.length+1 bytes of space in the HEAP.
32-
$stringToAscii__deps: ['$writeAsciiToMemory'],
33-
$stringToAscii: function(str, outPtr) {
34-
return writeAsciiToMemory(str, outPtr, false);
32+
$stringToAscii: function(str, buffer) {
33+
for (var i = 0; i < str.length; ++i) {
34+
#if ASSERTIONS
35+
assert(str.charCodeAt(i) === (str.charCodeAt(i) & 0xff));
36+
#endif
37+
{{{ makeSetValue('buffer++', 0, 'str.charCodeAt(i)', 'i8') }}};
38+
}
39+
// Null-terminate the string
40+
{{{ makeSetValue('buffer', 0, 0, 'i8') }}};
3541
},
3642

3743
#if TEXTDECODER == 2
@@ -229,55 +235,22 @@ mergeInto(LibraryManager.library, {
229235
$stringToNewUTF8: function(str) {
230236
var size = lengthBytesUTF8(str) + 1;
231237
var ret = {{{ makeMalloc('stringToNewUTF8', 'size') }}};
232-
if (ret) stringToUTF8Array(str, HEAP8, ret, size);
238+
if (ret) stringToUTF8(str, ret, size);
233239
return ret;
234240
},
235241

236242
// Allocate stack space for a JS string, and write it there.
237243
$stringToUTF8OnStack: function(str) {
238244
var size = lengthBytesUTF8(str) + 1;
239245
var ret = stackAlloc(size);
240-
stringToUTF8Array(str, HEAP8, ret, size);
246+
stringToUTF8(str, ret, size);
241247
return ret;
242248
},
243249

244-
// Deprecated: This function should not be called because it is unsafe and
245-
// does not provide a maximum length limit of how many bytes it is allowed to
246-
// write. Prefer calling the function stringToUTF8Array() instead, which takes
247-
// in a maximum length that can be used to be secure from out of bounds
248-
// writes.
249-
$writeStringToMemory__docs: '/** @deprecated @param {boolean=} dontAddNull */',
250-
$writeStringToMemory: function(string, buffer, dontAddNull) {
251-
warnOnce('writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!');
252-
253-
var /** @type {number} */ lastChar, /** @type {number} */ end;
254-
if (dontAddNull) {
255-
// stringToUTF8Array always appends null. If we don't want to do that, remember the
256-
// character that existed at the location where the null will be placed, and restore
257-
// that after the write (below).
258-
end = buffer + lengthBytesUTF8(string);
259-
lastChar = HEAP8[end];
260-
}
261-
stringToUTF8(string, buffer, Infinity);
262-
if (dontAddNull) HEAP8[end] = lastChar; // Restore the value under the null character.
263-
},
264-
265250
$writeArrayToMemory: function(array, buffer) {
266251
#if ASSERTIONS
267252
assert(array.length >= 0, 'writeArrayToMemory array must have a length (should be an array or typed array)')
268253
#endif
269254
HEAP8.set(array, buffer);
270255
},
271-
272-
$writeAsciiToMemory__docs: '/** @param {boolean=} dontAddNull */',
273-
$writeAsciiToMemory: function(str, buffer, dontAddNull) {
274-
for (var i = 0; i < str.length; ++i) {
275-
#if ASSERTIONS
276-
assert(str.charCodeAt(i) === (str.charCodeAt(i) & 0xff));
277-
#endif
278-
{{{ makeSetValue('buffer++', 0, 'str.charCodeAt(i)', 'i8') }}};
279-
}
280-
// Null-terminate the pointer to the HEAP.
281-
if (!dontAddNull) {{{ makeSetValue('buffer', 0, 0, 'i8') }}};
282-
},
283256
});

src/library_wasi.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ var WasiLibrary = {
8787
return 0;
8888
},
8989

90-
environ_get__deps: ['$getEnvStrings', '$writeAsciiToMemory'],
90+
environ_get__deps: ['$getEnvStrings', '$stringToAscii'],
9191
environ_get__nothrow: true,
9292
environ_get: function(__environ, environ_buf) {
9393
var bufSize = 0;
9494
getEnvStrings().forEach(function(string, i) {
9595
var ptr = environ_buf + bufSize;
9696
{{{ makeSetValue('__environ', `i*${POINTER_SIZE}`, 'ptr', POINTER_TYPE) }}};
97-
writeAsciiToMemory(string, ptr);
97+
stringToAscii(string, ptr);
9898
bufSize += string.length + 1;
9999
});
100100
return 0;
@@ -119,14 +119,14 @@ var WasiLibrary = {
119119
},
120120

121121
args_get__nothrow: true,
122-
args_get__deps: ['$writeAsciiToMemory'],
122+
args_get__deps: ['$stringToAscii'],
123123
args_get: function(argv, argv_buf) {
124124
#if MAIN_READS_PARAMS
125125
var bufSize = 0;
126126
mainArgs.forEach(function(arg, i) {
127127
var ptr = argv_buf + bufSize;
128128
{{{ makeSetValue('argv', `i*${POINTER_SIZE}`, 'ptr', POINTER_TYPE) }}};
129-
writeAsciiToMemory(arg, ptr);
129+
stringToAscii(arg, ptr);
130130
bufSize += arg.length + 1;
131131
});
132132
#endif
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27921
1+
27922
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6436
1+
6429
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
63435
1+
63386

test/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5887,11 +5887,11 @@ def test_utf32(self):
58875887

58885888
@crossplatform
58895889
def test_utf16(self):
5890-
self.set_setting('EXPORTED_RUNTIME_METHODS', ['writeAsciiToMemory', 'UTF16ToString', 'stringToUTF16'])
5890+
self.set_setting('EXPORTED_RUNTIME_METHODS', ['UTF16ToString', 'stringToUTF16'])
58915891
self.do_runf(test_file('core/test_utf16.cpp'), 'OK.')
58925892

58935893
def test_utf8(self):
5894-
self.set_setting('EXPORTED_RUNTIME_METHODS', ['UTF8ToString', 'stringToUTF8', 'AsciiToString', 'stringToAscii', 'writeAsciiToMemory'])
5894+
self.set_setting('EXPORTED_RUNTIME_METHODS', ['UTF8ToString', 'stringToUTF8', 'AsciiToString', 'stringToAscii'])
58955895
self.do_runf(test_file('utf8.cpp'), 'OK.')
58965896

58975897
@also_with_wasm_bigint

0 commit comments

Comments
 (0)