@@ -37,14 +37,6 @@ var LibraryExceptions = {
3737 { { { makeSetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . exceptionDestructor , 'destructor' , '*' ) } } } ;
3838 } ;
3939
40- this . get_destructor = function ( ) {
41- return { { { makeGetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . exceptionDestructor , '*' ) } } } ;
42- } ;
43-
44- this . set_refcount = function ( refcount ) {
45- { { { makeSetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . referenceCount , 'refcount' , 'i32' ) } } } ;
46- } ;
47-
4840 this . set_caught = function ( caught ) {
4941 caught = caught ? 1 : 0 ;
5042 { { { makeSetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . caught , 'caught' , 'i8' ) } } } ;
@@ -67,33 +59,7 @@ var LibraryExceptions = {
6759 this . init = function ( type , destructor ) {
6860 this . set_type ( type ) ;
6961 this . set_destructor ( destructor ) ;
70- this . set_refcount ( 0 ) ;
71- this . set_caught ( false ) ;
72- this . set_rethrown ( false ) ;
7362 }
74-
75- this . add_ref = function ( ) {
76- #if USE_PTHREADS
77- Atomics . add ( HEAP32 , ( this . ptr + { { { C_STRUCTS . __cxa_exception . referenceCount } } } ) >> 2 , 1 ) ;
78- #else
79- var value = { { { makeGetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . referenceCount , 'i32' ) } } } ;
80- { { { makeSetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . referenceCount , 'value + 1' , 'i32' ) } } } ;
81- #endif
82- } ;
83-
84- // Returns true if last reference released.
85- this . release_ref = function ( ) {
86- #if USE_PTHREADS
87- var prev = Atomics . sub ( HEAP32 , ( this . ptr + { { { C_STRUCTS . __cxa_exception . referenceCount } } } ) >> 2 , 1 ) ;
88- #else
89- var prev = { { { makeGetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . referenceCount , 'i32' ) } } } ;
90- { { { makeSetValue ( 'this.ptr' , C_STRUCTS . __cxa_exception . referenceCount , 'prev - 1' , 'i32' ) } } } ;
91- #endif
92- #if ASSERTIONS
93- assert ( prev > 0 ) ;
94- #endif
95- return prev === 1 ;
96- } ;
9763 } ,
9864
9965 $CatchInfo__deps: [ '$ExceptionInfo' , '__cxa_is_pointer_type' ] ,
@@ -160,80 +126,13 @@ var LibraryExceptions = {
160126 }
161127 } ,
162128
163- $exception_addRef: function ( info ) {
164- #if EXCEPTION_DEBUG
165- err ( 'addref ' + info . excPtr ) ;
166- #endif
167- info . add_ref ( ) ;
168- } ,
169-
170- $exception_decRef__deps : [ '__cxa_free_exception'
171- #if EXCEPTION_DEBUG
172- , '$exceptionLast' , '$exceptionCaught'
173- #endif
174- ] ,
175- $exception_decRef : function ( info ) {
176- #if EXCEPTION_DEBUG
177- err ( 'decref ' + info . excPtr ) ;
178- #endif
179- // A rethrown exception can reach refcount 0; it must not be discarded
180- // Its next handler will clear the rethrown flag and addRef it, prior to
181- // final decRef and destruction here
182- if ( info . release_ref ( ) && ! info . get_rethrown ( ) ) {
183- var destructor = info . get_destructor ( ) ;
184- if ( destructor ) {
185- // In Wasm, destructors return 'this' as in ARM
186- { { { makeDynCall ( 'ii' , 'destructor' ) } } } ( info . excPtr ) ;
187- }
188- ___cxa_free_exception ( info . excPtr ) ;
189- #if EXCEPTION_DEBUG
190- err ( 'decref freeing exception ' + [ info . excPtr , exceptionLast , 'stack' , exceptionCaught ] ) ;
191- #endif
192- }
193- } ,
194-
195- // Exceptions
196- __cxa_allocate_exception__sig : 'vi' ,
197- __cxa_allocate_exception : function ( size ) {
198- // Thrown object is prepended by exception metadata block
199- return _malloc ( size + { { { C_STRUCTS . __cxa_exception . __size__ } } } ) + { { { C_STRUCTS . __cxa_exception . __size__ } } } ;
200- } ,
201-
202- __cxa_free_exception__deps: [ '$ExceptionInfo' ] ,
203- __cxa_free_exception__sig : 'vi' ,
204- __cxa_free_exception : function ( ptr ) {
205- #if ABORTING_MALLOC || ASSERTIONS
206- try {
207- #endif
208- return _free ( new ExceptionInfo ( ptr ) . ptr ) ;
209- #if ABORTING_MALLOC || ASSERTIONS
210- } catch ( e ) {
211- #if ASSERTIONS
212- err ( 'exception during cxa_free_exception: ' + e ) ;
213- #endif
214- }
215- #endif
216- } ,
217-
218- __cxa_increment_exception_refcount__deps : [ '$exception_addRef' , '$ExceptionInfo' ] ,
219- __cxa_increment_exception_refcount : function ( ptr ) {
220- if ( ! ptr ) return ;
221- exception_addRef ( new ExceptionInfo ( ptr ) ) ;
222- } ,
223-
224- __cxa_decrement_exception_refcount__deps : [ '$exception_decRef' , '$ExceptionInfo' ] ,
225- __cxa_decrement_exception_refcount : function ( ptr ) {
226- if ( ! ptr ) return ;
227- exception_decRef ( new ExceptionInfo ( ptr ) ) ;
228- } ,
229-
230129 // Here, we throw an exception after recording a couple of values that we need to remember
231130 // We also remember that it was the last exception thrown as we need to know that later.
232131 __cxa_throw__sig: 'viii' ,
233132 __cxa_throw__deps : [ '$ExceptionInfo' , '$exceptionLast' , '$uncaughtExceptionCount' ] ,
234133 __cxa_throw : function ( ptr , type , destructor ) {
235134#if EXCEPTION_DEBUG
236- err ( 'Compiled code throwing an exception, ' + [ ptr , type , destructor ] ) ;
135+ err ( 'Compiled code throwing an exception, ' + [ ptr . toString ( 16 ) , type , destructor ] ) ;
237136#endif
238137 var info = new ExceptionInfo ( ptr ) ;
239138 // Initialize ExceptionInfo content after it was allocated in __cxa_allocate_exception.
@@ -276,8 +175,9 @@ var LibraryExceptions = {
276175 return type ;
277176 } ,
278177
279- __cxa_begin_catch__deps : [ '$CatchInfo' , '$exceptionCaught' , '$exception_addRef' ,
280- '$uncaughtExceptionCount' ] ,
178+ __cxa_begin_catch__deps : [ '$CatchInfo' , '$exceptionCaught' ,
179+ '$uncaughtExceptionCount' ,
180+ '__cxa_increment_exception_refcount' ] ,
281181 __cxa_begin_catch : function ( ptr ) {
282182 var catchInfo = new CatchInfo ( ptr ) ;
283183 var info = catchInfo . get_exception_info ( ) ;
@@ -290,15 +190,15 @@ var LibraryExceptions = {
290190#if EXCEPTION_DEBUG
291191 err ( 'cxa_begin_catch ' + [ ptr , 'stack' , exceptionCaught ] ) ;
292192#endif
293- exception_addRef ( info ) ;
193+ ___cxa_increment_exception_refcount ( catchInfo . get_base_ptr ( ) ) ;
294194 return catchInfo . get_exception_ptr ( ) ;
295195 } ,
296196
297197 // We're done with a catch. Now, we can run the destructor if there is one
298198 // and free the exception. Note that if the dynCall on the destructor fails
299199 // due to calling apply on undefined, that means that the destructor is
300200 // an invalid index into the FUNCTION_TABLE, so something has gone wrong.
301- __cxa_end_catch__deps : [ '$exceptionCaught' , '$exceptionLast' , '$exception_decRef ' ,
201+ __cxa_end_catch__deps : [ '$exceptionCaught' , '$exceptionLast' , '__cxa_decrement_exception_refcount ' ,
302202 '$CatchInfo' ] ,
303203 __cxa_end_catch__sig : 'v' ,
304204 __cxa_end_catch : function ( ) {
@@ -313,7 +213,7 @@ var LibraryExceptions = {
313213#if EXCEPTION_DEBUG
314214 err ( 'cxa_end_catch popped ' + [ catchInfo , exceptionLast , 'stack' , exceptionCaught ] ) ;
315215#endif
316- exception_decRef ( catchInfo . get_exception_info ( ) ) ;
216+ ___cxa_decrement_exception_refcount ( catchInfo . get_base_ptr ( ) ) ;
317217 catchInfo . free ( ) ;
318218 exceptionLast = 0 ; // XXX in decRef?
319219 } ,
@@ -339,13 +239,15 @@ var LibraryExceptions = {
339239 throw exception ;
340240 } ,
341241
342- __cxa_current_primary_exception__deps : [ '$exceptionCaught' , '$exception_addRef' , '$CatchInfo' ] ,
242+ __cxa_current_primary_exception__deps : [ '$exceptionCaught' ,
243+ '__cxa_increment_exception_refcount' ,
244+ '$CatchInfo' ] ,
343245 __cxa_current_primary_exception : function ( ) {
344246 if ( ! exceptionCaught . length ) {
345247 return 0 ;
346248 }
347249 var catchInfo = exceptionCaught [ exceptionCaught . length - 1 ] ;
348- exception_addRef ( catchInfo . get_exception_info ( ) ) ;
250+ ___cxa_increment_exception_refcount ( catchInfo . get_base_ptr ( ) ) ;
349251 return catchInfo . get_base_ptr ( ) ;
350252 } ,
351253
0 commit comments