forked from ivere27/toby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
99 lines (79 loc) · 2.74 KB
/
example.cpp
File metadata and controls
99 lines (79 loc) · 2.74 KB
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
#include <cstring>
#include <iostream>
#include <string>
#ifdef _WIN32
#include <windows.h>
#define SLEEP_ONE_SECOND Sleep(1000);
#else
#include <unistd.h>
#define SLEEP_ONE_SECOND usleep(1000*1000);
#endif
using namespace std;
typedef void (*TobyOnloadCB)(void* isolate);
typedef void (*TobyOnunloadCB)(void* isolate, int exitCode);
typedef char* (*TobyHostcallCB)(const char* name, const char* value);
typedef void (*TobyHostonCB)(int argc, char** argv);
extern "C" void tobyInit(const char* processName,
const char* userScript,
TobyOnloadCB,
TobyOnunloadCB,
TobyHostcallCB);
extern "C" int tobyJSCompile(const char* source, char* dest, size_t n);
extern "C" int tobyJSCall(const char* name, const char* value, char* dest, size_t n);
extern "C" int tobyJSEmit(const char* name, const char* value);
extern "C" int tobyHostOn(const char* name, TobyHostonCB);
void tobyOnLoad(void* isolate) {
cout << "\e[32m" << "** topyOnLoad : " << isolate << endl;
// custom source
const char* source = "function _f(x) {"
" return x ? x : ':)';"
"};"
"var _v = 43;";
char data[1024];
int ret = tobyJSCompile(source, data, sizeof(data));
if (ret < 0)
cout << "** tobyJSCompile error - code : " << ret << " , data : " << data << endl;
else
cout << "** tobyJSCompile : " << data << endl;
ret = tobyJSCall("_f", "", data, sizeof(data));
if (ret < 0)
cout << "** tobyJSCall error - code : " << ret << " , data : " << data << endl;
else
cout << "** tobyJSCall : " << data;
cout << "\e[0m" << endl << flush;
}
void tobyOnUnload(void* isolate, int exitCode) {
cout << "\e[31m" << "** tobyOnUnload : " << isolate;
cout << " exitCode : " << exitCode << endl;
cout << "\e[0m" << endl << flush;
_exit(exitCode);
}
char* tobyHostCall(const char* name, const char* value) {
cout << "\e[93m" << "** from javascript. name = " << name;
cout << " , value = " << value << "\e[0m";
cout << endl << flush;
char* data = new char[10];
strcpy(data, "hi there");
return data;
}
int main(int argc, char *argv[]) {
const char* userScript = "require('./app.js');";
// toby(processName, userScript, onloadCB, onunloadCB, hostCallCB)
tobyInit(argv[0],
userScript,
tobyOnLoad,
tobyOnUnload,
tobyHostCall);
tobyHostOn("exit", [](int argc, char** argv){
printf("tobyHostOn - argc : %d\n", argc);
for(int i = 0; i<argc;i++)
printf("tobyHostOn - argv[%d] = %s\n",i, argv[i]);
});
// dummy loop in host
static int i = 0;
while(true) {
SLEEP_ONE_SECOND;
tobyJSEmit("test", to_string(i++).c_str());
}
return 0;
}