Attach modules in C extensions when using krk_makeClass

This commit is contained in:
K Lange 2021-02-24 23:02:50 +09:00
parent 8e1f5f0565
commit 77b7f3ab22
2 changed files with 8 additions and 2 deletions

View File

@ -41,12 +41,14 @@ static KrkValue _class_to_str(int argc, KrkValue argv[], int hasKw) {
KrkValue module = NONE_VAL();
krk_tableGet(&AS_CLASS(argv[0])->fields, OBJECT_VAL(S("__module__")), &module);
int includeModule = !(IS_NONE(module) || (IS_STRING(module) && AS_STRING(module) == S("__builtins__")));
size_t allocSize = sizeof("<class ''>") + AS_CLASS(argv[0])->name->length;
if (IS_STRING(module)) allocSize += AS_STRING(module)->length + 1;
char * tmp = malloc(allocSize);
size_t l = snprintf(tmp, allocSize, "<class '%s%s%s'>",
IS_STRING(module) ? AS_CSTRING(module) : "",
IS_STRING(module) ? "." : "",
includeModule ? AS_CSTRING(module) : "",
includeModule ? "." : "",
AS_CLASS(argv[0])->name->chars);
KrkString * out = krk_copyString(tmp,l);
free(tmp);

View File

@ -421,6 +421,10 @@ KrkClass * krk_makeClass(KrkInstance * module, KrkClass ** _class, const char *
krk_push(OBJECT_VAL(*_class));
/* Bind it */
krk_attachNamedObject(&module->fields,name,(KrkObj*)*_class);
/* Now give it a __module__ */
KrkValue moduleName = NONE_VAL();
krk_tableGet(&module->fields, OBJECT_VAL(S("__name__")), &moduleName);
krk_attachNamedValue(&(*_class)->fields,"__module__",moduleName);
krk_pop();
}
krk_pop();