- Add support for objects

- Correct warnings on mac
This commit is contained in:
Sam Saffron 2016-05-12 09:38:47 +10:00
parent 73217e3573
commit 1218198afa
2 changed files with 29 additions and 4 deletions

View File

@ -151,7 +151,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
if (value->IsArray()) {
VALUE rb_array = rb_ary_new();
Local<Array> arr = Local<Array>::Cast(value);
for(int i=0; i<arr->Length(); i++) {
for(uint32_t i=0; i < arr->Length(); i++) {
Local<Value> element = arr->Get(i);
VALUE rb_elem = convert_v8_to_ruby(isolate, element);
rb_ary_push(rb_array, rb_elem);
@ -159,6 +159,24 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Handle<Value> &value) {
return rb_array;
}
if (value->IsObject()) {
VALUE rb_hash = rb_hash_new();
Local<Context> context = Context::New(isolate);
Local<Object> object = value->ToObject();
MaybeLocal<Array> maybe_props = object->GetOwnPropertyNames(context);
if (!maybe_props.IsEmpty()) {
Local<Array> props = maybe_props.ToLocalChecked();
for(uint32_t i=0; i < props->Length(); i++) {
Local<Value> key = props->Get(i);
VALUE rb_key = convert_v8_to_ruby(isolate, key);
Local<Value> value = object->Get(key);
VALUE rb_value = convert_v8_to_ruby(isolate, value);
rb_hash_aset(rb_hash, rb_key, rb_value);
}
}
return rb_hash;
}
Local<String> rstr = value->ToString();
return rb_enc_str_new(*v8::String::Utf8Value(rstr), rstr->Utf8Length(), rb_enc_find("utf-8"));
}
@ -273,7 +291,8 @@ static VALUE rb_context_eval_unsafe(VALUE self, VALUE str) {
rb_raise(rb_eJavaScriptError, "Unknown JavaScript Error during execution");
}
} else {
rb_raise(CLASS_OF(ruby_exception), RSTRING_PTR(rb_funcall(ruby_exception, rb_intern("to_s"), 0)));
VALUE rb_str = rb_funcall(ruby_exception, rb_intern("to_s"), 0);
rb_raise(CLASS_OF(ruby_exception), RSTRING_PTR(rb_str));
}
}
@ -356,8 +375,8 @@ gvl_ruby_callback(void* data) {
callback_data.args = ruby_args;
callback_data.failed = false;
result = rb_rescue(protected_callback, (VALUE)(&callback_data),
rescue_callback, (VALUE)(&callback_data));
result = rb_rescue((VALUE(*)(...))&protected_callback, (VALUE)(&callback_data),
(VALUE(*)(...))&rescue_callback, (VALUE)(&callback_data));
if(callback_data.failed) {
VALUE parent = rb_iv_get(self, "@parent");

View File

@ -22,6 +22,12 @@ class MiniRacerTest < Minitest::Test
assert_equal [1,"two"], context.eval('[1,"two"]')
end
def test_object
context = MiniRacer::Context.new
# remember JavaScript is quirky {"1" : 1} magically turns to {1: 1} cause magic
assert_equal({1 => 2, "two" => "two"}, context.eval('a={"1" : 2, "two" : "two"}'))
end
def test_it_returns_runtime_error
context = MiniRacer::Context.new
exp = nil