h8 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/h8/h8.cpp +11 -0
- data/ext/h8/h8.h +1 -1
- data/ext/h8/ruby_gate.cpp +18 -12
- data/lib/h8/context.rb +30 -3
- data/lib/h8/errors.rb +4 -0
- data/lib/h8/version.rb +1 -1
- data/lib/scripts/globals.coffee +6 -0
- data/spec/ruby_gate_spec.rb +37 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fbec1007c8688390ed4e78bcb8fb828d6f1335a
|
4
|
+
data.tar.gz: 1fc40b29112ab15dc624aea10bb21e7e136d353c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 886728e46a82de26977f1c3d517b1ec2d719ed9828cc47782eb6f13fe520928af145e409aab5de52b5ea780f9301f5983a4fe85827f2f6dd9b5155e155c283a5
|
7
|
+
data.tar.gz: 848d3efdb932098ac8d756e9bee535f222c6af8a66e4dcb17203024c9d70152767692535c8eb12067b48c51652de9b2a4bf8c54d949a5d8394e41aa628c7296a
|
data/ext/h8/h8.cpp
CHANGED
@@ -56,11 +56,18 @@ void h8::JsTimeoutError::raise() const {
|
|
56
56
|
rb_raise(js_timeout_exception, "timeout expired");
|
57
57
|
}
|
58
58
|
|
59
|
+
//static void prototype_cb(Local<String> prop,const PropertyCallbackInfo<Value>& info) {
|
60
|
+
// puts("PROTOCB!");
|
61
|
+
// info.GetReturnValue().Set(info.This()->GetPrototype());
|
62
|
+
//}
|
63
|
+
|
59
64
|
void h8::H8::SetupGateTemplate(const Local<ObjectTemplate>& templ) {
|
60
65
|
templ->SetInternalFieldCount(2);
|
61
66
|
templ->SetCallAsFunctionHandler(&RubyGate::ObjectCallback);
|
62
67
|
templ->SetNamedPropertyHandler(RubyGate::mapGet, RubyGate::mapSet, 0, RubyGate::mapDelete);
|
63
68
|
templ->SetIndexedPropertyHandler(RubyGate::indexGet, RubyGate::indexSet);
|
69
|
+
// templ->SetAccessor(String::NewFromUtf8(isolate, "prototype"),
|
70
|
+
// prototype_cb);
|
64
71
|
}
|
65
72
|
|
66
73
|
h8::H8::H8()
|
@@ -95,6 +102,10 @@ h8::H8::H8()
|
|
95
102
|
persistent_context.Reset(isolate, context);
|
96
103
|
gate_function_template.Reset(isolate,ft);
|
97
104
|
gate_function.Reset(isolate,fn);
|
105
|
+
|
106
|
+
ft->Set(String::NewFromUtf8(isolate, "prototype"),
|
107
|
+
ft->GetFunction());
|
108
|
+
|
98
109
|
}
|
99
110
|
|
100
111
|
|
data/ext/h8/h8.h
CHANGED
@@ -233,7 +233,7 @@ public:
|
|
233
233
|
private:
|
234
234
|
friend VALUE h8::context_alloc(VALUE klass);
|
235
235
|
void invoke(v8::Handle<v8::Script> script, Local<Value>& result);
|
236
|
-
|
236
|
+
void SetupGateTemplate(const Local<ObjectTemplate>& templ);
|
237
237
|
|
238
238
|
Isolate *isolate;
|
239
239
|
VALUE self;
|
data/ext/h8/ruby_gate.cpp
CHANGED
@@ -75,6 +75,12 @@ h8::RubyGate::RubyGate(H8* _context, Handle<Object> instance, VALUE object) :
|
|
75
75
|
|
76
76
|
void h8::RubyGate::mapGet(Local<String> name,
|
77
77
|
const PropertyCallbackInfo<Value> &info) {
|
78
|
+
v8::String::Utf8Value val(name);
|
79
|
+
if( strcmp(*val, "prototype") == 0 ) {
|
80
|
+
info.GetReturnValue().Set(info.This()->GetPrototype());
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
78
84
|
Local<Value> loc = info.This()->GetRealNamedPropertyInPrototypeChain(name);
|
79
85
|
if (!loc.IsEmpty())
|
80
86
|
info.GetReturnValue().Set(loc);
|
@@ -220,14 +226,14 @@ void h8::RubyGate::setProperty(Local<String> name, Local<Value> value,
|
|
220
226
|
with_gvl(this, [&] {
|
221
227
|
VALUE rb_args = rb_ary_new2(3);
|
222
228
|
rb_ary_push(rb_args, context->to_ruby(value)); // value
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
+
rb_ary_push(rb_args, ruby_object);// object
|
230
|
+
VALUE method = context->to_ruby(name);
|
231
|
+
method = rb_str_cat2(method, "=");
|
232
|
+
rb_ary_push(rb_args, method);// name=
|
233
|
+
rescued_call(rb_args, secure_call, [&] (VALUE res) {
|
234
|
+
info.GetReturnValue().Set(context->to_js(res));
|
235
|
+
});
|
229
236
|
});
|
230
|
-
});
|
231
237
|
}
|
232
238
|
|
233
239
|
void h8::RubyGate::deleteProperty(Local<String> name,
|
@@ -235,12 +241,12 @@ void h8::RubyGate::deleteProperty(Local<String> name,
|
|
235
241
|
with_gvl(this, [&] {
|
236
242
|
VALUE rb_args = rb_ary_new2(2);
|
237
243
|
rb_ary_push(rb_args, context->to_ruby(name)); // name
|
238
|
-
|
244
|
+
rb_ary_push(rb_args, ruby_object);// object
|
239
245
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
246
|
+
rescued_call(rb_args, ruby_delete_handler, [&] (VALUE res) {
|
247
|
+
auto success = Boolean::New(isolate(), res == Qnil ? false : true );
|
248
|
+
info.GetReturnValue().Set(success);
|
249
|
+
});
|
244
250
|
});
|
245
251
|
}
|
246
252
|
|
data/lib/h8/context.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'thread'
|
2
2
|
require 'h8'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
class Array
|
5
6
|
def _select_js callable
|
@@ -7,6 +8,26 @@ class Array
|
|
7
8
|
callable.call item
|
8
9
|
}
|
9
10
|
end
|
11
|
+
|
12
|
+
def indexOf item
|
13
|
+
index(item) || -1
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class String
|
18
|
+
def indexOf item
|
19
|
+
index(item) || -1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Object
|
24
|
+
def __to_json
|
25
|
+
if respond_to?(:as_json)
|
26
|
+
as_json
|
27
|
+
else
|
28
|
+
JSON[JSON[self]]
|
29
|
+
end
|
30
|
+
end
|
10
31
|
end
|
11
32
|
|
12
33
|
module H8
|
@@ -19,7 +40,12 @@ module H8
|
|
19
40
|
_set_var '___create_ruby_class', -> (cls, args) {
|
20
41
|
_do_create_ruby_class cls, args
|
21
42
|
}
|
22
|
-
|
43
|
+
|
44
|
+
self[:debug] = -> (*args) {
|
45
|
+
puts args.join(' ')
|
46
|
+
}
|
47
|
+
|
48
|
+
noglobals or execute_script 'globals.coffee'
|
23
49
|
end
|
24
50
|
|
25
51
|
# set variables from keyword arguments to this context
|
@@ -76,6 +102,7 @@ module H8
|
|
76
102
|
# It has very complex logic so the security model update should be done somehow
|
77
103
|
# else.
|
78
104
|
def self.secure_call instance, method, args=nil
|
105
|
+
# p [:sc, instance, method, args]
|
79
106
|
if instance.is_a?(Array)
|
80
107
|
method == 'select' and method = '_select_js'
|
81
108
|
end
|
@@ -177,9 +204,9 @@ module H8
|
|
177
204
|
@@cache = {}
|
178
205
|
|
179
206
|
def execute_script name
|
180
|
-
p [:exs, name]
|
207
|
+
# p [:exs, name]
|
181
208
|
script = @@cache[name] ||= begin
|
182
|
-
p 'cache miss'
|
209
|
+
# p 'cache miss'
|
183
210
|
script = open(File.join(@@base, name), 'r').read
|
184
211
|
name.downcase.end_with?('.coffee') ? H8::Coffee.compile(script) : script
|
185
212
|
end
|
data/lib/h8/errors.rb
CHANGED
data/lib/h8/version.rb
CHANGED
data/lib/scripts/globals.coffee
CHANGED
data/spec/ruby_gate_spec.rb
CHANGED
@@ -368,11 +368,42 @@ describe 'ruby gate' do
|
|
368
368
|
end
|
369
369
|
|
370
370
|
it 'should access ruby and java array functions' do
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
371
|
+
begin
|
372
|
+
cxt = H8::Context.new
|
373
|
+
src = cxt[:h] = [1, 20, 3, 4, 5]
|
374
|
+
cxt.eval("h.reverse()").should == [5, 4, 3, 20, 1]
|
375
|
+
cxt.eval("h.sort()").should == [1, 3, 4, 5, 20]
|
376
|
+
cxt.eval("h.select(function(x){return x >=4;}).sort()").should == [4, 5, 20]
|
377
|
+
cxt.eval("h.indexOf(20)").should == 1
|
378
|
+
cxt.eval("h.indexOf(21)").should == -1
|
379
|
+
rescue H8::NestedError => e
|
380
|
+
puts e.ruby_error.backtrace.join("\n")
|
381
|
+
raise
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'should access ruby string functions' do
|
386
|
+
begin
|
387
|
+
cxt = H8::Context.new
|
388
|
+
src = cxt[:h] = "Hello!"
|
389
|
+
cxt.eval("h.indexOf('ll')").should == 2
|
390
|
+
cxt.eval("h.indexOf('meow')").should == -1
|
391
|
+
rescue H8::NestedError => e
|
392
|
+
puts e.ruby_error.backtrace.join("\n")
|
393
|
+
raise
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
it 'should process to_json' do
|
398
|
+
begin
|
399
|
+
cxt = H8::Context.new
|
400
|
+
src = cxt[:h] = { 'hello' => 'world' }
|
401
|
+
cxt.eval("JSON.stringify(h)").should == "{\"hello\":\"world\"}"
|
402
|
+
rescue H8::NestedError => e
|
403
|
+
puts e.ruby_error.backtrace.join("\n")
|
404
|
+
raise
|
405
|
+
end
|
406
|
+
|
376
407
|
end
|
377
408
|
|
378
409
|
it 'should pass varargs' do
|
@@ -458,7 +489,7 @@ describe 'ruby gate' do
|
|
458
489
|
# We call gated ruby object with wrong number of args
|
459
490
|
# which in turn causes attempt to call not callable result:
|
460
491
|
expect(-> { cxt.eval('g1.checkself(12)') }).to raise_error(H8::NestedError) { |e|
|
461
|
-
|
492
|
+
e.ruby_error.should be_instance_of(NoMethodError)
|
462
493
|
}
|
463
494
|
end
|
464
495
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: h8
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sergeych
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|