tried to fix bugs (partial success)
This commit is contained in:
@@ -38,20 +38,28 @@ MonoMethod* tryGetMonoMethod(MonoClass* target_class, const std::string& name,
|
||||
{
|
||||
if(target_class == nullptr)
|
||||
throw UsefulException("target_class is nullptr");
|
||||
|
||||
// iterate each method
|
||||
void* iter = nullptr;
|
||||
MonoMethod* m = nullptr;
|
||||
MonoType* return_type = nullptr;
|
||||
std::vector<MonoType*> argument_types;
|
||||
while( (m = mono_class_get_methods(target_class, &iter)) ){
|
||||
// compare name
|
||||
std::string m_name = mono_method_get_name(m);
|
||||
if(m_name != name)
|
||||
continue;
|
||||
|
||||
argument_types.clear();
|
||||
MonoType* return_type = nullptr;
|
||||
getMethodSignatureTypes(m, &return_type, argument_types);
|
||||
// compare argument count
|
||||
if(argument_types.size() != arg_classes_size)
|
||||
continue;
|
||||
|
||||
// compare return type
|
||||
if(!mono_metadata_type_equal(return_type, mono_class_get_type(return_class)))
|
||||
continue;
|
||||
|
||||
// compare argument types
|
||||
bool argument_types_mismatch = false;
|
||||
for(size_t i = 0; i < arg_classes_size; i++){
|
||||
@@ -62,6 +70,7 @@ MonoMethod* tryGetMonoMethod(MonoClass* target_class, const std::string& name,
|
||||
}
|
||||
if(argument_types_mismatch)
|
||||
continue;
|
||||
|
||||
// passed all tests successfully
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -85,8 +85,11 @@ public:
|
||||
void* arg_array[] = { valueToVoidPtr(args)..., nullptr };
|
||||
MonoObject* ex = nullptr;
|
||||
mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex);
|
||||
if(ex)
|
||||
if(ex){
|
||||
//TODO: call mono_trace_set_printerr_handler from mono/mono/utils/mono-logger.h
|
||||
mono_print_unhandled_exception(ex);
|
||||
throw UsefulException("Some C# exception occured");
|
||||
}
|
||||
};
|
||||
|
||||
/// all types must implement getClass<T>()
|
||||
@@ -94,9 +97,10 @@ public:
|
||||
static MonoClass* arg_classes[] { getClass<ArgTypes>()... };
|
||||
static MonoClass* return_class { getClass<ReturnT>() };
|
||||
method_ptr = tryGetMonoMethod(target_class, name, return_class, arg_classes, sizeof...(ArgTypes));
|
||||
if(method_ptr == nullptr)
|
||||
if(method_ptr == nullptr){
|
||||
throw UsefulException(format("can't get method '%s' from class '%s'",
|
||||
name.c_str(), mono_class_get_name(target_class)));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -15,7 +15,8 @@ RuntimeJIT::RuntimeJIT(const std::string& domain_name){
|
||||
}
|
||||
|
||||
RuntimeJIT::~RuntimeJIT(){
|
||||
mono_jit_cleanup(domain);
|
||||
// TODO: fix segfault on cleanup
|
||||
// mono_jit_cleanup(domain);
|
||||
}
|
||||
|
||||
std::shared_ptr<Assembly> RuntimeJIT::loadAssembly(const std::string &name){
|
||||
|
||||
19
src/main.cpp
19
src/main.cpp
@@ -46,22 +46,23 @@ int main(int argc, const char** argv){
|
||||
MonoObject* exampleObjectManaged = mono_object_new(mono.getDomain(), gameObjectClass);
|
||||
u64 obj_id = 0;
|
||||
auto pair = p.emplace(GameObject(exampleObjectManaged, nullptr));
|
||||
mono_runtime_object_init(exampleObjectManaged);
|
||||
auto exampleObjectInit = Mono::Method<void(u64, u32)>(gameObjectClass, "Init");
|
||||
exampleObjectInit(exampleObjectManaged, obj_id++, pair.first);
|
||||
auto exampleObjectUpdate = Mono::Method<void(f64)>(gameObjectClass, "UpdateComponents");
|
||||
|
||||
auto gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||
gameObjectCtor(exampleObjectManaged, obj_id++, pair.first);
|
||||
|
||||
auto exampleObjectUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
||||
updateCallbacks.push_back([exampleObjectManaged, exampleObjectUpdate](f64 deltaTime) -> void {
|
||||
exampleObjectUpdate(exampleObjectManaged, deltaTime);
|
||||
});
|
||||
|
||||
auto tryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent");
|
||||
Mono::String componentNameManaged = mono_string_new(mono.getDomain(), "ExampleComponent");
|
||||
Mono::ObjectHandle componentNameObjectHandle((MonoObject*)(void*)componentNameManaged);
|
||||
auto tryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
||||
Mono::String componentNameManaged = mono_string_new(mono.getDomain(), "Ougge.ExampleComponent");
|
||||
// Mono::ObjectHandle componentNameObjectHandle((MonoObject*)(void*)componentNameManaged);
|
||||
Mono::Bool created = tryCreateComponent(exampleObjectManaged, componentNameManaged);
|
||||
if(!created.wide_bool)
|
||||
throw UsefulException("couldn't create ExampleComponent");
|
||||
|
||||
|
||||
std::cout<<"OK!"<<std::endl;
|
||||
return 0;
|
||||
GUI::MainWindow w;
|
||||
w.open("ougge", update);
|
||||
std::cout<<"created sdl window"<<std::endl;
|
||||
|
||||
Reference in New Issue
Block a user