Skip to content

Commit 1cfc21f

Browse files
tohlhRichDom2185
andauthored
Add stop program functionality (#6)
* Add stop program functionality * Using volatile for running field --------- Co-authored-by: Richard Dominick <[email protected]>
1 parent 656abf0 commit 1cfc21f

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

vm/include/sinter.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ typedef enum {
3333
sinter_fault_internal_error = 9,
3434
sinter_fault_function_arity = 10,
3535
sinter_fault_program_error = 11,
36-
sinter_fault_uninitialised_heap = 12
36+
sinter_fault_uninitialised_heap = 12,
37+
sinter_fault_stopped = 13
3738
} sinter_fault_t;
3839

3940
typedef struct {
@@ -61,6 +62,14 @@ sinter_fault_t sinter_run(const unsigned char *code, const size_t code_size, sin
6162
*/
6263
void sinter_setup_heap(void *heap, size_t size);
6364

65+
/**
66+
* Stops the currently running program.
67+
*
68+
* This function stops the currently running program.
69+
*/
70+
71+
void sinter_stop(void);
72+
6473
/**
6574
* The type of a string printer function.
6675
*

vm/include/sinter/vm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern "C" {
1919
#endif
2020

2121
struct sistate {
22-
bool running;
22+
volatile bool running;
2323
sinter_fault_t fault_reason;
2424
const opcode_t *pc;
2525
const opcode_t *program;
@@ -84,6 +84,8 @@ SINTER_INLINEIFC __attribute__((warn_unused_result)) sinanbox_t siexec_nanbox(si
8484

8585
bool sivm_equal(sinanbox_t l, sinanbox_t r);
8686

87+
void sistop(void);
88+
8789
#define SISTATE_CURADDR (sistate.pc - sistate.program)
8890
#define SISTATE_ADDRTOPC(addr) (sistate.program + (addr))
8991

vm/src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ return;
133133
siheap_size = size;
134134
#endif
135135
}
136+
137+
void sinter_stop(void) {
138+
sistop();
139+
}

vm/src/vm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ static void main_loop(void) {
159159
(void) previous_pc;
160160
#endif
161161
while (1) {
162+
if (!sistate.running) {
163+
SIDEBUG("The program has been stopped by the user.\n");
164+
sifault(sinter_fault_stopped);
165+
return;
166+
}
167+
162168
#ifdef SINTER_DEBUG_MEMORY_CHECK
163169
debug_memorycheck();
164170
#endif
@@ -924,3 +930,12 @@ sinanbox_t siexec(const svm_function_t *fn, siheap_env_t *parent_env, uint8_t ar
924930

925931
return ret;
926932
}
933+
934+
void sistop(void) {
935+
sistate.running = false;
936+
sistate.fault_reason = sinter_fault_stopped;
937+
sistate.pc = NULL;
938+
sistate.program = NULL;
939+
sistate.program_end = NULL;
940+
sistate.env = NULL;
941+
}

0 commit comments

Comments
 (0)