mini_racer 0.1.11 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.dockerignore +12 -0
- data/.github/workflows/ci.yml +80 -0
- data/.gitignore +1 -0
- data/.travis.yml +24 -12
- data/CHANGELOG +161 -0
- data/Dockerfile +21 -0
- data/LICENSE.txt +1 -1
- data/README.md +77 -4
- data/Rakefile +42 -0
- data/ext/mini_racer_extension/extconf.rb +24 -7
- data/ext/mini_racer_extension/mini_racer_extension.cc +1128 -472
- data/ext/mini_racer_loader/extconf.rb +8 -0
- data/ext/mini_racer_loader/mini_racer_loader.c +123 -0
- data/lib/mini_racer/version.rb +4 -1
- data/lib/mini_racer.rb +195 -40
- data/mini_racer.gemspec +14 -7
- metadata +43 -20
|
@@ -1,50 +1,97 @@
|
|
|
1
1
|
#include <stdio.h>
|
|
2
2
|
#include <ruby.h>
|
|
3
3
|
#include <ruby/thread.h>
|
|
4
|
+
#include <ruby/io.h>
|
|
4
5
|
#include <v8.h>
|
|
6
|
+
#include <v8-profiler.h>
|
|
5
7
|
#include <libplatform/libplatform.h>
|
|
6
8
|
#include <ruby/encoding.h>
|
|
7
9
|
#include <pthread.h>
|
|
8
10
|
#include <unistd.h>
|
|
9
11
|
#include <mutex>
|
|
12
|
+
#include <atomic>
|
|
10
13
|
#include <math.h>
|
|
11
14
|
|
|
12
15
|
using namespace v8;
|
|
13
16
|
|
|
14
|
-
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
15
|
-
public:
|
|
16
|
-
virtual void* Allocate(size_t length) {
|
|
17
|
-
void* data = AllocateUninitialized(length);
|
|
18
|
-
return data == NULL ? data : memset(data, 0, length);
|
|
19
|
-
}
|
|
20
|
-
virtual void* AllocateUninitialized(size_t length) {
|
|
21
|
-
return malloc(length);
|
|
22
|
-
}
|
|
23
|
-
virtual void Free(void* data, size_t) {
|
|
24
|
-
free(data);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
17
|
typedef struct {
|
|
29
18
|
const char* data;
|
|
30
19
|
int raw_size;
|
|
31
20
|
} SnapshotInfo;
|
|
32
21
|
|
|
33
|
-
|
|
22
|
+
class IsolateInfo {
|
|
23
|
+
public:
|
|
34
24
|
Isolate* isolate;
|
|
35
|
-
|
|
25
|
+
ArrayBuffer::Allocator* allocator;
|
|
36
26
|
StartupData* startup_data;
|
|
37
27
|
bool interrupted;
|
|
38
|
-
bool
|
|
28
|
+
bool added_gc_cb;
|
|
39
29
|
pid_t pid;
|
|
30
|
+
VALUE mutex;
|
|
31
|
+
|
|
32
|
+
class Lock {
|
|
33
|
+
VALUE &mutex;
|
|
34
|
+
|
|
35
|
+
public:
|
|
36
|
+
Lock(VALUE &mutex) : mutex(mutex) {
|
|
37
|
+
rb_mutex_lock(mutex);
|
|
38
|
+
}
|
|
39
|
+
~Lock() {
|
|
40
|
+
rb_mutex_unlock(mutex);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
IsolateInfo() : isolate(nullptr), allocator(nullptr), startup_data(nullptr),
|
|
46
|
+
interrupted(false), added_gc_cb(false), pid(getpid()), refs_count(0) {
|
|
47
|
+
VALUE cMutex = rb_const_get(rb_cThread, rb_intern("Mutex"));
|
|
48
|
+
mutex = rb_class_new_instance(0, nullptr, cMutex);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
~IsolateInfo();
|
|
52
|
+
|
|
53
|
+
void init(SnapshotInfo* snapshot_info = nullptr);
|
|
54
|
+
|
|
55
|
+
void mark() {
|
|
56
|
+
rb_gc_mark(mutex);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
Lock createLock() {
|
|
60
|
+
Lock lock(mutex);
|
|
61
|
+
return lock;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void hold() {
|
|
65
|
+
refs_count++;
|
|
66
|
+
}
|
|
67
|
+
void release() {
|
|
68
|
+
if (--refs_count <= 0) {
|
|
69
|
+
delete this;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
40
72
|
|
|
73
|
+
int refs() {
|
|
74
|
+
return refs_count;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static void* operator new(size_t size) {
|
|
78
|
+
return ruby_xmalloc(size);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static void operator delete(void *block) {
|
|
82
|
+
xfree(block);
|
|
83
|
+
}
|
|
84
|
+
private:
|
|
41
85
|
// how many references to this isolate exist
|
|
42
|
-
// we can't rely on Ruby's GC for this, because
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
86
|
+
// we can't rely on Ruby's GC for this, because Ruby could destroy the
|
|
87
|
+
// isolate before destroying the contexts that depend on them. We'd need to
|
|
88
|
+
// keep a list of linked contexts in the isolate to destroy those first when
|
|
89
|
+
// isolate destruction was requested. Keeping such a list would require
|
|
90
|
+
// notification from the context VALUEs when they are constructed and
|
|
91
|
+
// destroyed. With a ref count, those notifications are still needed, but
|
|
92
|
+
// we keep a simple int rather than a list of pointers.
|
|
93
|
+
std::atomic_int refs_count;
|
|
94
|
+
};
|
|
48
95
|
|
|
49
96
|
typedef struct {
|
|
50
97
|
IsolateInfo* isolate_info;
|
|
@@ -67,9 +114,33 @@ typedef struct {
|
|
|
67
114
|
Local<String>* filename;
|
|
68
115
|
useconds_t timeout;
|
|
69
116
|
EvalResult* result;
|
|
117
|
+
size_t max_memory;
|
|
70
118
|
} EvalParams;
|
|
71
119
|
|
|
120
|
+
typedef struct {
|
|
121
|
+
ContextInfo *context_info;
|
|
122
|
+
char *function_name;
|
|
123
|
+
int argc;
|
|
124
|
+
bool error;
|
|
125
|
+
Local<Function> fun;
|
|
126
|
+
Local<Value> *argv;
|
|
127
|
+
EvalResult result;
|
|
128
|
+
size_t max_memory;
|
|
129
|
+
} FunctionCall;
|
|
130
|
+
|
|
131
|
+
enum IsolateFlags {
|
|
132
|
+
IN_GVL,
|
|
133
|
+
DO_TERMINATE,
|
|
134
|
+
MEM_SOFTLIMIT_VALUE,
|
|
135
|
+
MEM_SOFTLIMIT_REACHED,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
static VALUE rb_cContext;
|
|
139
|
+
static VALUE rb_cSnapshot;
|
|
140
|
+
static VALUE rb_cIsolate;
|
|
141
|
+
|
|
72
142
|
static VALUE rb_eScriptTerminatedError;
|
|
143
|
+
static VALUE rb_eV8OutOfMemoryError;
|
|
73
144
|
static VALUE rb_eParseError;
|
|
74
145
|
static VALUE rb_eScriptRuntimeError;
|
|
75
146
|
static VALUE rb_cJavaScriptFunction;
|
|
@@ -80,15 +151,28 @@ static VALUE rb_mJSON;
|
|
|
80
151
|
static VALUE rb_cFailedV8Conversion;
|
|
81
152
|
static VALUE rb_cDateTime = Qnil;
|
|
82
153
|
|
|
83
|
-
static Platform
|
|
154
|
+
static std::unique_ptr<Platform> current_platform = NULL;
|
|
84
155
|
static std::mutex platform_lock;
|
|
85
156
|
|
|
157
|
+
static pthread_attr_t *thread_attr_p;
|
|
158
|
+
static pthread_rwlock_t exit_lock = PTHREAD_RWLOCK_INITIALIZER;
|
|
159
|
+
static bool ruby_exiting = false; // guarded by exit_lock
|
|
160
|
+
static bool single_threaded = false;
|
|
161
|
+
|
|
86
162
|
static VALUE rb_platform_set_flag_as_str(VALUE _klass, VALUE flag_as_str) {
|
|
87
163
|
bool platform_already_initialized = false;
|
|
88
164
|
|
|
165
|
+
if(TYPE(flag_as_str) != T_STRING) {
|
|
166
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE" (should be a string)",
|
|
167
|
+
rb_obj_class(flag_as_str));
|
|
168
|
+
}
|
|
169
|
+
|
|
89
170
|
platform_lock.lock();
|
|
90
171
|
|
|
91
172
|
if (current_platform == NULL) {
|
|
173
|
+
if (!strcmp(RSTRING_PTR(flag_as_str), "--single_threaded")) {
|
|
174
|
+
single_threaded = true;
|
|
175
|
+
}
|
|
92
176
|
V8::SetFlagsFromString(RSTRING_PTR(flag_as_str), (int)RSTRING_LEN(flag_as_str));
|
|
93
177
|
} else {
|
|
94
178
|
platform_already_initialized = true;
|
|
@@ -112,20 +196,128 @@ static void init_v8() {
|
|
|
112
196
|
|
|
113
197
|
if (current_platform == NULL) {
|
|
114
198
|
V8::InitializeICU();
|
|
115
|
-
current_platform = platform::
|
|
116
|
-
V8::InitializePlatform(current_platform);
|
|
199
|
+
current_platform = platform::NewDefaultPlatform();
|
|
200
|
+
V8::InitializePlatform(current_platform.get());
|
|
117
201
|
V8::Initialize();
|
|
118
202
|
}
|
|
119
203
|
|
|
120
204
|
platform_lock.unlock();
|
|
121
205
|
}
|
|
122
206
|
|
|
207
|
+
static void gc_callback(Isolate *isolate, GCType type, GCCallbackFlags flags) {
|
|
208
|
+
if((bool)isolate->GetData(MEM_SOFTLIMIT_REACHED)) return;
|
|
209
|
+
|
|
210
|
+
size_t softlimit = *(size_t*) isolate->GetData(MEM_SOFTLIMIT_VALUE);
|
|
211
|
+
|
|
212
|
+
HeapStatistics stats;
|
|
213
|
+
isolate->GetHeapStatistics(&stats);
|
|
214
|
+
size_t used = stats.used_heap_size();
|
|
215
|
+
|
|
216
|
+
if(used > softlimit) {
|
|
217
|
+
isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)true);
|
|
218
|
+
isolate->TerminateExecution();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// to be called with active lock and scope
|
|
223
|
+
static void prepare_result(MaybeLocal<Value> v8res,
|
|
224
|
+
TryCatch& trycatch,
|
|
225
|
+
Isolate* isolate,
|
|
226
|
+
Local<Context> context,
|
|
227
|
+
EvalResult& evalRes /* out */) {
|
|
228
|
+
|
|
229
|
+
// just don't touch .parsed
|
|
230
|
+
evalRes.terminated = false;
|
|
231
|
+
evalRes.json = false;
|
|
232
|
+
evalRes.value = nullptr;
|
|
233
|
+
evalRes.message = nullptr;
|
|
234
|
+
evalRes.backtrace = nullptr;
|
|
235
|
+
evalRes.executed = !v8res.IsEmpty();
|
|
236
|
+
|
|
237
|
+
if (evalRes.executed) {
|
|
238
|
+
// arrays and objects get converted to json
|
|
239
|
+
Local<Value> local_value = v8res.ToLocalChecked();
|
|
240
|
+
if ((local_value->IsObject() || local_value->IsArray()) &&
|
|
241
|
+
!local_value->IsDate() && !local_value->IsFunction()) {
|
|
242
|
+
Local<Object> JSON = context->Global()->Get(
|
|
243
|
+
context, String::NewFromUtf8Literal(isolate, "JSON"))
|
|
244
|
+
.ToLocalChecked().As<Object>();
|
|
245
|
+
|
|
246
|
+
Local<Function> stringify = JSON->Get(
|
|
247
|
+
context, v8::String::NewFromUtf8Literal(isolate, "stringify"))
|
|
248
|
+
.ToLocalChecked().As<Function>();
|
|
249
|
+
|
|
250
|
+
Local<Object> object = local_value->ToObject(context).ToLocalChecked();
|
|
251
|
+
const unsigned argc = 1;
|
|
252
|
+
Local<Value> argv[argc] = { object };
|
|
253
|
+
MaybeLocal<Value> json = stringify->Call(context, JSON, argc, argv);
|
|
254
|
+
|
|
255
|
+
if (json.IsEmpty()) {
|
|
256
|
+
evalRes.executed = false;
|
|
257
|
+
} else {
|
|
258
|
+
evalRes.json = true;
|
|
259
|
+
Persistent<Value>* persistent = new Persistent<Value>();
|
|
260
|
+
persistent->Reset(isolate, json.ToLocalChecked());
|
|
261
|
+
evalRes.value = persistent;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
} else {
|
|
265
|
+
Persistent<Value>* persistent = new Persistent<Value>();
|
|
266
|
+
persistent->Reset(isolate, local_value);
|
|
267
|
+
evalRes.value = persistent;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (!evalRes.executed || !evalRes.parsed) {
|
|
272
|
+
if (trycatch.HasCaught()) {
|
|
273
|
+
if (!trycatch.Exception()->IsNull()) {
|
|
274
|
+
evalRes.message = new Persistent<Value>();
|
|
275
|
+
Local<Message> message = trycatch.Message();
|
|
276
|
+
char buf[1000];
|
|
277
|
+
int len, line, column;
|
|
278
|
+
|
|
279
|
+
if (!message->GetLineNumber(context).To(&line)) {
|
|
280
|
+
line = 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!message->GetStartColumn(context).To(&column)) {
|
|
284
|
+
column = 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(isolate, message->Get()),
|
|
288
|
+
*String::Utf8Value(isolate, message->GetScriptResourceName()->ToString(context).ToLocalChecked()),
|
|
289
|
+
line,
|
|
290
|
+
column);
|
|
291
|
+
|
|
292
|
+
if ((size_t) len >= sizeof(buf)) {
|
|
293
|
+
len = sizeof(buf) - 1;
|
|
294
|
+
buf[len] = '\0';
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
Local<String> v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, len).ToLocalChecked();
|
|
298
|
+
evalRes.message->Reset(isolate, v8_message);
|
|
299
|
+
} else if(trycatch.HasTerminated()) {
|
|
300
|
+
evalRes.terminated = true;
|
|
301
|
+
evalRes.message = new Persistent<Value>();
|
|
302
|
+
Local<String> tmp = String::NewFromUtf8Literal(isolate, "JavaScript was terminated (either by timeout or explicitly)");
|
|
303
|
+
evalRes.message->Reset(isolate, tmp);
|
|
304
|
+
}
|
|
305
|
+
if (!trycatch.StackTrace(context).IsEmpty()) {
|
|
306
|
+
evalRes.backtrace = new Persistent<Value>();
|
|
307
|
+
evalRes.backtrace->Reset(isolate,
|
|
308
|
+
trycatch.StackTrace(context).ToLocalChecked()->ToString(context).ToLocalChecked());
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
123
314
|
void*
|
|
124
315
|
nogvl_context_eval(void* arg) {
|
|
125
316
|
|
|
126
317
|
EvalParams* eval_params = (EvalParams*)arg;
|
|
127
318
|
EvalResult* result = eval_params->result;
|
|
128
|
-
|
|
319
|
+
IsolateInfo* isolate_info = eval_params->context_info->isolate_info;
|
|
320
|
+
Isolate* isolate = isolate_info->isolate;
|
|
129
321
|
|
|
130
322
|
Isolate::Scope isolate_scope(isolate);
|
|
131
323
|
HandleScope handle_scope(isolate);
|
|
@@ -135,20 +327,24 @@ nogvl_context_eval(void* arg) {
|
|
|
135
327
|
v8::ScriptOrigin *origin = NULL;
|
|
136
328
|
|
|
137
329
|
// in gvl flag
|
|
138
|
-
isolate->SetData(
|
|
330
|
+
isolate->SetData(IN_GVL, (void*)false);
|
|
139
331
|
// terminate ASAP
|
|
140
|
-
isolate->SetData(
|
|
332
|
+
isolate->SetData(DO_TERMINATE, (void*)false);
|
|
333
|
+
// Memory softlimit
|
|
334
|
+
isolate->SetData(MEM_SOFTLIMIT_VALUE, (void*)false);
|
|
335
|
+
// Memory softlimit hit flag
|
|
336
|
+
isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)false);
|
|
141
337
|
|
|
142
338
|
MaybeLocal<Script> parsed_script;
|
|
143
339
|
|
|
144
340
|
if (eval_params->filename) {
|
|
145
|
-
|
|
341
|
+
origin = new v8::ScriptOrigin(*eval_params->filename);
|
|
146
342
|
}
|
|
147
343
|
|
|
148
344
|
parsed_script = Script::Compile(context, *eval_params->eval, origin);
|
|
149
345
|
|
|
150
346
|
if (origin) {
|
|
151
|
-
|
|
347
|
+
delete origin;
|
|
152
348
|
}
|
|
153
349
|
|
|
154
350
|
result->parsed = !parsed_script.IsEmpty();
|
|
@@ -157,125 +353,82 @@ nogvl_context_eval(void* arg) {
|
|
|
157
353
|
result->json = false;
|
|
158
354
|
result->value = NULL;
|
|
159
355
|
|
|
356
|
+
MaybeLocal<Value> maybe_value;
|
|
160
357
|
if (!result->parsed) {
|
|
161
|
-
|
|
162
|
-
|
|
358
|
+
result->message = new Persistent<Value>();
|
|
359
|
+
result->message->Reset(isolate, trycatch.Exception());
|
|
163
360
|
} else {
|
|
361
|
+
// parsing successful
|
|
362
|
+
if (eval_params->max_memory > 0) {
|
|
363
|
+
isolate->SetData(MEM_SOFTLIMIT_VALUE, &eval_params->max_memory);
|
|
364
|
+
if (!isolate_info->added_gc_cb) {
|
|
365
|
+
isolate->AddGCEpilogueCallback(gc_callback);
|
|
366
|
+
isolate_info->added_gc_cb = true;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
164
369
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
result->executed = !maybe_value.IsEmpty();
|
|
168
|
-
|
|
169
|
-
if (result->executed) {
|
|
170
|
-
|
|
171
|
-
// arrays and objects get converted to json
|
|
172
|
-
Local<Value> local_value = maybe_value.ToLocalChecked();
|
|
173
|
-
if ((local_value->IsObject() || local_value->IsArray()) &&
|
|
174
|
-
!local_value->IsDate() && !local_value->IsFunction()) {
|
|
175
|
-
Local<Object> JSON = context->Global()->Get(
|
|
176
|
-
String::NewFromUtf8(isolate, "JSON"))->ToObject();
|
|
177
|
-
|
|
178
|
-
Local<Function> stringify = JSON->Get(v8::String::NewFromUtf8(isolate, "stringify"))
|
|
179
|
-
.As<Function>();
|
|
180
|
-
|
|
181
|
-
Local<Object> object = local_value->ToObject();
|
|
182
|
-
const unsigned argc = 1;
|
|
183
|
-
Local<Value> argv[argc] = { object };
|
|
184
|
-
MaybeLocal<Value> json = stringify->Call(JSON, argc, argv);
|
|
185
|
-
|
|
186
|
-
if (json.IsEmpty()) {
|
|
187
|
-
result->executed = false;
|
|
188
|
-
} else {
|
|
189
|
-
result->json = true;
|
|
190
|
-
Persistent<Value>* persistent = new Persistent<Value>();
|
|
191
|
-
persistent->Reset(isolate, json.ToLocalChecked());
|
|
192
|
-
result->value = persistent;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
} else {
|
|
196
|
-
Persistent<Value>* persistent = new Persistent<Value>();
|
|
197
|
-
persistent->Reset(isolate, local_value);
|
|
198
|
-
result->value = persistent;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (!result->executed || !result->parsed) {
|
|
204
|
-
if (trycatch.HasCaught()) {
|
|
205
|
-
if (!trycatch.Exception()->IsNull()) {
|
|
206
|
-
result->message = new Persistent<Value>();
|
|
207
|
-
Local<Message> message = trycatch.Message();
|
|
208
|
-
char buf[1000];
|
|
209
|
-
int len;
|
|
210
|
-
len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(message->Get()),
|
|
211
|
-
*String::Utf8Value(message->GetScriptResourceName()->ToString()),
|
|
212
|
-
message->GetLineNumber(),
|
|
213
|
-
message->GetStartColumn());
|
|
214
|
-
|
|
215
|
-
Local<String> v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, (int)len).ToLocalChecked();
|
|
216
|
-
result->message->Reset(isolate, v8_message);
|
|
217
|
-
} else if(trycatch.HasTerminated()) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
result->terminated = true;
|
|
221
|
-
result->message = new Persistent<Value>();
|
|
222
|
-
Local<String> tmp = String::NewFromUtf8(isolate, "JavaScript was terminated (either by timeout or explicitly)");
|
|
223
|
-
result->message->Reset(isolate, tmp);
|
|
224
|
-
}
|
|
225
|
-
if (!trycatch.StackTrace().IsEmpty()) {
|
|
226
|
-
result->backtrace = new Persistent<Value>();
|
|
227
|
-
result->backtrace->Reset(isolate, trycatch.StackTrace()->ToString());
|
|
228
|
-
}
|
|
229
|
-
}
|
|
370
|
+
maybe_value = parsed_script.ToLocalChecked()->Run(context);
|
|
230
371
|
}
|
|
231
372
|
|
|
232
|
-
|
|
373
|
+
prepare_result(maybe_value, trycatch, isolate, context, *result);
|
|
233
374
|
|
|
375
|
+
isolate->SetData(IN_GVL, (void*)true);
|
|
234
376
|
|
|
235
377
|
return NULL;
|
|
236
378
|
}
|
|
237
379
|
|
|
238
|
-
static VALUE
|
|
380
|
+
static VALUE new_empty_failed_conv_obj() {
|
|
381
|
+
// TODO isolate code that translates execption to ruby
|
|
382
|
+
// exception so we can properly return it
|
|
383
|
+
return rb_funcall(rb_cFailedV8Conversion, rb_intern("new"), 1, rb_str_new2(""));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// assumes isolate locking is in place
|
|
387
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
|
|
388
|
+
Local<Value> value) {
|
|
239
389
|
|
|
240
390
|
Isolate::Scope isolate_scope(isolate);
|
|
241
391
|
HandleScope scope(isolate);
|
|
242
392
|
|
|
243
393
|
if (value->IsNull() || value->IsUndefined()){
|
|
244
|
-
|
|
394
|
+
return Qnil;
|
|
245
395
|
}
|
|
246
396
|
|
|
247
397
|
if (value->IsInt32()) {
|
|
248
|
-
|
|
398
|
+
return INT2FIX(value->Int32Value(context).ToChecked());
|
|
249
399
|
}
|
|
250
400
|
|
|
251
401
|
if (value->IsNumber()) {
|
|
252
|
-
|
|
402
|
+
return rb_float_new(value->NumberValue(context).ToChecked());
|
|
253
403
|
}
|
|
254
404
|
|
|
255
405
|
if (value->IsTrue()) {
|
|
256
|
-
|
|
406
|
+
return Qtrue;
|
|
257
407
|
}
|
|
258
408
|
|
|
259
409
|
if (value->IsFalse()) {
|
|
260
|
-
|
|
410
|
+
return Qfalse;
|
|
261
411
|
}
|
|
262
412
|
|
|
263
413
|
if (value->IsArray()) {
|
|
264
414
|
VALUE rb_array = rb_ary_new();
|
|
265
415
|
Local<Array> arr = Local<Array>::Cast(value);
|
|
266
416
|
for(uint32_t i=0; i < arr->Length(); i++) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
417
|
+
MaybeLocal<Value> element = arr->Get(context, i);
|
|
418
|
+
if (element.IsEmpty()) {
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
VALUE rb_elem = convert_v8_to_ruby(isolate, context, element.ToLocalChecked());
|
|
422
|
+
if (rb_funcall(rb_elem, rb_intern("class"), 0) == rb_cFailedV8Conversion) {
|
|
423
|
+
return rb_elem;
|
|
424
|
+
}
|
|
272
425
|
rb_ary_push(rb_array, rb_elem);
|
|
273
426
|
}
|
|
274
427
|
return rb_array;
|
|
275
428
|
}
|
|
276
429
|
|
|
277
430
|
if (value->IsFunction()){
|
|
278
|
-
|
|
431
|
+
return rb_funcall(rb_cJavaScriptFunction, rb_intern("new"), 0);
|
|
279
432
|
}
|
|
280
433
|
|
|
281
434
|
if (value->IsDate()){
|
|
@@ -287,38 +440,76 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
|
|
|
287
440
|
}
|
|
288
441
|
|
|
289
442
|
if (value->IsObject()) {
|
|
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
|
-
|
|
443
|
+
VALUE rb_hash = rb_hash_new();
|
|
444
|
+
TryCatch trycatch(isolate);
|
|
445
|
+
|
|
446
|
+
Local<Object> object = value->ToObject(context).ToLocalChecked();
|
|
447
|
+
auto maybe_props = object->GetOwnPropertyNames(context);
|
|
448
|
+
if (!maybe_props.IsEmpty()) {
|
|
449
|
+
Local<Array> props = maybe_props.ToLocalChecked();
|
|
450
|
+
for(uint32_t i=0; i < props->Length(); i++) {
|
|
451
|
+
MaybeLocal<Value> key = props->Get(context, i);
|
|
452
|
+
if (key.IsEmpty()) {
|
|
453
|
+
return rb_funcall(rb_cFailedV8Conversion, rb_intern("new"), 1, rb_str_new2(""));
|
|
454
|
+
}
|
|
455
|
+
VALUE rb_key = convert_v8_to_ruby(isolate, context, key.ToLocalChecked());
|
|
456
|
+
|
|
457
|
+
MaybeLocal<Value> prop_value = object->Get(context, key.ToLocalChecked());
|
|
458
|
+
// this may have failed due to Get raising
|
|
459
|
+
if (prop_value.IsEmpty() || trycatch.HasCaught()) {
|
|
460
|
+
return new_empty_failed_conv_obj();
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
VALUE rb_value = convert_v8_to_ruby(
|
|
464
|
+
isolate, context, prop_value.ToLocalChecked());
|
|
465
|
+
rb_hash_aset(rb_hash, rb_key, rb_value);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return rb_hash;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (value->IsSymbol()) {
|
|
472
|
+
v8::String::Utf8Value symbol_name(isolate,
|
|
473
|
+
Local<Symbol>::Cast(value)->Name());
|
|
474
|
+
|
|
475
|
+
VALUE str_symbol = rb_enc_str_new(
|
|
476
|
+
*symbol_name,
|
|
477
|
+
symbol_name.length(),
|
|
478
|
+
rb_enc_find("utf-8")
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
return ID2SYM(rb_intern_str(str_symbol));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
MaybeLocal<String> rstr_maybe = value->ToString(context);
|
|
485
|
+
|
|
486
|
+
if (rstr_maybe.IsEmpty()) {
|
|
487
|
+
return Qnil;
|
|
488
|
+
} else {
|
|
489
|
+
Local<String> rstr = rstr_maybe.ToLocalChecked();
|
|
490
|
+
return rb_enc_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(isolate), rb_enc_find("utf-8"));
|
|
315
491
|
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate,
|
|
495
|
+
const Persistent<Context>& context,
|
|
496
|
+
Local<Value> value) {
|
|
497
|
+
HandleScope scope(isolate);
|
|
498
|
+
return convert_v8_to_ruby(isolate,
|
|
499
|
+
Local<Context>::New(isolate, context),
|
|
500
|
+
value);
|
|
501
|
+
}
|
|
316
502
|
|
|
317
|
-
|
|
318
|
-
|
|
503
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate,
|
|
504
|
+
const Persistent<Context>& context,
|
|
505
|
+
const Persistent<Value>& value) {
|
|
506
|
+
HandleScope scope(isolate);
|
|
507
|
+
return convert_v8_to_ruby(isolate,
|
|
508
|
+
Local<Context>::New(isolate, context),
|
|
509
|
+
Local<Value>::New(isolate, value));
|
|
319
510
|
}
|
|
320
511
|
|
|
321
|
-
static
|
|
512
|
+
static Local<Value> convert_ruby_to_v8(Isolate* isolate, Local<Context> context, VALUE value) {
|
|
322
513
|
EscapableHandleScope scope(isolate);
|
|
323
514
|
|
|
324
515
|
Local<Array> array;
|
|
@@ -352,7 +543,7 @@ static Handle<Value> convert_ruby_to_v8(Isolate* isolate, VALUE value) {
|
|
|
352
543
|
length = RARRAY_LEN(value);
|
|
353
544
|
array = Array::New(isolate, (int)length);
|
|
354
545
|
for(i=0; i<length; i++) {
|
|
355
|
-
|
|
546
|
+
array->Set(context, i, convert_ruby_to_v8(isolate, context, rb_ary_entry(value, i)));
|
|
356
547
|
}
|
|
357
548
|
return scope.Escape(array);
|
|
358
549
|
case T_HASH:
|
|
@@ -361,8 +552,8 @@ static Handle<Value> convert_ruby_to_v8(Isolate* isolate, VALUE value) {
|
|
|
361
552
|
length = RARRAY_LEN(hash_as_array);
|
|
362
553
|
for(i=0; i<length; i++) {
|
|
363
554
|
pair = rb_ary_entry(hash_as_array, i);
|
|
364
|
-
|
|
365
|
-
|
|
555
|
+
object->Set(context, convert_ruby_to_v8(isolate, context, rb_ary_entry(pair, 0)),
|
|
556
|
+
convert_ruby_to_v8(isolate, context, rb_ary_entry(pair, 1)));
|
|
366
557
|
}
|
|
367
558
|
return scope.Escape(object);
|
|
368
559
|
case T_SYMBOL:
|
|
@@ -377,7 +568,7 @@ static Handle<Value> convert_ruby_to_v8(Isolate* isolate, VALUE value) {
|
|
|
377
568
|
value = rb_funcall(value, rb_intern("to_time"), 0);
|
|
378
569
|
}
|
|
379
570
|
value = rb_funcall(value, rb_intern("to_f"), 0);
|
|
380
|
-
return scope.Escape(Date::New(
|
|
571
|
+
return scope.Escape(Date::New(context, NUM2DBL(value) * 1000).ToLocalChecked());
|
|
381
572
|
}
|
|
382
573
|
case T_OBJECT:
|
|
383
574
|
case T_CLASS:
|
|
@@ -391,16 +582,90 @@ static Handle<Value> convert_ruby_to_v8(Isolate* isolate, VALUE value) {
|
|
|
391
582
|
case T_UNDEF:
|
|
392
583
|
case T_NODE:
|
|
393
584
|
default:
|
|
394
|
-
return scope.Escape(String::
|
|
585
|
+
return scope.Escape(String::NewFromUtf8Literal(isolate, "Undefined Conversion"));
|
|
586
|
+
}
|
|
395
587
|
}
|
|
396
|
-
|
|
397
|
-
}
|
|
398
588
|
|
|
399
589
|
static void unblock_eval(void *ptr) {
|
|
400
590
|
EvalParams* eval = (EvalParams*)ptr;
|
|
401
591
|
eval->context_info->isolate_info->interrupted = true;
|
|
402
592
|
}
|
|
403
593
|
|
|
594
|
+
/*
|
|
595
|
+
* The implementations of the run_extra_code(), create_snapshot_data_blob() and
|
|
596
|
+
* warm_up_snapshot_data_blob() functions have been derived from V8's test suite.
|
|
597
|
+
*/
|
|
598
|
+
static bool run_extra_code(Isolate *isolate, Local<v8::Context> context,
|
|
599
|
+
const char *utf8_source, const char *name) {
|
|
600
|
+
Context::Scope context_scope(context);
|
|
601
|
+
TryCatch try_catch(isolate);
|
|
602
|
+
Local<String> source_string;
|
|
603
|
+
if (!String::NewFromUtf8(isolate, utf8_source).ToLocal(&source_string)) {
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
Local<String> resource_name =
|
|
607
|
+
String::NewFromUtf8(isolate, name).ToLocalChecked();
|
|
608
|
+
ScriptOrigin origin(resource_name);
|
|
609
|
+
ScriptCompiler::Source source(source_string, origin);
|
|
610
|
+
Local<Script> script;
|
|
611
|
+
if (!ScriptCompiler::Compile(context, &source).ToLocal(&script))
|
|
612
|
+
return false;
|
|
613
|
+
if (script->Run(context).IsEmpty()) return false;
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
static StartupData
|
|
618
|
+
create_snapshot_data_blob(const char *embedded_source = nullptr) {
|
|
619
|
+
Isolate *isolate = Isolate::Allocate();
|
|
620
|
+
|
|
621
|
+
// Optionally run a script to embed, and serialize to create a snapshot blob.
|
|
622
|
+
SnapshotCreator snapshot_creator(isolate);
|
|
623
|
+
{
|
|
624
|
+
HandleScope scope(isolate);
|
|
625
|
+
Local<v8::Context> context = v8::Context::New(isolate);
|
|
626
|
+
if (embedded_source != nullptr &&
|
|
627
|
+
!run_extra_code(isolate, context, embedded_source, "<embedded>")) {
|
|
628
|
+
return {};
|
|
629
|
+
}
|
|
630
|
+
snapshot_creator.SetDefaultContext(context);
|
|
631
|
+
}
|
|
632
|
+
return snapshot_creator.CreateBlob(
|
|
633
|
+
SnapshotCreator::FunctionCodeHandling::kClear);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
StartupData warm_up_snapshot_data_blob(StartupData cold_snapshot_blob,
|
|
637
|
+
const char *warmup_source) {
|
|
638
|
+
// Use following steps to create a warmed up snapshot blob from a cold one:
|
|
639
|
+
// - Create a new isolate from the cold snapshot.
|
|
640
|
+
// - Create a new context to run the warmup script. This will trigger
|
|
641
|
+
// compilation of executed functions.
|
|
642
|
+
// - Create a new context. This context will be unpolluted.
|
|
643
|
+
// - Serialize the isolate and the second context into a new snapshot blob.
|
|
644
|
+
StartupData result = {nullptr, 0};
|
|
645
|
+
|
|
646
|
+
if (cold_snapshot_blob.raw_size > 0 && cold_snapshot_blob.data != nullptr &&
|
|
647
|
+
warmup_source != NULL) {
|
|
648
|
+
SnapshotCreator snapshot_creator(nullptr, &cold_snapshot_blob);
|
|
649
|
+
Isolate *isolate = snapshot_creator.GetIsolate();
|
|
650
|
+
{
|
|
651
|
+
HandleScope scope(isolate);
|
|
652
|
+
Local<Context> context = Context::New(isolate);
|
|
653
|
+
if (!run_extra_code(isolate, context, warmup_source, "<warm-up>")) {
|
|
654
|
+
return result;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
{
|
|
658
|
+
HandleScope handle_scope(isolate);
|
|
659
|
+
isolate->ContextDisposedNotification(false);
|
|
660
|
+
Local<Context> context = Context::New(isolate);
|
|
661
|
+
snapshot_creator.SetDefaultContext(context);
|
|
662
|
+
}
|
|
663
|
+
result = snapshot_creator.CreateBlob(
|
|
664
|
+
SnapshotCreator::FunctionCodeHandling::kKeep);
|
|
665
|
+
}
|
|
666
|
+
return result;
|
|
667
|
+
}
|
|
668
|
+
|
|
404
669
|
static VALUE rb_snapshot_size(VALUE self, VALUE str) {
|
|
405
670
|
SnapshotInfo* snapshot_info;
|
|
406
671
|
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
@@ -412,9 +677,14 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
|
|
|
412
677
|
SnapshotInfo* snapshot_info;
|
|
413
678
|
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
414
679
|
|
|
680
|
+
if(TYPE(str) != T_STRING) {
|
|
681
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
682
|
+
rb_obj_class(str));
|
|
683
|
+
}
|
|
684
|
+
|
|
415
685
|
init_v8();
|
|
416
686
|
|
|
417
|
-
StartupData startup_data =
|
|
687
|
+
StartupData startup_data = create_snapshot_data_blob(RSTRING_PTR(str));
|
|
418
688
|
|
|
419
689
|
if (startup_data.data == NULL && startup_data.raw_size == 0) {
|
|
420
690
|
rb_raise(rb_eSnapshotError, "Could not create snapshot, most likely the source is incorrect");
|
|
@@ -426,14 +696,26 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
|
|
|
426
696
|
return Qnil;
|
|
427
697
|
}
|
|
428
698
|
|
|
429
|
-
static VALUE
|
|
699
|
+
static VALUE rb_snapshot_dump(VALUE self, VALUE str) {
|
|
700
|
+
SnapshotInfo* snapshot_info;
|
|
701
|
+
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
702
|
+
|
|
703
|
+
return rb_str_new(snapshot_info->data, snapshot_info->raw_size);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
static VALUE rb_snapshot_warmup_unsafe(VALUE self, VALUE str) {
|
|
430
707
|
SnapshotInfo* snapshot_info;
|
|
431
708
|
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
432
709
|
|
|
710
|
+
if(TYPE(str) != T_STRING) {
|
|
711
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
712
|
+
rb_obj_class(str));
|
|
713
|
+
}
|
|
714
|
+
|
|
433
715
|
init_v8();
|
|
434
716
|
|
|
435
717
|
StartupData cold_startup_data = {snapshot_info->data, snapshot_info->raw_size};
|
|
436
|
-
StartupData warm_startup_data =
|
|
718
|
+
StartupData warm_startup_data = warm_up_snapshot_data_blob(cold_startup_data, RSTRING_PTR(str));
|
|
437
719
|
|
|
438
720
|
if (warm_startup_data.data == NULL && warm_startup_data.raw_size == 0) {
|
|
439
721
|
rb_raise(rb_eSnapshotError, "Could not warm up snapshot, most likely the source is incorrect");
|
|
@@ -447,27 +729,16 @@ static VALUE rb_snapshot_warmup(VALUE self, VALUE str) {
|
|
|
447
729
|
return self;
|
|
448
730
|
}
|
|
449
731
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
453
|
-
|
|
454
|
-
init_v8();
|
|
455
|
-
|
|
456
|
-
isolate_info->allocator = new ArrayBufferAllocator();
|
|
457
|
-
isolate_info->interrupted = false;
|
|
458
|
-
isolate_info->refs_count = 1;
|
|
732
|
+
void IsolateInfo::init(SnapshotInfo* snapshot_info) {
|
|
733
|
+
allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
459
734
|
|
|
460
735
|
Isolate::CreateParams create_params;
|
|
461
|
-
create_params.array_buffer_allocator =
|
|
462
|
-
|
|
463
|
-
StartupData* startup_data = NULL;
|
|
464
|
-
if (!NIL_P(snapshot)) {
|
|
465
|
-
SnapshotInfo* snapshot_info;
|
|
466
|
-
Data_Get_Struct(snapshot, SnapshotInfo, snapshot_info);
|
|
736
|
+
create_params.array_buffer_allocator = allocator;
|
|
467
737
|
|
|
738
|
+
if (snapshot_info) {
|
|
468
739
|
int raw_size = snapshot_info->raw_size;
|
|
469
740
|
char* data = new char[raw_size];
|
|
470
|
-
memcpy(data, snapshot_info->data,
|
|
741
|
+
memcpy(data, snapshot_info->data, raw_size);
|
|
471
742
|
|
|
472
743
|
startup_data = new StartupData;
|
|
473
744
|
startup_data->data = data;
|
|
@@ -476,8 +747,22 @@ static VALUE rb_isolate_init_with_snapshot(VALUE self, VALUE snapshot) {
|
|
|
476
747
|
create_params.snapshot_blob = startup_data;
|
|
477
748
|
}
|
|
478
749
|
|
|
479
|
-
|
|
480
|
-
|
|
750
|
+
isolate = Isolate::New(create_params);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
static VALUE rb_isolate_init_with_snapshot(VALUE self, VALUE snapshot) {
|
|
754
|
+
IsolateInfo* isolate_info;
|
|
755
|
+
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
756
|
+
|
|
757
|
+
init_v8();
|
|
758
|
+
|
|
759
|
+
SnapshotInfo* snapshot_info = nullptr;
|
|
760
|
+
if (!NIL_P(snapshot)) {
|
|
761
|
+
Data_Get_Struct(snapshot, SnapshotInfo, snapshot_info);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
isolate_info->init(snapshot_info);
|
|
765
|
+
isolate_info->hold();
|
|
481
766
|
|
|
482
767
|
return Qnil;
|
|
483
768
|
}
|
|
@@ -486,30 +771,70 @@ static VALUE rb_isolate_idle_notification(VALUE self, VALUE idle_time_in_ms) {
|
|
|
486
771
|
IsolateInfo* isolate_info;
|
|
487
772
|
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
488
773
|
|
|
489
|
-
|
|
774
|
+
if (current_platform == NULL) return Qfalse;
|
|
775
|
+
|
|
776
|
+
double duration = NUM2DBL(idle_time_in_ms) / 1000.0;
|
|
777
|
+
double now = current_platform->MonotonicallyIncreasingTime();
|
|
778
|
+
return isolate_info->isolate->IdleNotificationDeadline(now + duration) ? Qtrue : Qfalse;
|
|
490
779
|
}
|
|
491
780
|
|
|
492
|
-
static VALUE
|
|
781
|
+
static VALUE rb_isolate_low_memory_notification(VALUE self) {
|
|
782
|
+
IsolateInfo* isolate_info;
|
|
783
|
+
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
784
|
+
|
|
785
|
+
if (current_platform == NULL) return Qfalse;
|
|
786
|
+
|
|
787
|
+
isolate_info->isolate->LowMemoryNotification();
|
|
788
|
+
return Qnil;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
static VALUE rb_isolate_pump_message_loop(VALUE self) {
|
|
792
|
+
IsolateInfo* isolate_info;
|
|
793
|
+
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
794
|
+
|
|
795
|
+
if (current_platform == NULL) return Qfalse;
|
|
796
|
+
|
|
797
|
+
if (platform::PumpMessageLoop(current_platform.get(), isolate_info->isolate)){
|
|
798
|
+
return Qtrue;
|
|
799
|
+
} else {
|
|
800
|
+
return Qfalse;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
static VALUE rb_context_init_unsafe(VALUE self, VALUE isolate, VALUE snap) {
|
|
493
805
|
ContextInfo* context_info;
|
|
494
806
|
Data_Get_Struct(self, ContextInfo, context_info);
|
|
495
807
|
|
|
496
808
|
init_v8();
|
|
497
809
|
|
|
498
810
|
IsolateInfo* isolate_info;
|
|
499
|
-
|
|
811
|
+
|
|
812
|
+
if (NIL_P(isolate) || !rb_obj_is_kind_of(isolate, rb_cIsolate)) {
|
|
813
|
+
isolate_info = new IsolateInfo();
|
|
814
|
+
|
|
815
|
+
SnapshotInfo *snapshot_info = nullptr;
|
|
816
|
+
if (!NIL_P(snap) && rb_obj_is_kind_of(snap, rb_cSnapshot)) {
|
|
817
|
+
Data_Get_Struct(snap, SnapshotInfo, snapshot_info);
|
|
818
|
+
}
|
|
819
|
+
isolate_info->init(snapshot_info);
|
|
820
|
+
} else { // given isolate or snapshot
|
|
821
|
+
Data_Get_Struct(isolate, IsolateInfo, isolate_info);
|
|
822
|
+
}
|
|
500
823
|
|
|
501
824
|
context_info->isolate_info = isolate_info;
|
|
502
|
-
isolate_info->
|
|
825
|
+
isolate_info->hold();
|
|
503
826
|
|
|
504
827
|
{
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
828
|
+
// the ruby lock is needed if this isn't a new isolate
|
|
829
|
+
IsolateInfo::Lock ruby_lock(isolate_info->mutex);
|
|
830
|
+
Locker lock(isolate_info->isolate);
|
|
831
|
+
Isolate::Scope isolate_scope(isolate_info->isolate);
|
|
832
|
+
HandleScope handle_scope(isolate_info->isolate);
|
|
508
833
|
|
|
509
|
-
|
|
834
|
+
Local<Context> context = Context::New(isolate_info->isolate);
|
|
510
835
|
|
|
511
|
-
|
|
512
|
-
|
|
836
|
+
context_info->context = new Persistent<Context>();
|
|
837
|
+
context_info->context->Reset(isolate_info->isolate, context);
|
|
513
838
|
}
|
|
514
839
|
|
|
515
840
|
if (Qnil == rb_cDateTime && rb_funcall(rb_cObject, rb_intern("const_defined?"), 1, rb_str_new2("DateTime")) == Qtrue)
|
|
@@ -520,139 +845,175 @@ static VALUE rb_context_init_with_isolate(VALUE self, VALUE isolate) {
|
|
|
520
845
|
return Qnil;
|
|
521
846
|
}
|
|
522
847
|
|
|
523
|
-
static VALUE
|
|
848
|
+
static VALUE convert_result_to_ruby(VALUE self /* context */,
|
|
849
|
+
EvalResult& result) {
|
|
850
|
+
ContextInfo *context_info;
|
|
851
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
524
852
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
ContextInfo* context_info;
|
|
528
|
-
VALUE result;
|
|
853
|
+
Isolate *isolate = context_info->isolate_info->isolate;
|
|
854
|
+
Persistent<Context> *p_ctx = context_info->context;
|
|
529
855
|
|
|
530
856
|
VALUE message = Qnil;
|
|
531
857
|
VALUE backtrace = Qnil;
|
|
532
|
-
|
|
533
|
-
Data_Get_Struct(self, ContextInfo, context_info);
|
|
534
|
-
Isolate* isolate = context_info->isolate_info->isolate;
|
|
535
|
-
|
|
536
858
|
{
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
859
|
+
Locker lock(isolate);
|
|
860
|
+
if (result.message) {
|
|
861
|
+
message = convert_v8_to_ruby(isolate, *p_ctx, *result.message);
|
|
862
|
+
result.message->Reset();
|
|
863
|
+
delete result.message;
|
|
864
|
+
result.message = nullptr;
|
|
865
|
+
}
|
|
540
866
|
|
|
541
|
-
|
|
542
|
-
|
|
867
|
+
if (result.backtrace) {
|
|
868
|
+
backtrace = convert_v8_to_ruby(isolate, *p_ctx, *result.backtrace);
|
|
869
|
+
result.backtrace->Reset();
|
|
870
|
+
delete result.backtrace;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
543
873
|
|
|
544
|
-
|
|
874
|
+
// NOTE: this is very important, we can not do an rb_raise from within
|
|
875
|
+
// a v8 scope, if we do the scope is never cleaned up properly and we leak
|
|
876
|
+
if (!result.parsed) {
|
|
877
|
+
if(TYPE(message) == T_STRING) {
|
|
878
|
+
rb_raise(rb_eParseError, "%s", RSTRING_PTR(message));
|
|
879
|
+
} else {
|
|
880
|
+
rb_raise(rb_eParseError, "Unknown JavaScript Error during parse");
|
|
881
|
+
}
|
|
882
|
+
}
|
|
545
883
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
884
|
+
if (!result.executed) {
|
|
885
|
+
VALUE ruby_exception = rb_iv_get(self, "@current_exception");
|
|
886
|
+
if (ruby_exception == Qnil) {
|
|
887
|
+
bool mem_softlimit_reached = (bool)isolate->GetData(MEM_SOFTLIMIT_REACHED);
|
|
888
|
+
// If we were terminated or have the memory softlimit flag set
|
|
889
|
+
if (result.terminated || mem_softlimit_reached) {
|
|
890
|
+
ruby_exception = mem_softlimit_reached ? rb_eV8OutOfMemoryError : rb_eScriptTerminatedError;
|
|
891
|
+
} else {
|
|
892
|
+
ruby_exception = rb_eScriptRuntimeError;
|
|
893
|
+
}
|
|
553
894
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
895
|
+
// exception report about what happened
|
|
896
|
+
if (TYPE(backtrace) == T_STRING) {
|
|
897
|
+
rb_raise(ruby_exception, "%s", RSTRING_PTR(backtrace));
|
|
898
|
+
} else if(TYPE(message) == T_STRING) {
|
|
899
|
+
rb_raise(ruby_exception, "%s", RSTRING_PTR(message));
|
|
900
|
+
} else {
|
|
901
|
+
rb_raise(ruby_exception, "Unknown JavaScript Error during execution");
|
|
902
|
+
}
|
|
903
|
+
} else {
|
|
904
|
+
VALUE rb_str = rb_funcall(ruby_exception, rb_intern("to_s"), 0);
|
|
905
|
+
rb_raise(CLASS_OF(ruby_exception), "%s", RSTRING_PTR(rb_str));
|
|
906
|
+
}
|
|
907
|
+
}
|
|
562
908
|
|
|
563
|
-
|
|
564
|
-
eval_result.backtrace = NULL;
|
|
909
|
+
VALUE ret = Qnil;
|
|
565
910
|
|
|
566
|
-
|
|
911
|
+
// New scope for return value
|
|
912
|
+
{
|
|
913
|
+
Locker lock(isolate);
|
|
914
|
+
Isolate::Scope isolate_scope(isolate);
|
|
915
|
+
HandleScope handle_scope(isolate);
|
|
567
916
|
|
|
568
|
-
|
|
569
|
-
Local<Value> tmp = Local<Value>::New(isolate, *eval_result.message);
|
|
570
|
-
message = convert_v8_to_ruby(isolate, tmp);
|
|
571
|
-
eval_result.message->Reset();
|
|
572
|
-
delete eval_result.message;
|
|
573
|
-
}
|
|
917
|
+
Local<Value> tmp = Local<Value>::New(isolate, *result.value);
|
|
574
918
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
919
|
+
if (result.json) {
|
|
920
|
+
Local<String> rstr = tmp->ToString(p_ctx->Get(isolate)).ToLocalChecked();
|
|
921
|
+
VALUE json_string = rb_enc_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(isolate), rb_enc_find("utf-8"));
|
|
922
|
+
ret = rb_funcall(rb_mJSON, rb_intern("parse"), 1, json_string);
|
|
923
|
+
} else {
|
|
924
|
+
ret = convert_v8_to_ruby(isolate, *p_ctx, tmp);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
result.value->Reset();
|
|
928
|
+
delete result.value;
|
|
581
929
|
}
|
|
582
930
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
if(TYPE(message) == T_STRING) {
|
|
587
|
-
rb_raise(rb_eParseError, "%s", RSTRING_PTR(message));
|
|
588
|
-
} else {
|
|
589
|
-
rb_raise(rb_eParseError, "Unknown JavaScript Error during parse");
|
|
590
|
-
}
|
|
931
|
+
if (rb_funcall(ret, rb_intern("class"), 0) == rb_cFailedV8Conversion) {
|
|
932
|
+
// TODO try to recover stack trace from the conversion error
|
|
933
|
+
rb_raise(rb_eScriptRuntimeError, "Error converting JS object to Ruby object");
|
|
591
934
|
}
|
|
592
935
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
936
|
+
|
|
937
|
+
return ret;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
static VALUE rb_context_eval_unsafe(VALUE self, VALUE str, VALUE filename) {
|
|
941
|
+
|
|
942
|
+
EvalParams eval_params;
|
|
943
|
+
EvalResult eval_result;
|
|
944
|
+
ContextInfo* context_info;
|
|
945
|
+
|
|
946
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
947
|
+
Isolate* isolate = context_info->isolate_info->isolate;
|
|
948
|
+
|
|
949
|
+
if(TYPE(str) != T_STRING) {
|
|
950
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
951
|
+
rb_obj_class(str));
|
|
952
|
+
}
|
|
953
|
+
if(filename != Qnil && TYPE(filename) != T_STRING) {
|
|
954
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be nil or a string)",
|
|
955
|
+
rb_obj_class(filename));
|
|
609
956
|
}
|
|
610
957
|
|
|
611
|
-
// New scope for return value
|
|
612
958
|
{
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
Local<Value> tmp = Local<Value>::New(isolate, *eval_result.value);
|
|
618
|
-
|
|
619
|
-
if (eval_result.json) {
|
|
620
|
-
Local<String> rstr = tmp->ToString();
|
|
621
|
-
VALUE json_string = rb_enc_str_new(*String::Utf8Value(rstr), rstr->Utf8Length(), rb_enc_find("utf-8"));
|
|
622
|
-
result = rb_funcall(rb_mJSON, rb_intern("parse"), 1, json_string);
|
|
623
|
-
} else {
|
|
624
|
-
result = convert_v8_to_ruby(isolate, tmp);
|
|
625
|
-
}
|
|
959
|
+
Locker lock(isolate);
|
|
960
|
+
Isolate::Scope isolate_scope(isolate);
|
|
961
|
+
HandleScope handle_scope(isolate);
|
|
626
962
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
}
|
|
963
|
+
Local<String> eval = String::NewFromUtf8(isolate, RSTRING_PTR(str),
|
|
964
|
+
NewStringType::kNormal, (int)RSTRING_LEN(str)).ToLocalChecked();
|
|
630
965
|
|
|
631
|
-
|
|
632
|
-
// TODO try to recover stack trace from the conversion error
|
|
633
|
-
rb_raise(rb_eScriptRuntimeError, "Error converting JS object to Ruby object");
|
|
634
|
-
}
|
|
966
|
+
Local<String> local_filename;
|
|
635
967
|
|
|
968
|
+
if (filename != Qnil) {
|
|
969
|
+
local_filename = String::NewFromUtf8(isolate, RSTRING_PTR(filename),
|
|
970
|
+
NewStringType::kNormal, (int)RSTRING_LEN(filename)).ToLocalChecked();
|
|
971
|
+
eval_params.filename = &local_filename;
|
|
972
|
+
} else {
|
|
973
|
+
eval_params.filename = NULL;
|
|
974
|
+
}
|
|
636
975
|
|
|
637
|
-
|
|
976
|
+
eval_params.context_info = context_info;
|
|
977
|
+
eval_params.eval = &eval;
|
|
978
|
+
eval_params.result = &eval_result;
|
|
979
|
+
eval_params.timeout = 0;
|
|
980
|
+
eval_params.max_memory = 0;
|
|
981
|
+
VALUE timeout = rb_iv_get(self, "@timeout");
|
|
982
|
+
if (timeout != Qnil) {
|
|
983
|
+
eval_params.timeout = (useconds_t)NUM2LONG(timeout);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
VALUE mem_softlimit = rb_iv_get(self, "@max_memory");
|
|
987
|
+
if (mem_softlimit != Qnil) {
|
|
988
|
+
eval_params.max_memory = (size_t)NUM2ULONG(mem_softlimit);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
eval_result.message = NULL;
|
|
992
|
+
eval_result.backtrace = NULL;
|
|
993
|
+
|
|
994
|
+
rb_thread_call_without_gvl(nogvl_context_eval, &eval_params, unblock_eval, &eval_params);
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
return convert_result_to_ruby(self, eval_result);
|
|
638
998
|
}
|
|
639
999
|
|
|
640
1000
|
typedef struct {
|
|
641
1001
|
VALUE callback;
|
|
642
1002
|
int length;
|
|
643
|
-
VALUE
|
|
1003
|
+
VALUE ruby_args;
|
|
644
1004
|
bool failed;
|
|
645
1005
|
} protected_callback_data;
|
|
646
1006
|
|
|
647
|
-
static
|
|
648
|
-
VALUE protected_callback(VALUE rdata) {
|
|
1007
|
+
static VALUE protected_callback(VALUE rdata) {
|
|
649
1008
|
protected_callback_data* data = (protected_callback_data*)rdata;
|
|
650
1009
|
VALUE result;
|
|
651
1010
|
|
|
652
1011
|
if (data->length > 0) {
|
|
653
|
-
|
|
1012
|
+
result = rb_funcall2(data->callback, rb_intern("call"), data->length,
|
|
1013
|
+
RARRAY_PTR(data->ruby_args));
|
|
1014
|
+
RB_GC_GUARD(data->ruby_args);
|
|
654
1015
|
} else {
|
|
655
|
-
|
|
1016
|
+
result = rb_funcall(data->callback, rb_intern("call"), 0);
|
|
656
1017
|
}
|
|
657
1018
|
return result;
|
|
658
1019
|
}
|
|
@@ -668,78 +1029,94 @@ void*
|
|
|
668
1029
|
gvl_ruby_callback(void* data) {
|
|
669
1030
|
|
|
670
1031
|
FunctionCallbackInfo<Value>* args = (FunctionCallbackInfo<Value>*)data;
|
|
671
|
-
VALUE
|
|
1032
|
+
VALUE ruby_args = Qnil;
|
|
672
1033
|
int length = args->Length();
|
|
673
1034
|
VALUE callback;
|
|
674
1035
|
VALUE result;
|
|
675
1036
|
VALUE self;
|
|
1037
|
+
VALUE parent;
|
|
1038
|
+
ContextInfo* context_info;
|
|
676
1039
|
|
|
677
1040
|
{
|
|
678
|
-
|
|
679
|
-
|
|
1041
|
+
HandleScope scope(args->GetIsolate());
|
|
1042
|
+
Local<External> external = Local<External>::Cast(args->Data());
|
|
680
1043
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
callback = rb_iv_get(self, "@callback");
|
|
1044
|
+
self = *(VALUE*)(external->Value());
|
|
1045
|
+
callback = rb_iv_get(self, "@callback");
|
|
684
1046
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
1047
|
+
parent = rb_iv_get(self, "@parent");
|
|
1048
|
+
if (NIL_P(parent) || !rb_obj_is_kind_of(parent, rb_cContext)) {
|
|
1049
|
+
return NULL;
|
|
1050
|
+
}
|
|
688
1051
|
|
|
1052
|
+
Data_Get_Struct(parent, ContextInfo, context_info);
|
|
689
1053
|
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
1054
|
+
if (length > 0) {
|
|
1055
|
+
ruby_args = rb_ary_tmp_new(length);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
for (int i = 0; i < length; i++) {
|
|
1059
|
+
Local<Value> value = ((*args)[i]).As<Value>();
|
|
1060
|
+
VALUE tmp = convert_v8_to_ruby(args->GetIsolate(),
|
|
1061
|
+
*context_info->context, value);
|
|
1062
|
+
rb_ary_push(ruby_args, tmp);
|
|
1063
|
+
}
|
|
694
1064
|
}
|
|
695
1065
|
|
|
696
1066
|
// may raise exception stay clear of handle scope
|
|
697
1067
|
protected_callback_data callback_data;
|
|
698
1068
|
callback_data.length = length;
|
|
699
1069
|
callback_data.callback = callback;
|
|
700
|
-
callback_data.
|
|
1070
|
+
callback_data.ruby_args = ruby_args;
|
|
701
1071
|
callback_data.failed = false;
|
|
702
1072
|
|
|
703
|
-
if ((bool)args->GetIsolate()->GetData(
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
1073
|
+
if ((bool)args->GetIsolate()->GetData(DO_TERMINATE) == true) {
|
|
1074
|
+
args->GetIsolate()->ThrowException(
|
|
1075
|
+
String::NewFromUtf8Literal(args->GetIsolate(),
|
|
1076
|
+
"Terminated execution during transition from Ruby to JS"));
|
|
1077
|
+
args->GetIsolate()->TerminateExecution();
|
|
1078
|
+
if (length > 0) {
|
|
1079
|
+
rb_ary_clear(ruby_args);
|
|
1080
|
+
rb_gc_force_recycle(ruby_args);
|
|
1081
|
+
}
|
|
1082
|
+
return NULL;
|
|
707
1083
|
}
|
|
708
1084
|
|
|
709
1085
|
result = rb_rescue2((VALUE(*)(...))&protected_callback, (VALUE)(&callback_data),
|
|
710
|
-
|
|
1086
|
+
(VALUE(*)(...))&rescue_callback, (VALUE)(&callback_data), rb_eException, (VALUE)0);
|
|
711
1087
|
|
|
712
1088
|
if(callback_data.failed) {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
args->GetIsolate()->ThrowException(String::NewFromUtf8(args->GetIsolate(), "Ruby exception"));
|
|
1089
|
+
rb_iv_set(parent, "@current_exception", result);
|
|
1090
|
+
args->GetIsolate()->ThrowException(String::NewFromUtf8Literal(args->GetIsolate(), "Ruby exception"));
|
|
716
1091
|
}
|
|
717
1092
|
else {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
1093
|
+
HandleScope scope(args->GetIsolate());
|
|
1094
|
+
Handle<Value> v8_result = convert_ruby_to_v8(args->GetIsolate(), context_info->context->Get(args->GetIsolate()), result);
|
|
1095
|
+
args->GetReturnValue().Set(v8_result);
|
|
721
1096
|
}
|
|
722
1097
|
|
|
723
1098
|
if (length > 0) {
|
|
724
|
-
|
|
1099
|
+
rb_ary_clear(ruby_args);
|
|
1100
|
+
rb_gc_force_recycle(ruby_args);
|
|
725
1101
|
}
|
|
726
1102
|
|
|
727
|
-
if ((bool)args->GetIsolate()->GetData(
|
|
1103
|
+
if ((bool)args->GetIsolate()->GetData(DO_TERMINATE) == true) {
|
|
728
1104
|
Isolate* isolate = args->GetIsolate();
|
|
729
|
-
|
|
1105
|
+
isolate->TerminateExecution();
|
|
730
1106
|
}
|
|
731
1107
|
|
|
732
1108
|
return NULL;
|
|
733
1109
|
}
|
|
734
1110
|
|
|
735
1111
|
static void ruby_callback(const FunctionCallbackInfo<Value>& args) {
|
|
736
|
-
|
|
737
|
-
bool has_gvl = (bool)args.GetIsolate()->GetData(0);
|
|
1112
|
+
bool has_gvl = (bool)args.GetIsolate()->GetData(IN_GVL);
|
|
738
1113
|
|
|
739
1114
|
if(has_gvl) {
|
|
740
|
-
|
|
1115
|
+
gvl_ruby_callback((void*)&args);
|
|
741
1116
|
} else {
|
|
742
|
-
|
|
1117
|
+
args.GetIsolate()->SetData(IN_GVL, (void*)true);
|
|
1118
|
+
rb_thread_call_with_gvl(gvl_ruby_callback, (void*)(&args));
|
|
1119
|
+
args.GetIsolate()->SetData(IN_GVL, (void*)false);
|
|
743
1120
|
}
|
|
744
1121
|
}
|
|
745
1122
|
|
|
@@ -760,180 +1137,227 @@ static VALUE rb_external_function_notify_v8(VALUE self) {
|
|
|
760
1137
|
Isolate* isolate = context_info->isolate_info->isolate;
|
|
761
1138
|
|
|
762
1139
|
{
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1140
|
+
Locker lock(isolate);
|
|
1141
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1142
|
+
HandleScope handle_scope(isolate);
|
|
1143
|
+
|
|
1144
|
+
Local<Context> context = context_info->context->Get(isolate);
|
|
1145
|
+
Context::Scope context_scope(context);
|
|
1146
|
+
|
|
1147
|
+
Local<String> v8_str =
|
|
1148
|
+
String::NewFromUtf8(isolate, RSTRING_PTR(name),
|
|
1149
|
+
NewStringType::kNormal, (int)RSTRING_LEN(name))
|
|
1150
|
+
.ToLocalChecked();
|
|
1151
|
+
|
|
1152
|
+
// copy self so we can access from v8 external
|
|
1153
|
+
VALUE* self_copy;
|
|
1154
|
+
Data_Get_Struct(self, VALUE, self_copy);
|
|
1155
|
+
*self_copy = self;
|
|
1156
|
+
|
|
1157
|
+
Local<Value> external = External::New(isolate, self_copy);
|
|
1158
|
+
|
|
1159
|
+
if (parent_object == Qnil) {
|
|
1160
|
+
context->Global()->Set(
|
|
1161
|
+
context,
|
|
1162
|
+
v8_str,
|
|
1163
|
+
FunctionTemplate::New(isolate, ruby_callback, external)
|
|
1164
|
+
->GetFunction(context)
|
|
1165
|
+
.ToLocalChecked());
|
|
1166
|
+
|
|
1167
|
+
} else {
|
|
1168
|
+
Local<String> eval =
|
|
1169
|
+
String::NewFromUtf8(isolate, RSTRING_PTR(parent_object_eval),
|
|
1170
|
+
NewStringType::kNormal,
|
|
1171
|
+
(int)RSTRING_LEN(parent_object_eval))
|
|
1172
|
+
.ToLocalChecked();
|
|
1173
|
+
|
|
1174
|
+
MaybeLocal<Script> parsed_script = Script::Compile(context, eval);
|
|
1175
|
+
if (parsed_script.IsEmpty()) {
|
|
1176
|
+
parse_error = true;
|
|
1177
|
+
} else {
|
|
1178
|
+
MaybeLocal<Value> maybe_value =
|
|
1179
|
+
parsed_script.ToLocalChecked()->Run(context);
|
|
1180
|
+
attach_error = true;
|
|
1181
|
+
|
|
1182
|
+
if (!maybe_value.IsEmpty()) {
|
|
1183
|
+
Local<Value> value = maybe_value.ToLocalChecked();
|
|
1184
|
+
if (value->IsObject()) {
|
|
1185
|
+
value.As<Object>()->Set(
|
|
1186
|
+
context,
|
|
1187
|
+
v8_str,
|
|
1188
|
+
FunctionTemplate::New(isolate, ruby_callback, external)
|
|
1189
|
+
->GetFunction(context)
|
|
1190
|
+
.ToLocalChecked());
|
|
1191
|
+
attach_error = false;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
803
1196
|
}
|
|
804
1197
|
|
|
805
1198
|
// always raise out of V8 context
|
|
806
1199
|
if (parse_error) {
|
|
807
|
-
|
|
1200
|
+
rb_raise(rb_eParseError, "Invalid object %" PRIsVALUE, parent_object);
|
|
808
1201
|
}
|
|
809
1202
|
|
|
810
1203
|
if (attach_error) {
|
|
811
|
-
|
|
1204
|
+
rb_raise(rb_eParseError, "Was expecting %" PRIsVALUE" to be an object", parent_object);
|
|
812
1205
|
}
|
|
813
1206
|
|
|
814
1207
|
return Qnil;
|
|
815
1208
|
}
|
|
816
1209
|
|
|
817
|
-
|
|
1210
|
+
static VALUE rb_context_isolate_mutex(VALUE self) {
|
|
1211
|
+
ContextInfo* context_info;
|
|
1212
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
818
1213
|
|
|
819
|
-
if (isolate_info
|
|
820
|
-
|
|
1214
|
+
if (!context_info->isolate_info) {
|
|
1215
|
+
rb_raise(rb_eScriptRuntimeError, "Context has no Isolate available anymore");
|
|
821
1216
|
}
|
|
822
1217
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
fprintf(stderr, "WARNING: V8 isolate was interrupted by Ruby, it can not be disposed and memory will not be reclaimed till the Ruby process exits.\n");
|
|
826
|
-
} else {
|
|
1218
|
+
return context_info->isolate_info->mutex;
|
|
1219
|
+
}
|
|
827
1220
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
1221
|
+
IsolateInfo::~IsolateInfo() {
|
|
1222
|
+
if (isolate) {
|
|
1223
|
+
if (this->interrupted) {
|
|
1224
|
+
fprintf(stderr, "WARNING: V8 isolate was interrupted by Ruby, "
|
|
1225
|
+
"it can not be disposed and memory will not be "
|
|
1226
|
+
"reclaimed till the Ruby process exits.\n");
|
|
1227
|
+
} else {
|
|
1228
|
+
if (this->pid != getpid() && !single_threaded) {
|
|
1229
|
+
fprintf(stderr, "WARNING: V8 isolate was forked, "
|
|
1230
|
+
"it can not be disposed and "
|
|
1231
|
+
"memory will not be reclaimed "
|
|
1232
|
+
"till the Ruby process exits.\n"
|
|
1233
|
+
"It is VERY likely your process will hang.\n"
|
|
1234
|
+
"If you wish to use v8 in forked environment "
|
|
1235
|
+
"please ensure the platform is initialized with:\n"
|
|
1236
|
+
"MiniRacer::Platform.set_flags! :single_threaded\n"
|
|
1237
|
+
);
|
|
1238
|
+
} else {
|
|
1239
|
+
isolate->Dispose();
|
|
1240
|
+
}
|
|
833
1241
|
}
|
|
834
|
-
|
|
1242
|
+
isolate = nullptr;
|
|
835
1243
|
}
|
|
836
1244
|
|
|
837
|
-
if (
|
|
838
|
-
delete[]
|
|
839
|
-
delete
|
|
1245
|
+
if (startup_data) {
|
|
1246
|
+
delete[] startup_data->data;
|
|
1247
|
+
delete startup_data;
|
|
840
1248
|
}
|
|
841
1249
|
|
|
842
|
-
delete
|
|
843
|
-
isolate_info->disposed = true;
|
|
1250
|
+
delete allocator;
|
|
844
1251
|
}
|
|
845
1252
|
|
|
846
|
-
void
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1253
|
+
static void free_context_raw(void *arg) {
|
|
1254
|
+
ContextInfo* context_info = (ContextInfo*)arg;
|
|
1255
|
+
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
1256
|
+
Persistent<Context>* context = context_info->context;
|
|
1257
|
+
|
|
1258
|
+
if (context && isolate_info && isolate_info->isolate) {
|
|
1259
|
+
Locker lock(isolate_info->isolate);
|
|
1260
|
+
v8::Isolate::Scope isolate_scope(isolate_info->isolate);
|
|
1261
|
+
context->Reset();
|
|
1262
|
+
delete context;
|
|
854
1263
|
}
|
|
855
1264
|
|
|
856
|
-
|
|
1265
|
+
if (isolate_info) {
|
|
1266
|
+
isolate_info->release();
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
xfree(context_info);
|
|
857
1270
|
}
|
|
858
1271
|
|
|
1272
|
+
static void *free_context_thr(void* arg) {
|
|
1273
|
+
if (pthread_rwlock_tryrdlock(&exit_lock) != 0) {
|
|
1274
|
+
return NULL;
|
|
1275
|
+
}
|
|
1276
|
+
if (ruby_exiting) {
|
|
1277
|
+
return NULL;
|
|
1278
|
+
}
|
|
859
1279
|
|
|
860
|
-
|
|
1280
|
+
free_context_raw(arg);
|
|
861
1281
|
|
|
862
|
-
|
|
863
|
-
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
1282
|
+
pthread_rwlock_unlock(&exit_lock);
|
|
864
1283
|
|
|
865
|
-
|
|
866
|
-
Locker lock(isolate_info->isolate);
|
|
867
|
-
v8::Isolate::Scope isolate_scope(isolate_info->isolate);
|
|
868
|
-
context_info->context->Reset();
|
|
869
|
-
delete context_info->context;
|
|
870
|
-
context_info->context = NULL;
|
|
871
|
-
}
|
|
1284
|
+
return NULL;
|
|
872
1285
|
}
|
|
873
1286
|
|
|
874
|
-
|
|
1287
|
+
// destroys everything except freeing the ContextInfo struct (see deallocate())
|
|
1288
|
+
static void free_context(ContextInfo* context_info) {
|
|
875
1289
|
|
|
876
|
-
ContextInfo* context_info = (ContextInfo*)data;
|
|
877
1290
|
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
878
1291
|
|
|
879
|
-
|
|
1292
|
+
ContextInfo* context_info_copy = ALLOC(ContextInfo);
|
|
1293
|
+
context_info_copy->isolate_info = context_info->isolate_info;
|
|
1294
|
+
context_info_copy->context = context_info->context;
|
|
880
1295
|
|
|
881
|
-
if (isolate_info) {
|
|
882
|
-
|
|
883
|
-
|
|
1296
|
+
if (isolate_info && isolate_info->refs() > 1) {
|
|
1297
|
+
pthread_t free_context_thread;
|
|
1298
|
+
if (pthread_create(&free_context_thread, thread_attr_p,
|
|
1299
|
+
free_context_thr, (void*)context_info_copy)) {
|
|
1300
|
+
fprintf(stderr, "WARNING failed to release memory in MiniRacer, thread to release could not be created, process will leak memory\n");
|
|
1301
|
+
}
|
|
1302
|
+
} else {
|
|
1303
|
+
free_context_raw(context_info_copy);
|
|
884
1304
|
}
|
|
1305
|
+
|
|
1306
|
+
context_info->context = NULL;
|
|
1307
|
+
context_info->isolate_info = NULL;
|
|
885
1308
|
}
|
|
886
1309
|
|
|
887
|
-
void deallocate_isolate(void* data) {
|
|
1310
|
+
static void deallocate_isolate(void* data) {
|
|
888
1311
|
|
|
889
1312
|
IsolateInfo* isolate_info = (IsolateInfo*) data;
|
|
890
1313
|
|
|
891
|
-
isolate_info->
|
|
892
|
-
|
|
1314
|
+
isolate_info->release();
|
|
1315
|
+
}
|
|
893
1316
|
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1317
|
+
static void mark_isolate(void* data) {
|
|
1318
|
+
IsolateInfo* isolate_info = (IsolateInfo*) data;
|
|
1319
|
+
isolate_info->mark();
|
|
897
1320
|
}
|
|
898
1321
|
|
|
899
|
-
void deallocate(void* data) {
|
|
1322
|
+
static void deallocate(void* data) {
|
|
900
1323
|
ContextInfo* context_info = (ContextInfo*)data;
|
|
901
|
-
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
902
|
-
|
|
903
|
-
deallocate_context(data);
|
|
904
1324
|
|
|
905
|
-
|
|
906
|
-
xfree(isolate_info);
|
|
907
|
-
}
|
|
1325
|
+
free_context(context_info);
|
|
908
1326
|
|
|
909
1327
|
xfree(data);
|
|
910
1328
|
}
|
|
911
1329
|
|
|
1330
|
+
static void mark_context(void* data) {
|
|
1331
|
+
ContextInfo* context_info = (ContextInfo*)data;
|
|
1332
|
+
if (context_info->isolate_info) {
|
|
1333
|
+
context_info->isolate_info->mark();
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
912
1336
|
|
|
913
|
-
void deallocate_external_function(void * data) {
|
|
1337
|
+
static void deallocate_external_function(void * data) {
|
|
914
1338
|
xfree(data);
|
|
915
1339
|
}
|
|
916
1340
|
|
|
917
|
-
void deallocate_snapshot(void * data) {
|
|
1341
|
+
static void deallocate_snapshot(void * data) {
|
|
918
1342
|
SnapshotInfo* snapshot_info = (SnapshotInfo*)data;
|
|
919
1343
|
delete[] snapshot_info->data;
|
|
920
1344
|
xfree(snapshot_info);
|
|
921
1345
|
}
|
|
922
1346
|
|
|
923
|
-
VALUE allocate_external_function(VALUE klass) {
|
|
1347
|
+
static VALUE allocate_external_function(VALUE klass) {
|
|
924
1348
|
VALUE* self = ALLOC(VALUE);
|
|
925
1349
|
return Data_Wrap_Struct(klass, NULL, deallocate_external_function, (void*)self);
|
|
926
1350
|
}
|
|
927
1351
|
|
|
928
|
-
VALUE allocate(VALUE klass) {
|
|
1352
|
+
static VALUE allocate(VALUE klass) {
|
|
929
1353
|
ContextInfo* context_info = ALLOC(ContextInfo);
|
|
930
1354
|
context_info->isolate_info = NULL;
|
|
931
1355
|
context_info->context = NULL;
|
|
932
1356
|
|
|
933
|
-
return Data_Wrap_Struct(klass,
|
|
1357
|
+
return Data_Wrap_Struct(klass, mark_context, deallocate, (void*)context_info);
|
|
934
1358
|
}
|
|
935
1359
|
|
|
936
|
-
VALUE allocate_snapshot(VALUE klass) {
|
|
1360
|
+
static VALUE allocate_snapshot(VALUE klass) {
|
|
937
1361
|
SnapshotInfo* snapshot_info = ALLOC(SnapshotInfo);
|
|
938
1362
|
snapshot_info->data = NULL;
|
|
939
1363
|
snapshot_info->raw_size = 0;
|
|
@@ -941,18 +1365,10 @@ VALUE allocate_snapshot(VALUE klass) {
|
|
|
941
1365
|
return Data_Wrap_Struct(klass, NULL, deallocate_snapshot, (void*)snapshot_info);
|
|
942
1366
|
}
|
|
943
1367
|
|
|
944
|
-
VALUE allocate_isolate(VALUE klass) {
|
|
945
|
-
IsolateInfo* isolate_info =
|
|
946
|
-
|
|
947
|
-
isolate_info->isolate = NULL;
|
|
948
|
-
isolate_info->allocator = NULL;
|
|
949
|
-
isolate_info->startup_data = NULL;
|
|
950
|
-
isolate_info->interrupted = false;
|
|
951
|
-
isolate_info->refs_count = 0;
|
|
952
|
-
isolate_info->pid = getpid();
|
|
953
|
-
isolate_info->disposed = false;
|
|
1368
|
+
static VALUE allocate_isolate(VALUE klass) {
|
|
1369
|
+
IsolateInfo* isolate_info = new IsolateInfo();
|
|
954
1370
|
|
|
955
|
-
return Data_Wrap_Struct(klass,
|
|
1371
|
+
return Data_Wrap_Struct(klass, mark_isolate, deallocate_isolate, (void*)isolate_info);
|
|
956
1372
|
}
|
|
957
1373
|
|
|
958
1374
|
static VALUE
|
|
@@ -969,25 +1385,90 @@ rb_heap_stats(VALUE self) {
|
|
|
969
1385
|
|
|
970
1386
|
if (!isolate) {
|
|
971
1387
|
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1388
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_physical_size")), ULONG2NUM(0));
|
|
1389
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_heap_size_executable")), ULONG2NUM(0));
|
|
1390
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_heap_size")), ULONG2NUM(0));
|
|
1391
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("used_heap_size")), ULONG2NUM(0));
|
|
1392
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("heap_size_limit")), ULONG2NUM(0));
|
|
977
1393
|
|
|
978
1394
|
} else {
|
|
979
|
-
|
|
1395
|
+
isolate->GetHeapStatistics(&stats);
|
|
980
1396
|
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1397
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_physical_size")), ULONG2NUM(stats.total_physical_size()));
|
|
1398
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_heap_size_executable")), ULONG2NUM(stats.total_heap_size_executable()));
|
|
1399
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("total_heap_size")), ULONG2NUM(stats.total_heap_size()));
|
|
1400
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("used_heap_size")), ULONG2NUM(stats.used_heap_size()));
|
|
1401
|
+
rb_hash_aset(rval, ID2SYM(rb_intern("heap_size_limit")), ULONG2NUM(stats.heap_size_limit()));
|
|
986
1402
|
}
|
|
987
1403
|
|
|
988
1404
|
return rval;
|
|
989
1405
|
}
|
|
990
1406
|
|
|
1407
|
+
// https://github.com/bnoordhuis/node-heapdump/blob/master/src/heapdump.cc
|
|
1408
|
+
class FileOutputStream : public OutputStream {
|
|
1409
|
+
public:
|
|
1410
|
+
FileOutputStream(FILE* stream) : stream_(stream) {}
|
|
1411
|
+
|
|
1412
|
+
virtual int GetChunkSize() {
|
|
1413
|
+
return 65536;
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
virtual void EndOfStream() {}
|
|
1417
|
+
|
|
1418
|
+
virtual WriteResult WriteAsciiChunk(char* data, int size) {
|
|
1419
|
+
const size_t len = static_cast<size_t>(size);
|
|
1420
|
+
size_t off = 0;
|
|
1421
|
+
|
|
1422
|
+
while (off < len && !feof(stream_) && !ferror(stream_))
|
|
1423
|
+
off += fwrite(data + off, 1, len - off, stream_);
|
|
1424
|
+
|
|
1425
|
+
return off == len ? kContinue : kAbort;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
private:
|
|
1429
|
+
FILE* stream_;
|
|
1430
|
+
};
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
static VALUE
|
|
1434
|
+
rb_heap_snapshot(VALUE self, VALUE file) {
|
|
1435
|
+
|
|
1436
|
+
rb_io_t *fptr;
|
|
1437
|
+
|
|
1438
|
+
fptr = RFILE(file)->fptr;
|
|
1439
|
+
|
|
1440
|
+
if (!fptr) return Qfalse;
|
|
1441
|
+
|
|
1442
|
+
FILE* fp;
|
|
1443
|
+
fp = fdopen(fptr->fd, "w");
|
|
1444
|
+
if (fp == NULL) return Qfalse;
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
ContextInfo* context_info;
|
|
1448
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1449
|
+
Isolate* isolate;
|
|
1450
|
+
isolate = context_info->isolate_info ? context_info->isolate_info->isolate : NULL;
|
|
1451
|
+
|
|
1452
|
+
if (!isolate) return Qfalse;
|
|
1453
|
+
|
|
1454
|
+
Locker lock(isolate);
|
|
1455
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1456
|
+
HandleScope handle_scope(isolate);
|
|
1457
|
+
|
|
1458
|
+
HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
|
|
1459
|
+
|
|
1460
|
+
const HeapSnapshot* const snap = heap_profiler->TakeHeapSnapshot();
|
|
1461
|
+
|
|
1462
|
+
FileOutputStream stream(fp);
|
|
1463
|
+
snap->Serialize(&stream, HeapSnapshot::kJSON);
|
|
1464
|
+
|
|
1465
|
+
fflush(fp);
|
|
1466
|
+
|
|
1467
|
+
const_cast<HeapSnapshot*>(snap)->Delete();
|
|
1468
|
+
|
|
1469
|
+
return Qtrue;
|
|
1470
|
+
}
|
|
1471
|
+
|
|
991
1472
|
static VALUE
|
|
992
1473
|
rb_context_stop(VALUE self) {
|
|
993
1474
|
|
|
@@ -997,9 +1478,9 @@ rb_context_stop(VALUE self) {
|
|
|
997
1478
|
Isolate* isolate = context_info->isolate_info->isolate;
|
|
998
1479
|
|
|
999
1480
|
// flag for termination
|
|
1000
|
-
isolate->SetData(
|
|
1481
|
+
isolate->SetData(DO_TERMINATE, (void*)true);
|
|
1001
1482
|
|
|
1002
|
-
|
|
1483
|
+
isolate->TerminateExecution();
|
|
1003
1484
|
rb_funcall(self, rb_intern("stop_attached"), 0);
|
|
1004
1485
|
|
|
1005
1486
|
return Qnil;
|
|
@@ -1013,60 +1494,235 @@ rb_context_dispose(VALUE self) {
|
|
|
1013
1494
|
|
|
1014
1495
|
free_context(context_info);
|
|
1015
1496
|
|
|
1016
|
-
if (context_info->isolate_info && context_info->isolate_info->refs_count == 2) {
|
|
1017
|
-
// special case, we only have isolate + context so we can burn the
|
|
1018
|
-
// isolate as well
|
|
1019
|
-
free_isolate(context_info->isolate_info);
|
|
1020
|
-
}
|
|
1021
1497
|
return Qnil;
|
|
1022
1498
|
}
|
|
1023
1499
|
|
|
1024
|
-
|
|
1500
|
+
static void*
|
|
1501
|
+
nogvl_context_call(void *args) {
|
|
1502
|
+
|
|
1503
|
+
FunctionCall *call = (FunctionCall *) args;
|
|
1504
|
+
if (!call) {
|
|
1505
|
+
return NULL;
|
|
1506
|
+
}
|
|
1507
|
+
IsolateInfo *isolate_info = call->context_info->isolate_info;
|
|
1508
|
+
Isolate* isolate = isolate_info->isolate;
|
|
1509
|
+
|
|
1510
|
+
// in gvl flag
|
|
1511
|
+
isolate->SetData(IN_GVL, (void*)false);
|
|
1512
|
+
// terminate ASAP
|
|
1513
|
+
isolate->SetData(DO_TERMINATE, (void*)false);
|
|
1514
|
+
|
|
1515
|
+
if (call->max_memory > 0) {
|
|
1516
|
+
isolate->SetData(MEM_SOFTLIMIT_VALUE, &call->max_memory);
|
|
1517
|
+
isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)false);
|
|
1518
|
+
if (!isolate_info->added_gc_cb) {
|
|
1519
|
+
isolate->AddGCEpilogueCallback(gc_callback);
|
|
1520
|
+
isolate_info->added_gc_cb = true;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1525
|
+
EscapableHandleScope handle_scope(isolate);
|
|
1526
|
+
TryCatch trycatch(isolate);
|
|
1527
|
+
|
|
1528
|
+
Local<Context> context = call->context_info->context->Get(isolate);
|
|
1529
|
+
Context::Scope context_scope(context);
|
|
1530
|
+
|
|
1531
|
+
Local<Function> fun = call->fun;
|
|
1532
|
+
|
|
1533
|
+
EvalResult& eval_res = call->result;
|
|
1534
|
+
eval_res.parsed = true;
|
|
1535
|
+
|
|
1536
|
+
MaybeLocal<v8::Value> res = fun->Call(context, context->Global(), call->argc, call->argv);
|
|
1537
|
+
prepare_result(res, trycatch, isolate, context, eval_res);
|
|
1538
|
+
|
|
1539
|
+
isolate->SetData(IN_GVL, (void*)true);
|
|
1540
|
+
|
|
1541
|
+
return NULL;
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
static void unblock_function(void *args) {
|
|
1545
|
+
FunctionCall *call = (FunctionCall *) args;
|
|
1546
|
+
call->context_info->isolate_info->interrupted = true;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
static VALUE rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
|
|
1550
|
+
ContextInfo* context_info;
|
|
1551
|
+
FunctionCall call;
|
|
1552
|
+
VALUE *call_argv = NULL;
|
|
1553
|
+
|
|
1554
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1555
|
+
Isolate* isolate = context_info->isolate_info->isolate;
|
|
1556
|
+
|
|
1557
|
+
if (argc < 1) {
|
|
1558
|
+
rb_raise(rb_eArgError, "need at least one argument %d", argc);
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
VALUE function_name = argv[0];
|
|
1562
|
+
if (TYPE(function_name) != T_STRING) {
|
|
1563
|
+
rb_raise(rb_eTypeError, "first argument should be a String");
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
char *fname = RSTRING_PTR(function_name);
|
|
1567
|
+
if (!fname) {
|
|
1568
|
+
return Qnil;
|
|
1569
|
+
}
|
|
1025
1570
|
|
|
1026
|
-
|
|
1571
|
+
call.context_info = context_info;
|
|
1572
|
+
call.error = false;
|
|
1573
|
+
call.function_name = fname;
|
|
1574
|
+
call.argc = argc - 1;
|
|
1575
|
+
call.argv = NULL;
|
|
1576
|
+
if (call.argc > 0) {
|
|
1577
|
+
// skip first argument which is the function name
|
|
1578
|
+
call_argv = argv + 1;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
call.max_memory = 0;
|
|
1582
|
+
VALUE mem_softlimit = rb_iv_get(self, "@max_memory");
|
|
1583
|
+
if (mem_softlimit != Qnil) {
|
|
1584
|
+
unsigned long sl_int = NUM2ULONG(mem_softlimit);
|
|
1585
|
+
call.max_memory = (size_t)sl_int;
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
bool missingFunction = false;
|
|
1027
1589
|
{
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1590
|
+
Locker lock(isolate);
|
|
1591
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1592
|
+
HandleScope handle_scope(isolate);
|
|
1593
|
+
|
|
1594
|
+
Local<Context> context = context_info->context->Get(isolate);
|
|
1595
|
+
Context::Scope context_scope(context);
|
|
1596
|
+
|
|
1597
|
+
// examples of such usage can be found in
|
|
1598
|
+
// https://github.com/v8/v8/blob/36b32aa28db5e993312f4588d60aad5c8330c8a5/test/cctest/test-api.cc#L15711
|
|
1599
|
+
MaybeLocal<String> fname = String::NewFromUtf8(isolate, call.function_name);
|
|
1600
|
+
MaybeLocal<v8::Value> val;
|
|
1601
|
+
if (!fname.IsEmpty()) {
|
|
1602
|
+
val = context->Global()->Get(context, fname.ToLocalChecked());
|
|
1603
|
+
}
|
|
1033
1604
|
|
|
1034
|
-
|
|
1605
|
+
if (val.IsEmpty() || !val.ToLocalChecked()->IsFunction()) {
|
|
1606
|
+
missingFunction = true;
|
|
1607
|
+
} else {
|
|
1035
1608
|
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1609
|
+
Local<v8::Function> fun = Local<v8::Function>::Cast(val.ToLocalChecked());
|
|
1610
|
+
call.fun = fun;
|
|
1611
|
+
int fun_argc = call.argc;
|
|
1612
|
+
|
|
1613
|
+
if (fun_argc > 0) {
|
|
1614
|
+
call.argv = (v8::Local<Value> *) malloc(sizeof(void *) * fun_argc);
|
|
1615
|
+
if (!call.argv) {
|
|
1616
|
+
return Qnil;
|
|
1617
|
+
}
|
|
1618
|
+
for(int i=0; i < fun_argc; i++) {
|
|
1619
|
+
call.argv[i] = convert_ruby_to_v8(isolate, context, call_argv[i]);
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
rb_thread_call_without_gvl(nogvl_context_call, &call, unblock_function, &call);
|
|
1623
|
+
free(call.argv);
|
|
1040
1624
|
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
rb_ePlatformAlreadyInitializedError = rb_define_class_under(rb_mMiniRacer, "PlatformAlreadyInitialized", rb_eError);
|
|
1044
|
-
rb_cFailedV8Conversion = rb_define_class_under(rb_mMiniRacer, "FailedV8Conversion", rb_cObject);
|
|
1045
|
-
rb_mJSON = rb_define_module("JSON");
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1046
1627
|
|
|
1047
|
-
|
|
1628
|
+
if (missingFunction) {
|
|
1629
|
+
rb_raise(rb_eScriptRuntimeError, "Unknown JavaScript method invoked");
|
|
1630
|
+
}
|
|
1048
1631
|
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
rb_define_method(rb_cContext, "heap_stats", (VALUE(*)(...))&rb_heap_stats, 0);
|
|
1632
|
+
return convert_result_to_ruby(self, call.result);
|
|
1633
|
+
}
|
|
1052
1634
|
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1635
|
+
static VALUE rb_context_create_isolate_value(VALUE self) {
|
|
1636
|
+
ContextInfo* context_info;
|
|
1637
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1638
|
+
IsolateInfo *isolate_info = context_info->isolate_info;
|
|
1639
|
+
|
|
1640
|
+
if (!isolate_info) {
|
|
1641
|
+
return Qnil;
|
|
1642
|
+
}
|
|
1056
1643
|
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
rb_define_alloc_func(rb_cExternalFunction, allocate_external_function);
|
|
1644
|
+
isolate_info->hold();
|
|
1645
|
+
return Data_Wrap_Struct(rb_cIsolate, NULL, &deallocate_isolate, isolate_info);
|
|
1646
|
+
}
|
|
1061
1647
|
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
rb_define_private_method(rb_cSnapshot, "load", (VALUE(*)(...))&rb_snapshot_load, 1);
|
|
1648
|
+
static void set_ruby_exiting(VALUE value) {
|
|
1649
|
+
(void)value;
|
|
1065
1650
|
|
|
1066
|
-
|
|
1067
|
-
rb_define_private_method(rb_cIsolate, "init_with_snapshot",(VALUE(*)(...))&rb_isolate_init_with_snapshot, 1);
|
|
1651
|
+
int res = pthread_rwlock_wrlock(&exit_lock);
|
|
1068
1652
|
|
|
1069
|
-
|
|
1653
|
+
ruby_exiting = true;
|
|
1654
|
+
if (res == 0) {
|
|
1655
|
+
pthread_rwlock_unlock(&exit_lock);
|
|
1070
1656
|
}
|
|
1657
|
+
}
|
|
1071
1658
|
|
|
1659
|
+
extern "C" {
|
|
1660
|
+
|
|
1661
|
+
__attribute__((visibility("default"))) void Init_mini_racer_extension ( void )
|
|
1662
|
+
{
|
|
1663
|
+
VALUE rb_mMiniRacer = rb_define_module("MiniRacer");
|
|
1664
|
+
rb_cContext = rb_define_class_under(rb_mMiniRacer, "Context", rb_cObject);
|
|
1665
|
+
rb_cSnapshot = rb_define_class_under(rb_mMiniRacer, "Snapshot", rb_cObject);
|
|
1666
|
+
rb_cIsolate = rb_define_class_under(rb_mMiniRacer, "Isolate", rb_cObject);
|
|
1667
|
+
VALUE rb_cPlatform = rb_define_class_under(rb_mMiniRacer, "Platform", rb_cObject);
|
|
1668
|
+
|
|
1669
|
+
VALUE rb_eError = rb_define_class_under(rb_mMiniRacer, "Error", rb_eStandardError);
|
|
1670
|
+
|
|
1671
|
+
VALUE rb_eEvalError = rb_define_class_under(rb_mMiniRacer, "EvalError", rb_eError);
|
|
1672
|
+
rb_eScriptTerminatedError = rb_define_class_under(rb_mMiniRacer, "ScriptTerminatedError", rb_eEvalError);
|
|
1673
|
+
rb_eV8OutOfMemoryError = rb_define_class_under(rb_mMiniRacer, "V8OutOfMemoryError", rb_eEvalError);
|
|
1674
|
+
rb_eParseError = rb_define_class_under(rb_mMiniRacer, "ParseError", rb_eEvalError);
|
|
1675
|
+
rb_eScriptRuntimeError = rb_define_class_under(rb_mMiniRacer, "RuntimeError", rb_eEvalError);
|
|
1676
|
+
|
|
1677
|
+
rb_cJavaScriptFunction = rb_define_class_under(rb_mMiniRacer, "JavaScriptFunction", rb_cObject);
|
|
1678
|
+
rb_eSnapshotError = rb_define_class_under(rb_mMiniRacer, "SnapshotError", rb_eError);
|
|
1679
|
+
rb_ePlatformAlreadyInitializedError = rb_define_class_under(rb_mMiniRacer, "PlatformAlreadyInitialized", rb_eError);
|
|
1680
|
+
rb_cFailedV8Conversion = rb_define_class_under(rb_mMiniRacer, "FailedV8Conversion", rb_cObject);
|
|
1681
|
+
rb_mJSON = rb_define_module("JSON");
|
|
1682
|
+
|
|
1683
|
+
VALUE rb_cExternalFunction = rb_define_class_under(rb_cContext, "ExternalFunction", rb_cObject);
|
|
1684
|
+
|
|
1685
|
+
rb_define_method(rb_cContext, "stop", (VALUE(*)(...))&rb_context_stop, 0);
|
|
1686
|
+
rb_define_method(rb_cContext, "dispose_unsafe", (VALUE(*)(...))&rb_context_dispose, 0);
|
|
1687
|
+
rb_define_method(rb_cContext, "heap_stats", (VALUE(*)(...))&rb_heap_stats, 0);
|
|
1688
|
+
rb_define_method(rb_cContext, "write_heap_snapshot_unsafe", (VALUE(*)(...))&rb_heap_snapshot, 1);
|
|
1689
|
+
|
|
1690
|
+
rb_define_private_method(rb_cContext, "create_isolate_value",(VALUE(*)(...))&rb_context_create_isolate_value, 0);
|
|
1691
|
+
rb_define_private_method(rb_cContext, "eval_unsafe",(VALUE(*)(...))&rb_context_eval_unsafe, 2);
|
|
1692
|
+
rb_define_private_method(rb_cContext, "call_unsafe", (VALUE(*)(...))&rb_context_call_unsafe, -1);
|
|
1693
|
+
rb_define_private_method(rb_cContext, "isolate_mutex", (VALUE(*)(...))&rb_context_isolate_mutex, 0);
|
|
1694
|
+
rb_define_private_method(rb_cContext, "init_unsafe",(VALUE(*)(...))&rb_context_init_unsafe, 2);
|
|
1695
|
+
|
|
1696
|
+
rb_define_alloc_func(rb_cContext, allocate);
|
|
1697
|
+
rb_define_alloc_func(rb_cSnapshot, allocate_snapshot);
|
|
1698
|
+
rb_define_alloc_func(rb_cIsolate, allocate_isolate);
|
|
1699
|
+
|
|
1700
|
+
rb_define_private_method(rb_cExternalFunction, "notify_v8", (VALUE(*)(...))&rb_external_function_notify_v8, 0);
|
|
1701
|
+
rb_define_alloc_func(rb_cExternalFunction, allocate_external_function);
|
|
1702
|
+
|
|
1703
|
+
rb_define_method(rb_cSnapshot, "size", (VALUE(*)(...))&rb_snapshot_size, 0);
|
|
1704
|
+
rb_define_method(rb_cSnapshot, "dump", (VALUE(*)(...))&rb_snapshot_dump, 0);
|
|
1705
|
+
rb_define_method(rb_cSnapshot, "warmup_unsafe!", (VALUE(*)(...))&rb_snapshot_warmup_unsafe, 1);
|
|
1706
|
+
rb_define_private_method(rb_cSnapshot, "load", (VALUE(*)(...))&rb_snapshot_load, 1);
|
|
1707
|
+
|
|
1708
|
+
rb_define_method(rb_cIsolate, "idle_notification", (VALUE(*)(...))&rb_isolate_idle_notification, 1);
|
|
1709
|
+
rb_define_method(rb_cIsolate, "low_memory_notification", (VALUE(*)(...))&rb_isolate_low_memory_notification, 0);
|
|
1710
|
+
rb_define_method(rb_cIsolate, "pump_message_loop", (VALUE(*)(...))&rb_isolate_pump_message_loop, 0);
|
|
1711
|
+
rb_define_private_method(rb_cIsolate, "init_with_snapshot",(VALUE(*)(...))&rb_isolate_init_with_snapshot, 1);
|
|
1712
|
+
|
|
1713
|
+
rb_define_singleton_method(rb_cPlatform, "set_flag_as_str!", (VALUE(*)(...))&rb_platform_set_flag_as_str, 1);
|
|
1714
|
+
|
|
1715
|
+
rb_set_end_proc(set_ruby_exiting, Qnil);
|
|
1716
|
+
|
|
1717
|
+
static pthread_attr_t attr;
|
|
1718
|
+
if (pthread_attr_init(&attr) == 0) {
|
|
1719
|
+
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0) {
|
|
1720
|
+
thread_attr_p = &attr;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
auto on_fork_for_child = []() {
|
|
1724
|
+
exit_lock = PTHREAD_RWLOCK_INITIALIZER;
|
|
1725
|
+
};
|
|
1726
|
+
pthread_atfork(nullptr, nullptr, on_fork_for_child);
|
|
1727
|
+
}
|
|
1072
1728
|
}
|