@@ -55,6 +55,14 @@ int_t Module::store_pointer(void *ptr)
55
55
return m_pointer_array.size ();
56
56
}
57
57
58
+ void Module::bind_constants (jl_module_t * mod)
59
+ {
60
+ for (auto & dt_pair : m_jl_constants)
61
+ {
62
+ jl_set_const (mod, jl_symbol (dt_pair.first .c_str ()), dt_pair.second );
63
+ }
64
+ }
65
+
58
66
Module &ModuleRegistry::create_module (jl_module_t * jmod)
59
67
{
60
68
if (jmod == nullptr )
@@ -91,8 +99,43 @@ JLCXX_API ModuleRegistry& registry()
91
99
92
100
JLCXX_API jl_value_t * julia_type (const std::string& name, const std::string& module_name)
93
101
{
94
- const auto mods = {module_name.empty () ? nullptr : (jl_module_t *)jl_get_global (jl_current_module, jl_symbol (module_name.c_str ())), jl_base_module, g_cxxwrap_module, jl_current_module, jl_current_module->parent };
95
- std::string found_type;
102
+ std::vector<jl_module_t *> mods;
103
+ mods.reserve (6 );
104
+ jl_module_t * current_mod = registry ().has_current_module () ? registry ().current_module ().julia_module () : nullptr ;
105
+ if (!module_name.empty ())
106
+ {
107
+ jl_sym_t * modsym = jl_symbol (module_name.c_str ());
108
+ jl_module_t * found_mod = nullptr ;
109
+ if (current_mod != nullptr )
110
+ {
111
+ found_mod = (jl_module_t *)jl_get_global (current_mod, modsym);
112
+ }
113
+ if (found_mod == nullptr )
114
+ {
115
+ found_mod = (jl_module_t *)jl_get_global (jl_main_module, jl_symbol (module_name.c_str ()));
116
+ }
117
+ if (found_mod != nullptr )
118
+ {
119
+ mods.push_back (found_mod);
120
+ }
121
+ else
122
+ {
123
+ throw std::runtime_error (" Failed to find module " + module_name);
124
+ }
125
+ }
126
+ else
127
+ {
128
+ if (current_mod != nullptr )
129
+ {
130
+ mods.push_back (current_mod);
131
+ }
132
+ mods.push_back (jl_main_module);
133
+ mods.push_back (jl_base_module);
134
+ mods.push_back (g_cxxwrap_module);
135
+ mods.push_back (jl_top_module);
136
+ }
137
+
138
+ std::string found_type = " null" ;
96
139
for (jl_module_t * mod : mods)
97
140
{
98
141
if (mod == nullptr )
@@ -170,6 +213,24 @@ std::wstring ConvertToCpp<std::wstring, false, false, false>::operator()(jl_valu
170
213
return std::wstring (arr.data (), arr.size ());
171
214
}
172
215
216
+ static constexpr const char * dt_prefix = " __cxxwrap_dt_" ;
217
+
218
+ jl_datatype_t * existing_datatype (jl_module_t * mod, jl_sym_t * name)
219
+ {
220
+ const std::string prefixed_name = dt_prefix + symbol_name (name);
221
+ jl_value_t * found_dt = jl_get_global (mod, jl_symbol (prefixed_name.c_str ()));
222
+ if (found_dt == nullptr || !jl_is_datatype (found_dt))
223
+ {
224
+ return nullptr ;
225
+ }
226
+ return (jl_datatype_t *)found_dt;
227
+ }
228
+
229
+ void set_internal_constant (jl_module_t * mod, jl_datatype_t * dt, const std::string& prefixed_name)
230
+ {
231
+ jl_set_const (mod, jl_symbol (prefixed_name.c_str ()), (jl_value_t *)dt);
232
+ }
233
+
173
234
JLCXX_API jl_datatype_t * new_datatype (jl_sym_t *name,
174
235
jl_module_t * module ,
175
236
jl_datatype_t *super,
@@ -182,11 +243,32 @@ JLCXX_API jl_datatype_t* new_datatype(jl_sym_t *name,
182
243
{
183
244
throw std::runtime_error (" null module when creating type" );
184
245
}
185
- #if JULIA_VERSION_MAJOR == 0 && JULIA_VERSION_MINOR < 7
186
- return jl_new_datatype (name, super, parameters, fnames, ftypes, abstract, mutabl, ninitialized);
187
- #else
188
- return jl_new_datatype (name, module , super, parameters, fnames, ftypes, abstract, mutabl, ninitialized);
189
- #endif
246
+ jl_datatype_t * dt = existing_datatype (module , name);
247
+ if (dt != nullptr )
248
+ {
249
+ return dt;
250
+ }
251
+
252
+ dt = jl_new_datatype (name, module , super, parameters, fnames, ftypes, abstract, mutabl, ninitialized);
253
+ set_internal_constant (module , dt, dt_prefix + symbol_name (name));
254
+ return dt;
255
+ }
256
+
257
+ JLCXX_API jl_datatype_t * new_bitstype (jl_sym_t *name,
258
+ jl_module_t * module ,
259
+ jl_datatype_t *super,
260
+ jl_svec_t *parameters, const size_t nbits)
261
+ {
262
+ assert (module != nullptr );
263
+ jl_datatype_t * dt = existing_datatype (module , name);
264
+ if (dt != nullptr )
265
+ {
266
+ return dt;
267
+ }
268
+
269
+ dt = jl_new_primitivetype ((jl_value_t *)name, module , super, parameters, nbits);
270
+ set_internal_constant (module , dt, dt_prefix + symbol_name (name));
271
+ return dt;
190
272
}
191
273
192
274
}
0 commit comments