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