Clean up a bunch of potential type checking issues
This commit is contained in:
parent
7ff7c26fbd
commit
25d5f88454
@ -263,7 +263,7 @@ KRK_METHOD(bytesiterator,__init__,{
|
||||
KRK_METHOD(bytesiterator,__call__,{
|
||||
KrkValue _list = self->l;
|
||||
size_t _counter = self->i;
|
||||
if (_counter >= AS_BYTES(_list)->length) {
|
||||
if (!IS_BYTES(_list) || _counter >= AS_BYTES(_list)->length) {
|
||||
return argv[0];
|
||||
} else {
|
||||
self->i = _counter + 1;
|
||||
@ -292,6 +292,9 @@ KRK_METHOD(bytearray,__init__,{
|
||||
return argv[0];
|
||||
})
|
||||
|
||||
#undef IS_bytearray
|
||||
#define IS_bytearray(o) (krk_isInstanceOf(o,vm.baseClasses->bytearrayClass) && IS_BYTES(AS_bytearray(o)->actual))
|
||||
|
||||
/* bytes objects are not interned; need to do this the old-fashioned way. */
|
||||
KRK_METHOD(bytearray,__eq__,{
|
||||
if (!IS_bytearray(argv[1])) return BOOLEAN_VAL(0);
|
||||
@ -306,6 +309,11 @@ KRK_METHOD(bytearray,__repr__,{
|
||||
|
||||
krk_push(self->actual);
|
||||
KrkValue repred_bytes = krk_callDirect(vm.baseClasses->bytesClass->_reprer, 1);
|
||||
if (!IS_STRING(repred_bytes)) {
|
||||
/* Invalid repr of bytes? */
|
||||
discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
pushStringBuilderStr(&sb, AS_STRING(repred_bytes)->chars, AS_STRING(repred_bytes)->length);
|
||||
pushStringBuilder(&sb,')');
|
||||
return finishStringBuilder(&sb);
|
||||
@ -413,6 +421,7 @@ void _createAndBind_bytesClass(void) {
|
||||
krk_finalizeClass(bytes);
|
||||
|
||||
KrkClass * bytesiterator = ADD_BASE_CLASS(vm.baseClasses->bytesiteratorClass, "bytesiterator", vm.baseClasses->objectClass);
|
||||
bytesiterator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
bytesiterator->allocSize = sizeof(struct BytesIterator);
|
||||
bytesiterator->_ongcscan = _bytesiterator_gcscan;
|
||||
BIND_METHOD(bytesiterator,__init__);
|
||||
|
@ -80,6 +80,10 @@ KrkInstance * krk_buildGenerator(KrkClosure * closure, KrkValue * argsIn, size_t
|
||||
return (KrkInstance *)self;
|
||||
}
|
||||
|
||||
FUNC_SIG(generator,__init__) {
|
||||
return krk_runtimeError(vm.exceptions->typeError, "cannot create '%s' instances", "generator");
|
||||
}
|
||||
|
||||
KRK_METHOD(generator,__repr__,{
|
||||
METHOD_TAKES_NONE();
|
||||
|
||||
@ -226,6 +230,8 @@ void _createAndBind_generatorClass(void) {
|
||||
generator->allocSize = sizeof(struct generator);
|
||||
generator->_ongcscan = _generator_gcscan;
|
||||
generator->_ongcsweep = _generator_gcsweep;
|
||||
generator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(generator,__init__);
|
||||
BIND_METHOD(generator,__iter__);
|
||||
BIND_METHOD(generator,__call__);
|
||||
BIND_METHOD(generator,__repr__);
|
||||
|
@ -628,6 +628,7 @@ void _createAndBind_listClass(void) {
|
||||
KrkClass * listiterator = ADD_BASE_CLASS(vm.baseClasses->listiteratorClass, "listiterator", vm.baseClasses->objectClass);
|
||||
listiterator->allocSize = sizeof(struct ListIterator);
|
||||
listiterator->_ongcscan = _listiterator_gcscan;
|
||||
listiterator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(listiterator,__init__);
|
||||
BIND_METHOD(listiterator,__call__);
|
||||
krk_finalizeClass(listiterator);
|
||||
|
@ -1747,6 +1747,7 @@ KRK_METHOD(int,to_bytes,{
|
||||
_noexport
|
||||
void _createAndBind_longClass(void) {
|
||||
_long = ADD_BASE_CLASS(vm.baseClasses->longClass, "long", vm.baseClasses->intClass);
|
||||
_long->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
_long->allocSize = sizeof(struct BigInt);
|
||||
_long->_ongcsweep = _long_gcsweep;
|
||||
|
||||
|
@ -288,7 +288,18 @@ KRK_METHOD(int,__bin__,{
|
||||
}
|
||||
|
||||
return finishStringBuilder(&sb);
|
||||
})
|
||||
|
||||
KRK_METHOD(int,__invert__,{
|
||||
return INTEGER_VAL(~self);
|
||||
})
|
||||
|
||||
KRK_METHOD(int,__neg__,{
|
||||
return INTEGER_VAL(-self);
|
||||
})
|
||||
|
||||
KRK_METHOD(int,__abs__,{
|
||||
return self < 0 ? INTEGER_VAL(-self) : INTEGER_VAL(self);
|
||||
})
|
||||
|
||||
#undef CURRENT_CTYPE
|
||||
@ -351,6 +362,14 @@ KRK_METHOD(float,__hash__,{
|
||||
return INTEGER_VAL((uint32_t)self);
|
||||
})
|
||||
|
||||
KRK_METHOD(float,__neg__,{
|
||||
return FLOATING_VAL(-self);
|
||||
})
|
||||
|
||||
KRK_METHOD(float,__abs__,{
|
||||
return self < 0.0 ? FLOATING_VAL(-self) : INTEGER_VAL(self);
|
||||
})
|
||||
|
||||
#define BASIC_BIN_OP(name,operator) \
|
||||
KRK_METHOD(float,__ ## name ## __,{ \
|
||||
METHOD_TAKES_EXACTLY(1); \
|
||||
@ -442,6 +461,11 @@ KRK_METHOD(bool,__str__,{
|
||||
return OBJECT_VAL((self ? S("True") : S("False")));
|
||||
})
|
||||
|
||||
FUNC_SIG(NoneType,__init__) {
|
||||
if (argc > 1) return krk_runtimeError(vm.exceptions->argumentError, "%s takes no arguments", "NoneType");
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
KRK_METHOD(NoneType,__str__,{
|
||||
return OBJECT_VAL(S("None"));
|
||||
})
|
||||
@ -450,8 +474,20 @@ KRK_METHOD(NoneType,__hash__,{
|
||||
return INTEGER_VAL((uint32_t)AS_INTEGER(argv[0]));
|
||||
})
|
||||
|
||||
KRK_METHOD(NoneType,__eq__,{
|
||||
METHOD_TAKES_EXACTLY(1);
|
||||
if (IS_NONE(argv[1])) return BOOLEAN_VAL(1);
|
||||
return NOTIMPL_VAL();
|
||||
})
|
||||
|
||||
#define IS_NotImplementedType(o) IS_NOTIMPL(o)
|
||||
#define AS_NotImplementedType(o) (1)
|
||||
|
||||
FUNC_SIG(NotImplementedType,__init__) {
|
||||
if (argc > 1) return krk_runtimeError(vm.exceptions->argumentError, "%s takes no arguments", "NotImplementedType");
|
||||
return NOTIMPL_VAL();
|
||||
}
|
||||
|
||||
KRK_METHOD(NotImplementedType,__str__,{
|
||||
return OBJECT_VAL(S("NotImplemented"));
|
||||
})
|
||||
@ -460,6 +496,12 @@ KRK_METHOD(NotImplementedType,__hash__,{
|
||||
return INTEGER_VAL(0);
|
||||
})
|
||||
|
||||
KRK_METHOD(NotImplementedType,__eq__,{
|
||||
METHOD_TAKES_EXACTLY(1);
|
||||
if (IS_NOTIMPL(argv[1])) return BOOLEAN_VAL(1);
|
||||
return NOTIMPL_VAL();
|
||||
})
|
||||
|
||||
#undef BIND_METHOD
|
||||
/* These class names conflict with C types, so we need to cheat a bit */
|
||||
#define BIND_METHOD(klass,method) do { krk_defineNative(& _ ## klass->methods, #method, _ ## klass ## _ ## method); } while (0)
|
||||
@ -500,6 +542,9 @@ void _createAndBind_numericClasses(void) {
|
||||
BIND_METHOD(int,__hex__);
|
||||
BIND_METHOD(int,__oct__);
|
||||
BIND_METHOD(int,__bin__);
|
||||
BIND_METHOD(int,__invert__);
|
||||
BIND_METHOD(int,__neg__);
|
||||
BIND_METHOD(int,__abs__);
|
||||
|
||||
krk_defineNative(&_int->methods, "__repr__", FUNC_NAME(int,__str__));
|
||||
krk_finalizeClass(_int);
|
||||
@ -522,6 +567,8 @@ void _createAndBind_numericClasses(void) {
|
||||
BIND_METHOD(float,__gt__);
|
||||
BIND_METHOD(float,__le__);
|
||||
BIND_METHOD(float,__ge__);
|
||||
BIND_METHOD(float,__neg__);
|
||||
BIND_METHOD(float,__abs__);
|
||||
krk_defineNative(&_float->methods, "__repr__", FUNC_NAME(float,__str__));
|
||||
krk_finalizeClass(_float);
|
||||
KRK_DOC(_float, "Convert a number or string type to a float representation.");
|
||||
@ -536,15 +583,19 @@ void _createAndBind_numericClasses(void) {
|
||||
|
||||
KrkClass * _NoneType = ADD_BASE_CLASS(vm.baseClasses->noneTypeClass, "NoneType", vm.baseClasses->objectClass);
|
||||
_NoneType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(NoneType, __init__);
|
||||
BIND_METHOD(NoneType, __str__);
|
||||
BIND_METHOD(NoneType, __hash__);
|
||||
BIND_METHOD(NoneType, __eq__);
|
||||
krk_defineNative(&_NoneType->methods, "__repr__", FUNC_NAME(NoneType,__str__));
|
||||
krk_finalizeClass(_NoneType);
|
||||
|
||||
KrkClass * _NotImplementedType = ADD_BASE_CLASS(vm.baseClasses->notImplClass, "NotImplementedType", vm.baseClasses->objectClass);
|
||||
_NotImplementedType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(NotImplementedType, __init__);
|
||||
BIND_METHOD(NotImplementedType, __str__);
|
||||
BIND_METHOD(NotImplementedType, __hash__);
|
||||
BIND_METHOD(NotImplementedType, __eq__);
|
||||
krk_defineNative(&_NotImplementedType->methods, "__repr__", FUNC_NAME(NotImplementedType,__str__));
|
||||
krk_finalizeClass(_NotImplementedType);
|
||||
|
||||
|
@ -117,6 +117,7 @@ _noexport
|
||||
void _createAndBind_rangeClass(void) {
|
||||
range = ADD_BASE_CLASS(vm.baseClasses->rangeClass, "range", vm.baseClasses->objectClass);
|
||||
range->allocSize = sizeof(struct Range);
|
||||
range->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
KRK_DOC(BIND_METHOD(range,__init__),
|
||||
"@brief Create an iterable that produces sequential numeric values.\n"
|
||||
"@arguments [min,] max, [step]\n\n"
|
||||
@ -130,6 +131,7 @@ void _createAndBind_rangeClass(void) {
|
||||
|
||||
rangeiterator = ADD_BASE_CLASS(vm.baseClasses->rangeiteratorClass, "rangeiterator", vm.baseClasses->objectClass);
|
||||
rangeiterator->allocSize = sizeof(struct RangeIterator);
|
||||
rangeiterator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(rangeiterator,__init__);
|
||||
BIND_METHOD(rangeiterator,__call__);
|
||||
krk_finalizeClass(rangeiterator);
|
||||
|
@ -257,6 +257,9 @@ KRK_METHOD(setiterator,__init__,{
|
||||
|
||||
KRK_METHOD(setiterator,__call__,{
|
||||
METHOD_TAKES_NONE();
|
||||
|
||||
if (unlikely(!IS_set(self->set))) return argv[0];
|
||||
|
||||
do {
|
||||
if (self->i >= AS_set(self->set)->entries.capacity) return argv[0];
|
||||
if (!IS_KWARGS(AS_set(self->set)->entries.entries[self->i].key)) {
|
||||
@ -316,6 +319,7 @@ void _createAndBind_setClass(void) {
|
||||
krk_finalizeClass(set);
|
||||
|
||||
krk_makeClass(vm.builtins, &setiterator, "setiterator", vm.baseClasses->objectClass);
|
||||
setiterator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
setiterator->allocSize = sizeof(struct SetIterator);
|
||||
setiterator->_ongcscan = _setiterator_gcscan;
|
||||
BIND_METHOD(setiterator,__init__);
|
||||
|
@ -170,6 +170,7 @@ void _createAndBind_sliceClass(void) {
|
||||
KrkClass * slice = ADD_BASE_CLASS(vm.baseClasses->sliceClass, "slice", vm.baseClasses->objectClass);
|
||||
slice->allocSize = sizeof(struct KrkSlice);
|
||||
slice->_ongcscan = _slice_gcscan;
|
||||
slice->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(slice,__init__);
|
||||
BIND_METHOD(slice,__repr__);
|
||||
BIND_PROP(slice,start);
|
||||
|
@ -968,11 +968,11 @@ KRK_METHOD(striterator,__call__,{
|
||||
KrkValue _str;
|
||||
KrkValue _counter;
|
||||
const char * errorStr = NULL;
|
||||
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("s")), &_str)) {
|
||||
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("s")), &_str) || !IS_STRING(_str)) {
|
||||
errorStr = "no str pointer";
|
||||
goto _corrupt;
|
||||
}
|
||||
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("i")), &_counter)) {
|
||||
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("i")), &_counter) || !IS_INTEGER(_counter)) {
|
||||
errorStr = "no index";
|
||||
goto _corrupt;
|
||||
}
|
||||
@ -1043,6 +1043,7 @@ void _createAndBind_strClass(void) {
|
||||
KRK_DOC(str, "Obtain a string representation of an object.");
|
||||
|
||||
KrkClass * striterator = ADD_BASE_CLASS(vm.baseClasses->striteratorClass, "striterator", vm.baseClasses->objectClass);
|
||||
striterator->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
|
||||
BIND_METHOD(striterator,__init__);
|
||||
BIND_METHOD(striterator,__call__);
|
||||
krk_finalizeClass(striterator);
|
||||
|
Loading…
Reference in New Issue
Block a user