//## 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)); }
static METHOD knh__String_concat(Ctx *ctx, knh_sfp_t *sfp) { int i, ac = knh_stack_argc(ctx, sfp); knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_sfp_t *esp = KNH_LOCAL(ctx); for(i = 0; i < ac; i++) { if(IS_bString(sfp[i].o)) { knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[i].s)); } else { KNH_ASSERT(esp == ctx->esp); KNH_SETv(ctx, esp[1].o, sfp[i].o); esp[1].data = sfp[i].data; knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL); } } KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb)); }
METHOD knh__Script_eval(Ctx *ctx, knh_sfp_t *sfp) { if(IS_NOTNULL(sfp[1].s)) { knh_sfp_t *lsfp = KNH_LOCAL(ctx); knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_cwb_write(ctx, cwb, knh_String_tobytes(sfp[1].s)); knh_cwb_putc(ctx, cwb, '\n'); InputStream *in = new_BytesInputStream(ctx, cwb->ba, cwb->pos, knh_Bytes_size(cwb->ba)); KNH_LPUSH(ctx, in); DP(in)->uri = URI_EVAL; DP(in)->line = 0; knh_NameSpace_load(ctx, ctx->share->mainns, in, 1/*isEval*/,0/*isThrowable*/); knh_cwb_close(cwb); KNH_LOCALBACK(ctx, lsfp); } KNH_RETURN_void(ctx, sfp); }
static METHOD knh__String_opAdd(Ctx *ctx, knh_sfp_t *sfp) { knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_sfp_t *esp = KNH_LOCAL(ctx); if(IS_bString(sfp[0].o)) { knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[0].s)); } else { KNH_SETv(ctx, esp[1].o, sfp[0].o); esp[1].data = sfp[0].data; knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL); } if(IS_bString(sfp[1].o)) { knh_Bytes_write(ctx, cwb->ba, knh_String_tobytes(sfp[1].s)); } else { KNH_ASSERT(esp == ctx->esp); KNH_SETv(ctx, esp[1].o, sfp[1].o); esp[1].data = sfp[1].data; knh_esp1_format(ctx, METHODN__s, cwb->w, KNH_NULL); } KNH_RETURN(ctx, sfp, knh_cwb_newString(ctx, cwb)); }