/* copied from package/konoha.compiler/compiler.c */ static kMethod *load_method(CTX ctx, kclass_t cid, kbytes_t t) { kmethodn_t mn = knh_getmn(ctx, t, MN_NONAME); kMethod *mtd = knh_NameSpace_getMethodNULL(ctx, NULL, cid, mn); assert(mtd != NULL); return mtd; }
//## method @Virtual void Chardev.read(File! file, OutputStream ous); static ssize_t script_read (struct file* filp, char __user *user_buf, size_t count, loff_t *offset) { printk("%s at %d\n",__func__,__LINE__); knh_device_t *dev = filp->private_data; knh_Object_t *self = dev->self; if(*offset > 0) return 0; Ctx *ctx = knh_getCurrentContext(); knh_sfp_t *sfp = KNH_LOCAL(ctx); knh_Method_t *mtd = dev->read; if (!IS_Method(mtd) || knh_Method_isAbstract(mtd)) { knh_bytes_t fname = B("read"); knh_methodn_t mn = knh_getmn(ctx, fname, METHODN_NONAME); mtd = knh_Class_getMethod(ctx, knh_Object_cid(self), mn); KNH_SETv(ctx, dev->read, mtd); } //knh_File_t *f = (knh_File_t*)new_Object_bcid(ctx, CLASS_File, 0); //f->file = filp; if (IS_Method(mtd)) { KNH_MOV(ctx, sfp[0].o, new_ExceptionHandler(ctx)); KNH_TRY(ctx, L_CATCH, sfp, 0); { knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); // THIS IS DANGEROUS. DON'T USED by KIMIO // knh_OutputStream_t *o = new_BytesOutputStream(ctx, cwb->ba); // KNH_MOV(ctx, sfp[1].o, self); // //KNH_MOV(ctx, sfp[2].o, f); // KNH_MOV(ctx, sfp[2].o, KNH_NULL); // KNH_MOV(ctx, sfp[3].o, o); // KNH_SCALL(ctx, sfp, 0, mtd, 2); // { // size_t len = knh_cwb_size(cwb); // char *buf = knh_cwb_tochar(ctx, cwb); // if(copy_to_user(user_buf,buf,len)){ // printk(KERN_ALERT "%s: copy_to_user failed\n",__func__); // return -EFAULT; // } // knh_cwb_close(cwb); // knh_Context_clearstack(ctx); // *offset += len; // return len; // } } /* catch */ L_CATCH:; KNH_PRINT_STACKTRACE(ctx, sfp, 0); } return 0; }
static METHOD knh__String_format(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t fmt = knh_String_tobytes(sfp[0].s); knh_sfp_t *param = sfp + 1; int ac = knh_stack_argc(ctx, param); knh_bytes_t mt, expr, next; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { KNH_RETURN(ctx, sfp, sfp[0].s); } knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); int count; for(count = 0; ; count++) { if(mt.buf > fmt.buf + 1) { fmt.len = (mt.buf - fmt.buf) - 1; knh_Bytes_write(ctx, cwb->ba, fmt); } int index = count; if(expr.len > 0) { knh_int_t num; if(knh_bytes_parseint(expr, &num)) { index = (int)num; } } if(0 <= index && index < ac) { knh_sfp_t *esp = KNH_LOCAL(ctx); KNH_SETv(ctx, esp[1].o, param[index].o); esp[1].data = param[index].data; Object *m = KNH_NULL; if(knh_bytes_isOptionalMT(mt)) m = UP(new_String(ctx, mt, NULL)); mt.buf = mt.buf - 1; mt.len++; /* 's' == > '%s' */ knh_methodn_t mn = knh_getmn(ctx, mt, METHODN__empty); knh_esp1_format(ctx, mn, cwb->w, m); } else { if(knh_Context_isDebug(ctx)) { KNH_THROW_OUTOFINDEX(ctx, index, ac); } } fmt.buf = next.buf; fmt.len = next.len; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { break; } } if(fmt.len > 0) { knh_Bytes_write(ctx, cwb->ba, fmt); } KNH_RETURN(ctx, sfp, new_StringX__cwb(ctx, knh_Object_cid(sfp[0].o), cwb)); }
void init_mtd(Ctx *ctx, struct func_data *d) { while(d->cname != NULL) { //printk("func '%s.%s', func=%p\n", d->cname, d->fname, d->func); knh_class_t cid = knh_findcid(ctx, B(d->cname)); if(cid != CLASS_unknown) { //fprintf(stderr, "find cid=%d\n",cid); knh_bytes_t fname = B(d->fname); knh_methodn_t mn = knh_getmn(ctx, fname, METHODN_NONAME); knh_Method_t *mtd = knh_lookupMethod(ctx, cid, mn); if(IS_Method(mtd) && d->func != NULL) { //printk("sync func '%s(cid=%d).%s(mtd=%p)'\n", d->cname, cid, d->fname, mtd); knh_Method_syncFunc(mtd, d->func); } } d++; } }