Skip to content

Commit

Permalink
feat: expose the __name and __qname properties of all Argon types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopodl committed Mar 20, 2024
1 parent fa1bd11 commit 53c393b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
40 changes: 23 additions & 17 deletions argon/vm/datatype/arobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,24 +931,14 @@ bool ExportDefaultMethod(TypeInfo *type) {
bool InitMembers(TypeInfo *type) {
auto *ns = (Namespace *) type->tp_map;

if (type->object == nullptr)
return true;

// Function/Method
if (type->object->methods != nullptr) {
for (const FunctionDef *cursor = type->object->methods; cursor->name != nullptr; cursor++) {
auto *fn = (ArObject *) FunctionNew(cursor, type, nullptr);
if (fn == nullptr)
return false;
if (!NamespaceNewSymbol(ns, "__name", type->name, AttributeFlag::CONST | AttributeFlag::PUBLIC))
return false;

if (!NamespaceNewSymbol(ns, cursor->name, fn, AttributeFlag::CONST | AttributeFlag::PUBLIC)) {
Release(fn);
return false;
}
if (!NamespaceNewSymbol(ns, "__qname", type->qname, AttributeFlag::CONST | AttributeFlag::PUBLIC))
return false;

Release(fn);
}
}
if (type->object == nullptr)
return true;

// Members
if (type->object->members != nullptr) {
Expand All @@ -966,6 +956,22 @@ bool InitMembers(TypeInfo *type) {
}
}

// Function/Method
if (type->object->methods != nullptr) {
for (const FunctionDef *cursor = type->object->methods; cursor->name != nullptr; cursor++) {
auto *fn = (ArObject *) FunctionNew(cursor, type, nullptr);
if (fn == nullptr)
return false;

if (!NamespaceNewSymbol(ns, cursor->name, fn, AttributeFlag::CONST | AttributeFlag::PUBLIC)) {
Release(fn);
return false;
}

Release(fn);
}
}

return true;
}

Expand Down Expand Up @@ -1119,7 +1125,7 @@ bool argon::vm::datatype::TypeOF(const ArObject *object, const TypeInfo *type) {
if (AR_TYPEOF(object, type))
return true;

return TraitIsImplemented( AR_GET_TYPE(object), type);
return TraitIsImplemented(AR_GET_TYPE(object), type);
}

int argon::vm::datatype::MonitorAcquire(ArObject *object) {
Expand Down
20 changes: 20 additions & 0 deletions argon/vm/datatype/namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ bool argon::vm::datatype::NamespaceNewSymbol(Namespace *ns, const char *key, ArO
return ok;
}

bool argon::vm::datatype::NamespaceNewSymbol(Namespace *ns, const char *key, const char *value, AttributeFlag aa) {
auto *s_key = StringIntern(key);
if (s_key == nullptr)
return false;

auto *s_value = StringNew(value);
if (s_value == nullptr) {
Release(s_key);

return false;
}

auto ok = NamespaceNewSymbol(ns, (ArObject *) s_key, (ArObject *) s_value, aa);

Release(s_key);
Release(s_value);

return ok;
}

bool argon::vm::datatype::NamespaceSet(Namespace *ns, ArObject *key, ArObject *value) {
NSEntry *entry;

Expand Down
11 changes: 11 additions & 0 deletions argon/vm/datatype/namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ namespace argon::vm::datatype {
*/
bool NamespaceNewSymbol(Namespace *ns, const char *key, ArObject *value, AttributeFlag aa);

/**
* @brief Add new string to namespace.
*
* @param ns Pointer to namespace.
* @param key Pointer to the C-string that represent the key.
* @param value Pointer to the C-string that represent the string value.
* @param aa AttributeFlag
* @return True on success, false otherwise.
*/
bool NamespaceNewSymbol(Namespace *ns, const char *key, const char *value, AttributeFlag aa);

/**
* @brief Replaces the value associated with a key.
*
Expand Down

0 comments on commit 53c393b

Please sign in to comment.