// @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); }
static KMETHOD TypeCheck_to(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck(stmt, expr, gma, reqty); kExpr *targetExpr = SUGAR kStmt_TypeCheckExprAt(kctx, stmt, expr, 2, gma, TY_var, 0); kExpr *selfExpr = SUGAR kStmt_TypeCheckExprAt(kctx, stmt, expr, 1, gma, targetExpr->ty, TypeCheckPolicy_NOCHECK); if(selfExpr != K_NULLEXPR && targetExpr != K_NULLEXPR) { KonohaClass *selfClass = CT_(selfExpr->ty), *targetClass = CT_(targetExpr->ty); if(selfExpr->ty == targetExpr->ty || selfClass->isSubType(kctx, selfClass, targetClass)) { kStmtExpr_Message(kctx, stmt, selfExpr, InfoTag, "no need: %s to %s", TY_t(selfExpr->ty), TY_t(targetExpr->ty)); KReturn(selfExpr); } kNameSpace *ns = Stmt_ns(stmt); kMethod *mtd = KLIB kNameSpace_GetCoercionMethodNULL(kctx, ns, selfExpr->ty, targetExpr->ty); if(mtd == NULL) { mtd = KLIB kNameSpace_GetMethodByParamSizeNULL(kctx, ns, selfExpr->ty, MN_("to"), 0); DBG_ASSERT(mtd != NULL); // because Object.to is found. if(mtd->typeId != selfExpr->ty) { KReturn(kStmtExpr_Message(kctx, stmt, selfExpr, ErrTag, "undefined coercion: %s to %s", TY_t(selfExpr->ty), TY_t(targetExpr->ty))); } } KReturn(SUGAR kStmtkExpr_TypeCheckCallParam(kctx, stmt, expr, mtd, gma, targetClass->typeId)); } }
//## @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); }
//## String String.new(Bytes ba); static KMETHOD String_new_fromBytes_withDefaultDecode(KonohaContext *kctx, KonohaStack *sfp) { kBytes *ba = sfp[1].asBytes; kString *s = TS_EMPTY; if(ba->bytesize != 0) { KMakeTrace(trace, sfp); KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KBuffer_convertCharset(kctx, &wb, "UTF-8", I18NAPI systemCharset, ba->buf, ba->bytesize, trace); KLIB KBuffer_text(kctx, &wb, EnsureZero); /* String must be Null terminated */ s = KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer); } KReturn(s); }
static KMETHOD Expression_Defined(KonohaContext *kctx, KonohaStack *sfp) { VAR_Expression(expr, tokenList, beginIdx, currentIdx, endIdx); kNameSpace *ns = kNode_ns(expr); if(beginIdx == currentIdx && beginIdx + 1 < endIdx) { kTokenVar *definedToken = tokenList->TokenVarItems[beginIdx]; // defined kTokenVar *pToken = tokenList->TokenVarItems[beginIdx+1]; if(IS_Array(pToken->GroupTokenList)) { SUGAR kNode_Op(kctx, expr, definedToken, 0); FilterDefinedParam(kctx, ns, RangeGroup(pToken->GroupTokenList)); KReturn(SUGAR AppendParsedNode(kctx, expr, RangeGroup(pToken->GroupTokenList), NULL, ParseExpressionOption, "(")); } } }
//## Stat System.stat(String path) static KMETHOD System_stat(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); struct stat buf = {}; /* zero */ int ret = stat(systemPath, &buf); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "stat", LogText("path", kString_text(path)), LogErrno); } KReturn(KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), (uintptr_t)&buf)); }
// MecabNode MecabNode.enext() static KMETHOD MecabNode_enext(KonohaContext *kctx, KonohaStack *sfp) { struct _kMecabNode *node = (struct _kMecabNode *)sfp[0].asObject; mecab_node_t* enext = node->node->enext; struct _kMecabNode* ret; if(node != NULL) { ret = (struct _kMecabNode *)KLIB new_kObject(kctx, OnStack, KGetReturnType(sfp), 0); ret->node = enext; KReturn(ret); } else { KReturnDefaultValue(); } }
static KMETHOD TypeCheck_to(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck2(stmt, expr, ns, reqc); kNode *targetNode = SUGAR TypeCheckNodeAt(kctx, expr, 2, ns, KClass_INFER, 0); kNode *selfNode = SUGAR TypeCheckNodeAt(kctx, expr, 1, ns, KClass_(targetNode->attrTypeId), TypeCheckPolicy_NoCheck); if(selfNode != K_NULLNODE && targetNode != K_NULLNODE) { KClass *selfClass = KClass_(selfNode->attrTypeId), *targetClass = KClass_(targetNode->attrTypeId); if(selfNode->attrTypeId == targetNode->attrTypeId || selfClass->isSubType(kctx, selfClass, targetClass)) { SUGAR MessageNode(kctx, selfNode, NULL, ns, InfoTag, "no need: %s to %s", KType_text(selfNode->attrTypeId), KType_text(targetNode->attrTypeId)); KReturn(selfNode); } kNameSpace *ns = kNode_ns(stmt); kMethod *mtd = KLIB kNameSpace_GetCoercionMethodNULL(kctx, ns, selfClass, targetClass); if(mtd == NULL) { mtd = KLIB kNameSpace_GetMethodByParamSizeNULL(kctx, ns, selfClass, KMethodName_("to"), 0, KMethodMatch_CamelStyle); DBG_ASSERT(mtd != NULL); // because Object.to is found. if(mtd->typeId != selfClass->typeId) { KReturn(SUGAR MessageNode(kctx, selfNode, NULL, ns, ErrTag, "undefined coercion: %s to %s", KClass_text(selfClass), KClass_text(targetClass))); } } KReturn(SUGAR TypeCheckMethodParam(kctx, mtd, expr, ns, targetClass)); } }
static CMPIStatus _set_value(KValue* kv, KTag tag, const CMPIData* cd) { /* Strip unused flags */ tag = KTypeOf(tag) | (tag & KTAG_ARRAY); if (_cmpitype_to_ktag(cd->type) != tag) { KReturn(ERR_FAILED); } kv->exists = 1; if (cd->state & CMPI_nullValue) { kv->null = 1; kv->u.uint64 = 0; KReturn(OK); } else { kv->u.uint64 = cd->value.uint64; if (tag == KTYPE_STRING) { KString* ks = (KString*)kv; ks->chars = KChars(ks->value); } if (tag & KTAG_ARRAY) { KArray* ks = (KArray*)kv; ks->count = CMGetArrayCount(ks->value, NULL); } /* ATTN: validate references and instances */ KReturn(OK); } }
static KMETHOD Array_shift(KonohaContext *kctx, KonohaStack *sfp) { kArray *a = sfp[0].asArray; struct _kAbstractArray *a2 = (struct _kAbstractArray *)a; if(kArray_isUnboxData(a)) { uintptr_t v = a->unboxItems[0]; kArray_removeAt(kctx, a, 0); KReturnUnboxValue(v); } else { kObject *value = a2->a.ObjectItems[0]; kArray_removeAt(kctx, a, 0); KReturn(value); } }
static void KStackReturnTypeCheck(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd, KClass *reqType, KClass *thisClass) { if(!kMethod_Is(SmartReturn, mtd)) { KClass *returnType = kMethod_GetReturnType(mtd); returnType = returnType->realtype(kctx, returnType, thisClass); if(reqType == returnType || returnType->isSubType(kctx, returnType, reqType)) { if(KClass_Is(UnboxType, returnType) && !KClass_Is(UnboxType, reqType)) { KReturn(KLIB new_kObject(kctx, OnStack, returnType, sfp[K_RTNIDX].unboxValue)); } } else { ThrowTypeError(kctx, sfp, -1); } } }
//## @SmartReturn Object ResultSet.get(String n); static KMETHOD ResultSet_get(KonohaContext *kctx, KonohaStack *sfp) { KClass *retClass = KGetReturnType(sfp); if(retClass->typeId == KType_Int) { ResultSet_getInt(kctx, sfp); } else if(retClass->typeId == KType_String) { ResultSet_getString(kctx, sfp); } else if(KDefinedKonohaCommonModule() && retClass->typeId == KType_float) { ResultSet_getFloat(kctx, sfp); } else { kObject *returnValue = KLIB Knull(kctx, retClass); KStackSetUnboxValue(sfp[K_RTNIDX].unboxValue, kObject_Unbox(returnValue)); KReturn(returnValue); } }
//## Token[] Stmt.getTokenList(symbol keyword, Token[] def); static KMETHOD Stmt_getTokenList(KonohaContext *kctx, KonohaStack *sfp) { kStmt *stmt = sfp[0].asStmt; ksymbol_t keyword = (ksymbol_t)sfp[1].intValue; kArray *def = sfp[2].asArray; kTokenArray *tokenList = (kTokenArray *)kStmt_GetObject(kctx, stmt, keyword, def); kTokenArray *ret; if(!IS_Array(tokenList)) { ret = new_(TokenArray, 0, OnStack); KLIB kArray_Add(kctx, ret, tokenList); } else { ret = tokenList; } KReturn(ret); }
//## String String.new(Bytes ba, String charset); static KMETHOD String_new_fromBytes_withSpecifiedDecode(KonohaContext *kctx, KonohaStack *sfp) { kBytes *ba = sfp[1].asBytes; kString *charset = sfp[2].asString; kString *s = TS_EMPTY; if(ba->bytesize != 0) { // At this point, we assuem 'ba' is null terminated. DBG_ASSERT(ba->buf[ba->bytesize] == '\0'); KMakeTrace(trace, sfp); KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KBuffer_convertCharset(kctx, &wb, kString_text(charset), "UTF-8", ba->buf, ba->bytesize, trace); s = KLIB KBuffer_Stringfy(kctx, &wb, OnStack, StringPolicy_FreeKBuffer); } KReturn(s); }
static KMETHOD Array_newArray(KonohaContext *kctx, KonohaStack *sfp) { kArrayVar *a = (kArrayVar *)sfp[0].asObject; size_t asize = (size_t)sfp[1].intValue; a->bytemax = asize * sizeof(uintptr_t); kArray_SetSize(a, asize); a->ObjectItems = (kObject**)KCalloc_UNTRACE(a->bytemax, 1); if(!kArray_isUnboxData(a)) { size_t i; kObject *null = KLIB Knull(kctx, CT_(O_p0(a))); for(i = 0; i < asize; i++) { KFieldSet(a, a->ObjectItems[i], null); } } KReturn(a); }
//## method Array<T> Array.concat(Array<T> a1); static KMETHOD Array_concat(KonohaContext *kctx, KonohaStack *sfp) { kArray *a0 = sfp[0].asArray; kArray *a1 = sfp[1].asArray; size_t i; if(kArray_isUnboxData(a1)) { for (i = 0; i < kArray_size(a1); i++){ UnboxArray_Add(kctx, a0, a1->unboxItems[i]); } } else { for (i = 0; i < kArray_size(a1); i++){ KLIB kArray_Add(kctx, a0, a1->ObjectItems[i]); } } KReturn(a0); }
static KMETHOD TypeCheck_Getter(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck(stmt, expr, gma, reqty); kToken *tkN = expr->cons->TokenItems[0]; ksymbol_t fn = tkN->resolvedSymbol; kExpr *self = SUGAR kStmt_TypeCheckExprAt(kctx, stmt, expr, 1, gma, TY_var, 0); kNameSpace *ns = Stmt_ns(stmt); if(self != K_NULLEXPR) { kMethod *mtd = KLIB kNameSpace_GetGetterMethodNULL(kctx, ns, self->ty, fn, TY_var); if(mtd != NULL) { KFieldSet(expr->cons, expr->cons->MethodItems[0], mtd); KReturn(SUGAR kStmtkExpr_TypeCheckCallParam(kctx, stmt, expr, mtd, gma, reqty)); } SUGAR kStmt_Message2(kctx, stmt, tkN, ErrTag, "undefined field: %s", S_text(tkN->text)); } }
static KMETHOD Statement_CStyleFor(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck(stmt, ns, reqc); int KSymbol_InitNode = KSymbol_("init"), KSymbol_IteratorNode = KSymbol_("Iterator"); KDump(stmt); kNode *initNode = SUGAR TypeCheckNodeByName(kctx, stmt, KSymbol_InitNode, ns, KClass_void, TypeCheckPolicy_AllowEmpty); if(initNode != NULL) { kNode_Set(OpenBlock, initNode, true); } SUGAR TypeCheckNodeByName(kctx, stmt, KSymbol_IteratorNode, ns, KClass_void, TypeCheckPolicy_AllowEmpty); SUGAR TypeCheckNodeByName(kctx, stmt, KSymbol_ExprPattern, ns, KClass_Boolean, 0); kNode_Set(CatchContinue, stmt, true); // set before TypeCheckAll kNode_Set(CatchBreak, stmt, true); //kNode_Set(RedoLoop, stmt, true); SUGAR TypeCheckNodeByName(kctx, stmt, KSymbol_BlockPattern, ns, KClass_void, 0); KReturn(kNode_Type(kctx, stmt, KNode_For, KType_void)); }
static KMETHOD TypeCheck_Defined(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck(stmt, expr, gma, reqty); size_t i; kbool_t isDefined = true; SugarContext *sugarContext = GetSugarContext(kctx); int popIsBlockingErrorMessage = sugarContext->isBlockedErrorMessage; sugarContext->isBlockedErrorMessage = true; for(i = 1; i < kArray_size(expr->cons); i++) { kExpr *typedExpr = SUGAR kStmt_TypeCheckExprAt(kctx, stmt, expr, i, gma, TY_var, TypeCheckPolicy_ALLOWVOID); if(typedExpr == K_NULLEXPR) { isDefined = false; break; } } sugarContext->isBlockedErrorMessage = popIsBlockingErrorMessage; KReturn(SUGAR kExpr_SetUnboxConstValue(kctx, expr, TY_boolean, isDefined)); }
static KMETHOD TypeCheck_Defined(KonohaContext *kctx, KonohaStack *sfp) { VAR_TypeCheck2(stmt, expr, ns, reqc); size_t i; kbool_t isDefined = true; KParserContext *sugarContext = KGetParserContext(kctx); int popIsBlockingErrorMessage = sugarContext->isBlockedErrorMessage; sugarContext->isBlockedErrorMessage = true; for(i = 1; i < kArray_size(expr->NodeList); i++) { kNode *typedNode = SUGAR TypeCheckNodeAt(kctx, expr, i, ns, KClass_INFER, TypeCheckPolicy_AllowVoid); if(kNode_IsError(typedNode)) { isDefined = false; break; } } sugarContext->isBlockedErrorMessage = popIsBlockingErrorMessage; KReturn(SUGAR kNode_SetUnboxConst(kctx, expr, KType_Boolean, isDefined)); }
//## Bytes String.toBytes(); static KMETHOD String_toBytes(KonohaContext *kctx, KonohaStack *sfp) { kString* thisString = sfp[0].asString; size_t size = kString_size(thisString); if(PLATAPI I18NModule.isSystemCharsetUTF8(kctx)) { KReturn(new_kBytes(kctx, OnStack, KGetReturnType(sfp), kString_text(thisString), size)); } else { KMakeTrace(trace, sfp); KBuffer wb; KLIB KBuffer_Init(&(kctx->stack->cwb), &wb); KBuffer_convertCharset(kctx, &wb, I18NAPI systemCharset, "UTF-8", kString_text(thisString), size, trace); KReturnWith( new_kBytes(kctx, OnStack, KGetReturnType(sfp), KLIB KBuffer_text(kctx, &wb, NonZero), KBuffer_bytesize(&wb)), KLIB KBuffer_Free(&wb) ); } }
//## buffereventCBArg buffereventCBArg.new(Func[void, int, Object arg] cb, Object cbArg); static KMETHOD buffereventCBArg_new(KonohaContext *kctx, KonohaStack *sfp) { struct buffereventCBArg *bcbarg = (struct buffereventCBArg *) sfp[0].asObject; kObjectVar *cbArg = sfp[4].asObjectVar; //deliver callback method bcbarg->kctx = kctx; enum e_buffereventCB i; for (i = BEV_ReadCB; i < NUM_BuffereventCB; i++){ kFunc *cb = sfp[i + 1].asFunc; KFieldSet(bcbarg, bcbarg->kcb[i], cb); } /* !!ATTENTION!! 'bcbarg->cbev' will be set in cbufferevent_setcb() function */ KFieldSet(bcbarg, bcbarg->arg, cbArg); KReturn(bcbarg); }
//## @Const method String String.replace(RegExp searchvalue, String newvalue); static KMETHOD String_replace(KonohaContext *kctx, KonohaStack *sfp) { kString *s0 = sfp[0].asString; kRegExp *re = sfp[1].asRegExp; const char* fmttext = S_text(sfp[2].asString); size_t fmtlen = S_size(sfp[2].asString); kString *s = s0; if(IS_NOTNULL(re) && S_size(re->pattern) > 0) { KGrowingBuffer wb; KLIB Kwb_Init(&(kctx->stack->cwb), &wb); const char *str = S_text(s0); // necessary const char *base = str; const char *eos = str + S_size(s0); // end of str kregmatch_t pmatch[KREGEXP_MATCHSIZE+1]; int isGlobalOption = RegExp_isGlobal(re); do { if(str >= eos) break; int res = pcre_regexec(kctx, re->reg, str, KREGEXP_MATCHSIZE, pmatch, re->eflags); if(res != 0) { // TODO //LOG_regex(kctx, sfp, res, re, str); break; } size_t len = pmatch[0].rm_eo; if(pmatch[0].rm_so > 0) { KLIB Kwb_write(kctx, &wb, str, pmatch[0].rm_so); } size_t matched = knh_regexp_Matched(pmatch, KREGEXP_MATCHSIZE); if(len > 0) { Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); str += len; } else { if(str == base) { // 0-length match at head of string Kwb_writeRegexFormat(kctx, &wb, fmttext, fmtlen, base, pmatch, matched); } break; } } while(isGlobalOption); KLIB Kwb_write(kctx, &wb, str, strlen(str)); // write out remaining string s = Kwb_newString(kctx, OnStack, &wb); // close cwb KLIB Kwb_Free(&wb); } KReturn(s); }
//## 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); }
//## Date Date.new(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds); static KMETHOD Date_new3(KonohaContext *kctx, KonohaStack *sfp) { struct kDateVar *d = (struct kDateVar *)sfp[0].asDate; struct tm lt = {}; if(sfp[1].intValue < 100) { lt.tm_year = sfp[1].intValue; } else { lt.tm_year = sfp[1].intValue - 1900; } lt.tm_mon = sfp[2].intValue; lt.tm_mday = sfp[3].intValue; lt.tm_hour = sfp[4].intValue; lt.tm_min = sfp[5].intValue; lt.tm_sec = sfp[6].intValue; d->tv.tv_sec = mktime(<); d->tv.tv_usec = sfp[7].intValue * 1000; KReturn((kObject *)d); }
static KMETHOD Array_newList(KonohaContext *kctx, KonohaStack *sfp) { kArrayVar *a = (kArrayVar *)sfp[0].asObject; size_t i = 0; KonohaStack *p = sfp+1; if(kArray_isUnboxData(a)) { for(i = 0; p + i < kctx->esp; i++) { a->unboxItems[i] = p[i].unboxValue; } } else { for(i = 0; p + i < kctx->esp; i++) { KFieldSet(a, a->ObjectItems[i], p[i].asObject); } } kArray_SetSize(a, i); DBG_ASSERT(a->bytesize <= a->bytemax); KReturn(a); }
static KMETHOD Expression_BinarySugar(KonohaContext *kctx, KonohaStack *sfp) { VAR_Expression(stmt, tokenList, beginIdx, operatorIdx, endIdx); kToken *opToken = tokenList->TokenItems[operatorIdx]; SugarSyntax *opSyntax = opToken->resolvedSyntaxInfo; if(opSyntax->macroParamSize == 2) { TokenSequence macro = {Stmt_nameSpace(stmt), tokenList}; TokenSequence_push(kctx, macro); MacroSet macroParam[] = { {SYM_("X"), tokenList, beginIdx, operatorIdx}, {SYM_("Y"), tokenList, operatorIdx+1, endIdx}, {0, NULL, 0, 0}, }; macro.TargetPolicy.RemovingIndent = true; SUGAR TokenSequence_applyMacro(kctx, ¯o, opSyntax->macroDataNULL_OnList, 0, kArray_size(opSyntax->macroDataNULL_OnList), opSyntax->macroParamSize, macroParam); kExpr *expr = SUGAR kStmt_parseExpr(kctx, stmt, macro.tokenList, macro.beginIdx, macro.endIdx, NULL); TokenSequence_pop(kctx, macro); KReturn(expr); } }
static CMPIStatus _DefaultGI_returnInstance( const CMPIResult* self, const CMPIInstance* ci) { DefaultGI_Handle* handle = (DefaultGI_Handle*)(((DefaultGI_Result*)self)->hdl); const CMPIResult* result = handle->result; const CMPIObjectPath* cop; CMPIStatus status; if (!(cop = CMGetObjectPath(ci, &status))) return status; if (KMatch(cop, handle->cop)) { handle->found = 1; return result->ft->returnInstance(result, ci); } KReturn(OK); }
static KMETHOD Array_Pop(KonohaContext *kctx, KonohaStack *sfp) { kArray *a = sfp[0].asArray; if(kArray_size(a) == 0) KReturnDefaultValue(); size_t n = kArray_size(a) - 1; if(kArray_isUnboxData(a)) { uintptr_t v = a->unboxItems[n]; a->unboxItems[n] = 0; kArray_SetSize(a, n); KReturnUnboxValue(v); } else { struct _kAbstractArray *a2 = (struct _kAbstractArray *)a; kObject *value = a2->a.ObjectItems[n]; kObject** null = NULL; KFieldInit(a2, a2->a.ObjectItems[n], null); a2->a.bytesize = n * sizeof(uintptr_t); KReturn(value); } }
static KMETHOD Array_reverse(KonohaContext *kctx, KonohaStack *sfp) { kArray *a = sfp[0].asArray; size_t asize = kArray_size(a); size_t asize_half = asize / 2; size_t i; if(kArray_isUnboxData(a)) { for(i = 0; i != asize_half; ++i) { uintptr_t temp = a->unboxItems[asize - 1 - i]; a->unboxItems[asize - 1 - i] = a->unboxItems[i]; a->unboxItems[i] = temp; } } else { for(i = 0; i != asize_half; ++i){ kObject *temp = a->ObjectItems[asize - 1 - i]; KFieldSet(a, a->ObjectItems[asize - 1 - i], a->ObjectItems[i]); KFieldSet(a, a->ObjectItems[i], temp); } } KReturn(a); }