Update of /cvsroot/pure-data/externals/clr In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29926/clr
Modified Files: clr.cpp Log Message: restructured project (MSVC can't cope well with folder/classname mismatch) search for CLR DLLs in PD path first steps to efficient method handling
Index: clr.cpp =================================================================== RCS file: /cvsroot/pure-data/externals/clr/clr.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** clr.cpp 6 Mar 2006 01:10:36 -0000 1.6 --- clr.cpp 7 Mar 2006 13:18:29 -0000 1.7 *************** *** 28,35 **** #include <map>
static MonoDomain *monodomain; static MonoClass *clr_symbol,*clr_pointer,*clr_atom,*clr_atomlist,*clr_external; ! static MonoMethodDesc *clr_desc_main,*clr_desc_ctor; static MonoMethodDesc *clr_desc_bang,*clr_desc_float,*clr_desc_symbol,*clr_desc_pointer,*clr_desc_list,*clr_desc_anything; static MonoMethod *ext_method_bang,*ext_method_float,*ext_method_symbol,*ext_method_list,*ext_method_pointer,*ext_method_anything; --- 28,37 ---- #include <map>
+ #define CORELIB "PureData" + #define DLLEXT "dll"
static MonoDomain *monodomain; static MonoClass *clr_symbol,*clr_pointer,*clr_atom,*clr_atomlist,*clr_external; ! static MonoMethodDesc *clr_desc_tostring,*clr_desc_ctor; static MonoMethodDesc *clr_desc_bang,*clr_desc_float,*clr_desc_symbol,*clr_desc_pointer,*clr_desc_list,*clr_desc_anything; static MonoMethod *ext_method_bang,*ext_method_float,*ext_method_symbol,*ext_method_list,*ext_method_pointer,*ext_method_anything; *************** *** 46,49 **** --- 48,52 ---- MonoClassField *mono_obj_field; MonoMethod *mono_ctor; + t_symbol *name; // ClrMethodMap *methods; } t_clr_class; *************** *** 66,69 **** --- 69,87 ----
+ // Print error message given by exception + static void error_exc(char *txt,char *cname,MonoObject *exc) + { + MonoMethod *m = mono_method_desc_search_in_class(clr_desc_tostring,mono_get_exception_class()); + assert(m); + m = mono_object_get_virtual_method(exc,m); + assert(m); + MonoString *str = (MonoString *)mono_runtime_invoke(m,exc,NULL,NULL); + assert(str); + error("CLR class %s: %s",txt,cname); + error(mono_string_to_utf8(str)); + } + + + static void clr_method_bang(t_clr *x) { *************** *** 73,79 **** MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,NULL,&exc); ! if(exc) { ! error("Exception raised"); ! } }
--- 91,95 ---- MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,NULL,&exc); ! if(exc) error_exc("Exception raised",x->clr_clss->name->s_name,exc); }
*************** *** 86,92 **** MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) { ! error("Exception raised"); ! } }
--- 102,106 ---- MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) error_exc("Exception raised",x->clr_clss->name->s_name,exc); }
*************** *** 105,111 **** MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) { ! error("Exception raised"); ! } }
--- 119,140 ---- MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) error_exc("Exception raised",x->clr_clss->name->s_name,exc); ! } ! ! static void clr_method_pointer(t_clr *x,t_gpointer *p) ! { ! assert(x && x->clr_clss); ! MonoMethod *m = mono_object_get_virtual_method(x->mono_obj,ext_method_pointer); ! assert(m); ! #if 0 ! MonoObject *ptrobj = mono_value_box(monodomain,clr_pointer,&p); ! MonoObject *o = (MonoObject *)mono_object_unbox(ptrobj); ! gpointer args = o; ! #else ! gpointer args = &p; ! #endif ! MonoObject *exc; ! mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) error_exc("Exception raised",x->clr_clss->name->s_name,exc); }
*************** *** 152,177 **** MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) { ! error("Exception raised"); ! } ! } ! ! static void clr_method_pointer(t_clr *x,t_gpointer *p) ! { ! assert(x && x->clr_clss); ! MonoMethod *m = mono_object_get_virtual_method(x->mono_obj,ext_method_pointer); ! assert(m); ! #if 0 ! MonoObject *ptrobj = mono_value_box(monodomain,clr_pointer,&p); ! MonoObject *o = (MonoObject *)mono_object_unbox(ptrobj); ! gpointer args = o; ! #else ! gpointer args = &p; ! #endif ! MonoObject *exc; ! mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) { ! error("Exception raised"); ! } }
--- 181,185 ---- MonoObject *exc; mono_runtime_invoke(m,x->mono_obj,&args,&exc); ! if(exc) error_exc("Exception raised",x->clr_clss->name->s_name,exc); }
*************** *** 198,202 **** }
- // this function is called by mono when it wants post messages to pd static void PD_Post(MonoString *str) --- 206,209 ---- *************** *** 236,239 **** --- 243,275 ---- }
+ static void PD_AddBang(MonoObject *method) + { + post("Add bang method"); + } + + static void PD_AddFloat(MonoObject *method) + { + post("Add float method"); + } + + static void PD_AddSymbol(MonoObject *method) + { + post("Add symbol method"); + } + + static void PD_AddPointer(MonoObject *method) + { + post("Add pointer method"); + } + + static void PD_AddList(MonoObject *method) + { + post("Add list method"); + } + + static void PD_AddAnything(MonoObject *method) + { + post("Add anything method"); + }
#if 0 *************** *** 317,320 **** --- 353,357 ---- // store class pointer x->clr_clss = clss; + x->clr_clss->name = classname;
// store our object pointer in External::ptr member *************** *** 330,345 **** gpointer args = lstobj; #endif MonoObject *exc; ! ! // call static constructor ! MonoObject *ret = mono_runtime_invoke(clss->mono_ctor,x->mono_obj,&args,&exc); ! if(ret) { ! post("Warning: returned value from %s::.ctor ignored",classname->s_name); ! // ??? do we have to mark ret as free? ! }
if(exc) { pd_free((t_pd *)x); ! error("CLR class %s - exception raised in constructor",classname->s_name); return NULL; } --- 367,377 ---- gpointer args = lstobj; #endif + // call constructor MonoObject *exc; ! mono_runtime_invoke(clss->mono_ctor,x->mono_obj,&args,&exc);
if(exc) { pd_free((t_pd *)x); ! error_exc("exception raised in constructor",classname->s_name,exc); return NULL; } *************** *** 366,370 **** // search for classname.dll in the PD path int fd; ! if ((fd = open_via_path(dirname, classname, ".dll", dirbuf, &nameptr, MAXPDSTRING, 1)) < 0) // not found goto bailout; --- 398,402 ---- // search for classname.dll in the PD path int fd; ! if ((fd = open_via_path(dirname, classname, "." DLLEXT, dirbuf, &nameptr, MAXPDSTRING, 1)) < 0) // not found goto bailout; *************** *** 378,391 ****
// try to load assembly ! char path[MAXPDSTRING]; ! strcpy(path,dirname); ! strcat(path,"/"); ! // strcat(path,dirbuf); ! // strcat(path,"/"); ! strcat(path,nameptr);
! assembly = mono_domain_assembly_open(monodomain,path); if(!assembly) { ! error("clr: file %s couldn't be loaded!",path); goto bailout; } --- 410,420 ----
// try to load assembly ! strcat(dirbuf,"/"); ! strcat(dirbuf,classname); ! strcat(dirbuf,"." DLLEXT);
! assembly = mono_domain_assembly_open(monodomain,dirbuf); if(!assembly) { ! error("clr: file %s couldn't be loaded!",dirbuf); goto bailout; } *************** *** 415,425 **** // find static Main method
method = mono_method_desc_search_in_class(clr_desc_main,clr_class->mono_class); if(method) { // set current class clr_setup_class = clr_class;
// call static Main method ! MonoObject *ret = mono_runtime_invoke(method,NULL,NULL,NULL);
// unset current class --- 444,469 ---- // find static Main method
+ MonoMethodDesc *clr_desc_main = mono_method_desc_new(":Setup",FALSE); + assert(clr_desc_main); + method = mono_method_desc_search_in_class(clr_desc_main,clr_class->mono_class); if(method) { + MonoObject *obj = mono_object_new(monodomain,clr_class->mono_class); + if(!obj) { + error("CLR class %s could not be instantiated",classname); + goto bailout; + } + + // store NULL in External::ptr member + void *x = NULL; + mono_field_set_value(obj,clr_class->mono_obj_field,&x); + // set current class clr_setup_class = clr_class;
// call static Main method ! gpointer args = obj; ! MonoObject *exc; ! MonoObject *ret = mono_runtime_invoke(method,NULL,&args,&exc);
// unset current class *************** *** 427,436 ****
if(ret) { ! post("CLR - Warning: returned value from %s.Main ignored",classname); // ??? do we have to mark ret as free? } } else ! post("CLR - Warning: no %s.Main method found",classname);
// find and save constructor --- 471,487 ----
if(ret) { ! post("CLR - Warning: returned value from %s.Setup ignored",classname); // ??? do we have to mark ret as free? } + + if(exc) { + MonoObject *ret = mono_runtime_invoke(method,NULL,&args,&exc); + + error_exc("CLR - Exception raised by Setup",classname,exc); + goto bailout; + } } else ! post("CLR - Warning: no %s.Setup method found",classname);
// find and save constructor *************** *** 503,507 ****
try { ! monodomain = mono_jit_init("PureData"); } catch(...) { --- 554,558 ----
try { ! monodomain = mono_jit_init(CORELIB); } catch(...) { *************** *** 510,526 ****
if(monodomain) { ! // add mono to C hooks ! mono_add_internal_call("PureData.Core::Post",(const void *)PD_Post); ! mono_add_internal_call("PureData.Core::PostError",(const void *)PD_PostError); ! mono_add_internal_call("PureData.Core::PostBug",(const void *)PD_PostBug); ! mono_add_internal_call("PureData.Core::PostVerbose",(const void *)PD_PostVerbose); ! ! mono_add_internal_call("PureData.Core::SymGen", (const void *)PD_SymGen); ! mono_add_internal_call("PureData.Core::SymEval", (const void *)PD_SymEval); !
! MonoAssembly *assembly = mono_domain_assembly_open (monodomain, "PureData.dll"); if(!assembly) { ! error("clr: assembly PureData.dll not found!"); return; } --- 561,579 ----
if(monodomain) { ! // try to find PureData.dll in the PD path ! char dirbuf[MAXPDSTRING],*nameptr; ! // search for classname.dll in the PD path ! int fd; ! if ((fd = open_via_path("",CORELIB,"." DLLEXT,dirbuf,&nameptr,MAXPDSTRING,1)) >= 0) { ! strcat(dirbuf,"/" CORELIB "." DLLEXT); ! close(fd); ! } ! else ! strcpy(dirbuf,CORELIB "." DLLEXT);
! // look for PureData.dll ! MonoAssembly *assembly = mono_domain_assembly_open (monodomain,dirbuf); if(!assembly) { ! error("clr: assembly " CORELIB "." DLLEXT " not found!"); return; } *************** *** 529,532 **** --- 582,602 ---- assert(image);
+ // add mono to C hooks + + mono_add_internal_call("PureData.Internal::SymGen(string)", (const void *)PD_SymGen); + mono_add_internal_call("PureData.Internal::SymEval(void*)", (const void *)PD_SymEval); + + mono_add_internal_call("PureData.External::Post(string)",(const void *)PD_Post); + mono_add_internal_call("PureData.External::PostError(string)",(const void *)PD_PostError); + mono_add_internal_call("PureData.External::PostBug(string)",(const void *)PD_PostBug); + mono_add_internal_call("PureData.External::PostVerbose(string)",(const void *)PD_PostVerbose); + + mono_add_internal_call("PureData.External::Add(PureData.External/MethodBang)", (const void *)PD_AddBang); + mono_add_internal_call("PureData.External::Add(PureData.External/MethodFloat)", (const void *)PD_AddFloat); + mono_add_internal_call("PureData.External::Add(PureData.External/MethodSymbol)", (const void *)PD_AddSymbol); + mono_add_internal_call("PureData.External::Add(PureData.External/MethodPointer)", (const void *)PD_AddPointer); + mono_add_internal_call("PureData.External::Add(PureData.External/MethodList)", (const void *)PD_AddList); + mono_add_internal_call("PureData.External::Add(PureData.External/MethodAnything)", (const void *)PD_AddAnything); + // load important classes clr_symbol = mono_class_from_name(image,"PureData","Symbol"); *************** *** 541,546 **** assert(clr_external);
! clr_desc_main = mono_method_desc_new("::Main()",FALSE); ! assert(clr_desc_main); clr_desc_ctor = mono_method_desc_new("::.ctor(AtomList)",FALSE); assert(clr_desc_ctor); --- 611,616 ---- assert(clr_external);
! clr_desc_tostring = mono_method_desc_new("::ToString()",FALSE); ! assert(clr_desc_tostring); clr_desc_ctor = mono_method_desc_new("::.ctor(AtomList)",FALSE); assert(clr_desc_ctor); *************** *** 571,577 ****
// ready! ! post("CLR - (c)2006 Davide Morelli, Thomas Grill"); } else ! error("clr: mono domain couldn't be initialized!"); } --- 641,647 ----
// ready! ! post("CLR extension - (c)2006 Davide Morelli, Thomas Grill"); } else ! error("clr: mono domain couldn't be initialized!"); }