-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathtest-posix.cpp
154 lines (122 loc) · 4.05 KB
/
test-posix.cpp
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
// Copyright & License details are available under JXCORE_LICENSE file
#include "../commons/common-posix.h"
#include "jx/job.h"
void callback(JXValue *results, int argc) {
// do nothing
}
// native JavaScript method we'll be calling from JS land
void sampleMethod(JXValue *results, int argc) {
std::stringstream ss_result;
for (int i = 0; i < argc; i++) {
std::string str_result;
ConvertResult(&results[i], str_result);
ss_result << i << " : ";
ss_result << str_result << "\n";
}
flush_console("%s", ss_result.str().c_str());
// return an Array back to JS Land
const char *str = "[1, 2, 3]";
// results[argc] corresponds to return value
JX_SetJSON(&results[argc], str, strlen(str));
}
const char *contents =
"console.log('hello world from:', process.threadId); \n"
"global.webview = {}; \n"
"setTimeout(function(){},10);\n"
"webview.call = function() { \n"
" return process.natives.sampleMethod(arguments[0], arguments[1]);\n"
"};\n";
const char *eval_str =
"webview.call([\"concat\",\"A\",\"B\",{\"jxcore_webview_callbackId\":1}]);";
#ifdef JS_ENGINE_V8
void *
#else
void
#endif
create_jxcore_instance(void *_) {
// create a new JXcore instance
JX_InitializeNewEngine();
// define entry file
JX_DefineMainFile(contents);
// define native -named- method
// we will be reaching to this method from the javascript side like this;
// process.natives.sampleMethod( ... )
JX_DefineExtension("sampleMethod", sampleMethod);
// start the engine (executes entry file)
JX_StartEngine();
// loop for possible IO
// or JX_Loop() without usleep / while
while (JX_LoopOnce() != 0) usleep(1);
JXValue result;
// evaluate a piece of JavaScript code
JX_Evaluate(eval_str, "myscript", &result);
// see if the result is a JavaScript object
if (!JX_IsJSON(&result)) {
flush_console("RETURN TYPE WAS: %d\n", result.type_);
}
assert(JX_IsJSON(&result) && "expected result here is a JSON (JS array)");
// free the memory
JX_Free(&result);
// loop for possible IO
// or JX_Loop() without usleep / while
while (JX_LoopOnce() != 0) usleep(1);
// destroy the instance
JX_StopEngine();
#ifdef JS_ENGINE_V8
return 0;
#endif
}
#define NUM_THREADS 10
int main(int argc, char **args) {
// Call JX_Initialize only once per app
JX_Initialize(args[0], callback);
// Creates a new engine for the current thread
// It's our first engine instance hence it will be the
// parent engine for all the other engine instances.
// If you need to destroy this engine instance, you should
// destroy everything else first. For the sake of this sample
// we have our first instance sitting on the main thread
// and it will be destroyed when the app exists.
JX_InitializeNewEngine();
#ifdef JS_ENGINE_V8
pthread_t thread[NUM_THREADS];
#elif defined(JS_ENGINE_MOZJS)
void *thread[NUM_THREADS];
#endif
// define the entry file contents
JX_DefineMainFile(contents);
// define native -named- method
// we will be reaching to this method from the javascript side like this;
// process.natives.sampleMethod( ... )
JX_DefineExtension("sampleMethod", sampleMethod);
// start the engine (executes entry file)
JX_StartEngine();
// create and run other instances
for (int i = 0; i < NUM_THREADS; i++) {
#ifdef JS_ENGINE_V8
assert(pthread_create(&thread[i], NULL, create_jxcore_instance, 0) == 0);
#elif defined(JS_ENGINE_MOZJS)
thread[i] = jxcore::CreateThread(create_jxcore_instance, NULL);
#endif
}
// loop for possible IO
// or JX_Loop() without usleep / while
while (JX_LoopOnce() != 0) usleep(1);
JXValue result;
// evaluate a piece of JavaScript code
JX_Evaluate(eval_str, "myscript", &result);
// free the memory
JX_Free(&result);
// loop for possible IO
// or JX_Loop() without usleep / while
while (JX_LoopOnce() != 0) usleep(1);
for (int i = 0; i < NUM_THREADS; i++) {
#ifdef JS_ENGINE_V8
pthread_join(thread[i], NULL);
#elif defined(JS_ENGINE_MOZJS)
jxcore::JoinThread(thread[i]);
#endif
}
// finally destroy the first instance
JX_StopEngine();
}