From c955b8b54c5178b32ad091d11887b1781d0db55e Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Sun, 20 Aug 2023 16:52:54 +0100 Subject: [PATCH 1/2] Redo internals to change property to attribute --- docs/docs/classes.md | 36 +++---- examples/templateEngine.du | 2 +- src/optionals/argparse/argparse-source.h | 5 +- src/optionals/argparse/argparse.du | 5 +- src/optionals/http/http.c | 6 +- src/vm/compiler.c | 84 +++++++-------- src/vm/datatypes/copy.c | 14 +-- src/vm/datatypes/instance.c | 80 ++++++++++----- src/vm/debug.c | 32 +++--- src/vm/memory.c | 16 +-- src/vm/object.c | 12 +-- src/vm/object.h | 8 +- src/vm/opcodes.h | 16 +-- src/vm/vm.c | 124 +++++++++++------------ tests/classes/getAttributes.du | 23 +++-- 15 files changed, 246 insertions(+), 217 deletions(-) diff --git a/docs/docs/classes.md b/docs/docs/classes.md index b387d750..0cc0077c 100644 --- a/docs/docs/classes.md +++ b/docs/docs/classes.md @@ -37,8 +37,8 @@ A big part of the OOP paradigm is encapsulation, the fact that the outside inter internal to the class do not need to be known to the user of the interface. Dictu manages this concept much the same as other languages through access levels. Unlike other languages, Dictu only has two access levels, `public` or `private`. If a method or instance variable is marked as `private` it is only accessible from within the class itself. To mark an instance variable as private it can be done in two -ways, via [implicit properties](#implicit-properties) or by marking the property as private (note this must be done before defining the -property otherwise it will be publically visible). To mark a method as private, preface the name with `private`. +ways, via [implicit attributes](#implicit-attributes) or by marking the attribute as private (note this must be done before defining the +attribute otherwise it will be publicly visible). To mark a method as private, preface the name with `private`. ```js class SomeClass { @@ -54,7 +54,7 @@ class SomeClass { } print(SomeClass(10).getX()); // 10 -print(SomeClass(10).x); // Cannot access private property 'x' on 'SomeClass' instance. +print(SomeClass(10).x); // Cannot access private attribute 'x' on 'SomeClass' instance. ``` ## Constructor @@ -72,9 +72,9 @@ class SomeClass { SomeClass("Object created!"); // Object created! ``` -### Implicit properties +### Implicit Attributes -Dictu actually has a way to define properties on the object without explicitly setting each variable passed into the constructor on the object through `this`. +Dictu actually has a way to define attributes on the object without explicitly setting each variable passed into the constructor on the object through `this`. If `var` is used the instance variable has public visibility, if `private` is used the instance variable has private visibility. ```cs @@ -92,8 +92,8 @@ The `var` or `private` keywords are optional on the constructor parameters, and ```cs class SomeClass { init(var a, b, c, var d, private e) { - // b and c are not set as instance properties - // e is set as a private property + // b and c are not set as instance attributes + // e is set as a private attribute } } @@ -223,10 +223,10 @@ var myObject = Test(); print(myObject.hasAttribute("x")); // true print(myObject.hasAttribute("y")); // false -print(myObject.z); // Undefined property 'z'. +print(myObject.z); // Undefined attribute 'z'. ``` -### getAttribute(String) -> value +### getAttribute(String) -> Value Sometimes in Dictu we may wish to access an attribute of an object without knowing the attribute until runtime. We can do this via the `getAttribute` method. This method takes a string and an optional default value and returns either the attribute value or the default value (if there is no attribute and no default value, nil is returned). @@ -247,9 +247,9 @@ print(myObject.getAttribute("y", 100)); // 100 print(myObject.getAttribute("y")); // nil ``` -### getAttributes() -> List +### getAttributes() -> Dict -The `getAttributes` method returns all public attributes on the given instance of a class. +The `getAttributes` method returns all class variables / constants, public methods and public properties. ```cs class Test { @@ -259,14 +259,14 @@ class Test { } var myObject = Test(); -print(myObject.getAttributes()); // ["x"] +print(myObject.getAttributes()); // {"fields": [], "methods": ["init"], "attributes": ["_class", "x"]} ``` -### setAttribute(String, value) +### setAttribute(String, Value) Similar concept to `getAttribute` however this allows us to set an attribute on an instance. -Note: Will set a property with public visibility. +Note: Will set a attribute with public visibility. ```cs class Test { @@ -282,10 +282,10 @@ print(myObject.x); // 100 ### Optional Chaining -Optional chaining allows you to read a property or method of an instance without explicitly having to check for `nil` before +Optional chaining allows you to read a attribute or method of an instance without explicitly having to check for `nil` before attempting to access. -**Note:** If the left hand value is not nil the property / method **must** still exist when attempting to access otherwise a runtime error will occur. +**Note:** If the left hand value is not nil the attribute / method **must** still exist when attempting to access otherwise a runtime error will occur. ```js class Test { @@ -303,8 +303,8 @@ class Test { // Here there is no explicit nil check. print(Test().someMethod()?.someOtherMethod()); // nil -// If the operand is not nil the method / property must exist -print(Test()?.unknownMethod()); // Undefined property 'unknownMethod'. +// If the operand is not nil the method / attribute must exist +print(Test()?.unknownMethod()); // Undefined attribute 'unknownMethod'. ``` ### _class diff --git a/examples/templateEngine.du b/examples/templateEngine.du index 7e12c192..27d27e93 100644 --- a/examples/templateEngine.du +++ b/examples/templateEngine.du @@ -11,7 +11,7 @@ class Template { // matches the fields in the class to the template // fields and replaces them with the class values. render() { - const classAttrs = this.klass.getAttributes()["properties"]; + const classAttrs = this.klass.getAttributes()["attributes"]; var rendered = this.tmpl; classAttrs.forEach(def(attr) => { diff --git a/src/optionals/argparse/argparse-source.h b/src/optionals/argparse/argparse-source.h index ef25e5e6..df5e9264 100644 --- a/src/optionals/argparse/argparse-source.h +++ b/src/optionals/argparse/argparse-source.h @@ -6,15 +6,12 @@ "}\n" \ "\n" \ "class Parser {\n" \ -" private name;\n" \ -" private desc;\n" \ -" private userUsage;\n" \ " private args;\n" \ "\n" \ " var preArgs = [];\n" \ " var required = [];\n" \ "\n" \ -" init(var name, var desc, var userUsage) {\n" \ +" init(private name, private desc, private userUsage) {\n" \ " this.args = Args(name, desc);\n" \ " }\n" \ "\n" \ diff --git a/src/optionals/argparse/argparse.du b/src/optionals/argparse/argparse.du index 520c34dc..79912887 100644 --- a/src/optionals/argparse/argparse.du +++ b/src/optionals/argparse/argparse.du @@ -6,15 +6,12 @@ class Args { } class Parser { - private name; - private desc; - private userUsage; private args; var preArgs = []; var required = []; - init(var name, var desc, var userUsage) { + init(private name, private desc, private userUsage) { this.args = Args(name, desc); } diff --git a/src/optionals/http/http.c b/src/optionals/http/http.c index ecd61f14..3ba7d615 100644 --- a/src/optionals/http/http.c +++ b/src/optionals/http/http.c @@ -377,17 +377,17 @@ static ObjInstance *endRequest(DictuVM *vm, CURL *curl, Response response, bool ObjString *string = copyString(vm, "content", 7); push(vm, OBJ_VAL(string)); - tableSet(vm, &responseInstance->publicFields, string, OBJ_VAL(content)); + tableSet(vm, &responseInstance->publicAttributes, string, OBJ_VAL(content)); pop(vm); string = copyString(vm, "headers", 7); push(vm, OBJ_VAL(string)); - tableSet(vm, &responseInstance->publicFields, string, OBJ_VAL(response.headers)); + tableSet(vm, &responseInstance->publicAttributes, string, OBJ_VAL(response.headers)); pop(vm); string = copyString(vm, "statusCode", 10); push(vm, OBJ_VAL(string)); - tableSet(vm, &responseInstance->publicFields, string, NUMBER_VAL(response.statusCode)); + tableSet(vm, &responseInstance->publicAttributes, string, NUMBER_VAL(response.statusCode)); pop(vm); // Pop instance diff --git a/src/vm/compiler.c b/src/vm/compiler.c index c7180c26..05d494b3 100644 --- a/src/vm/compiler.c +++ b/src/vm/compiler.c @@ -698,86 +698,86 @@ static void dot(Compiler *compiler, LangToken previousToken, bool canAssign) { privatePropertyExists(identifier, compiler))) { if (canAssign && match(compiler, TOKEN_EQUAL)) { expression(compiler); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_PLUS_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_ADD); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_MINUS_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_SUBTRACT); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_MULTIPLY_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_MULTIPLY); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_DIVIDE_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_DIVIDE); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_AMPERSAND_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_AND); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_CARET_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_XOR); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_PIPE_EQUALS)) { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_OR); - emitBytes(compiler, OP_SET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_SET_PRIVATE_ATTRIBUTE, name); } else { - emitBytes(compiler, OP_GET_PRIVATE_PROPERTY, name); + emitBytes(compiler, OP_GET_PRIVATE_ATTRIBUTE, name); } } else { if (canAssign && match(compiler, TOKEN_EQUAL)) { expression(compiler); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_PLUS_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_ADD); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_MINUS_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_SUBTRACT); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_MULTIPLY_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_MULTIPLY); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_DIVIDE_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_DIVIDE); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_AMPERSAND_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_AND); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_CARET_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_XOR); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else if (canAssign && match(compiler, TOKEN_PIPE_EQUALS)) { - emitBytes(compiler, OP_GET_PROPERTY_NO_POP, name); + emitBytes(compiler, OP_GET_ATTRIBUTE_NO_POP, name); expression(compiler); emitByte(compiler, OP_BITWISE_OR); - emitBytes(compiler, OP_SET_PROPERTY, name); + emitBytes(compiler, OP_SET_ATTRIBUTE, name); } else { - emitBytes(compiler, OP_GET_PROPERTY, name); + emitBytes(compiler, OP_GET_ATTRIBUTE, name); } } } @@ -907,7 +907,7 @@ static void beginFunction(Compiler *compiler, Compiler *fnCompiler, FunctionType fnCompiler->function->propertyIndexes[i] = indexes[i]; } - emitBytes(fnCompiler, OP_SET_INIT_PROPERTIES, makeConstant(fnCompiler, OBJ_VAL(fnCompiler->function))); + emitBytes(fnCompiler, OP_SET_INIT_ATTRIBUTES, makeConstant(fnCompiler, OBJ_VAL(fnCompiler->function))); } if (fnCompiler->function->privatePropertyCount > 0) { @@ -922,7 +922,7 @@ static void beginFunction(Compiler *compiler, Compiler *fnCompiler, FunctionType fnCompiler->function->privatePropertyIndexes[i] = privateIndexes[i]; } - emitBytes(fnCompiler, OP_SET_PRIVATE_INIT_PROPERTIES, makeConstant(fnCompiler, OBJ_VAL(fnCompiler->function))); + emitBytes(fnCompiler, OP_SET_PRIVATE_INIT_ATTRIBUTES, makeConstant(fnCompiler, OBJ_VAL(fnCompiler->function))); } } @@ -2117,15 +2117,15 @@ static int getArgCount(uint8_t *code, const ValueArray constants, int ip) { case OP_SET_MODULE: case OP_GET_UPVALUE: case OP_SET_UPVALUE: - case OP_GET_PROPERTY: - case OP_GET_PRIVATE_PROPERTY: - case OP_GET_PROPERTY_NO_POP: - case OP_GET_PRIVATE_PROPERTY_NO_POP: - case OP_SET_PROPERTY: - case OP_SET_PRIVATE_PROPERTY: + case OP_GET_ATTRIBUTE: + case OP_GET_PRIVATE_ATTRIBUTE: + case OP_GET_ATTRIBUTE_NO_POP: + case OP_GET_PRIVATE_ATTRIBUTE_NO_POP: + case OP_SET_ATTRIBUTE: + case OP_SET_PRIVATE_ATTRIBUTE: case OP_SET_CLASS_VAR: - case OP_SET_INIT_PROPERTIES: - case OP_SET_PRIVATE_INIT_PROPERTIES: + case OP_SET_INIT_ATTRIBUTES: + case OP_SET_PRIVATE_INIT_ATTRIBUTES: case OP_GET_SUPER: case OP_METHOD: case OP_IMPORT: diff --git a/src/vm/datatypes/copy.c b/src/vm/datatypes/copy.c index a85cb29d..55c21b2d 100644 --- a/src/vm/datatypes/copy.c +++ b/src/vm/datatypes/copy.c @@ -68,10 +68,10 @@ ObjInstance *copyInstance(DictuVM* vm, ObjInstance *oldInstance, bool shallow) { push(vm, OBJ_VAL(instance)); if (shallow) { - tableAddAll(vm, &oldInstance->publicFields, &instance->publicFields); + tableAddAll(vm, &oldInstance->publicAttributes, &instance->publicAttributes); } else { - for (int i = 0; i <= oldInstance->publicFields.capacityMask; i++) { - Entry *entry = &oldInstance->publicFields.entries[i]; + for (int i = 0; i <= oldInstance->publicAttributes.capacityMask; i++) { + Entry *entry = &oldInstance->publicAttributes.entries[i]; if (entry->key != NULL) { Value val = entry->value; @@ -85,13 +85,13 @@ ObjInstance *copyInstance(DictuVM* vm, ObjInstance *oldInstance, bool shallow) { // Push to stack to avoid GC push(vm, val); - tableSet(vm, &instance->publicFields, entry->key, val); + tableSet(vm, &instance->publicAttributes, entry->key, val); pop(vm); } } - for (int i = 0; i <= oldInstance->privateFields.capacityMask; i++) { - Entry *entry = &oldInstance->privateFields.entries[i]; + for (int i = 0; i <= oldInstance->privateAttributes.capacityMask; i++) { + Entry *entry = &oldInstance->privateAttributes.entries[i]; if (entry->key != NULL) { Value val = entry->value; @@ -105,7 +105,7 @@ ObjInstance *copyInstance(DictuVM* vm, ObjInstance *oldInstance, bool shallow) { // Push to stack to avoid GC push(vm, val); - tableSet(vm, &instance->privateFields, entry->key, val); + tableSet(vm, &instance->privateAttributes, entry->key, val); pop(vm); } } diff --git a/src/vm/datatypes/instance.c b/src/vm/datatypes/instance.c index a0f7df5f..0952169e 100644 --- a/src/vm/datatypes/instance.c +++ b/src/vm/datatypes/instance.c @@ -32,7 +32,7 @@ static Value hasAttribute(DictuVM *vm, int argCount, Value *args) { } Value _; // Unused variable - if (tableGet(&instance->publicFields, AS_STRING(value), &_)) { + if (tableGet(&instance->publicAttributes, AS_STRING(value), &_)) { return TRUE_VAL; } @@ -40,15 +40,15 @@ static Value hasAttribute(DictuVM *vm, int argCount, Value *args) { return TRUE_VAL; } - // Check class for properties + // Check class for attributes ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, AS_STRING(value), &value)) { + if (tableGet(&klass->constants, AS_STRING(value), &value)) { return TRUE_VAL; } - if (tableGet(&klass->publicProperties, AS_STRING(value), &value)) { + if (tableGet(&klass->variables, AS_STRING(value), &value)) { return TRUE_VAL; } @@ -80,7 +80,7 @@ static Value getAttribute(DictuVM *vm, int argCount, Value *args) { ObjInstance *instance = AS_INSTANCE(args[0]); Value value; - if (tableGet(&instance->publicFields, AS_STRING(key), &value)) { + if (tableGet(&instance->publicAttributes, AS_STRING(key), &value)) { return value; } @@ -94,7 +94,7 @@ static Value getAttribute(DictuVM *vm, int argCount, Value *args) { ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicProperties, AS_STRING(key), &value)) { + if (tableGet(&klass->variables, AS_STRING(key), &value)) { return value; } @@ -104,6 +104,16 @@ static Value getAttribute(DictuVM *vm, int argCount, Value *args) { return defaultValue; } +static bool exists(ObjList *list, ObjString *search) { + for (int i = 0; i < list->values.count; ++i) { + if (valuesEqual(list->values.values[i], OBJ_VAL(search))) { + return true; + } + } + + return false; +} + static Value getAttributes(DictuVM *vm, int argCount, Value *args) { if (argCount > 0) { runtimeError(vm, "getAttributes() takes 0 arguments (%d given)", argCount); @@ -115,50 +125,70 @@ static Value getAttributes(DictuVM *vm, int argCount, Value *args) { ObjDict *dict = newDict(vm); push(vm, OBJ_VAL(dict)); - ObjList *classVariables = newList(vm); - push(vm, OBJ_VAL(classVariables)); + ObjList *fields = newList(vm); + push(vm, OBJ_VAL(fields)); ObjClass *klass = instance->klass; // Walk the inheritance chain while (klass != NULL) { - for (int i = 0; i < klass->publicProperties.capacityMask + 1; i++) { - if (klass->publicProperties.entries[i].key == NULL) { + for (int i = 0; i < klass->variables.capacityMask + 1; i++) { + if (klass->variables.entries[i].key == NULL) { + continue; + } + + if (exists(fields, klass->variables.entries[i].key)) { + continue; + } + + writeValueArray(vm, &fields->values, OBJ_VAL(klass->variables.entries[i].key)); + } + + for (int i = 0; i < klass->constants.capacityMask + 1; i++) { + if (klass->constants.entries[i].key == NULL) { + continue; + } + + if (exists(fields, klass->constants.entries[i].key)) { continue; } - writeValueArray(vm, &classVariables->values, OBJ_VAL(klass->publicProperties.entries[i].key)); + writeValueArray(vm, &fields->values, OBJ_VAL(klass->constants.entries[i].key)); } klass = klass->superclass; } - ObjString *cv = copyString(vm, "classVariables", 14); + ObjString *cv = copyString(vm, "fields", 6); push(vm, OBJ_VAL(cv)); - dictSet(vm, dict, OBJ_VAL(cv), OBJ_VAL(classVariables)); + dictSet(vm, dict, OBJ_VAL(cv), OBJ_VAL(fields)); - pop(vm); // "classVariables" string - pop(vm); // "classVariables" list + pop(vm); // "fields" string + pop(vm); // "fields" list - ObjList *properties = newList(vm); - push(vm, OBJ_VAL(properties)); + ObjList *attributes = newList(vm); + push(vm, OBJ_VAL(attributes)); - for (int i = 0; i < instance->publicFields.capacityMask + 1; i++) { - if (instance->publicFields.entries[i].key == NULL) { + for (int i = 0; i < instance->publicAttributes.capacityMask + 1; i++) { + if (instance->publicAttributes.entries[i].key == NULL) { + continue; + } + + if (exists(attributes, instance->publicAttributes.entries[i].key)) { continue; } - writeValueArray(vm, &properties->values, OBJ_VAL(instance->publicFields.entries[i].key)); + writeValueArray(vm, &attributes->values, OBJ_VAL(instance->publicAttributes.entries[i].key)); } - ObjString *pv = copyString(vm, "properties", 10); + ObjString *pv = copyString(vm, "attributes", 10); push(vm, OBJ_VAL(pv)); - dictSet(vm, dict, OBJ_VAL(pv), OBJ_VAL(properties)); + dictSet(vm, dict, OBJ_VAL(pv), OBJ_VAL(attributes)); - pop(vm); // "properties" string - pop(vm); // "properties" list + pop(vm); // "attributes" string + pop(vm); // "attributes" list ObjList *methods = newList(vm); push(vm, OBJ_VAL(methods)); @@ -198,7 +228,7 @@ static Value setAttribute(DictuVM *vm, int argCount, Value *args) { } ObjInstance *instance = AS_INSTANCE(args[0]); - tableSet(vm, &instance->publicFields, AS_STRING(key), value); + tableSet(vm, &instance->publicAttributes, AS_STRING(key), value); return NIL_VAL; } diff --git a/src/vm/debug.c b/src/vm/debug.c index 6c5f36e3..4b804912 100644 --- a/src/vm/debug.c +++ b/src/vm/debug.c @@ -163,24 +163,24 @@ int disassembleInstruction(Chunk *chunk, int offset) { return byteInstruction("OP_GET_UPVALUE", chunk, offset); case OP_SET_UPVALUE: return byteInstruction("OP_SET_UPVALUE", chunk, offset); - case OP_GET_PROPERTY: - return constantInstruction("OP_GET_PROPERTY", chunk, offset); - case OP_GET_PRIVATE_PROPERTY: - return constantInstruction("OP_GET_PRIVATE_PROPERTY", chunk, offset); - case OP_GET_PROPERTY_NO_POP: - return constantInstruction("OP_GET_PROPERTY_NO_POP", chunk, offset); - case OP_GET_PRIVATE_PROPERTY_NO_POP: - return constantInstruction("OP_GET_PRIVATE_PROPERTY_NO_POP", chunk, offset); - case OP_SET_PROPERTY: - return constantInstruction("OP_SET_PROPERTY", chunk, offset); - case OP_SET_PRIVATE_PROPERTY: - return constantInstruction("OP_SET_PRIVATE_PROPERTY", chunk, offset); + case OP_GET_ATTRIBUTE: + return constantInstruction("OP_GET_ATTRIBUTE", chunk, offset); + case OP_GET_PRIVATE_ATTRIBUTE: + return constantInstruction("OP_GET_PRIVATE_ATTRIBUTE", chunk, offset); + case OP_GET_ATTRIBUTE_NO_POP: + return constantInstruction("OP_GET_ATTRIBUTE_NO_POP", chunk, offset); + case OP_GET_PRIVATE_ATTRIBUTE_NO_POP: + return constantInstruction("OP_GET_PRIVATE_ATTRIBUTE_NO_POP", chunk, offset); + case OP_SET_ATTRIBUTE: + return constantInstruction("OP_SET_ATTRIBUTE", chunk, offset); + case OP_SET_PRIVATE_ATTRIBUTE: + return constantInstruction("OP_SET_PRIVATE_ATTRIBUTE", chunk, offset); case OP_SET_CLASS_VAR: return constantInstruction("OP_SET_CLASS_VAR", chunk, offset); - case OP_SET_INIT_PROPERTIES: - return constantInstruction("OP_SET_INIT_PROPERTIES", chunk, offset); - case OP_SET_PRIVATE_INIT_PROPERTIES: - return constantInstruction("OP_SET_PRIVATE_INIT_PROPERTIES", chunk, offset); + case OP_SET_INIT_ATTRIBUTES: + return constantInstruction("OP_SET_INIT_ATTRIBUTES", chunk, offset); + case OP_SET_PRIVATE_INIT_ATTRIBUTES: + return constantInstruction("OP_SET_PRIVATE_INIT_ATTRIBUTES", chunk, offset); case OP_GET_SUPER: return constantInstruction("OP_GET_SUPER", chunk, offset); case OP_EQUAL: diff --git a/src/vm/memory.c b/src/vm/memory.c index b775cb8e..836ac28a 100644 --- a/src/vm/memory.c +++ b/src/vm/memory.c @@ -104,8 +104,8 @@ static void blackenObject(DictuVM *vm, Obj *object) { grayTable(vm, &klass->publicMethods); grayTable(vm, &klass->privateMethods); grayTable(vm, &klass->abstractMethods); - grayTable(vm, &klass->publicProperties); - grayTable(vm, &klass->publicConstantProperties); + grayTable(vm, &klass->variables); + grayTable(vm, &klass->constants); break; } @@ -135,8 +135,8 @@ static void blackenObject(DictuVM *vm, Obj *object) { case OBJ_INSTANCE: { ObjInstance *instance = (ObjInstance *) object; grayObject(vm, (Obj *) instance->klass); - grayTable(vm, &instance->publicFields); - grayTable(vm, &instance->privateFields); + grayTable(vm, &instance->publicAttributes); + grayTable(vm, &instance->privateAttributes); break; } @@ -204,8 +204,8 @@ void freeObject(DictuVM *vm, Obj *object) { freeTable(vm, &klass->publicMethods); freeTable(vm, &klass->privateMethods); freeTable(vm, &klass->abstractMethods); - freeTable(vm, &klass->publicProperties); - freeTable(vm, &klass->publicConstantProperties); + freeTable(vm, &klass->variables); + freeTable(vm, &klass->constants); FREE(vm, ObjClass, object); break; } @@ -243,8 +243,8 @@ void freeObject(DictuVM *vm, Obj *object) { case OBJ_INSTANCE: { ObjInstance *instance = (ObjInstance *) object; - freeTable(vm, &instance->publicFields); - freeTable(vm, &instance->privateFields); + freeTable(vm, &instance->publicAttributes); + freeTable(vm, &instance->privateAttributes); FREE(vm, ObjInstance, object); break; } diff --git a/src/vm/object.c b/src/vm/object.c index 7fceab35..1a51b03d 100644 --- a/src/vm/object.c +++ b/src/vm/object.c @@ -67,8 +67,8 @@ ObjClass *newClass(DictuVM *vm, ObjString *name, ObjClass *superclass, ClassType initTable(&klass->abstractMethods); initTable(&klass->privateMethods); initTable(&klass->publicMethods); - initTable(&klass->publicProperties); - initTable(&klass->publicConstantProperties); + initTable(&klass->variables); + initTable(&klass->constants); klass->classAnnotations = NULL; klass->methodAnnotations = NULL; klass->fieldAnnotations = NULL; @@ -76,7 +76,7 @@ ObjClass *newClass(DictuVM *vm, ObjString *name, ObjClass *superclass, ClassType push(vm, OBJ_VAL(klass)); ObjString *nameString = copyString(vm, "_name", 5); push(vm, OBJ_VAL(nameString)); - tableSet(vm, &klass->publicConstantProperties, nameString, OBJ_VAL(name)); + tableSet(vm, &klass->constants, nameString, OBJ_VAL(name)); pop(vm); pop(vm); @@ -127,13 +127,13 @@ ObjFunction *newFunction(DictuVM *vm, ObjModule *module, FunctionType type, Acce ObjInstance *newInstance(DictuVM *vm, ObjClass *klass) { ObjInstance *instance = ALLOCATE_OBJ(vm, ObjInstance, OBJ_INSTANCE); instance->klass = klass; - initTable(&instance->publicFields); - initTable(&instance->privateFields); + initTable(&instance->publicAttributes); + initTable(&instance->privateAttributes); push(vm, OBJ_VAL(instance)); ObjString *classString = copyString(vm, "_class", 6); push(vm, OBJ_VAL(classString)); - tableSet(vm, &instance->publicFields, classString, OBJ_VAL(klass)); + tableSet(vm, &instance->publicAttributes, classString, OBJ_VAL(klass)); pop(vm); pop(vm); diff --git a/src/vm/object.h b/src/vm/object.h index f07ef991..4c5b3404 100644 --- a/src/vm/object.h +++ b/src/vm/object.h @@ -223,8 +223,8 @@ typedef struct sObjClass { Table publicMethods; Table privateMethods; Table abstractMethods; - Table publicProperties; - Table publicConstantProperties; + Table variables; + Table constants; ObjDict *classAnnotations; ObjDict *methodAnnotations; ObjDict *fieldAnnotations; @@ -240,8 +240,8 @@ typedef struct sObjEnum { typedef struct { Obj obj; ObjClass *klass; - Table privateFields; - Table publicFields; + Table privateAttributes; + Table publicAttributes; } ObjInstance; typedef struct { diff --git a/src/vm/opcodes.h b/src/vm/opcodes.h index 641828d1..766ba140 100644 --- a/src/vm/opcodes.h +++ b/src/vm/opcodes.h @@ -19,15 +19,15 @@ OPCODE(DEFINE_OPTIONAL) OPCODE(SET_MODULE) OPCODE(GET_UPVALUE) OPCODE(SET_UPVALUE) -OPCODE(GET_PROPERTY) -OPCODE(GET_PRIVATE_PROPERTY) -OPCODE(GET_PROPERTY_NO_POP) -OPCODE(GET_PRIVATE_PROPERTY_NO_POP) -OPCODE(SET_PROPERTY) -OPCODE(SET_PRIVATE_PROPERTY) +OPCODE(GET_ATTRIBUTE) +OPCODE(GET_PRIVATE_ATTRIBUTE) +OPCODE(GET_ATTRIBUTE_NO_POP) +OPCODE(GET_PRIVATE_ATTRIBUTE_NO_POP) +OPCODE(SET_ATTRIBUTE) +OPCODE(SET_PRIVATE_ATTRIBUTE) OPCODE(SET_CLASS_VAR) -OPCODE(SET_INIT_PROPERTIES) -OPCODE(SET_PRIVATE_INIT_PROPERTIES) +OPCODE(SET_INIT_ATTRIBUTES) +OPCODE(SET_PRIVATE_INIT_ATTRIBUTES) OPCODE(GET_SUPER) OPCODE(EQUAL) OPCODE(GREATER) diff --git a/src/vm/vm.c b/src/vm/vm.c index 261fa928..e45be96d 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -373,11 +373,11 @@ static bool invokeFromClass(DictuVM *vm, ObjClass *klass, ObjString *name, Value method; if (!tableGet(&klass->publicMethods, name, &method)) { if (tableGet(&klass->privateMethods, name, &method)) { - runtimeError(vm, "Cannot access private property '%s' from superclass '%s'.", name->chars, klass->name->chars); + runtimeError(vm, "Cannot access private attribute '%s' from superclass '%s'.", name->chars, klass->name->chars); return false; } - runtimeError(vm, "Undefined property '%s'.", name->chars); + runtimeError(vm, "Undefined attribute '%s'.", name->chars); return false; } @@ -408,7 +408,7 @@ static bool invokeInternal(DictuVM *vm, ObjString *name, int argCount, bool unpa } // Look for a field which may shadow a method. - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { vm->stackTop[-argCount - 1] = value; return callValue(vm, value, argCount, unpack); } @@ -448,7 +448,7 @@ static bool invokeInternal(DictuVM *vm, ObjString *name, int argCount, bool unpa } } - runtimeError(vm, "Undefined property '%s'.", name->chars); + runtimeError(vm, "Undefined attribute '%s'.", name->chars); return false; } @@ -490,7 +490,7 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount, bool unpack) { Value value; if (!tableGet(&module->values, name, &value)) { - runtimeError(vm, "Undefined property '%s'.", name->chars); + runtimeError(vm, "Undefined attribute '%s'.", name->chars); return false; } return callValue(vm, value, argCount, unpack); @@ -517,7 +517,7 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount, bool unpack) { return callNativeMethod(vm, method, argCount); } - runtimeError(vm, "Undefined property '%s'.", name->chars); + runtimeError(vm, "Undefined attribute '%s'.", name->chars); return false; } @@ -536,17 +536,17 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount, bool unpack) { } // Look for a field which may shadow a method. - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { vm->stackTop[-argCount - 1] = value; return callValue(vm, value, argCount, unpack); } if (tableGet(&instance->klass->privateMethods, name, &value)) { - runtimeError(vm, "Cannot access private property '%s' on '%s' instance.", name->chars, instance->klass->name->chars); + runtimeError(vm, "Cannot access private attribute '%s' on '%s' instance.", name->chars, instance->klass->name->chars); return false; } - runtimeError(vm, "Undefined property '%s'.", name->chars); + runtimeError(vm, "Undefined attribute '%s'.", name->chars); return false; } @@ -660,7 +660,7 @@ static bool invoke(DictuVM *vm, ObjString *name, int argCount, bool unpack) { return callValue(vm, value, argCount, false); } - runtimeError(vm, "'%s' enum has no property '%s'.", enumObj->name->chars, name->chars); + runtimeError(vm, "'%s' enum has no value '%s'.", enumObj->name->chars, name->chars); return false; } @@ -1050,11 +1050,11 @@ static DictuInterpretResult run(DictuVM *vm) { DISPATCH(); } - CASE_CODE(GET_PROPERTY): { + CASE_CODE(GET_ATTRIBUTE): { Value receiver = peek(vm, 0); if (!IS_OBJ(receiver)) { - RUNTIME_ERROR_TYPE("'%s' type has no properties", 0); + RUNTIME_ERROR_TYPE("'%s' type has no attributes", 0); } switch (getObjType(receiver)) { @@ -1062,7 +1062,7 @@ static DictuInterpretResult run(DictuVM *vm) { ObjInstance *instance = AS_INSTANCE(receiver); ObjString *name = READ_STRING(); Value value; - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); @@ -1076,13 +1076,13 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); @@ -1091,11 +1091,11 @@ static DictuInterpretResult run(DictuVM *vm) { klass = klass->superclass; } - if (tableGet(&instance->privateFields, name, &value)) { - RUNTIME_ERROR("Cannot access private property '%s' on '%s' instance.", name->chars, instance->klass->name->chars); + if (tableGet(&instance->privateAttributes, name, &value)) { + RUNTIME_ERROR("Cannot access private attribute '%s' on '%s' instance.", name->chars, instance->klass->name->chars); } - RUNTIME_ERROR("'%s' instance has no property: '%s'.", instance->klass->name->chars, name->chars); + RUNTIME_ERROR("'%s' instance has no attribute: '%s'.", instance->klass->name->chars, name->chars); } case OBJ_MODULE: { @@ -1108,7 +1108,7 @@ static DictuInterpretResult run(DictuVM *vm) { DISPATCH(); } - RUNTIME_ERROR("'%s' module has no property: '%s'.", module->name->chars, name->chars); + RUNTIME_ERROR("'%s' module has no attribute: '%s'.", module->name->chars, name->chars); } case OBJ_CLASS: { @@ -1119,13 +1119,13 @@ static DictuInterpretResult run(DictuVM *vm) { Value value; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { pop(vm); // Class. push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { pop(vm); // Class. push(vm, value); DISPATCH(); @@ -1152,7 +1152,7 @@ static DictuInterpretResult run(DictuVM *vm) { DISPATCH(); } - RUNTIME_ERROR("'%s' class has no property: '%s'.", klassStore->name->chars, name->chars); + RUNTIME_ERROR("'%s' class has no attribute: '%s'.", klassStore->name->chars, name->chars); } case OBJ_ENUM: { @@ -1166,7 +1166,7 @@ static DictuInterpretResult run(DictuVM *vm) { DISPATCH(); } - RUNTIME_ERROR("'%s' enum has no property: '%s'.", enumObj->name->chars, name->chars); + RUNTIME_ERROR("'%s' enum has no attribute: '%s'.", enumObj->name->chars, name->chars); } default: { @@ -1175,18 +1175,18 @@ static DictuInterpretResult run(DictuVM *vm) { } } - CASE_CODE(GET_PRIVATE_PROPERTY): { + CASE_CODE(GET_PRIVATE_ATTRIBUTE): { if (IS_INSTANCE(peek(vm, 0))) { ObjInstance *instance = AS_INSTANCE(peek(vm, 0)); ObjString *name = READ_STRING(); Value value; - if (tableGet(&instance->privateFields, name, &value)) { + if (tableGet(&instance->privateAttributes, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); } - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); @@ -1200,13 +1200,13 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { pop(vm); // Instance. push(vm, value); DISPATCH(); @@ -1215,7 +1215,7 @@ static DictuInterpretResult run(DictuVM *vm) { klass = klass->superclass; } - RUNTIME_ERROR("'%s' instance has no property: '%s'.", instance->klass->name->chars, name->chars); + RUNTIME_ERROR("'%s' instance has no attribute1: '%s'.", instance->klass->name->chars, name->chars); } else if (IS_CLASS(peek(vm, 0))) { ObjClass *klass = AS_CLASS(peek(vm, 0)); // Used to keep a reference to the class for the runtime error below @@ -1224,13 +1224,13 @@ static DictuInterpretResult run(DictuVM *vm) { Value value; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { pop(vm); // Class. push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { pop(vm); // Class. push(vm, value); DISPATCH(); @@ -1239,21 +1239,21 @@ static DictuInterpretResult run(DictuVM *vm) { klass = klass->superclass; } - RUNTIME_ERROR("'%s' class has no property: '%s'.", klassStore->name->chars, name->chars); + RUNTIME_ERROR("'%s' class has no attribute: '%s'.", klassStore->name->chars, name->chars); } - RUNTIME_ERROR_TYPE("'%s' type has no properties", 0); + RUNTIME_ERROR_TYPE("'%s' type has no attributes", 0); } - CASE_CODE(GET_PROPERTY_NO_POP): { + CASE_CODE(GET_ATTRIBUTE_NO_POP): { if (!IS_INSTANCE(peek(vm, 0))) { - RUNTIME_ERROR("Only instances have properties."); + RUNTIME_ERROR("Only instances have attrributes."); } ObjInstance *instance = AS_INSTANCE(peek(vm, 0)); ObjString *name = READ_STRING(); Value value; - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { push(vm, value); DISPATCH(); } @@ -1266,12 +1266,12 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { push(vm, value); DISPATCH(); } @@ -1279,27 +1279,27 @@ static DictuInterpretResult run(DictuVM *vm) { klass = klass->superclass; } - if (tableGet(&instance->privateFields, name, &value)) { - RUNTIME_ERROR("Cannot access private property '%s' on '%s' instance.", name->chars, instance->klass->name->chars); + if (tableGet(&instance->privateAttributes, name, &value)) { + RUNTIME_ERROR("Cannot access private attribute '%s' on '%s' instance.", name->chars, instance->klass->name->chars); } - RUNTIME_ERROR("'%s' instance has no property: '%s'.", instance->klass->name->chars, name->chars); + RUNTIME_ERROR("'%s' instance has no attribute2: '%s'.", instance->klass->name->chars, name->chars); } - CASE_CODE(GET_PRIVATE_PROPERTY_NO_POP): { + CASE_CODE(GET_PRIVATE_ATTRIBUTE_NO_POP): { if (!IS_INSTANCE(peek(vm, 0))) { - RUNTIME_ERROR("Only instances have properties."); + RUNTIME_ERROR("Only instances have attributes."); } ObjInstance *instance = AS_INSTANCE(peek(vm, 0)); ObjString *name = READ_STRING(); Value value; - if (tableGet(&instance->privateFields, name, &value)) { + if (tableGet(&instance->privateAttributes, name, &value)) { push(vm, value); DISPATCH(); } - if (tableGet(&instance->publicFields, name, &value)) { + if (tableGet(&instance->publicAttributes, name, &value)) { push(vm, value); DISPATCH(); } @@ -1312,12 +1312,12 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *klass = instance->klass; while (klass != NULL) { - if (tableGet(&klass->publicConstantProperties, name, &value)) { + if (tableGet(&klass->constants, name, &value)) { push(vm, value); DISPATCH(); } - if (tableGet(&klass->publicProperties, name, &value)) { + if (tableGet(&klass->variables, name, &value)) { push(vm, value); DISPATCH(); } @@ -1325,13 +1325,13 @@ static DictuInterpretResult run(DictuVM *vm) { klass = klass->superclass; } - RUNTIME_ERROR("'%s' instance has no property: '%s'.", instance->klass->name->chars, name->chars); + RUNTIME_ERROR("'%s' instance has no attribute3: '%s'.", instance->klass->name->chars, name->chars); } - CASE_CODE(SET_PROPERTY): { + CASE_CODE(SET_ATTRIBUTE): { if (IS_INSTANCE(peek(vm, 1))) { ObjInstance *instance = AS_INSTANCE(peek(vm, 1)); - tableSet(vm, &instance->publicFields, READ_STRING(), peek(vm, 0)); + tableSet(vm, &instance->publicAttributes, READ_STRING(), peek(vm, 0)); pop(vm); pop(vm); push(vm, NIL_VAL); @@ -1341,24 +1341,24 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *klass = AS_CLASS(peek(vm, 1)); Value _; - if (tableGet(&klass->publicConstantProperties, key, &_)) { + if (tableGet(&klass->constants, key, &_)) { RUNTIME_ERROR("Cannot assign to class constant '%s.%s'.", klass->name->chars, key->chars); } - tableSet(vm, &klass->publicProperties, key, peek(vm, 0)); + tableSet(vm, &klass->variables, key, peek(vm, 0)); pop(vm); pop(vm); push(vm, NIL_VAL); DISPATCH(); } - RUNTIME_ERROR_TYPE("Can not set property on type '%s'", 1); + RUNTIME_ERROR_TYPE("Can not set attribute on type '%s'", 1); } - CASE_CODE(SET_PRIVATE_PROPERTY): { + CASE_CODE(SET_PRIVATE_ATTRIBUTE): { if (IS_INSTANCE(peek(vm, 1))) { ObjInstance *instance = AS_INSTANCE(peek(vm, 1)); - tableSet(vm, &instance->privateFields, READ_STRING(), peek(vm, 0)); + tableSet(vm, &instance->privateAttributes, READ_STRING(), peek(vm, 0)); pop(vm); pop(vm); push(vm, NIL_VAL); @@ -1375,35 +1375,35 @@ static DictuInterpretResult run(DictuVM *vm) { bool constant = READ_BYTE(); if (constant) { - tableSet(vm, &klass->publicConstantProperties, key, peek(vm, 0)); + tableSet(vm, &klass->constants, key, peek(vm, 0)); } else { - tableSet(vm, &klass->publicProperties, key, peek(vm, 0)); + tableSet(vm, &klass->variables, key, peek(vm, 0)); } pop(vm); DISPATCH(); } - CASE_CODE(SET_INIT_PROPERTIES): { + CASE_CODE(SET_INIT_ATTRIBUTES): { ObjFunction *function = AS_FUNCTION(READ_CONSTANT()); int argCount = function->arity + function->arityOptional; ObjInstance *instance = AS_INSTANCE(peek(vm, function->arity + function->arityOptional)); for (int i = 0; i < function->propertyCount; ++i) { ObjString *propertyName = AS_STRING(function->chunk.constants.values[function->propertyNames[i]]); - tableSet(vm, &instance->publicFields, propertyName, peek(vm, argCount - function->propertyIndexes[i] - 1)); + tableSet(vm, &instance->publicAttributes, propertyName, peek(vm, argCount - function->propertyIndexes[i] - 1)); } DISPATCH(); } - CASE_CODE(SET_PRIVATE_INIT_PROPERTIES): { + CASE_CODE(SET_PRIVATE_INIT_ATTRIBUTES): { ObjFunction *function = AS_FUNCTION(READ_CONSTANT()); int argCount = function->arity + function->arityOptional; ObjInstance *instance = AS_INSTANCE(peek(vm, function->arity + function->arityOptional)); for (int i = 0; i < function->privatePropertyCount; ++i) { ObjString *propertyName = AS_STRING(function->chunk.constants.values[function->privatePropertyNames[i]]); - tableSet(vm, &instance->privateFields, propertyName, peek(vm, argCount - function->privatePropertyIndexes[i] - 1)); + tableSet(vm, &instance->privateAttributes, propertyName, peek(vm, argCount - function->privatePropertyIndexes[i] - 1)); } DISPATCH(); @@ -1414,7 +1414,7 @@ static DictuInterpretResult run(DictuVM *vm) { ObjClass *superclass = AS_CLASS(pop(vm)); if (!bindMethod(vm, superclass, name)) { - RUNTIME_ERROR("Undefined property '%s'.", name->chars); + RUNTIME_ERROR("Undefined attribute '%s'.", name->chars); } DISPATCH(); } diff --git a/tests/classes/getAttributes.du b/tests/classes/getAttributes.du index 7c56f31a..7e66bec7 100644 --- a/tests/classes/getAttributes.du +++ b/tests/classes/getAttributes.du @@ -9,6 +9,8 @@ from UnitTest import UnitTest; class Test { var x = 10; + const y = 20; + init(private priv = 10, var pub = 20) { this.prop = 30; } @@ -19,30 +21,33 @@ class Test { } class Inherit < Test { - + var x = 20; } class TestGetAttributes < UnitTest { testGetAttributes() { this.assertEquals(Test().getAttributes().len(), 3); - this.assertTruthy(Test().getAttributes().exists("properties")); - this.assertTruthy(Test().getAttributes().exists("classVariables")); + this.assertTruthy(Test().getAttributes().exists("attributes")); + this.assertTruthy(Test().getAttributes().exists("fields")); this.assertTruthy(Test().getAttributes().exists("methods")); // 3 because of the implicit "_class" property - this.assertEquals(Test().getAttributes().get("properties").len(), 3); - this.assertEquals(Test().getAttributes().get("classVariables").len(), 1); + this.assertEquals(Test().getAttributes().get("attributes").len(), 3); + // 3 because of the implicit "_name" property + this.assertEquals(Test().getAttributes().get("fields").len(), 3); this.assertEquals(Test().getAttributes().get("methods").len(), 2); this.assertTruthy(Test().getAttributes().get("methods").find(def (item) => item == "publicMethod")); } testGetAttributesInherit() { this.assertEquals(Inherit().getAttributes().len(), 3); - this.assertTruthy(Inherit().getAttributes().exists("properties")); - this.assertTruthy(Inherit().getAttributes().exists("classVariables")); + this.assertTruthy(Inherit().getAttributes().exists("attributes")); + this.assertTruthy(Inherit().getAttributes().exists("fields")); this.assertTruthy(Inherit().getAttributes().exists("methods")); // 3 because of the implicit "_class" property - this.assertEquals(Inherit().getAttributes().get("properties").len(), 3); - this.assertEquals(Inherit().getAttributes().get("classVariables").len(), 1); + this.assertEquals(Inherit().getAttributes().get("attributes").len(), 3); + // 3 because of the implicit "_name" property + print(Inherit().getAttributes()); + this.assertEquals(Inherit().getAttributes().get("fields").len(), 3); this.assertEquals(Inherit().getAttributes().get("methods").len(), 2); this.assertTruthy(Inherit().getAttributes().get("methods").find(def (item) => item == "publicMethod")); } From c1031734be27ff79e2ad8033489730ea099df47d Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Sun, 20 Aug 2023 16:56:19 +0100 Subject: [PATCH 2/2] Update use of property to attribute --- docs/docs/classes.md | 10 +++++----- docs/docs/standard-lib/http.md | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/docs/classes.md b/docs/docs/classes.md index 0cc0077c..1bc86d87 100644 --- a/docs/docs/classes.md +++ b/docs/docs/classes.md @@ -210,7 +210,7 @@ print(myObject.x); // 10 Attempting to access an attribute of an object that does not exist will throw a runtime error, and instead before accessing an attribute that may not be there, you should check if the object has the given attribute. This is done via `hasAttribute`. -Note: Will only check properties with public visibility. +Note: Will only check attributes with public visibility. ```cs class Test { @@ -231,7 +231,7 @@ print(myObject.z); // Undefined attribute 'z'. Sometimes in Dictu we may wish to access an attribute of an object without knowing the attribute until runtime. We can do this via the `getAttribute` method. This method takes a string and an optional default value and returns either the attribute value or the default value (if there is no attribute and no default value, nil is returned). -Note: Will only retrieve properties with public visibility. +Note: Will only retrieve attributes with public visibility. ```cs class Test { @@ -249,7 +249,7 @@ print(myObject.getAttribute("y")); // nil ### getAttributes() -> Dict -The `getAttributes` method returns all class variables / constants, public methods and public properties. +The `getAttributes` method returns all class variables / constants, public methods and public attributes. ```cs class Test { @@ -259,7 +259,7 @@ class Test { } var myObject = Test(); -print(myObject.getAttributes()); // {"fields": [], "methods": ["init"], "attributes": ["_class", "x"]} +print(myObject.getAttributes()); // {"fields": ["_name"], "methods": ["init"], "attributes": ["_class", "x"]} ``` ### setAttribute(String, Value) @@ -674,7 +674,7 @@ class ClassWithMethodAnnotation { } ``` -Annotations are access via the `.classAnnotations`, `.methodAnnotations` and `.fieldAnnotations` properties available on all classes. +Annotations are access via the `.classAnnotations`, `.methodAnnotations` and `.fieldAnnotations` attributes available on all classes. For class annotations, the returned data structure returned is a dictionary with keys set to the names of the annotations and their values if present. If no value is provided to the annotation, the value associated with the key is set to `nil`. diff --git a/docs/docs/standard-lib/http.md b/docs/docs/standard-lib/http.md index ce1d2c3b..196d62c5 100644 --- a/docs/docs/standard-lib/http.md +++ b/docs/docs/standard-lib/http.md @@ -93,14 +93,14 @@ httpClient.post("https://httpbin.org/post", {"test": 10}); ### Response Both HTTP.get(), HTTP.post(), httpClient.get(), and httpClient.post() return a Result that unwraps a Response object on success, or nil on error. -The Response object returned has 3 public properties, "content", "headers" and "statusCode". "content" is the actual content returned from the +The Response object returned has 3 public attributes, "content", "headers" and "statusCode". "content" is the actual content returned from the HTTP request as a string, "headers" is a list of all the response headers and "statusCode" is a number denoting the status code from the response #### Quick Reference Table -##### Properties +##### Attributes -| Property | Description | +| Attribute | Description | | ---------- | ------------------------------------------------- | | content | Raw string content returned from the HTTP request | | headers | A list of headers returned from the HTTP request | @@ -108,9 +108,9 @@ the response ##### Methods -| Method | Description | -| ------ | ------------------------------------ | -| json | Convert the content property to JSON | +| Method | Description | +| ------ | ------------------------------------- | +| json | Convert the content attribute to JSON | Example response from [httpbin.org](https://httpbin.org)