Skip to content

Commit e6f870e

Browse files
committed
Remove Python type tables from interrogatedb
1 parent da05ef1 commit e6f870e

File tree

12 files changed

+216
-163
lines changed

12 files changed

+216
-163
lines changed

dtool/src/dtoolbase/typeHandle.cxx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ deallocate_array(void *ptr) {
153153
PANDA_FREE_ARRAY(ptr);
154154
}
155155

156+
/**
157+
* Returns the internal void pointer that is stored for interrogate's benefit.
158+
*/
159+
PyObject *TypeHandle::
160+
get_python_type() const {
161+
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, nullptr);
162+
if (rnode != nullptr) {
163+
return rnode->get_python_type();
164+
} else {
165+
return nullptr;
166+
}
167+
}
168+
156169
/**
157170
* Return the Index of the BEst fit Classs from a set
158171
*/

dtool/src/dtoolbase/typeHandle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class EXPCL_DTOOL_DTOOLBASE TypeHandle final {
138138
MAKE_SEQ_PROPERTY(child_classes, get_num_child_classes, get_child_class);
139139

140140
public:
141+
PyObject *get_python_type() const;
142+
141143
void *allocate_array(size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT);
142144
void *reallocate_array(void *ptr, size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT);
143145
void deallocate_array(void *ptr);

dtool/src/dtoolbase/typeRegistry.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ record_alternate_name(TypeHandle type, const string &name) {
207207
_lock->unlock();
208208
}
209209

210+
/**
211+
* Records the given Python type pointer in the type registry for the benefit
212+
* of interrogate.
213+
*/
214+
void TypeRegistry::
215+
record_python_type(TypeHandle type, PyObject *python_type) {
216+
_lock->lock();
217+
218+
TypeRegistryNode *rnode = look_up(type, nullptr);
219+
if (rnode != nullptr) {
220+
rnode->_python_type = python_type;
221+
}
222+
223+
_lock->unlock();
224+
}
225+
210226
/**
211227
* Looks for a previously-registered type of the given name. Returns its
212228
* TypeHandle if it exists, or TypeHandle::none() if there is no such type.

dtool/src/dtoolbase/typeRegistry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class EXPCL_DTOOL_DTOOLBASE TypeRegistry : public MemoryBase {
4545

4646
void record_derivation(TypeHandle child, TypeHandle parent);
4747
void record_alternate_name(TypeHandle type, const std::string &name);
48+
void record_python_type(TypeHandle type, PyObject *python_type);
4849

4950
TypeHandle find_type(const std::string &name) const;
5051
TypeHandle find_type_by_id(int id) const;

dtool/src/dtoolbase/typeRegistryNode.I

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
* @date 2001-08-06
1212
*/
1313

14+
/**
15+
* Returns the Python type object associated with this node.
16+
*/
17+
INLINE PyObject *TypeRegistryNode::
18+
get_python_type() const {
19+
if (_python_type != nullptr || _parent_classes.empty()) {
20+
return _python_type;
21+
} else {
22+
// Recurse through parent classes.
23+
return r_get_python_type();
24+
}
25+
}
26+
1427
/**
1528
*
1629
*/

dtool/src/dtoolbase/typeRegistryNode.cxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,29 @@ r_build_subtrees(TypeRegistryNode *top, int bit_count,
308308
}
309309
}
310310

