Skip to content

Commit 53c393b

Browse files
committed
feat: expose the __name and __qname properties of all Argon types
1 parent fa1bd11 commit 53c393b

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

argon/vm/datatype/arobject.cpp

+23-17
Original file line numberDiff line numberDiff line change
@@ -931,24 +931,14 @@ bool ExportDefaultMethod(TypeInfo *type) {
931931
bool InitMembers(TypeInfo *type) {
932932
auto *ns = (Namespace *) type->tp_map;
933933

934-
if (type->object == nullptr)
935-
return true;
936-
937-
// Function/Method
938-
if (type->object->methods != nullptr) {
939-
for (const FunctionDef *cursor = type->object->methods; cursor->name != nullptr; cursor++) {
940-
auto *fn = (ArObject *) FunctionNew(cursor, type, nullptr);
941-
if (fn == nullptr)
942-
return false;
934+
if (!NamespaceNewSymbol(ns, "__name", type->name, AttributeFlag::CONST | AttributeFlag::PUBLIC))
935+
return false;
943936

944-
if (!NamespaceNewSymbol(ns, cursor->name, fn, AttributeFlag::CONST | AttributeFlag::PUBLIC)) {
945-
Release(fn);
946-
return false;
947-
}
937+
if (!NamespaceNewSymbol(ns, "__qname", type->qname, AttributeFlag::CONST | AttributeFlag::PUBLIC))
938+
return false;
948939

949-
Release(fn);
950-
}
951-
}
940+
if (type->object == nullptr)
941+
return true;
952942

953943
// Members
954944
if (type->object->members != nullptr) {
@@ -966,6 +956,22 @@ bool InitMembers(TypeInfo *type) {
966956
}
967957
}
968958

959+
// Function/Method
960+
if (type->object->methods != nullptr) {
961+
for (const FunctionDef *cursor = type->object->methods; cursor->name != nullptr; cursor++) {
962+
auto *fn = (ArObject *) FunctionNew(cursor, type, nullptr);
963+
if (fn == nullptr)
964+
return false;
965+
966+
if (!NamespaceNewSymbol(ns, cursor->name, fn, AttributeFlag::CONST | AttributeFlag::PUBLIC)) {
967+
Release(fn);
968+
return false;
969+
}
970+
971+
Release(fn);
972+
}
973+
}
974+
969975
return true;
970976
}
971977

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

1122-
return TraitIsImplemented( AR_GET_TYPE(object), type);
1128+
return TraitIsImplemented(AR_GET_TYPE(object), type);
11231129
}
11241130

11251131
int argon::vm::datatype::MonitorAcquire(ArObject *object) {

argon/vm/datatype/namespace.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@ bool argon::vm::datatype::NamespaceNewSymbol(Namespace *ns, const char *key, ArO
200200
return ok;
201201
}
202202

203+
bool argon::vm::datatype::NamespaceNewSymbol(Namespace *ns, const char *key, const char *value, AttributeFlag aa) {
204+
auto *s_key = StringIntern(key);
205+
if (s_key == nullptr)
206+
return false;
207+
208+
auto *s_value = StringNew(value);
209+
if (s_value == nullptr) {
210+
Release(s_key);
211+
212+
return false;
213+
}
214+
215+
auto ok = NamespaceNewSymbol(ns, (ArObject *) s_key, (ArObject *) s_value, aa);
216+
217+
Release(s_key);
218+
Release(s_value);
219+
220+
return ok;
221+
}
222+
203223
bool argon::vm::datatype::NamespaceSet(Namespace *ns, ArObject *key, ArObject *value) {
204224
NSEntry *entry;
205225

argon/vm/datatype/namespace.h

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ namespace argon::vm::datatype {
140140
*/
141141
bool NamespaceNewSymbol(Namespace *ns, const char *key, ArObject *value, AttributeFlag aa);
142142

143+
/**
144+
* @brief Add new string to namespace.
145+
*
146+
* @param ns Pointer to namespace.
147+
* @param key Pointer to the C-string that represent the key.
148+
* @param value Pointer to the C-string that represent the string value.
149+
* @param aa AttributeFlag
150+
* @return True on success, false otherwise.
151+
*/
152+
bool NamespaceNewSymbol(Namespace *ns, const char *key, const char *value, AttributeFlag aa);
153+
143154
/**
144155
* @brief Replaces the value associated with a key.
145156
*

0 commit comments

Comments
 (0)