Esempio n. 1
0
//## 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);
}
Esempio n. 2
0
static kbool_t FuelVM_VisitErrorNode(KonohaContext *kctx, KBuilder *builder, kNode *stmt, void *thunk)
{
	kString *Err  = kNode_getErrorMessage(kctx, stmt);
	INode   *Node = CreateObject(BLD(builder), TYPE_String, (void *)Err);
	CreateThrow(BLD(builder), Node, KException_("RuntimeScript"), SoftwareFault, kNode_uline(stmt));
	return true;
}
Esempio n. 3
0
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;
}
Esempio n. 4
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;
}
Esempio n. 5
0
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;
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
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;
}
Esempio n. 8
0
//## 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);
}
Esempio n. 9
0
static void THROW_ZeroDividedException(KonohaContext *kctx, KonohaStack *sfp)
{
	KLIB KRuntime_raise(kctx, KException_("ZeroDivided"), SoftwareFault, NULL, sfp);
}
Esempio n. 10
0
static void ThrowTypeError(KonohaContext *kctx, KonohaStack *sfp, int argc)
{
	KLIB KRuntime_raise(kctx, KException_("TypeError"), SoftwareFault, NULL, sfp);
}