311+
/**
312+
* Recurses through the parent nodes to find the best Python type object to
313+
* represent objects of this type.
314+
*/
315+
PyObject *TypeRegistryNode::
316+
r_get_python_type() const {
317+
Classes::const_iterator ni;
318+
for (ni = _parent_classes.begin(); ni != _parent_classes.end(); ++ni) {
319+
const TypeRegistryNode *parent = *ni;
320+
if (parent->_python_type != nullptr) {
321+
return parent->_python_type;
322+
323+
} else if (!parent->_parent_classes.empty()) {
324+
PyObject *py_type = parent->r_get_python_type();
325+
if (py_type != nullptr) {
326+
return py_type;
327+
}
328+
}
329+
}
330+
331+
return nullptr;
332+
}
333+
311334
/**
312335
* A recursive function to double-check the result of is_derived_from(). This
313336
* is the slow, examine-the-whole-graph approach, as opposed to the clever and

dtool/src/dtoolbase/typeRegistryNode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class EXPCL_DTOOL_DTOOLBASE TypeRegistryNode {
3737
static TypeHandle get_parent_towards(const TypeRegistryNode *child,
3838
const TypeRegistryNode *base);
3939

40+
INLINE PyObject *get_python_type() const;
41+
4042
void clear_subtree();
4143
void define_subtree();
4244

@@ -46,6 +48,7 @@ class EXPCL_DTOOL_DTOOLBASE TypeRegistryNode {
4648
typedef std::vector<TypeRegistryNode *> Classes;
4749
Classes _parent_classes;
4850
Classes _child_classes;
51+
PyObject *_python_type = nullptr;
4952

5053
AtomicAdjust::Integer _memory_usage[TypeHandle::MC_limit];
5154

@@ -77,6 +80,8 @@ class EXPCL_DTOOL_DTOOLBASE TypeRegistryNode {
7780
void r_build_subtrees(TypeRegistryNode *top,
7881
int bit_count, SubtreeMaskType bits);
7982

83+
PyObject *r_get_python_type() const;
84+
8085
static bool check_derived_from(const TypeRegistryNode *child,
8186
const TypeRegistryNode *base);
8287

dtool/src/interrogate/interfaceMakerPythonNative.cxx

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,54 @@ write_prototypes(ostream &out_code, ostream *out_h) {
821821
}
822822
}
823823

824+
out_code << "/**\n";
825+
out_code << " * Declarations for exported classes\n";
826+
out_code << " */\n";
827+
828+
out_code << "static const Dtool_TypeDef exports[] = {\n";
829+
830+
for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
831+
Object *object = (*oi).second;
832+
833+
if (object->_itype.is_class() || object->_itype.is_struct()) {
834+
CPPType *type = object->_itype._cpptype;
835+
836+
if (isExportThisRun(type) && is_cpp_type_legal(type)) {
837+
string class_name = type->get_local_name(&parser);
838+
string safe_name = make_safe_name(class_name);
839+
840+
out_code << " {\"" << class_name << "\", &Dtool_" << safe_name << "},\n";
841+
}
842+
}
843+
}
844+
845+
out_code << " {nullptr, nullptr},\n";
846+
out_code << "};\n\n";
847+
824848
out_code << "/**\n";
825849
out_code << " * Extern declarations for imported classes\n";
826850
out_code << " */\n";
827851

