static kbool_t konoha_defineMethod(KonohaContext *kctx, kNameSpace *ns, KTraceInfo *trace) { KDEFINE_CLASS defKonohaFactory = {0}; SETSTRUCTNAME(defKonohaFactory, KonohaFactory); defKonohaFactory.init = kKonohaFactory_Init; defKonohaFactory.free = kKonohaFactory_Free; KDEFINE_CLASS defKonoha = {0}; SETSTRUCTNAME(defKonoha, Konoha); defKonoha.init = kKonoha_Init; defKonoha.free = kKonoha_Free; KClass *cKonohaFactory = KLIB KClass_define(kctx, ns->packageId, NULL, &defKonohaFactory, trace); KClass *cKonoha = KLIB KClass_define(kctx, ns->packageId, NULL, &defKonoha, trace); int KType_KonohaFactory = cKonohaFactory->typeId, KType_Konoha = cKonoha->typeId; KDEFINE_METHOD MethodData[] = { kMethod_Public, _F(KonohaFactory_new), KType_KonohaFactory, KType_KonohaFactory, KMethodName_("new"), 0, kMethod_Public, _F(KonohaFactory_loadModule), KType_Boolean, KType_KonohaFactory, KMethodName_("loadModule"), 1, KType_String, KFieldName_("name"), kMethod_Public, _F(KonohaFactory_create), KType_Konoha, KType_KonohaFactory, KMethodName_("create"), 0, kMethod_Public, _F(Konoha_loadScript), KType_Boolean, KType_Konoha, KMethodName_("loadScript"), 1, KType_String, KFieldName_("filename"), kMethod_Public, _F(Konoha_eval), KType_Konoha, KType_Konoha, KMethodName_("eval"), 1, KType_String, KFieldName_("script"), DEND, }; KLIB kNameSpace_LoadMethodData(kctx, ns, MethodData, trace); return true; }
void test_gc(KonohaContext *kctx) { #define KClass_Dummy ct int i, j; KClass *ct = KLIB KClass_define(kctx, 0, NULL, &DummyDef, 0); /* small size */ for (i = 0; i < 10; ++i) { for (j = 0; j < 100; ++j) { kDummy *dummy = new_(Dummy, 0xdeadbeaf, NULL); assert(__Init__ == dummy->x+1); } assert(__Init__ == (i+1) * 100); assert(__trace__ == -1); PLATAPI GCModule.ScheduleGC(kctx, NULL); } int small_object_count = __Init__; /* middle size */ for (i = 0; i < 100; ++i) { for (j = 0; j < 1000; ++j) { kDummy *dummy = new_(Dummy, 0xdeadbeaf, NULL); assert(__Init__ == dummy->x+1); } assert(__Init__ == (i+1) * 1000 + small_object_count); assert(__trace__ == -1); PLATAPI GCModule.ScheduleGC(kctx, NULL); } }
void MODSUGAR_Init(KonohaContext *kctx, KonohaContextVar *ctx) { KParserModel *mod = (KParserModel *)KCalloc_UNTRACE(sizeof(KParserModel), 1); mod->h.name = "sugar"; mod->h.allocSize = sizeof(KParserModel); mod->h.setupModelContext = SugarModule_Setup; KLIB KRuntime_SetModule(kctx, ParserModelIndex, (KRuntimeModel *)mod, 0); KonohaLibVar *l = (KonohaLibVar *)ctx->klib; l->kNameSpace_GetClassByFullName = kNameSpace_GetClassByFullName; l->kNameSpace_DefineClass = kNameSpace_DefineClass; l->kNameSpace_LoadMethodData = kNameSpace_LoadMethodData; l->kNameSpace_SetConstData = kNameSpace_SetConstData; l->kNameSpace_LoadConstData = kNameSpace_LoadConstData; l->kNameSpace_GetGetterMethodNULL = kNameSpace_GetGetterMethodNULL; l->kNameSpace_GetSetterMethodNULL = kNameSpace_GetSetterMethodNULL; l->kNameSpace_GetCoercionMethodNULL = kNameSpace_GetCoercionMethodNULL; l->kNameSpace_GetMethodByParamSizeNULL = kNameSpace_GetMethodByParamSizeNULL; l->kNameSpace_GetMethodBySignatureNULL = kNameSpace_GetMethodBySignatureNULL; l->kMethod_DoLazyCompilation = kMethod_DoLazyCompilation; l->kNameSpace_FreeSugarExtension = kNameSpace_FreeSugarExtension; l->Konoha_LoadScript = Konoha_LoadScript; l->Konoha_Eval = Konoha_Eval; l->kMethod_GenCode = kMethod_GenCode; l->kMethod_SetFunc = kMethod_SetFunc; KDEFINE_CLASS defSymbol = {0}; defSymbol.structname = "Symbol"; defSymbol.typeId = KTypeAttr_NewId; defSymbol.cflag = KClassFlag_int; defSymbol.init = KClass_(KType_Int)->init; defSymbol.unbox = KClass_(KType_Int)->unbox; defSymbol.format = kSymbol_format; KDEFINE_CLASS defSyntax = {0}; SETSTRUCTNAME(defSyntax, Syntax); defSyntax.init = kSyntax_Init; //defSyntax.format = kSyntax_format; KDEFINE_CLASS defToken = {0}; SETSTRUCTNAME(defToken, Token); defToken.init = kToken_Init; defToken.reftrace = kToken_Reftrace; defToken.format = kToken_format; KDEFINE_CLASS defNode = {0}; SETSTRUCTNAME(defNode, Node); defNode.init = kUntypedNode_Init; defNode.reftrace = kUntypedNode_Reftrace; defNode.format = kUntypedNode_format; InitNodeClass(kctx, mod); mod->cSymbol = KLIB KClass_define(kctx, PackageId_sugar, NULL, &defSymbol, 0); mod->cSyntax = KLIB KClass_define(kctx, PackageId_sugar, NULL, &defSyntax, 0); mod->cToken = KLIB KClass_define(kctx, PackageId_sugar, NULL, &defToken, 0); mod->cNode = KLIB KClass_define(kctx, PackageId_sugar, NULL, &defNode, 0); mod->cTokenArray = KClass_p0(kctx, KClass_Array, mod->cToken->typeId); KLIB Knull(kctx, mod->cToken); KLIB Knull(kctx, mod->cNode); SugarModule_Setup(kctx, &mod->h, 0); KDEFINE_INT_CONST ClassData[] = { // konoha defined class {"void", VirtualType_KClass, (uintptr_t)KClass_void}, {"boolean", VirtualType_KClass, (uintptr_t)KClass_Boolean}, {"int", VirtualType_KClass, (uintptr_t)KClass_Int}, {"String", VirtualType_KClass, (uintptr_t)KClass_String}, {"Func", VirtualType_KClass, (uintptr_t)KClass_Func}, {"System", VirtualType_KClass, (uintptr_t)KClass_System}, {NULL}, }; kNameSpace_LoadConstData(kctx, KNULL(NameSpace), KConst_(ClassData), 0); l->Tokenize = Tokenize; l->ApplyMacroData = ApplyMacroData; l->SetMacroData = SetMacroData; l->Preprocess = Preprocess; l->EvalTokenList = EvalTokenList; l->ParseTypePattern = ParseTypePattern; l->kToken_ToBraceGroup = kToken_ToBraceGroup; l->kUntypedNode_AddParsedObject = kUntypedNode_AddParsedObject; l->FindEndOfStatement = FindEndOfStatement; l->kUntypedNode_ParseFlag = kUntypedNode_ParseFlag; l->kUntypedNode_GetToken = kUntypedNode_GetToken; l->kUntypedNode_GetNode = kUntypedNode_GetNode; l->kUntypedNode_SetConst = kUntypedNode_SetConst; l->kUntypedNode_SetUnboxConst = kUntypedNode_SetUnboxConst; l->kUntypedNode_SetVariable = kUntypedNode_SetVariable; l->TypeCheckNodeAt = TypeCheckNodeAt; l->TypeCheckNodeByName = TypeCheckNodeByName; l->TypeCheckMethodParam = TypeCheckMethodParam; l->new_MethodNode = new_MethodNode; l->AddLocalVariable = AddLocalVariable; l->kUntypedNode_DeclType = kUntypedNode_DeclType; l->TypeVariableNULL = TypeVariableNULL; l->kNameSpace_DefineSyntax = kNameSpace_DefineSyntax; l->kNameSpace_GetSyntax = kNameSpace_GetSyntax; l->kSyntax_AddPattern = kSyntax_AddPattern; l->kNameSpace_AddSyntax = kNameSpace_AddSyntax; l->kNameSpace_UseDefaultVirtualMachine = kNameSpace_UseDefaultVirtualMachine; l->kUntypedNode_InsertAfter = kUntypedNode_InsertAfter; l->kUntypedNode_AddNode = kUntypedNode_AddNode; l->kUntypedNode_Op = kUntypedNode_Op; l->ParseSyntaxNode = ParseSyntaxNode; l->ParseNode = ParseNode; l->ParseNewNode = ParseNewNode; l->AppendParsedNode = AppendParsedNode; l->kToken_ToError = kToken_ToError; l->MessageNode = MessageNode; l->VisitNode = VisitNode; #ifndef USE_SMALLBUILD l->dumpToken = dumpToken; l->dumpTokenArray = dumpTokenArray; #endif DefineDefaultSyntax(kctx, KNULL(NameSpace)); }