mini_racer 0.1.14 → 0.2.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/.travis.yml +10 -6
- data/CHANGELOG +16 -0
- data/README.md +22 -0
- data/ext/mini_racer_extension/extconf.rb +1 -0
- data/ext/mini_racer_extension/mini_racer_extension.cc +584 -288
- data/lib/mini_racer/version.rb +1 -1
- data/lib/mini_racer.rb +87 -33
- data/mini_racer.gemspec +2 -2
- metadata +8 -8
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
#include <pthread.h>
|
|
8
8
|
#include <unistd.h>
|
|
9
9
|
#include <mutex>
|
|
10
|
+
#include <atomic>
|
|
10
11
|
#include <math.h>
|
|
11
12
|
|
|
12
13
|
using namespace v8;
|
|
@@ -16,21 +17,77 @@ typedef struct {
|
|
|
16
17
|
int raw_size;
|
|
17
18
|
} SnapshotInfo;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
class IsolateInfo {
|
|
21
|
+
public:
|
|
20
22
|
Isolate* isolate;
|
|
21
23
|
ArrayBuffer::Allocator* allocator;
|
|
22
24
|
StartupData* startup_data;
|
|
23
25
|
bool interrupted;
|
|
24
|
-
bool disposed;
|
|
25
26
|
pid_t pid;
|
|
27
|
+
VALUE mutex;
|
|
28
|
+
|
|
29
|
+
class Lock {
|
|
30
|
+
VALUE &mutex;
|
|
31
|
+
|
|
32
|
+
public:
|
|
33
|
+
Lock(VALUE &mutex) : mutex(mutex) {
|
|
34
|
+
rb_mutex_lock(mutex);
|
|
35
|
+
}
|
|
36
|
+
~Lock() {
|
|
37
|
+
rb_mutex_unlock(mutex);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
IsolateInfo() : isolate(nullptr), allocator(nullptr), startup_data(nullptr),
|
|
43
|
+
interrupted(false), pid(getpid()), refs_count(0) {
|
|
44
|
+
VALUE cMutex = rb_const_get(rb_cThread, rb_intern("Mutex"));
|
|
45
|
+
mutex = rb_class_new_instance(0, nullptr, cMutex);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
~IsolateInfo() {
|
|
49
|
+
void free_isolate(IsolateInfo*);
|
|
50
|
+
free_isolate(this);
|
|
51
|
+
}
|
|
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
|
+
}
|
|
72
|
+
|
|
73
|
+
static void* operator new(size_t size) {
|
|
74
|
+
return ruby_xmalloc(size);
|
|
75
|
+
}
|
|
26
76
|
|
|
77
|
+
static void operator delete(void *block) {
|
|
78
|
+
xfree(block);
|
|
79
|
+
}
|
|
80
|
+
private:
|
|
27
81
|
// how many references to this isolate exist
|
|
28
|
-
// we can't rely on Ruby's GC for this, because
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
82
|
+
// we can't rely on Ruby's GC for this, because Ruby could destroy the
|
|
83
|
+
// isolate before destroying the contexts that depend on them. We'd need to
|
|
84
|
+
// keep a list of linked contexts in the isolate to destroy those first when
|
|
85
|
+
// isolate destruction was requested. Keeping such a list would require
|
|
86
|
+
// notification from the context VALUEs when they are constructed and
|
|
87
|
+
// destroyed. With a ref count, those notifications are still needed, but
|
|
88
|
+
// we keep a simple int rather than a list of pointers.
|
|
89
|
+
std::atomic_int refs_count;
|
|
90
|
+
};
|
|
34
91
|
|
|
35
92
|
typedef struct {
|
|
36
93
|
IsolateInfo* isolate_info;
|
|
@@ -53,9 +110,30 @@ typedef struct {
|
|
|
53
110
|
Local<String>* filename;
|
|
54
111
|
useconds_t timeout;
|
|
55
112
|
EvalResult* result;
|
|
56
|
-
|
|
113
|
+
size_t max_memory;
|
|
57
114
|
} EvalParams;
|
|
58
115
|
|
|
116
|
+
typedef struct {
|
|
117
|
+
ContextInfo *context_info;
|
|
118
|
+
char *function_name;
|
|
119
|
+
int argc;
|
|
120
|
+
bool error;
|
|
121
|
+
Local<Function> fun;
|
|
122
|
+
Local<Value> *argv;
|
|
123
|
+
EvalResult result;
|
|
124
|
+
} FunctionCall;
|
|
125
|
+
|
|
126
|
+
enum IsolateFlags {
|
|
127
|
+
IN_GVL,
|
|
128
|
+
DO_TERMINATE,
|
|
129
|
+
MEM_SOFTLIMIT_VALUE,
|
|
130
|
+
MEM_SOFTLIMIT_REACHED,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
static VALUE rb_cContext;
|
|
134
|
+
static VALUE rb_cSnapshot;
|
|
135
|
+
static VALUE rb_cIsolate;
|
|
136
|
+
|
|
59
137
|
static VALUE rb_eScriptTerminatedError;
|
|
60
138
|
static VALUE rb_eV8OutOfMemoryError;
|
|
61
139
|
static VALUE rb_eParseError;
|
|
@@ -74,6 +152,11 @@ static std::mutex platform_lock;
|
|
|
74
152
|
static VALUE rb_platform_set_flag_as_str(VALUE _klass, VALUE flag_as_str) {
|
|
75
153
|
bool platform_already_initialized = false;
|
|
76
154
|
|
|
155
|
+
if(TYPE(flag_as_str) != T_STRING) {
|
|
156
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE" (should be a string)",
|
|
157
|
+
rb_obj_class(flag_as_str));
|
|
158
|
+
}
|
|
159
|
+
|
|
77
160
|
platform_lock.lock();
|
|
78
161
|
|
|
79
162
|
if (current_platform == NULL) {
|
|
@@ -109,17 +192,96 @@ static void init_v8() {
|
|
|
109
192
|
}
|
|
110
193
|
|
|
111
194
|
static void gc_callback(Isolate *isolate, GCType type, GCCallbackFlags flags) {
|
|
112
|
-
if((bool)isolate->GetData(
|
|
195
|
+
if((bool)isolate->GetData(MEM_SOFTLIMIT_REACHED)) return;
|
|
113
196
|
|
|
114
|
-
|
|
197
|
+
size_t softlimit = *(size_t*) isolate->GetData(MEM_SOFTLIMIT_VALUE);
|
|
115
198
|
|
|
116
199
|
HeapStatistics* stats = new HeapStatistics();
|
|
117
200
|
isolate->GetHeapStatistics(stats);
|
|
118
|
-
|
|
201
|
+
size_t used = stats->used_heap_size();
|
|
119
202
|
|
|
120
203
|
if(used > softlimit) {
|
|
121
|
-
isolate->SetData(
|
|
122
|
-
|
|
204
|
+
isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)true);
|
|
205
|
+
isolate->TerminateExecution();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// to be called with active lock and scope
|
|
210
|
+
static void prepare_result(MaybeLocal<Value> v8res,
|
|
211
|
+
TryCatch& trycatch,
|
|
212
|
+
Isolate* isolate,
|
|
213
|
+
Local<Context> context,
|
|
214
|
+
EvalResult& evalRes /* out */) {
|
|
215
|
+
|
|
216
|
+
// just don't touch .parsed
|
|
217
|
+
evalRes.terminated = false;
|
|
218
|
+
evalRes.json = false;
|
|
219
|
+
evalRes.value = nullptr;
|
|
220
|
+
evalRes.message = nullptr;
|
|
221
|
+
evalRes.backtrace = nullptr;
|
|
222
|
+
evalRes.executed = !v8res.IsEmpty();
|
|
223
|
+
|
|
224
|
+
if (evalRes.executed) {
|
|
225
|
+
// arrays and objects get converted to json
|
|
226
|
+
Local<Value> local_value = v8res.ToLocalChecked();
|
|
227
|
+
if ((local_value->IsObject() || local_value->IsArray()) &&
|
|
228
|
+
!local_value->IsDate() && !local_value->IsFunction()) {
|
|
229
|
+
Local<Object> JSON = context->Global()->Get(
|
|
230
|
+
String::NewFromUtf8(isolate, "JSON"))->ToObject();
|
|
231
|
+
|
|
232
|
+
Local<Function> stringify = JSON->Get(v8::String::NewFromUtf8(isolate, "stringify"))
|
|
233
|
+
.As<Function>();
|
|
234
|
+
|
|
235
|
+
Local<Object> object = local_value->ToObject();
|
|
236
|
+
const unsigned argc = 1;
|
|
237
|
+
Local<Value> argv[argc] = { object };
|
|
238
|
+
MaybeLocal<Value> json = stringify->Call(JSON, argc, argv);
|
|
239
|
+
|
|
240
|
+
if (json.IsEmpty()) {
|
|
241
|
+
evalRes.executed = false;
|
|
242
|
+
} else {
|
|
243
|
+
evalRes.json = true;
|
|
244
|
+
Persistent<Value>* persistent = new Persistent<Value>();
|
|
245
|
+
persistent->Reset(isolate, json.ToLocalChecked());
|
|
246
|
+
evalRes.value = persistent;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
} else {
|
|
250
|
+
Persistent<Value>* persistent = new Persistent<Value>();
|
|
251
|
+
persistent->Reset(isolate, local_value);
|
|
252
|
+
evalRes.value = persistent;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!evalRes.executed || !evalRes.parsed) {
|
|
257
|
+
if (trycatch.HasCaught()) {
|
|
258
|
+
if (!trycatch.Exception()->IsNull()) {
|
|
259
|
+
evalRes.message = new Persistent<Value>();
|
|
260
|
+
Local<Message> message = trycatch.Message();
|
|
261
|
+
char buf[1000];
|
|
262
|
+
int len;
|
|
263
|
+
len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(message->Get()),
|
|
264
|
+
*String::Utf8Value(message->GetScriptResourceName()->ToString()),
|
|
265
|
+
message->GetLineNumber(),
|
|
266
|
+
message->GetStartColumn());
|
|
267
|
+
if ((size_t) len >= sizeof(buf)) {
|
|
268
|
+
len = sizeof(buf) - 1;
|
|
269
|
+
buf[len] = '\0';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
Local<String> v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, len).ToLocalChecked();
|
|
273
|
+
evalRes.message->Reset(isolate, v8_message);
|
|
274
|
+
} else if(trycatch.HasTerminated()) {
|
|
275
|
+
evalRes.terminated = true;
|
|
276
|
+
evalRes.message = new Persistent<Value>();
|
|
277
|
+
Local<String> tmp = String::NewFromUtf8(isolate, "JavaScript was terminated (either by timeout or explicitly)");
|
|
278
|
+
evalRes.message->Reset(isolate, tmp);
|
|
279
|
+
}
|
|
280
|
+
if (!trycatch.StackTrace().IsEmpty()) {
|
|
281
|
+
evalRes.backtrace = new Persistent<Value>();
|
|
282
|
+
evalRes.backtrace->Reset(isolate, trycatch.StackTrace()->ToString());
|
|
283
|
+
}
|
|
284
|
+
}
|
|
123
285
|
}
|
|
124
286
|
}
|
|
125
287
|
|
|
@@ -138,13 +300,13 @@ nogvl_context_eval(void* arg) {
|
|
|
138
300
|
v8::ScriptOrigin *origin = NULL;
|
|
139
301
|
|
|
140
302
|
// in gvl flag
|
|
141
|
-
isolate->SetData(
|
|
303
|
+
isolate->SetData(IN_GVL, (void*)false);
|
|
142
304
|
// terminate ASAP
|
|
143
|
-
isolate->SetData(
|
|
305
|
+
isolate->SetData(DO_TERMINATE, (void*)false);
|
|
144
306
|
// Memory softlimit
|
|
145
|
-
isolate->SetData(
|
|
307
|
+
isolate->SetData(MEM_SOFTLIMIT_VALUE, (void*)false);
|
|
146
308
|
// Memory softlimit hit flag
|
|
147
|
-
isolate->SetData(
|
|
309
|
+
isolate->SetData(MEM_SOFTLIMIT_REACHED, (void*)false);
|
|
148
310
|
|
|
149
311
|
MaybeLocal<Script> parsed_script;
|
|
150
312
|
|
|
@@ -164,90 +326,30 @@ nogvl_context_eval(void* arg) {
|
|
|
164
326
|
result->json = false;
|
|
165
327
|
result->value = NULL;
|
|
166
328
|
|
|
329
|
+
MaybeLocal<Value> maybe_value;
|
|
167
330
|
if (!result->parsed) {
|
|
168
331
|
result->message = new Persistent<Value>();
|
|
169
332
|
result->message->Reset(isolate, trycatch.Exception());
|
|
170
333
|
} else {
|
|
334
|
+
// parsing successful
|
|
335
|
+
if (eval_params->max_memory > 0) {
|
|
336
|
+
isolate->SetData(MEM_SOFTLIMIT_VALUE, &eval_params->max_memory);
|
|
337
|
+
isolate->AddGCEpilogueCallback(gc_callback);
|
|
338
|
+
}
|
|
171
339
|
|
|
172
|
-
|
|
173
|
-
isolate->SetData(2, &eval_params->max_memory);
|
|
174
|
-
isolate->AddGCEpilogueCallback(gc_callback);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
MaybeLocal<Value> maybe_value = parsed_script.ToLocalChecked()->Run(context);
|
|
178
|
-
|
|
179
|
-
result->executed = !maybe_value.IsEmpty();
|
|
180
|
-
|
|
181
|
-
if (result->executed) {
|
|
182
|
-
|
|
183
|
-
// arrays and objects get converted to json
|
|
184
|
-
Local<Value> local_value = maybe_value.ToLocalChecked();
|
|
185
|
-
if ((local_value->IsObject() || local_value->IsArray()) &&
|
|
186
|
-
!local_value->IsDate() && !local_value->IsFunction()) {
|
|
187
|
-
Local<Object> JSON = context->Global()->Get(
|
|
188
|
-
String::NewFromUtf8(isolate, "JSON"))->ToObject();
|
|
189
|
-
|
|
190
|
-
Local<Function> stringify = JSON->Get(v8::String::NewFromUtf8(isolate, "stringify"))
|
|
191
|
-
.As<Function>();
|
|
192
|
-
|
|
193
|
-
Local<Object> object = local_value->ToObject();
|
|
194
|
-
const unsigned argc = 1;
|
|
195
|
-
Local<Value> argv[argc] = { object };
|
|
196
|
-
MaybeLocal<Value> json = stringify->Call(JSON, argc, argv);
|
|
197
|
-
|
|
198
|
-
if (json.IsEmpty()) {
|
|
199
|
-
result->executed = false;
|
|
200
|
-
} else {
|
|
201
|
-
result->json = true;
|
|
202
|
-
Persistent<Value>* persistent = new Persistent<Value>();
|
|
203
|
-
persistent->Reset(isolate, json.ToLocalChecked());
|
|
204
|
-
result->value = persistent;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
} else {
|
|
208
|
-
Persistent<Value>* persistent = new Persistent<Value>();
|
|
209
|
-
persistent->Reset(isolate, local_value);
|
|
210
|
-
result->value = persistent;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (!result->executed || !result->parsed) {
|
|
216
|
-
if (trycatch.HasCaught()) {
|
|
217
|
-
if (!trycatch.Exception()->IsNull()) {
|
|
218
|
-
result->message = new Persistent<Value>();
|
|
219
|
-
Local<Message> message = trycatch.Message();
|
|
220
|
-
char buf[1000];
|
|
221
|
-
int len;
|
|
222
|
-
len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(message->Get()),
|
|
223
|
-
*String::Utf8Value(message->GetScriptResourceName()->ToString()),
|
|
224
|
-
message->GetLineNumber(),
|
|
225
|
-
message->GetStartColumn());
|
|
226
|
-
|
|
227
|
-
Local<String> v8_message = String::NewFromUtf8(isolate, buf, NewStringType::kNormal, (int)len).ToLocalChecked();
|
|
228
|
-
result->message->Reset(isolate, v8_message);
|
|
229
|
-
} else if(trycatch.HasTerminated()) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
result->terminated = true;
|
|
233
|
-
result->message = new Persistent<Value>();
|
|
234
|
-
Local<String> tmp = String::NewFromUtf8(isolate, "JavaScript was terminated (either by timeout or explicitly)");
|
|
235
|
-
result->message->Reset(isolate, tmp);
|
|
236
|
-
}
|
|
237
|
-
if (!trycatch.StackTrace().IsEmpty()) {
|
|
238
|
-
result->backtrace = new Persistent<Value>();
|
|
239
|
-
result->backtrace->Reset(isolate, trycatch.StackTrace()->ToString());
|
|
240
|
-
}
|
|
241
|
-
}
|
|
340
|
+
maybe_value = parsed_script.ToLocalChecked()->Run(context);
|
|
242
341
|
}
|
|
243
342
|
|
|
244
|
-
|
|
343
|
+
prepare_result(maybe_value, trycatch, isolate, context, *result);
|
|
245
344
|
|
|
345
|
+
isolate->SetData(IN_GVL, (void*)true);
|
|
246
346
|
|
|
247
347
|
return NULL;
|
|
248
348
|
}
|
|
249
349
|
|
|
250
|
-
|
|
350
|
+
// assumes isolate locking is in place
|
|
351
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate, Local<Context> context,
|
|
352
|
+
Local<Value> value) {
|
|
251
353
|
|
|
252
354
|
Isolate::Scope isolate_scope(isolate);
|
|
253
355
|
HandleScope scope(isolate);
|
|
@@ -277,7 +379,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
|
|
|
277
379
|
Local<Array> arr = Local<Array>::Cast(value);
|
|
278
380
|
for(uint32_t i=0; i < arr->Length(); i++) {
|
|
279
381
|
Local<Value> element = arr->Get(i);
|
|
280
|
-
VALUE rb_elem = convert_v8_to_ruby(isolate, element);
|
|
382
|
+
VALUE rb_elem = convert_v8_to_ruby(isolate, context, element);
|
|
281
383
|
if (rb_funcall(rb_elem, rb_intern("class"), 0) == rb_cFailedV8Conversion) {
|
|
282
384
|
return rb_elem;
|
|
283
385
|
}
|
|
@@ -301,16 +403,15 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
|
|
|
301
403
|
if (value->IsObject()) {
|
|
302
404
|
VALUE rb_hash = rb_hash_new();
|
|
303
405
|
TryCatch trycatch(isolate);
|
|
304
|
-
Local<Context> context = Context::New(isolate);
|
|
305
406
|
|
|
306
407
|
Local<Object> object = value->ToObject();
|
|
307
|
-
|
|
408
|
+
auto maybe_props = object->GetOwnPropertyNames(context);
|
|
308
409
|
if (!maybe_props.IsEmpty()) {
|
|
309
410
|
Local<Array> props = maybe_props.ToLocalChecked();
|
|
310
411
|
for(uint32_t i=0; i < props->Length(); i++) {
|
|
311
412
|
Local<Value> key = props->Get(i);
|
|
312
|
-
VALUE rb_key = convert_v8_to_ruby(isolate, key);
|
|
313
|
-
Local<Value>
|
|
413
|
+
VALUE rb_key = convert_v8_to_ruby(isolate, context, key);
|
|
414
|
+
Local<Value> prop_value = object->Get(key);
|
|
314
415
|
// this may have failed due to Get raising
|
|
315
416
|
|
|
316
417
|
if (trycatch.HasCaught()) {
|
|
@@ -319,7 +420,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
|
|
|
319
420
|
return rb_funcall(rb_cFailedV8Conversion, rb_intern("new"), 1, rb_str_new2(""));
|
|
320
421
|
}
|
|
321
422
|
|
|
322
|
-
VALUE rb_value = convert_v8_to_ruby(isolate,
|
|
423
|
+
VALUE rb_value = convert_v8_to_ruby(isolate, context, prop_value);
|
|
323
424
|
rb_hash_aset(rb_hash, rb_key, rb_value);
|
|
324
425
|
}
|
|
325
426
|
}
|
|
@@ -330,7 +431,25 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
|
|
|
330
431
|
return rb_enc_str_new(*String::Utf8Value(rstr), rstr->Utf8Length(), rb_enc_find("utf-8"));
|
|
331
432
|
}
|
|
332
433
|
|
|
333
|
-
static
|
|
434
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate,
|
|
435
|
+
const Persistent<Context>& context,
|
|
436
|
+
Local<Value> value) {
|
|
437
|
+
HandleScope scope(isolate);
|
|
438
|
+
return convert_v8_to_ruby(isolate,
|
|
439
|
+
Local<Context>::New(isolate, context),
|
|
440
|
+
value);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
static VALUE convert_v8_to_ruby(Isolate* isolate,
|
|
444
|
+
const Persistent<Context>& context,
|
|
445
|
+
const Persistent<Value>& value) {
|
|
446
|
+
HandleScope scope(isolate);
|
|
447
|
+
return convert_v8_to_ruby(isolate,
|
|
448
|
+
Local<Context>::New(isolate, context),
|
|
449
|
+
Local<Value>::New(isolate, value));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
static Local<Value> convert_ruby_to_v8(Isolate* isolate, VALUE value) {
|
|
334
453
|
EscapableHandleScope scope(isolate);
|
|
335
454
|
|
|
336
455
|
Local<Array> array;
|
|
@@ -424,6 +543,11 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
|
|
|
424
543
|
SnapshotInfo* snapshot_info;
|
|
425
544
|
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
426
545
|
|
|
546
|
+
if(TYPE(str) != T_STRING) {
|
|
547
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
548
|
+
rb_obj_class(str));
|
|
549
|
+
}
|
|
550
|
+
|
|
427
551
|
init_v8();
|
|
428
552
|
|
|
429
553
|
StartupData startup_data = V8::CreateSnapshotDataBlob(RSTRING_PTR(str));
|
|
@@ -438,10 +562,15 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
|
|
|
438
562
|
return Qnil;
|
|
439
563
|
}
|
|
440
564
|
|
|
441
|
-
static VALUE
|
|
565
|
+
static VALUE rb_snapshot_warmup_unsafe(VALUE self, VALUE str) {
|
|
442
566
|
SnapshotInfo* snapshot_info;
|
|
443
567
|
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
|
|
444
568
|
|
|
569
|
+
if(TYPE(str) != T_STRING) {
|
|
570
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
571
|
+
rb_obj_class(str));
|
|
572
|
+
}
|
|
573
|
+
|
|
445
574
|
init_v8();
|
|
446
575
|
|
|
447
576
|
StartupData cold_startup_data = {snapshot_info->data, snapshot_info->raw_size};
|
|
@@ -459,27 +588,16 @@ static VALUE rb_snapshot_warmup(VALUE self, VALUE str) {
|
|
|
459
588
|
return self;
|
|
460
589
|
}
|
|
461
590
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
465
|
-
|
|
466
|
-
init_v8();
|
|
467
|
-
|
|
468
|
-
isolate_info->allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
469
|
-
isolate_info->interrupted = false;
|
|
470
|
-
isolate_info->refs_count = 1;
|
|
591
|
+
void IsolateInfo::init(SnapshotInfo* snapshot_info) {
|
|
592
|
+
allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
471
593
|
|
|
472
594
|
Isolate::CreateParams create_params;
|
|
473
|
-
create_params.array_buffer_allocator =
|
|
474
|
-
|
|
475
|
-
StartupData* startup_data = NULL;
|
|
476
|
-
if (!NIL_P(snapshot)) {
|
|
477
|
-
SnapshotInfo* snapshot_info;
|
|
478
|
-
Data_Get_Struct(snapshot, SnapshotInfo, snapshot_info);
|
|
595
|
+
create_params.array_buffer_allocator = allocator;
|
|
479
596
|
|
|
597
|
+
if (snapshot_info) {
|
|
480
598
|
int raw_size = snapshot_info->raw_size;
|
|
481
599
|
char* data = new char[raw_size];
|
|
482
|
-
memcpy(data, snapshot_info->data,
|
|
600
|
+
memcpy(data, snapshot_info->data, raw_size);
|
|
483
601
|
|
|
484
602
|
startup_data = new StartupData;
|
|
485
603
|
startup_data->data = data;
|
|
@@ -488,8 +606,22 @@ static VALUE rb_isolate_init_with_snapshot(VALUE self, VALUE snapshot) {
|
|
|
488
606
|
create_params.snapshot_blob = startup_data;
|
|
489
607
|
}
|
|
490
608
|
|
|
491
|
-
|
|
492
|
-
|
|
609
|
+
isolate = Isolate::New(create_params);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
static VALUE rb_isolate_init_with_snapshot(VALUE self, VALUE snapshot) {
|
|
613
|
+
IsolateInfo* isolate_info;
|
|
614
|
+
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
615
|
+
|
|
616
|
+
init_v8();
|
|
617
|
+
|
|
618
|
+
SnapshotInfo* snapshot_info = nullptr;
|
|
619
|
+
if (!NIL_P(snapshot)) {
|
|
620
|
+
Data_Get_Struct(snapshot, SnapshotInfo, snapshot_info);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
isolate_info->init(snapshot_info);
|
|
624
|
+
isolate_info->hold();
|
|
493
625
|
|
|
494
626
|
return Qnil;
|
|
495
627
|
}
|
|
@@ -498,23 +630,40 @@ static VALUE rb_isolate_idle_notification(VALUE self, VALUE idle_time_in_ms) {
|
|
|
498
630
|
IsolateInfo* isolate_info;
|
|
499
631
|
Data_Get_Struct(self, IsolateInfo, isolate_info);
|
|
500
632
|
|
|
501
|
-
|
|
633
|
+
if (current_platform == NULL) return Qfalse;
|
|
634
|
+
|
|
635
|
+
double duration = NUM2DBL(idle_time_in_ms) / 1000.0;
|
|
636
|
+
double now = current_platform->MonotonicallyIncreasingTime();
|
|
637
|
+
return isolate_info->isolate->IdleNotificationDeadline(now + duration) ? Qtrue : Qfalse;
|
|
502
638
|
}
|
|
503
639
|
|
|
504
|
-
static VALUE
|
|
640
|
+
static VALUE rb_context_init_unsafe(VALUE self, VALUE isolate, VALUE snap) {
|
|
505
641
|
ContextInfo* context_info;
|
|
506
642
|
Data_Get_Struct(self, ContextInfo, context_info);
|
|
507
643
|
|
|
508
644
|
init_v8();
|
|
509
645
|
|
|
510
646
|
IsolateInfo* isolate_info;
|
|
511
|
-
|
|
647
|
+
|
|
648
|
+
if (NIL_P(isolate) || !rb_obj_is_kind_of(isolate, rb_cIsolate)) {
|
|
649
|
+
isolate_info = new IsolateInfo();
|
|
650
|
+
|
|
651
|
+
SnapshotInfo *snapshot_info = nullptr;
|
|
652
|
+
if (!NIL_P(snap) && rb_obj_is_kind_of(snap, rb_cSnapshot)) {
|
|
653
|
+
Data_Get_Struct(snap, SnapshotInfo, snapshot_info);
|
|
654
|
+
}
|
|
655
|
+
isolate_info->init(snapshot_info);
|
|
656
|
+
} else { // given isolate or snapshot
|
|
657
|
+
Data_Get_Struct(isolate, IsolateInfo, isolate_info);
|
|
658
|
+
}
|
|
512
659
|
|
|
513
660
|
context_info->isolate_info = isolate_info;
|
|
514
|
-
isolate_info->
|
|
661
|
+
isolate_info->hold();
|
|
515
662
|
|
|
516
663
|
{
|
|
517
|
-
|
|
664
|
+
// the ruby lock is needed if this isn't a new isolate
|
|
665
|
+
IsolateInfo::Lock ruby_lock(isolate_info->mutex);
|
|
666
|
+
Locker lock(isolate_info->isolate);
|
|
518
667
|
Isolate::Scope isolate_scope(isolate_info->isolate);
|
|
519
668
|
HandleScope handle_scope(isolate_info->isolate);
|
|
520
669
|
|
|
@@ -532,19 +681,116 @@ static VALUE rb_context_init_with_isolate(VALUE self, VALUE isolate) {
|
|
|
532
681
|
return Qnil;
|
|
533
682
|
}
|
|
534
683
|
|
|
684
|
+
static VALUE convert_result_to_ruby(VALUE self /* context */,
|
|
685
|
+
EvalResult& result) {
|
|
686
|
+
ContextInfo *context_info;
|
|
687
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
688
|
+
|
|
689
|
+
Isolate *isolate = context_info->isolate_info->isolate;
|
|
690
|
+
Persistent<Context> *p_ctx = context_info->context;
|
|
691
|
+
|
|
692
|
+
VALUE message = Qnil;
|
|
693
|
+
VALUE backtrace = Qnil;
|
|
694
|
+
{
|
|
695
|
+
Locker lock(isolate);
|
|
696
|
+
if (result.message) {
|
|
697
|
+
message = convert_v8_to_ruby(isolate, *p_ctx, *result.message);
|
|
698
|
+
result.message->Reset();
|
|
699
|
+
delete result.message;
|
|
700
|
+
result.message = nullptr;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (result.backtrace) {
|
|
704
|
+
backtrace = convert_v8_to_ruby(isolate, *p_ctx, *result.backtrace);
|
|
705
|
+
result.backtrace->Reset();
|
|
706
|
+
delete result.backtrace;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// NOTE: this is very important, we can not do an rb_raise from within
|
|
711
|
+
// a v8 scope, if we do the scope is never cleaned up properly and we leak
|
|
712
|
+
if (!result.parsed) {
|
|
713
|
+
if(TYPE(message) == T_STRING) {
|
|
714
|
+
rb_raise(rb_eParseError, "%s", RSTRING_PTR(message));
|
|
715
|
+
} else {
|
|
716
|
+
rb_raise(rb_eParseError, "Unknown JavaScript Error during parse");
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (!result.executed) {
|
|
721
|
+
VALUE ruby_exception = rb_iv_get(self, "@current_exception");
|
|
722
|
+
if (ruby_exception == Qnil) {
|
|
723
|
+
bool mem_softlimit_reached = (bool)isolate->GetData(MEM_SOFTLIMIT_REACHED);
|
|
724
|
+
// If we were terminated or have the memory softlimit flag set
|
|
725
|
+
if (result.terminated || mem_softlimit_reached) {
|
|
726
|
+
ruby_exception = mem_softlimit_reached ? rb_eV8OutOfMemoryError : rb_eScriptTerminatedError;
|
|
727
|
+
} else {
|
|
728
|
+
ruby_exception = rb_eScriptRuntimeError;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// exception report about what happened
|
|
732
|
+
if (TYPE(backtrace) == T_STRING) {
|
|
733
|
+
rb_raise(ruby_exception, "%s", RSTRING_PTR(backtrace));
|
|
734
|
+
} else if(TYPE(message) == T_STRING) {
|
|
735
|
+
rb_raise(ruby_exception, "%s", RSTRING_PTR(message));
|
|
736
|
+
} else {
|
|
737
|
+
rb_raise(ruby_exception, "Unknown JavaScript Error during execution");
|
|
738
|
+
}
|
|
739
|
+
} else {
|
|
740
|
+
VALUE rb_str = rb_funcall(ruby_exception, rb_intern("to_s"), 0);
|
|
741
|
+
rb_raise(CLASS_OF(ruby_exception), "%s", RSTRING_PTR(rb_str));
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
VALUE ret = Qnil;
|
|
746
|
+
|
|
747
|
+
// New scope for return value
|
|
748
|
+
{
|
|
749
|
+
Locker lock(isolate);
|
|
750
|
+
Isolate::Scope isolate_scope(isolate);
|
|
751
|
+
HandleScope handle_scope(isolate);
|
|
752
|
+
|
|
753
|
+
Local<Value> tmp = Local<Value>::New(isolate, *result.value);
|
|
754
|
+
|
|
755
|
+
if (result.json) {
|
|
756
|
+
Local<String> rstr = tmp->ToString();
|
|
757
|
+
VALUE json_string = rb_enc_str_new(*String::Utf8Value(rstr), rstr->Utf8Length(), rb_enc_find("utf-8"));
|
|
758
|
+
ret = rb_funcall(rb_mJSON, rb_intern("parse"), 1, json_string);
|
|
759
|
+
} else {
|
|
760
|
+
ret = convert_v8_to_ruby(isolate, *p_ctx, tmp);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
result.value->Reset();
|
|
764
|
+
delete result.value;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (rb_funcall(ret, rb_intern("class"), 0) == rb_cFailedV8Conversion) {
|
|
768
|
+
// TODO try to recover stack trace from the conversion error
|
|
769
|
+
rb_raise(rb_eScriptRuntimeError, "Error converting JS object to Ruby object");
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
return ret;
|
|
774
|
+
}
|
|
775
|
+
|
|
535
776
|
static VALUE rb_context_eval_unsafe(VALUE self, VALUE str, VALUE filename) {
|
|
536
777
|
|
|
537
778
|
EvalParams eval_params;
|
|
538
779
|
EvalResult eval_result;
|
|
539
780
|
ContextInfo* context_info;
|
|
540
|
-
VALUE result;
|
|
541
|
-
|
|
542
|
-
VALUE message = Qnil;
|
|
543
|
-
VALUE backtrace = Qnil;
|
|
544
781
|
|
|
545
782
|
Data_Get_Struct(self, ContextInfo, context_info);
|
|
546
783
|
Isolate* isolate = context_info->isolate_info->isolate;
|
|
547
784
|
|
|
785
|
+
if(TYPE(str) != T_STRING) {
|
|
786
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
|
|
787
|
+
rb_obj_class(str));
|
|
788
|
+
}
|
|
789
|
+
if(filename != Qnil && TYPE(filename) != T_STRING) {
|
|
790
|
+
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be nil or a string)",
|
|
791
|
+
rb_obj_class(filename));
|
|
792
|
+
}
|
|
793
|
+
|
|
548
794
|
{
|
|
549
795
|
Locker lock(isolate);
|
|
550
796
|
Isolate::Scope isolate_scope(isolate);
|
|
@@ -575,97 +821,22 @@ static VALUE rb_context_eval_unsafe(VALUE self, VALUE str, VALUE filename) {
|
|
|
575
821
|
|
|
576
822
|
VALUE mem_softlimit = rb_iv_get(self, "@max_memory");
|
|
577
823
|
if (mem_softlimit != Qnil) {
|
|
578
|
-
|
|
824
|
+
eval_params.max_memory = (size_t)NUM2ULONG(mem_softlimit);
|
|
579
825
|
}
|
|
580
826
|
|
|
581
827
|
eval_result.message = NULL;
|
|
582
828
|
eval_result.backtrace = NULL;
|
|
583
829
|
|
|
584
830
|
rb_thread_call_without_gvl(nogvl_context_eval, &eval_params, unblock_eval, &eval_params);
|
|
585
|
-
|
|
586
|
-
if (eval_result.message != NULL) {
|
|
587
|
-
Local<Value> tmp = Local<Value>::New(isolate, *eval_result.message);
|
|
588
|
-
message = convert_v8_to_ruby(isolate, tmp);
|
|
589
|
-
eval_result.message->Reset();
|
|
590
|
-
delete eval_result.message;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if (eval_result.backtrace != NULL) {
|
|
594
|
-
Local<Value> tmp = Local<Value>::New(isolate, *eval_result.backtrace);
|
|
595
|
-
backtrace = convert_v8_to_ruby(isolate, tmp);
|
|
596
|
-
eval_result.backtrace->Reset();
|
|
597
|
-
delete eval_result.backtrace;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
// NOTE: this is very important, we can not do an rb_raise from within
|
|
602
|
-
// a v8 scope, if we do the scope is never cleaned up properly and we leak
|
|
603
|
-
if (!eval_result.parsed) {
|
|
604
|
-
if(TYPE(message) == T_STRING) {
|
|
605
|
-
rb_raise(rb_eParseError, "%s", RSTRING_PTR(message));
|
|
606
|
-
} else {
|
|
607
|
-
rb_raise(rb_eParseError, "Unknown JavaScript Error during parse");
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
if (!eval_result.executed) {
|
|
612
|
-
VALUE ruby_exception = rb_iv_get(self, "@current_exception");
|
|
613
|
-
if (ruby_exception == Qnil) {
|
|
614
|
-
bool mem_softlimit_reached = (bool)isolate->GetData(3);
|
|
615
|
-
// If we were terminated or have the memory softlimit flag set
|
|
616
|
-
if(eval_result.terminated || mem_softlimit_reached) {
|
|
617
|
-
ruby_exception = mem_softlimit_reached ? rb_eV8OutOfMemoryError : rb_eScriptTerminatedError;
|
|
618
|
-
} else {
|
|
619
|
-
ruby_exception = rb_eScriptRuntimeError;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
// exception report about what happened
|
|
623
|
-
if(TYPE(backtrace) == T_STRING) {
|
|
624
|
-
rb_raise(ruby_exception, "%s", RSTRING_PTR(backtrace));
|
|
625
|
-
} else if(TYPE(message) == T_STRING) {
|
|
626
|
-
rb_raise(ruby_exception, "%s", RSTRING_PTR(message));
|
|
627
|
-
} else {
|
|
628
|
-
rb_raise(ruby_exception, "Unknown JavaScript Error during execution");
|
|
629
|
-
}
|
|
630
|
-
} else {
|
|
631
|
-
VALUE rb_str = rb_funcall(ruby_exception, rb_intern("to_s"), 0);
|
|
632
|
-
rb_raise(CLASS_OF(ruby_exception), "%s", RSTRING_PTR(rb_str));
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
// New scope for return value
|
|
637
|
-
{
|
|
638
|
-
Locker lock(isolate);
|
|
639
|
-
Isolate::Scope isolate_scope(isolate);
|
|
640
|
-
HandleScope handle_scope(isolate);
|
|
641
|
-
|
|
642
|
-
Local<Value> tmp = Local<Value>::New(isolate, *eval_result.value);
|
|
643
|
-
|
|
644
|
-
if (eval_result.json) {
|
|
645
|
-
Local<String> rstr = tmp->ToString();
|
|
646
|
-
VALUE json_string = rb_enc_str_new(*String::Utf8Value(rstr), rstr->Utf8Length(), rb_enc_find("utf-8"));
|
|
647
|
-
result = rb_funcall(rb_mJSON, rb_intern("parse"), 1, json_string);
|
|
648
|
-
} else {
|
|
649
|
-
result = convert_v8_to_ruby(isolate, tmp);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
eval_result.value->Reset();
|
|
653
|
-
delete eval_result.value;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
if (rb_funcall(result, rb_intern("class"), 0) == rb_cFailedV8Conversion) {
|
|
657
|
-
// TODO try to recover stack trace from the conversion error
|
|
658
|
-
rb_raise(rb_eScriptRuntimeError, "Error converting JS object to Ruby object");
|
|
659
831
|
}
|
|
660
832
|
|
|
661
|
-
|
|
662
|
-
return result;
|
|
833
|
+
return convert_result_to_ruby(self, eval_result);
|
|
663
834
|
}
|
|
664
835
|
|
|
665
836
|
typedef struct {
|
|
666
837
|
VALUE callback;
|
|
667
838
|
int length;
|
|
668
|
-
VALUE
|
|
839
|
+
VALUE ruby_args;
|
|
669
840
|
bool failed;
|
|
670
841
|
} protected_callback_data;
|
|
671
842
|
|
|
@@ -675,7 +846,9 @@ VALUE protected_callback(VALUE rdata) {
|
|
|
675
846
|
VALUE result;
|
|
676
847
|
|
|
677
848
|
if (data->length > 0) {
|
|
678
|
-
result = rb_funcall2(data->callback, rb_intern("call"), data->length,
|
|
849
|
+
result = rb_funcall2(data->callback, rb_intern("call"), data->length,
|
|
850
|
+
RARRAY_PTR(data->ruby_args));
|
|
851
|
+
RB_GC_GUARD(data->ruby_args);
|
|
679
852
|
} else {
|
|
680
853
|
result = rb_funcall(data->callback, rb_intern("call"), 0);
|
|
681
854
|
}
|
|
@@ -693,28 +866,36 @@ void*
|
|
|
693
866
|
gvl_ruby_callback(void* data) {
|
|
694
867
|
|
|
695
868
|
FunctionCallbackInfo<Value>* args = (FunctionCallbackInfo<Value>*)data;
|
|
696
|
-
VALUE
|
|
869
|
+
VALUE ruby_args = Qnil;
|
|
697
870
|
int length = args->Length();
|
|
698
871
|
VALUE callback;
|
|
699
872
|
VALUE result;
|
|
700
873
|
VALUE self;
|
|
701
|
-
|
|
874
|
+
VALUE parent;
|
|
702
875
|
{
|
|
703
|
-
|
|
704
|
-
|
|
876
|
+
HandleScope scope(args->GetIsolate());
|
|
877
|
+
Local<External> external = Local<External>::Cast(args->Data());
|
|
705
878
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
879
|
+
self = *(VALUE*)(external->Value());
|
|
880
|
+
callback = rb_iv_get(self, "@callback");
|
|
881
|
+
|
|
882
|
+
parent = rb_iv_get(self, "@parent");
|
|
883
|
+
if (NIL_P(parent) || !rb_obj_is_kind_of(parent, rb_cContext)) {
|
|
884
|
+
return NULL;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
ContextInfo* context_info;
|
|
888
|
+
Data_Get_Struct(parent, ContextInfo, context_info);
|
|
709
889
|
|
|
710
890
|
if (length > 0) {
|
|
711
|
-
ruby_args =
|
|
891
|
+
ruby_args = rb_ary_tmp_new(length);
|
|
712
892
|
}
|
|
713
893
|
|
|
714
|
-
|
|
715
894
|
for (int i = 0; i < length; i++) {
|
|
716
895
|
Local<Value> value = ((*args)[i]).As<Value>();
|
|
717
|
-
|
|
896
|
+
VALUE tmp = convert_v8_to_ruby(args->GetIsolate(),
|
|
897
|
+
*context_info->context, value);
|
|
898
|
+
rb_ary_push(ruby_args, tmp);
|
|
718
899
|
}
|
|
719
900
|
}
|
|
720
901
|
|
|
@@ -722,12 +903,16 @@ gvl_ruby_callback(void* data) {
|
|
|
722
903
|
protected_callback_data callback_data;
|
|
723
904
|
callback_data.length = length;
|
|
724
905
|
callback_data.callback = callback;
|
|
725
|
-
callback_data.
|
|
906
|
+
callback_data.ruby_args = ruby_args;
|
|
726
907
|
callback_data.failed = false;
|
|
727
908
|
|
|
728
|
-
if ((bool)args->GetIsolate()->GetData(
|
|
909
|
+
if ((bool)args->GetIsolate()->GetData(DO_TERMINATE) == true) {
|
|
729
910
|
args->GetIsolate()->ThrowException(String::NewFromUtf8(args->GetIsolate(), "Terminated execution during transition from Ruby to JS"));
|
|
730
|
-
|
|
911
|
+
args->GetIsolate()->TerminateExecution();
|
|
912
|
+
if (length > 0) {
|
|
913
|
+
rb_ary_clear(ruby_args);
|
|
914
|
+
rb_gc_force_recycle(ruby_args);
|
|
915
|
+
}
|
|
731
916
|
return NULL;
|
|
732
917
|
}
|
|
733
918
|
|
|
@@ -735,7 +920,6 @@ gvl_ruby_callback(void* data) {
|
|
|
735
920
|
(VALUE(*)(...))&rescue_callback, (VALUE)(&callback_data), rb_eException, (VALUE)0);
|
|
736
921
|
|
|
737
922
|
if(callback_data.failed) {
|
|
738
|
-
VALUE parent = rb_iv_get(self, "@parent");
|
|
739
923
|
rb_iv_set(parent, "@current_exception", result);
|
|
740
924
|
args->GetIsolate()->ThrowException(String::NewFromUtf8(args->GetIsolate(), "Ruby exception"));
|
|
741
925
|
}
|
|
@@ -746,25 +930,27 @@ gvl_ruby_callback(void* data) {
|
|
|
746
930
|
}
|
|
747
931
|
|
|
748
932
|
if (length > 0) {
|
|
749
|
-
|
|
933
|
+
rb_ary_clear(ruby_args);
|
|
934
|
+
rb_gc_force_recycle(ruby_args);
|
|
750
935
|
}
|
|
751
936
|
|
|
752
|
-
if ((bool)args->GetIsolate()->GetData(
|
|
937
|
+
if ((bool)args->GetIsolate()->GetData(DO_TERMINATE) == true) {
|
|
753
938
|
Isolate* isolate = args->GetIsolate();
|
|
754
|
-
|
|
939
|
+
isolate->TerminateExecution();
|
|
755
940
|
}
|
|
756
941
|
|
|
757
942
|
return NULL;
|
|
758
943
|
}
|
|
759
944
|
|
|
760
945
|
static void ruby_callback(const FunctionCallbackInfo<Value>& args) {
|
|
761
|
-
|
|
762
|
-
bool has_gvl = (bool)args.GetIsolate()->GetData(0);
|
|
946
|
+
bool has_gvl = (bool)args.GetIsolate()->GetData(IN_GVL);
|
|
763
947
|
|
|
764
948
|
if(has_gvl) {
|
|
765
949
|
gvl_ruby_callback((void*)&args);
|
|
766
950
|
} else {
|
|
951
|
+
args.GetIsolate()->SetData(IN_GVL, (void*)true);
|
|
767
952
|
rb_thread_call_with_gvl(gvl_ruby_callback, (void*)(&args));
|
|
953
|
+
args.GetIsolate()->SetData(IN_GVL, (void*)false);
|
|
768
954
|
}
|
|
769
955
|
}
|
|
770
956
|
|
|
@@ -829,16 +1015,27 @@ static VALUE rb_external_function_notify_v8(VALUE self) {
|
|
|
829
1015
|
|
|
830
1016
|
// always raise out of V8 context
|
|
831
1017
|
if (parse_error) {
|
|
832
|
-
rb_raise(rb_eParseError, "Invalid object %
|
|
1018
|
+
rb_raise(rb_eParseError, "Invalid object %" PRIsVALUE, parent_object);
|
|
833
1019
|
}
|
|
834
1020
|
|
|
835
1021
|
if (attach_error) {
|
|
836
|
-
rb_raise(rb_eParseError, "Was expecting %
|
|
1022
|
+
rb_raise(rb_eParseError, "Was expecting %" PRIsVALUE" to be an object", parent_object);
|
|
837
1023
|
}
|
|
838
1024
|
|
|
839
1025
|
return Qnil;
|
|
840
1026
|
}
|
|
841
1027
|
|
|
1028
|
+
static VALUE rb_context_isolate_mutex(VALUE self) {
|
|
1029
|
+
ContextInfo* context_info;
|
|
1030
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1031
|
+
|
|
1032
|
+
if (!context_info->isolate_info) {
|
|
1033
|
+
rb_raise(rb_eScriptRuntimeError, "Context has no Isolate available anymore");
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
return context_info->isolate_info->mutex;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
842
1039
|
void free_isolate(IsolateInfo* isolate_info) {
|
|
843
1040
|
|
|
844
1041
|
if (isolate_info->isolate) {
|
|
@@ -865,26 +1062,11 @@ void free_isolate(IsolateInfo* isolate_info) {
|
|
|
865
1062
|
}
|
|
866
1063
|
|
|
867
1064
|
delete isolate_info->allocator;
|
|
868
|
-
isolate_info->disposed = true;
|
|
869
1065
|
}
|
|
870
1066
|
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
// still need it
|
|
874
|
-
//
|
|
875
|
-
// there is a sequence issue here where Ruby may call the deallocator on the
|
|
876
|
-
// context object after it calles the dallocator on the isolate
|
|
877
|
-
if (isolate_info->refs_count != 0 || isolate_info->disposed) {
|
|
878
|
-
return;
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
free_isolate(isolate_info);
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
void free_context(void* data) {
|
|
1067
|
+
// destroys everything except freeing the ContextInfo struct (see deallocate())
|
|
1068
|
+
static void free_context(ContextInfo* context_info) {
|
|
886
1069
|
|
|
887
|
-
ContextInfo* context_info = (ContextInfo*)data;
|
|
888
1070
|
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
889
1071
|
|
|
890
1072
|
if (context_info->context && isolate_info && isolate_info->isolate) {
|
|
@@ -894,46 +1076,39 @@ void free_context(void* data) {
|
|
|
894
1076
|
delete context_info->context;
|
|
895
1077
|
context_info->context = NULL;
|
|
896
1078
|
}
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
void deallocate_context(void* data) {
|
|
900
|
-
|
|
901
|
-
ContextInfo* context_info = (ContextInfo*)data;
|
|
902
|
-
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
903
|
-
|
|
904
|
-
free_context(data);
|
|
905
1079
|
|
|
906
1080
|
if (isolate_info) {
|
|
907
|
-
|
|
908
|
-
|
|
1081
|
+
isolate_info->release();
|
|
1082
|
+
context_info->isolate_info = NULL;
|
|
909
1083
|
}
|
|
910
1084
|
}
|
|
911
1085
|
|
|
912
|
-
void deallocate_isolate(void* data) {
|
|
1086
|
+
static void deallocate_isolate(void* data) {
|
|
913
1087
|
|
|
914
1088
|
IsolateInfo* isolate_info = (IsolateInfo*) data;
|
|
915
1089
|
|
|
916
|
-
isolate_info->
|
|
917
|
-
|
|
1090
|
+
isolate_info->release();
|
|
1091
|
+
}
|
|
918
1092
|
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
1093
|
+
static void mark_isolate(void* data) {
|
|
1094
|
+
IsolateInfo* isolate_info = (IsolateInfo*) data;
|
|
1095
|
+
isolate_info->mark();
|
|
922
1096
|
}
|
|
923
1097
|
|
|
924
1098
|
void deallocate(void* data) {
|
|
925
1099
|
ContextInfo* context_info = (ContextInfo*)data;
|
|
926
|
-
IsolateInfo* isolate_info = context_info->isolate_info;
|
|
927
|
-
|
|
928
|
-
deallocate_context(data);
|
|
929
1100
|
|
|
930
|
-
|
|
931
|
-
xfree(isolate_info);
|
|
932
|
-
}
|
|
1101
|
+
free_context(context_info);
|
|
933
1102
|
|
|
934
1103
|
xfree(data);
|
|
935
1104
|
}
|
|
936
1105
|
|
|
1106
|
+
static void mark_context(void* data) {
|
|
1107
|
+
ContextInfo* context_info = (ContextInfo*)data;
|
|
1108
|
+
if (context_info->isolate_info) {
|
|
1109
|
+
context_info->isolate_info->mark();
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
937
1112
|
|
|
938
1113
|
void deallocate_external_function(void * data) {
|
|
939
1114
|
xfree(data);
|
|
@@ -955,7 +1130,7 @@ VALUE allocate(VALUE klass) {
|
|
|
955
1130
|
context_info->isolate_info = NULL;
|
|
956
1131
|
context_info->context = NULL;
|
|
957
1132
|
|
|
958
|
-
return Data_Wrap_Struct(klass,
|
|
1133
|
+
return Data_Wrap_Struct(klass, mark_context, deallocate, (void*)context_info);
|
|
959
1134
|
}
|
|
960
1135
|
|
|
961
1136
|
VALUE allocate_snapshot(VALUE klass) {
|
|
@@ -967,17 +1142,9 @@ VALUE allocate_snapshot(VALUE klass) {
|
|
|
967
1142
|
}
|
|
968
1143
|
|
|
969
1144
|
VALUE allocate_isolate(VALUE klass) {
|
|
970
|
-
IsolateInfo* isolate_info =
|
|
1145
|
+
IsolateInfo* isolate_info = new IsolateInfo();
|
|
971
1146
|
|
|
972
|
-
|
|
973
|
-
isolate_info->allocator = NULL;
|
|
974
|
-
isolate_info->startup_data = NULL;
|
|
975
|
-
isolate_info->interrupted = false;
|
|
976
|
-
isolate_info->refs_count = 0;
|
|
977
|
-
isolate_info->pid = getpid();
|
|
978
|
-
isolate_info->disposed = false;
|
|
979
|
-
|
|
980
|
-
return Data_Wrap_Struct(klass, NULL, deallocate_isolate, (void*)isolate_info);
|
|
1147
|
+
return Data_Wrap_Struct(klass, mark_isolate, deallocate_isolate, (void*)isolate_info);
|
|
981
1148
|
}
|
|
982
1149
|
|
|
983
1150
|
static VALUE
|
|
@@ -1022,9 +1189,9 @@ rb_context_stop(VALUE self) {
|
|
|
1022
1189
|
Isolate* isolate = context_info->isolate_info->isolate;
|
|
1023
1190
|
|
|
1024
1191
|
// flag for termination
|
|
1025
|
-
isolate->SetData(
|
|
1192
|
+
isolate->SetData(DO_TERMINATE, (void*)true);
|
|
1026
1193
|
|
|
1027
|
-
|
|
1194
|
+
isolate->TerminateExecution();
|
|
1028
1195
|
rb_funcall(self, rb_intern("stop_attached"), 0);
|
|
1029
1196
|
|
|
1030
1197
|
return Qnil;
|
|
@@ -1038,22 +1205,148 @@ rb_context_dispose(VALUE self) {
|
|
|
1038
1205
|
|
|
1039
1206
|
free_context(context_info);
|
|
1040
1207
|
|
|
1041
|
-
if (context_info->isolate_info && context_info->isolate_info->refs_count == 2) {
|
|
1042
|
-
// special case, we only have isolate + context so we can burn the
|
|
1043
|
-
// isolate as well
|
|
1044
|
-
free_isolate(context_info->isolate_info);
|
|
1045
|
-
}
|
|
1046
1208
|
return Qnil;
|
|
1047
1209
|
}
|
|
1048
1210
|
|
|
1211
|
+
static void*
|
|
1212
|
+
nogvl_context_call(void *args) {
|
|
1213
|
+
|
|
1214
|
+
FunctionCall *call = (FunctionCall *) args;
|
|
1215
|
+
if (!call) {
|
|
1216
|
+
return NULL;
|
|
1217
|
+
}
|
|
1218
|
+
Isolate* isolate = call->context_info->isolate_info->isolate;
|
|
1219
|
+
|
|
1220
|
+
// in gvl flag
|
|
1221
|
+
isolate->SetData(IN_GVL, (void*)false);
|
|
1222
|
+
// terminate ASAP
|
|
1223
|
+
isolate->SetData(DO_TERMINATE, (void*)false);
|
|
1224
|
+
|
|
1225
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1226
|
+
EscapableHandleScope handle_scope(isolate);
|
|
1227
|
+
TryCatch trycatch(isolate);
|
|
1228
|
+
|
|
1229
|
+
Local<Context> context = call->context_info->context->Get(isolate);
|
|
1230
|
+
Context::Scope context_scope(context);
|
|
1231
|
+
|
|
1232
|
+
Local<Function> fun = call->fun;
|
|
1233
|
+
|
|
1234
|
+
EvalResult& eval_res = call->result;
|
|
1235
|
+
eval_res.parsed = true;
|
|
1236
|
+
|
|
1237
|
+
MaybeLocal<v8::Value> res = fun->Call(context, context->Global(), call->argc, call->argv);
|
|
1238
|
+
prepare_result(res, trycatch, isolate, context, eval_res);
|
|
1239
|
+
|
|
1240
|
+
isolate->SetData(IN_GVL, (void*)true);
|
|
1241
|
+
|
|
1242
|
+
return NULL;
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
static void unblock_function(void *args) {
|
|
1246
|
+
FunctionCall *call = (FunctionCall *) args;
|
|
1247
|
+
call->context_info->isolate_info->interrupted = true;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
static VALUE
|
|
1251
|
+
rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
|
|
1252
|
+
ContextInfo* context_info;
|
|
1253
|
+
FunctionCall call;
|
|
1254
|
+
VALUE *call_argv = NULL;
|
|
1255
|
+
|
|
1256
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1257
|
+
Isolate* isolate = context_info->isolate_info->isolate;
|
|
1258
|
+
|
|
1259
|
+
if (argc < 1) {
|
|
1260
|
+
rb_raise(rb_eArgError, "need at least one argument %d", argc);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
VALUE function_name = argv[0];
|
|
1264
|
+
if (TYPE(function_name) != T_STRING) {
|
|
1265
|
+
rb_raise(rb_eTypeError, "first argument should be a String");
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
char *fname = RSTRING_PTR(function_name);
|
|
1269
|
+
if (!fname) {
|
|
1270
|
+
return Qnil;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
call.context_info = context_info;
|
|
1274
|
+
call.error = false;
|
|
1275
|
+
call.function_name = fname;
|
|
1276
|
+
call.argc = argc - 1;
|
|
1277
|
+
call.argv = NULL;
|
|
1278
|
+
if (call.argc > 0) {
|
|
1279
|
+
// skip first argument which is the function name
|
|
1280
|
+
call_argv = argv + 1;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
bool missingFunction = false;
|
|
1284
|
+
|
|
1285
|
+
{
|
|
1286
|
+
Locker lock(isolate);
|
|
1287
|
+
Isolate::Scope isolate_scope(isolate);
|
|
1288
|
+
HandleScope handle_scope(isolate);
|
|
1289
|
+
|
|
1290
|
+
Local<Context> context = context_info->context->Get(isolate);
|
|
1291
|
+
Context::Scope context_scope(context);
|
|
1292
|
+
|
|
1293
|
+
// examples of such usage can be found in
|
|
1294
|
+
// https://github.com/v8/v8/blob/36b32aa28db5e993312f4588d60aad5c8330c8a5/test/cctest/test-api.cc#L15711
|
|
1295
|
+
Local<String> fname = String::NewFromUtf8(isolate, call.function_name);
|
|
1296
|
+
MaybeLocal<v8::Value> val = context->Global()->Get(fname);
|
|
1297
|
+
|
|
1298
|
+
if (val.IsEmpty() || !val.ToLocalChecked()->IsFunction()) {
|
|
1299
|
+
missingFunction = true;
|
|
1300
|
+
} else {
|
|
1301
|
+
|
|
1302
|
+
Local<v8::Function> fun = Local<v8::Function>::Cast(val.ToLocalChecked());
|
|
1303
|
+
call.fun = fun;
|
|
1304
|
+
int fun_argc = call.argc;
|
|
1305
|
+
|
|
1306
|
+
if (fun_argc > 0) {
|
|
1307
|
+
call.argv = (v8::Local<Value> *) malloc(sizeof(void *) * fun_argc);
|
|
1308
|
+
if (!call.argv) {
|
|
1309
|
+
return Qnil;
|
|
1310
|
+
}
|
|
1311
|
+
for(int i=0; i < fun_argc; i++) {
|
|
1312
|
+
call.argv[i] = convert_ruby_to_v8(isolate, call_argv[i]);
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
rb_thread_call_without_gvl(nogvl_context_call, &call, unblock_function, &call);
|
|
1317
|
+
free(call.argv);
|
|
1318
|
+
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
if (missingFunction) {
|
|
1323
|
+
rb_raise(rb_eScriptRuntimeError, "Unknown JavaScript method invoked");
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
return convert_result_to_ruby(self, call.result);
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
static VALUE rb_context_create_isolate_value(VALUE self) {
|
|
1330
|
+
ContextInfo* context_info;
|
|
1331
|
+
Data_Get_Struct(self, ContextInfo, context_info);
|
|
1332
|
+
IsolateInfo *isolate_info = context_info->isolate_info;
|
|
1333
|
+
|
|
1334
|
+
if (!isolate_info) {
|
|
1335
|
+
return Qnil;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
isolate_info->hold();
|
|
1339
|
+
return Data_Wrap_Struct(rb_cIsolate, NULL, &deallocate_isolate, isolate_info);
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1049
1342
|
extern "C" {
|
|
1050
1343
|
|
|
1051
1344
|
void Init_mini_racer_extension ( void )
|
|
1052
1345
|
{
|
|
1053
1346
|
VALUE rb_mMiniRacer = rb_define_module("MiniRacer");
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1347
|
+
rb_cContext = rb_define_class_under(rb_mMiniRacer, "Context", rb_cObject);
|
|
1348
|
+
rb_cSnapshot = rb_define_class_under(rb_mMiniRacer, "Snapshot", rb_cObject);
|
|
1349
|
+
rb_cIsolate = rb_define_class_under(rb_mMiniRacer, "Isolate", rb_cObject);
|
|
1057
1350
|
VALUE rb_cPlatform = rb_define_class_under(rb_mMiniRacer, "Platform", rb_cObject);
|
|
1058
1351
|
|
|
1059
1352
|
VALUE rb_eError = rb_define_class_under(rb_mMiniRacer, "Error", rb_eStandardError);
|
|
@@ -1075,18 +1368,21 @@ extern "C" {
|
|
|
1075
1368
|
rb_define_method(rb_cContext, "stop", (VALUE(*)(...))&rb_context_stop, 0);
|
|
1076
1369
|
rb_define_method(rb_cContext, "dispose_unsafe", (VALUE(*)(...))&rb_context_dispose, 0);
|
|
1077
1370
|
rb_define_method(rb_cContext, "heap_stats", (VALUE(*)(...))&rb_heap_stats, 0);
|
|
1371
|
+
rb_define_private_method(rb_cContext, "create_isolate_value",(VALUE(*)(...))&rb_context_create_isolate_value, 0);
|
|
1372
|
+
rb_define_private_method(rb_cContext, "eval_unsafe",(VALUE(*)(...))&rb_context_eval_unsafe, 2);
|
|
1373
|
+
rb_define_private_method(rb_cContext, "call_unsafe", (VALUE(*)(...))&rb_context_call_unsafe, -1);
|
|
1374
|
+
rb_define_private_method(rb_cContext, "isolate_mutex", (VALUE(*)(...))&rb_context_isolate_mutex, 0);
|
|
1375
|
+
rb_define_private_method(rb_cContext, "init_unsafe",(VALUE(*)(...))&rb_context_init_unsafe, 2);
|
|
1078
1376
|
|
|
1079
1377
|
rb_define_alloc_func(rb_cContext, allocate);
|
|
1080
1378
|
rb_define_alloc_func(rb_cSnapshot, allocate_snapshot);
|
|
1081
1379
|
rb_define_alloc_func(rb_cIsolate, allocate_isolate);
|
|
1082
1380
|
|
|
1083
|
-
rb_define_private_method(rb_cContext, "eval_unsafe",(VALUE(*)(...))&rb_context_eval_unsafe, 2);
|
|
1084
|
-
rb_define_private_method(rb_cContext, "init_with_isolate",(VALUE(*)(...))&rb_context_init_with_isolate, 1);
|
|
1085
1381
|
rb_define_private_method(rb_cExternalFunction, "notify_v8", (VALUE(*)(...))&rb_external_function_notify_v8, 0);
|
|
1086
1382
|
rb_define_alloc_func(rb_cExternalFunction, allocate_external_function);
|
|
1087
1383
|
|
|
1088
1384
|
rb_define_method(rb_cSnapshot, "size", (VALUE(*)(...))&rb_snapshot_size, 0);
|
|
1089
|
-
rb_define_method(rb_cSnapshot, "
|
|
1385
|
+
rb_define_method(rb_cSnapshot, "warmup_unsafe!", (VALUE(*)(...))&rb_snapshot_warmup_unsafe, 1);
|
|
1090
1386
|
rb_define_private_method(rb_cSnapshot, "load", (VALUE(*)(...))&rb_snapshot_load, 1);
|
|
1091
1387
|
|
|
1092
1388
|
rb_define_method(rb_cIsolate, "idle_notification", (VALUE(*)(...))&rb_isolate_idle_notification, 1);
|