//## FILE FILE.new(String path, String mode); static KMETHOD File_new(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = I18NAPI formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace); const char *mode = kString_text(sfp[2].asString); FILE *fp = fopen(systemPath, mode); kFile *file = (kFile *) sfp[0].asObject; if(fp == NULL) { int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "fopen", LogText("filename", kString_text(path)), LogText("mode", mode), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), fault, NULL, sfp); } if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') { KTraceChangeSystemPoint(trace, "fopen", LogFileName(kString_text(path)), LogText("mode", mode)); } file->fp = fp; KFieldInit(file, file->PathInfoNULL, path); if(!I18NAPI isSystemCharsetUTF8(kctx)) { if(mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') { file->writerIconv = I18NAPI iconvUTF8ToSystemCharset(kctx, trace); } else { file->readerIconv = I18NAPI iconvSystemCharsetToUTF8(kctx, trace); } } KReturn(file); }
static kbool_t ParseJson(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *text, size_t length, KTraceInfo *trace) { JSON json = parseJSON((JSONMemoryPool *)(JSONAPI JsonHandler), text, text + length); if(IsError(json.val)) { KLIB KRuntime_raise(kctx, KException_("InvalidJsonText"), SoftwareFault, NULL, trace->baseStack); } jsonbuf->json_i = json.bits; return jsonbuf->json_i != 0; }
static int TRACE_fgetc(KonohaContext *kctx, kFile *file, KTraceInfo *trace) { int ch = fgetc(file->fp); if(ferror(file->fp) != 0) { KTraceErrorPoint(trace, SystemFault, "fgetc", LogFile(file), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack); } return ch; }
static size_t TRACE_fread(KonohaContext *kctx, kFile *file, char *buf, size_t bufsiz, KTraceInfo *trace) { size_t size = fread(buf, 1, bufsiz, file->fp); if(ferror(file->fp) != 0){ KTraceErrorPoint(trace, SystemFault, "fread", LogFile(file), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack); } kFile_CheckEOF(kctx, file, trace); return size; }
static int TRACE_fputc(KonohaContext *kctx, kFile *file, int ch, KTraceInfo *trace) { if(fputc(ch, file->fp) == EOF) { KTraceErrorPoint(trace, SystemFault, "fputc", LogFile(file), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack); } else if(!kFile_is(ChangeLessStream, file)) { KTraceChangeSystemPoint(trace, "fputc", LogFile(file), LogWrittenByte(1)); } return ch; }
static size_t TRACE_fwrite(KonohaContext *kctx, kFile *file, const char *buf, size_t bufsiz, KTraceInfo *trace) { size_t size = fwrite(buf, 1, bufsiz, file->fp); if(ferror(file->fp) != 0){ KTraceErrorPoint(trace, SystemFault, "fwrite", LogFile(file), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), SystemFault, NULL, trace->baseStack); } if(size > 0 && !kFile_is(ChangeLessStream, file)) { KTraceChangeSystemPoint(trace, "fwrite", LogFile(file), LogWrittenByte(size)); } return size; }
static void Kthrow(KonohaContext *kctx, KonohaStack *sfp, kException *e) { if(IS_Exception(e)) { KonohaStack *p = (sfp == NULL) ? kctx->esp : sfp - 1; KonohaStack *bottom = kctx->stack->stack; while(bottom < p) { if(p[0].calledMethod != NULL && isCalledMethod(kctx, p)) { kException_AddStackTrace(kctx, p+1, e); p[0].calledMethod = 0; //p = p[-1]; } p--; } } KLIB KRuntime_raise(kctx, e->symbol, SoftwareFault, NULL, sfp); }
//## DIR System.opendir(String path) static KMETHOD System_opendir(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace); DIR *d = opendir(systemPath); if(d == NULL) { int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "opendir", LogText("dirname", kString_text(path)), LogErrno); KLIB KRuntime_raise(kctx, KException_("IO"), fault, NULL, sfp); } kDir *dir = (kDir *)KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)d); KFieldSet(dir, dir->PathInfoNULL, path); if(!PLATAPI I18NModule.isSystemCharsetUTF8(kctx)) { dir->readerIconv = PLATAPI I18NModule.iconvSystemCharsetToUTF8(kctx, trace); } KReturn(dir); }
static void THROW_ZeroDividedException(KonohaContext *kctx, KonohaStack *sfp) { KLIB KRuntime_raise(kctx, KException_("ZeroDivided"), SoftwareFault, NULL, sfp); }
static void ThrowTypeError(KonohaContext *kctx, KonohaStack *sfp, int argc) { KLIB KRuntime_raise(kctx, KException_("TypeError"), SoftwareFault, NULL, sfp); }