Object module_cuda_init() { if (cuda_module != NULL) return cuda_module; CudaError = alloc_Exception("CudaError", ErrorObject); gc_root(CudaError); ClassData c = alloc_class("Module<cuda>", 13); add_Method(c, "over()map", &cuda_over_map); add_Method(c, "floatArray", &cuda_floatArray); add_Method(c, "deviceName", &cuda_deviceName); add_Method(c, "computeCapability", &cuda_computeCapability); add_Method(c, "cores", &cuda_cores); add_Method(c, "using()do()blockWidth()blockHeight()gridWidth()gridHeight", &cuda_using_do_blockWidth_blockHeight_gridWidth_gridHeight); add_Method(c, "using()do", &cuda_using_do); add_Method(c, "using()times()do", &cuda_using_times_do); add_Method(c, "do", &cuda_do_infer); add_Method(c, "do()blockWidth()blockHeight()gridWidth()gridHeight", &cuda_do_dimensions_infer); add_Method(c, "bindir", &cuda_bindir); add_Method(c, "includedir", &cuda_includedir); Object o = alloc_newobj(0, c); cuda_module = o; gc_root(o); return o; }
// Add a wrapper for the named method that actually points to the trampoline // function so that the function will call back to the interpreter to actually // execute the method. Object repl_addmethod(Object self, int nparts, int *argcv, Object *args, int flags) { Object o = args[0]; Object objname = args[1]; gc_root(objname); Object methname = args[2]; gc_root(methname); int pos = integerfromAny(args[3]); Object(*func)(Object, int, int*, Object*, int); func = (Object(*)(Object, int, int*, Object*, int))trampoline; Method *m = add_Method(o->class, cstringfromString(methname), func); m->flags &= ~MFLAG_REALSELFONLY; m->flags |= MFLAG_REALSELFALSO; m->pos = pos; Object callinfo = alloc_List(); gc_root(callinfo); // ugly workaround gc_pause(); int largcv[] = {1}; callmethod(callinfo, "push", 1, largcv, &objname); callmethod(callinfo, "push", 1, largcv, &methname); gc_unpause(); adddatum2(o, callinfo, pos); return alloc_none(); }
// Create and return a grace object which contains the above functions Object module_math_init() { if (math_module != NULL) return math_module; srand(time(NULL)); ClassData c = alloc_class("Module<math>", 13); add_Method(c, "asString", &math_asString); add_Method(c, "asDebugString", &math_asDebugString); add_Method(c, "sin", &math_sin); add_Method(c, "cos", &math_cos); add_Method(c, "tan", &math_tan); add_Method(c, "asin", &math_asin); add_Method(c, "acos", &math_acos); add_Method(c, "atan", &math_atan); add_Method(c, "random", &math_random); add_Method(c, "pi", &math_pi); add_Method(c, "π", &math_pi); add_Method(c, "sqrt", &math_sqrt); add_Method(c, "abs", &math_abs); Object o = alloc_newobj(0, c); math_module = o; gc_root(math_module); return o; }
Object module_repl_init() { if (repl_module != NULL) return repl_module; ClassData c = alloc_class("Module<repl>", 12); add_Method(c, "asString", &Object_asString); add_Method(c, "registerVisitor", &repl_registerVisitor); add_Method(c, "createobject", &repl_createobject); add_Method(c, "addmethod", &repl_addmethod); Object o = alloc_newobj(0, c); repl_module = o; gc_root(repl_module); if (module_ast == NULL) module_ast = module_ast_init(); if (module_interactive == NULL) module_interactive = module_interactive_init(); return o; }
// This function will receive the name and target object of a method, // create the appropriate interactive objects for the interpreter and then // call back to the interpreter with them. This way the actual method gets // executed in the interpreter and we don't have to create functions at // runtime. Object trampoline(Object self, Object realself, int nparts, int *argcv, Object *args, int flags) { int pos = (flags >> 16) & 255; Object callinfo = getdatum(self, pos, 0); int largcv[] = {1}; Object idx1 = alloc_Integer32(1); Object idx2 = alloc_Integer32(2); Object objname = callmethod(callinfo, "at", 1, largcv, &idx1); Object methname = callmethod(callinfo, "at", 1, largcv, &idx2); Object replobjclass = callmethod(module_interactive, "replObj", 0, NULL, NULL); Object kind = alloc_String("value"); Object roargs[] = {kind, self}; int roargcv[] = {2}; Object selfreplobj = callmethod(replobjclass, "new", 1, roargcv, roargs); gc_root(selfreplobj); Object idclass = callmethod(module_ast, "identifierNode", 0, NULL, NULL); Object idargs[] = {objname, alloc_Boolean(0)}; int idargcv[] = {2}; Object selfid = callmethod(idclass, "new", 1, idargcv, idargs); gc_root(selfid); Object memberclass = callmethod(module_ast, "memberNode", 0, NULL, NULL); Object memberargs[] = {methname, selfid}; int memberargcv[] = {2}; Object methmember = callmethod(memberclass, "new", 1, memberargcv, memberargs); gc_root(methmember); Object withclass = callmethod(module_ast, "callWithPart", 0, NULL, NULL); Object with = alloc_List(); int i, j; int k = 0; int pushargcv[] = {1}; int withargcv[] = {2}; for (i = 0; i < nparts; i++) { Object curpart = alloc_List(); for (j = 0; j < argcv[i]; j++) { Object curarg = args[k]; callmethod(curpart, "push", 1, pushargcv, &curarg); k++; } Object withargs[] = {alloc_String(""), curpart}; Object withpart = callmethod(withclass, "new", 1, withargcv, withargs); gc_root(withpart); callmethod(with, "push", 1, pushargcv, &withpart); } gc_root(with); Object callclass = callmethod(module_ast, "callNode", 0, NULL, NULL); Object nodeargs[] = {methmember, with}; int nodeargcv[] = {2}; Object callnode = callmethod(callclass, "new", 1, nodeargcv, nodeargs); gc_root(callnode); int acceptargcv[] = {1}; callmethod(callnode, "accept", 1, acceptargcv, &visitor); Object replvar = callmethod(visitor, "_result", 0, NULL, NULL); return callmethod(replvar, "val", 0, NULL, NULL); }