@@ -108,9 +108,13 @@ void interpreterHandler(void *pvParameters) {
108108 duk_context *ctx =
109109 duk_create_heap (alloc_function, realloc_function, free_function, NULL , js_fatal_error_handler);
110110
111+ duk_push_pointer (ctx, interpreterJS);
112+ duk_put_global_string (ctx, DUK_HIDDEN_SYMBOL (" INTERPRETER_POINTER" ));
113+
111114 // Add native functions to context.
112115 bduk_register_c_lightfunc (ctx, " now" , native_now, 0 );
113116 bduk_register_c_lightfunc (ctx, " delay" , native_delay, 1 );
117+ bduk_register_c_lightfunc (ctx, " sleep" , native_sleep, 1 );
114118 bduk_register_c_lightfunc (ctx, " parse_int" , native_parse_int, 1 );
115119 bduk_register_c_lightfunc (ctx, " to_string" , native_to_string, 1 );
116120 bduk_register_c_lightfunc (ctx, " to_hex_string" , native_to_hex_string, 1 );
@@ -359,26 +363,32 @@ InterpreterJS::InterpreterJS(char *script, const char *scriptName, const char *s
359363
360364InterpreterJS::~InterpreterJS () { terminate (); }
361365
362- void InterpreterJS::terminate () {
366+ void InterpreterJS::toForeground () {}
367+
368+ void InterpreterJS::toBackground () {}
363369
364- if (isRunning == false ) {
370+ void InterpreterJS::terminate (bool waitForTermination) {
371+ if (_isExecuting && waitForTermination) {
372+ while (_isExecuting == true ) { delay (100 ); }
373+ }
374+
375+ if (_isExecuting == false ) {
365376 vTaskDelete (taskHandle);
366377 taskHandle = nullptr ;
367- isRunning = false ;
368378
369379 taskManager.unregisterTask (taskId);
370- free (( char *) script);
380+ free (script);
371381 script = NULL ;
372382
373383 if (ctx) {
374384 duk_destroy_heap (ctx);
375385 ctx = nullptr ;
376386 }
387+ } else {
388+ shouldTerminate = true ;
377389 }
378390}
379391
380- uint8_t InterpreterJS::getState () { return 1 ; }
381-
382392// function to start the JS Interpreterm choosinng the file, processing and
383393// start
384394void run_bjs_script () {
@@ -397,13 +407,14 @@ void run_bjs_script() {
397407 if (script == NULL ) { return ; }
398408
399409 new InterpreterJS (script, filename.c_str ());
400-
401- returnToMenu = true ;
402410}
403411
404412bool run_bjs_script_headless (char *code) {
413+ Task *task = taskManager.getTaskByName (" eval" );
414+ if (task != nullptr ) { task->terminate (true ); }
415+
405416 new InterpreterJS (code, " eval" );
406- returnToMenu = true ;
417+
407418 return true ;
408419}
409420
0 commit comments