@@ -107,8 +107,8 @@ Espruino.Core.Serial.write("\x10\x01\x80\x0Bhello world")
107107#define ASCII_SOH (1)
108108
109109JsVar * events = 0 ; // Array of events to execute
110- JsVarRef timerArray = 0 ; // Linked List of timers to check and run
111- JsVarRef watchArray = 0 ; // Linked List of input watches to check and run
110+ JsVar * timerArray = 0 ; // Linked List of timers to check and run
111+ JsVar * watchArray = 0 ; // Linked List of input watches to check and run
112112// ----------------------------------------------------------------------------
113113IOEventFlags consoleDevice = DEFAULT_CONSOLE_DEVICE ; ///< The console device for user interaction
114114#ifndef SAVE_ON_FLASH
@@ -488,14 +488,6 @@ void jsiSetSleep(JsiSleepType isSleep) {
488488#endif
489489}
490490
491- static JsVarRef _jsiInitNamedArray (const char * name ) {
492- JsVar * array = jsvObjectGetChild (execInfo .hiddenRoot , name , JSV_ARRAY );
493- JsVarRef arrayRef = 0 ;
494- if (array ) arrayRef = jsvGetRef (jsvRef (array ));
495- jsvUnLock (array );
496- return arrayRef ;
497- }
498-
499491// Used when recovering after being flashed
500492// 'claim' anything we are using
501493void jsiSoftInit (bool hasBeenReset ) {
@@ -514,8 +506,8 @@ void jsiSoftInit(bool hasBeenReset) {
514506#endif
515507
516508 // Load timer/watch arrays
517- timerArray = _jsiInitNamedArray ( JSI_TIMERS_NAME );
518- watchArray = _jsiInitNamedArray ( JSI_WATCHES_NAME );
509+ timerArray = jsvObjectGetChild ( execInfo . hiddenRoot , JSI_TIMERS_NAME , JSV_ARRAY );
510+ watchArray = jsvObjectGetChild ( execInfo . hiddenRoot , JSI_WATCHES_NAME , JSV_ARRAY );
519511
520512 // Make sure we set up lastIdleTime, as this could be used
521513 // when adding an interval from onInit (called below)
@@ -548,9 +540,8 @@ void jsiSoftInit(bool hasBeenReset) {
548540
549541 // Check any existing watches and set up interrupts for them
550542 if (watchArray ) {
551- JsVar * watchArrayPtr = jsvLock (watchArray );
552543 JsvObjectIterator it ;
553- jsvObjectIteratorNew (& it , watchArrayPtr );
544+ jsvObjectIteratorNew (& it , watchArray );
554545 while (jsvObjectIteratorHasValue (& it )) {
555546 JsVar * watch = jsvObjectIteratorGetValue (& it );
556547 JsVar * watchPin = jsvObjectGetChildIfExists (watch , "pin" );
@@ -560,7 +551,6 @@ void jsiSoftInit(bool hasBeenReset) {
560551 jsvObjectIteratorNext (& it );
561552 }
562553 jsvObjectIteratorFree (& it );
563- jsvUnLock (watchArrayPtr );
564554 }
565555
566556 // Timers are stored by time in the future now, so no need
@@ -807,14 +797,13 @@ void jsiSoftKill() {
807797 events = 0 ;
808798 }
809799 if (timerArray ) {
810- jsvUnRefRef (timerArray );
800+ jsvUnLock (timerArray );
811801 timerArray = 0 ;
812802 }
813803 if (watchArray ) {
814804 // Check any existing watches and disable interrupts for them
815- JsVar * watchArrayPtr = jsvLock (watchArray );
816805 JsvObjectIterator it ;
817- jsvObjectIteratorNew (& it , watchArrayPtr );
806+ jsvObjectIteratorNew (& it , watchArray );
818807 while (jsvObjectIteratorHasValue (& it )) {
819808 JsVar * watchPtr = jsvObjectIteratorGetValue (& it );
820809 JsVar * watchPin = jsvObjectGetChildIfExists (watchPtr , "pin" );
@@ -823,8 +812,7 @@ void jsiSoftKill() {
823812 jsvObjectIteratorNext (& it );
824813 }
825814 jsvObjectIteratorFree (& it );
826- jsvUnRef (watchArrayPtr );
827- jsvUnLock (watchArrayPtr );
815+ jsvUnLock (watchArray );
828816 watchArray = 0 ;
829817 }
830818 // Save flags if required
@@ -2136,11 +2124,7 @@ void jsiClearTimeout(JsVar *timeout) {
21362124}
21372125
21382126bool jsiHasTimers () {
2139- if (!timerArray ) return false;
2140- JsVar * timerArrayPtr = jsvLock (timerArray );
2141- bool hasTimers = !jsvArrayIsEmpty (timerArrayPtr );
2142- jsvUnLock (timerArrayPtr );
2143- return hasTimers ;
2127+ return !jsvArrayIsEmpty (timerArray );
21442128}
21452129
21462130/// Is the given watch object meant to be executed when the current value of the pin is pinIsHigh
@@ -2155,9 +2139,8 @@ bool jsiIsWatchingPin(Pin pin) {
21552139 if (jshGetPinShouldStayWatched (pin ))
21562140 return true;
21572141 bool isWatched = false;
2158- JsVar * watchArrayPtr = jsvLock (watchArray );
21592142 JsvObjectIterator it ;
2160- jsvObjectIteratorNew (& it , watchArrayPtr );
2143+ jsvObjectIteratorNew (& it , watchArray );
21612144 while (jsvObjectIteratorHasValue (& it )) {
21622145 JsVar * watchPtr = jsvObjectIteratorGetValue (& it );
21632146 JsVar * pinVar = jsvObjectGetChildIfExists (watchPtr , "pin" );
@@ -2167,7 +2150,6 @@ bool jsiIsWatchingPin(Pin pin) {
21672150 jsvObjectIteratorNext (& it );
21682151 }
21692152 jsvObjectIteratorFree (& it );
2170- jsvUnLock (watchArrayPtr );
21712153 return isWatched ;
21722154}
21732155
@@ -2283,9 +2265,8 @@ void jsiIdle() {
22832265 } else if (DEVICE_IS_EXTI (eventType )) { // ---------------------------------------------------------------- PIN WATCH
22842266 // we have an event... find out what it was for...
22852267 // Check everything in our Watch array
2286- JsVar * watchArrayPtr = jsvLock (watchArray );
22872268 JsvObjectIterator it ;
2288- jsvObjectIteratorNew (& it , watchArrayPtr );
2269+ jsvObjectIteratorNew (& it , watchArray );
22892270 while (jsvObjectIteratorHasValue (& it )) {
22902271 bool hasDeletedWatch = false;
22912272 JsVar * watchPtr = jsvObjectIteratorGetValue (& it );
@@ -2385,7 +2366,7 @@ void jsiIdle() {
23852366 jsvUnLock (data );
23862367 if (!watchRecurring ) {
23872368 // free all
2388- jsvObjectIteratorRemoveAndGotoNext (& it , watchArrayPtr );
2369+ jsvObjectIteratorRemoveAndGotoNext (& it , watchArray );
23892370 hasDeletedWatch = true;
23902371 if (!jsiIsWatchingPin (pin ))
23912372 jshPinWatch (pin , false, JSPW_NONE );
@@ -2401,7 +2382,6 @@ void jsiIdle() {
24012382 jsvObjectIteratorNext (& it );
24022383 }
24032384 jsvObjectIteratorFree (& it );
2404- jsvUnLock (watchArrayPtr );
24052385 }
24062386 }
24072387
@@ -2423,10 +2403,9 @@ void jsiIdle() {
24232403 jsiTimeSinceCtrlC = 0xFFFFFFFF ;
24242404#endif
24252405
2426- JsVar * timerArrayPtr = jsvLock (timerArray );
24272406 JsvObjectIterator it ;
24282407 // Go through all intervals and decrement time
2429- jsvObjectIteratorNew (& it , timerArrayPtr );
2408+ jsvObjectIteratorNew (& it , timerArray );
24302409 while (jsvObjectIteratorHasValue (& it )) {
24312410 JsVar * timerPtr = jsvObjectIteratorGetValue (& it );
24322411 JsSysTime timerTime = (JsSysTime )jsvGetLongIntegerAndUnLock (jsvObjectGetChildIfExists (timerPtr , "time" ));
@@ -2439,7 +2418,7 @@ void jsiIdle() {
24392418 // Now go through intervals and execute if needed
24402419 do {
24412420 jsiStatus = jsiStatus & ~JSIS_TIMERS_CHANGED ;
2442- jsvObjectIteratorNew (& it , timerArrayPtr );
2421+ jsvObjectIteratorNew (& it , timerArray );
24432422 while (jsvObjectIteratorHasValue (& it ) && !(jsiStatus & JSIS_TIMERS_CHANGED )) {
24442423 bool hasDeletedTimer = false;
24452424 JsVar * timerPtr = jsvObjectIteratorGetValue (& it );
@@ -2507,12 +2486,10 @@ void jsiIdle() {
25072486 if (exec ) {
25082487 bool watchRecurring = jsvObjectGetBoolChild (watchPtr , "recur" );
25092488 if (!watchRecurring ) {
2510- JsVar * watchArrayPtr = jsvLock (watchArray );
2511- JsVar * watchNamePtr = jsvGetIndexOf (watchArrayPtr , watchPtr , true);
2489+ JsVar * watchNamePtr = jsvGetIndexOf (watchArray , watchPtr , true);
25122490 if (watchNamePtr ) {
2513- jsvRemoveChildAndUnLock (watchArrayPtr , watchNamePtr );
2491+ jsvRemoveChildAndUnLock (watchArray , watchNamePtr );
25142492 }
2515- jsvUnLock (watchArrayPtr );
25162493 Pin pin = jshGetPinFromVarAndUnLock (jsvObjectGetChildIfExists (watchPtr , "pin" ));
25172494 if (!jsiIsWatchingPin (pin ))
25182495 jshPinWatch (pin , false, JSPW_NONE );
@@ -2528,7 +2505,7 @@ void jsiIdle() {
25282505 } else {
25292506 // free
25302507 // Beware... may have already been removed!
2531- jsvObjectIteratorRemoveAndGotoNext (& it , timerArrayPtr );
2508+ jsvObjectIteratorRemoveAndGotoNext (& it , timerArray );
25322509 hasDeletedTimer = true;
25332510 timerTime = -1 ;
25342511 }
@@ -2544,7 +2521,6 @@ void jsiIdle() {
25442521 }
25452522 jsvObjectIteratorFree (& it );
25462523 } while (jsiStatus & JSIS_TIMERS_CHANGED );
2547- jsvUnLock (timerArrayPtr );
25482524 /* We might have left the timers loop with stuff to do because the contents of it
25492525 * changed. It's not a big deal because it could only have changed because a timer
25502526 * got executed - so `wasBusy` got set and we know we're going to go around the
@@ -2755,9 +2731,7 @@ void jsiDumpState(vcbprintf_callback user_callback, void *user_data) {
27552731 }
27562732 jsvObjectIteratorFree (& it );
27572733 // Now do timers
2758- JsVar * timerArrayPtr = jsvLock (timerArray );
2759- jsvObjectIteratorNew (& it , timerArrayPtr );
2760- jsvUnLock (timerArrayPtr );
2734+ jsvObjectIteratorNew (& it , timerArray );
27612735 while (jsvObjectIteratorHasValue (& it )) {
27622736 JsVar * timer = jsvObjectIteratorGetValue (& it );
27632737 JsVar * timerNumber = jsvObjectIteratorGetKey (& it );
@@ -2773,9 +2747,7 @@ void jsiDumpState(vcbprintf_callback user_callback, void *user_data) {
27732747 }
27742748 jsvObjectIteratorFree (& it );
27752749 // Now do watches
2776- JsVar * watchArrayPtr = jsvLock (watchArray );
2777- jsvObjectIteratorNew (& it , watchArrayPtr );
2778- jsvUnLock (watchArrayPtr );
2750+ jsvObjectIteratorNew (& it , watchArray );
27792751 while (jsvObjectIteratorHasValue (& it )) {
27802752 JsVar * watch = jsvObjectIteratorGetValue (& it );
27812753 JsVar * watchCallback = jsvSkipOneNameAndUnLock (jsvFindChildFromString (watch , "cb" ));
@@ -2811,10 +2783,7 @@ void jsiDumpState(vcbprintf_callback user_callback, void *user_data) {
28112783}
28122784
28132785JsVarInt jsiTimerAdd (JsVar * timerPtr ) {
2814- JsVar * timerArrayPtr = jsvLock (timerArray );
2815- JsVarInt itemIndex = jsvArrayAddToEnd (timerArrayPtr , timerPtr , 1 ) - 1 ;
2816- jsvUnLock (timerArrayPtr );
2817- return itemIndex ;
2786+ return jsvArrayAddToEnd (timerArray , timerPtr , 1 ) - 1 ;
28182787}
28192788
28202789void jsiTimersChanged () {
0 commit comments