Skip to content

Commit

Permalink
Update Ring VM - Better support for nested calls to ring_vm_runcode()…
Browse files Browse the repository at this point in the history
… (Nested events in GUI Applications)
  • Loading branch information
MahmoudFayed committed Feb 4, 2017
1 parent bfe4476 commit a827b31
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/ring_vm.h
Expand Up @@ -86,6 +86,7 @@ typedef struct VM {
char nCallClassInit ;
char nRetEvalDontDelete ;
char *cPrevFileName ;
char nRunCode ;
} VM ;
/*
** Functions
Expand Down
29 changes: 27 additions & 2 deletions src/ring_vm.c
Expand Up @@ -182,6 +182,8 @@ VM * ring_vm_new ( RingState *pRingState )
** In this case we set this flag to avoid the delete operation and solve the problem
*/
pVM->nRetEvalDontDelete = 0 ;
/* Counter to know if we are inside ring_vm_runcode() */
pVM->nRunCode = 0 ;
return pVM ;
}

Expand Down Expand Up @@ -858,24 +860,47 @@ void ring_vm_newbytecodeitem ( VM *pVM,int x )

RING_API void ring_vm_runcode ( VM *pVM,const char *cStr )
{
int nEvalReturnPC,nEvalReallocationFlag,nPC,nRunVM ;
int nEvalReturnPC,nEvalReallocationFlag,nPC,nRunVM,nSP,nFuncSP ;
List *pStackList ;
/* Save state to take in mind nested events execution */
pVM->nRunCode++ ;
nEvalReturnPC = pVM->nEvalReturnPC ;
nEvalReallocationFlag = pVM->nEvalReallocationFlag ;
nPC = pVM->nPC ;
nSP = pVM->nSP ;
nFuncSP = pVM->nFuncSP ;
pStackList = ring_vm_savestack(pVM);
ring_vm_mutexlock(pVM);
pVM->nEvalCalledFromRingCode = 1 ;
pVM->nRetEvalDontDelete = 0 ;
/* Check removing the new byte code */
if ( pVM->nRunCode == 1 ) {
pVM->nRetEvalDontDelete = 0 ;
}
else {
/* We have nested events that call this function */
pVM->nRetEvalDontDelete = 1 ;
}
nRunVM = ring_vm_eval(pVM,cStr);
pVM->nEvalCalledFromRingCode = 0 ;
ring_vm_mutexunlock(pVM);
if ( nRunVM ) {
ring_vm_mainloop(pVM);
}
/* Restore state to take in mind nested events execution */
pVM->nRunCode-- ;
pVM->nEvalReturnPC = nEvalReturnPC ;
pVM->nEvalReallocationFlag = nEvalReallocationFlag ;
pVM->nPC = nPC ;
if ( pVM->nRunCode != 0 ) {
/* It's a nested event (Here we don't care about the output and we can restore the stack) */
ring_vm_restorestack(pVM,pStackList);
pVM->nSP = nSP ;
pVM->nFuncSP = nFuncSP ;
}
else {
/* Here we free the list because, restorestack() that free it (is not called) */
ring_list_delete(pStackList);
}
}

void ring_vm_init ( RingState *pRingState )
Expand Down
14 changes: 7 additions & 7 deletions tests/output/current_output.txt
@@ -1,8 +1,8 @@

B:\ring\tests>call ring scripts/s28.ring
1
114
113
0
115
115

B:\ring\tests>call ring scripts/s60.ring
hello
Expand Down Expand Up @@ -693,7 +693,7 @@ country: egypt
phone: 1234567879

B:\ring\tests>call ring scripts/oop12.ring
Time 4632
Time 4729

B:\ring\tests>call ring scripts/oop13.ring
Welcome
Expand Down Expand Up @@ -2348,7 +2348,7 @@ Can't open file scripts/bracexpreval.ring
B:\ring\tests>call ring scripts/english.ring 0<scripts/testenglish.txt
Enter Number (1) :Enter Number (2) :The Sum : 7
B:\ring\tests>call ring scripts/objectpointer.ring
0084B8B0
006CDB68
OBJECTPOINTER
0
1
Expand Down Expand Up @@ -2590,8 +2590,8 @@ nindex: 1.000000
nindex: 2.000000

B:\ring\tests>call ring scripts/dynamicattr2.ring
date: 27/01/2017
time: 04:16:26
date: 04/02/2017
time: 07:34:32

B:\ring\tests>call ring scripts/sortobjscase.ring
SPY SP500 0
Expand Down
8 changes: 6 additions & 2 deletions visualsrc/ring_vm.c.isense
Expand Up @@ -157,7 +157,9 @@ nCallClassInit
Type: char
nRetEvalDontDelete
Type: char *
cCallerFileName
cPrevFileName
Type: char
nRunCode
END:
Type: VM
struct VM
Expand Down Expand Up @@ -379,7 +381,9 @@ Type: Item *
pItem
ring_vm_runcode
Type: int
nEvalReturnPC,nEvalReallocationFlag,nPC,nRunVM
nEvalReturnPC,nEvalReallocationFlag,nPC,nRunVM,nSP,nFuncSP
Type: List *
pStackList
ring_vm_init
Type: Scanner *
pScanner
Expand Down
Binary file modified visualsrc/ring_vm.fpt
Binary file not shown.
Binary file modified visualsrc/ring_vm.ssf
Binary file not shown.

0 comments on commit a827b31

Please sign in to comment.