-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.c
75 lines (64 loc) · 1.74 KB
/
app.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef use_test_runtime
#define use_test_runtime
#endif
#ifdef use_test_runtime
#include "duktapert/testrt/duktape.h"
#else
#include "duktapert/duktape.h"
#endif
#include "libs/iothubclientmqttlib/iothubclientmqttlib.h"
#include "helper/helper.h"
int main(int argc, const char *argv[]) {
#ifdef use_test_runtime
(void)printf("use_test_runtime on\r\n");
#endif
duk_context *ctx = NULL;
char line[4096];
char idx;
int ch;
ctx = duk_create_heap_default();
if (!ctx) {
(void)printf("Failed to create a Duktape heap.\n");
exit(1);
}
// register native funcs here
duk_push_global_object(ctx);
duk_push_c_function(ctx, duk_readFile, 1 /*nargs*/);
duk_put_prop_string(ctx, -2, "readFile");
duk_pop(ctx);
iothubclient_init(ctx);
if (duk_peval_file(ctx, "modulesearch.js") != 0) {
(void)printf("Error: %s\n", duk_safe_to_string(ctx, -1));
goto finished;
}
duk_pop(ctx); // ignore result
(void)printf("Input JS file to execute.\n\n");
memset(line, 0, sizeof(line));
idx = 0;
for (;;) {
if (idx >= sizeof(line)) {
printf("Line too long\n");
exit(1);
}
ch = fgetc(stdin);
if (ch == 0x0a) {
line[idx++] = '\0';
if(strcmp(line, "quit") == 0) {
goto finished;
}
if (duk_peval_file(ctx, line) != 0) {
(void)printf("Error: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx); // ignore result
}
idx = 0;
} else {
line[idx++] = (char) ch;
}
}
finished:
duk_destroy_heap(ctx);
exit(0);
}