Try to make builds smaller

... by:
    - Normalizing some exception strings.
    - Putting the 'dis' module behind -DDEBUG.
This commit is contained in:
K. Lange 2021-03-22 18:09:12 +09:00
parent 6746ccf001
commit a63aef6b92
10 changed files with 73 additions and 55 deletions

View File

@ -13,7 +13,9 @@ FUNC_SIG(list,__init__);
FUNC_SIG(list,sort);
KrkValue krk_dirObject(int argc, KrkValue argv[], int hasKw) {
if (argc != 1) return krk_runtimeError(vm.exceptions->argumentError, "wrong number of arguments or bad type, got %d\n", argc);
if (argc != 1)
return krk_runtimeError(vm.exceptions->argumentError, "%s() takes %s %d argument%s (%d given)",
"dir", "exactly", 1, "", argc);
/* Create a new list instance */
KrkValue myList = krk_list_of(0,NULL,0);
@ -489,12 +491,12 @@ static KrkValue _print(int argc, KrkValue argv[], int hasKw) {
char * end = "\n"; size_t endLen = 1;
if (hasKw) {
if (krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("sep")), &sepVal)) {
if (!IS_STRING(sepVal)) return krk_runtimeError(vm.exceptions->typeError, "'sep' should be a string, not '%s'", krk_typeName(sepVal));
if (!IS_STRING(sepVal)) return krk_runtimeError(vm.exceptions->typeError, "'%s' should be a string, not '%s'", "sep", krk_typeName(sepVal));
sep = AS_CSTRING(sepVal);
sepLen = AS_STRING(sepVal)->length;
}
if (krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("end")), &endVal)) {
if (!IS_STRING(endVal)) return krk_runtimeError(vm.exceptions->typeError, "'end' should be a string, not '%s'", krk_typeName(endVal));
if (!IS_STRING(endVal)) return krk_runtimeError(vm.exceptions->typeError, "'%s' should be a string, not '%s'", "end", krk_typeName(endVal));
end = AS_CSTRING(endVal);
endLen = AS_STRING(endVal)->length;
}
@ -610,7 +612,7 @@ KRK_FUNC(isinstance,{
}
return BOOLEAN_VAL(0);
} else {
return krk_runtimeError(vm.exceptions->typeError, "isinstance() arg 2 must be class or tuple");
return TYPE_ERROR(class or tuple,argv[1]);
}
})
@ -802,7 +804,7 @@ KRK_METHOD(property,__get__,{
KrkValue fget;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("fget")), &fget))
return krk_runtimeError(vm.exceptions->valueError, "property object is missing 'fget' attribute");
return krk_runtimeError(vm.exceptions->attributeError, "'%s' object has no attribute '%s'", "property", "fget");
krk_push(argv[1]);
return krk_callSimple(fget, 1, 0);
@ -827,7 +829,7 @@ KRK_METHOD(property,__set__,{
KRK_FUNC(id,{
FUNCTION_TAKES_EXACTLY(1);
if (!IS_OBJECT(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "'%s' is a primitive type and has no identity", krk_typeName(argv[0]));
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]));
})

View File

@ -68,3 +68,13 @@ size_t krk_writeConstant(KrkChunk * chunk, KrkValue value, size_t line) {
krk_emitConstant(chunk, ind, line);
return ind;
}
size_t krk_lineNumber(KrkChunk * chunk, size_t offset) {
size_t line = 0;
for (size_t i = 0; i < chunk->linesCount; ++i) {
if (chunk->lines[i].startOffset > offset) break;
line = chunk->lines[i].line;
}
return line;
}

View File

