-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathlibrary_exceptions.js
330 lines (303 loc) · 11.5 KB
/
library_exceptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* @license
* Copyright 2010 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
var LibraryExceptions = {
__exception_last: '0',
__exception_caught: ' []',
__exception_infos: '{}',
__exception_deAdjust__deps: ['__exception_infos'],
__exception_deAdjust: function(adjusted) {
if (!adjusted || ___exception_infos[adjusted]) return adjusted;
for (var key in ___exception_infos) {
var ptr = +key; // the iteration key is a string, and if we throw this, it must be an integer as that is what we look for
var adj = ___exception_infos[ptr].adjusted;
var len = adj.length;
for (var i = 0; i < len; i++) {
if (adj[i] === adjusted) {
#if EXCEPTION_DEBUG
err('de-adjusted exception ptr ' + adjusted + ' to ' + ptr);
#endif
return ptr;
}
}
}
#if EXCEPTION_DEBUG
err('no de-adjustment for unknown exception ptr ' + adjusted);
#endif
return adjusted;
},
__exception_addRef__deps: ['__exception_infos'],
__exception_addRef: function(ptr) {
#if EXCEPTION_DEBUG
err('addref ' + ptr);
#endif
if (!ptr) return;
var info = ___exception_infos[ptr];
info.refcount++;
},
__exception_decRef__deps: ['__exception_infos', '__cxa_free_exception'
#if EXCEPTION_DEBUG
, '__exception_last', '__exception_caught'
#endif
],
__exception_decRef: function(ptr) {
#if EXCEPTION_DEBUG
err('decref ' + ptr);
#endif
if (!ptr) return;
var info = ___exception_infos[ptr];
#if ASSERTIONS
assert(info.refcount > 0);
#endif
info.refcount--;
// A rethrown exception can reach refcount 0; it must not be discarded
// Its next handler will clear the rethrown flag and addRef it, prior to
// final decRef and destruction here
if (info.refcount === 0 && !info.rethrown) {
if (info.destructor) {
#if WASM_BACKEND == 0
Module['dynCall_vi'](info.destructor, ptr);
#else
// In Wasm, destructors return 'this' as in ARM
Module['dynCall_ii'](info.destructor, ptr);
#endif
}
delete ___exception_infos[ptr];
___cxa_free_exception(ptr);
#if EXCEPTION_DEBUG
err('decref freeing exception ' + [ptr, ___exception_last, 'stack', ___exception_caught]);
#endif
}
},
__exception_clearRef__deps: ['__exception_infos'],
__exception_clearRef: function(ptr) {
if (!ptr) return;
var info = ___exception_infos[ptr];
info.refcount = 0;
},
// Exceptions
__cxa_allocate_exception: function(size) {
return _malloc(size);
},
__cxa_free_exception: function(ptr) {
#if ABORTING_MALLOC || ASSERTIONS
try {
#endif
return _free(ptr);
#if ABORTING_MALLOC || ASSERTIONS
} catch(e) {
#if ASSERTIONS
err('exception during cxa_free_exception: ' + e);
#endif
}
#endif
},
__cxa_increment_exception_refcount__deps: ['__exception_addRef', '__exception_deAdjust'],
__cxa_increment_exception_refcount: function(ptr) {
___exception_addRef(___exception_deAdjust(ptr));
},
__cxa_decrement_exception_refcount__deps: ['__exception_decRef', '__exception_deAdjust'],
__cxa_decrement_exception_refcount: function(ptr) {
___exception_decRef(___exception_deAdjust(ptr));
},
// Here, we throw an exception after recording a couple of values that we need to remember
// We also remember that it was the last exception thrown as we need to know that later.
__cxa_throw__sig: 'viii',
__cxa_throw__deps: ['__exception_infos', '__exception_last', '_ZSt18uncaught_exceptionv'],
__cxa_throw: function(ptr, type, destructor) {
#if EXCEPTION_DEBUG
err('Compiled code throwing an exception, ' + [ptr,type,destructor]);
#endif
___exception_infos[ptr] = {
ptr: ptr,
adjusted: [ptr],
type: type,
destructor: destructor,
refcount: 0,
caught: false,
rethrown: false
};
___exception_last = ptr;
if (!("uncaught_exception" in __ZSt18uncaught_exceptionv)) {
__ZSt18uncaught_exceptionv.uncaught_exceptions = 1;
} else {
__ZSt18uncaught_exceptionv.uncaught_exceptions++;
}
{{{ makeThrow('ptr') }}}
},
// This exception will be caught twice, but while begin_catch runs twice,
// we early-exit from end_catch when the exception has been rethrown, so
// pop that here from the caught exceptions.
__cxa_rethrow__deps: ['__exception_caught', '__exception_deAdjust', '__exception_infos', '__exception_last'],
__cxa_rethrow: function() {
var ptr = ___exception_caught.pop();
ptr = ___exception_deAdjust(ptr);
if (!___exception_infos[ptr].rethrown) {
// Only pop if the corresponding push was through rethrow_primary_exception
___exception_caught.push(ptr);
___exception_infos[ptr].rethrown = true;
}
#if EXCEPTION_DEBUG
err('Compiled code RE-throwing an exception, popped ' + [ptr, ___exception_last, 'stack', ___exception_caught]);
#endif
___exception_last = ptr;
{{{ makeThrow('ptr') }}}
},
llvm_eh_exception__deps: ['__exception_last'],
llvm_eh_exception: function() {
return ___exception_last;
},
llvm_eh_selector__jsargs: true,
llvm_eh_selector__deps: ['__exception_last'],
llvm_eh_selector: function(unused_exception_value, personality/*, varargs*/) {
var type = ___exception_last;
for (var i = 2; i < arguments.length; i++) {
if (arguments[i] == type) return type;
}
return 0;
},
llvm_eh_typeid_for: function(type) {
return type;
},
__cxa_begin_catch__deps: ['__exception_infos', '__exception_caught', '__exception_addRef', '__exception_deAdjust', '_ZSt18uncaught_exceptionv'],
__cxa_begin_catch: function(ptr) {
var info = ___exception_infos[ptr];
if (info && !info.caught) {
info.caught = true;
__ZSt18uncaught_exceptionv.uncaught_exceptions--;
}
if (info) info.rethrown = false;
___exception_caught.push(ptr);
#if EXCEPTION_DEBUG
err('cxa_begin_catch ' + [ptr, 'stack', ___exception_caught]);
#endif
___exception_addRef(___exception_deAdjust(ptr));
return ptr;
},
// We're done with a catch. Now, we can run the destructor if there is one
// and free the exception. Note that if the dynCall on the destructor fails
// due to calling apply on undefined, that means that the destructor is
// an invalid index into the FUNCTION_TABLE, so something has gone wrong.
__cxa_end_catch__deps: ['__exception_caught', '__exception_last', '__exception_decRef', '__exception_deAdjust', 'setThrew'],
__cxa_end_catch: function() {
// Clear state flag.
_setThrew(0);
// Call destructor if one is registered then clear it.
var ptr = ___exception_caught.pop();
#if EXCEPTION_DEBUG
err('cxa_end_catch popped ' + [ptr, ___exception_last, 'stack', ___exception_caught]);
#endif
if (ptr) {
___exception_decRef(___exception_deAdjust(ptr));
___exception_last = 0; // XXX in decRef?
}
},
__cxa_get_exception_ptr: function(ptr) {
#if EXCEPTION_DEBUG
err('cxa_get_exception_ptr ' + ptr);
#endif
// TODO: use info.adjusted?
return ptr;
},
_ZSt18uncaught_exceptionv: function() { // std::uncaught_exception()
return __ZSt18uncaught_exceptionv.uncaught_exceptions > 0;
},
__cxa_uncaught_exceptions__deps: ['_ZSt18uncaught_exceptionv'],
__cxa_uncaught_exceptions: function() {
return __ZSt18uncaught_exceptionv.uncaught_exceptions;
},
__cxa_call_unexpected: function(exception) {
err('Unexpected exception thrown, this is not properly supported - aborting');
#if !MINIMAL_RUNTIME
ABORT = true;
#endif
throw exception;
},
__cxa_current_primary_exception__deps: ['__exception_caught', '__exception_addRef', '__exception_deAdjust'],
__cxa_current_primary_exception: function() {
var ret = ___exception_caught[___exception_caught.length-1] || 0;
if (ret) ___exception_addRef(___exception_deAdjust(ret));
return ret;
},
__cxa_rethrow_primary_exception__deps: ['__exception_deAdjust', '__exception_caught', '__exception_infos', '__cxa_rethrow'],
__cxa_rethrow_primary_exception: function(ptr) {
if (!ptr) return;
ptr = ___exception_deAdjust(ptr);
___exception_caught.push(ptr);
___exception_infos[ptr].rethrown = true;
___cxa_rethrow();
},
// Finds a suitable catch clause for when an exception is thrown.
// In normal compilers, this functionality is handled by the C++
// 'personality' routine. This is passed a fairly complex structure
// relating to the context of the exception and makes judgements
// about how to handle it. Some of it is about matching a suitable
// catch clause, and some of it is about unwinding. We already handle
// unwinding using 'if' blocks around each function, so the remaining
// functionality boils down to picking a suitable 'catch' block.
// We'll do that here, instead, to keep things simpler.
__cxa_find_matching_catch__deps: ['__exception_last', '__exception_infos', '__resumeException'],
__cxa_find_matching_catch: function() {
var thrown = ___exception_last;
if (!thrown) {
// just pass through the null ptr
{{{ makeStructuralReturn([0, 0]) }}};
}
var info = ___exception_infos[thrown];
var throwntype = info.type;
if (!throwntype) {
// just pass through the thrown ptr
{{{ makeStructuralReturn(['thrown', 0]) }}};
}
var typeArray = Array.prototype.slice.call(arguments);
var pointer = {{{ exportedAsmFunc('___cxa_is_pointer_type') }}}(throwntype);
// can_catch receives a **, add indirection
#if EXCEPTION_DEBUG
out("can_catch on " + [thrown]);
#endif
#if DISABLE_EXCEPTION_CATCHING == 1
var buffer = 0;
#else
var buffer = {{{ makeStaticAlloc(4) }}};
#endif
{{{ makeSetValue('buffer', '0', 'thrown', '*') }}};
thrown = buffer;
// The different catch blocks are denoted by different types.
// Due to inheritance, those types may not precisely match the
// type of the thrown object. Find one which matches, and
// return the type of the catch block which should be called.
for (var i = 0; i < typeArray.length; i++) {
if (typeArray[i] && {{{ exportedAsmFunc('___cxa_can_catch') }}}(typeArray[i], throwntype, thrown)) {
thrown = {{{ makeGetValue('thrown', '0', '*') }}}; // undo indirection
info.adjusted.push(thrown);
#if EXCEPTION_DEBUG
out(" can_catch found " + [thrown, typeArray[i]]);
#endif
{{{ makeStructuralReturn(['thrown', 'typeArray[i]']) }}};
}
}
// Shouldn't happen unless we have bogus data in typeArray
// or encounter a type for which emscripten doesn't have suitable
// typeinfo defined. Best-efforts match just in case.
thrown = {{{ makeGetValue('thrown', '0', '*') }}}; // undo indirection
{{{ makeStructuralReturn(['thrown', 'throwntype']) }}};
},
__resumeException__deps: [function() { '__exception_last', Functions.libraryFunctions['___resumeException'] = 1 }], // will be called directly from compiled code
__resumeException: function(ptr) {
#if EXCEPTION_DEBUG
out("Resuming exception " + [ptr, ___exception_last]);
#endif
if (!___exception_last) { ___exception_last = ptr; }
{{{ makeThrow('ptr') }}}
},
};
// In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc.
// where the number specifies the number of arguments. In Emscripten, route all these to a single function '__cxa_find_matching_catch'
// that variadically processes all of these functions using JS 'arguments' object.
addCxaCatch = function(n) {
LibraryManager.library['__cxa_find_matching_catch_' + n] = LibraryExceptions['__cxa_find_matching_catch'];
LibraryManager.library['__cxa_find_matching_catch_' + n + '__sig'] = new Array(n + 2).join('i');
};
mergeInto(LibraryManager.library, LibraryExceptions);