correct tests, repair snapshot in debug mode

This commit is contained in:
Sam 2018-06-27 12:15:03 +10:00
parent c1bcfcf536
commit 3c6832c07b
2 changed files with 39 additions and 4 deletions

View File

@ -562,7 +562,7 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
return Qnil;
}
static VALUE rb_snapshot_warmup(VALUE self, VALUE str) {
static VALUE rb_snapshot_warmup_unsafe(VALUE self, VALUE str) {
SnapshotInfo* snapshot_info;
Data_Get_Struct(self, SnapshotInfo, snapshot_info);
@ -1275,6 +1275,8 @@ rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
call_argv = argv + 1;
}
bool missingFunction = false;
{
Locker lock(isolate);
Isolate::Scope isolate_scope(isolate);
@ -1289,7 +1291,7 @@ rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
MaybeLocal<v8::Value> val = context->Global()->Get(fname);
if (val.IsEmpty() || !val.ToLocalChecked()->IsFunction()) {
return Qnil;
missingFunction = true;
} else {
Local<v8::Function> fun = Local<v8::Function>::Cast(val.ToLocalChecked());
@ -1312,6 +1314,10 @@ rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
}
}
if (missingFunction) {
rb_raise(rb_eScriptRuntimeError, "Unknown JavaScript method invoked");
}
return convert_result_to_ruby(self, call.result);
}
@ -1371,7 +1377,7 @@ extern "C" {
rb_define_alloc_func(rb_cExternalFunction, allocate_external_function);
rb_define_method(rb_cSnapshot, "size", (VALUE(*)(...))&rb_snapshot_size, 0);
rb_define_method(rb_cSnapshot, "warmup!", (VALUE(*)(...))&rb_snapshot_warmup, 1);
rb_define_method(rb_cSnapshot, "warmup_unsafe!", (VALUE(*)(...))&rb_snapshot_warmup_unsafe, 1);
rb_define_private_method(rb_cSnapshot, "load", (VALUE(*)(...))&rb_snapshot_load, 1);
rb_define_method(rb_cIsolate, "idle_notification", (VALUE(*)(...))&rb_isolate_idle_notification, 1);

View File

@ -212,11 +212,13 @@ module MiniRacer
wrapped = lambda do |*args|
begin
r = nil
begin
@callback_mutex.synchronize{
@callback_running = true
}
callback.call(*args)
r = callback.call(*args)
ensure
@callback_mutex.synchronize{
@callback_running = false
@ -232,6 +234,8 @@ module MiniRacer
end
}
r
ensure
@callback_mutex.synchronize {
@thread_raise_called = false
@ -311,8 +315,33 @@ module MiniRacer
# `size` and `warmup!` public methods are defined in the C class
class Snapshot
def initialize(str = '')
# ensure it first can load
begin
ctx = MiniRacer::Context.new
ctx.eval(str)
rescue MiniRacer::RuntimeError => e
raise MiniRacer::SnapshotError.new, e.message
end
@source = str
# defined in the C class
load(str)
end
def warmup!(src)
# we have to do something here
# we are bloating memory a bit but it is more correct
# than hitting an exception when attempty to compile invalid source
begin
ctx = MiniRacer::Context.new
ctx.eval(@source)
ctx.eval(src)
rescue MiniRacer::RuntimeError => e
raise MiniRacer::SnapshotError.new, e.message
end
warmup_unsafe!(src)
end
end
end