static attribute to class added

This commit is contained in:
Thakee Nathees 2022-06-07 00:08:12 +05:30
parent 0e355ed6c4
commit ca0fb6b54d
8 changed files with 33 additions and 7 deletions

View File

@ -36,9 +36,9 @@ IGNORE_FILES = (
"cli/argparse.h", ## FIXME: collect all thirdparty files.
## Contain longer lines. I shoule add something like static-check-off,
## static-check-on like clang or just use clang.
"src/libs/ext_term.c",
"src/libs/std_term.c",
## Contain extendedted ascii characters that raise UnicodeDecodeError.
"src/libs/ext_term.pk",
"src/libs/std_term.pk",
)
## A list of directory, contains C source files to perform static checks.

View File

@ -125,6 +125,7 @@ static inline bool validateCond(PKVM* vm, bool condition, const char* err) {
VALIDATE_ARG_OBJ(Closure, OBJ_CLOSURE, "closure")
VALIDATE_ARG_OBJ(Fiber, OBJ_FIBER, "fiber")
VALIDATE_ARG_OBJ(Class, OBJ_CLASS, "class")
VALIDATE_ARG_OBJ(Module, OBJ_MODULE, "module")
/*****************************************************************************/
/* SHARED FUNCTIONS */
@ -147,7 +148,6 @@ void initializeModule(PKVM* vm, Module* module, bool is_main) {
if (is_main) {
// TODO: consider static string "@main" stored in PKVM. to reduce
// allocations everytime here.
ASSERT(module->name == NULL, OOPS);
name = newString(vm, "@main");
module->name = name;
vmPushTempRef(vm, &name->_super); // _main.
@ -2112,6 +2112,9 @@ Var varGetAttrib(PKVM* vm, Var on, String* attrib) {
}
}
Var value = mapGet(cls->static_attribs, VAR_OBJ(attrib));
if (!IS_UNDEF(value)) return value;
for (int i = 0; i < (int)cls->methods.count; i++) {
Closure* method_ = cls->methods.data[i];
ASSERT(method_->fn->is_method, OOPS);
@ -2187,6 +2190,12 @@ void varSetAttrib(PKVM* vm, Var on, String* attrib, Var value) {
UNREACHABLE(); // Functions aren't first class objects.
break;
case OBJ_CLASS: {
Class* cls = (Class*) obj;
mapSet(vm, cls->static_attribs, VAR_OBJ(attrib), value);
return;
}
case OBJ_INST: {
Instance* inst = (Instance*)obj;

View File

@ -246,6 +246,7 @@ static void popMarkedObjectsInternal(Object* obj, PKVM* vm) {
markObject(vm, &cls->owner->_super);
markObject(vm, &cls->ctor->_super);
markObject(vm, &cls->name->_super);
markObject(vm, &cls->static_attribs->_super);
markClosureBuffer(vm, &cls->methods);
vm->bytes_allocated += sizeof(Closure) * cls->methods.capacity;
@ -520,6 +521,7 @@ Class* newClass(PKVM* vm, const char* name, int length,
vmPushTempRef(vm, &cls->_super); // class.
pkClosureBufferInit(&cls->methods);
cls->static_attribs = newMap(vm);
cls->class_of = PK_INSTANCE;
cls->super_class = super;

View File

@ -543,6 +543,9 @@ struct Class {
// A buffer of methods of the class.
pkClosureBuffer methods;
// Static attributes of the class.
Map* static_attribs;
// Allocater and de-allocator functions for native types.
// For script/ builtin types it'll be NULL.
pkNewInstanceFn new_fn;

View File

@ -18,10 +18,8 @@ void registerModulePath(PKVM* vm);
void registerModuleOS(PKVM* vm);
void registerModuleJson(PKVM* vm);
void registerModuleDummy(PKVM* vm);
// Extra libraries.
void registerModuleTerm(PKVM* vm);
void cleanupModuleTerm(PKVM* vm);
// Registers the modules.
@ -34,8 +32,8 @@ void registerLibs(PKVM* vm) {
registerModuleOS(vm);
registerModuleJson(vm);
registerModuleDummy(vm);
registerModuleTerm(vm);
}
// Cleanup the modules.

View File

@ -5,6 +5,20 @@
## TODO: write some tests here.
## No enums supports (yet?)
class Day end
Day.SUNDAY = 0
Day.MONDAY = 1
Day.TUESDAY = 2
Day.WEDNESDAY = 3
Day.THURSDAY = 4
Day.FRIDAY = 5
Day.SATURDAY = 6
assert(Day.SUNDAY == 0)
assert(Day.FRIDAY == 5)
###############################################################################
## INHERITANCE
###############################################################################