Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/liblegacy.js
6175 views
1
/**
2
* @license
3
* Copyright 2010 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*
6
* Legacy library symbols that are no longer used by emscripten itself but
7
* could have external users.
8
*
9
* Symbols in this file are not available in `-sSTRICT` mode.
10
*
11
* Any usage of symbols in this file will result in a `-Wdeprecated` warning.
12
*
13
* Symbol in this file should be removed after "enough time" has passed such
14
* that all external users have been able to transition away.
15
*/
16
17
legacyFuncs = {
18
$ALLOC_NORMAL: 0, // Tries to use _malloc()
19
$ALLOC_STACK: 1, // Lives for the duration of the current function call
20
21
/**
22
* allocate(): This function is no longer used by emscripten but is kept around to avoid
23
* breaking external users.
24
* You should normally not use allocate(), and instead allocate
25
* memory using _malloc()/stackAlloc(), initialize it with
26
* setValue(), and so forth.
27
* @param {(Uint8Array|Array<number>)} slab: An array of data.
28
* @param {number=} allocator : How to allocate memory, see ALLOC_*
29
*/
30
$allocate__deps: ['$ALLOC_STACK', 'malloc', '$stackAlloc'],
31
$allocate: (slab, allocator) => {
32
var ret;
33
#if ASSERTIONS
34
assert(typeof allocator == 'number', 'allocate no longer takes a type argument')
35
assert(typeof slab != 'number', 'allocate no longer takes a number as arg0')
36
#endif
37
38
if (allocator == ALLOC_STACK) {
39
ret = stackAlloc(slab.length);
40
} else {
41
ret = _malloc(slab.length);
42
}
43
44
if (!slab.subarray && !slab.slice) {
45
slab = new Uint8Array(slab);
46
}
47
HEAPU8.set(slab, ret);
48
return ret;
49
},
50
51
// Deprecated: This function should not be called because it is unsafe and
52
// does not provide a maximum length limit of how many bytes it is allowed to
53
// write. Prefer calling the function stringToUTF8Array() instead, which takes
54
// in a maximum length that can be used to be secure from out of bounds
55
// writes.
56
$writeStringToMemory__docs: '/** @deprecated @param {boolean=} dontAddNull */',
57
$writeStringToMemory__deps: ['$lengthBytesUTF8', '$stringToUTF8'],
58
$writeStringToMemory: (string, buffer, dontAddNull) => {
59
warnOnce('writeStringToMemory is deprecated and should not be called! Use stringToUTF8() instead!');
60
61
var /** @type {number} */ lastChar, /** @type {number} */ end;
62
if (dontAddNull) {
63
// stringToUTF8 always appends null. If we don't want to do that, remember the
64
// character that existed at the location where the null will be placed, and restore
65
// that after the write (below).
66
end = buffer + lengthBytesUTF8(string);
67
lastChar = HEAP8[end];
68
}
69
stringToUTF8(string, buffer, Infinity);
70
if (dontAddNull) HEAP8[end] = lastChar; // Restore the value under the null character.
71
},
72
73
// Deprecated: Use stringToAscii
74
$writeAsciiToMemory__docs: '/** @param {boolean=} dontAddNull */',
75
$writeAsciiToMemory: (str, buffer, dontAddNull) => {
76
for (var i = 0; i < str.length; ++i) {
77
#if ASSERTIONS
78
assert(str.charCodeAt(i) === (str.charCodeAt(i) & 0xff));
79
#endif
80
{{{ makeSetValue('buffer++', 0, 'str.charCodeAt(i)', 'i8') }}};
81
}
82
// Null-terminate the string
83
if (!dontAddNull) {{{ makeSetValue('buffer', 0, 0, 'i8') }}};
84
},
85
86
$allocateUTF8__deps: ['$stringToNewUTF8'],
87
$allocateUTF8: (...args) => stringToNewUTF8(...args),
88
$allocateUTF8OnStack__deps: ['$stringToUTF8OnStack'],
89
$allocateUTF8OnStack: (...args) => stringToUTF8OnStack(...args),
90
91
#if LINK_AS_CXX
92
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free', '$stringToUTF8OnStack'],
93
$demangle: (func) => {
94
// If demangle has failed before, stop demangling any further function names
95
// This avoids an infinite recursion with malloc()->abort()->stackTrace()->demangle()->malloc()->...
96
demangle.recursionGuard = (demangle.recursionGuard|0)+1;
97
if (demangle.recursionGuard > 1) return func;
98
return withStackSave(() => {
99
try {
100
var s = func;
101
if (s.startsWith('__Z'))
102
s = s.slice(1);
103
var buf = stringToUTF8OnStack(s);
104
var status = stackAlloc(4);
105
var ret = ___cxa_demangle(buf, 0, 0, status);
106
if ({{{ makeGetValue('status', '0', 'i32') }}} === 0 && ret) {
107
return UTF8ToString(ret);
108
}
109
// otherwise, libcxxabi failed
110
} catch(e) {
111
} finally {
112
_free(ret);
113
if (demangle.recursionGuard < 2) --demangle.recursionGuard;
114
}
115
// failure when using libcxxabi, don't demangle
116
return func;
117
});
118
},
119
#endif
120
121
$stackTrace__deps: ['$jsStackTrace'],
122
$stackTrace: () => {
123
var js = jsStackTrace();
124
if (Module['extraStackTrace']) js += '\n' + Module['extraStackTrace']();
125
return js;
126
},
127
128
// Legacy names for runtime `out`/`err` symbols.
129
$print: '=out',
130
$printErr: '=err',
131
132
// Converts a JS string to an integer base-10. Despite _s, which
133
// suggests signaling error handling, this returns NaN on error.
134
// (This was a mistake in the original implementation, and kept
135
// to avoid breakage.)
136
$jstoi_s: 'Number',
137
138
$getNativeTypeSize__deps: ['$POINTER_SIZE'],
139
$getNativeTypeSize: {{{ getNativeTypeSize }}},
140
};
141
142
if (WARN_DEPRECATED && !INCLUDE_FULL_LIBRARY) {
143
for (const name of Object.keys(legacyFuncs)) {
144
if (!isDecorator(name)) {
145
const depsKey = `${name}__deps`;
146
legacyFuncs[depsKey] ??= []
147
legacyFuncs[depsKey].push(() => {
148
warn(`JS library symbol '${name}' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]`);
149
});
150
}
151
}
152
}
153
154
addToLibrary(legacyFuncs);
155
156