-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs.c
471 lines (400 loc) · 15 KB
/
js.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// main.c
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/view.h>
#include <gui/modules/text_box.h>
#include <gui/modules/dialog_ex.h>
#include <storage/storage.h>
#include "microvium.h"
#define JS_APP_PATH_FOLDER STORAGE_APP_DATA_PATH_PREFIX
#define TAG "microvium"
static int32_t js_run(void* context);
// A function in the host (this file) for the VM to call
#define IMPORT_FLIPPER_FURI_DELAY_MS 1
#define IMPORT_FLIPPER_CANVAS_STOP 2
#define IMPORT_FLIPPER_CANVAS_SET_FONT 3
#define IMPORT_FLIPPER_CANVAS_DRAW_STR 4
#define IMPORT_FLIPPER_CANVAS_DRAW_STR_ALIGNED 5
#define IMPORT_CONSOLE_CLEAR 6
#define IMPORT_CONSOLE_LOG 7
#define IMPORT_CONSOLE_WARN 8
#define IMPORT_FS_OPEN_SYNC 9
// A function exported by VM to for the host to call
const mvm_VMExportID MAIN = 1;
/* Use when needed
const mvm_VMExportID INIT = 2;
*/
mvm_TeError resolveImport(mvm_HostFunctionID id, void*, mvm_TfHostFunction* out);
mvm_TeError flipper_furi_delay_ms(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError flipper_canvas_stop(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError flipper_canvas_set_font(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError flipper_canvas_draw_str(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError flipper_canvas_draw_str_aligned(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError console_clear(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError console_log(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError console_warn(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
mvm_TeError fs_open_sync(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount);
typedef enum {
MyEventTypeKey,
MyEventTypeDone,
} MyEventType;
typedef struct {
MyEventType type; // The reason for this event.
InputEvent input; // This data is specific to keypress data.
} MyEvent;
typedef enum {
JSDisplay,
JSConsole,
// JSConfirm,
} ViewId;
FuriMessageQueue* queue;
ViewId current_view;
ViewDispatcher* view_dispatcher;
TextBox* text_box;
/*
bool confirmGot = false;
bool confirmRes = false;
*/
typedef struct {
FuriThread* thread;
} JSRtThread;
typedef struct {
FuriString* conLog;
} Console;
Console* console;
typedef enum {
CNone,
CDrawStr,
CDrawStrAli,
} CDrawEvent;
typedef struct {
CDrawEvent cEvent;
Font font;
const char* str;
int x;
int y;
Align horizontal;
Align vertical;
} Display;
Display* display;
typedef struct {
bool is_alloc;
File* file;
bool is_open;
const char* path;
} JSFile;
JSFile jsFile[3];
Storage* storage;
size_t fileSize;
uint8_t* fileBuff;
static void draw_callback(Canvas* canvas, void* context) {
UNUSED(context);
canvas_set_font(canvas, display->font);
if(display->cEvent == CDrawStr) {
canvas_draw_str(canvas, display->x, display->y, display->str);
} else if (display->cEvent == CDrawStrAli) {
canvas_draw_str_aligned(canvas, display->x, display->y, display->horizontal, display->vertical, display->str);
}
}
static bool input_callback(InputEvent* input_event, void* context) {
UNUSED(context);
bool handled = false;
// we set our callback context to be the view_dispatcher.
if(input_event->type == InputTypeShort) {
if(input_event->key == InputKeyBack) {
// Default back handler.
handled = false;
} else if(input_event->key == InputKeyOk) {
// switch the view!
view_dispatcher_send_custom_event(view_dispatcher, 42);
handled = true;
}
}
return handled;
}
bool navigation_event_callback(void* context) {
UNUSED(context);
// We did not handle the event, so return false.
return false;
}
bool custom_event_callback(void* context, uint32_t event) {
UNUSED(context);
bool handled = false;
if(event == 42) {
if(current_view == JSDisplay) {
current_view = JSConsole;
}
view_dispatcher_switch_to_view(view_dispatcher, current_view);
handled = true;
}
// NOTE: The return value is not currently used by the ViewDispatcher.
return handled;
}
static uint32_t exit_console_callback(void* context) {
UNUSED(context);
return JSDisplay;
}
static int32_t js_run(void* context) {
UNUSED(context);
mvm_TeError err;
mvm_VM* vm;
mvm_Value main;
mvm_Value result;
// Restore the VM from the snapshot
err = mvm_restore(&vm, fileBuff, fileSize, NULL, resolveImport);
if (err != MVM_E_SUCCESS) {
FURI_LOG_E(TAG, "Error with restore: %d", err);
return err;
}
// Find the "sayHello" function exported by the VM
err = mvm_resolveExports(vm, &MAIN, &main, 1);
if (err != MVM_E_SUCCESS) {
FURI_LOG_E(TAG, "Error with exports: %d", err);
return err;
}
// Call "main"
err = mvm_call(vm, main, &result, NULL, 0);
if (err != MVM_E_SUCCESS) {
FURI_LOG_E(TAG, "Error with call: %d", err);
return err;
}
// Clean up
mvm_runGC(vm, true);
return 0;
}
int32_t js_app() {
JSRtThread* jsThread = malloc(sizeof(JSRtThread));
storage = furi_record_open(RECORD_STORAGE);
File* bytecode = storage_file_alloc(storage);
storage_file_open(bytecode, APP_DATA_PATH("script.mvm-bc"), FSAM_READ, FSOM_OPEN_EXISTING);
fileSize = storage_file_size(bytecode);
FURI_LOG_I("microvium", "File Size: %d", fileSize);
fileBuff = malloc(fileSize);
storage_file_read(bytecode, fileBuff, fileSize);
storage_file_close(bytecode);
storage_file_free(bytecode);
//furi_record_close(RECORD_STORAGE);
for (int i = 0; i < 2; ++i) {
jsFile[i].is_alloc = false;
jsFile[i].is_open = false;
}
jsThread->thread = furi_thread_alloc_ex("microium", 1024, js_run, jsThread);
view_dispatcher = view_dispatcher_alloc();
// For this demo, we just use view_dispatcher as our application context.
void* context = view_dispatcher;
View* view1 = view_alloc();
view_set_context(view1, context);
view_set_draw_callback(view1, draw_callback);
view_set_input_callback(view1, input_callback);
view_set_orientation(view1, ViewOrientationHorizontal);
text_box = text_box_alloc();
text_box_set_font(text_box, TextBoxFontText);
view_set_previous_callback(text_box_get_view(text_box), exit_console_callback);
/*
DialogEx* dialog_ex = dialog_ex_alloc();
dialog_ex_set_left_button_text(dialog_ex, "No");
dialog_ex_set_right_button_text(dialog_ex, "Yes");
dialog_ex_set_context(dialog_ex, context);
dialog_ex_set_result_callback(dialog_ex, confirm_callback);
*/
// set param 1 of custom event callback (impacts tick and navigation too).
view_dispatcher_set_event_callback_context(view_dispatcher, context);
view_dispatcher_set_navigation_event_callback(
view_dispatcher, navigation_event_callback);
view_dispatcher_set_custom_event_callback(
view_dispatcher, custom_event_callback);
view_dispatcher_enable_queue(view_dispatcher);
view_dispatcher_add_view(view_dispatcher, JSDisplay, view1);
view_dispatcher_add_view(view_dispatcher, JSConsole, text_box_get_view(text_box));
//view_dispatcher_add_view(view_dispatcher, JSConfirm, dialog_ex_get_view(dialog_ex));
Gui* gui = furi_record_open(RECORD_GUI);
view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen);
current_view = JSDisplay;
view_dispatcher_switch_to_view(view_dispatcher, current_view);
// console init
console = malloc(sizeof(Console));
console->conLog = furi_string_alloc();
// display init and set defaults
display = malloc(sizeof(Display));
display->cEvent = CNone;
display->font = FontSecondary;
furi_thread_start(jsThread->thread);
view_dispatcher_run(view_dispatcher);
furi_thread_join(jsThread->thread);
furi_thread_free(jsThread->thread);
free(jsThread);
furi_string_free(console->conLog);
free(console);
free(display);
free(fileBuff);
for (int i = 0; i < 2; ++i) {
if(jsFile[i].is_alloc) {
if(jsFile[i].is_open) {
storage_file_close(jsFile[i].file);
jsFile[i].is_open = false;
}
storage_file_free(jsFile[i].file);
jsFile[i].is_alloc = false;
}
}
view_dispatcher_remove_view(view_dispatcher, JSDisplay);
view_dispatcher_remove_view(view_dispatcher, JSConsole);
furi_record_close(RECORD_GUI);
view_dispatcher_free(view_dispatcher);
return 0;
}
void fatalError(void* vm, int e) {
UNUSED(vm);
FURI_LOG_E(TAG, "Error: %d\n", e);
furi_crash("Microvium fatal error");
}
/*
* This function is called by `mvm_restore` to search for host functions
* imported by the VM based on their ID. Given an ID, it needs to pass back
* a pointer to the corresponding C function to be used by the VM.
*/
mvm_TeError resolveImport(mvm_HostFunctionID funcID, void* context, mvm_TfHostFunction* out) {
UNUSED(context);
if (funcID == IMPORT_FLIPPER_FURI_DELAY_MS) {
*out = flipper_furi_delay_ms;
} else if (funcID == IMPORT_FLIPPER_CANVAS_SET_FONT) {
*out = flipper_canvas_set_font;
return MVM_E_SUCCESS;
} else if (funcID == IMPORT_FLIPPER_CANVAS_DRAW_STR) {
*out = flipper_canvas_draw_str;
return MVM_E_SUCCESS;
} else if (funcID == IMPORT_FLIPPER_CANVAS_DRAW_STR_ALIGNED) {
*out = flipper_canvas_draw_str_aligned;
} else if (funcID == IMPORT_CONSOLE_LOG) {
*out = console_log;
return MVM_E_SUCCESS;
} else if (funcID == IMPORT_CONSOLE_CLEAR) {
*out = console_clear;
return MVM_E_SUCCESS;
} else if (funcID == IMPORT_CONSOLE_WARN) {
*out = console_warn;
return MVM_E_SUCCESS;
} else if (funcID == IMPORT_FS_OPEN_SYNC) {
*out = fs_open_sync;
return MVM_E_SUCCESS;
}
return MVM_E_UNRESOLVED_IMPORT;
}
mvm_TeError flipper_furi_delay_ms(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
furi_assert(argCount == 1);
FURI_LOG_I(TAG, "delay_ms()");
furi_delay_ms((int32_t)mvm_toInt32(vm, args[0]));
return MVM_E_SUCCESS;
}
mvm_TeError flipper_canvas_stop(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(vm);
UNUSED(funcID);
UNUSED(result);
UNUSED(args);
furi_assert(argCount == 0);
FURI_LOG_I(TAG, "canvas_stop()");
display->cEvent = CNone;
return MVM_E_SUCCESS;
}
mvm_TeError flipper_canvas_set_font(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
furi_assert(argCount == 1);
FURI_LOG_I(TAG, "canvas_set_font()");
// display->cEvent = CSetFont;
display->font = mvm_toInt32(vm, args[0]);
// display->cEvent = CNone;
return MVM_E_SUCCESS;
}
mvm_TeError flipper_canvas_draw_str(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
furi_assert(argCount == 3);
FURI_LOG_I(TAG, "canvas_draw_str()");
display->x = (int32_t)mvm_toInt32(vm, args[0]);
display->y = (int32_t)mvm_toInt32(vm, args[1]);
display->str = (const char*)mvm_toStringUtf8(vm, args[2], NULL);
display->cEvent = CDrawStr;
return MVM_E_SUCCESS;
}
mvm_TeError flipper_canvas_draw_str_aligned(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
furi_assert(argCount == 5);
FURI_LOG_I(TAG, "canvas_draw_str_aligned()");
display->x = (int32_t)mvm_toInt32(vm, args[0]);
display->y = (int32_t)mvm_toInt32(vm, args[1]);
display->horizontal = (int32_t)mvm_toInt32(vm, args[2]);
display->vertical = (int32_t)mvm_toInt32(vm, args[3]);
display->str = (const char*)mvm_toStringUtf8(vm, args[4], NULL);
display->cEvent = CDrawStrAli;
return MVM_E_SUCCESS;
}
mvm_TeError console_clear(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(vm);
UNUSED(funcID);
UNUSED(result);
UNUSED(args);
furi_assert(argCount == 0);
FURI_LOG_I(TAG, "console.clear()");
furi_string_reset(console->conLog);
text_box_set_text(text_box, furi_string_get_cstr(console->conLog));
return MVM_E_SUCCESS;
}
mvm_TeError console_log(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
FURI_LOG_I(TAG, "console.log()");
for (int i = 0; i < argCount-1; i++) {
if(mvm_typeOf(vm, args[i]) == VM_T_NUMBER) {
furi_string_cat_printf(console->conLog, "%d", (int)mvm_toInt32(vm, args[i]));
} else if (mvm_typeOf(vm, args[i]) == VM_T_STRING) {
furi_string_cat_printf(console->conLog, "%s", (const char*)mvm_toStringUtf8(vm, args[i], NULL));
}
}
furi_string_cat_printf(console->conLog, "\n");
text_box_set_text(text_box, furi_string_get_cstr(console->conLog));
return MVM_E_SUCCESS;
}
mvm_TeError console_warn(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
UNUSED(result);
furi_assert(argCount == 1);
FURI_LOG_I(TAG, "console.warn()");
for (int i = 0; i < argCount-1; i++) {
if(mvm_typeOf(vm, args[i]) == VM_T_NUMBER) {
FURI_LOG_W(TAG, "%d", (int)mvm_toInt32(vm, args[i]));
} else if (mvm_typeOf(vm, args[i]) == VM_T_STRING) {
FURI_LOG_W(TAG, "%s", (const char*)mvm_toStringUtf8(vm, args[i], NULL));
}
}
FURI_LOG_W(TAG, "\n");
return MVM_E_SUCCESS;
}
mvm_TeError fs_open_sync(mvm_VM* vm, mvm_HostFunctionID funcID, mvm_Value* result, mvm_Value* args, uint8_t argCount) {
UNUSED(funcID);
furi_assert(argCount == 2);
//const char* path = (const char*)mvm_toStringUtf8(vm, args[0], NULL);
//const char* type = (const char*)mvm_toStringUtf8(vm, args[1], NULL);
for (int i = 0; i < 2; ++i) {
if (!jsFile[i].is_alloc) {
jsFile[i].file = storage_file_alloc(storage);
jsFile[i].is_alloc = true;
}
if (!jsFile[i].is_open) {
jsFile[i].path = (const char*)mvm_toStringUtf8(vm, args[0], NULL);
storage_file_open(jsFile[i].file, jsFile[i].path, FSAM_READ, FSOM_OPEN_EXISTING);
jsFile[i].is_open = true;
*result = mvm_newInt32(vm, i);
return MVM_E_SUCCESS;
}
}
return MVM_E_SUCCESS; // todo: return error
}