Raise KeyboardInterrupt on ^C

This commit is contained in:
K. Lange 2021-01-08 16:40:43 +09:00
parent 3f66b85757
commit 3735b9c16b
6 changed files with 36 additions and 1 deletions

View File

@ -31,5 +31,5 @@ tags: $(wildcard *.c) $(wildcard *.h)
.PHONY: test .PHONY: test
test: test:
@for i in test/*.krk; do echo $$i; ./kuroko $$i > $$i.expect; done @for i in test/*.krk; do echo $$i; KUROKO_TEST_ENV=1 ./kuroko $$i > $$i.expect; done
@git diff test/*.expect @git diff test/*.expect

View File

@ -9,6 +9,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#ifdef __toaru__ #ifdef __toaru__
#include <toaru/rline.h> #include <toaru/rline.h>
@ -213,6 +214,14 @@ static void tab_complete_func(rline_context_t * c) {
} }
#endif #endif
static void handleSigint(int sigNum) {
if (vm.frameCount) {
krk_runtimeError(vm.exceptions.keyboardInterrupt, "Keyboard interrupt.");
}
signal(sigNum, handleSigint);
}
/* Runs the interpreter to get the version information. */ /* Runs the interpreter to get the version information. */
static int version(void) { static int version(void) {
krk_initVM(0); krk_initVM(0);
@ -296,6 +305,9 @@ int main(int argc, char * argv[]) {
krk_initVM(flags); krk_initVM(flags);
/* Bind interrupt signal */
signal(SIGINT, handleSigint);
#ifdef STATIC_ONLY #ifdef STATIC_ONLY
/* Add any other modules you want to include that are normally built as shared objects. */ /* Add any other modules you want to include that are normally built as shared objects. */
STATIC_MODULE(fileio); STATIC_MODULE(fileio);

View File

@ -0,0 +1,20 @@
import time
import os
if 'KUROKO_TEST_ENV' in os.environ:
print('(This test is skipped by the test suite.)')
return 0
print("This while loop should handle a ^C.")
try:
while True:
print("Tick.")
time.sleep(1)
except:
print("Caught an exception:", exception.__class__.__name__)
print("This loop will not...")
while True:
print("Tick.")
time.sleep(1)

View File

@ -0,0 +1 @@
(This test is skipped by the test suite.)

1
vm.c
View File

@ -2622,6 +2622,7 @@ void krk_initVM(int flags) {
ADD_EXCEPTION_CLASS(vm.exceptions.importError, "ImportError", vm.exceptions.baseException); ADD_EXCEPTION_CLASS(vm.exceptions.importError, "ImportError", vm.exceptions.baseException);
ADD_EXCEPTION_CLASS(vm.exceptions.ioError, "IOError", vm.exceptions.baseException); ADD_EXCEPTION_CLASS(vm.exceptions.ioError, "IOError", vm.exceptions.baseException);
ADD_EXCEPTION_CLASS(vm.exceptions.valueError, "ValueError", vm.exceptions.baseException); ADD_EXCEPTION_CLASS(vm.exceptions.valueError, "ValueError", vm.exceptions.baseException);
ADD_EXCEPTION_CLASS(vm.exceptions.keyboardInterrupt, "KeyboardInterrupt", vm.exceptions.baseException);
/* Build classes for basic types */ /* Build classes for basic types */
ADD_BASE_CLASS(vm.baseClasses.typeClass, "type", vm.objectClass); ADD_BASE_CLASS(vm.baseClasses.typeClass, "type", vm.objectClass);

1
vm.h
View File

@ -78,6 +78,7 @@ typedef struct {
KrkClass * importError; KrkClass * importError;
KrkClass * ioError; KrkClass * ioError;
KrkClass * valueError; KrkClass * valueError;
KrkClass * keyboardInterrupt;
} exceptions; } exceptions;
struct { struct {