Skip to content

Commit 21f00bc

Browse files
committed
Fix task manager
1 parent ca1a426 commit 21f00bc

File tree

6 files changed

+78
-25
lines changed

6 files changed

+78
-25
lines changed

src/modules/bjs_interpreter/functions_js.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "display_js.h"
1010
#include "gui_js.h"
1111
#include "helpers_js.h"
12+
#include "interpreter.h"
1213
#include "wifi_js.h"
1314

1415
duk_ret_t native_noop(duk_context *ctx) { return 0; }
@@ -33,10 +34,42 @@ duk_ret_t native_now(duk_context *ctx) {
3334
}
3435

3536
duk_ret_t native_delay(duk_context *ctx) {
37+
duk_push_global_object(ctx);
38+
duk_push_string(ctx, DUK_HIDDEN_SYMBOL("INTERPRETER_POINTER"));
39+
duk_get_prop(ctx, -2);
40+
InterpreterJS *interpreterJS = (InterpreterJS *)duk_get_pointer(ctx, -1);
41+
interpreterJS->_isExecuting = false;
42+
43+
if (interpreterJS->shouldTerminate == false) {
44+
interpreterJS->terminate(true);
45+
return 0;
46+
}
47+
3648
delay(duk_to_int(ctx, 0));
3749
return 0;
3850
}
3951

52+
duk_ret_t native_sleep(duk_context *ctx) {
53+
duk_push_global_object(ctx);
54+
duk_push_string(ctx, DUK_HIDDEN_SYMBOL("INTERPRETER_POINTER"));
55+
duk_get_prop(ctx, -2);
56+
InterpreterJS *interpreterJS = (InterpreterJS *)duk_get_pointer(ctx, -1);
57+
interpreterJS->_isExecuting = false;
58+
59+
if (interpreterJS->shouldTerminate == false) {
60+
interpreterJS->terminate(true);
61+
return 0;
62+
}
63+
64+
duk_int_t delayMs = duk_to_int(ctx, 0);
65+
66+
for (int i = 0; i < delayMs; i += 10) {
67+
delay(10);
68+
if (interpreterJS->isForeground) break;
69+
}
70+
return 0;
71+
}
72+
4073
duk_ret_t native_assert(duk_context *ctx) {
4174
if (duk_get_boolean_default(ctx, 0, false)) {
4275
duk_push_boolean(ctx, true);

src/modules/bjs_interpreter/functions_js.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ duk_ret_t native_serialPrintln(duk_context *ctx);
1111

1212
duk_ret_t native_now(duk_context *ctx);
1313
duk_ret_t native_delay(duk_context *ctx);
14+
duk_ret_t native_sleep(duk_context *ctx);
1415
duk_ret_t native_assert(duk_context *ctx);
1516
duk_ret_t native_random(duk_context *ctx);
1617

src/modules/bjs_interpreter/interpreter.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

360364
InterpreterJS::~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
384394
void 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

404412
bool 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

src/modules/bjs_interpreter/interpreter.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ class InterpreterJS : Task {
1717
const char *getName() { return scriptName.c_str(); };
1818
const char *getScriptDirpath() { return scriptDirpath.c_str(); };
1919
const char *getScript() { return script; };
20-
// void start();
21-
void terminate();
22-
uint8_t getState();
23-
// duk_context *getContext();
20+
21+
void toForeground();
22+
void toBackground();
23+
24+
void terminate(bool waitForTermination = false);
25+
26+
bool _isExecuting = false;
27+
bool shouldTerminate = false;
28+
bool isForeground = true;
2429

2530
private:
2631
int taskId;
2732
char *script;
28-
String scriptDirpath;
2933
String scriptName;
30-
bool isRunning;
31-
bool shouldTerminate;
34+
String scriptDirpath;
35+
3236
TaskHandle_t taskHandle = nullptr;
3337
duk_context *ctx = nullptr;
3438
};
@@ -38,6 +42,6 @@ void run_bjs_script();
3842
void interpreterHandler(void *pvParameters);
3943

4044
bool run_bjs_script_headless(char *code);
41-
bool run_bjs_script_headless(FS fs, String filename);
45+
bool run_bjs_script_headless(FS &fs, String filename);
4246

4347
#endif

src/modules/bjs_interpreter/task_manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TaskManager::~TaskManager() {
1313
tasks.clear();
1414
}
1515

16-
Task *TaskManager::getTaskById(uint8_t id) {
16+
Task *TaskManager::getTaskById(int8_t id) {
1717
if (id < tasks.size()) { return tasks[id]; }
1818
return nullptr;
1919
}
@@ -25,14 +25,14 @@ Task *TaskManager::getTaskByName(const char *name) {
2525
return nullptr;
2626
}
2727

28-
uint8_t TaskManager::registerTask(Task *task) {
28+
int8_t TaskManager::registerTask(Task *task) {
2929
if (!task) return -1;
3030

3131
tasks.push_back(task);
3232
return tasks.size() - 1;
3333
}
3434

35-
bool TaskManager::unregisterTask(uint8_t taskId) {
35+
bool TaskManager::unregisterTask(int8_t taskId) {
3636
if (taskId == -1) return false;
3737
auto task = tasks[taskId];
3838
task->terminate();

src/modules/bjs_interpreter/task_manager.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66

77
class Task {
88
public:
9+
virtual ~Task() {};
910
virtual const char *getName() = 0;
10-
virtual void terminate() = 0;
11-
virtual uint8_t getState() = 0;
11+
12+
virtual void toForeground() = 0;
13+
virtual void toBackground() = 0;
14+
15+
virtual void terminate(bool waitForTermination = false) = 0;
1216
};
1317

1418
class TaskManager {
1519
public:
1620
TaskManager();
1721
~TaskManager();
1822

19-
Task *getTaskById(uint8_t id);
23+
Task *getTaskById(int8_t id);
2024
Task *getTaskByName(const char *name);
2125

22-
uint8_t registerTask(Task *task);
23-
bool unregisterTask(uint8_t taskId);
26+
int8_t registerTask(Task *task);
27+
bool unregisterTask(int8_t taskId);
2428

2529
private:
2630
std::vector<Task *> tasks;

0 commit comments

Comments
 (0)