852+
// Write out a table of the externally imported types that will be filled in
853+
// upon module initialization.
854+
if (!_external_imports.empty()) {
855+
out_code << "#ifndef LINK_ALL_STATIC\n";
856+
out_code << "static Dtool_TypeDef imports[] = {\n";
857+
858+
int idx = 0;
859+
for (CPPType *type : _external_imports) {
860+
string class_name = type->get_local_name(&parser);
861+
string safe_name = make_safe_name(class_name);
862+
863+
out_code << " {\"" << class_name << "\", nullptr},\n";
864+
out_code << "#define Dtool_Ptr_" << safe_name << " (imports[" << idx << "].type)\n";
865+
++idx;
866+
}
867+
out_code << " {nullptr, nullptr},\n";
868+
out_code << "};\n";
869+
out_code << "#endif\n\n";
870+
}
871+
828872
for (CPPType *type : _external_imports) {
829873
string class_name = type->get_local_name(&parser);
830874
string safe_name = make_safe_name(class_name);
@@ -834,7 +878,9 @@ write_prototypes(ostream &out_code, ostream *out_h) {
834878
out_code << "#ifndef LINK_ALL_STATIC\n";
835879
// out_code << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" <<
836880
// safe_name << ";\n";
837-
out_code << "static struct Dtool_PyTypedObject *Dtool_Ptr_" << safe_name << ";\n";
881+
//if (has_get_class_type_function(type)) {
882+
// out_code << "static struct Dtool_PyTypedObject *Dtool_Ptr_" << safe_name << ";\n";
883+
//}
838884
// out_code << "#define Dtool_Ptr_" << safe_name << " &Dtool_" <<
839885
// safe_name << "\n"; out_code << "IMPORT_THIS void
840886
// Dtool_PyModuleClassInit_" << safe_name << "(PyObject *module);\n";
@@ -1258,36 +1304,36 @@ write_module_support(ostream &out, ostream *out_h, InterrogateModuleDef *def) {
12581304

12591305
Objects::iterator oi;
12601306

1261-
out << "void Dtool_" << def->library_name << "_RegisterTypes() {\n";
1307+
out << "void Dtool_" << def->library_name << "_RegisterTypes() {\n"
1308+
" TypeRegistry *registry = TypeRegistry::ptr();\n"
1309+
" nassertv(registry != nullptr);\n";
1310+
12621311
for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
12631312
Object *object = (*oi).second;
1264-
if (object->_itype.is_class() ||
1265-
object->_itype.is_struct()) {
1266-
if (is_cpp_type_legal(object->_itype._cpptype) &&
1267-
isExportThisRun(object->_itype._cpptype)) {
1268-
string class_name = make_safe_name(object->_itype.get_scoped_name());
1269-
bool is_typed = has_get_class_type_function(object->_itype._cpptype);
1313+
if (object->_itype.is_class() || object->_itype.is_struct()) {
1314+
CPPType *type = object->_itype._cpptype;
1315+
if (is_cpp_type_legal(type) && isExportThisRun(type)) {
1316+
string class_name = object->_itype.get_scoped_name();
1317+
string safe_name = make_safe_name(class_name);
1318+
bool is_typed = has_get_class_type_function(type);
12701319

12711320
if (is_typed) {
1272-
if (has_init_type_function(object->_itype._cpptype)) {
1321+
out << " {\n";
1322+
if (has_init_type_function(type)) {
12731323
// Call the init_type function. This isn't necessary for all
12741324
// types as many of them are automatically initialized at static
12751325
// init type, but for some extension classes it's useful.
1276-
out << " " << object->_itype._cpptype->get_local_name(&parser)
1326+
out << " " << type->get_local_name(&parser)
12771327
<< "::init_type();\n";
12781328
}
1279-
out << " Dtool_" << class_name << "._type = "
1280-
<< object->_itype._cpptype->get_local_name(&parser)
1281-
<< "::get_class_type();\n"
1282-
<< " RegisterRuntimeTypedClass(Dtool_" << class_name << ");\n";
1283-
1329+
out << " TypeHandle handle = " << type->get_local_name(&parser)
1330+
<< "::get_class_type();\n";
1331+
out << " Dtool_" << safe_name << "._type = handle;\n";
1332+
out << " registry->record_python_type(handle, "
1333+
"(PyObject *)&Dtool_" << safe_name << ");\n";
1334+
out << " }\n";
12841335
} else {
1285-
out << "#ifndef LINK_ALL_STATIC\n"
1286-
<< " RegisterNamedClass(\"" << object->_itype.get_scoped_name()
1287-
<< "\", Dtool_" << class_name << ");\n"
1288-
<< "#endif\n";
1289-
1290-
if (IsPandaTypedObject(object->_itype._cpptype->as_struct_type())) {
1336+
if (IsPandaTypedObject(type->as_struct_type())) {
12911337
nout << object->_itype.get_scoped_name() << " derives from TypedObject, "
12921338
<< "but does not define a get_class_type() function.\n";
12931339
}
@@ -1297,23 +1343,6 @@ write_module_support(ostream &out, ostream *out_h, InterrogateModuleDef *def) {
12971343
}
12981344
out << "}\n\n";
12991345

1300-
out << "void Dtool_" << def->library_name << "_ResolveExternals() {\n";
1301-
out << "#ifndef LINK_ALL_STATIC\n";
1302-
out << " // Resolve externally imported types.\n";
1303-
1304-
for (CPPType *type : _external_imports) {
1305-
string class_name = type->get_local_name(&parser);
1306-
string safe_name = make_safe_name(class_name);
1307-
1308-
if (has_get_class_type_function(type)) {
1309-
out << " Dtool_Ptr_" << safe_name << " = LookupRuntimeTypedClass(" << class_name << "::get_class_type());\n";
1310-
} else {
1311-
out << " Dtool_Ptr_" << safe_name << " = LookupNamedClass(\"" << class_name << "\");\n";
1312-
}
1313-
}
1314-
out << "#endif\n";
1315-
out << "}\n\n";
1316-
13171346
out << "void Dtool_" << def->library_name << "_BuildInstants(PyObject *module) {\n";
13181347
out << " (void) module;\n";
13191348

@@ -1466,9 +1495,14 @@ write_module_support(ostream &out, ostream *out_h, InterrogateModuleDef *def) {
14661495

14671496
out << " {nullptr, nullptr, 0, nullptr}\n" << "};\n\n";
14681497

1469-
out << "struct LibraryDef " << def->library_name << "_moddef = {python_simple_funcs};\n";
1498+
out << "extern const struct LibraryDef " << def->library_name << "_moddef = {python_simple_funcs, exports, ";
1499+
if (_external_imports.empty()) {
1500+
out << "nullptr};\n";
1501+
} else {
1502+
out << "imports};\n";
1503+
}
14701504
if (out_h != nullptr) {
1471-
*out_h << "extern struct LibraryDef " << def->library_name << "_moddef;\n";
1505+
*out_h << "extern const struct LibraryDef " << def->library_name << "_moddef;\n";
14721506
}
14731507
}
14741508

dtool/src/interrogate/interrogate_module.cxx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ int write_python_table_native(std::ostream &out) {
286286
vector_string::const_iterator ii;
287287
for (ii = libraries.begin(); ii != libraries.end(); ++ii) {
288288
printf("Referencing Library %s\n", (*ii).c_str());
289-
out << "extern LibraryDef " << *ii << "_moddef;\n";
289+
out << "extern const struct LibraryDef " << *ii << "_moddef;\n";
290290
out << "extern void Dtool_" << *ii << "_RegisterTypes();\n";
291-
out << "extern void Dtool_" << *ii << "_ResolveExternals();\n";
292291
out << "extern void Dtool_" << *ii << "_BuildInstants(PyObject *module);\n";
293292
}
294293

@@ -339,12 +338,9 @@ int write_python_table_native(std::ostream &out) {
339338
for (ii = libraries.begin(); ii != libraries.end(); ii++) {
340339
out << " Dtool_" << *ii << "_RegisterTypes();\n";
341340
}
342-
for (ii = libraries.begin(); ii != libraries.end(); ii++) {
343-
out << " Dtool_" << *ii << "_ResolveExternals();\n";
344-
}
345341
out << "\n";
346342

347-
out << " LibraryDef *defs[] = {";
343+
out << " const LibraryDef *defs[] = {";
348344
for(ii = libraries.begin(); ii != libraries.end(); ii++) {
349345
out << "&" << *ii << "_moddef, ";
350346
}
@@ -386,12 +382,9 @@ int write_python_table_native(std::ostream &out) {
386382
for (ii = libraries.begin(); ii != libraries.end(); ii++) {
387383
out << " Dtool_" << *ii << "_RegisterTypes();\n";
388384
}
389-
for (ii = libraries.begin(); ii != libraries.end(); ii++) {
390-
out << " Dtool_" << *ii << "_ResolveExternals();\n";
391-
}
392385
out << "\n";
393386

394-
out << " LibraryDef *defs[] = {";
387+
out << " const LibraryDef *defs[] = {";
395388
for(ii = libraries.begin(); ii != libraries.end(); ii++) {
396389
out << "&" << *ii << "_moddef, ";
397390
}

dtool/src/interrogatedb/py_panda.I

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
template<class T> INLINE bool
2727
DtoolInstance_GetPointer(PyObject *self, T *&into) {
2828
if (DtoolInstance_Check(self)) {
29-
Dtool_PyTypedObject *target_class = Dtool_RuntimeTypeDtoolType(get_type_handle(T).get_index());
29+
Dtool_PyTypedObject *target_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
3030
if (target_class != nullptr) {
3131
if (_IS_FINAL(T)) {
3232
if (DtoolInstance_TYPE(self) == target_class) {
@@ -116,28 +116,28 @@ INLINE long Dtool_EnumValue_AsLong(PyObject *value) {
116116
*/
117117
template<class T> INLINE PyObject *
118118
DTool_CreatePyInstance(const T *obj, bool memory_rules) {
119-
Dtool_PyTypedObject *known_class = Dtool_RuntimeTypeDtoolType(get_type_handle(T).get_index());
119+
Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
120120
nassertr(known_class != nullptr, nullptr);
121121
return DTool_CreatePyInstance((void*) obj, *known_class, memory_rules, true);
122122
}
123123

124124
template<class T> INLINE PyObject *
125125
DTool_CreatePyInstance(T *obj, bool memory_rules) {
126-
Dtool_PyTypedObject *known_class = Dtool_RuntimeTypeDtoolType(get_type_handle(T).get_index());
126+
Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
127127
nassertr(known_class != nullptr, nullptr);
128128
return DTool_CreatePyInstance((void*) obj, *known_class, memory_rules, false);
129129
}
130130

131131
template<class T> INLINE PyObject *
132132
DTool_CreatePyInstanceTyped(const T *obj, bool memory_rules) {
133-
Dtool_PyTypedObject *known_class = Dtool_RuntimeTypeDtoolType(get_type_handle(T).get_index());
133+
Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
134134
nassertr(known_class != nullptr, nullptr);
135135
return DTool_CreatePyInstanceTyped((void*) obj, *known_class, memory_rules, true, obj->get_type().get_index());
136136
}
137137

138138
template<class T> INLINE PyObject *
139139
DTool_CreatePyInstanceTyped(T *obj, bool memory_rules) {
140-
Dtool_PyTypedObject *known_class = Dtool_RuntimeTypeDtoolType(get_type_handle(T).get_index());
140+
Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
141141
nassertr(known_class != nullptr, nullptr);
142142
return DTool_CreatePyInstanceTyped((void*) obj, *known_class, memory_rules, false, obj->get_type().get_index());
143143
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy