Beispiel #1
0
//## String DIR.readPath()
static KMETHOD DIR_readPath(KonohaContext *kctx, KonohaStack *sfp)
{
	kDir *dir = (kDir *)sfp[0].asObject;
	if(dir->dirp != NULL) {
		KMakeTrace(trace, sfp);
		struct dirent entry, *result;
		int ret = readdir_r(dir->dirp, &entry, &result);
		if(result != NULL) {
			char *d_name = result->d_name, delim[2] = {'/', 0};
			KBuffer wb;
			KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
			KLIB KBuffer_Write(kctx, &wb, kString_text(dir->PathInfoNULL), kString_size(dir->PathInfoNULL));
			KLIB KBuffer_Write(kctx, &wb, delim, 1);
			if(dir->readerIconv != ICONV_NULL) {
				KLIB KBuffer_Write(kctx, &wb, d_name, strlen(d_name));
			}
			else {
				KLIB KBuffer_iconv(kctx, &wb, dir->readerIconv, d_name, strlen(d_name), trace);
			}
			KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer));
		}
		if(ret == -1) {
			KTraceErrorPoint(trace, SystemFault, "readdir", LogErrno);
		}
		kDir_close(kctx, dir);
	}
	KReturn(KNULL(String));
}
/* copied from src/sugar/sugarfunc.h */
static kString *kToken_ResolveEscapeSequence(KonohaContext *kctx, kToken *tk, size_t start)
{
	KBuffer wb;
	KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
	const char *text = kString_text(tk->text) + start;
	const char *end  = kString_text(tk->text) + kString_size(tk->text);
	KLIB KBuffer_Write(kctx, &wb, kString_text(tk->text), start);
	while(text < end) {
		int ch = *text;
		if(ch == '\\' && *(text+1) != '\0') {
			switch (*(text+1)) {
			/*
			 * compatible with ECMA-262
			 * http://ecma-international.org/ecma-262/5.1/#sec-7.8.4
			 */
			case 'b':  ch = '\b'; text++; break;
			case 't':  ch = '\t'; text++; break;
			case 'n':  ch = '\n'; text++; break;
			case 'v':  ch = '\v'; text++; break;
			case 'f':  ch = '\f'; text++; break;
			case 'r':  ch = '\r'; text++; break;
			case '"':  ch = '"';  text++; break;
			case '\'': ch = '\''; text++; break;
			case '\\': ch = '\\'; text++; break;
			default: return NULL;
			}
		}
		{
			char buf[1] = {ch};
			KLIB KBuffer_Write(kctx, &wb, (const char *)buf, 1);
		}
		text++;
	}
	return KLIB KBuffer_Stringfy(kctx, &wb, OnGcStack, StringPolicy_FreeKBuffer);
}
Beispiel #3
0
static void Person_p(KonohaContext *kctx, KonohaValue *v, int pos, KGrowingBuffer *wb)
{
	/* This function is called when serializing the object. */
	struct Person *p = (struct Person *) v[pos].asObject;
	KLIB KBuffer_Write(kctx, wb, S_text(p->name), S_size(p->name));
	KLIB KBuffer_Write(kctx, wb, ",", 1);
	KLIB KBuffer_printf(kctx, wb, KINT_FMT, p->age);
}
Beispiel #4
0
//## String File.readLine();
static KMETHOD File_readLine(KonohaContext *kctx, KonohaStack *sfp)
{
	kFile *file = (kFile *)sfp[0].asObject;
	if(file->fp != NULL) {
		KBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		int ch, pos = 0, hasUTF8 = false, bufferCount = 0, policy = StringPolicy_ASCII;
		char buffer[K_PAGESIZE];
		KMakeTrace(trace, sfp);
		while((ch = TRACE_fgetc(kctx, file, trace)) != EOF) {
			//DBG_P("ch='%c', pos=%d", ch, pos);
			if(ch == '\r') continue;
			if(ch == '\n') {
				if(bufferCount == 0 && (!hasUTF8 || file->readerIconv == ICONV_NULL)) {
					KReturn(KLIB new_kString(kctx, OnStack, buffer, pos, policy));
				}
				break;
			}
			if(ch > 127) {
				hasUTF8 = true;
				policy = StringPolicy_UTF8;
			}
			buffer[pos] = ch; pos++;
			if(!(pos < K_PAGESIZE)) {
				if(hasUTF8 && file->readerIconv != ICONV_NULL) {
					KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace);
				}
				else {
					KLIB KBuffer_Write(kctx, &wb, buffer, pos);
				}
				bufferCount++;
				hasUTF8 = false;
				pos = 0;
			}
		}
		if(pos > 0) {
			if(hasUTF8 && file->readerIconv != ICONV_NULL) {
				KLIB KBuffer_iconv(kctx, &wb, file->readerIconv, buffer, pos, trace);
			}
			else {
				KLIB KBuffer_Write(kctx, &wb, buffer, pos);
			}
		}
		kFile_CheckEOF(kctx, file, trace);
		KReturn(KLIB KBuffer_Stringfy(kctx, &wb, OnStack, policy | StringPolicy_FreeKBuffer));
	}
	else {
		KReturn(KNULL(String));
	}
}
Beispiel #5
0
//## @Public @Const @Immutable @Coercion String Symbol.toString();
static KMETHOD KSymbol_toString(KonohaContext *kctx, KonohaStack *sfp)
{
	ksymbol_t symbol = (ksymbol_t)sfp[0].intValue;
	kString *s = KSymbol_GetString(kctx, KSymbol_Unmask(symbol));
	if(KSymbol_Attr(symbol) != 0) {
		KBuffer wb;
		const char *prefix = KSymbol_prefixText(symbol);
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		KLIB KBuffer_Write(kctx, &wb, prefix, strlen(prefix));
		KLIB KBuffer_Write(kctx, &wb, kString_text(s), kString_size(s));
		s = KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer);
	}
	KReturn(s);
}
Beispiel #6
0
static void BasicBlock_WriteBuffer(KonohaContext *kctx, bblock_t blockId, KBuffer *wb)
{
	BasicBlock *bb = BasicBlock_FindById(kctx, blockId);
	while(bb != NULL && bb->codeoffset == -1) {
		size_t len = bb->size - sizeof(BasicBlock);
		bb->codeoffset = CodeOffset(wb);
		if(bb->nextid == bb->branchid  && bb->nextid != -1) {
			bb->branchid = -1;
			len -= sizeof(KVirtualCode); // remove unnecesarry jump ..
		}
		if(len > 0) {
			bblock_t id = BasicBlock_id(kctx, bb);
			char buf[len];  // bb is growing together with wb.
			memcpy(buf, ((char *)bb) + sizeof(BasicBlock), len);
			KLIB KBuffer_Write(kctx, wb, buf, len);
			bb = BasicBlock_FindById(kctx, id);  // recheck
			bb->lastoffset = CodeOffset(wb) - sizeof(KVirtualCode);
			DBG_ASSERT(bb->codeoffset + ((len / sizeof(KVirtualCode)) - 1) * sizeof(KVirtualCode) == bb->lastoffset);
		}
		else {
			DBG_ASSERT(bb->branchid == -1);
		}
		bb = BasicBlock_FindById(kctx, bb->nextid);
	}
	bb = BasicBlock_FindById(kctx, blockId);
	while(bb != NULL) {
		if(bb->branchid != -1 /*&& bb->branchid != builder->bbReturnId*/) {
			BasicBlock *bbJ = BasicBlock_FindById(kctx, bb->branchid);
			if(bbJ->codeoffset == -1) {
				BasicBlock_WriteBuffer(kctx, bb->branchid, wb);
			}
		}
		bb = BasicBlock_FindById(kctx, bb->nextid);
	}
}
Beispiel #7
0
// writefunction for Curl.receiveString()
static size_t writeToBuffer(void *buffer, size_t size, size_t nmemb, void *obj)
{
	struct ReceiveBuffer *rbuf = (struct ReceiveBuffer *)obj;
	KonohaContext *kctx = rbuf->kctx;
	size_t writeSize = size * nmemb;
	KLIB KBuffer_Write(kctx, &rbuf->wb, (char *)buffer, writeSize);
	return writeSize;
}
Beispiel #8
0
static void kFile_format(KonohaContext *kctx, KonohaValue *v, int pos, KBuffer *wb)
{
	kFile *file = (kFile *)v[pos].asObject;
	if(file->PathInfoNULL != NULL) {
		KLIB KBuffer_Write(kctx, wb, kString_text(file->PathInfoNULL), kString_size(file->PathInfoNULL));
	}
	else {
		KLIB KBuffer_printf(kctx, wb, "FILE:%p", file->fp);
	}
}
static kNode *ParseSource(KonohaContext *kctx, kNameSpace *ns, const char *script, size_t len)
{
	KBuffer wb;
	KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
	KLIB KBuffer_Write(kctx, &wb, "(", 1);
	KLIB KBuffer_Write(kctx, &wb, script, len);
	KLIB KBuffer_Write(kctx, &wb, ")", 1);

	KTokenSeq tokens = {ns, KGetParserContext(kctx)->preparedTokenList};
	KTokenSeq_Push(kctx, tokens);
	const char *buf = KLIB KBuffer_text(kctx, &wb, EnsureZero);
	SUGAR Tokenize(kctx, ns, buf, 0, 0, tokens.tokenList);
	KTokenSeq_End(kctx, tokens);

	KTokenSeq step2 = {ns, tokens.tokenList, kArray_size(tokens.tokenList)};
	SUGAR Preprocess(kctx, ns, RangeTokenSeq(tokens), NULL, step2.tokenList);
	KTokenSeq_End(kctx, step2);
	kNode *newexpr = SUGAR ParseNewNode(kctx, ns, step2.tokenList, &step2.beginIdx, step2.endIdx, 0, NULL);
	KTokenSeq_Pop(kctx, tokens);
	KLIB KBuffer_Free(&wb);
	return newexpr;
}
Beispiel #10
0
static void kMethod_WriteToBuffer(KonohaContext *kctx, kMethod *mtd, KBuffer *wb)
{
	kParam *pa = kMethod_GetParam(mtd);
	Method_WriteAttributeToBuffer(kctx, mtd, wb);
	KLIB KBuffer_printf(kctx, wb, "%s %s.%s%s", KType_text(pa->rtype), KType_text(mtd->typeId), KMethodName_Fmt2(mtd->mn));
	{
		size_t i;
		KLIB KBuffer_Write(kctx, wb, "(", 1);
		for(i = 0; i < pa->psize; i++) {
			if(i > 0) {
				KLIB KBuffer_Write(kctx, wb, ", ", 2);
			}
			if(KTypeAttr_Is(ReadOnly, pa->paramtypeItems[i].attrTypeId)) {
				KLIB KBuffer_printf(kctx, wb, "@ReadOnly ");
			}
			if(KTypeAttr_Is(Coercion, pa->paramtypeItems[i].attrTypeId)) {
				KLIB KBuffer_printf(kctx, wb, "@Coercion ");
			}
			KLIB KBuffer_printf(kctx, wb, "%s %s", KType_text(pa->paramtypeItems[i].attrTypeId), KSymbol_text(pa->paramtypeItems[i].name));
		}
		KLIB KBuffer_Write(kctx, wb, ")", 1);
	}
}
Beispiel #11
0
static KMETHOD Statement_import(KonohaContext *kctx, KonohaStack *sfp)
{
	int ret = false;
	VAR_Statement(stmt, gma);
	kTokenArray *tokenList = (kTokenArray *) kStmt_GetObjectNULL(kctx, stmt, KW_TokenPattern);
	if(tokenList == NULL) {
		KReturnUnboxValue(false);
	}
	kNameSpace *ns = Stmt_ns(stmt);
	SugarSyntaxVar *syn = (SugarSyntaxVar *) SYN_(ns, KW_ExprMethodCall);
	kExpr *expr;
	kTokenVar *tkImport = /*G*/new_(TokenVar, 0, OnGcStack);
	tkImport->resolvedSymbol = MN_("import");
	if(IS_Token(tokenList)) {
		kTokenArray *list = ((kToken *) tokenList)->subTokenList;
		if(IS_String(list)) {
			/* case: import cstyle; */
			kString *pkgname = (kString *) list;
			expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname);
		}
		else if(kArray_size(list) == 1) {
			/* case : import("konoha.import"); */
			kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text);
			expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 3,
					tkImport, new_ConstValueExpr(kctx, O_ct(ns), UPCAST(ns)), param0);
		}
		else if(kArray_size(list) == 2) {
			/* case : import("konoha.import", "import"); */
			kExpr *param0 = makeStringConstValue(kctx, list->TokenItems[0]->text);
			kExpr *param1 = makeStringConstValue(kctx, list->TokenItems[1]->text);
			expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 4,
					tkImport, new_ConstValueExpr(kctx, O_ct(ns), UPCAST(ns)),
					param0, param1);
		} else {
			KReturnUnboxValue(false);
		}
	} else {
		KGrowingBuffer wb;
		KLIB KBuffer_Init(&(kctx->stack->cwb), &wb);
		/* case : import konoha.import */
		ksymbol_t star = SYM_("*");
		size_t i = 0;
		if(i + 2 < kArray_size(tokenList)) {
			for (; i < kArray_size(tokenList)-1; i+=2) {
				/* name . */
				kToken *tk  = tokenList->TokenItems[i+0];
				if(i+2 < kArray_size(tokenList)) {
					kToken *startTk = tokenList->TokenItems[i+2];
					if(startTk->resolvedSyntaxInfo->keyword == star) {
						break;
					}
				}
				KLIB KBuffer_Write(kctx, &wb, S_text(tk->text), S_size(tk->text));
				KLIB KBuffer_Write(kctx, &wb, ".", 1);
			}
		}
		kString *name = tokenList->TokenItems[i]->text;
		KLIB KBuffer_Write(kctx, &wb, S_text(name), S_size(name));

		kString *pkgname = KLIB new_kString(kctx, OnGcStack, KLIB KBuffer_Stringfy(kctx, &wb, 1), KBuffer_bytesize(&wb), 0);
		expr = CreateImportCall(kctx, syn, tkImport, ns, pkgname);
	}
	KLIB kObjectProto_SetObject(kctx, stmt, KW_ExprPattern, TY_Expr, expr);
	ret = SUGAR kStmt_TypeCheckByName(kctx, stmt, KW_ExprPattern, gma, CT_void, TypeCheckPolicy_ALLOWVOID);
	if(ret) {
		kStmt_typed(stmt, EXPR);
	}
	KReturnUnboxValue(ret);
}
Beispiel #12
0
static void kPrototype_format(KonohaContext *kctx, KonohaValue *v, int pos, KBuffer *wb)
{
	KLIB KBuffer_Write(kctx, wb, "{", 1);
	KLIB kObjectProto_format(kctx, v, pos, wb, 0);
	KLIB KBuffer_Write(kctx, wb, "}", 1);
}