aboutsummaryrefslogtreecommitdiff
path: root/system/lib/embind/bind.cpp
blob: c69c439923950f66d5577f3e76a7de72d17bd163 (plain)
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
#include <emscripten/bind.h>
#include <../lib/libcxxabi/src/private_typeinfo.h>
#include <../lib/libcxxabi/include/cxxabi.h>
#include <list>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include <emscripten/emscripten.h>

using namespace emscripten;

namespace __cxxabiv1 {
    std::vector<const __class_type_info*> __getBaseClasses(const __class_type_info* cti) {
        std::vector<const __class_type_info*> bases;

        const __si_class_type_info* scti = dynamic_cast<const __si_class_type_info*>(cti);
        if (scti) {
            bases.emplace_back(scti->__base_type);
        } else {
            const __vmi_class_type_info* vcti = dynamic_cast<const __vmi_class_type_info*>(cti);
            if (vcti) {
                for (int i = 0; i < vcti->__base_count; i++) {
                    bases.emplace_back(vcti->__base_info[i].__base_type);
                }
            }
        }
        return bases;
    }

    int __getBaseOffset(const __class_type_info* ctiDv, const __class_type_info* ctiBs) {
        int offset = 0;

        const __vmi_class_type_info* vcti = dynamic_cast<const __vmi_class_type_info*>(ctiDv);
        if (vcti) {
            for (int i = 0; i < vcti->__base_count; i++) {
                if (vcti->__base_info[i].__base_type == ctiBs) {
                    offset = vcti->__base_info[i].__offset_flags >> __base_class_type_info::__offset_shift;
                    break;
                }
            }
        }
        return offset;
    }

    void __getDerivationPaths(const __class_type_info* dv, const __class_type_info* bs, std::vector<const __class_type_info*>path, std::vector<std::vector<const __class_type_info*>>& paths) {
        std::vector<const __class_type_info*> newPath(path);
        newPath.emplace_back(dv);
        if (dv == bs) {
            paths.emplace_back(newPath);
        } else {
            std::vector<const __class_type_info*> bases = __getBaseClasses(dv);
            for (int i = 0; i < bases.size(); i++) {
                __getDerivationPaths(bases[i], bs, newPath, paths);
            }
        }
    }

    int __pathOffset(std::vector<const __cxxabiv1::__class_type_info*> path) {
        int offset = 0;
        for (int i = 0; i < path.size()-1; i++) {
            offset += __getBaseOffset(path[i], path[i+1]);
        }
        return offset;
    }
}

namespace emscripten {
    namespace internal {
        void registerStandardTypes() {
            static bool first = true;
            if (first) {
                first = false;

                _embind_register_void(TypeID<void>::get(), "void");

                _embind_register_bool(TypeID<bool>::get(), "bool", true, false);

                _embind_register_integer(TypeID<char>::get(), "char");
                _embind_register_integer(TypeID<signed char>::get(), "signed char");
                _embind_register_integer(TypeID<unsigned char>::get(), "unsigned char");
                _embind_register_integer(TypeID<signed short>::get(), "short");
                _embind_register_integer(TypeID<unsigned short>::get(), "unsigned short");
                _embind_register_integer(TypeID<signed int>::get(), "int");
                _embind_register_integer(TypeID<unsigned int>::get(), "unsigned int");
                _embind_register_integer(TypeID<signed long>::get(), "long");
                _embind_register_integer(TypeID<unsigned long>::get(), "unsigned long");

                _embind_register_float(TypeID<float>::get(), "float");
                _embind_register_float(TypeID<double>::get(), "double");

                _embind_register_cstring(TypeID<std::string>::get(), "std::string");
                _embind_register_emval(TypeID<val>::get(), "emscripten::val");
            }
        }

