__init__ should always return None

This commit is contained in:
K. Lange 2023-01-26 12:05:53 +09:00
parent 8e80e04a33
commit 81abff3d75
14 changed files with 84 additions and 60 deletions

View File

@ -196,7 +196,7 @@ KRK_StaticMethod(object,__new__) {
}
KRK_Method(object,__init__) {
return argv[0];
return NONE_VAL();
}
@ -494,7 +494,7 @@ KRK_Method(map,__init__) {
iters->values.values[iters->values.count++] = asIter;
}
return argv[0];
return NONE_VAL();
}
KRK_Method(map,__iter__) {
@ -558,7 +558,7 @@ KRK_Method(zip,__init__) {
iters->values.values[iters->values.count++] = asIter;
}
return argv[0];
return NONE_VAL();
}
KRK_Method(zip,__iter__) {
@ -605,7 +605,8 @@ KRK_Method(filter,__init__) {
KrkValue asIter = krk_callDirect(type->_iter, 1);
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) return NONE_VAL();
krk_attachNamedValue(&self->fields, "_iterator", asIter);
return argv[0];
return NONE_VAL();
}
KRK_Method(filter,__iter__) {
@ -672,7 +673,7 @@ KRK_Method(enumerate,__init__) {
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) return NONE_VAL();
krk_attachNamedValue(&self->fields, "_iterator", asIter);
return argv[0];
return NONE_VAL();
}
KRK_Method(enumerate,__iter__) {
@ -1078,7 +1079,7 @@ KRK_Method(property,__init__) {
((struct Property*)self)->fset = IS_OBJECT(argv[2]) ? AS_OBJECT(argv[2]) : NULL;
}
return argv[0];
return NONE_VAL();
}
KRK_Method(property,setter) {

View File

@ -559,9 +559,7 @@ static void _emitBytes(struct GlobalState * state, uint8_t byte1, uint8_t byte2)
#define emitBytes(a,b) _emitBytes(state,a,b)
static void emitReturn(struct GlobalState * state) {
if (state->current->type == TYPE_INIT) {
emitBytes(OP_GET_LOCAL, 0);
} else if (state->current->type != TYPE_LAMBDA && state->current->type != TYPE_CLASS) {
if (state->current->type != TYPE_LAMBDA && state->current->type != TYPE_CLASS) {
emitByte(OP_NONE);
}
emitByte(OP_RETURN);

View File

@ -45,7 +45,7 @@ KRK_Method(BaseException,__init__) {
}
krk_attachNamedValue(&self->fields, "__cause__", NONE_VAL());
krk_attachNamedValue(&self->fields, "__context__", NONE_VAL());
return argv[0];
return NONE_VAL();
}
/**

View File

@ -36,27 +36,18 @@ struct socket {
#define CURRENT_CTYPE struct socket *
#define CURRENT_NAME self
#define NAMED_ARG(name,type,ctype,def,ind) \
ctype name = def; \
if (argc > ind) { \
CHECK_ARG(ind,type,ctype,_tmp); \
name = _tmp; \
} \
if (hasKw) { \
KrkValue tmp; \
if (krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S(#name)), &tmp)) { \
if (!IS_ ## type (tmp)) return TYPE_ERROR(type,tmp); \
name = AS_ ## type (tmp); \
} \
}
KRK_Method(socket,__init__) {
METHOD_TAKES_AT_MOST(3);
/* Complex argument processing time... */
NAMED_ARG(family,int,krk_integer_type,AF_INET,1);
NAMED_ARG(type,int,krk_integer_type,SOCK_STREAM,2);
NAMED_ARG(proto,int,krk_integer_type,0,3);
int family = AF_INET;
int type = SOCK_STREAM;
int proto = 0;
if (!krk_parseArgs(".|iii",
(const char *[]){"family","type","proto"},
&family, &type, &proto)) {
return NONE_VAL();
}
int result = socket(family,type,proto);
@ -69,7 +60,7 @@ KRK_Method(socket,__init__) {
self->type = type;
self->proto = proto;
return argv[0];
return NONE_VAL();
}
static char * _af_name(int afval) {
@ -404,6 +395,21 @@ KRK_Function(htons) {
return INTEGER_VAL(htons(value));
}
KRK_Method(socket,family) {
if (argc > 1) return krk_runtimeError(vm.exceptions->attributeError, "readonly attribute");
return INTEGER_VAL(self->family);
}
KRK_Method(socket,type) {
if (argc > 1) return krk_runtimeError(vm.exceptions->attributeError, "readonly attribute");
return INTEGER_VAL(self->type);
}
KRK_Method(socket,proto) {
if (argc > 1) return krk_runtimeError(vm.exceptions->attributeError, "readonly attribute");
return INTEGER_VAL(self->proto);
}
KrkValue krk_module_onload_socket(void) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
@ -459,6 +465,11 @@ KrkValue krk_module_onload_socket(void) {
"@arguments level,optname,value\n\n"
"@p level and @p optname should be integer values defined by @c SOL and @c SO options. "
"@p value must be either an @ref int or a @ref bytes object.");
BIND_PROP(socket,family);
BIND_PROP(socket,type);
BIND_PROP(socket,proto);
krk_defineNative(&socket->methods,"__str__", FUNC_NAME(socket,__repr__));
krk_finalizeClass(SocketClass);

View File

@ -11,9 +11,6 @@
#define CURRENT_CTYPE KrkClass *
/**
* This should really be the default behavior of type.__new__?
*/
static void _callSetName(KrkClass * _class) {
KrkValue setnames = krk_list_of(0,NULL,0);
krk_push(setnames);
@ -55,7 +52,6 @@ static void _callSetName(KrkClass * _class) {
krk_pop();
}
KRK_StaticMethod(type,__new__) {
KrkClass * metaclass;
KrkString * name;
@ -193,6 +189,7 @@ KRK_Method(type,__call__) {
if (unlikely(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) return NONE_VAL();
if (krk_isInstanceOf(krk_peek(0), self) && likely(self->_init != NULL)) {
krk_push(krk_peek(0));
for (int i = 0; i < argc - 1; ++i) {
krk_push(argv[i+1]);
}
@ -203,7 +200,11 @@ KRK_Method(type,__call__) {
krk_push(KWARGS_VAL(1));
}
return krk_callDirect(self->_init, argCount);
KrkValue result = krk_callDirect(self->_init, argCount);
if (!IS_NONE(result)) {
fprintf(stderr, "Warning: Non-None result returned from %s.__init__\n",
self->name->chars);
}
}
return krk_pop();

View File

@ -275,7 +275,7 @@ KRK_Method(bytesiterator,__init__) {
CHECK_ARG(1,bytes,KrkBytes*,bytes);
self->l = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
KRK_Method(bytesiterator,__call__) {
@ -307,7 +307,7 @@ KRK_Method(bytearray,__init__) {
} else {
return krk_runtimeError(vm.exceptions->valueError, "expected bytes");
}
return argv[0];
return NONE_VAL();
}
#undef IS_bytearray

View File

@ -87,7 +87,8 @@ KRK_Method(dict,__init__) {
if (hasKw) {
krk_tableAddAll(AS_DICT(argv[argc]), &self->entries);
}
return argv[0];
return NONE_VAL();
}
KRK_Method(dict,__eq__) {
@ -316,7 +317,7 @@ KRK_Method(dictitems,__init__) {
CHECK_ARG(1,dict,KrkDict*,source);
self->dict = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
KRK_Method(dictitems,__iter__) {
@ -400,7 +401,7 @@ KRK_Method(dictkeys,__init__) {
CHECK_ARG(1,dict,KrkDict*,source);
self->dict = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
KRK_Method(dictkeys,__iter__) {
@ -466,7 +467,7 @@ KRK_Method(dictvalues,__init__) {
CHECK_ARG(1,dict,KrkDict*,source);
self->dict = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
KRK_Method(dictvalues,__iter__) {

View File

@ -195,7 +195,7 @@ KRK_Method(list,__init__) {
if (argc == 2) {
_list_extend(2,(KrkValue[]){argv[0],argv[1]},0);
}
return argv[0];
return NONE_VAL();
}
KRK_Method(list,__mul__) {
@ -517,7 +517,7 @@ KRK_Method(listiterator,__init__) {
CHECK_ARG(1,list,KrkList*,list);
self->l = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
FUNC_SIG(listiterator,__call__) {

View File

@ -1253,8 +1253,11 @@ struct BigInt {
#define CURRENT_CTYPE struct BigInt *
#define CURRENT_NAME self
static void make_long(krk_integer_type t, struct BigInt * self) {
static KrkValue make_long(krk_integer_type t) {
struct BigInt * self = (struct BigInt*)krk_newInstance(KRK_BASE_CLASS(long));
krk_push(OBJECT_VAL(self));
krk_long_init_si(self->value, t);
return krk_pop();
}
static void _long_gcsweep(KrkInstance * self) {
@ -1263,28 +1266,33 @@ static void _long_gcsweep(KrkInstance * self) {
KrkValue krk_int_from_float(double val);
KRK_Method(long,__init__) {
METHOD_TAKES_AT_MOST(1);
KRK_StaticMethod(long,__new__) {
FUNCTION_TAKES_AT_MOST(2);
/* Some less likely scenarios */
if (argc < 2) {
make_long(0,self);
return make_long(0);
} else if (IS_INTEGER(argv[1])) {
make_long(AS_INTEGER(argv[1]),self);
return make_long(AS_INTEGER(argv[1]));
} else if (IS_BOOLEAN(argv[1])) {
make_long(AS_BOOLEAN(argv[1]),self);
return make_long(AS_BOOLEAN(argv[1]));
} else if (IS_FLOATING(argv[1])) {
return krk_int_from_float(AS_FLOATING(argv[1]));
} else if (IS_STRING(argv[1])) {
/* XXX This should probably work like int(...) does and default to base 10... and take a base at all... */
struct BigInt * self = (struct BigInt*)krk_newInstance(KRK_BASE_CLASS(long));
krk_push(OBJECT_VAL(self));
if (krk_long_parse_string(AS_CSTRING(argv[1]),self->value,0,AS_STRING(argv[1])->length)) {
return krk_runtimeError(vm.exceptions->valueError, "invalid literal for long() with base 0: %R", argv[1]);
}
return krk_pop();
} else if (IS_long(argv[1])) {
struct BigInt * self = (struct BigInt*)krk_newInstance(KRK_BASE_CLASS(long));
krk_push(OBJECT_VAL(self));
krk_long_init_copy(self->value,AS_long(argv[1])->value);
} else if (IS_FLOATING(argv[1])) {
return krk_int_from_float(AS_FLOATING(argv[1]));
return krk_pop();
} else {
return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%T'", "int", argv[1]);
}
/* our value should be set */
return argv[0];
}
/**
@ -2012,8 +2020,10 @@ KRK_Method(int,to_bytes) {
}
#undef BIND_METHOD
#undef BIND_STATICMETHOD
/* 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)
#define BIND_STATICMETHOD(klass,method) do { krk_defineNativeStaticMethod(& _ ## klass->methods, #method, _ ## klass ## _ ## method); } while (0)
#define BIND_TRIPLET(klass,name) \
BIND_METHOD(klass,__ ## name ## __); \
BIND_METHOD(klass,__r ## name ## __); \
@ -2025,7 +2035,7 @@ void _createAndBind_longClass(void) {
_long->allocSize = sizeof(struct BigInt);
_long->_ongcsweep = _long_gcsweep;
BIND_METHOD(long,__init__);
BIND_STATICMETHOD(long,__new__);
BIND_METHOD(long,__str__);
BIND_METHOD(long,__eq__);
BIND_METHOD(long,__hash__);

View File

@ -54,7 +54,7 @@ KRK_Method(range,__init__) {
self->step = _step;
}
}
return argv[0];
return NONE_VAL();
}
KRK_Method(range,__repr__) {
@ -90,7 +90,7 @@ KRK_Method(rangeiterator,__init__) {
self->i = i;
self->max = max;
self->step = step;
return argv[0];
return NONE_VAL();
}
KRK_Method(rangeiterator,__call__) {

View File

@ -58,7 +58,7 @@ KRK_Method(set,__init__) {
if (argc == 2) {
if (krk_unpackIterable(argv[1], self, _set_init_callback)) return NONE_VAL();
}
return argv[0];
return NONE_VAL();
}
KRK_Method(set,__contains__) {
@ -312,7 +312,7 @@ KRK_Method(setiterator,__init__) {
CHECK_ARG(1,set,void*,source);
self->set = argv[1];
self->i = 0;
return argv[0];
return NONE_VAL();
}
KRK_Method(setiterator,__call__) {

View File

@ -112,7 +112,8 @@ KRK_Method(slice,__init__) {
self->step = NONE_VAL();
}
}
return argv[0];
return NONE_VAL();
}
KRK_Method(slice,__repr__) {

View File

@ -1012,7 +1012,8 @@ KRK_Method(striterator,__init__) {
krk_push(OBJECT_VAL(self));
krk_attachNamedObject(&self->fields, "s", (KrkObj*)base);
krk_attachNamedValue(&self->fields, "i", INTEGER_VAL(0));
return krk_pop();
krk_pop();
return NONE_VAL();
}
KRK_Method(striterator,__call__) {

View File

@ -152,7 +152,7 @@ KRK_Method(Thread,is_alive) {
KRK_Method(Lock,__init__) {
METHOD_TAKES_NONE(); /* TODO lock options, like recursive or error-checked? */
pthread_mutex_init(&self->mutex, NULL);
return argv[0];
return NONE_VAL();
}
static inline void _pushLockStatus(struct Lock * self, struct StringBuilder * sb) {