@ -6,6 +6,8 @@
#include <kuroko/util.h>
#include <kuroko/compiler.h>
#ifdef DEBUG
/**
* When tracing is enabled, we will present the elements on the stack with
* a safe printer; the format of values printed by krk_printValueSafe will
@ -183,15 +185,6 @@ static int isJumpTarget(KrkCodeObject * func, size_t startPoint) {
} \
}
size_t krk_lineNumber(KrkChunk * chunk, size_t offset) {
size_t line = 0;
for (size_t i = 0; i < chunk->linesCount; ++i) {
if (chunk->lines[i].startOffset > offset) break;
line = chunk->lines[i].line;
}
return line;
}
size_t krk_disassembleInstruction(FILE * f, KrkCodeObject * func, size_t offset) {
KrkChunk * chunk = &func->chunk;
if (offset > 0 && krk_lineNumber(chunk, offset) == krk_lineNumber(chunk, offset - 1)) {
@ -776,3 +769,5 @@ void _createAndBind_disMod(void) {
#undef LOCAL_MORE
#undef EXPAND_ARGS_MORE
}
#endif

View File

@ -372,6 +372,7 @@ _cleanup:
}
#endif
#ifdef DEBUG
static char * lastDebugCommand = NULL;
static int debuggerHook(KrkCallFrame * frame) {
@ -615,6 +616,7 @@ static int debuggerHook(KrkCallFrame * frame) {
_dbgQuit:
return KRK_DEBUGGER_QUIT;
}
#endif
static void handleSigint(int sigNum) {
/* Don't set the signal flag if the VM is not running */
@ -810,7 +812,9 @@ _finishArgs:
findInterpreter(argv);
krk_initVM(flags);
#ifdef DEBUG
krk_debug_registerCallback(debuggerHook);
#endif
/* Attach kuroko.argv - argv[0] will be set to an empty string for the repl */
if (argc == optind) krk_push(OBJECT_VAL(krk_copyString("",0)));

View File

@ -220,3 +220,17 @@ extern void krk_emitConstant(KrkChunk * chunk, size_t ind, size_t line);
* @memberof KrkChunk
*/
extern size_t krk_writeConstant(KrkChunk * chunk, KrkValue value, size_t line);
/**
* @brief Obtain the line number for a byte offset into a bytecode chunk.
* @memberof KrkChunk
*
* Scans the line mapping table for the given chunk to find the
* correct line number from the original source file for the instruction
* at byte index 'offset'.
*
* @param chunk Bytecode chunk containing the instruction.
* @param offset Byte offset of the instruction to locate.
* @return Line number, 1-indexed.
*/
extern size_t krk_lineNumber(KrkChunk * chunk, size_t offset);

View File

@ -20,6 +20,8 @@
#include "chunk.h"
#include "object.h"
#ifdef DEBUG
/**
* @brief Print a disassembly of 'func' to the stream 'f'.
*
@ -46,19 +48,6 @@ extern void krk_disassembleCodeObject(FILE * f, KrkCodeObject * func, const char
*/
extern size_t krk_disassembleInstruction(FILE * f, KrkCodeObject * func, size_t offset);
/**
* @brief Obtain the line number for a byte offset into a bytecode chunk.
*
* Scans the line mapping table for the given chunk to find the
* correct line number from the original source file for the instruction
* at byte index 'offset'.
*
* @param chunk Bytecode chunk containing the instruction.
* @param offset Byte offset of the instruction to locate.
* @return Line number, 1-indexed.
*/
extern size_t krk_lineNumber(KrkChunk * chunk, size_t offset);
/* Internal stuff */
extern void _createAndBind_disMod(void);
@ -246,3 +235,5 @@ extern void krk_debug_dumpStack(FILE * f, KrkCallFrame * frame);
#define KRK_DEBUGGER_STEP 2
#define KRK_DEBUGGER_RAISE 3
#define KRK_DEBUGGER_QUIT 4
#endif

View File

