h8 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+ require 'h8'
3
+
4
+ describe 'ruby gate' do
5
+
6
+ it 'should gate callables' do
7
+ cxt = H8::Context.new
8
+ cxt[:fn] = -> (a, b) {
9
+ a + b
10
+ }
11
+
12
+ res = cxt.eval "fn(11, 22);"
13
+ res.to_i.should == 33
14
+ cxt = nil
15
+ res = nil
16
+ GC.start
17
+ end
18
+
19
+ it 'should gate callables with varargs' do
20
+ cxt = H8::Context.new
21
+ cxt[:fn] = -> (*args) {
22
+ args.reduce(0){|all,x| all+x }
23
+ }
24
+
25
+ cxt.eval('fn(11, 22, 100);').should == 133
26
+ GC.start
27
+ end
28
+
29
+ it 'should convert nil' do
30
+ cxt = H8::Context.new
31
+ cxt[:fn] = -> {
32
+ nil
33
+ }
34
+ cxt.eval('fn();').should == nil
35
+ end
36
+
37
+ it 'should pass through ruby objects across js code' do
38
+ class MyObject
39
+ attr :some_val
40
+
41
+ def initialize some_val
42
+ @some_val = some_val
43
+ end
44
+ end
45
+
46
+ cxt = H8::Context.new
47
+ cxt[:fn] = -> (a, b) {
48
+ MyObject.new(a+b)
49
+ }
50
+
51
+ res = cxt.eval "fn(11, 22);"
52
+ res.should be_kind_of(MyObject)
53
+ res.some_val.should == 33
54
+ end
55
+
56
+ it 'should properly pass exceptions via callables' do
57
+ class MyException < StandardError;
58
+ end
59
+ cxt = H8::Context.new
60
+ cxt[:fn] = -> (a, b) {
61
+ raise MyException, "Shit happens"
62
+ }
63
+ res = cxt.eval <<-End
64
+ var result = "bad";
65
+ try {
66
+ result = fn(11, 22);
67
+ }
68
+ catch(e) {
69
+ result = { code: 'caught!', exception: e, message: e.message };
70
+ }
71
+ result;
72
+ End
73
+ res.should_not == 'bad'
74
+ res.code.should == 'caught!'
75
+ res.message.should == 'ruby exception'
76
+ x = res.exception.source
77
+ x.should be_kind_of(MyException)
78
+ x.message.should == 'Shit happens'
79
+ end
80
+
81
+ it 'should pass through uncaught ruby exceptions' do
82
+ class MyException < StandardError;
83
+ end
84
+ cxt = H8::Context.new
85
+ # pending
86
+ cxt[:fn] = -> (a, b) {
87
+ raise MyException, "Shit happens"
88
+ }
89
+ expect(-> {
90
+ res = cxt.eval <<-End
91
+ result = fn(11, 22);
92
+ End
93
+ }).to raise_error(MyException) { |e|
94
+ e.message.should == 'Shit happens'
95
+ }
96
+ end
97
+
98
+ it 'should have information about javascript exceptions'
99
+
100
+ context 'accessing ruby code' do
101
+ class Base
102
+ def base
103
+ raise "It should not be called"
104
+ end
105
+ end
106
+
107
+ class Test < Base
108
+ attr :ro
109
+ attr_accessor :rw
110
+
111
+ def initialize
112
+ @ro = 'readonly'
113
+ @rw = 'not initialized'
114
+ end
115
+
116
+ def test_args *args
117
+ args.join('-')
118
+ end
119
+
120
+ protected
121
+
122
+ def prot_method
123
+ raise 'should not be called'
124
+ end
125
+
126
+ private
127
+
128
+ def priv_method
129
+ raise 'should not be called'
130
+ end
131
+
132
+ end
133
+
134
+ it 'should access object properties and methods' do
135
+ cxt = H8::Context.new
136
+ cxt[:foo] = Test.new
137
+ cxt.eval('foo.ro').should == 'readonly'
138
+ cxt.eval('foo.rw').should == 'not initialized'
139
+ cxt.eval('foo.base').should == H8::Undefined
140
+ cxt.eval('foo.send').should == H8::Undefined
141
+ cxt.eval('foo.prot_method').should == H8::Undefined
142
+ cxt.eval('foo.priv_method').should == H8::Undefined
143
+ cxt.eval('foo.test_args').should be_kind_of(Proc)
144
+ cxt.eval('foo.test_args("hi", "you")').should == 'hi-you'
145
+ end
146
+
147
+ it 'should set ruby properties' do
148
+ cxt = H8::Context.new
149
+ cxt[:foo] = t = Test.new
150
+ cxt.eval('foo.rw="hello";')
151
+ t.rw.should == 'hello'
152
+ cxt.eval('foo.rw').should == 'hello'
153
+ end
154
+
155
+ it 'should gate classes'
156
+ end
157
+
158
+ it 'should retain ruby objects'
159
+ end
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
  RSpec.configure do |config|
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ # config.treat_symbols_as_metadata_keys_with_true_values = true
9
9
  config.run_all_when_everything_filtered = true
