aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library.js21
-rw-r--r--tests/runner.py31
2 files changed, 52 insertions, 0 deletions
diff --git a/src/library.js b/src/library.js
index 2e0adb64..73caa37c 100644
--- a/src/library.js
+++ b/src/library.js
@@ -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);
diff --git a/tests/runner.py b/tests/runner.py
index bc5db67d..95fe03c4 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1063,6 +1063,37 @@ if 'benchmark' not in str(sys.argv):
'''
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>