@ -108,7 +108,7 @@ KRK_METHOD(set,__and__,{
KrkClass * type = krk_getType(argv[1]);
KrkValue contains;
if (!krk_tableGet(&type->methods, OBJECT_VAL(S("__contains__")), &contains))
return krk_runtimeError(vm.exceptions->typeError, "unsupported operand type for &");
return krk_runtimeError(vm.exceptions->typeError, "unsupported operand types for %s: '%s' and '%s'", "&", "set", krk_typeName(argv[1]));
size_t len = self->entries.capacity;
for (size_t i = 0; i < len; ++i) {

View File

@ -77,8 +77,10 @@ KRK_METHOD(str,__setitem__,{
*/
KRK_METHOD(str,__getslice__,{
METHOD_TAKES_EXACTLY(2);
if (!(IS_INTEGER(argv[1]) || IS_NONE(argv[1])) || !(IS_INTEGER(argv[2]) || IS_NONE(argv[2])))
return krk_runtimeError(vm.exceptions->typeError, "slice: expected two integer arguments");
if (!(IS_INTEGER(argv[1]) || IS_NONE(argv[1])))
return TYPE_ERROR(int,argv[1]);
if (!(IS_INTEGER(argv[2]) || IS_NONE(argv[2])))
return TYPE_ERROR(int,argv[2]);
/* bounds check */
long start = IS_NONE(argv[1]) ? 0 : AS_INTEGER(argv[1]);
long end = IS_NONE(argv[2]) ? (long)self->codesLength : AS_INTEGER(argv[2]);
@ -345,7 +347,7 @@ KRK_METHOD(str,join,{
return finishStringBuilder(&sb);
_expectedString:
krk_runtimeError(vm.exceptions->typeError, "Expected string, got %s.", errorStr);
krk_runtimeError(vm.exceptions->typeError, "%s() expects %s, not '%s'", "join", "str", errorStr);
discardStringBuilder(&sb);
})

View File

@ -300,9 +300,6 @@ inline void krk_push(KrkValue value) {
inline KrkValue krk_pop() {
krk_currentThread.stackTop--;
if (unlikely(krk_currentThread.stackTop < krk_currentThread.stack)) {
fprintf(stderr, "Fatal error: stack underflow detected in VM (krk_currentThread.stackTop = %p, krk_currentThread.stack = %p)\n",
(void*)krk_currentThread.stackTop,
(void*)krk_currentThread.stack);
abort();
return NONE_VAL();
}
@ -850,7 +847,7 @@ int krk_callValue(KrkValue callee, int argCount, int extra) {
} else if (krk_tableGet(&_class->methods, vm.specialMethodNames[METHOD_CALL], &callFunction)) {
return krk_callValue(callFunction, argCount + 1, 0);
} else {
krk_runtimeError(vm.exceptions->typeError, "Attempted to call non-callable type: %s", krk_typeName(callee));
krk_runtimeError(vm.exceptions->typeError, "'%s' object is not callable", krk_typeName(callee));
return 0;
}
}
@ -864,7 +861,8 @@ int krk_callValue(KrkValue callee, int argCount, int extra) {
} else if (krk_tableGet(&_class->methods, vm.specialMethodNames[METHOD_INIT], &initializer)) {
return krk_callValue(initializer, argCount + 1, 0);
} else if (argCount != 0) {
krk_runtimeError(vm.exceptions->attributeError, "Class does not have an __init__ but arguments were passed to initializer: %d", argCount);
krk_runtimeError(vm.exceptions->typeError, "%s() takes no arguments (%d given)",
_class->name->chars, argCount);
return 0;
}
return 1;
@ -882,7 +880,7 @@ int krk_callValue(KrkValue callee, int argCount, int extra) {
break;
}
}
krk_runtimeError(vm.exceptions->typeError, "Attempted to call non-callable type: %s", krk_typeName(callee));
krk_runtimeError(vm.exceptions->typeError, "'%s' object is not callable", krk_typeName(callee));
return 0;
}
@ -1221,12 +1219,14 @@ void krk_initVM(int flags) {
_createAndBind_rangeClass();
_createAndBind_setClass();
_createAndBind_exceptions();
_createAndBind_generatorClass();
_createAndBind_gcMod();
_createAndBind_disMod();
_createAndBind_timeMod();
_createAndBind_osMod();
_createAndBind_fileioMod();
_createAndBind_generatorClass();
#ifdef DEBUG
_createAndBind_disMod();
#endif
#ifdef ENABLE_THREADING
_createAndBind_threadsMod();
#endif
@ -1462,9 +1462,7 @@ static int handleException() {
/* Find the call frame that owns this stack slot */
for (frameOffset = krk_currentThread.frameCount - 1; frameOffset >= 0 && (int)krk_currentThread.frames[frameOffset].slots > stackOffset; frameOffset--);
if (frameOffset == -1) {
fprintf(stderr, "Internal error: Call stack is corrupted - unable to find\n");
fprintf(stderr, " call frame that owns exception handler.\n");
exit(1);
abort();
}
/* We found an exception handler and can reset the VM to its call frame. */
@ -1498,8 +1496,8 @@ int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs) {
/* Obtain __builtins__.module_paths */
if (!krk_tableGet(&vm.system->fields, OBJECT_VAL(S("module_paths")), &modulePaths) || !IS_INSTANCE(modulePaths)) {
*moduleOut = NONE_VAL();
krk_runtimeError(vm.exceptions->baseException,
"Internal error: kuroko.module_paths not defined.");
krk_runtimeError(vm.exceptions->importError,
"kuroko.module_paths not defined.");
return 0;
}
@ -2078,14 +2076,14 @@ _finishReturn: (void)0;
case OP_BITNEGATE: {
KrkValue value = krk_pop();
if (IS_INTEGER(value)) krk_push(INTEGER_VAL(~AS_INTEGER(value)));
else { krk_runtimeError(vm.exceptions->typeError, "Incompatible operand type for bit negation."); goto _finishException; }
else { krk_runtimeError(vm.exceptions->typeError, "Incompatible operand type for %s negation.", "bit"); goto _finishException; }
break;
}
case OP_NEGATE: {
KrkValue value = krk_pop();
if (IS_INTEGER(value)) krk_push(INTEGER_VAL(-AS_INTEGER(value)));
else if (IS_FLOATING(value)) krk_push(FLOATING_VAL(-AS_FLOATING(value)));
else { krk_runtimeError(vm.exceptions->typeError, "Incompatible operand type for prefix negation."); goto _finishException; }
else { krk_runtimeError(vm.exceptions->typeError, "Incompatible operand type for %s negation.", "prefix"); goto _finishException; }
break;
}
case OP_NONE: krk_push(NONE_VAL()); break;
@ -2268,12 +2266,14 @@ _finishReturn: (void)0;
}
break;
}
#ifdef DEBUG
case OP_BREAKPOINT: {
/* First off, halt execution. */
krk_debugBreakpointHandler();
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) goto _finishException;
goto _resumeHook;
}
#endif
case OP_YIELD: {
KrkValue result = krk_peek(0);
krk_currentThread.frameCount--;
@ -2287,7 +2287,6 @@ _finishReturn: (void)0;
AS_CLOSURE(krk_peek(1))->annotations = krk_pop();
} else if (IS_NONE(krk_peek(0))) {
krk_swap(1);
fprintf(stderr, "TODO: Global annotation.\n");
krk_pop();
} else {
krk_runtimeError(vm.exceptions->typeError, "Can not annotate '%s'.", krk_typeName(krk_peek(0)));
@ -2332,7 +2331,8 @@ _finishReturn: (void)0;
KrkValue contextManager = krk_peek(0);
KrkClass * type = krk_getType(contextManager);
if (unlikely(!type->_enter || !type->_exit)) {
krk_runtimeError(vm.exceptions->attributeError, "Can not use '%s' as context manager", krk_typeName(contextManager));
if (!type->_enter) krk_runtimeError(vm.exceptions->attributeError, "__enter__");
else if (!type->_exit) krk_runtimeError(vm.exceptions->attributeError, "__exit__");
goto _finishException;
}
krk_push(contextManager);
@ -2562,7 +2562,7 @@ _finishReturn: (void)0;
KrkString * name = READ_STRING(OPERAND);
KrkClass * superclass = AS_CLASS(krk_pop());
if (!krk_bindMethod(superclass, name)) {
krk_runtimeError(vm.exceptions->attributeError, "super(%s) has no attribute '%s'",
krk_runtimeError(vm.exceptions->attributeError, "'%s' object has no attribute '%s'",
superclass->name->chars, name->chars);
goto _finishException;
}
@ -2672,7 +2672,7 @@ _finishReturn: (void)0;
} else {
KrkClass * type = krk_getType(sequence);
if (!type->_iter) {
krk_runtimeError(vm.exceptions->typeError, "Can not unpack non-iterable '%s'", krk_typeName(sequence));
krk_runtimeError(vm.exceptions->typeError, "'%s' object is not iterable", krk_typeName(sequence));
goto _finishException;
} else {
size_t stackStart = krk_currentThread.stackTop - krk_currentThread.stack - 1;
@ -2786,7 +2786,7 @@ KrkValue krk_interpret(const char * src, char * fromFile) {
KrkValue krk_runfile(const char * fileName, char * fromFile) {
FILE * f = fopen(fileName,"r");
if (!f) {
fprintf(stderr, "kuroko: could not read file '%s': %s\n", fileName, strerror(errno));
fprintf(stderr, "%s: could not read file '%s': %s\n", "kuroko", fileName, strerror(errno));
return INTEGER_VAL(errno);
}
@ -2796,7 +2796,7 @@ KrkValue krk_runfile(const char * fileName, char * fromFile) {
char * buf = malloc(size+1);
if (fread(buf, 1, size, f) != size) {
fprintf(stderr, "Warning: Failed to read file.\n");
fprintf(stderr, "%s: could not read file '%s': %s\n", "kuroko", fileName, strerror(errno));
}
fclose(f);
buf[size] = '\0';

View File

@ -1,2 +1,2 @@
super(object) has no attribute '__init__'
super(object) has no attribute '__init__'
'object' object has no attribute '__init__'
'object' object has no attribute '__init__'