diff --git a/src/builtins.c b/src/builtins.c index ad6ef4d..513c912 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -87,15 +87,15 @@ KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw) { #define CURRENT_CTYPE KrkValue #define CURRENT_NAME self -KRK_METHOD(object,__dir__,{ +KRK_Method(object,__dir__) { return krk_dirObject(argc,argv,hasKw); -}) +} -KRK_METHOD(object,__class__,{ +KRK_Method(object,__class__) { return OBJECT_VAL(krk_getType(self)); -}) +} -KRK_METHOD(object,__hash__,{ +KRK_Method(object,__hash__) { if (!IS_OBJECT(self)) { uint32_t hashed; if (krk_hashValue(self, &hashed)) return NONE_VAL(); @@ -107,12 +107,12 @@ KRK_METHOD(object,__hash__,{ obj->flags |= KRK_OBJ_FLAGS_VALID_HASH; } return INTEGER_VAL(obj->hash); -}) +} -KRK_METHOD(object,__eq__,{ +KRK_Method(object,__eq__) { METHOD_TAKES_EXACTLY(1); return BOOLEAN_VAL(argc == 2 && IS_OBJECT(argv[0]) && IS_OBJECT(argv[1]) && AS_OBJECT(argv[0]) == AS_OBJECT(argv[1])); -}) +} /** * object.__str__() / object.__repr__() @@ -127,7 +127,7 @@ KRK_METHOD(object,__eq__,{ * all types should have a string representation available through * those methods. */ -KRK_METHOD(object,__str__,{ +KRK_Method(object,__str__) { KrkClass * type = krk_getType(self); KrkValue module = NONE_VAL(); @@ -153,12 +153,12 @@ KRK_METHOD(object,__str__,{ KrkValue out = OBJECT_VAL(krk_copyString(tmp, len)); free(tmp); return out; -}) +} #undef CURRENT_CTYPE #undef CURRENT_NAME -KRK_FUNC(len,{ +KRK_Function(len) { FUNCTION_TAKES_EXACTLY(1); /* Shortcuts */ if (IS_STRING(argv[0])) return INTEGER_VAL(AS_STRING(argv[0])->codesLength); @@ -169,9 +169,9 @@ KRK_FUNC(len,{ krk_push(argv[0]); return krk_callDirect(type->_len, 1); -}) +} -KRK_FUNC(dir,{ +KRK_Function(dir) { FUNCTION_TAKES_AT_MOST(1); if (argc) { KrkClass * type = krk_getType(argv[0]); @@ -202,15 +202,15 @@ KRK_FUNC(dir,{ FUNC_NAME(list,sort)(1,(KrkValue[]){krk_peek(0)},0); return krk_pop(); /* Return the list */ } -}) +} -KRK_FUNC(repr,{ +KRK_Function(repr) { FUNCTION_TAKES_EXACTLY(1); /* Everything should have a __repr__ */ KrkClass * type = krk_getType(argv[0]); krk_push(argv[0]); return krk_callDirect(type->_reprer, 1); -}) +} #define trySlowMethod(name) do { \ KrkClass * type = krk_getType(argv[0]); \ @@ -225,35 +225,35 @@ KRK_FUNC(repr,{ } \ } while (0) -KRK_FUNC(ord,{ +KRK_Function(ord) { FUNCTION_TAKES_EXACTLY(1); trySlowMethod(vm.specialMethodNames[METHOD_ORD]); return TYPE_ERROR(string of length 1,argv[0]); -}) +} -KRK_FUNC(chr,{ +KRK_Function(chr) { FUNCTION_TAKES_EXACTLY(1); trySlowMethod(vm.specialMethodNames[METHOD_CHR]); return TYPE_ERROR(int,argv[0]); -}) +} -KRK_FUNC(hex,{ +KRK_Function(hex) { FUNCTION_TAKES_EXACTLY(1); trySlowMethod(OBJECT_VAL(S("__hex__"))); return TYPE_ERROR(int,argv[0]); -}) +} -KRK_FUNC(oct,{ +KRK_Function(oct) { FUNCTION_TAKES_EXACTLY(1); trySlowMethod(OBJECT_VAL(S("__oct__"))); return TYPE_ERROR(int,argv[0]); -}) +} -KRK_FUNC(bin,{ +KRK_Function(bin) { FUNCTION_TAKES_EXACTLY(1); trySlowMethod(OBJECT_VAL(S("__bin__"))); return TYPE_ERROR(int,argv[0]); -}) +} #define KRK_STRING_FAST(string,offset) (uint32_t)\ ((string->obj.flags & KRK_OBJ_FLAGS_STRING_MASK) <= (KRK_OBJ_FLAGS_STRING_UCS1) ? ((uint8_t*)string->codes)[offset] : \ @@ -341,12 +341,12 @@ static int _any_callback(void * context, const KrkValue * values, size_t count) return 0; } -KRK_FUNC(any,{ +KRK_Function(any) { FUNCTION_TAKES_EXACTLY(1); struct SimpleContext context = { BOOLEAN_VAL(0) }; krk_unpackIterable(argv[0], &context, _any_callback); return context.base; -}) +} static int _all_callback(void * context, const KrkValue * values, size_t count) { struct SimpleContext * _context = context; @@ -359,12 +359,12 @@ static int _all_callback(void * context, const KrkValue * values, size_t count) return 0; } -KRK_FUNC(all,{ +KRK_Function(all) { FUNCTION_TAKES_EXACTLY(1); struct SimpleContext context = { BOOLEAN_VAL(1) }; krk_unpackIterable(argv[0], &context, _all_callback); return context.base; -}) +} #define CURRENT_CTYPE KrkInstance * #define CURRENT_NAME self @@ -372,7 +372,7 @@ KRK_FUNC(all,{ #define IS_map(o) (krk_isInstanceOf(o,map)) #define AS_map(o) (AS_INSTANCE(o)) static KrkClass * map; -KRK_METHOD(map,__init__,{ +KRK_Method(map,__init__) { METHOD_TAKES_AT_LEAST(2); /* Attach the function to it */ @@ -399,14 +399,14 @@ KRK_METHOD(map,__init__,{ } return argv[0]; -}) +} -KRK_METHOD(map,__iter__,{ +KRK_Method(map,__iter__) { METHOD_TAKES_NONE(); return OBJECT_VAL(self); -}) +} -KRK_METHOD(map,__call__,{ +KRK_Method(map,__call__) { METHOD_TAKES_NONE(); size_t stackOffset = krk_currentThread.stackTop - krk_currentThread.stack; @@ -438,12 +438,12 @@ KRK_METHOD(map,__call__,{ KrkValue val = krk_callStack(AS_TUPLE(iterators)->values.count); krk_currentThread.stackTop = krk_currentThread.stack + stackOffset; return val; -}) +} #define IS_zip(o) (krk_isInstanceOf(o,zip)) #define AS_zip(o) (AS_INSTANCE(o)) static KrkClass * zip; -KRK_METHOD(zip,__init__,{ +KRK_Method(zip,__init__) { if (hasKw) return krk_runtimeError(vm.exceptions->typeError, "%s() takes no keyword arguments", "zip"); KrkTuple * iters = krk_newTuple(argc - 1); @@ -464,14 +464,14 @@ KRK_METHOD(zip,__init__,{ } return argv[0]; -}) +} -KRK_METHOD(zip,__iter__,{ +KRK_Method(zip,__iter__) { METHOD_TAKES_NONE(); return OBJECT_VAL(self); -}) +} -KRK_METHOD(zip,__call__,{ +KRK_Method(zip,__call__) { METHOD_TAKES_NONE(); /* Get members */ @@ -495,12 +495,12 @@ KRK_METHOD(zip,__call__,{ /* Return tuple */ return krk_pop(); -}) +} #define IS_filter(o) (krk_isInstanceOf(o,filter)) #define AS_filter(o) (AS_INSTANCE(o)) static KrkClass * filter; -KRK_METHOD(filter,__init__,{ +KRK_Method(filter,__init__) { METHOD_TAKES_EXACTLY(2); krk_attachNamedValue(&self->fields, "_function", argv[1]); KrkClass * type = krk_getType(argv[2]); @@ -512,14 +512,14 @@ KRK_METHOD(filter,__init__,{ if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) return NONE_VAL(); krk_attachNamedValue(&self->fields, "_iterator", asIter); return argv[0]; -}) +} -KRK_METHOD(filter,__iter__,{ +KRK_Method(filter,__iter__) { METHOD_TAKES_NONE(); return OBJECT_VAL(self); -}) +} -KRK_METHOD(filter,__call__,{ +KRK_Method(filter,__call__) { METHOD_TAKES_NONE(); size_t stackOffset = krk_currentThread.stackTop - krk_currentThread.stack; KrkValue function = NONE_VAL(); @@ -557,12 +557,12 @@ KRK_METHOD(filter,__call__,{ krk_currentThread.stackTop = krk_currentThread.stack + stackOffset; return out; } -}) +} #define IS_enumerate(o) (krk_isInstanceOf(o,enumerate)) #define AS_enumerate(o) (AS_INSTANCE(o)) static KrkClass * enumerate; -KRK_METHOD(enumerate,__init__,{ +KRK_Method(enumerate,__init__) { METHOD_TAKES_EXACTLY(1); KrkValue start = INTEGER_VAL(0); if (hasKw) krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("start")), &start); @@ -580,15 +580,15 @@ KRK_METHOD(enumerate,__init__,{ krk_attachNamedValue(&self->fields, "_iterator", asIter); return argv[0]; -}) +} -KRK_METHOD(enumerate,__iter__,{ +KRK_Method(enumerate,__iter__) { METHOD_TAKES_NONE(); return OBJECT_VAL(self); -}) +} extern KrkValue krk_operator_add (KrkValue a, KrkValue b); -KRK_METHOD(enumerate,__call__,{ +KRK_Method(enumerate,__call__) { METHOD_TAKES_NONE(); size_t stackOffset = krk_currentThread.stackTop - krk_currentThread.stack; KrkValue counter = NONE_VAL(); @@ -624,7 +624,7 @@ KRK_METHOD(enumerate,__call__,{ KrkValue out = krk_pop(); krk_currentThread.stackTop = krk_currentThread.stack + stackOffset; return out; -}) +} static int _sum_callback(void * context, const KrkValue * values, size_t count) { struct SimpleContext * _context = context; @@ -635,7 +635,7 @@ static int _sum_callback(void * context, const KrkValue * values, size_t count) return 0; } -KRK_FUNC(sum,{ +KRK_Function(sum) { FUNCTION_TAKES_AT_LEAST(1); KrkValue base = INTEGER_VAL(0); if (hasKw) { @@ -644,7 +644,7 @@ KRK_FUNC(sum,{ struct SimpleContext context = { base }; if (krk_unpackIterable(argv[0], &context, _sum_callback)) return NONE_VAL(); return context.base; -}) +} static int _min_callback(void * context, const KrkValue * values, size_t count) { struct SimpleContext * _context = context; @@ -659,7 +659,7 @@ static int _min_callback(void * context, const KrkValue * values, size_t count) return 0; } -KRK_FUNC(min,{ +KRK_Function(min) { FUNCTION_TAKES_AT_LEAST(1); struct SimpleContext context = { KWARGS_VAL(0) }; if (argc > 1) { @@ -669,7 +669,7 @@ KRK_FUNC(min,{ } if (IS_KWARGS(context.base)) return krk_runtimeError(vm.exceptions->valueError, "empty argument to %s()", "min"); return context.base; -}) +} static int _max_callback(void * context, const KrkValue * values, size_t count) { struct SimpleContext * _context = context; @@ -684,7 +684,7 @@ static int _max_callback(void * context, const KrkValue * values, size_t count) return 0; } -KRK_FUNC(max,{ +KRK_Function(max) { FUNCTION_TAKES_AT_LEAST(1); struct SimpleContext context = { KWARGS_VAL(0) }; if (argc > 1) { @@ -694,9 +694,9 @@ KRK_FUNC(max,{ } if (IS_KWARGS(context.base)) return krk_runtimeError(vm.exceptions->valueError, "empty argument to %s()", "max"); return context.base; -}) +} -KRK_FUNC(print,{ +KRK_Function(print) { KrkValue sepVal; KrkValue endVal; char * sep = " "; size_t sepLen = 1; @@ -734,14 +734,16 @@ KRK_FUNC(print,{ fputc(thingToPrint[j], stdout); } } -}) + + return NONE_VAL(); +} /** * globals() * * Returns a dict of names -> values for all the globals. */ -KRK_FUNC(globals,{ +KRK_Function(globals) { FUNCTION_TAKES_NONE(); /* Make a new empty dict */ KrkValue dict = krk_dict_of(0, NULL, 0); @@ -751,14 +753,14 @@ KRK_FUNC(globals,{ krk_pop(); return dict; -}) +} /** * locals() * * This is a bit trickier. Local names are... complicated. But we can do this! */ -KRK_FUNC(locals,{ +KRK_Function(locals) { FUNCTION_TAKES_AT_MOST(1); KrkValue dict = krk_dict_of(0, NULL, 0); krk_push(dict); @@ -815,9 +817,9 @@ KRK_FUNC(locals,{ } return krk_pop(); -}) +} -KRK_FUNC(isinstance,{ +KRK_Function(isinstance) { FUNCTION_TAKES_EXACTLY(2); if (IS_CLASS(argv[1])) { return BOOLEAN_VAL(krk_isInstanceOf(argv[0], AS_CLASS(argv[1]))); @@ -831,7 +833,7 @@ KRK_FUNC(isinstance,{ } else { return TYPE_ERROR(class or tuple,argv[1]); } -}) +} static int _isSubClass(KrkClass * cls, KrkClass * base) { while (cls) { @@ -841,7 +843,7 @@ static int _isSubClass(KrkClass * cls, KrkClass * base) { return 0; } -KRK_FUNC(issubclass,{ +KRK_Function(issubclass) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,class,KrkClass*,cls); if (IS_CLASS(argv[1])) { @@ -856,12 +858,12 @@ KRK_FUNC(issubclass,{ } else { return TYPE_ERROR(class or tuple,argv[1]); } -}) +} #define IS_module(o) (IS_INSTANCE(o)) #define AS_module(o) (AS_INSTANCE(o)) -KRK_METHOD(module,__repr__,{ +KRK_Method(module,__repr__) { KrkValue name = NONE_VAL(); krk_tableGet(&self->fields, vm.specialMethodNames[METHOD_NAME], &name); @@ -884,9 +886,9 @@ KRK_METHOD(module,__repr__,{ KrkValue out = OBJECT_VAL(krk_copyString(tmp, len)); free(tmp); return out; -}) +} -KRK_FUNC(getattr,{ +KRK_Function(getattr) { FUNCTION_TAKES_AT_LEAST(2); KrkValue object = argv[0]; CHECK_ARG(1,str,KrkString*,property); @@ -895,31 +897,31 @@ KRK_FUNC(getattr,{ } else { return krk_valueGetAttribute(object, property->chars); } -}) +} -KRK_FUNC(setattr,{ +KRK_Function(setattr) { FUNCTION_TAKES_EXACTLY(3); KrkValue object = argv[0]; CHECK_ARG(1,str,KrkString*,property); KrkValue value = argv[2]; return krk_valueSetAttribute(object, property->chars, value); -}) +} -KRK_FUNC(hasattr,{ +KRK_Function(hasattr) { FUNCTION_TAKES_AT_LEAST(2); KrkValue object = argv[0]; CHECK_ARG(1,str,KrkString*,property); return BOOLEAN_VAL(!IS_KWARGS(krk_valueGetAttribute_default(object, property->chars, KWARGS_VAL(0)))); -}) +} -KRK_FUNC(delattr,{ +KRK_Function(delattr) { FUNCTION_TAKES_AT_LEAST(2); KrkValue object = argv[0]; CHECK_ARG(1,str,KrkString*,property); return krk_valueDelAttribute(object, property->chars); -}) +} #define IS_Helper(o) (krk_isInstanceOf(o, Helper)) @@ -927,11 +929,11 @@ KRK_FUNC(delattr,{ #define IS_LicenseReader(o) (krk_isInstanceOf(o, LicenseReader)) #define AS_LicenseReader(o) (AS_INSTANCE(o)) -KRK_METHOD(Helper,__repr__,{ +KRK_Method(Helper,__repr__) { return OBJECT_VAL(S("Type help() for more help, or help(obj) to describe an object.")); -}) +} -KRK_METHOD(Helper,__call__,{ +KRK_Method(Helper,__call__) { METHOD_TAKES_AT_MOST(1); if (!krk_doRecursiveModuleLoad(S("help"))) return NONE_VAL(); KrkValue helpModule = krk_pop(); @@ -950,13 +952,13 @@ KRK_METHOD(Helper,__call__,{ } return krk_runtimeError(vm.exceptions->typeError, "unexpected error"); -}) +} -KRK_METHOD(LicenseReader,__repr__,{ +KRK_Method(LicenseReader,__repr__) { return OBJECT_VAL(S("Copyright 2020-2022 K. Lange . Type `license()` for more information.")); -}) +} -KRK_METHOD(LicenseReader,__call__,{ +KRK_Method(LicenseReader,__call__) { METHOD_TAKES_NONE(); if (!krk_doRecursiveModuleLoad(S("help"))) return NONE_VAL(); KrkValue helpModule = krk_pop(); @@ -970,11 +972,11 @@ KRK_METHOD(LicenseReader,__call__,{ } return krk_runtimeError(vm.exceptions->typeError, "unexpected error"); -}) +} #define IS_property(o) (krk_isInstanceOf(o,property)) #define AS_property(o) (AS_INSTANCE(o)) -KRK_METHOD(property,__init__,{ +KRK_Method(property,__init__) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(2); /* TODO fdel */ krk_attachNamedValue(&self->fields, "fget", argv[1]); @@ -1001,15 +1003,15 @@ KRK_METHOD(property,__init__,{ krk_attachNamedValue(&self->fields, "fset", argv[2]); return argv[0]; -}) +} -KRK_METHOD(property,setter,{ +KRK_Method(property,setter) { METHOD_TAKES_EXACTLY(1); krk_attachNamedValue(&self->fields, "fset", argv[1]); return argv[0]; /* Return the original property */ -}) +} -KRK_METHOD(property,__get__,{ +KRK_Method(property,__get__) { METHOD_TAKES_EXACTLY(1); /* the owner */ KrkValue fget; @@ -1019,9 +1021,9 @@ KRK_METHOD(property,__get__,{ krk_push(fget); krk_push(argv[1]); return krk_callStack(1); -}) +} -KRK_METHOD(property,__set__,{ +KRK_Method(property,__set__) { METHOD_TAKES_EXACTLY(2); /* the owner and the value */ KrkValue fset; @@ -1041,28 +1043,28 @@ KRK_METHOD(property,__set__,{ } return krk_runtimeError(vm.exceptions->attributeError, "attribute can not be set"); -}) +} -KRK_FUNC(id,{ +KRK_Function(id) { FUNCTION_TAKES_EXACTLY(1); if (!IS_OBJECT(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "'%s' has no identity", krk_typeName(argv[0])); return INTEGER_VAL((size_t)AS_OBJECT(argv[0])); -}) +} -KRK_FUNC(hash,{ +KRK_Function(hash) { FUNCTION_TAKES_EXACTLY(1); uint32_t hashed; if (krk_hashValue(argv[0], &hashed)) return NONE_VAL(); return INTEGER_VAL(hashed); -}) +} -KRK_FUNC(next,{ +KRK_Function(next) { FUNCTION_TAKES_EXACTLY(1); krk_push(argv[0]); return krk_callStack(0); -}) +} -KRK_FUNC(abs,{ +KRK_Function(abs) { FUNCTION_TAKES_EXACTLY(1); if (IS_INTEGER(argv[0])) { krk_integer_type i = AS_INTEGER(argv[0]); @@ -1075,7 +1077,7 @@ KRK_FUNC(abs,{ return krk_runtimeError(vm.exceptions->typeError, "bad operand type for 'abs()': '%s'", krk_typeName(argv[0])); } -}) +} static void module_sweep(KrkInstance * inst) { #ifndef STATIC_ONLY diff --git a/src/debug.c b/src/debug.c index adb79c9..c973067 100644 --- a/src/debug.c +++ b/src/debug.c @@ -341,11 +341,12 @@ int krk_debug_enableBreakpoint(int breakIndex) { breakpoints[breakIndex].inFunction->chunk.code[breakpoints[breakIndex].offset] = OP_BREAKPOINT; return 0; } -KRK_FUNC(enablebreakpoint,{ +KRK_Function(enablebreakpoint) { CHECK_ARG(0,int,krk_integer_type,breakIndex); if (krk_debug_enableBreakpoint(breakIndex)) return krk_runtimeError(vm.exceptions->indexError, "invalid breakpoint id"); -}) + return NONE_VAL(); +} int krk_debug_disableBreakpoint(int breakIndex) { if (breakIndex < 0 || breakIndex >= breakpointsCount || breakpoints[breakIndex].inFunction == NULL) @@ -357,11 +358,12 @@ int krk_debug_disableBreakpoint(int breakIndex) { } return 0; } -KRK_FUNC(disablebreakpoint,{ +KRK_Function(disablebreakpoint) { CHECK_ARG(0,int,krk_integer_type,breakIndex); if (krk_debug_disableBreakpoint(breakIndex)) return krk_runtimeError(vm.exceptions->indexError, "invalid breakpoint id"); -}) + return NONE_VAL(); +} int krk_debug_removeBreakpoint(int breakIndex) { if (breakIndex < 0 || breakIndex >= breakpointsCount || breakpoints[breakIndex].inFunction == NULL) @@ -373,13 +375,14 @@ int krk_debug_removeBreakpoint(int breakIndex) { } return 0; } -KRK_FUNC(delbreakpoint,{ +KRK_Function(delbreakpoint) { CHECK_ARG(0,int,krk_integer_type,breakIndex); if (krk_debug_removeBreakpoint(breakIndex)) return krk_runtimeError(vm.exceptions->indexError, "invalid breakpoint id"); -}) + return NONE_VAL(); +} -KRK_FUNC(addbreakpoint,{ +KRK_Function(addbreakpoint) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(1,int,krk_integer_type,lineNo); @@ -425,7 +428,7 @@ KRK_FUNC(addbreakpoint,{ return krk_runtimeError(vm.exceptions->baseException, "Could not add breakpoint."); return INTEGER_VAL(result); -}) +} /* * Begin debugger utility functions. @@ -566,7 +569,7 @@ int krk_debugBreakpointHandler(void) { /** * dis.dis(object) */ -KRK_FUNC(dis,{ +KRK_Function(dis) { FUNCTION_TAKES_EXACTLY(1); if (IS_CLOSURE(argv[0])) { @@ -599,9 +602,9 @@ KRK_FUNC(dis,{ } return NONE_VAL(); -}) +} -KRK_FUNC(build,{ +KRK_Function(build) { FUNCTION_TAKES_AT_LEAST(1); FUNCTION_TAKES_AT_MOST(2); CHECK_ARG(0,str,KrkString*,code); @@ -620,7 +623,7 @@ KRK_FUNC(build,{ krk_pop(); if (c) return OBJECT_VAL(c); else return NONE_VAL(); -}) +} #define SIMPLE(opc) case opc: size = 1; break; #define CONSTANT(opc,more) case opc: { constant = chunk->code[offset + 1]; size = 2; more; break; } \ @@ -700,11 +703,11 @@ static KrkValue _examineInternal(KrkCodeObject* func) { return krk_pop(); } -KRK_FUNC(examine,{ +KRK_Function(examine) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,codeobject,KrkCodeObject*,func); return _examineInternal(func); -}) +} #undef SIMPLE #undef OPERANDB diff --git a/src/exceptions.c b/src/exceptions.c index 7fa4fe6..997691c 100644 --- a/src/exceptions.c +++ b/src/exceptions.c @@ -36,14 +36,14 @@ * * @param arg Optional string to attach to the exception object. */ -KRK_METHOD(Exception,__init__,{ +KRK_Method(Exception,__init__) { if (argc > 1) { krk_attachNamedValue(&self->fields, "arg", argv[1]); } krk_attachNamedValue(&self->fields, "__cause__", NONE_VAL()); krk_attachNamedValue(&self->fields, "__context__", NONE_VAL()); return argv[0]; -}) +} /** * @brief Create a string representation of an Exception. @@ -52,7 +52,7 @@ KRK_METHOD(Exception,__init__,{ * * Generates a string representation of the form @c "Exception(arg)" . */ -KRK_METHOD(Exception,__repr__,{ +KRK_Method(Exception,__repr__) { KrkValue arg; struct StringBuilder sb = {0}; @@ -69,7 +69,7 @@ KRK_METHOD(Exception,__repr__,{ pushStringBuilder(&sb, ')'); return finishStringBuilder(&sb); -}) +} /** * @brief Obtain a descriptive string from an exception. @@ -79,7 +79,7 @@ KRK_METHOD(Exception,__repr__,{ * For most exceptions, this is the 'arg' value attached at initialization * and is printed during a traceback after the name of the exception type. */ -KRK_METHOD(Exception,__str__,{ +KRK_Method(Exception,__str__) { KrkValue arg; if (!krk_tableGet(&self->fields, OBJECT_VAL(S("arg")), &arg) || IS_NONE(arg)) { return OBJECT_VAL(S("")); @@ -93,11 +93,10 @@ KRK_METHOD(Exception,__str__,{ } else { return arg; } -}) +} -KRK_METHOD(KeyError,__str__,{ +KRK_Method(KeyError,__str__) { if (!IS_INSTANCE(argv[0])) return NONE_VAL(); /* uh oh */ - KrkInstance * self = AS_INSTANCE(argv[0]); KrkValue arg; if (krk_tableGet(&self->fields, OBJECT_VAL(S("arg")), &arg)) { KrkClass * type = krk_getType(arg); @@ -107,7 +106,7 @@ KRK_METHOD(KeyError,__str__,{ } } return FUNC_NAME(Exception,__str__)(argc,argv,hasKw); -}) +} /** * @brief Generate printable text for a syntax error. @@ -120,7 +119,7 @@ KRK_METHOD(KeyError,__str__,{ * {str(Exception)} for syntax errors and they handle the rest. This is a bit * of a kludge, but it works for now. */ -KRK_METHOD(SyntaxError,__str__,{ +KRK_Method(SyntaxError,__str__) { /* .arg */ KrkValue file, line, lineno, colno, arg, func, width; if (!krk_tableGet(&self->fields, OBJECT_VAL(S("file")), &file) || !IS_STRING(file)) goto _badSyntaxError; @@ -169,7 +168,7 @@ KRK_METHOD(SyntaxError,__str__,{ _badSyntaxError: return OBJECT_VAL(S("SyntaxError: invalid syntax")); -}) +} /** diff --git a/src/fileio.c b/src/fileio.c index cc11be8..be449c8 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -49,7 +49,7 @@ struct Directory { #define CURRENT_CTYPE struct File * #define CURRENT_NAME self -KRK_FUNC(open,{ +KRK_Function(open) { FUNCTION_TAKES_AT_LEAST(1); FUNCTION_TAKES_AT_MOST(2); CHECK_ARG(0,str,KrkString*,filename); @@ -93,11 +93,11 @@ KRK_FUNC(open,{ krk_pop(); krk_pop(); return OBJECT_VAL(fileObject); -}) +} #define BLOCK_SIZE 1024 -KRK_METHOD(File,__str__,{ +KRK_Method(File,__str__) { METHOD_TAKES_NONE(); KrkValue filename; KrkValue modestr; @@ -109,9 +109,9 @@ KRK_METHOD(File,__str__,{ KrkString * out = krk_copyString(tmp, len); free(tmp); return OBJECT_VAL(out); -}) +} -KRK_METHOD(File,readline,{ +KRK_Method(File,readline) { METHOD_TAKES_NONE(); FILE * file = self->filePtr; @@ -152,9 +152,9 @@ _finish_line: (void)0; KrkString * out = krk_copyString(buffer,sizeRead); free(buffer); return OBJECT_VAL(out); -}) +} -KRK_METHOD(File,readlines,{ +KRK_Method(File,readlines) { METHOD_TAKES_NONE(); KrkValue myList = krk_list_of(0,NULL,0); krk_push(myList); @@ -171,9 +171,9 @@ KRK_METHOD(File,readlines,{ krk_pop(); /* myList */ return myList; -}) +} -KRK_METHOD(File,read,{ +KRK_Method(File,read) { METHOD_TAKES_AT_MOST(1); krk_integer_type sizeToRead = -1; @@ -225,9 +225,9 @@ KRK_METHOD(File,read,{ KrkString * out = krk_copyString(buffer,sizeRead); free(buffer); return OBJECT_VAL(out); -}) +} -KRK_METHOD(File,write,{ +KRK_Method(File,write) { METHOD_TAKES_EXACTLY(1); if (!IS_STRING(argv[1])) return TYPE_ERROR(str,argv[1]); /* Find the file ptr reference */ @@ -238,29 +238,33 @@ KRK_METHOD(File,write,{ } return INTEGER_VAL(fwrite(AS_CSTRING(argv[1]), 1, AS_STRING(argv[1])->length, file)); -}) +} -KRK_METHOD(File,close,{ +KRK_Method(File,close) { METHOD_TAKES_NONE(); FILE * file = self->filePtr; if (file) fclose(file); self->filePtr = NULL; -}) + return NONE_VAL(); +} -KRK_METHOD(File,flush,{ +KRK_Method(File,flush) { METHOD_TAKES_NONE(); FILE * file = self->filePtr; if (file) fflush(file); -}) + return NONE_VAL(); +} -KRK_METHOD(File,__init__,{ +KRK_Method(File,__init__) { return krk_runtimeError(vm.exceptions->typeError, "File objects can not be instantiated; use fileio.open() to obtain File objects."); -}) +} -KRK_METHOD(File,__enter__,{}) -KRK_METHOD(File,__exit__,{ +KRK_Method(File,__enter__) { + return NONE_VAL(); +} +KRK_Method(File,__exit__) { return FUNC_NAME(File,close)(1,argv,0); -}) +} static void makeFileInstance(KrkInstance * module, const char name[], FILE * file) { KrkInstance * fileObject = krk_newInstance(File); @@ -278,7 +282,7 @@ static void makeFileInstance(KrkInstance * module, const char name[], FILE * fil krk_pop(); /* fileObject */ } -KRK_METHOD(BinaryFile,readline,{ +KRK_Method(BinaryFile,readline) { METHOD_TAKES_NONE(); FILE * file = self->filePtr; @@ -319,9 +323,9 @@ _finish_line: (void)0; KrkBytes * out = krk_newBytes(sizeRead, (unsigned char*)buffer); free(buffer); return OBJECT_VAL(out); -}) +} -KRK_METHOD(BinaryFile,readlines,{ +KRK_Method(BinaryFile,readlines) { METHOD_TAKES_NONE(); KrkValue myList = krk_list_of(0,NULL,0); krk_push(myList); @@ -338,9 +342,9 @@ KRK_METHOD(BinaryFile,readlines,{ krk_pop(); /* myList */ return myList; -}) +} -KRK_METHOD(BinaryFile,read,{ +KRK_Method(BinaryFile,read) { METHOD_TAKES_AT_MOST(1); krk_integer_type sizeToRead = -1; @@ -393,9 +397,9 @@ KRK_METHOD(BinaryFile,read,{ KrkBytes * out = krk_newBytes(sizeRead, (unsigned char*)buffer); free(buffer); return OBJECT_VAL(out); -}) +} -KRK_METHOD(BinaryFile,write,{ +KRK_Method(BinaryFile,write) { METHOD_TAKES_EXACTLY(1); if (!IS_BYTES(argv[1])) return TYPE_ERROR(bytes,argv[1]); /* Find the file ptr reference */ @@ -406,7 +410,7 @@ KRK_METHOD(BinaryFile,write,{ } return INTEGER_VAL(fwrite(AS_BYTES(argv[1])->bytes, 1, AS_BYTES(argv[1])->length, file)); -}) +} #undef CURRENT_CTYPE @@ -426,7 +430,7 @@ static void _dir_sweep(KrkInstance * self) { } } -KRK_FUNC(opendir,{ +KRK_Function(opendir) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,str,KrkString*,path); @@ -440,11 +444,11 @@ KRK_FUNC(opendir,{ dirObj->dirPtr = dir; return krk_pop(); -}) +} #define CURRENT_CTYPE struct Directory * -KRK_METHOD(Directory,__call__,{ +KRK_Method(Directory,__call__) { METHOD_TAKES_NONE(); if (!self->dirPtr) return argv[0]; struct dirent * entry = readdir(self->dirPtr); @@ -457,22 +461,23 @@ KRK_METHOD(Directory,__call__,{ krk_attachNamedValue(AS_DICT(outDict), "inode", INTEGER_VAL(entry->d_ino)); return krk_pop(); -}) +} -KRK_METHOD(Directory,__iter__,{ +KRK_Method(Directory,__iter__) { METHOD_TAKES_NONE(); return argv[0]; -}) +} -KRK_METHOD(Directory,close,{ +KRK_Method(Directory,close) { METHOD_TAKES_NONE(); if (self->dirPtr) { closedir(self->dirPtr); self->dirPtr = NULL; } -}) + return NONE_VAL(); +} -KRK_METHOD(Directory,__repr__,{ +KRK_Method(Directory,__repr__) { METHOD_TAKES_NONE(); KrkValue path; if (!krk_tableGet(&self->inst.fields, OBJECT_VAL(S("path")), &path) || !IS_STRING(path)) @@ -484,12 +489,14 @@ KRK_METHOD(Directory,__repr__,{ KrkString * out = krk_copyString(tmp, len); free(tmp); return OBJECT_VAL(out); -}) +} -KRK_METHOD(Directory,__enter__,{}) -KRK_METHOD(Directory,__exit__,{ +KRK_Method(Directory,__enter__) { + return NONE_VAL(); +} +KRK_Method(Directory,__exit__) { return FUNC_NAME(Directory,close)(1,argv,0); -}) +} _noexport void _createAndBind_fileioMod(void) { diff --git a/src/kuroko.c b/src/kuroko.c index 4e46b78..98e69c6 100644 --- a/src/kuroko.c +++ b/src/kuroko.c @@ -39,12 +39,13 @@ static int enableRline = 1; static int exitRepl = 0; static int pasteEnabled = 0; -KRK_FUNC(exit,{ +KRK_Function(exit) { FUNCTION_TAKES_NONE(); exitRepl = 1; -}) + return NONE_VAL(); +} -KRK_FUNC(paste,{ +KRK_Function(paste) { FUNCTION_TAKES_AT_MOST(1); if (argc) { CHECK_ARG(0,bool,int,enabled); @@ -53,7 +54,8 @@ KRK_FUNC(paste,{ pasteEnabled = !pasteEnabled; } fprintf(stderr, "Pasting is %s.\n", pasteEnabled ? "enabled" : "disabled"); -}) + return NONE_VAL(); +} static int doRead(char * buf, size_t bufSize) { #ifndef NO_RLINE @@ -115,7 +117,7 @@ _exit: * In an interactive session, presents the configured prompt without * a trailing linefeed. */ -KRK_FUNC(input,{ +KRK_Function(input) { FUNCTION_TAKES_AT_MOST(1); char * prompt = ""; @@ -143,7 +145,7 @@ KRK_FUNC(input,{ } return readLine(prompt, promptLength, syntaxHighlighter); -}) +} #ifndef NO_RLINE /** diff --git a/src/kuroko/util.h b/src/kuroko/util.h index b04b59a..42747b6 100644 --- a/src/kuroko/util.h +++ b/src/kuroko/util.h @@ -64,6 +64,8 @@ #define FUNC_NAME(klass, name) _ ## klass ## _ ## name #define FUNC_SIG(klass, name) _noexport KrkValue FUNC_NAME(klass,name) (int argc, const KrkValue argv[], int hasKw) + +/* These forms are deprecated. */ #define KRK_METHOD(klass, name, ...) FUNC_SIG(klass, name) { \ static __attribute__ ((unused)) const char* _method_name = # name; \ CHECK_ARG(0,klass,CURRENT_CTYPE,CURRENT_NAME); \ @@ -81,6 +83,33 @@ #define BIND_PROP(klass,method) krk_defineNativeProperty(&klass->methods, #method, _ ## klass ## _ ## method) #define BIND_FUNC(module,func) krk_defineNative(&module->fields, #func, _krk_ ## func) +#define KRK_Method_internal_name(klass, name) \ + _krk_method_ ## klass ## _ ## name +#define KRK_Method_internal_sig(klass, name) \ + static inline KrkValue KRK_Method_internal_name(klass,name) (const char * _method_name, CURRENT_CTYPE CURRENT_NAME, int argc, const KrkValue argv[], int hasKw) + +#define KRK_Method(klass, name) \ + KRK_Method_internal_sig(klass, name); \ + FUNC_SIG(klass, name) { \ + static const char * _method_name = # name; \ + CHECK_ARG(0,klass,CURRENT_CTYPE,CURRENT_NAME); \ + return KRK_Method_internal_name(klass,name)(_method_name, CURRENT_NAME, argc, argv, hasKw); \ + } \ + KRK_Method_internal_sig(klass,name) + +#define KRK_Function_internal_name(name) \ + _krk_function_ ## name +#define KRK_Function_internal_sig(name) \ + static inline KrkValue KRK_Function_internal_name(name) (const char * _method_name, int argc, const KrkValue argv[], int hasKw) + +#define KRK_Function(name) \ + KRK_Function_internal_sig(name); \ + static KrkValue _krk_ ## name (int argc, const KrkValue argv[], int hasKw) { \ + static const char* _method_name = # name; \ + return KRK_Function_internal_name(name)(_method_name,argc,argv,hasKw); \ + } \ + KRK_Function_internal_sig(name) + /** * @brief Inline flexible string array. */ diff --git a/src/memory.c b/src/memory.c index 3c10da2..1dc3249 100644 --- a/src/memory.c +++ b/src/memory.c @@ -558,21 +558,23 @@ size_t krk_collectGarbage(void) { return out; } -KRK_FUNC(collect,{ +KRK_Function(collect) { FUNCTION_TAKES_NONE(); if (&krk_currentThread != vm.threads) return krk_runtimeError(vm.exceptions->valueError, "only the main thread can do that"); return INTEGER_VAL(krk_collectGarbage()); -}) +} -KRK_FUNC(pause,{ +KRK_Function(pause) { FUNCTION_TAKES_NONE(); vm.globalFlags |= (KRK_GLOBAL_GC_PAUSED); -}) + return NONE_VAL(); +} -KRK_FUNC(resume,{ +KRK_Function(resume) { FUNCTION_TAKES_NONE(); vm.globalFlags &= ~(KRK_GLOBAL_GC_PAUSED); -}) + return NONE_VAL(); +} _noexport void _createAndBind_gcMod(void) { diff --git a/src/modules/module_random.c b/src/modules/module_random.c index 6a5a0d8..b799165 100644 --- a/src/modules/module_random.c +++ b/src/modules/module_random.c @@ -32,15 +32,15 @@ void _srand(unsigned int seed) { w = 88675123; } -KRK_FUNC(random,{ +KRK_Function(random) { FUNCTION_TAKES_NONE(); double r = (double)_rand() / (double)(RAND_MAX); return FLOATING_VAL(r); -}) +} -KRK_FUNC(seed,{ +KRK_Function(seed) { FUNCTION_TAKES_AT_MOST(1); int seed; @@ -55,7 +55,8 @@ KRK_FUNC(seed,{ } _srand(seed); -}) + return NONE_VAL(); +} KrkValue krk_module_onload_random(void) { KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass); diff --git a/src/modules/module_socket.c b/src/modules/module_socket.c index 5cbcd53..927a44f 100644 --- a/src/modules/module_socket.c +++ b/src/modules/module_socket.c @@ -50,7 +50,7 @@ struct socket { } \ } -KRK_METHOD(socket,__init__,{ +KRK_Method(socket,__init__) { METHOD_TAKES_AT_MOST(3); /* Complex argument processing time... */ @@ -70,7 +70,7 @@ KRK_METHOD(socket,__init__,{ self->proto = proto; return argv[0]; -}) +} static char * _af_name(int afval) { static char tmp[30]; @@ -102,12 +102,12 @@ static char * _sock_type(int type) { } } -KRK_METHOD(socket,__repr__,{ +KRK_Method(socket,__repr__) { char tmp[4096]; size_t len = snprintf(tmp, 4096, "", self->sockfd, _af_name(self->family), _sock_type(self->type), self->proto); return OBJECT_VAL(krk_copyString(tmp,len)); -}) +} static int socket_parse_address(struct socket * self, KrkValue address, struct sockaddr_storage *sock_addr, socklen_t *sock_size) { if (self->family == AF_INET) { @@ -179,7 +179,7 @@ static int socket_parse_address(struct socket * self, KrkValue address, struct s return 1; } -KRK_METHOD(socket,connect,{ +KRK_Method(socket,connect) { METHOD_TAKES_EXACTLY(1); struct sockaddr_storage sock_addr; @@ -198,9 +198,11 @@ KRK_METHOD(socket,connect,{ if (result < 0) { return krk_runtimeError(SocketError, "Socket error: %s", strerror(errno)); } -}) -KRK_METHOD(socket,bind,{ + return NONE_VAL(); +} + +KRK_Method(socket,bind) { METHOD_TAKES_EXACTLY(1); struct sockaddr_storage sock_addr; @@ -219,9 +221,11 @@ KRK_METHOD(socket,bind,{ if (result < 0) { return krk_runtimeError(SocketError, "Socket error: %s", strerror(errno)); } -}) -KRK_METHOD(socket,listen,{ + return NONE_VAL(); +} + +KRK_Method(socket,listen) { METHOD_TAKES_AT_MOST(1); int backlog = 0; if (argc > 1) { @@ -233,9 +237,11 @@ KRK_METHOD(socket,listen,{ if (result < 0) { return krk_runtimeError(SocketError, "Socket error: %s", strerror(errno)); } -}) -KRK_METHOD(socket,accept,{ + return NONE_VAL(); +} + +KRK_Method(socket,accept) { struct sockaddr_storage addr; socklen_t addrlen; @@ -280,9 +286,9 @@ KRK_METHOD(socket,accept,{ krk_pop(); return krk_pop(); -}) +} -KRK_METHOD(socket,shutdown,{ +KRK_Method(socket,shutdown) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,how); @@ -291,9 +297,11 @@ KRK_METHOD(socket,shutdown,{ if (result < 0) { return krk_runtimeError(SocketError, "Socket error: %s", strerror(errno)); } -}) -KRK_METHOD(socket,recv,{ + return NONE_VAL(); +} + +KRK_Method(socket,recv) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(2); CHECK_ARG(1,int,krk_integer_type,bufsize); @@ -313,9 +321,9 @@ KRK_METHOD(socket,recv,{ KrkBytes * out = krk_newBytes(result,buf); free(buf); return OBJECT_VAL(out); -}) +} -KRK_METHOD(socket,send,{ +KRK_Method(socket,send) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(2); CHECK_ARG(1,bytes,KrkBytes*,buf); @@ -331,9 +339,9 @@ KRK_METHOD(socket,send,{ } return INTEGER_VAL(result); -}) +} -KRK_METHOD(socket,sendto,{ +KRK_Method(socket,sendto) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(3); CHECK_ARG(1,bytes,KrkBytes*,buf); @@ -360,14 +368,14 @@ KRK_METHOD(socket,sendto,{ } return INTEGER_VAL(result); -}) +} -KRK_METHOD(socket,fileno,{ +KRK_Method(socket,fileno) { return INTEGER_VAL(self->sockfd); -}) +} -KRK_METHOD(socket,setsockopt,{ +KRK_Method(socket,setsockopt) { METHOD_TAKES_EXACTLY(3); CHECK_ARG(1,int,krk_integer_type,level); CHECK_ARG(2,int,krk_integer_type,optname); @@ -387,13 +395,14 @@ KRK_METHOD(socket,setsockopt,{ return krk_runtimeError(SocketError, "Socket error: %s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(htons,{ +KRK_Function(htons) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,value); return INTEGER_VAL(htons(value)); -}) +} KrkValue krk_module_onload_socket(void) { KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass); diff --git a/src/modules/module_timeit.c b/src/modules/module_timeit.c index 1f6a7fc..47da84b 100644 --- a/src/modules/module_timeit.c +++ b/src/modules/module_timeit.c @@ -11,7 +11,7 @@ #include #include -KRK_FUNC(timeit,{ +KRK_Function(timeit) { KrkValue number; int times = 1000000; if (hasKw && krk_tableGet_fast(AS_DICT(argv[argc]), S("number"), &number)) { @@ -32,7 +32,7 @@ KRK_FUNC(timeit,{ double after = (double)tv_after.tv_sec + (double)tv_after.tv_usec / 1000000.0; return FLOATING_VAL(after-before); -}) +} KrkValue krk_module_onload_timeit(void) { KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass); diff --git a/src/modules/module_wcwidth.c b/src/modules/module_wcwidth.c index 2bf0728..c61ed3e 100644 --- a/src/modules/module_wcwidth.c +++ b/src/modules/module_wcwidth.c @@ -11,11 +11,11 @@ static #include "../wcwidth._h" #endif -KRK_FUNC(wcwidth,{ +KRK_Function(wcwidth) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,codepoint); return INTEGER_VAL(wcwidth(codepoint)); -}) +} KrkValue krk_module_onload_wcwidth(void) { KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass); diff --git a/src/obj_base.c b/src/obj_base.c index 3cdea8e..3e71c63 100644 --- a/src/obj_base.c +++ b/src/obj_base.c @@ -9,10 +9,10 @@ #define IS_type(o) (1) #define AS_type(o) (o) #define CURRENT_CTYPE KrkValue -KRK_METHOD(type,__init__,{ +KRK_Method(type,__init__) { METHOD_TAKES_EXACTLY(1); return OBJECT_VAL(krk_getType(argv[1])); -}) +} #undef IS_type #undef AS_type #undef CURRENT_CTYPE @@ -22,23 +22,23 @@ KRK_METHOD(type,__init__,{ #define CURRENT_CTYPE KrkClass * -KRK_METHOD(type,__base__,{ +KRK_Method(type,__base__) { return self->base ? OBJECT_VAL(self->base) : NONE_VAL(); -}) +} -KRK_METHOD(type,__name__,{ +KRK_Method(type,__name__) { return self->name ? OBJECT_VAL(self->name) : NONE_VAL(); -}) +} -KRK_METHOD(type,__file__,{ +KRK_Method(type,__file__) { return self->filename ? OBJECT_VAL(self->filename) : NONE_VAL(); -}) +} -KRK_METHOD(type,__doc__,{ +KRK_Method(type,__doc__) { return self->docstring ? OBJECT_VAL(self->docstring) : NONE_VAL(); -}) +} -KRK_METHOD(type,__str__,{ +KRK_Method(type,__str__) { /* Determine if this class has a module */ KrkValue module = NONE_VAL(); krk_tableGet(&self->methods, OBJECT_VAL(S("__module__")), &module); @@ -59,9 +59,9 @@ KRK_METHOD(type,__str__,{ KrkString * out = krk_copyString(tmp,l); free(tmp); return OBJECT_VAL(out); -}) +} -KRK_METHOD(type,__subclasses__,{ +KRK_Method(type,__subclasses__) { KrkValue myList = krk_list_of(0,NULL,0); krk_push(myList); @@ -72,7 +72,7 @@ KRK_METHOD(type,__subclasses__,{ } return krk_pop(); -}) +} _noexport void _createAndBind_type(void) { diff --git a/src/obj_bytes.c b/src/obj_bytes.c index 27705d4..2bafea9 100644 --- a/src/obj_bytes.c +++ b/src/obj_bytes.c @@ -15,7 +15,7 @@ struct ByteArray { #undef IS_bytes #define IS_bytes(o) (IS_BYTES(o) || krk_isInstanceOf(o, vm.baseClasses->bytesClass)) -KRK_METHOD(bytes,__init__,{ +KRK_Method(bytes,__init__) { if (argc < 2) { return OBJECT_VAL(krk_newBytes(0,NULL)); } @@ -51,12 +51,12 @@ KRK_METHOD(bytes,__init__,{ } return krk_runtimeError(vm.exceptions->typeError, "Can not convert '%s' to bytes", krk_typeName(argv[1])); -}) +} #undef IS_bytes #define IS_bytes(o) IS_BYTES(o) -KRK_METHOD(bytes,__hash__,{ +KRK_Method(bytes,__hash__) { METHOD_TAKES_NONE(); uint32_t hash = 0; /* This is the so-called "sdbm" hash. It comes from a piece of @@ -65,12 +65,11 @@ KRK_METHOD(bytes,__hash__,{ hash = (int)self->bytes[i] + (hash << 6) + (hash << 16) - hash; } return INTEGER_VAL(hash); -}) +} /* bytes objects are not interned; need to do this the old-fashioned way. */ -KRK_METHOD(bytes,__eq__,{ +KRK_Method(bytes,__eq__) { if (!IS_BYTES(argv[1])) return BOOLEAN_VAL(0); - KrkBytes * self = AS_BYTES(argv[0]); KrkBytes * them = AS_BYTES(argv[1]); if (self->length != them->length) return BOOLEAN_VAL(0); if (self->obj.hash != them->obj.hash) return BOOLEAN_VAL(0); @@ -78,11 +77,11 @@ KRK_METHOD(bytes,__eq__,{ if (self->bytes[i] != them->bytes[i]) return BOOLEAN_VAL(0); } return BOOLEAN_VAL(1); -}) +} #define AT_END() (self->length == 0 || i == self->length - 1) -KRK_METHOD(bytes,__repr__,{ +KRK_Method(bytes,__repr__) { struct StringBuilder sb = {0}; pushStringBuilder(&sb, 'b'); @@ -119,9 +118,9 @@ KRK_METHOD(bytes,__repr__,{ pushStringBuilder(&sb, '\''); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(bytes,__getitem__,{ +KRK_Method(bytes,__getitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { @@ -154,21 +153,21 @@ KRK_METHOD(bytes,__getitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) +} -KRK_METHOD(bytes,__len__,{ +KRK_Method(bytes,__len__) { return INTEGER_VAL(AS_BYTES(argv[0])->length); -}) +} -KRK_METHOD(bytes,__contains__,{ +KRK_Method(bytes,__contains__) { METHOD_TAKES_EXACTLY(1); return krk_runtimeError(vm.exceptions->notImplementedError, "not implemented"); -}) +} -KRK_METHOD(bytes,decode,{ +KRK_Method(bytes,decode) { METHOD_TAKES_NONE(); return OBJECT_VAL(krk_copyString((char*)AS_BYTES(argv[0])->bytes, AS_BYTES(argv[0])->length)); -}) +} struct _bytes_join_context { struct StringBuilder * sb; @@ -197,7 +196,7 @@ static int _bytes_join_callback(void * context, const KrkValue * values, size_t return 0; } -KRK_METHOD(bytes,join,{ +KRK_Method(bytes,join) { METHOD_TAKES_EXACTLY(1); struct StringBuilder sb = {0}; @@ -210,9 +209,9 @@ KRK_METHOD(bytes,join,{ } return finishStringBuilderBytes(&sb); -}) +} -KRK_METHOD(bytes,__add__,{ +KRK_Method(bytes,__add__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,bytes,KrkBytes*,them); @@ -221,11 +220,11 @@ KRK_METHOD(bytes,__add__,{ pushStringBuilderStr(&sb, (char*)them->bytes, them->length); return finishStringBuilderBytes(&sb); -}) +} FUNC_SIG(bytesiterator,__init__); -KRK_METHOD(bytes,__iter__,{ +KRK_Method(bytes,__iter__) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->bytesiteratorClass); @@ -234,7 +233,7 @@ KRK_METHOD(bytes,__iter__,{ krk_pop(); return OBJECT_VAL(output); -}) +} #undef CURRENT_CTYPE @@ -252,15 +251,15 @@ static void _bytesiterator_gcscan(KrkInstance * self) { krk_markValue(((struct BytesIterator*)self)->l); } -KRK_METHOD(bytesiterator,__init__,{ +KRK_Method(bytesiterator,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,bytes,KrkBytes*,bytes); self->l = argv[1]; self->i = 0; return argv[0]; -}) +} -KRK_METHOD(bytesiterator,__call__,{ +KRK_Method(bytesiterator,__call__) { KrkValue _list = self->l; size_t _counter = self->i; if (!IS_BYTES(_list) || _counter >= AS_BYTES(_list)->length) { @@ -269,7 +268,7 @@ KRK_METHOD(bytesiterator,__call__,{ self->i = _counter + 1; return INTEGER_VAL(AS_BYTES(_list)->bytes[_counter]); } -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE struct ByteArray * @@ -278,7 +277,7 @@ static void _bytearray_gcscan(KrkInstance * self) { krk_markValue(((struct ByteArray*)self)->actual); } -KRK_METHOD(bytearray,__init__,{ +KRK_Method(bytearray,__init__) { METHOD_TAKES_AT_MOST(1); if (argc < 2) { self->actual = OBJECT_VAL(krk_newBytes(0,NULL)); @@ -290,19 +289,19 @@ KRK_METHOD(bytearray,__init__,{ return krk_runtimeError(vm.exceptions->valueError, "expected bytes"); } 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__,{ +KRK_Method(bytearray,__eq__) { if (!IS_bytearray(argv[1])) return BOOLEAN_VAL(0); struct ByteArray * them = AS_bytearray(argv[1]); return BOOLEAN_VAL(krk_valuesEqual(self->actual, them->actual)); -}) +} -KRK_METHOD(bytearray,__repr__,{ +KRK_Method(bytearray,__repr__) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; pushStringBuilderStr(&sb, "bytearray(", 10); @@ -317,9 +316,9 @@ KRK_METHOD(bytearray,__repr__,{ pushStringBuilderStr(&sb, AS_STRING(repred_bytes)->chars, AS_STRING(repred_bytes)->length); pushStringBuilder(&sb,')'); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(bytearray,__getitem__,{ +KRK_Method(bytearray,__getitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { @@ -352,9 +351,9 @@ KRK_METHOD(bytearray,__getitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) +} -KRK_METHOD(bytearray,__setitem__,{ +KRK_Method(bytearray,__setitem__) { METHOD_TAKES_EXACTLY(2); CHECK_ARG(1,int,krk_integer_type,asInt); CHECK_ARG(2,int,krk_integer_type,val); @@ -366,27 +365,27 @@ KRK_METHOD(bytearray,__setitem__,{ AS_BYTES(self->actual)->bytes[asInt] = val; return INTEGER_VAL(AS_BYTES(self->actual)->bytes[asInt]); -}) +} -KRK_METHOD(bytearray,__len__,{ +KRK_Method(bytearray,__len__) { return INTEGER_VAL(AS_BYTES(self->actual)->length); -}) +} -KRK_METHOD(bytearray,__contains__,{ +KRK_Method(bytearray,__contains__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,val); for (size_t i = 0; i < AS_BYTES(self->actual)->length; ++i) { if (AS_BYTES(self->actual)->bytes[i] == val) return BOOLEAN_VAL(1); } return BOOLEAN_VAL(0); -}) +} -KRK_METHOD(bytearray,decode,{ +KRK_Method(bytearray,decode) { METHOD_TAKES_NONE(); return OBJECT_VAL(krk_copyString((char*)AS_BYTES(self->actual)->bytes, AS_BYTES(self->actual)->length)); -}) +} -KRK_METHOD(bytearray,__iter__,{ +KRK_Method(bytearray,__iter__) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->bytesiteratorClass); @@ -395,7 +394,7 @@ KRK_METHOD(bytearray,__iter__,{ krk_pop(); return OBJECT_VAL(output); -}) +} _noexport diff --git a/src/obj_dict.c b/src/obj_dict.c index a519ecd..23928e1 100644 --- a/src/obj_dict.c +++ b/src/obj_dict.c @@ -92,7 +92,7 @@ static int unpackKeyValuePair(void * self, const KrkValue * pairs, size_t count) return 0; } -KRK_METHOD(dict,__init__,{ +KRK_Method(dict,__init__) { METHOD_TAKES_AT_MOST(1); krk_initTable(&self->entries); @@ -104,9 +104,9 @@ KRK_METHOD(dict,__init__,{ krk_tableAddAll(AS_DICT(argv[argc]), &self->entries); } return argv[0]; -}) +} -KRK_METHOD(dict,__getitem__,{ +KRK_Method(dict,__getitem__) { METHOD_TAKES_EXACTLY(1); KrkValue out; if (!krk_tableGet(&self->entries, argv[1], &out)) { @@ -114,15 +114,15 @@ KRK_METHOD(dict,__getitem__,{ KEY_ERROR(argv[1]); } return out; -}) +} -KRK_METHOD(dict,__setitem__,{ +KRK_Method(dict,__setitem__) { METHOD_TAKES_EXACTLY(2); krk_tableSet(&self->entries, argv[1], argv[2]); return argv[2]; -}) +} -KRK_METHOD(dict,__or__,{ +KRK_Method(dict,__or__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,dict,KrkDict*,them); KrkValue outDict = krk_dict_of(0,NULL,0); @@ -130,33 +130,34 @@ KRK_METHOD(dict,__or__,{ krk_tableAddAll(&self->entries, AS_DICT(outDict)); krk_tableAddAll(&them->entries, AS_DICT(outDict)); return krk_pop(); -}) +} -KRK_METHOD(dict,__delitem__,{ +KRK_Method(dict,__delitem__) { METHOD_TAKES_EXACTLY(1); if (!krk_tableDelete(&self->entries, argv[1])) { if (!IS_NONE(krk_currentThread.currentException)) return NONE_VAL(); KEY_ERROR(argv[1]); } -}) + return NONE_VAL(); +} -KRK_METHOD(dict,__len__,{ +KRK_Method(dict,__len__) { METHOD_TAKES_NONE(); return INTEGER_VAL(self->entries.count); -}) +} -KRK_METHOD(dict,__contains__,{ +KRK_Method(dict,__contains__) { METHOD_TAKES_EXACTLY(1); KrkValue _unused; return BOOLEAN_VAL(krk_tableGet(&self->entries, argv[1], &_unused)); -}) +} -KRK_METHOD(dict,capacity,{ +KRK_Method(dict,capacity) { METHOD_TAKES_NONE(); return INTEGER_VAL(self->entries.capacity); -}) +} -KRK_METHOD(dict,__repr__,{ +KRK_Method(dict,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("{...}")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -197,31 +198,32 @@ KRK_METHOD(dict,__repr__,{ pushStringBuilder(&sb,'}'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(dict,copy,{ +KRK_Method(dict,copy) { METHOD_TAKES_NONE(); KrkValue dictOut = krk_dict_of(0,NULL,0); krk_push(dictOut); krk_tableAddAll(&self->entries, AS_DICT(dictOut)); return krk_pop(); -}) +} -KRK_METHOD(dict,clear,{ +KRK_Method(dict,clear) { METHOD_TAKES_NONE(); krk_freeTable(&self->entries); -}) + return NONE_VAL(); +} -KRK_METHOD(dict,get,{ +KRK_Method(dict,get) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(2); KrkValue out = NONE_VAL(); if (argc > 2) out = argv[2]; krk_tableGet(&self->entries, argv[1], &out); return out; -}) +} -KRK_METHOD(dict,setdefault,{ +KRK_Method(dict,setdefault) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(2); KrkValue out = NONE_VAL(); @@ -233,9 +235,9 @@ KRK_METHOD(dict,setdefault,{ } return out; -}) +} -KRK_METHOD(dict,update,{ +KRK_Method(dict,update) { METHOD_TAKES_AT_MOST(1); if (argc > 1) { /* TODO sequence */ @@ -245,47 +247,48 @@ KRK_METHOD(dict,update,{ if (hasKw) { krk_tableAddAll(AS_DICT(argv[argc]), &self->entries); } -}) + return NONE_VAL(); +} -KRK_METHOD(dict,__ior__,{ +KRK_Method(dict,__ior__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,dict,KrkDict*,other); krk_tableAddAll(&other->entries, &self->entries); return argv[0]; -}) +} FUNC_SIG(dictkeys,__init__); -KRK_METHOD(dict,keys,{ +KRK_Method(dict,keys) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->dictkeysClass); krk_push(OBJECT_VAL(output)); FUNC_NAME(dictkeys,__init__)(2, (KrkValue[]){krk_peek(0), argv[0]},0); krk_pop(); return OBJECT_VAL(output); -}) +} FUNC_SIG(dictitems,__init__); -KRK_METHOD(dict,items,{ +KRK_Method(dict,items) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->dictitemsClass); krk_push(OBJECT_VAL(output)); FUNC_NAME(dictitems,__init__)(2, (KrkValue[]){krk_peek(0), argv[0]},0); krk_pop(); return OBJECT_VAL(output); -}) +} FUNC_SIG(dictvalues,__init__); -KRK_METHOD(dict,values,{ +KRK_Method(dict,values) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->dictvaluesClass); krk_push(OBJECT_VAL(output)); FUNC_NAME(dictvalues,__init__)(2, (KrkValue[]){krk_peek(0), argv[0]},0); krk_pop(); return OBJECT_VAL(output); -}) +} KrkValue krk_dict_nth_key_fast(size_t capacity, KrkTableEntry * entries, size_t index) { size_t found = 0; @@ -304,21 +307,21 @@ static void _dictitems_gcscan(KrkInstance * self) { krk_markValue(((struct DictItems*)self)->dict); } -KRK_METHOD(dictitems,__init__,{ +KRK_Method(dictitems,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,dict,KrkDict*,source); self->dict = argv[1]; self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictitems,__iter__,{ +KRK_Method(dictitems,__iter__) { METHOD_TAKES_NONE(); self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictitems,__call__,{ +KRK_Method(dictitems,__call__) { do { if (self->i >= AS_DICT(self->dict)->capacity) return argv[0]; if (!IS_KWARGS(AS_DICT(self->dict)->entries[self->i].key)) { @@ -332,9 +335,9 @@ KRK_METHOD(dictitems,__call__,{ } self->i++; } while (1); -}) +} -KRK_METHOD(dictitems,__repr__,{ +KRK_Method(dictitems,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("dictitems([...])")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -379,7 +382,7 @@ KRK_METHOD(dictitems,__repr__,{ pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE struct DictKeys * @@ -388,21 +391,21 @@ static void _dictkeys_gcscan(KrkInstance * self) { krk_markValue(((struct DictKeys*)self)->dict); } -KRK_METHOD(dictkeys,__init__,{ +KRK_Method(dictkeys,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,dict,KrkDict*,source); self->dict = argv[1]; self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictkeys,__iter__,{ +KRK_Method(dictkeys,__iter__) { METHOD_TAKES_NONE(); self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictkeys,__call__,{ +KRK_Method(dictkeys,__call__) { METHOD_TAKES_NONE(); do { if (self->i >= AS_DICT(self->dict)->capacity) return argv[0]; @@ -413,9 +416,9 @@ KRK_METHOD(dictkeys,__call__,{ } self->i++; } while (1); -}) +} -KRK_METHOD(dictkeys,__repr__,{ +KRK_Method(dictkeys,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("dictkeys([...])")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -445,7 +448,7 @@ KRK_METHOD(dictkeys,__repr__,{ pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE struct DictValues * @@ -454,21 +457,21 @@ static void _dictvalues_gcscan(KrkInstance * self) { krk_markValue(((struct DictValues*)self)->dict); } -KRK_METHOD(dictvalues,__init__,{ +KRK_Method(dictvalues,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,dict,KrkDict*,source); self->dict = argv[1]; self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictvalues,__iter__,{ +KRK_Method(dictvalues,__iter__) { METHOD_TAKES_NONE(); self->i = 0; return argv[0]; -}) +} -KRK_METHOD(dictvalues,__call__,{ +KRK_Method(dictvalues,__call__) { METHOD_TAKES_NONE(); do { if (self->i >= AS_DICT(self->dict)->capacity) return argv[0]; @@ -479,9 +482,9 @@ KRK_METHOD(dictvalues,__call__,{ } self->i++; } while (1); -}) +} -KRK_METHOD(dictvalues,__repr__,{ +KRK_Method(dictvalues,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("dictvalues([...])")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -511,7 +514,7 @@ KRK_METHOD(dictvalues,__repr__,{ pushStringBuilderStr(&sb,"])",2); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} _noexport void _createAndBind_dictClass(void) { diff --git a/src/obj_function.c b/src/obj_function.c index e0bfc80..bf4a271 100644 --- a/src/obj_function.c +++ b/src/obj_function.c @@ -60,7 +60,7 @@ FUNC_SIG(function,__init__) { return krk_runtimeError(vm.exceptions->typeError, "function object is not instantiable"); } -KRK_METHOD(function,__doc__,{ +KRK_Method(function,__doc__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (IS_NATIVE(self) && AS_NATIVE(self)->doc) { @@ -68,9 +68,11 @@ KRK_METHOD(function,__doc__,{ } else if (IS_CLOSURE(self) && AS_CLOSURE(self)->function->docstring) { return OBJECT_VAL(AS_CLOSURE(self)->function->docstring); } -}) -KRK_METHOD(function,__name__,{ + return NONE_VAL(); +} + +KRK_Method(function,__name__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (IS_NATIVE(self)) { @@ -80,17 +82,19 @@ KRK_METHOD(function,__name__,{ } return OBJECT_VAL(S("")); -}) +} -KRK_METHOD(function,__qualname__,{ +KRK_Method(function,__qualname__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (IS_CLOSURE(self) && AS_CLOSURE(self)->function->qualname) { return OBJECT_VAL(AS_CLOSURE(self)->function->qualname); } -}) -KRK_METHOD(function,_ip_to_line,{ + return NONE_VAL(); +} + +KRK_Method(function,_ip_to_line) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,ip); @@ -99,9 +103,9 @@ KRK_METHOD(function,_ip_to_line,{ size_t line = krk_lineNumber(&AS_CLOSURE(self)->function->chunk, ip); return INTEGER_VAL(line); -}) +} -KRK_METHOD(function,__str__,{ +KRK_Method(function,__str__) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; @@ -126,9 +130,9 @@ KRK_METHOD(function,__str__,{ pushStringBuilder(&sb,'>'); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(function,__file__,{ +KRK_Method(function,__file__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (IS_NATIVE(self)) return OBJECT_VAL(S("")); @@ -136,26 +140,26 @@ KRK_METHOD(function,__file__,{ return AS_CLOSURE(self)->function->chunk.filename ? OBJECT_VAL(AS_CLOSURE(self)->function->chunk.filename) : OBJECT_VAL(S("")); -}) +} -KRK_METHOD(function,__args__,{ +KRK_Method(function,__args__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (!IS_CLOSURE(self)) return OBJECT_VAL(krk_newTuple(0)); KrkTuple * tuple = functionArgs(AS_CLOSURE(self)->function); return OBJECT_VAL(tuple); -}) +} -KRK_METHOD(function,__annotations__,{ +KRK_Method(function,__annotations__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (!IS_CLOSURE(self)) return NONE_VAL(); return AS_CLOSURE(self)->annotations; -}) +} -KRK_METHOD(function,__code__,{ +KRK_Method(function,__code__) { ATTRIBUTE_NOT_ASSIGNABLE(); if (!IS_CLOSURE(self)) return NONE_VAL(); return OBJECT_VAL(AS_CLOSURE(self)->function); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE KrkCodeObject* @@ -164,12 +168,12 @@ FUNC_SIG(codeobject,__init__) { return krk_runtimeError(vm.exceptions->typeError, "codeobject object is not instantiable"); } -KRK_METHOD(codeobject,__name__,{ +KRK_Method(codeobject,__name__) { ATTRIBUTE_NOT_ASSIGNABLE(); return self->name ? OBJECT_VAL(self->name) : OBJECT_VAL(S("")); -}) +} -KRK_METHOD(codeobject,__str__,{ +KRK_Method(codeobject,__str__) { METHOD_TAKES_NONE(); KrkValue s = FUNC_NAME(codeobject,__name__)(1,argv,0); if (!IS_STRING(s)) return NONE_VAL(); @@ -183,16 +187,16 @@ KRK_METHOD(codeobject,__str__,{ krk_pop(); return s; -}) +} -KRK_METHOD(codeobject,_ip_to_line,{ +KRK_Method(codeobject,_ip_to_line) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,ip); size_t line = krk_lineNumber(&self->chunk, ip); return INTEGER_VAL(line); -}) +} -KRK_METHOD(codeobject,__constants__,{ +KRK_Method(codeobject,__constants__) { ATTRIBUTE_NOT_ASSIGNABLE(); krk_push(OBJECT_VAL(krk_newTuple(self->chunk.constants.count))); memcpy(AS_TUPLE(krk_peek(0))->values.values, @@ -200,17 +204,17 @@ KRK_METHOD(codeobject,__constants__,{ sizeof(KrkValue) * self->chunk.constants.count); AS_TUPLE(krk_peek(0))->values.count = self->chunk.constants.count; return krk_pop(); -}) +} -KRK_METHOD(codeobject,co_code,{ +KRK_Method(codeobject,co_code) { return OBJECT_VAL(krk_newBytes(self->chunk.count, self->chunk.code)); -}) +} -KRK_METHOD(codeobject,co_argcount,{ +KRK_Method(codeobject,co_argcount) { return INTEGER_VAL(self->potentialPositionals); -}) +} -KRK_METHOD(codeobject,__locals__,{ +KRK_Method(codeobject,__locals__) { krk_push(OBJECT_VAL(krk_newTuple(self->localNameCount))); for (size_t i = 0; i < self->localNameCount; ++i) { krk_push(OBJECT_VAL(krk_newTuple(4))); @@ -222,10 +226,10 @@ KRK_METHOD(codeobject,__locals__,{ krk_pop(); } return krk_pop(); -}) +} /* Python-compatibility */ -KRK_METHOD(codeobject,co_flags,{ +KRK_Method(codeobject,co_flags) { ATTRIBUTE_NOT_ASSIGNABLE(); int out = 0; @@ -239,13 +243,13 @@ KRK_METHOD(codeobject,co_flags,{ if (self->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_IS_COROUTINE) out |= 0x80; return INTEGER_VAL(out); -}) +} -KRK_METHOD(codeobject,__args__,{ +KRK_Method(codeobject,__args__) { ATTRIBUTE_NOT_ASSIGNABLE(); KrkTuple * tuple = functionArgs(self); return OBJECT_VAL(tuple); -}) +} #undef CURRENT_CTYPE @@ -260,22 +264,22 @@ FUNC_SIG(method,__init__) { return OBJECT_VAL(krk_newBoundMethod(argv[2],AS_OBJECT(argv[1]))); } -KRK_METHOD(method,__name__,{ +KRK_Method(method,__name__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__name__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__qualname__,{ +KRK_Method(method,__qualname__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__qualname__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,_ip_to_line,{ +KRK_Method(method,_ip_to_line) { METHOD_TAKES_EXACTLY(1); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,_ip_to_line)(2,(KrkValue[]){OBJECT_VAL(self->method),argv[1]},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__str__,{ +KRK_Method(method,__str__) { METHOD_TAKES_NONE(); KrkValue s = FUNC_NAME(method,__qualname__)(1,argv,0); if (!IS_STRING(s)) s = FUNC_NAME(method,__name__)(1,argv,0); @@ -293,58 +297,58 @@ KRK_METHOD(method,__str__,{ free(tmp); krk_pop(); return s; -}) +} -KRK_METHOD(method,__file__,{ +KRK_Method(method,__file__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__file__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__args__,{ +KRK_Method(method,__args__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__args__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__doc__,{ +KRK_Method(method,__doc__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__doc__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__annotations__,{ +KRK_Method(method,__annotations__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__annotations__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__code__,{ +KRK_Method(method,__code__) { ATTRIBUTE_NOT_ASSIGNABLE(); return IS_function(OBJECT_VAL(self->method)) ? FUNC_NAME(function,__code__)(1,(KrkValue[]){OBJECT_VAL(self->method)},0) : OBJECT_VAL(S("?")); -}) +} -KRK_METHOD(method,__func__,{ +KRK_Method(method,__func__) { ATTRIBUTE_NOT_ASSIGNABLE(); return OBJECT_VAL(self->method); -}) +} -KRK_METHOD(method,__self__,{ +KRK_Method(method,__self__) { ATTRIBUTE_NOT_ASSIGNABLE(); return OBJECT_VAL(self->receiver); -}) +} -KRK_FUNC(staticmethod,{ +KRK_Function(staticmethod) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,CLOSURE,KrkClosure*,method); method->obj.flags &= ~(KRK_OBJ_FLAGS_FUNCTION_MASK); method->obj.flags |= KRK_OBJ_FLAGS_FUNCTION_IS_STATIC_METHOD; return argv[0]; -}) +} -KRK_FUNC(classmethod,{ +KRK_Function(classmethod) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,CLOSURE,KrkClosure*,method); method->obj.flags &= ~(KRK_OBJ_FLAGS_FUNCTION_MASK); method->obj.flags |= KRK_OBJ_FLAGS_FUNCTION_IS_CLASS_METHOD; return argv[0]; -}) +} _noexport void _createAndBind_functionClass(void) { diff --git a/src/obj_gen.c b/src/obj_gen.c index d28cd0a..2f876ef 100644 --- a/src/obj_gen.c +++ b/src/obj_gen.c @@ -84,7 +84,7 @@ FUNC_SIG(generator,__init__) { return krk_runtimeError(vm.exceptions->typeError, "cannot create '%s' instances", "generator"); } -KRK_METHOD(generator,__repr__,{ +KRK_Method(generator,__repr__) { METHOD_TAKES_NONE(); char * typeStr = "generator"; @@ -103,14 +103,14 @@ KRK_METHOD(generator,__repr__,{ (void*)self); return OBJECT_VAL(krk_takeString(tmp,lenActual)); -}) +} -KRK_METHOD(generator,__iter__,{ +KRK_Method(generator,__iter__) { METHOD_TAKES_NONE(); return OBJECT_VAL(self); -}) +} -KRK_METHOD(generator,__call__,{ +KRK_Method(generator,__call__) { METHOD_TAKES_AT_MOST(1); if (!self->ip) return OBJECT_VAL(self); /* Prepare frame */ @@ -175,28 +175,28 @@ KRK_METHOD(generator,__call__,{ krk_currentThread.stackTop = krk_currentThread.stack + frame->slots; return result; -}) +} -KRK_METHOD(generator,send,{ +KRK_Method(generator,send) { METHOD_TAKES_EXACTLY(1); if (!self->started && !IS_NONE(argv[1])) { return krk_runtimeError(vm.exceptions->typeError, "Can not send non-None value to just-started generator"); } return FUNC_NAME(generator,__call__)(argc,argv,0); -}) +} -KRK_METHOD(generator,__finish__,{ +KRK_Method(generator,__finish__) { METHOD_TAKES_NONE(); return self->result; -}) +} /* * For compatibility with Python... */ -KRK_METHOD(generator,gi_running,{ +KRK_Method(generator,gi_running) { METHOD_TAKES_NONE(); return BOOLEAN_VAL(self->running); -}) +} int krk_getAwaitable(void) { if (IS_generator(krk_peek(0)) && AS_generator(krk_peek(0))->type == KRK_OBJ_FLAGS_CODEOBJECT_IS_COROUTINE) { diff --git a/src/obj_list.c b/src/obj_list.c index 5d7ea9e..48b594d 100644 --- a/src/obj_list.c +++ b/src/obj_list.c @@ -46,7 +46,7 @@ KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw) { #define CURRENT_CTYPE KrkList * #define CURRENT_NAME self -KRK_METHOD(list,__getitem__,{ +KRK_Method(list,__getitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { CHECK_ARG(1,int,krk_integer_type,index); @@ -93,9 +93,9 @@ KRK_METHOD(list,__getitem__,{ } else { return TYPE_ERROR(int or slice,argv[1]); } -}) +} -KRK_METHOD(list,__eq__,{ +KRK_Method(list,__eq__) { METHOD_TAKES_EXACTLY(1); if (!IS_list(argv[1])) return NOTIMPL_VAL(); KrkList * them = AS_list(argv[1]); @@ -104,16 +104,17 @@ KRK_METHOD(list,__eq__,{ if (!krk_valuesEqual(self->values.values[i], them->values.values[i])) return BOOLEAN_VAL(0); } return BOOLEAN_VAL(1); -}) +} -KRK_METHOD(list,append,{ +KRK_Method(list,append) { METHOD_TAKES_EXACTLY(1); pthread_rwlock_wrlock(&self->rwlock); krk_writeValueArray(&self->values, argv[1]); pthread_rwlock_unlock(&self->rwlock); -}) + return NONE_VAL(); +} -KRK_METHOD(list,insert,{ +KRK_Method(list,insert) { METHOD_TAKES_EXACTLY(2); CHECK_ARG(1,int,krk_integer_type,index); pthread_rwlock_wrlock(&self->rwlock); @@ -126,9 +127,10 @@ KRK_METHOD(list,insert,{ ); self->values.values[index] = argv[2]; pthread_rwlock_unlock(&self->rwlock); -}) + return NONE_VAL(); +} -KRK_METHOD(list,__repr__,{ +KRK_Method(list,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("[...]")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -154,8 +156,7 @@ KRK_METHOD(list,__repr__,{ pushStringBuilder(&sb,']'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) - +} static int _list_extend_callback(void * context, const KrkValue * values, size_t count) { KrkValueArray * positionals = context; @@ -172,7 +173,7 @@ static int _list_extend_callback(void * context, const KrkValue * values, size_t return 0; } -KRK_METHOD(list,extend,{ +KRK_Method(list,extend) { METHOD_TAKES_EXACTLY(1); pthread_rwlock_wrlock(&self->rwlock); KrkValueArray * positionals = AS_LIST(argv[0]); @@ -184,10 +185,10 @@ KRK_METHOD(list,extend,{ krk_unpackIterable(other, positionals, _list_extend_callback); pthread_rwlock_unlock(&self->rwlock); -}) -#undef unpackArray + return NONE_VAL(); +} -KRK_METHOD(list,__init__,{ +KRK_Method(list,__init__) { METHOD_TAKES_AT_MOST(1); krk_initValueArray(AS_LIST(argv[0])); pthread_rwlock_init(&self->rwlock, NULL); @@ -195,9 +196,9 @@ KRK_METHOD(list,__init__,{ _list_extend(2,(KrkValue[]){argv[0],argv[1]},0); } return argv[0]; -}) +} -KRK_METHOD(list,__mul__,{ +KRK_Method(list,__mul__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,int,krk_integer_type,howMany); @@ -210,14 +211,14 @@ KRK_METHOD(list,__mul__,{ } return krk_pop(); -}) +} -KRK_METHOD(list,__len__,{ +KRK_Method(list,__len__) { METHOD_TAKES_NONE(); return INTEGER_VAL(self->values.count); -}) +} -KRK_METHOD(list,__contains__,{ +KRK_Method(list,__contains__) { METHOD_TAKES_EXACTLY(1); pthread_rwlock_rdlock(&self->rwlock); for (size_t i = 0; i < self->values.count; ++i) { @@ -229,9 +230,9 @@ KRK_METHOD(list,__contains__,{ } pthread_rwlock_unlock(&self->rwlock); return BOOLEAN_VAL(0); -}) +} -KRK_METHOD(list,pop,{ +KRK_Method(list,pop) { METHOD_TAKES_AT_MOST(1); pthread_rwlock_wrlock(&self->rwlock); krk_integer_type index = self->values.count - 1; @@ -254,9 +255,9 @@ KRK_METHOD(list,pop,{ pthread_rwlock_unlock(&self->rwlock); return outItem; } -}) +} -KRK_METHOD(list,__setitem__,{ +KRK_Method(list,__setitem__) { METHOD_TAKES_EXACTLY(2); if (IS_INTEGER(argv[1])) { CHECK_ARG(1,int,krk_integer_type,index); @@ -299,10 +300,9 @@ KRK_METHOD(list,__setitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) +} - -KRK_METHOD(list,__delitem__,{ +KRK_Method(list,__delitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { @@ -325,9 +325,11 @@ KRK_METHOD(list,__delitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) -KRK_METHOD(list,remove,{ + return NONE_VAL(); +} + +KRK_Method(list,remove) { METHOD_TAKES_EXACTLY(1); pthread_rwlock_wrlock(&self->rwlock); for (size_t i = 0; i < self->values.count; ++i) { @@ -342,16 +344,17 @@ KRK_METHOD(list,remove,{ } pthread_rwlock_unlock(&self->rwlock); return krk_runtimeError(vm.exceptions->valueError, "not found"); -}) +} -KRK_METHOD(list,clear,{ +KRK_Method(list,clear) { METHOD_TAKES_NONE(); pthread_rwlock_wrlock(&self->rwlock); krk_freeValueArray(&self->values); pthread_rwlock_unlock(&self->rwlock); -}) + return NONE_VAL(); +} -KRK_METHOD(list,index,{ +KRK_Method(list,index) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(3); @@ -389,9 +392,9 @@ KRK_METHOD(list,index,{ pthread_rwlock_unlock(&self->rwlock); return krk_runtimeError(vm.exceptions->valueError, "not found"); -}) +} -KRK_METHOD(list,count,{ +KRK_Method(list,count) { METHOD_TAKES_EXACTLY(1); krk_integer_type count = 0; @@ -403,17 +406,17 @@ KRK_METHOD(list,count,{ pthread_rwlock_unlock(&self->rwlock); return INTEGER_VAL(count); -}) +} -KRK_METHOD(list,copy,{ +KRK_Method(list,copy) { METHOD_TAKES_NONE(); pthread_rwlock_rdlock(&self->rwlock); KrkValue result = krk_list_of(self->values.count, self->values.values, 0); pthread_rwlock_unlock(&self->rwlock); return result; -}) +} -KRK_METHOD(list,reverse,{ +KRK_Method(list,reverse) { METHOD_TAKES_NONE(); pthread_rwlock_wrlock(&self->rwlock); for (size_t i = 0; i < (self->values.count) / 2; i++) { @@ -423,7 +426,7 @@ KRK_METHOD(list,reverse,{ } pthread_rwlock_unlock(&self->rwlock); return NONE_VAL(); -}) +} static int _list_sorter(const void * _a, const void * _b) { KrkValue a = *(KrkValue*)_a; @@ -438,15 +441,17 @@ static int _list_sorter(const void * _a, const void * _b) { return 0; } -KRK_METHOD(list,sort,{ +KRK_Method(list,sort) { METHOD_TAKES_NONE(); pthread_rwlock_wrlock(&self->rwlock); qsort(self->values.values, self->values.count, sizeof(KrkValue), _list_sorter); pthread_rwlock_unlock(&self->rwlock); -}) -KRK_METHOD(list,__add__,{ + return NONE_VAL(); +} + +KRK_Method(list,__add__) { METHOD_TAKES_EXACTLY(1); if (!IS_list(argv[1])) return TYPE_ERROR(list,argv[1]); @@ -455,11 +460,11 @@ KRK_METHOD(list,__add__,{ pthread_rwlock_unlock(&self->rwlock); FUNC_NAME(list,extend)(2,(KrkValue[]){outList,argv[1]},0); /* extend */ return outList; -}) +} FUNC_SIG(listiterator,__init__); -KRK_METHOD(list,__iter__,{ +KRK_Method(list,__iter__) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->listiteratorClass); @@ -468,7 +473,7 @@ KRK_METHOD(list,__iter__,{ krk_pop(); return OBJECT_VAL(output); -}) +} #undef CURRENT_CTYPE @@ -486,13 +491,13 @@ static void _listiterator_gcscan(KrkInstance * self) { krk_markValue(((struct ListIterator*)self)->l); } -KRK_METHOD(listiterator,__init__,{ +KRK_Method(listiterator,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,list,KrkList*,list); self->l = argv[1]; self->i = 0; return argv[0]; -}) +} FUNC_SIG(listiterator,__call__) { static __attribute__ ((unused)) const char* _method_name = "__call__"; diff --git a/src/obj_long.c b/src/obj_long.c index c42cc4e..7ce4e59 100644 --- a/src/obj_long.c +++ b/src/obj_long.c @@ -1174,7 +1174,7 @@ static void _long_gcsweep(KrkInstance * self) { krk_long_clear(((struct BigInt*)self)->value); } -KRK_METHOD(long,__init__,{ +KRK_Method(long,__init__) { METHOD_TAKES_AT_MOST(1); if (argc < 2) { make_long(0,self); @@ -1197,7 +1197,7 @@ KRK_METHOD(long,__init__,{ } /* our value should be set */ return argv[0]; -}) +} /** * Float conversions. @@ -1218,9 +1218,9 @@ static double krk_long_get_double(const KrkLong * value) { return out; } -KRK_METHOD(long,__float__,{ +KRK_Method(long,__float__) { return FLOATING_VAL(krk_long_get_double(self->value)); -}) +} static KrkValue _krk_long_truediv(KrkLong * top, KrkLong * bottom) { if (bottom->width == 0) return krk_runtimeError(vm.exceptions->valueError, "float division by zero"); @@ -1244,39 +1244,39 @@ static KrkValue checked_float_div(double top, double bottom) { return FLOATING_VAL(top/bottom); } -KRK_METHOD(long,__truediv__,{ +KRK_Method(long,__truediv__) { krk_long tmp; if (IS_long(argv[1])) krk_long_init_copy(tmp, AS_long(argv[1])->value); else if (IS_INTEGER(argv[1])) krk_long_init_si(tmp, AS_INTEGER(argv[1])); else if (IS_FLOATING(argv[1])) return checked_float_div(krk_long_get_double(self->value), AS_FLOATING(argv[1])); else return NOTIMPL_VAL(); return _krk_long_truediv(self->value,tmp); -}) +} -KRK_METHOD(long,__rtruediv__,{ +KRK_Method(long,__rtruediv__) { krk_long tmp; if (IS_long(argv[1])) krk_long_init_copy(tmp, AS_long(argv[1])->value); else if (IS_INTEGER(argv[1])) krk_long_init_si(tmp, AS_INTEGER(argv[1])); else if (IS_FLOATING(argv[1])) return checked_float_div(AS_FLOATING(argv[1]), krk_long_get_double(self->value)); else return NOTIMPL_VAL(); return _krk_long_truediv(tmp,self->value); -}) +} #define PRINTER(name,base,prefix) \ - KRK_METHOD(long,__ ## name ## __,{ \ + KRK_Method(long,__ ## name ## __) { \ size_t size; \ char * rev = krk_long_to_str(self->value, base, prefix, &size); \ return OBJECT_VAL(krk_takeString(rev,size)); \ - }) + } PRINTER(str,10,"") PRINTER(hex,16,"x0") PRINTER(oct,8,"o0") PRINTER(bin,2,"b0") -KRK_METHOD(long,__hash__,{ +KRK_Method(long,__hash__) { return INTEGER_VAL((uint32_t)(krk_long_medium(self->value))); -}) +} static KrkValue make_long_obj(KrkLong * val) { krk_integer_type maybe = 0; @@ -1309,12 +1309,12 @@ KrkValue krk_parse_int(const char * start, size_t width, unsigned int base) { return make_long_obj(&_value); } -KRK_METHOD(long,__int__,{ +KRK_Method(long,__int__) { return INTEGER_VAL(krk_long_medium(self->value)); -}) +} #define BASIC_BIN_OP_FLOATS(name, long_func, MAYBE_FLOAT, MAYBE_FLOAT_INV) \ - KRK_METHOD(long,__ ## name ## __,{ \ + KRK_Method(long,__ ## name ## __) { \ krk_long tmp; \ if (IS_long(argv[1])) krk_long_init_copy(tmp, AS_long(argv[1])->value); \ else if (IS_INTEGER(argv[1])) krk_long_init_si(tmp, AS_INTEGER(argv[1])); \ @@ -1322,8 +1322,8 @@ KRK_METHOD(long,__int__,{ else return NOTIMPL_VAL(); \ long_func(tmp,self->value,tmp); \ return make_long_obj(tmp); \ - }) \ - KRK_METHOD(long,__r ## name ## __,{ \ + } \ + KRK_Method(long,__r ## name ## __) { \ krk_long tmp; \ if (IS_long(argv[1])) krk_long_init_copy(tmp, AS_long(argv[1])->value); \ else if (IS_INTEGER(argv[1])) krk_long_init_si(tmp, AS_INTEGER(argv[1])); \ @@ -1331,7 +1331,7 @@ KRK_METHOD(long,__int__,{ else return NOTIMPL_VAL(); \ long_func(tmp,tmp,self->value); \ return make_long_obj(tmp); \ - }) \ + } \ _noexport \ KrkValue krk_long_coerced_ ## name (krk_integer_type a, krk_integer_type b) { \ krk_long tmp_res, tmp_a, tmp_b; \ @@ -1455,7 +1455,7 @@ BASIC_BIN_OP(floordiv,_krk_long_div) BASIC_BIN_OP(pow,_krk_long_pow) #define COMPARE_OP(name, comp) \ - KRK_METHOD(long,__ ## name ## __,{ \ + KRK_Method(long,__ ## name ## __) { \ krk_long tmp; \ if (IS_long(argv[1])) krk_long_init_copy(tmp, AS_long(argv[1])->value); \ else if (IS_INTEGER(argv[1])) krk_long_init_si(tmp, AS_INTEGER(argv[1])); \ @@ -1464,7 +1464,7 @@ BASIC_BIN_OP(pow,_krk_long_pow) int cmp = krk_long_compare(self->value,tmp); \ krk_long_clear(tmp); \ return BOOLEAN_VAL(cmp comp 0); \ - }) + } COMPARE_OP(lt, <) COMPARE_OP(gt, >) @@ -1475,11 +1475,11 @@ COMPARE_OP(eq, ==) #undef BASIC_BIN_OP #undef COMPARE_OP -KRK_METHOD(long,__len__,{ +KRK_Method(long,__len__) { return INTEGER_VAL(krk_long_sign(self->value)); -}) +} -KRK_METHOD(long,__invert__,{ +KRK_Method(long,__invert__) { KrkLong tmp, one; krk_long_init_copy(&tmp, self->value); krk_long_init_si(&one, 1); @@ -1487,21 +1487,21 @@ KRK_METHOD(long,__invert__,{ krk_long_set_sign(&tmp, tmp.width > 0 ? -1 : 1); krk_long_clear(&one); return make_long_obj(&tmp); -}) +} -KRK_METHOD(long,__neg__,{ +KRK_Method(long,__neg__) { KrkLong tmp; krk_long_init_copy(&tmp, self->value); krk_long_set_sign(&tmp, tmp.width > 0 ? -1 : 1); return make_long_obj(&tmp); -}) +} -KRK_METHOD(long,__abs__,{ +KRK_Method(long,__abs__) { KrkLong tmp; krk_long_init_copy(&tmp, self->value); krk_long_set_sign(&tmp, 1); return make_long_obj(&tmp); -}) +} static KrkValue long_bit_count(KrkLong * val) { size_t count = 0; @@ -1516,9 +1516,9 @@ static KrkValue long_bit_count(KrkLong * val) { return make_long_obj(&tmp); } -KRK_METHOD(long,bit_count,{ +KRK_Method(long,bit_count) { return long_bit_count(self->value); -}) +} static KrkValue long_bit_length(KrkLong * val) { size_t bits = _bits_in(val); @@ -1527,9 +1527,9 @@ static KrkValue long_bit_length(KrkLong * val) { return make_long_obj(&tmp); } -KRK_METHOD(long,bit_length,{ +KRK_Method(long,bit_length) { return long_bit_length(self->value); -}) +} static KrkValue long_to_bytes(KrkLong * val, size_t argc, const KrkValue argv[], int hasKw) { static const char _method_name[] = "to_bytes"; @@ -1648,10 +1648,10 @@ static KrkValue long_to_bytes(KrkLong * val, size_t argc, const KrkValue argv[], return krk_pop(); } -KRK_METHOD(long,to_bytes,{ +KRK_Method(long,to_bytes) { METHOD_TAKES_AT_LEAST(2); return long_to_bytes(self->value, argc, argv, hasKw); -}) +} /** * @fn long._digit_count() @@ -1661,11 +1661,11 @@ KRK_METHOD(long,to_bytes,{ * @return The number of digits in the internal representation of the long. * The result will be negative if the long is negative. */ -KRK_METHOD(long,_digit_count,{ +KRK_Method(long,_digit_count) { krk_long result; /* since it's a ssize_t */ krk_long_init_si(result, self->value[0].width); return make_long_obj(result); -}) +} /** * @fn long._get_digit(index) @@ -1677,7 +1677,7 @@ KRK_METHOD(long,_digit_count,{ * @param index Digit to get. May be an @c int or a @c long >= 0 and <= 2. * @return An int representation of the unsigned digit @p index of the long. */ -KRK_METHOD(long,_get_digit,{ +KRK_Method(long,_get_digit) { METHOD_TAKES_EXACTLY(1); KrkLong * _self = self->value; @@ -1702,7 +1702,7 @@ KRK_METHOD(long,_get_digit,{ } return INTEGER_VAL(_self->digits[index]); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE krk_integer_type @@ -1713,29 +1713,29 @@ KRK_METHOD(long,_get_digit,{ * Convert to a @c long and just use those versions... */ -KRK_METHOD(int,bit_count,{ +KRK_Method(int,bit_count) { krk_long value; krk_long_init_si(value, self); KrkValue out = long_bit_count(value); krk_long_clear(value); return out; -}) +} -KRK_METHOD(int,bit_length,{ +KRK_Method(int,bit_length) { krk_long value; krk_long_init_si(value, self); KrkValue out = long_bit_length(value); krk_long_clear(value); return out; -}) +} -KRK_METHOD(int,to_bytes,{ +KRK_Method(int,to_bytes) { krk_long value; krk_long_init_si(value, self); KrkValue out = long_to_bytes(value, argc, argv, hasKw); krk_long_clear(value); return out; -}) +} #undef BIND_METHOD /* These class names conflict with C types, so we need to cheat a bit */ diff --git a/src/obj_numeric.c b/src/obj_numeric.c index 12598b4..23ebe8c 100644 --- a/src/obj_numeric.c +++ b/src/obj_numeric.c @@ -40,31 +40,31 @@ FUNC_SIG(int,__init__) { return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%s'", "int", krk_typeName(argv[1])); } -KRK_METHOD(int,__str__,{ +KRK_Method(int,__str__) { char tmp[100]; size_t l = snprintf(tmp, 100, PRIkrk_int, self); return OBJECT_VAL(krk_copyString(tmp, l)); -}) +} -KRK_METHOD(int,__int__,{ return argv[0]; }) -KRK_METHOD(int,__float__,{ return FLOATING_VAL(self); }) +KRK_Method(int,__int__) { return argv[0]; } +KRK_Method(int,__float__) { return FLOATING_VAL(self); } -KRK_METHOD(int,__chr__,{ +KRK_Method(int,__chr__) { unsigned char bytes[5] = {0}; size_t len = krk_codepointToBytes(self, bytes); return OBJECT_VAL(krk_copyString((char*)bytes, len)); -}) +} -KRK_METHOD(int,__eq__,{ +KRK_Method(int,__eq__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) return self == AS_INTEGER(argv[1]); else if (IS_FLOATING(argv[1])) return self == AS_FLOATING(argv[1]); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__hash__,{ +KRK_Method(int,__hash__) { return INTEGER_VAL((uint32_t)AS_INTEGER(argv[0])); -}) +} /** * We _could_ use the __builtin_XXX_overflow(_p) functions gcc+clang provide, @@ -93,33 +93,33 @@ OVERFLOW_CHECKED_INT_OPERATION(sub,-) OVERFLOW_CHECKED_INT_OPERATION(mul,*) #define BASIC_BIN_OP(name,operator) \ - KRK_METHOD(int,__ ## name ## __,{ \ + KRK_Method(int,__ ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return krk_int_op_ ## name(self, AS_INTEGER(argv[1])); \ else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL((double)self operator AS_FLOATING(argv[1])); \ return NOTIMPL_VAL(); \ - }) \ - KRK_METHOD(int,__r ## name ## __,{ \ + } \ + KRK_Method(int,__r ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return krk_int_op_ ## name(AS_INTEGER(argv[1]), self); \ else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) operator (double)self); \ return NOTIMPL_VAL(); \ - }) + } #define INT_ONLY_BIN_OP(name,operator) \ - KRK_METHOD(int,__ ## name ## __,{ \ + KRK_Method(int,__ ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return INTEGER_VAL(self operator AS_INTEGER(argv[1])); \ return NOTIMPL_VAL(); \ - }) \ - KRK_METHOD(int,__r ## name ## __,{ \ + } \ + KRK_Method(int,__r ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return INTEGER_VAL(AS_INTEGER(argv[1]) operator self); \ return NOTIMPL_VAL(); \ - }) + } #define COMPARE_OP(name,operator) \ - KRK_METHOD(int,__ ## name ## __,{ \ + KRK_Method(int,__ ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return BOOLEAN_VAL(self operator AS_INTEGER(argv[1])); \ else if (likely(IS_FLOATING(argv[1]))) return BOOLEAN_VAL((double)self operator AS_FLOATING(argv[1])); \ return NOTIMPL_VAL(); \ - }) + } BASIC_BIN_OP(add,+) BASIC_BIN_OP(sub,-) @@ -130,14 +130,14 @@ INT_ONLY_BIN_OP(and,&) #define DEFER_TO_LONG(name) \ extern KrkValue krk_long_coerced_ ## name (krk_integer_type a, krk_integer_type b); \ - KRK_METHOD(int,__ ## name ## __,{ \ + KRK_Method(int,__ ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return krk_long_coerced_ ## name (self, AS_INTEGER(argv[1])); \ return NOTIMPL_VAL(); \ - }) \ - KRK_METHOD(int,__r ## name ## __,{ \ + } \ + KRK_Method(int,__r ## name ## __) { \ if (likely(IS_INTEGER(argv[1]))) return krk_long_coerced_ ## name (AS_INTEGER(argv[1]), self); \ return NOTIMPL_VAL(); \ - }) + } DEFER_TO_LONG(lshift) DEFER_TO_LONG(rshift) @@ -152,7 +152,7 @@ COMPARE_OP(ge, >=) #undef INT_ONLY_BIN_OP #undef COMPARE_OP -KRK_METHOD(int,__truediv__,{ +KRK_Method(int,__truediv__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) { krk_integer_type b = AS_INTEGER(argv[1]); @@ -164,15 +164,15 @@ KRK_METHOD(int,__truediv__,{ return FLOATING_VAL((double)self / b); } return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__rtruediv__,{ +KRK_Method(int,__rtruediv__) { METHOD_TAKES_EXACTLY(1); if (unlikely(self == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero"); else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / (double)self); else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) / (double)self); return NOTIMPL_VAL(); -}) +} #ifdef __TINYC__ #include @@ -214,20 +214,20 @@ static KrkValue _krk_int_mod(krk_integer_type a, krk_integer_type b) { } -KRK_METHOD(int,__mod__,{ +KRK_Method(int,__mod__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) return _krk_int_mod(self, AS_INTEGER(argv[1])); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__rmod__,{ +KRK_Method(int,__rmod__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) return _krk_int_mod(AS_INTEGER(argv[1]), self); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__floordiv__,{ +KRK_Method(int,__floordiv__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) { return _krk_int_div(self,AS_INTEGER(argv[1])); @@ -237,33 +237,33 @@ KRK_METHOD(int,__floordiv__,{ return FLOATING_VAL(__builtin_floor((double)self / b)); } return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__rfloordiv__,{ +KRK_Method(int,__rfloordiv__) { METHOD_TAKES_EXACTLY(1); if (unlikely(self == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero"); else if (likely(IS_INTEGER(argv[1]))) return _krk_int_div(AS_INTEGER(argv[1]), self); else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(__builtin_floor(AS_FLOATING(argv[1]) / (double)self)); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(int,__hex__,{ +KRK_Method(int,__hex__) { METHOD_TAKES_NONE(); char tmp[20]; unsigned long long val = self < 0 ? -self : self; size_t len = snprintf(tmp, 20, "%s0x%llx", self < 0 ? "-" : "", val); return OBJECT_VAL(krk_copyString(tmp,len)); -}) +} -KRK_METHOD(int,__oct__,{ +KRK_Method(int,__oct__) { METHOD_TAKES_NONE(); char tmp[20]; unsigned long long val = self < 0 ? -self : self; size_t len = snprintf(tmp, 20, "%s0o%llo", self < 0 ? "-" : "", val); return OBJECT_VAL(krk_copyString(tmp,len)); -}) +} -KRK_METHOD(int,__bin__,{ +KRK_Method(int,__bin__) { METHOD_TAKES_NONE(); unsigned long long val = self; if (self < 0) val = -val; @@ -288,19 +288,19 @@ KRK_METHOD(int,__bin__,{ } return finishStringBuilder(&sb); -}) +} -KRK_METHOD(int,__invert__,{ +KRK_Method(int,__invert__) { return INTEGER_VAL(~self); -}) +} -KRK_METHOD(int,__neg__,{ +KRK_Method(int,__neg__) { return INTEGER_VAL(-self); -}) +} -KRK_METHOD(int,__abs__,{ +KRK_Method(int,__abs__) { return self < 0 ? INTEGER_VAL(-self) : INTEGER_VAL(self); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE double @@ -331,8 +331,8 @@ FUNC_SIG(float,__init__) { return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%s'", "float", krk_typeName(argv[1])); } -KRK_METHOD(float,__int__,{ return INTEGER_VAL(self); }) -KRK_METHOD(float,__float__,{ return argv[0]; }) +KRK_Method(float,__int__) { return INTEGER_VAL(self); } +KRK_Method(float,__float__) { return argv[0]; } static int isDigits(const char * c) { while (*c) { @@ -342,55 +342,55 @@ static int isDigits(const char * c) { return 1; } -KRK_METHOD(float,__str__,{ +KRK_Method(float,__str__) { char tmp[100]; size_t l = snprintf(tmp, 97, "%.16g", self); if (!strstr(tmp,".") && isDigits(tmp)) { l = snprintf(tmp,100,"%.16g.0",self); } return OBJECT_VAL(krk_copyString(tmp, l)); -}) +} -KRK_METHOD(float,__eq__,{ +KRK_Method(float,__eq__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) return self == (double)AS_INTEGER(argv[1]); else if (IS_FLOATING(argv[1])) return self == AS_FLOATING(argv[1]); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(float,__hash__,{ +KRK_Method(float,__hash__) { return INTEGER_VAL((uint32_t)self); -}) +} -KRK_METHOD(float,__neg__,{ +KRK_Method(float,__neg__) { return FLOATING_VAL(-self); -}) +} -KRK_METHOD(float,__abs__,{ +KRK_Method(float,__abs__) { return self < 0.0 ? FLOATING_VAL(-self) : INTEGER_VAL(self); -}) +} #define BASIC_BIN_OP(name,operator) \ - KRK_METHOD(float,__ ## name ## __,{ \ + KRK_Method(float,__ ## name ## __) { \ METHOD_TAKES_EXACTLY(1); \ if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(self operator AS_FLOATING(argv[1])); \ else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL(self operator (double)AS_INTEGER(argv[1])); \ return NOTIMPL_VAL(); \ - }) \ - KRK_METHOD(float,__r ## name ## __,{ \ + } \ + KRK_Method(float,__r ## name ## __) { \ METHOD_TAKES_EXACTLY(1); \ if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) operator self); \ else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) operator self); \ return NOTIMPL_VAL(); \ - }) + } #define COMPARE_OP(name,operator) \ - KRK_METHOD(float,__ ## name ## __,{ \ + KRK_Method(float,__ ## name ## __) { \ METHOD_TAKES_EXACTLY(1); \ if (likely(IS_FLOATING(argv[1]))) return BOOLEAN_VAL(self operator AS_FLOATING(argv[1])); \ else if (likely(IS_INTEGER(argv[1]))) return BOOLEAN_VAL(self operator (double)AS_INTEGER(argv[1])); \ return NOTIMPL_VAL(); \ - }) + } BASIC_BIN_OP(add,+) BASIC_BIN_OP(sub,-) @@ -403,7 +403,7 @@ COMPARE_OP(ge, >=) #undef BASIC_BIN_OP #undef COMPARE_OP -KRK_METHOD(float,__truediv__,{ +KRK_Method(float,__truediv__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_FLOATING(argv[1]))) { double b = AS_FLOATING(argv[1]); @@ -415,17 +415,17 @@ KRK_METHOD(float,__truediv__,{ return FLOATING_VAL(self / (double)b); } return NOTIMPL_VAL(); -}) +} -KRK_METHOD(float,__rtruediv__,{ +KRK_Method(float,__rtruediv__) { METHOD_TAKES_EXACTLY(1); if (unlikely(self == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero"); else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) / self); else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / self); return NOTIMPL_VAL(); -}) +} -KRK_METHOD(float,__floordiv__,{ +KRK_Method(float,__floordiv__) { METHOD_TAKES_EXACTLY(1); if (likely(IS_INTEGER(argv[1]))) { krk_integer_type b = AS_INTEGER(argv[1]); @@ -437,15 +437,15 @@ KRK_METHOD(float,__floordiv__,{ return FLOATING_VAL(__builtin_floor(self / b)); } return NOTIMPL_VAL(); -}) +} -KRK_METHOD(float,__rfloordiv__,{ +KRK_Method(float,__rfloordiv__) { METHOD_TAKES_EXACTLY(1); if (unlikely(self == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero"); else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / self); else if (IS_FLOATING(argv[1])) return FLOATING_VAL(__builtin_floor(AS_FLOATING(argv[1]) / self)); return NOTIMPL_VAL(); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE krk_integer_type @@ -457,28 +457,28 @@ FUNC_SIG(bool,__init__) { return BOOLEAN_VAL(!krk_isFalsey(argv[1])); } -KRK_METHOD(bool,__str__,{ +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__,{ +KRK_Method(NoneType,__str__) { return OBJECT_VAL(S("None")); -}) +} -KRK_METHOD(NoneType,__hash__,{ +KRK_Method(NoneType,__hash__) { return INTEGER_VAL((uint32_t)AS_INTEGER(argv[0])); -}) +} -KRK_METHOD(NoneType,__eq__,{ +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) @@ -488,19 +488,19 @@ FUNC_SIG(NotImplementedType,__init__) { return NOTIMPL_VAL(); } -KRK_METHOD(NotImplementedType,__str__,{ +KRK_Method(NotImplementedType,__str__) { return OBJECT_VAL(S("NotImplemented")); -}) +} -KRK_METHOD(NotImplementedType,__hash__,{ +KRK_Method(NotImplementedType,__hash__) { return INTEGER_VAL(0); -}) +} -KRK_METHOD(NotImplementedType,__eq__,{ +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 */ diff --git a/src/obj_range.c b/src/obj_range.c index dde5969..1fdc3ee 100644 --- a/src/obj_range.c +++ b/src/obj_range.c @@ -35,7 +35,7 @@ FUNC_SIG(rangeiterator,__init__); #define CURRENT_NAME self #define CURRENT_CTYPE struct Range * -KRK_METHOD(range,__init__,{ +KRK_Method(range,__init__) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(3); self->min = 0; @@ -57,9 +57,9 @@ KRK_METHOD(range,__init__,{ } } return argv[0]; -}) +} -KRK_METHOD(range,__repr__,{ +KRK_Method(range,__repr__) { METHOD_TAKES_NONE(); krk_integer_type min = self->min; krk_integer_type max = self->max; @@ -73,9 +73,9 @@ KRK_METHOD(range,__repr__,{ len = snprintf(tmp,1024,"range(" PRIkrk_int "," PRIkrk_int "," PRIkrk_int ")", min, max, step); } return OBJECT_VAL(krk_copyString(tmp,len)); -}) +} -KRK_METHOD(range,__iter__,{ +KRK_Method(range,__iter__) { KrkInstance * output = krk_newInstance(rangeiterator); krk_integer_type min = self->min; krk_integer_type max = self->max; @@ -86,12 +86,12 @@ KRK_METHOD(range,__iter__,{ krk_pop(); return OBJECT_VAL(output); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE struct RangeIterator * -KRK_METHOD(rangeiterator,__init__,{ +KRK_Method(rangeiterator,__init__) { METHOD_TAKES_EXACTLY(3); CHECK_ARG(1,int,krk_integer_type,i); CHECK_ARG(2,int,krk_integer_type,max); @@ -100,9 +100,9 @@ KRK_METHOD(rangeiterator,__init__,{ self->max = max; self->step = step; return argv[0]; -}) +} -KRK_METHOD(rangeiterator,__call__,{ +KRK_Method(rangeiterator,__call__) { METHOD_TAKES_NONE(); krk_integer_type i = self->i; if (self->step > 0 ? (i >= self->max) : (i <= self->max)) { @@ -111,7 +111,7 @@ KRK_METHOD(rangeiterator,__call__,{ self->i = i + self->step; return INTEGER_VAL(i); } -}) +} _noexport void _createAndBind_rangeClass(void) { diff --git a/src/obj_set.c b/src/obj_set.c index 4cd8de6..ac96508 100644 --- a/src/obj_set.c +++ b/src/obj_set.c @@ -56,22 +56,22 @@ static int _set_init_callback(void * context, const KrkValue * values, size_t co return 0; } -KRK_METHOD(set,__init__,{ +KRK_Method(set,__init__) { METHOD_TAKES_AT_MOST(1); krk_initTable(&self->entries); if (argc == 2) { if (krk_unpackIterable(argv[1], self, _set_init_callback)) return NONE_VAL(); } return argv[0]; -}) +} -KRK_METHOD(set,__contains__,{ +KRK_Method(set,__contains__) { METHOD_TAKES_EXACTLY(1); KrkValue _unused; return BOOLEAN_VAL(krk_tableGet(&self->entries, argv[1], &_unused)); -}) +} -KRK_METHOD(set,__repr__,{ +KRK_Method(set,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL("{...}"); if (!self->entries.capacity) return OBJECT_VAL(S("set()")); @@ -100,9 +100,9 @@ KRK_METHOD(set,__repr__,{ pushStringBuilder(&sb,'}'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(set,__and__,{ +KRK_Method(set,__and__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,set,struct Set*,them); @@ -128,9 +128,9 @@ KRK_METHOD(set,__and__,{ } return krk_pop(); -}) +} -KRK_METHOD(set,__xor__,{ +KRK_Method(set,__xor__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,set,struct Set*,them); @@ -172,10 +172,10 @@ KRK_METHOD(set,__xor__,{ } return krk_pop(); -}) +} -KRK_METHOD(set,__or__,{ +KRK_Method(set,__or__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,set,struct Set*,them); @@ -187,14 +187,14 @@ KRK_METHOD(set,__or__,{ krk_tableAddAll(&them->entries, &AS_set(outSet)->entries); return krk_pop(); -}) +} -KRK_METHOD(set,__len__,{ +KRK_Method(set,__len__) { METHOD_TAKES_NONE(); return INTEGER_VAL(self->entries.count); -}) +} -KRK_METHOD(set,__eq__,{ +KRK_Method(set,__eq__) { METHOD_TAKES_EXACTLY(1); if (!IS_set(argv[1])) return NOTIMPL_VAL(); @@ -210,52 +210,56 @@ KRK_METHOD(set,__eq__,{ } return BOOLEAN_VAL(1); -}) +} -KRK_METHOD(set,add,{ +KRK_Method(set,add) { METHOD_TAKES_EXACTLY(1); krk_tableSet(&self->entries, argv[1], BOOLEAN_VAL(1)); -}) + return NONE_VAL(); +} -KRK_METHOD(set,remove,{ +KRK_Method(set,remove) { METHOD_TAKES_EXACTLY(1); if (!krk_tableDelete(&self->entries, argv[1])) return krk_runtimeError(vm.exceptions->keyError, "key error"); -}) + return NONE_VAL(); +} -KRK_METHOD(set,discard,{ +KRK_Method(set,discard) { METHOD_TAKES_EXACTLY(1); krk_tableDelete(&self->entries, argv[1]); -}) + return NONE_VAL(); +} -KRK_METHOD(set,clear,{ +KRK_Method(set,clear) { METHOD_TAKES_NONE(); krk_freeTable(&self->entries); krk_initTable(&self->entries); -}) + return NONE_VAL(); +} FUNC_SIG(setiterator,__init__); -KRK_METHOD(set,__iter__,{ +KRK_Method(set,__iter__) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(setiterator); krk_push(OBJECT_VAL(output)); FUNC_NAME(setiterator,__init__)(2,(KrkValue[]){krk_peek(0), argv[0]}, 0); return krk_pop(); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE struct SetIterator * -KRK_METHOD(setiterator,__init__,{ +KRK_Method(setiterator,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,set,void*,source); self->set = argv[1]; self->i = 0; return argv[0]; -}) +} -KRK_METHOD(setiterator,__call__,{ +KRK_Method(setiterator,__call__) { METHOD_TAKES_NONE(); if (unlikely(!IS_set(self->set))) return argv[0]; @@ -269,7 +273,7 @@ KRK_METHOD(setiterator,__call__,{ } self->i++; } while (1); -}) +} KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw) { KrkValue outSet = OBJECT_VAL(krk_newInstance(set)); diff --git a/src/obj_slice.c b/src/obj_slice.c index 0d8cc56..700d7c3 100644 --- a/src/obj_slice.c +++ b/src/obj_slice.c @@ -95,7 +95,7 @@ int krk_extractSlicer(const char * _method_name, KrkValue slicerVal, krk_integer #define CURRENT_CTYPE struct KrkSlice * #define CURRENT_NAME self -KRK_METHOD(slice,__init__,{ +KRK_Method(slice,__init__) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(3); @@ -113,9 +113,9 @@ KRK_METHOD(slice,__init__,{ } } return argv[0]; -}) +} -KRK_METHOD(slice,__repr__,{ +KRK_Method(slice,__repr__) { METHOD_TAKES_NONE(); if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL("slice(...)"); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; @@ -148,22 +148,22 @@ KRK_METHOD(slice,__repr__,{ pushStringBuilder(&sb,')'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(slice,start,{ +KRK_Method(slice,start) { ATTRIBUTE_NOT_ASSIGNABLE(); return self->start; -}) +} -KRK_METHOD(slice,end,{ +KRK_Method(slice,end) { ATTRIBUTE_NOT_ASSIGNABLE(); return self->end; -}) +} -KRK_METHOD(slice,step,{ +KRK_Method(slice,step) { ATTRIBUTE_NOT_ASSIGNABLE(); return self->step; -}) +} _noexport void _createAndBind_sliceClass(void) { diff --git a/src/obj_str.c b/src/obj_str.c index 5eaf251..8cb1350 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -25,14 +25,14 @@ static KrkValue FUNC_NAME(striterator,__init__)(int,const KrkValue[],int); #define CODEPOINT_BYTES(cp) (cp < 0x80 ? 1 : (cp < 0x800 ? 2 : (cp < 0x10000 ? 3 : 4))) -KRK_METHOD(str,__ord__,{ +KRK_Method(str,__ord__) { METHOD_TAKES_NONE(); if (self->codesLength != 1) return krk_runtimeError(vm.exceptions->typeError, "ord() expected a character, but string of length %d found", (int)self->codesLength); return INTEGER_VAL(krk_unicodeCodepoint(self,0)); -}) +} -KRK_METHOD(str,__init__,{ +KRK_Method(str,__init__) { /* Ignore argument which would have been an instance */ if (argc < 2) { return OBJECT_VAL(S("")); @@ -43,9 +43,9 @@ KRK_METHOD(str,__init__,{ krk_push(argv[1]); if (!krk_getType(argv[1])->_tostr) return krk_runtimeError(vm.exceptions->typeError, "Can not convert %s to str", krk_typeName(argv[1])); return krk_callDirect(krk_getType(argv[1])->_tostr, 1); -}) +} -KRK_METHOD(str,__add__,{ +KRK_Method(str,__add__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,str,KrkString*,them); const char * a; @@ -82,34 +82,34 @@ KRK_METHOD(str,__add__,{ KrkString * result = krk_takeStringVetted(chars, length, cpLength, type, hash); if (needsPop) krk_pop(); return OBJECT_VAL(result); -}) +} -KRK_METHOD(str,__hash__,{ +KRK_Method(str,__hash__) { return INTEGER_VAL(self->obj.hash); -}) +} -KRK_METHOD(str,__len__,{ +KRK_Method(str,__len__) { return INTEGER_VAL(self->codesLength); -}) +} -KRK_METHOD(str,__setitem__,{ +KRK_Method(str,__setitem__) { return krk_runtimeError(vm.exceptions->typeError, "Strings are not mutable."); -}) +} /* str.__int__(base=10) */ -KRK_METHOD(str,__int__,{ +KRK_Method(str,__int__) { METHOD_TAKES_AT_MOST(1); int base = (argc < 2 || !IS_INTEGER(argv[1])) ? 0 : (int)AS_INTEGER(argv[1]); return krk_parse_int(AS_CSTRING(argv[0]), AS_STRING(argv[0])->length, base); -}) +} /* str.__float__() */ -KRK_METHOD(str,__float__,{ +KRK_Method(str,__float__) { METHOD_TAKES_NONE(); return FLOATING_VAL(strtod(AS_CSTRING(argv[0]),NULL)); -}) +} -KRK_METHOD(str,__getitem__,{ +KRK_Method(str,__getitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { CHECK_ARG(1,int,krk_integer_type,asInt); @@ -167,10 +167,10 @@ KRK_METHOD(str,__getitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) +} /* str.format(**kwargs) */ -KRK_METHOD(str,format,{ +KRK_Method(str,format) { KrkValue kwargs = NONE_VAL(); if (hasKw) { kwargs = argv[argc]; @@ -310,9 +310,9 @@ _freeAndDone: FREE_ARRAY(char,stringBytes,stringCapacity); free(workSpace); return NONE_VAL(); -}) +} -KRK_METHOD(str,__mul__,{ +KRK_Method(str,__mul__) { METHOD_TAKES_EXACTLY(1); if (!IS_INTEGER(argv[1])) return NOTIMPL_VAL(); CHECK_ARG(1,int,krk_integer_type,howMany); @@ -330,13 +330,13 @@ KRK_METHOD(str,__mul__,{ *c = '\0'; return OBJECT_VAL(krk_takeString(out, totalLength)); -}) +} -KRK_METHOD(str,__rmul__,{ +KRK_Method(str,__rmul__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) return FUNC_NAME(str,__mul__)(argc,argv,hasKw); return NOTIMPL_VAL(); -}) +} struct _str_join_context { struct StringBuilder * sb; @@ -366,7 +366,7 @@ static int _str_join_callback(void * context, const KrkValue * values, size_t co } /* str.join(list) */ -KRK_METHOD(str,join,{ +KRK_Method(str,join) { METHOD_TAKES_EXACTLY(1); struct StringBuilder sb = {0}; @@ -378,7 +378,7 @@ KRK_METHOD(str,join,{ } return finishStringBuilder(&sb); -}) +} static int isWhitespace(char c) { return (c == ' ' || c == '\t' || c == '\n' || c == '\r'); @@ -393,7 +393,7 @@ static int substringMatch(const char * haystack, size_t haystackLen, const char } /* str.__contains__ */ -KRK_METHOD(str,__contains__,{ +KRK_Method(str,__contains__) { METHOD_TAKES_EXACTLY(1); if (IS_NONE(argv[1])) return BOOLEAN_VAL(0); CHECK_ARG(1,str,KrkString*,needle); @@ -403,7 +403,7 @@ KRK_METHOD(str,__contains__,{ } } return BOOLEAN_VAL(0); -}) +} static int charIn(char c, const char * str) { for (const char * s = str; *s; s++) { @@ -439,21 +439,21 @@ static KrkValue _string_strip_shared(int argc, const KrkValue argv[], int which) return OBJECT_VAL(krk_copyString(&AS_CSTRING(argv[0])[start], end-start)); } -KRK_METHOD(str,strip,{ +KRK_Method(str,strip) { METHOD_TAKES_AT_MOST(1); /* TODO */ return _string_strip_shared(argc,argv,0); -}) -KRK_METHOD(str,lstrip,{ +} +KRK_Method(str,lstrip) { METHOD_TAKES_AT_MOST(1); /* TODO */ return _string_strip_shared(argc,argv,1); -}) -KRK_METHOD(str,rstrip,{ +} +KRK_Method(str,rstrip) { METHOD_TAKES_AT_MOST(1); /* TODO */ return _string_strip_shared(argc,argv,2); -}) +} #define strCompare(name,lop,iop,rop) \ - KRK_METHOD(str,name,{ \ + KRK_Method(str,name) { \ METHOD_TAKES_EXACTLY(1); \ if (!IS_STRING(argv[1])) { \ return NOTIMPL_VAL(); \ @@ -467,14 +467,14 @@ KRK_METHOD(str,rstrip,{ if (a[i] iop b[i]) return BOOLEAN_VAL(0); \ } \ return BOOLEAN_VAL((aLen rop bLen)); \ - }) + } strCompare(__gt__,>,<,>) strCompare(__lt__,<,>,<) strCompare(__ge__,>,<,>=) strCompare(__le__,<,>,<=) -KRK_METHOD(str,__mod__,{ +KRK_Method(str,__mod__) { METHOD_TAKES_EXACTLY(1); KrkTuple * myTuple; @@ -578,10 +578,10 @@ _notEnough: _exception: discardStringBuilder(&sb); return NONE_VAL(); -}) +} /* str.split() */ -KRK_METHOD(str,split,{ +KRK_Method(str,split) { METHOD_TAKES_AT_MOST(2); if (argc > 1) { if (!IS_STRING(argv[1])) { @@ -668,9 +668,9 @@ KRK_METHOD(str,split,{ krk_pop(); return myList; -}) +} -KRK_METHOD(str,replace,{ +KRK_Method(str,replace) { METHOD_TAKES_AT_LEAST(2); METHOD_TAKES_AT_MOST(3); CHECK_ARG(1,str,KrkString*,oldStr); @@ -705,14 +705,14 @@ KRK_METHOD(str,replace,{ KrkValue tmp = OBJECT_VAL(krk_copyString(stringBytes, stringLength)); if (stringBytes) FREE_ARRAY(char,stringBytes,stringCapacity); return tmp; -}) +} #define WRAP_INDEX(index) \ if (index < 0) index += self->codesLength; \ if (index < 0) index = 0; \ if (index >= (krk_integer_type)self->codesLength) index = self->codesLength -KRK_METHOD(str,find,{ +KRK_Method(str,find) { METHOD_TAKES_AT_LEAST(1); METHOD_TAKES_AT_MOST(3); CHECK_ARG(1,str,KrkString*,substr); @@ -752,29 +752,29 @@ KRK_METHOD(str,find,{ } return INTEGER_VAL(-1); -}) +} -KRK_METHOD(str,index,{ +KRK_Method(str,index) { KrkValue result = FUNC_NAME(str,find)(argc,argv,hasKw); if (IS_INTEGER(result) && AS_INTEGER(result) == -1) { return krk_runtimeError(vm.exceptions->valueError, "substring not found"); } return result; -}) +} -KRK_METHOD(str,startswith,{ +KRK_Method(str,startswith) { METHOD_TAKES_EXACTLY(1); /* I know the Python versions of these take optional start, end... */ CHECK_ARG(1,str,KrkString*,prefix); return BOOLEAN_VAL(substringMatch(self->chars,self->length,prefix->chars,prefix->length)); -}) +} -KRK_METHOD(str,endswith,{ +KRK_Method(str,endswith) { METHOD_TAKES_EXACTLY(1); /* I know the Python versions of these take optional start, end... */ CHECK_ARG(1,str,KrkString*,suffix); if (suffix->length > self->length) return BOOLEAN_VAL(0); return BOOLEAN_VAL(substringMatch(self->chars + (self->length - suffix->length), suffix->length, suffix->chars, suffix->length)); -}) +} /** * str.__repr__() @@ -782,7 +782,7 @@ KRK_METHOD(str,endswith,{ * Strings are special because __str__ should do nothing but __repr__ * should escape characters like quotes. */ -KRK_METHOD(str,__repr__,{ +KRK_Method(str,__repr__) { METHOD_TAKES_NONE(); size_t stringCapacity = 0; size_t stringLength = 0; @@ -836,17 +836,17 @@ KRK_METHOD(str,__repr__,{ KrkValue tmp = OBJECT_VAL(krk_copyString(stringBytes, stringLength)); if (stringBytes) FREE_ARRAY(char,stringBytes,stringCapacity); return tmp; -}) +} -KRK_METHOD(str,encode,{ +KRK_Method(str,encode) { METHOD_TAKES_NONE(); return OBJECT_VAL(krk_newBytes(AS_STRING(argv[0])->length, (uint8_t*)AS_CSTRING(argv[0]))); -}) +} -KRK_METHOD(str,__str__,{ +KRK_Method(str,__str__) { METHOD_TAKES_NONE(); return argv[0]; -}) +} void krk_addObjects(void) { KrkValue tmp = FUNC_NAME(str,__add__)(2, (KrkValue[]){krk_peek(1), krk_peek(0)},0); @@ -854,7 +854,7 @@ void krk_addObjects(void) { krk_push(tmp); } -KRK_METHOD(str,__iter__,{ +KRK_Method(str,__iter__) { METHOD_TAKES_NONE(); KrkInstance * output = krk_newInstance(vm.baseClasses->striteratorClass); @@ -863,7 +863,7 @@ KRK_METHOD(str,__iter__,{ krk_pop(); return OBJECT_VAL(output); -}) +} #define CHECK_ALL(test) do { \ krk_unicodeString(self); \ @@ -872,35 +872,35 @@ KRK_METHOD(str,__iter__,{ if (!(test)) { return BOOLEAN_VAL(0); } \ } return BOOLEAN_VAL(1); } while (0) -KRK_METHOD(str,isalnum,{ +KRK_Method(str,isalnum) { CHECK_ALL( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ); -}) +} -KRK_METHOD(str,isalpha,{ +KRK_Method(str,isalpha) { CHECK_ALL( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ); -}) +} -KRK_METHOD(str,isdigit,{ +KRK_Method(str,isdigit) { CHECK_ALL( (c >= '0' && c <= '9') ); -}) +} -KRK_METHOD(str,isxdigit,{ +KRK_Method(str,isxdigit) { CHECK_ALL( (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9') ); -}) +} -KRK_METHOD(str,isspace, { +KRK_Method(str,isspace) { CHECK_ALL( (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v') ); -}) +} -KRK_METHOD(str,islower, { +KRK_Method(str,islower) { CHECK_ALL( (c >= 'a' && c <= 'z') ); -}) +} -KRK_METHOD(str,isupper, { +KRK_Method(str,isupper) { CHECK_ALL( (c >= 'A' && c <= 'Z') ); -}) +} -KRK_METHOD(str,lower, { +KRK_Method(str,lower) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; @@ -913,9 +913,9 @@ KRK_METHOD(str,lower, { } return finishStringBuilder(&sb); -}) +} -KRK_METHOD(str,upper, { +KRK_Method(str,upper) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; @@ -928,9 +928,9 @@ KRK_METHOD(str,upper, { } return finishStringBuilder(&sb); -}) +} -KRK_METHOD(str,title, { +KRK_Method(str,title) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; @@ -950,20 +950,20 @@ KRK_METHOD(str,title, { } return finishStringBuilder(&sb); -}) +} #undef CURRENT_CTYPE #define CURRENT_CTYPE KrkInstance * -KRK_METHOD(striterator,__init__,{ +KRK_Method(striterator,__init__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,str,KrkString*,base); krk_push(OBJECT_VAL(self)); krk_attachNamedObject(&self->fields, "s", (KrkObj*)base); krk_attachNamedValue(&self->fields, "i", INTEGER_VAL(0)); return krk_pop(); -}) +} -KRK_METHOD(striterator,__call__,{ +KRK_Method(striterator,__call__) { METHOD_TAKES_NONE(); KrkValue _str; KrkValue _counter; @@ -985,7 +985,7 @@ KRK_METHOD(striterator,__call__,{ } _corrupt: return krk_runtimeError(vm.exceptions->typeError, "Corrupt str iterator: %s", errorStr); -}) +} _noexport void _createAndBind_strClass(void) { diff --git a/src/obj_tuple.c b/src/obj_tuple.c index dbcd11f..ce12280 100644 --- a/src/obj_tuple.c +++ b/src/obj_tuple.c @@ -59,20 +59,20 @@ KrkValue krk_tuple_of(int argc, const KrkValue argv[], int hasKw) { #define CURRENT_CTYPE KrkTuple * #define CURRENT_NAME self -KRK_METHOD(tuple,__contains__,{ +KRK_Method(tuple,__contains__) { METHOD_TAKES_EXACTLY(1); for (size_t i = 0; i < self->values.count; ++i) { if (krk_valuesEqual(self->values.values[i], argv[1])) return BOOLEAN_VAL(1); } return BOOLEAN_VAL(0); -}) +} -KRK_METHOD(tuple,__len__,{ +KRK_Method(tuple,__len__) { METHOD_TAKES_NONE(); return INTEGER_VAL(self->values.count); -}) +} -KRK_METHOD(tuple,__getitem__,{ +KRK_Method(tuple,__getitem__) { METHOD_TAKES_EXACTLY(1); if (IS_INTEGER(argv[1])) { CHECK_ARG(1,int,krk_integer_type,index); @@ -111,9 +111,9 @@ KRK_METHOD(tuple,__getitem__,{ } else { return TYPE_ERROR(int or slice, argv[1]); } -}) +} -KRK_METHOD(tuple,__eq__,{ +KRK_Method(tuple,__eq__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); KrkTuple * them = AS_tuple(argv[1]); @@ -122,9 +122,9 @@ KRK_METHOD(tuple,__eq__,{ if (!krk_valuesEqual(self->values.values[i], them->values.values[i])) return BOOLEAN_VAL(0); } return BOOLEAN_VAL(1); -}) +} -KRK_METHOD(tuple,__lt__,{ +KRK_Method(tuple,__lt__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); KrkTuple * them = AS_tuple(argv[1]); @@ -139,9 +139,9 @@ KRK_METHOD(tuple,__lt__,{ /* continue on == */ } return BOOLEAN_VAL((self->values.count < them->values.count)); -}) +} -KRK_METHOD(tuple,__gt__,{ +KRK_Method(tuple,__gt__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); KrkTuple * them = AS_tuple(argv[1]); @@ -155,9 +155,9 @@ KRK_METHOD(tuple,__gt__,{ if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(0); } return BOOLEAN_VAL((self->values.count > them->values.count)); -}) +} -KRK_METHOD(tuple,__le__,{ +KRK_Method(tuple,__le__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); KrkTuple * them = AS_tuple(argv[1]); @@ -172,9 +172,9 @@ KRK_METHOD(tuple,__le__,{ /* continue on == */ } return BOOLEAN_VAL((self->values.count <= them->values.count)); -}) +} -KRK_METHOD(tuple,__ge__,{ +KRK_Method(tuple,__ge__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); KrkTuple * them = AS_tuple(argv[1]); @@ -188,9 +188,9 @@ KRK_METHOD(tuple,__ge__,{ if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(0); } return BOOLEAN_VAL((self->values.count >= them->values.count)); -}) +} -KRK_METHOD(tuple,__repr__,{ +KRK_Method(tuple,__repr__) { if (((KrkObj*)self)->flags & KRK_OBJ_FLAGS_IN_REPR) return OBJECT_VAL(S("(...)")); ((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR; /* String building time. */ @@ -216,9 +216,9 @@ KRK_METHOD(tuple,__repr__,{ pushStringBuilder(&sb, ')'); ((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(tuple,__add__,{ +KRK_Method(tuple,__add__) { METHOD_TAKES_EXACTLY(1); if (!IS_tuple(argv[1])) return krk_runtimeError(vm.exceptions->typeError, @@ -235,7 +235,7 @@ KRK_METHOD(tuple,__add__,{ out->values.values[out->values.count++] = other->values.values[i]; } return krk_pop(); -}) +} /** * @brief Iterator over the values in a tuple. @@ -270,15 +270,15 @@ static KrkValue _tuple_iter_call(int argc, const KrkValue argv[], int hasKw) { } } -KRK_METHOD(tuple,__iter__,{ +KRK_Method(tuple,__iter__) { KrkInstance * output = krk_newInstance(vm.baseClasses->tupleiteratorClass); krk_push(OBJECT_VAL(output)); _tuple_iter_init(2, (KrkValue[]){krk_peek(0), argv[0]}, 0); krk_pop(); return OBJECT_VAL(output); -}) +} -KRK_METHOD(tuple,__hash__,{ +KRK_Method(tuple,__hash__) { if (self->obj.flags & KRK_OBJ_FLAGS_VALID_HASH) { return INTEGER_VAL(self->obj.hash); } @@ -295,7 +295,7 @@ KRK_METHOD(tuple,__hash__,{ return INTEGER_VAL(self->obj.hash); _unhashable: return NONE_VAL(); -}) +} _noexport void _createAndBind_tupleClass(void) { diff --git a/src/obj_typing.c b/src/obj_typing.c index 44bfed0..460a716 100644 --- a/src/obj_typing.c +++ b/src/obj_typing.c @@ -47,7 +47,7 @@ static KrkValue typeToString(KrkValue val) { } } -KRK_FUNC(__class_getitem__,{ +KRK_Function(__class_getitem__) { FUNCTION_TAKES_EXACTLY(2); if (!IS_CLASS(argv[0])) return TYPE_ERROR(class,argv[0]); @@ -62,6 +62,6 @@ KRK_FUNC(__class_getitem__,{ krk_pop(); pushStringBuilder(&sb,']'); return finishStringBuilder(&sb); -}) +} NativeFn KrkGenericAlias = FUNC_NAME(krk,__class_getitem__); diff --git a/src/os.c b/src/os.c index ed41aba..50865dd 100644 --- a/src/os.c +++ b/src/os.c @@ -30,7 +30,7 @@ static KrkClass * stat_result = NULL; #define S_KEY(key,val) krk_attachNamedObject(AS_DICT(result), #key, (KrkObj*)val); #ifndef _WIN32 -KRK_FUNC(uname,{ +KRK_Function(uname) { struct utsname buf; if (uname(&buf) < 0) return NONE_VAL(); @@ -44,9 +44,9 @@ KRK_FUNC(uname,{ DO_KEY(machine); return krk_pop();; -}) +} #else -KRK_FUNC(uname,{ +KRK_Function(uname) { KrkValue result = krk_dict_of(0, NULL, 0); krk_push(result); @@ -88,7 +88,7 @@ KRK_FUNC(uname,{ S_KEY(nodename,krk_copyString(buffer,dwSize)); return krk_pop(); -}) +} #endif static KrkClass * Environ; @@ -109,7 +109,7 @@ static int _setVar(KrkString * key, KrkString * val) { #endif } -KRK_METHOD(Environ,__setitem__,{ +KRK_Method(Environ,__setitem__) { METHOD_TAKES_EXACTLY(2); CHECK_ARG(1,str,KrkString*,key); CHECK_ARG(2,str,KrkString*,val); @@ -122,7 +122,7 @@ KRK_METHOD(Environ,__setitem__,{ } return krk_runtimeError(OSError, "%s", strerror(errno)); -}) +} static void _unsetVar(KrkString * str) { #ifndef _WIN32 @@ -136,14 +136,14 @@ static void _unsetVar(KrkString * str) { #endif } -KRK_METHOD(Environ,__delitem__,{ +KRK_Method(Environ,__delitem__) { METHOD_TAKES_EXACTLY(1); CHECK_ARG(1,str,KrkString*,key); _unsetVar(key); krk_push(argv[0]); krk_push(argv[1]); return krk_callDirect(vm.baseClasses->dictClass->_delitem, 2); -}) +} static void _loadEnviron(KrkInstance * module) { /* Create a new class to subclass `dict` */ @@ -193,74 +193,77 @@ static void _loadEnviron(KrkInstance * module) { } -KRK_FUNC(system,{ +KRK_Function(system) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,str,KrkString*,cmd); return INTEGER_VAL(system(cmd->chars)); -}) +} -KRK_FUNC(getcwd,{ +KRK_Function(getcwd) { FUNCTION_TAKES_NONE(); char buf[4096]; /* TODO PATH_MAX? */ if (!getcwd(buf, 4096)) return krk_runtimeError(OSError, "%s", strerror(errno)); return OBJECT_VAL(krk_copyString(buf, strlen(buf))); -}) +} -KRK_FUNC(chdir,{ +KRK_Function(chdir) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,str,KrkString*,newDir); if (chdir(newDir->chars)) return krk_runtimeError(OSError, "%s", strerror(errno)); -}) + return NONE_VAL(); +} -KRK_FUNC(getpid,{ +KRK_Function(getpid) { FUNCTION_TAKES_NONE(); return INTEGER_VAL(getpid()); -}) +} -KRK_FUNC(strerror,{ +KRK_Function(strerror) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,errorNo); char *s = strerror(errorNo); if (!s) return NONE_VAL(); return OBJECT_VAL(krk_copyString(s,strlen(s))); -}) +} -KRK_FUNC(access,{ +KRK_Function(access) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,str,KrkString*,path); CHECK_ARG(1,int,krk_integer_type,mask); if (access(path->chars, mask) == 0) return BOOLEAN_VAL(1); return BOOLEAN_VAL(0); -}) +} -KRK_FUNC(abort,{ +KRK_Function(abort) { abort(); -}) +} -KRK_FUNC(exit,{ +KRK_Function(exit) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,retcode); exit(retcode); -}) +} -KRK_FUNC(remove,{ +KRK_Function(remove) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,str,KrkString*,path); if (remove(path->chars) != 0) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(truncate,{ +KRK_Function(truncate) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,str,KrkString*,path); CHECK_ARG(1,int,krk_integer_type,length); if (truncate(path->chars, length) != 0) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(dup,{ +KRK_Function(dup) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,fd); int result = dup(fd); @@ -268,9 +271,9 @@ KRK_FUNC(dup,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(dup2,{ +KRK_Function(dup2) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,int,krk_integer_type,fd); CHECK_ARG(1,int,krk_integer_type,fd2); @@ -279,15 +282,15 @@ KRK_FUNC(dup2,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(isatty,{ +KRK_Function(isatty) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,fd); return BOOLEAN_VAL(isatty(fd)); -}) +} -KRK_FUNC(lseek,{ +KRK_Function(lseek) { FUNCTION_TAKES_EXACTLY(3); CHECK_ARG(0,int,krk_integer_type,fd); CHECK_ARG(1,int,krk_integer_type,pos); @@ -297,9 +300,9 @@ KRK_FUNC(lseek,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(open,{ +KRK_Function(open) { FUNCTION_TAKES_AT_LEAST(2); FUNCTION_TAKES_AT_MOST(3); CHECK_ARG(0,str,KrkString*,path); @@ -314,20 +317,21 @@ KRK_FUNC(open,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(close,{ +KRK_Function(close) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,fd); if (close(fd) == -1) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} #ifdef _WIN32 #define mkdir(p,m) mkdir(p); (void)m #endif -KRK_FUNC(mkdir,{ +KRK_Function(mkdir) { FUNCTION_TAKES_AT_LEAST(1); FUNCTION_TAKES_AT_MOST(2); CHECK_ARG(0,str,KrkString*,path); @@ -340,9 +344,10 @@ KRK_FUNC(mkdir,{ if (result == -1) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(read,{ +KRK_Function(read) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,int,krk_integer_type,fd); CHECK_ARG(1,int,krk_integer_type,n); @@ -357,14 +362,14 @@ KRK_FUNC(read,{ free(tmp); return krk_pop(); } -}) +} #ifndef IS_bytes #define IS_bytes(o) IS_BYTES(o) #define AS_bytes(o) AS_BYTES(o) #endif -KRK_FUNC(write,{ +KRK_Function(write) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,int,krk_integer_type,fd); CHECK_ARG(1,bytes,KrkBytes*,data); @@ -373,10 +378,10 @@ KRK_FUNC(write,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} #ifndef _WIN32 -KRK_FUNC(pipe,{ +KRK_Function(pipe) { FUNCTION_TAKES_NONE(); int fds[2]; if (pipe(fds) == -1) { @@ -387,32 +392,33 @@ KRK_FUNC(pipe,{ AS_TUPLE(krk_peek(0))->values.values[1] = INTEGER_VAL(fds[1]); AS_TUPLE(krk_peek(0))->values.count = 2; return krk_pop(); -}) +} -KRK_FUNC(kill,{ +KRK_Function(kill) { FUNCTION_TAKES_EXACTLY(2); int result = kill(AS_INTEGER(argv[0]), AS_INTEGER(argv[1])); if (result == -1) { return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(fork,{ +KRK_Function(fork) { FUNCTION_TAKES_NONE(); return INTEGER_VAL(fork()); -}) +} -KRK_FUNC(symlink,{ +KRK_Function(symlink) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,str,KrkString*,src); CHECK_ARG(1,str,KrkString*,dst); if (symlink(src->chars, dst->chars) != 0) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(tcgetpgrp,{ +KRK_Function(tcgetpgrp) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,fd); int result = tcgetpgrp(fd); @@ -420,9 +426,9 @@ KRK_FUNC(tcgetpgrp,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return INTEGER_VAL(result); -}) +} -KRK_FUNC(tcsetpgrp,{ +KRK_Function(tcsetpgrp) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,int,krk_integer_type,fd); CHECK_ARG(1,int,krk_integer_type,pgrp); @@ -430,9 +436,10 @@ KRK_FUNC(tcsetpgrp,{ if (result == -1) { return krk_runtimeError(OSError, "%s", strerror(errno)); } -}) + return NONE_VAL(); +} -KRK_FUNC(ttyname,{ +KRK_Function(ttyname) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,fd); char * result = ttyname(fd); @@ -440,9 +447,9 @@ KRK_FUNC(ttyname,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return OBJECT_VAL(krk_copyString(result,strlen(result))); -}) +} -KRK_FUNC(get_terminal_size,{ +KRK_Function(get_terminal_size) { FUNCTION_TAKES_AT_MOST(1); int fd = 1; if (argc > 0) { @@ -462,7 +469,7 @@ KRK_FUNC(get_terminal_size,{ AS_TUPLE(krk_peek(0))->values.values[1] = INTEGER_VAL(wsz.ws_row); AS_TUPLE(krk_peek(0))->values.count = 2; return krk_pop(); -}) +} #endif static int makeArgs(int count, const KrkValue * values, char *** argsOut, const char * _method_name) { @@ -480,7 +487,7 @@ static int makeArgs(int count, const KrkValue * values, char *** argsOut, const return 0; } -KRK_FUNC(execl,{ +KRK_Function(execl) { FUNCTION_TAKES_AT_LEAST(1); CHECK_ARG(0,str,KrkString*,path); char ** args; @@ -490,9 +497,9 @@ KRK_FUNC(execl,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return krk_runtimeError(OSError, "Expected to not return from exec, but did."); -}) +} -KRK_FUNC(execlp,{ +KRK_Function(execlp) { FUNCTION_TAKES_AT_LEAST(1); CHECK_ARG(0,str,KrkString*,filename); char ** args; @@ -502,9 +509,9 @@ KRK_FUNC(execlp,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return krk_runtimeError(OSError, "Expected to not return from exec, but did."); -}) +} -KRK_FUNC(execle,{ +KRK_Function(execle) { FUNCTION_TAKES_AT_LEAST(1); CHECK_ARG(0,str,KrkString*,path); CHECK_ARG((argc-1),list,KrkList*,envp); @@ -521,9 +528,9 @@ KRK_FUNC(execle,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return krk_runtimeError(OSError, "Expected to not return from exec, but did."); -}) +} -KRK_FUNC(execv,{ +KRK_Function(execv) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,str,KrkString*,filename); CHECK_ARG(1,list,KrkList*,args); @@ -534,9 +541,9 @@ KRK_FUNC(execv,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return krk_runtimeError(OSError, "Expected to not return from exec, but did."); -}) +} -KRK_FUNC(execvp,{ +KRK_Function(execvp) { FUNCTION_TAKES_EXACTLY(2); CHECK_ARG(0,str,KrkString*,path); CHECK_ARG(1,list,KrkList*,args); @@ -547,7 +554,7 @@ KRK_FUNC(execvp,{ return krk_runtimeError(OSError, "%s", strerror(errno)); } return krk_runtimeError(OSError, "Expected to not return from exec, but did."); -}) +} #define SET(thing) krk_attachNamedValue(&out->fields, #thing, INTEGER_VAL(buf. thing)) #ifdef _WIN32 @@ -556,7 +563,7 @@ KRK_FUNC(execvp,{ #else #define STAT_STRUCT struct stat #endif -KRK_FUNC(stat,{ +KRK_Function(stat) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,str,KrkString*,path); STAT_STRUCT buf; @@ -579,7 +586,7 @@ KRK_FUNC(stat,{ /* TODO block sizes */ return krk_pop(); -}) +} #undef SET #define IS_stat_result(o) (krk_isInstanceOf(o,stat_result)) @@ -591,7 +598,7 @@ KRK_FUNC(stat,{ krk_tableGet(&self->fields, OBJECT_VAL(S(#name)), &name); \ if (!IS_INTEGER(name)) return krk_runtimeError(vm.exceptions->valueError, "stat_result is invalid") -KRK_METHOD(stat_result,__repr__,{ +KRK_Method(stat_result,__repr__) { METHOD_TAKES_NONE(); getProp(st_dev); getProp(st_ino); @@ -623,44 +630,44 @@ KRK_METHOD(stat_result,__repr__,{ krk_push(OBJECT_VAL(krk_copyString(buf,len))); free(buf); return krk_pop(); -}) +} -KRK_FUNC(S_ISBLK,{ +KRK_Function(S_ISBLK) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISBLK(mode)); -}) -KRK_FUNC(S_ISCHR,{ +} +KRK_Function(S_ISCHR) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISCHR(mode)); -}) -KRK_FUNC(S_ISDIR,{ +} +KRK_Function(S_ISDIR) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISDIR(mode)); -}) -KRK_FUNC(S_ISFIFO,{ +} +KRK_Function(S_ISFIFO) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISFIFO(mode)); -}) -KRK_FUNC(S_ISREG,{ +} +KRK_Function(S_ISREG) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISREG(mode)); -}) +} #ifndef _WIN32 -KRK_FUNC(S_ISLNK,{ +KRK_Function(S_ISLNK) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISLNK(mode)); -}) -KRK_FUNC(S_ISSOCK,{ +} +KRK_Function(S_ISSOCK) { FUNCTION_TAKES_EXACTLY(1); CHECK_ARG(0,int,krk_integer_type,mode); return INTEGER_VAL(S_ISSOCK(mode)); -}) +} #endif _noexport diff --git a/src/threads.c b/src/threads.c index dcaaaec..c92178f 100644 --- a/src/threads.c +++ b/src/threads.c @@ -54,10 +54,10 @@ struct Lock { pthread_mutex_t mutex; }; -KRK_FUNC(current_thread,{ +KRK_Function(current_thread) { if (&krk_currentThread == vm.threads) return NONE_VAL(); return krk_currentThread.stack[0]; -}) +} #define IS_Thread(o) (krk_isInstanceOf(o, Thread)) #define AS_Thread(o) ((struct Thread *)AS_OBJECT(o)) @@ -115,21 +115,22 @@ static void * _startthread(void * _threadObj) { return NULL; } -KRK_METHOD(Thread,tid,{ +KRK_Method(Thread,tid) { METHOD_TAKES_NONE(); /* Property, but can not be assigned. */ return INTEGER_VAL(self->tid); -}) +} -KRK_METHOD(Thread,join,{ +KRK_Method(Thread,join) { if (self->threadState == &krk_currentThread) return krk_runtimeError(ThreadError, "Thread can not join itself."); if (!self->started) return krk_runtimeError(ThreadError, "Thread has not been started."); pthread_join(self->nativeRef, NULL); -}) + return NONE_VAL(); +} -KRK_METHOD(Thread,start,{ +KRK_Method(Thread,start) { METHOD_TAKES_NONE(); if (self->started) @@ -140,12 +141,12 @@ KRK_METHOD(Thread,start,{ pthread_create(&self->nativeRef, NULL, _startthread, (void*)self); return argv[0]; -}) +} -KRK_METHOD(Thread,is_alive,{ +KRK_Method(Thread,is_alive) { METHOD_TAKES_NONE(); return BOOLEAN_VAL(self->alive); -}) +} #undef CURRENT_CTYPE @@ -153,11 +154,11 @@ KRK_METHOD(Thread,is_alive,{ #define AS_Lock(o) ((struct Lock *)AS_OBJECT(o)) #define CURRENT_CTYPE struct Lock * -KRK_METHOD(Lock,__init__,{ +KRK_Method(Lock,__init__) { METHOD_TAKES_NONE(); /* TODO lock options, like recursive or error-checked? */ pthread_mutex_init(&self->mutex, NULL); return argv[0]; -}) +} static inline void _pushLockStatus(struct Lock * self, struct StringBuilder * sb) { #ifdef __GLIBC__ @@ -174,7 +175,7 @@ static inline void _pushLockStatus(struct Lock * self, struct StringBuilder * sb #endif } -KRK_METHOD(Lock,__repr__,{ +KRK_Method(Lock,__repr__) { METHOD_TAKES_NONE(); struct StringBuilder sb = {0}; pushStringBuilderStr(&sb, "'); return finishStringBuilder(&sb); -}) +} -KRK_METHOD(Lock,__enter__,{ +KRK_Method(Lock,__enter__) { METHOD_TAKES_NONE(); pthread_mutex_lock(&self->mutex); -}) + return NONE_VAL(); +} -KRK_METHOD(Lock,__exit__,{ +KRK_Method(Lock,__exit__) { pthread_mutex_unlock(&self->mutex); -}) + return NONE_VAL(); +} _noexport void _createAndBind_threadsMod(void) { diff --git a/src/time.c b/src/time.c index fd23eb6..cf35f5f 100644 --- a/src/time.c +++ b/src/time.c @@ -11,7 +11,7 @@ #include #include -KRK_FUNC(sleep,{ +KRK_Function(sleep) { FUNCTION_TAKES_EXACTLY(1); if (!IS_INTEGER(argv[0]) && !IS_FLOATING(argv[0])) { @@ -25,9 +25,9 @@ KRK_FUNC(sleep,{ usleep(usecs); return BOOLEAN_VAL(1); -}) +} -KRK_FUNC(time,{ +KRK_Function(time) { FUNCTION_TAKES_NONE(); struct timeval tv; @@ -36,7 +36,7 @@ KRK_FUNC(time,{ double out = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; return FLOATING_VAL(out); -}) +} _noexport void _createAndBind_timeMod(void) { diff --git a/src/vm.c b/src/vm.c index ece2a4e..6cce6fc 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1244,7 +1244,7 @@ int krk_isFalsey(KrkValue value) { } #ifndef KRK_DISABLE_DEBUG -KRK_FUNC(set_tracing,{ +KRK_Function(set_tracing) { if (hasKw) { KrkValue test; if (krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("tracing")), &test) && IS_INTEGER(test)) { @@ -1258,14 +1258,14 @@ KRK_FUNC(set_tracing,{ if (AS_INTEGER(test) == 1) vm.globalFlags |= KRK_GLOBAL_ENABLE_STRESS_GC; else krk_currentThread.flags &= ~KRK_GLOBAL_ENABLE_STRESS_GC; } } return BOOLEAN_VAL(1); -}) +} #else -KRK_FUNC(set_tracing,{ +KRK_Function(set_tracing) { return krk_runtimeError(vm.exceptions->typeError,"Debugging is not enabled in this build."); -}) +} #endif -KRK_FUNC(getsizeof,{ +KRK_Function(getsizeof) { if (argc < 1 || !IS_OBJECT(argv[0])) return INTEGER_VAL(0); size_t mySize = 0; switch (AS_OBJECT(argv[0])->type) { @@ -1348,25 +1348,25 @@ KRK_FUNC(getsizeof,{ default: break; } return INTEGER_VAL(mySize); -}) +} -KRK_FUNC(set_clean_output,{ +KRK_Function(set_clean_output) { if (!argc || (IS_BOOLEAN(argv[0]) && AS_BOOLEAN(argv[0]))) { vm.globalFlags |= KRK_GLOBAL_CLEAN_OUTPUT; } else { vm.globalFlags &= ~KRK_GLOBAL_CLEAN_OUTPUT; } return NONE_VAL(); -}) +} -KRK_FUNC(importmodule,{ +KRK_Function(importmodule) { FUNCTION_TAKES_EXACTLY(1); if (!IS_STRING(argv[0])) return TYPE_ERROR(str,argv[0]); if (!krk_doRecursiveModuleLoad(AS_STRING(argv[0]))) return NONE_VAL(); /* ImportError already raised */ return krk_pop(); -}) +} -KRK_FUNC(modules,{ +KRK_Function(modules) { FUNCTION_TAKES_NONE(); KrkValue moduleList = krk_list_of(0,NULL,0); krk_push(moduleList); @@ -1376,20 +1376,21 @@ KRK_FUNC(modules,{ krk_writeValueArray(AS_LIST(moduleList), entry->key); } return krk_pop(); -}) +} -KRK_FUNC(unload,{ +KRK_Function(unload) { FUNCTION_TAKES_EXACTLY(1); if (!IS_STRING(argv[0])) return TYPE_ERROR(str,argv[0]); if (!krk_tableDelete(&vm.modules, argv[0])) { return krk_runtimeError(vm.exceptions->keyError, "Module is not loaded."); } -}) + return NONE_VAL(); +} -KRK_FUNC(inspect_value,{ +KRK_Function(inspect_value) { FUNCTION_TAKES_EXACTLY(1); return OBJECT_VAL(krk_newBytes(sizeof(KrkValue),(uint8_t*)&argv[0])); -}) +} void krk_setMaximumRecursionDepth(size_t maxDepth) { vm.maximumCallDepth = maxDepth;