10
10
  config.filter_run :focus
11
11
 
@@ -14,4 +14,34 @@ RSpec.configure do |config|
14
14
  # the seed, which is printed after each run.
15
15
  # --seed 1234
16
16
  config.order = 'random'
17
+
18
+ config.expect_with :rspec do |expectations|
19
+ # This option will default to `true` in RSpec 4. It makes the `description`
20
+ # and `failure_message` of custom matchers include text for helper methods
21
+ # defined using `chain`, e.g.:
22
+ # be_bigger_than(2).and_smaller_than(4).description
23
+ # # => "be bigger than 2 and smaller than 4"
24
+ # ...rather than:
25
+ # # => "be bigger than 2"
26
+ expectations.syntax = [:should, :expect]
27
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
28
+ end
29
+
30
+ end
31
+
32
+ def true.true?
33
+ true
34
+ end
35
+
36
+ def true.false?
37
+ false
17
38
  end
39
+
40
+ def false.true?
41
+ false
42
+ end
43
+
44
+ def false.false?
45
+ true
46
+ end
47
+
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.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - sergeych
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-26 00:00:00.000000000 Z
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.14.0
69
- description: 'Should be more or less replacement for broken therubyracer gem and riny
69
+ description: 'Should be more or less replacement for broken therubyracer gem and ruby
70
70
  2.1+ '
71
71
  email:
72
72
  - real.sergeych@gmail.com
@@ -81,13 +81,22 @@ files:
81
81
  - LICENSE.txt
82
82
  - README.md
83
83
  - Rakefile
84
+ - ext/.DS_Store
85
+ - ext/h8/.cproject
86
+ - ext/h8/.project
87
+ - ext/h8/JsCatcher.cpp
88
+ - ext/h8/JsCatcher.h
89
+ - ext/h8/allocated_resource.h
90
+ - ext/h8/chain.h
84
91
  - ext/h8/extconf.rb
85
92
  - ext/h8/h8.cpp
86
93
  - ext/h8/h8.h
94
+ - ext/h8/js_gate.cpp
87
95
  - ext/h8/js_gate.h
88
96
  - ext/h8/main.cpp
89
97
  - ext/h8/object_wrap.h
90
- - ext/h8/ruby_wrap.h
98
+ - ext/h8/ruby_gate.cpp
99
+ - ext/h8/ruby_gate.h
91
100
  - hybrid8.gemspec
92
101
  - lib/h8.rb
93
102
  - lib/h8/context.rb
@@ -95,6 +104,7 @@ files:
95
104
  - lib/h8/version.rb
96
105
  - spec/context_spec.rb
97
106
  - spec/js_gate_spec.rb
107
+ - spec/ruby_gate_spec.rb
98
108
  - spec/spec_helper.rb
99
109
  homepage: ''
100
110
  licenses:
@@ -124,4 +134,5 @@ summary: Minimalistic and sane v8 bindings
124
134
  test_files:
125
135
  - spec/context_spec.rb
126
136
  - spec/js_gate_spec.rb
137
+ - spec/ruby_gate_spec.rb
127
138
  - spec/spec_helper.rb
@@ -1,32 +0,0 @@
1
- #ifndef __ruby_gate_h
2
- #define __ruby_gate_h
3
-
4
- #include "h8.h"
5
- #include "object_wrap.h"
6
-
7
- namespace h8 {
8
-
9
- class RubyWrap : public ObjectWrap {
10
- public:
11
-
12
- RubyWrap(H8* ctx) : context(ctx) {
13
- ctx->registerWrap(this);
14
- }
15
-
16
- public void setRubyInstance(VALUE instance) {
17
- this->instance = instance;
18
- }
19
-
20
- virtual ~RubyWrap() {
21
- context->unregisterWrap(this);
22
- }
23
-
24
- private:
25
- H8 *context;
26
- VALUE instance = Qnil;
27
-
28
- };
29
- }
30
-
31
-
32
- #endif