-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathworker.cpp
More file actions
158 lines (125 loc) · 3.64 KB
/
Copy pathworker.cpp
File metadata and controls
158 lines (125 loc) · 3.64 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
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
#include <cstdio>
#include <cstdint>
#include <chrono>
#include <thread>
#include <jsapi.h>
#include <js/CompilationAndEvaluation.h>
#include <js/Conversions.h>
#include <js/Initialization.h>
#include <js/SourceText.h>
#include "boilerplate.h"
// This example illustrates usage of SpiderMonkey in multiple threads. It does
// no error handling and simply exits if something goes wrong.
//
// See 'boilerplate.cpp' for the parts of this example that are reused in many
// simple embedding examples.
//
// To use SpiderMonkey API in multiple threads, you need to create a JSContext
// in the thread, using the main thread's JSRuntime as a parent, and initialize
// self-hosted code, and create its own global.
static bool ExecuteCode(JSContext* cx, const char* code) {
JS::CompileOptions options(cx);
options.setFileAndLine("noname", 1);
JS::SourceText<mozilla::Utf8Unit> source;
if (!source.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed)) {
return false;
}
JS::Rooted<JS::Value> rval(cx);
if (!JS::Evaluate(cx, options, source, &rval)) {
return false;
}
return true;
}
static bool Print(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::Rooted<JS::Value> arg(cx, args.get(0));
JS::Rooted<JSString*> str(cx, JS::ToString(cx, arg));
if (!str) {
return false;
}
JS::UniqueChars chars = JS_EncodeStringToUTF8(cx, str);
fprintf(stderr, "%s\n", chars.get());
args.rval().setUndefined();
return true;
}
static bool Sleep(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::Rooted<JS::Value> arg(cx, args.get(0));
int32_t ms;
if (!JS::ToInt32(cx, arg, &ms)) {
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
args.rval().setUndefined();
return true;
}
bool DefineFunctions(JSContext* cx, JS::Handle<JSObject*> global) {
if (!JS_DefineFunction(cx, global, "print", &Print, 0, 0)) {
return false;
}
if (!JS_DefineFunction(cx, global, "sleep", &Sleep, 0, 0)) {
return false;
}
return true;
}
static void WorkerMain(JSRuntime* parentRuntime) {
JSContext* cx = JS_NewContext(8L * 1024L * 1024L, parentRuntime);
if (!JS::InitSelfHostedCode(cx)) {
fprintf(stderr, "Error: Failed during JS::InitSelfHostedCode\n");
return;
}
{
JS::Rooted<JSObject*> global(cx, boilerplate::CreateGlobal(cx));
if (!global) {
fprintf(stderr, "Error: Failed during boilerplate::CreateGlobal\n");
return;
}
JSAutoRealm ar(cx, global);
if (!DefineFunctions(cx, global)) {
boilerplate::ReportAndClearException(cx);
return;
}
if (!ExecuteCode(cx, R"js(
for (let i = 0; i < 10; i++) {
print(`in worker thread, it is ${new Date()}`);
sleep(1000);
}
)js")) {
boilerplate::ReportAndClearException(cx);
return;
}
}
JS_DestroyContext(cx);
return;
}
static bool WorkerExample(JSContext* cx) {
JS::Rooted<JSObject*> global(cx, boilerplate::CreateGlobal(cx));
if (!global) {
return false;
}
std::thread thread1(WorkerMain, JS_GetRuntime(cx));
std::thread thread2(WorkerMain, JS_GetRuntime(cx));
JSAutoRealm ar(cx, global);
if (!DefineFunctions(cx, global)) {
boilerplate::ReportAndClearException(cx);
return false;
}
if (!ExecuteCode(cx, R"js(
for (let i = 0; i < 10; i++) {
print(`in main thread, it is ${new Date()}`);
sleep(1000);
}
)js")) {
boilerplate::ReportAndClearException(cx);
return false;
}
thread1.join();
thread2.join();
return true;
}
int main(int argc, const char* argv[]) {
if (!boilerplate::RunExample(WorkerExample)) {
return 1;
}
return 0;
}