Skip to content

Commit 35f05b5

Browse files
committed
fix: resolved an issue with the behavior of typeof and implements
1 parent 0905ff4 commit 35f05b5

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

argon/vm/datatype/arobject.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ArObject *type_get_attr(const ArObject *instance, ArObject *key, bool static_att
8282
return nullptr;
8383
}
8484

85-
if (!aprop.IsPublic() && !TraitIsImplemented(instance, base)) {
85+
if (!aprop.IsPublic() && !TraitIsImplemented(ancestor, base)) {
8686
ErrorFormat(kAccessViolationError[0], kAccessViolationError[1],
8787
ARGON_RAW_STRING((String *) key), ancestor->name);
8888

@@ -1096,16 +1096,10 @@ bool argon::vm::datatype::TypeInit(TypeInfo *type, ArObject *auxiliary) {
10961096
return false;
10971097
}
10981098

1099-
bool argon::vm::datatype::TraitIsImplemented(const ArObject *object, const TypeInfo *type) {
1100-
const TypeInfo *obj_type;
1101-
1102-
if (object == nullptr || type == nullptr)
1099+
bool argon::vm::datatype::TraitIsImplemented(const TypeInfo *obj_type, const TypeInfo *type) {
1100+
if (obj_type == nullptr || type == nullptr)
11031101
return false;
11041102

1105-
if ((const TypeInfo *) object == type)
1106-
return true;
1107-
1108-
obj_type = AR_GET_TYPE(object);
11091103
if (obj_type == type)
11101104
return true;
11111105

@@ -1121,6 +1115,13 @@ bool argon::vm::datatype::TraitIsImplemented(const ArObject *object, const TypeI
11211115
return false;
11221116
}
11231117

1118+
bool argon::vm::datatype::TypeOF(const ArObject *object, const TypeInfo *type) {
1119+
if (AR_TYPEOF(object, type))
1120+
return true;
1121+
1122+
return TraitIsImplemented( AR_GET_TYPE(object), type);
1123+
}
1124+
11241125
int argon::vm::datatype::MonitorAcquire(ArObject *object) {
11251126
auto *monitor = AR_GET_MON(object).load(std::memory_order_consume);
11261127

argon/vm/datatype/arobject.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ namespace argon::vm::datatype {
7070

7171
bool TypeInit(TypeInfo *type, ArObject *auxiliary);
7272

73-
bool TraitIsImplemented(const ArObject *object, const TypeInfo *type);
73+
bool TraitIsImplemented(const TypeInfo *obj_type, const TypeInfo *type);
74+
75+
bool TypeOF(const ArObject *object, const TypeInfo *type);
7476

7577
int MonitorAcquire(ArObject *object);
7678

argon/vm/datatype/function.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ ArObject *argon::vm::datatype::FunctionInvokeNative(Function *func, ArObject **a
249249
if (f_count > 0 && func->IsMethod()) {
250250
instance = *f_args;
251251

252-
if (!TraitIsImplemented(instance, func->base)) {
252+
if (!TraitIsImplemented(AR_GET_TYPE(instance), func->base)) {
253253
ErrorFormat(kTypeError[0], kTypeError[5], ARGON_RAW_STRING(func->qname), AR_TYPE_NAME(instance));
254254
goto ERROR;
255255
}

argon/vm/mod/builtins.cpp

+28-9
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,25 @@ ARGON_FUNCTION(builtins_iscallable, iscallable,
192192
}
193193

194194
ARGON_FUNCTION(builtins_implements, implements,
195-
"Check if object implements all the indicated traits.\n"
195+
"Check if type implements all the indicated traits.\n"
196196
"\n"
197197
"- Parameters:\n"
198-
" - obj: Object to check.\n"
198+
" - obj: Type to check.\n"
199199
" - ...traits: Traits list.\n"
200-
"- Returns: True if the object implements ALL indicated traits, false otherwise.",
200+
"- Returns: True if type implements ALL indicated traits, false otherwise.",
201201
": obj, : traits", true, false) {
202+
if (!AR_TYPEOF(*args, type_type_)) {
203+
ErrorFormat(kTypeError[0], kTypeError[1], AR_TYPE_QNAME(*args));
204+
return nullptr;
205+
}
206+
202207
for (ArSize i = 1; i < argc; i++) {
203-
if (!TraitIsImplemented(args[0], (TypeInfo *) args[i]))
208+
if (!AR_TYPEOF(args[i], type_type_)) {
209+
ErrorFormat(kTypeError[0], kTypeError[1], AR_TYPE_QNAME(args[i]));
210+
return nullptr;
211+
}
212+
213+
if (!TraitIsImplemented((const TypeInfo *) args[0], (TypeInfo *) args[i]))
204214
return BoolToArBool(false);
205215
}
206216

@@ -241,8 +251,20 @@ ARGON_FUNCTION(builtins_require, require,
241251
Result *result;
242252

243253
auto *fiber = argon::vm::GetFiber();
254+
auto *path = (String *) *args;
255+
256+
auto index = StringRFind(path, ".");
257+
if (index > 0) {
258+
path = StringSubs(path, 0, index);
259+
if (path == nullptr)
260+
return nullptr;
261+
}
262+
263+
auto *mod = LoadModule(fiber->context->imp, path, nullptr);
264+
265+
if (index >= 0)
266+
Release(path);
244267

245-
auto *mod = LoadModule(fiber->context->imp, ((String *) *args), nullptr);
246268
if (mod != nullptr) {
247269
if ((result = ResultNew((ArObject *) mod, true)) == nullptr)
248270
Release(mod);
@@ -348,10 +370,7 @@ ARGON_FUNCTION(builtins_typeof, typeof,
348370
for (auto i = 1; i < argc; i++) {
349371
auto *tp = (const TypeInfo *) args[i];
350372

351-
if (!AR_TYPEOF(tp, type_type_))
352-
tp = AR_GET_TYPE(tp);
353-
354-
if (AR_TYPEOF(base, tp))
373+
if (TypeOF(base, tp))
355374
return BoolToArBool(true);
356375
}
357376

0 commit comments

Comments
 (0)