Skip to content

Commit

Permalink
basic support for dynamic_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Nov 8, 2011
1 parent 938b121 commit 6fe0acc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/library.js
Expand Up @@ -4051,6 +4051,27 @@ LibraryManager.library = {
return 1;
},

_ZTVN10__cxxabiv117__class_type_infoE: [1], // no inherited classes
_ZTVN10__cxxabiv120__si_class_type_infoE: [2], // yes inherited classes

__dynamic_cast: function(ptr, knownTI, attemptedTI, idunno) {
var ptrTV = {{{ makeGetValue('ptr', '0', '*') }}};
var count = {{{ makeGetValue('ptrTV', '0', '*') }}};
ptrTV -= {{{ QUANTUM_SIZE }}};
var TI = {{{ makeGetValue('ptrTV', '0', '*') }}};
do {
if (TI == attemptedTI) return 1;
// Go to parent class
var type_infoAddr = {{{ makeGetValue('TI', '0', '*') }}} - {{{ QUANTUM_SIZE*2 }}};
var type_info = {{{ makeGetValue('type_infoAddr', '0', '*') }}};
if (type_info == 1) return 0; // no parent class
var TIAddr = TI + {{{ QUANTUM_SIZE*2 }}};
var TI = {{{ makeGetValue('TIAddr', '0', '*') }}};
} while (1);

return 0;
},

// Exceptions
__cxa_allocate_exception: function(size) {
return _malloc(size);
Expand Down
31 changes: 31 additions & 0 deletions tests/runner.py
Expand Up @@ -1063,6 +1063,37 @@ def test_polymorph(self):
'''
self.do_run(src, '*11,74,32,1012*\n*11*\n*22*')

def test_dynamic_cast(self):
src = '''
#include <stdio.h>
class CBase { virtual void dummy() {} };
class CDerived : public CBase { int a; };
class CDerivedest : public CDerived { float b; };
int main ()
{
CBase *pa = new CBase;
CBase *pb = new CDerived;
CBase *pc = new CDerivedest;
printf("a1: %d\\n", dynamic_cast<CDerivedest*>(pa) != NULL);
printf("a2: %d\\n", dynamic_cast<CDerived*>(pa) != NULL);
printf("a3: %d\\n", dynamic_cast<CBase*>(pa) != NULL);
printf("b1: %d\\n", dynamic_cast<CDerivedest*>(pb) != NULL);
printf("b2: %d\\n", dynamic_cast<CDerived*>(pb) != NULL);
printf("b3: %d\\n", dynamic_cast<CBase*>(pb) != NULL);
printf("c1: %d\\n", dynamic_cast<CDerivedest*>(pc) != NULL);
printf("c2: %d\\n", dynamic_cast<CDerived*>(pc) != NULL);
printf("c3: %d\\n", dynamic_cast<CBase*>(pc) != NULL);
return 0;
}
'''
self.do_run(src, 'a1: 0\na2: 0\na3: 1\nb1: 0\nb2: 1\nb3: 1\nc1: 1\nc2: 1\nc3: 1\n')

def test_funcptr(self):
src = '''
#include <stdio.h>
Expand Down

0 comments on commit 6fe0acc

Please sign in to comment.