        extern "C" {
            // These three routines constitute an extension to the compiler's support for dynamic type conversion.
            // They are used by embind.js to implement automatic downcasting of return values which are pointers to
            // polymorphic objects.

            // __getDerivationPath returns an array of type_info pointers describing the derivation chain starting with
            // the derived type and proceeding toward (and ending with) the base type. Types are only included if they
            // appear on all possible derivation paths.
            std::vector<int> __getDerivationPath(int dv, const int bs) {
                std::vector<std::vector<const __cxxabiv1::__class_type_info*>> paths;

                const std::type_info* dv1 = (std::type_info*)dv;
                const std::type_info* bs1 = (std::type_info*)bs;
                const __cxxabiv1::__class_type_info* dv2 = dynamic_cast<const __cxxabiv1::__class_type_info*>(dv1);
                const __cxxabiv1::__class_type_info* bs2 = dynamic_cast<const __cxxabiv1::__class_type_info*>(bs1);

                if (dv2 && bs2) {
                    __cxxabiv1::__getDerivationPaths(dv2, bs2, std::vector<const __cxxabiv1::__class_type_info*>(), paths);
                }

                std::vector<int> derivationPath;
                if (paths.size() > 0) {
                    for (int j = 0; j < paths[0].size(); j++) {
                        bool disqualified = false;
                        for (int i = 1; i < paths.size(); i++) {
                            bool found = false;
                            for (int j2 = 0; j2 < paths[i].size(); j2++) {
                                if (paths[i][j2] == paths[0][j]) {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found) {
                                disqualified = true;
                                break;
                            }
                        }
                        if (!disqualified) {
                            derivationPath.emplace_back((int)paths[0][j]);
                        }
                    }
                }
                return derivationPath;
            }

            void* EMSCRIPTEN_KEEPALIVE __staticPointerCast(void* p, int from, int to) {
                std::vector<std::vector<const __cxxabiv1::__class_type_info*>> paths;
                int direction = 1;

                const std::type_info* from1 = (std::type_info*)from;
                const std::type_info* to1 = (std::type_info*)to;
                const __cxxabiv1::__class_type_info* from2 = dynamic_cast<const __cxxabiv1::__class_type_info*>(from1);
                const __cxxabiv1::__class_type_info* to2 = dynamic_cast<const __cxxabiv1::__class_type_info*>(to1);

                if (from2 && to2) {
                    __cxxabiv1::__getDerivationPaths(from2, to2, std::vector<const __cxxabiv1::__class_type_info*>(), paths);
                    if (paths.size() == 0) {
                        __cxxabiv1::__getDerivationPaths(to2, from2, std::vector<const __cxxabiv1::__class_type_info*>(), paths);
                        direction = -1;
                    }
                }

                int offset = -1;
                for (int i = 0; i < paths.size(); i++) {
                    if (offset < 0) {
                        offset = __cxxabiv1::__pathOffset(paths[i]);
                    } else {
                        if (offset != __cxxabiv1::__pathOffset(paths[i])) {
                            return (void *)-2;
                        }
                    }
                }
                if (offset < 0) {
                    return (void *)-1;
                }
                if (p == 0) {
                    return (void *)0;
                }
                return (void *)((int)p + offset * direction);
            }

            // __getDynamicPointerType returns (for polymorphic types only!) the type of the instance actually
            // pointed to.
            int EMSCRIPTEN_KEEPALIVE __getDynamicPointerType(int p) {
                void** vtable = *(void***)p;
                return (int)static_cast<const std::type_info*>(vtable[-1]);
            }

            // Calls to __dynamic_cast are generated by the compiler to implement dynamic_cast<>() -- its prototype is
            // not available through any header file. It is called directly here because it allows run-time
            // specification of the target pointer type (which can only be specified at compile time when using
            // dynamic_cast<>().
            void* __dynamic_cast(void*, const std::type_info*, const std::type_info*, int);

            // __dynamicPointerCast performs a C++ dynamic_cast<>() operation, but allowing run-time specification of
            // the from and to pointer types.
            int EMSCRIPTEN_KEEPALIVE __dynamicPointer