mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
expose Get() method on native message object. Re-raise javascript exceptions in ruby.
This commit is contained in:
parent
395f553872
commit
36628a1792
5 changed files with 25 additions and 15 deletions
|
@ -65,6 +65,8 @@ extern "C" {
|
|||
rb_define_method(V8_C_Object, "Set", (VALUE(*)(...))v8_Object_Set, 2);
|
||||
|
||||
V8_C_Message = rb_define_class_under(rb_mNative, "Message", rb_cObject);
|
||||
rb_define_method(V8_C_Message, "Get", (VALUE(*)(...))v8_Message_Get, 0);
|
||||
|
||||
V8_C_Function = rb_define_class_under(rb_mNative, "Function", V8_C_Object);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,17 +25,10 @@ VALUE v8_cxt_Global(VALUE self) {
|
|||
|
||||
VALUE v8_cxt_open(VALUE self) {
|
||||
HandleScope handles;
|
||||
TryCatch exceptions;
|
||||
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
||||
Context::Scope enter(cxt);
|
||||
if (rb_block_given_p()) {
|
||||
VALUE result = rb_yield(self);
|
||||
if (exceptions.HasCaught()) {
|
||||
return V8_Wrap_Message(exceptions.Message());
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
return rb_yield(self);
|
||||
} else {
|
||||
return Qnil;
|
||||
}
|
||||
|
@ -43,12 +36,17 @@ VALUE v8_cxt_open(VALUE self) {
|
|||
|
||||
VALUE v8_cxt_eval(VALUE self, VALUE source) {
|
||||
HandleScope handles;
|
||||
TryCatch exceptions;
|
||||
Local<Context> cxt = V8_Ref_Get<Context>(self);
|
||||
Context::Scope enter(cxt);
|
||||
Local<Value> source_str = RB2V8(source);
|
||||
Local<Script> script = Script::Compile(source_str->ToString());
|
||||
Local<Value> result = script->Run();
|
||||
return V82RB(result);
|
||||
if (exceptions.HasCaught()) {
|
||||
return V8_Ref_Create(V8_C_Message, exceptions.Message());
|
||||
} else {
|
||||
return V82RB(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
|
||||
#include "converters.h"
|
||||
#include "v8_msg.h"
|
||||
#include "v8_ref.h"
|
||||
|
||||
|
@ -8,4 +8,10 @@ VALUE V8_C_Message;
|
|||
|
||||
VALUE V8_Wrap_Message(Handle<v8::Message> msg) {
|
||||
return V8_Ref_Create(V8_C_Message, msg);
|
||||
}
|
||||
|
||||
VALUE v8_Message_Get(VALUE self) {
|
||||
Local<Message> message = V8_Ref_Get<Message>(self);
|
||||
Local<Value> str = message->Get();
|
||||
return V82RB(str);
|
||||
}
|
|
@ -6,4 +6,5 @@
|
|||
|
||||
extern VALUE V8_C_Message;
|
||||
VALUE V8_Wrap_Message(v8::Handle<v8::Message> msg);
|
||||
VALUE v8_Message_Get(VALUE self);
|
||||
#endif
|
|
@ -6,14 +6,15 @@ module V8
|
|||
|
||||
def open(&block)
|
||||
@native.open do
|
||||
block.call(self).tap do |result|
|
||||
raise JavascriptError.new(result) if result.kind_of?(C::Message)
|
||||
end
|
||||
block.call(self)
|
||||
end if block_given?
|
||||
end
|
||||
|
||||
def eval(javascript)
|
||||
To.ruby @native.eval(javascript)
|
||||
@native.eval(javascript).tap do |result|
|
||||
raise JavascriptError.new(result) if result.kind_of?(C::Message)
|
||||
return To.ruby(result)
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate(*args)
|
||||
|
@ -36,6 +37,8 @@ module V8
|
|||
class ContextError < StandardError
|
||||
end
|
||||
class JavascriptError < StandardError
|
||||
|
||||
def initialize(v8_message)
|
||||
super(v8_message.Get())
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue