Пример #1
0
static KMETHOD Array_toIterator(KonohaContext *kctx, KonohaStack *sfp)
{
	kArray *a = sfp[0].asArray;
	KonohaClass *cIterator = CT_p0(kctx, CT_Iterator, O_ct(a)->p0);
	kIterator *itr = (kIterator *)KLIB new_kObject(kctx, OnStack, cIterator, 0);
	KFieldSet(itr, itr->arrayList, a);
	itr->hasNext = Array_hasNext;
	itr->setNextResult = TY_isUnbox(O_ct(a)->p0) ? Array_setNextResultUnbox : Array_setNextResult;
	KReturn(itr);
}
Пример #2
0
//## Json Json.getJson(String key);
static KMETHOD Json_getJson(KonohaContext *kctx, KonohaStack *sfp)
{
	json_t* obj = ((struct _kJson *)sfp[0].asObject)->obj;
	CHECK_JSON(obj, KReturn((kJson *)KLIB Knull(kctx, O_ct(sfp[0].asObject))));
	const char *key = S_text(sfp[1].asString);
	json_t* ret = json_object_get(obj, key);
	CHECK_JSON(ret, KReturn((kJson *)KLIB Knull(kctx, O_ct(sfp[0].asObject))));
	ret = json_incref(ret);
	struct _kJson *json = (struct _kJson *)KLIB new_kObjectDontUseThis(kctx, KGetReturnType(sfp), 0);
	json->obj = ret;
	KReturn(json);
}
Пример #3
0
static void DumpVisitor_visitConstExpr(KonohaContext *kctx, IRBuilder *self, kExpr *expr)
{
	KGrowingBuffer wb;
	KLIB Kwb_init(&(kctx->stack->cwb), &wb);
	KonohaStack sfp[1];
	kObject *obj = expr->objectConstValue;
	sfp[0].asObject = obj;
	O_ct(obj)->p(kctx, sfp, 0, &wb, 0);
	char  *str = (char *) KLIB Kwb_top(kctx, &wb, 0);
	char buf[128];
	snprintf(buf, 128, "CONST:%s:'%s'", CT_t(O_ct(obj)), str);
	emit_string(buf, "", "", DUMPER(self)->indent);
	KLIB Kwb_free(&wb);
}
Пример #4
0
static kExpr *CreateImportCall(KonohaContext *kctx, SugarSyntaxVar *syn, kToken *tkImport, kNameSpace *ns, kString *pkgname)
{
	kExpr *ePKG = makeStringConstValue(kctx, pkgname);
	kExpr *expr = SUGAR new_UntypedCallStyleExpr(kctx, syn, 3,
			tkImport, new_ConstValueExpr(kctx, O_ct(ns), UPCAST(ns)), ePKG);
	return expr;
}
Пример #5
0
//## method int Array.lastIndexOf(T0 a1);
static KMETHOD Array_lastIndexOf(KonohaContext *kctx, KonohaStack *sfp)
{
	kArray *a = sfp[0].asArray;
	kint_t res = -1;
	size_t i = 0;
	if(kArray_isUnboxData(a)) {
		uintptr_t nv = sfp[1].unboxValue;
		for(i = kArray_size(a)- 1; i != 0; i--) {
			if(a->unboxItems[i] == nv) {
				break;
			}
		}
	} else {
		//TODO: Need to implement Object compareTo;
		kObject *o = sfp[1].asObject;
		for(i = kArray_size(a)- 1; i != 0; i--) {
//			KMakeTrace(trace, sfp);
//			KLIB KonohaRuntime_raise(kctx, EXPT_("NotImplemented"), NULL, trace);
			if(O_ct(o)->compareObject(a->ObjectItems[i], o) == 0) {
				break;
			}
		}
	}
	res = i;
	KReturnUnboxValue(res);
}
Пример #6
0
// PoolPlugin React.create(String traceName, String key);
static KMETHOD React_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_react *p = POOL_PLUGIN_CLONE(pool_plugin_react);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	p->conf.traceName = copy_string(kctx, sfp[1].asString);
	p->conf.key       = copy_string(kctx, sfp[2].s);
	RETURN_(ret);
}
Пример #7
0
// PoolPlugin KeyFilter.create(String key);
static KMETHOD KeyFilter_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_key_filter *p = POOL_PLUGIN_CLONE(pool_plugin_key_filter);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	p->klen = S_size(sfp[1].asString);
	p->key  = copy_string(kctx, sfp[2].s);
	RETURN_(ret);
}
Пример #8
0
//## Expr Expr.new(Object value);
static KMETHOD Expr_new(KonohaContext *kctx, KonohaStack *sfp)
{
	KonohaClass *ct = O_ct(sfp[1].asObject);
	if(CT_isUnbox(ct)) {
		KReturn(new_UnboxConstValueExpr(kctx, ct->typeId, sfp[1].unboxValue));
	}
	KReturn(new_ConstValueExpr(kctx, ct->typeId, sfp[1].asObject));
}
Пример #9
0
// PoolPlugin Response.create(Event ev);
static KMETHOD Response_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_response *p = POOL_PLUGIN_CLONE(pool_plugin_response);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	kRawPtr *ev = (kRawPtr *) sfp[1].asObject;
	p->bev = ev->rawptr;
	RETURN_(ret);
}
Пример #10
0
static void Iterator_Init(KonohaContext *kctx, kObject *o, void *conf)
{
	kIterator *itr = (kIterator *)o;
	int isUnboxEntry = TY_isUnbox(O_ct(itr)->p0);
	KFieldInit(itr, itr->source, K_NULL);
	itr->current_pos = 0;
	itr->hasNext = Nothing_hasNext;
	itr->setNextResult = isUnboxEntry ? Nothing_setNextResultUnbox : Nothing_setNextResult;
}
Пример #11
0
//## Expr Expr.setConstValue(Object value);
static KMETHOD Expr_setConstValue(KonohaContext *kctx, KonohaStack *sfp)
{
	kExpr *expr = sfp[0].asExpr;
	KonohaClass *ct = O_ct(sfp[1].asObject);
	if(CT_isUnbox(ct)) {
		KReturn(SUGAR kExpr_SetUnboxConstValue(kctx, expr, ct->typeId, sfp[1].unboxValue));
	}
	KReturn(SUGAR kExpr_SetConstValue(kctx, expr, ct->typeId, sfp[1].asObject));
}
Пример #12
0
// PoolPlugin Statics.create(Func initFo, Func exitFo, Func func);
static KMETHOD Statics_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_statics *p = POOL_PLUGIN_CLONE(pool_plugin_statics);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	p->context = (uintptr_t)statics_init(kctx, sfp[1].asFunc, sfp[2].asFunc, sfp[3].asFunc);
	p->finit = p_init;
	p->fexit = p_exit;
	p->function = p_func;
	RETURN_(ret);
}
Пример #13
0
// PoolPlugin Timer.create(int timer, int startFlat, int contFlat, int finFlag);
static KMETHOD Timer_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_timer *p = POOL_PLUGIN_CLONE(pool_plugin_timer);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	p->timer = sfp[1].intValue;
	p->flag_start  = sfp[2].intValue;
	p->flag_cont   = sfp[3].intValue;
	p->flag_finish = sfp[4].intValue;
	RETURN_(ret);
}
Пример #14
0
static void dumpMethod(CTX, ksfp_t *sfp, kMethod *mtd)
{
	kwb_t wb;
	kwb_init(&(_ctx->stack->cwb), &wb);
	KSETv(sfp[2].mtd, mtd);
	O_ct(mtd)->p(_ctx, sfp, 2, &wb, 1);
	PLAT printf_i("%s\n", kwb_top(&wb, 1));
	kwb_free(&wb);
	return;
}
Пример #15
0
static void ObjectField_Reftrace(KonohaContext *kctx, kObject *o, KObjectVisitor *visitor)
{
	KonohaClass *c =O_ct(o);
	KonohaClassField *fieldItems = c->fieldItems;
	size_t i, fieldsize = c->fieldsize;
	for (i = 0; i < fieldsize; i++) {
		if(fieldItems[i].isobj) {
			KRefTraceNullable(o->fieldObjectItems[i]);   // FIXME:
		}
	}
}
Пример #16
0
static kbool_t KonohaClass_setClassFieldObjectValue(KonohaContext *kctx, KonohaClassVar *definedClass, ksymbol_t sym, kObject *ObjectValue)
{
	int i;
	for(i = definedClass->fieldsize - 1; i >= 0; i--) {
		if(definedClass->fieldItems[i].fn == sym  && O_ct(definedClass->defaultNullValueVar_OnGlobalConstList->fieldObjectItems[i]) == O_ct(ObjectValue)) {
			kObjectVar *o = definedClass->defaultNullValueVar_OnGlobalConstList;
			KFieldSet(o, o->fieldObjectItems[i], ObjectValue);
			return true;
		}
	}
	return false;
}
Пример #17
0
static void RETURN_PyObject_(KonohaContext *kctx, KonohaStack *sfp, PyObject *pyo)
{
	if(pyo != NULL) {
		RETURN_(KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)pyo));
	}
	else {
		// ERROR if python object is NULL
		// add ktrace.
		// looks stupid
		PyErr_Print();
	}
}
Пример #18
0
//## void NameSpace.useStaticFunc(Object o);
static KMETHOD NameSpace_useStaticFunc(KonohaContext *kctx, KonohaStack *sfp)
{
	KMakeTrace(trace, sfp);
	KonohaClass *ct = O_ct(sfp[1].asObject);
	kNameSpace *ns = sfp[0].asNameSpace;
	kNameSpace_SetStaticFunction(kctx, ns, ct->methodList_OnGlobalConstList, ct->typeId, trace);
	while(ns != NULL) {
		kNameSpace_SetStaticFunction(kctx, ns, ns->methodList_OnList, ct->typeId, trace);
		ns = ns->parentNULL;
	}
	KReturnVoid();
}
Пример #19
0
//## @Static Json Json.parse(String str);
static KMETHOD Json_parse(KonohaContext *kctx, KonohaStack *sfp)
{
	const char *buf = S_text(sfp[1].asString);
	json_t* obj;
	json_error_t err;
	obj = json_loads(buf, 0, &err);
	struct _kJson *ret = (struct _kJson *)KLIB new_kObjectDontUseThis(kctx, KGetReturnType(sfp), 0);
	CHECK_JSON(obj, KReturn((kJson *)KLIB Knull(kctx, O_ct(ret))));
	obj = json_incref(obj);
	ret->obj = obj;
	KReturn(ret);
}
Пример #20
0
// @SmartReturn Object Object.as(Object target)
static KMETHOD Object_as(KonohaContext *kctx, KonohaStack *sfp)
{
	KonohaClass *selfClass = O_ct(sfp[0].asObject), *targetClass = KGetReturnType(sfp);
	kObject *returnValue;
	if(selfClass == targetClass || selfClass->isSubType(kctx, selfClass, targetClass)) {
		returnValue = sfp[0].asObject;
	}
	else {
		returnValue = KLIB Knull(kctx, targetClass);
	}
	sfp[K_RTNIDX].unboxValue = O_unbox(returnValue);
	KReturn(returnValue);
}
Пример #21
0
//## Log LogPool.get()
static KMETHOD LogPool_get(KonohaContext *kctx, KonohaStack *sfp)
{
	logpool_t *lp = (logpool_t *) ((kRawPtr *) sfp[0].asObject)->rawptr;
	char *buf = malloc(256);
	char *ret = logpool_client_get(lp, buf, 256);
	kObject *log = KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), 0);
	if (ret == NULL) {
		kObject_setNullObject(log, 1);
		free(buf);
	}
	RawPtr_init(kctx, log, buf);
	RETURN_(log);
}
Пример #22
0
static size_t sweep0(GcContext *mng, void *p, int n, size_t sizeOfObject)
{
	KonohaContext *kctx = mng->kctx;
	unsigned i;
	size_t collected = 0;
	size_t pageSize = K_PAGESIZE/sizeOfObject;
	for(i = 0; i < pageSize; ++i) {
		kGCObject0 *o = (kGCObject0 *)ShiftPointer(p,sizeOfObject*i);
		if(!Object_isMark((kObject*)o)) {
			if( O_ct(o)) {
				DBG_P("~Object%d %s", n, O_ct(o)->DBG_NAME);
				KLIB kObjectProto_Free(kctx, (kObjectVar*)o);
				assert(O_ct(o)->cstruct_size == sizeOfObject);
				++collected;
				OBJECT_REUSE(o, n);
				MSGC(n).freelist.size += 1;
			}
		}
		Object_unsetMark(((kObjectVar*)o));
	}
	return collected;
}
Пример #23
0
// PoolPlugin ValFilter.create(String key, String val, String op);
static KMETHOD ValFilter_create(KonohaContext *kctx, KonohaStack *sfp)
{
	struct pool_plugin_val_filter *p = POOL_PLUGIN_CLONE(pool_plugin_val_filter);
	kRawPtr *ret = (kRawPtr *) KLIB new_kObject(kctx, O_ct(sfp[K_RTNIDX].o), (uintptr_t)p);
	p->klen = S_size(sfp[1].asString);
	p->key  = copy_string(kctx, sfp[1].asString);
	p->vlen = S_size(sfp[2].s);
	p->val  = copy_string(kctx, sfp[2].s);
	if (strncmp(S_text(sfp[3].s), "eq", 2) == 0) {
		p->val_cmp = val_eq;
	} else {
		assert(0 && "TODO");
	}
	RETURN_(ret);
}
Пример #24
0
static KMETHOD NameSpace_man(KonohaContext *kctx, KonohaStack *sfp)
{
    INIT_GCSTACK();
    kArray *list = kctx->stack->gcstack_OnContextConstList;
    size_t start = kArray_size(list);
    kNameSpace *ns = sfp[0].asNameSpace;
    KonohaClass *ct = O_ct(sfp[1].asObject);
    DBG_P("*** man %s", TY_t(ct->typeId));
    while(ns != NULL) {
        copyMethodList(kctx, ct->typeId, ns->methodList_OnList, list);
        ns = ns->parentNULL;
    }
    copyMethodList(kctx, ct->typeId, ct->methodList_OnGlobalConstList, list);
    dumpMethodList(kctx, sfp, start, list);
    RESET_GCSTACK();
}
Пример #25
0
static kbool_t kMethod_CheckMethodCallStack(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd, int argc)
{
	int i;
	kParam *param = Method_param(mtd);
	for(i = 1; i <= argc; i++) {
		KonohaClass *paramClass = O_ct(sfp[i].asObject);
		ktype_t ptype = param->paramtypeItems[i-1].ty;
		if(ptype != paramClass->typeId) {
			return false;
		}
		if(CT_isUnbox(paramClass)) {
			sfp[i].unboxValue = O_unbox(sfp[i].asObject);
		}
	}
	return true;

}
Пример #26
0
static kMethod *Object_newProtoSetterNULL(KonohaContext *kctx, kStmt *stmt, kObject *o, ktype_t ty, ksymbol_t symbol)
{
    ktype_t cid = O_typeId(o);
    kNameSpace *ns = Stmt_ns(stmt);
    kMethod *mtd = KLIB kNameSpace_GetSetterMethodNULL(kctx, ns, cid, symbol, TY_var);
    if(mtd != NULL) {
        SUGAR kStmt_Message2(kctx, stmt, NULL, ErrTag, "already defined name: %s", SYM_t(symbol));
        return NULL;
    }
    mtd = KLIB kNameSpace_GetGetterMethodNULL(kctx, ns, cid, symbol, TY_var);
    if(mtd != NULL && Method_returnType(mtd) != ty) {
        SUGAR kStmt_Message2(kctx, stmt, NULL, ErrTag, "differently defined name: %s", SYM_t(symbol));
        return NULL;
    }
    int flag = kField_Setter;
    if(mtd == NULL) { // no getter
        flag |= kField_Getter;
    }
    KLIB KonohaClass_AddField(kctx, O_ct(o), flag, ty, symbol);
    return KLIB kNameSpace_GetSetterMethodNULL(kctx, ns, cid, symbol, ty);
}
Пример #27
0
static void dumpOPCODE(KonohaContext *kctx, VirtualMachineInstruction *c, VirtualMachineInstruction *pc_start)
{
	size_t i, size = OPDATA[c->opcode].size;
	const kushort_t *vmt = OPDATA[c->opcode].types;
	if(pc_start == NULL) {
		DUMP_P("[%p:%d]\t%s(%d)", c, c->line, T_opcode(c->opcode), (int)c->opcode);
	}
	else {
		DUMP_P("[L%d:%d]\t%s(%d)", (int)(c - pc_start), c->line, T_opcode(c->opcode), (int)c->opcode);
	}
	for(i = 0; i < size; i++) {
		DUMP_P(" ");
		switch(vmt[i]) {
		case VMT_VOID: break;
		case VMT_ADDR:
			if(pc_start == NULL) {
				DUMP_P("%p", c->p[i]);
			}
			else {
				DUMP_P("L%d", (int)((VirtualMachineInstruction*)c->p[i] - pc_start));
			}
			break;
		case VMT_R:
			DUMP_P("sfp[%d,r=%d]", (int)c->data[i]/2, (int)c->data[i]);
			break;
		case VMT_U:
			DUMP_P("u%lu", c->data[i]); break;
		case VMT_I:
		case VMT_INT:
			DUMP_P("i%ld", c->data[i]); break;
		case VMT_F:
			DUMP_P("function(%p)", c->p[i]); break;
		case VMT_CID:
			DUMP_P("CT(%s)", CT_t(c->ct[i])); break;
		case VMT_CO:
			DUMP_P("CT(%s)", CT_t(O_ct(c->o[i]))); break;
		}/*switch*/
	}
	DUMP_P("\n");
}
Пример #28
0
static KMETHOD PyObject_toString(KonohaContext *kctx, KonohaStack *sfp)
{
	kPyObject *po = (kPyObject*)sfp[0].asObject;
	KUtilsWriteBuffer wb;
	// assert
	DBG_ASSERT(po->self != NULL);
	KLIB Kwb_init(&(kctx->stack->cwb), &wb);
	O_ct(sfp[0].asObject)->p(kctx, sfp, 0, &wb, 0);
	kString* s = KLIB new_kString(kctx, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0);
	KLIB Kwb_free(&wb);
	RETURN_(s);
	//if (PyString_Check(po->self)) {
	//	//dec
	//	t = PyString_AsString(po->self);
	//	RETURN_(KLIB new_kString(kctx, t, strlen(t), 0));
	//}
	//else if (PyUnicode_Check(po->self)) {
	//	//dec
	//	PyObject* s = PyUnicode_AsUTF8String(po->self);
	//	// [TODO] there is no t's NULL check. Is it OK?
	//	t = PyString_AsString(s);
	//	RETURN_(KLIB new_kString(kctx, t, strlen(t), 0));
	//}
	//else if (PyByteArray_Check(po->self)) {
	//	//dec
	//	t = PyByteArray_AsString(po->self);
	//	RETURN_(KLIB new_kString(kctx, t, strlen(t), 0));
	//}
	//else {
	//	KUtilsWriteBuffer wb;
	//	KLIB Kwb_init(&(kctx->stack->cwb), &wb);
	//	O_ct(sfp[0].asObject)->p(kctx, sfp, 0, &wb, 0);
	//	kString* s = KLIB new_kString(kctx, KLIB Kwb_top(kctx, &wb, 1), Kwb_bytesize(&wb), 0);
	//	KLIB Kwb_free(&wb);
	//	RETURN_(s);
	//}
}
Пример #29
0
static void dumpMethodList(CTX, ksfp_t *sfp, size_t start, kArray *list)
{
	size_t i;
	for(i = start; i < kArray_size(list); i++) {
		dumpMethod(_ctx, sfp, list->methods[i]);
	}
}

KMETHOD KonohaSpace_man(CTX, ksfp_t *sfp _RIX)
{
	INIT_GCSTACK();
	kArray *list = _ctx->stack->gcstack;
	size_t start = kArray_size(list);
	kKonohaSpace *ks = sfp[0].ks;
	kclass_t *ct = O_ct(sfp[1].o);
	DBG_P("*** man %s", TY_t(ct->cid));
	while(ks != NULL) {
		copyMethodList(_ctx, ct->cid, ks->methods, list);
		ks = ks->parentNULL;
	}
	copyMethodList(_ctx, ct->cid, ct->methods, list);
	dumpMethodList(_ctx, sfp, start, list);
	RESET_GCSTACK();
}

// --------------------------------------------------------------------------

#define _Public   kMethod_Public
#define _Const    kMethod_Const
#define _Coercion kMethod_Coercion
Пример #30
0
// boolean Object.instanceOf(Object o)
static KMETHOD Object_instanceOf(KonohaContext *kctx, KonohaStack *sfp)
{
	KonohaClass *selfClass = O_ct(sfp[0].asObject), *targetClass = O_ct(sfp[1].asObject);
	KReturnUnboxValue(selfClass == targetClass || selfClass->isSubType(kctx, selfClass, targetClass));
}