h8 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +68 -4
- data/ext/h8/extconf.rb +29 -10
- data/ext/h8/h8.cpp +16 -0
- data/ext/h8/h8.h +159 -0
- data/ext/h8/js_gate.h +196 -0
- data/ext/h8/main.cpp +162 -0
- data/ext/h8/object_wrap.h +138 -0
- data/ext/h8/ruby_wrap.h +32 -0
- data/hybrid8.gemspec +23 -4
- data/lib/h8.rb +9 -0
- data/lib/h8/context.rb +21 -0
- data/lib/h8/value.rb +77 -0
- data/lib/h8/version.rb +1 -1
- data/spec/context_spec.rb +39 -0
- data/spec/js_gate_spec.rb +162 -0
- data/spec/spec_helper.rb +17 -0
- metadata +39 -8
- data/lib/hybrid8.rb +0 -5
    
        data/lib/h8/version.rb
    CHANGED
    
    
| @@ -0,0 +1,39 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'h8'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe 'context' do
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              it 'should create' do
         | 
| 7 | 
            +
                cxt = H8::Context.new
         | 
| 8 | 
            +
                cxt.eval("'Res: ' + (2+5);")
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              it 'should gate simple values to JS context' do
         | 
| 12 | 
            +
                cxt = H8::Context.new foo: 'hello', bar: 'world'
         | 
| 13 | 
            +
                cxt[:sign] = '!'
         | 
| 14 | 
            +
                res = cxt.eval "foo+' '+bar+sign;"
         | 
| 15 | 
            +
                res.should == 'hello world!'
         | 
| 16 | 
            +
                cxt.set one: 101, real: 1.21
         | 
| 17 | 
            +
                cxt.eval("one + one;").should == 202
         | 
| 18 | 
            +
                cxt.eval("real + one;").should == (101 + 1.21)
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              it 'should gate H8::Values back to JS context' do
         | 
| 22 | 
            +
                cxt = H8::Context.new
         | 
| 23 | 
            +
                obj = cxt.eval "('che bel');"
         | 
| 24 | 
            +
                cxt[:first] = obj
         | 
| 25 | 
            +
                res = cxt.eval "first + ' giorno';"
         | 
| 26 | 
            +
                res.should == 'che bel giorno'
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              it 'should not gate H8::Values between contexts' do
         | 
| 30 | 
            +
                cxt = H8::Context.new
         | 
| 31 | 
            +
                obj = cxt.eval "('che bel');"
         | 
| 32 | 
            +
                cxt1 = H8::Context.new
         | 
| 33 | 
            +
                expect( -> {
         | 
| 34 | 
            +
                  cxt1[:first] = obj
         | 
| 35 | 
            +
                  res = cxt1.eval "first + ' giorno';"
         | 
| 36 | 
            +
                }).to raise_error(H8::Error)
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            end
         | 
| @@ -0,0 +1,162 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
            require 'h8'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe 'js_gate' do
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              it 'should return number as string' do
         | 
| 7 | 
            +
                cxt = H8::Context.new
         | 
| 8 | 
            +
                res = cxt.eval("0.2 + 122.1;")
         | 
| 9 | 
            +
                res.to_s.should == '122.3'
         | 
| 10 | 
            +
                # res.should_not be_integer
         | 
| 11 | 
            +
                # res.should_not be_string
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              it 'should return integers' do
         | 
| 15 | 
            +
                cxt = H8::Context.new
         | 
| 16 | 
            +
                res = cxt.eval("0.2 + 122.1;")
         | 
| 17 | 
            +
                res.to_i.should == 122
         | 
| 18 | 
            +
                res.should_not be_integer
         | 
| 19 | 
            +
                res = cxt.eval("11+22;")
         | 
| 20 | 
            +
                res.to_i.should == 33
         | 
| 21 | 
            +
                res.should be_integer
         | 
| 22 | 
            +
                res.should be_float
         | 
| 23 | 
            +
                res.should_not be_string
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              it 'should return floats' do
         | 
| 27 | 
            +
                cxt = H8::Context.new
         | 
| 28 | 
            +
                res = cxt.eval("0.2 + 122.1;")
         | 
| 29 | 
            +
                res.to_s.should == '122.3'
         | 
| 30 | 
            +
                res.should be_float
         | 
| 31 | 
            +
                res.should_not be_integer
         | 
| 32 | 
            +
                res.should_not be_string
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              it 'should return strings' do
         | 
| 36 | 
            +
                res = H8::Context.eval("'hel' + 'lo';")
         | 
| 37 | 
            +
                res.to_s.should == 'hello'
         | 
| 38 | 
            +
                res.should be_string
         | 
| 39 | 
            +
                res.should_not be_integer
         | 
| 40 | 
            +
                res.should_not be_float
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              it 'should retreive JS fieds as indexes' do
         | 
| 44 | 
            +
                res = H8::Context.eval("({ 'foo': 'bar', 'bar': 122 });")
         | 
| 45 | 
            +
                res['foo'].to_s.should == 'bar'
         | 
| 46 | 
            +
                res['bar'].to_i.should == 122
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              it 'should retreive JS fieds as properties' do
         | 
| 50 | 
            +
                res = H8::Context.eval("({ 'foo': 'bar', 'bar': 122 });")
         | 
| 51 | 
            +
                res.bar.to_i.should == 122
         | 
| 52 | 
            +
                res.foo.to_s.should == 'bar'
         | 
| 53 | 
            +
                res.foo.to_s.should == 'bar'
         | 
| 54 | 
            +
                res.bar.to_i.should == 122
         | 
| 55 | 
            +
                # cached method check
         | 
| 56 | 
            +
                res.foo.to_s.should == 'bar'
         | 
| 57 | 
            +
                res.bar.to_i.should == 122
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              it 'should access arrays' do
         | 
| 61 | 
            +
                res = H8::Context.eval("[-10, 'foo', 'bar'];")
         | 
| 62 | 
            +
                res.should_not be_undefined
         | 
| 63 | 
            +
                res.array?.should be_true
         | 
| 64 | 
            +
                res.length.should == 3
         | 
| 65 | 
            +
                3.times {
         | 
| 66 | 
            +
                  res[0].to_i.should == -10
         | 
| 67 | 
            +
                  res[1].to_s.should == 'foo'
         | 
| 68 | 
            +
                  res[2].to_s.should == 'bar'
         | 
| 69 | 
            +
                }
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              it 'should eval and keep context alive' do
         | 
| 73 | 
            +
                obj = H8::Context.eval("({ 'foo': 'bar', 'bar': 122 });")
         | 
| 74 | 
            +
                GC.start # Here Context of obj that is not referenced should be kept
         | 
| 75 | 
            +
                obj.foo.should == 'bar'
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              it 'should convert simple types to ruby' do
         | 
| 79 | 
            +
                res = H8::Context.eval("({ 'foo': 'bar', 'bar': 122, pi: 3.1415 });")
         | 
| 80 | 
            +
                r   = res.foo.to_ruby
         | 
| 81 | 
            +
                r.should be_kind_of(String)
         | 
| 82 | 
            +
                r.should == 'bar'
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                r = res.bar.to_ruby
         | 
| 85 | 
            +
                r.should be_kind_of(Fixnum)
         | 
| 86 | 
            +
                r.should == 122
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                r = res.pi.to_ruby
         | 
| 89 | 
            +
                r.should be_kind_of(Float)
         | 
| 90 | 
            +
                (r == 3.1415).should be_true
         | 
| 91 | 
            +
              end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
              it 'should convert arrays to ruby' do
         | 
| 94 | 
            +
                res = H8::Context.eval("[-10, 'foo', 'bar'];")
         | 
| 95 | 
            +
                res.to_ruby.should == [-10, 'foo', 'bar']
         | 
| 96 | 
            +
                res.to_ary.should == [-10, 'foo', 'bar']
         | 
| 97 | 
            +
                expect(-> { H8::Context.eval("'not array'").to_ary}).to raise_error(H8::Error)
         | 
| 98 | 
            +
              end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
              it 'should provide hash methods' do
         | 
| 101 | 
            +
                pending
         | 
| 102 | 
            +
                obj = H8::Context.eval("({ 'foo': 'bar', 'bar': 122 });")
         | 
| 103 | 
            +
                obj.keys.should == ['foo', 'bar']
         | 
| 104 | 
            +
              end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
              it 'should convert compare to ruby objects' do
         | 
| 107 | 
            +
                res = H8::Context.eval("({ 'foo': 'bar', 'bar': 122 });")
         | 
| 108 | 
            +
                (res.foo != 'bar').should be_false
         | 
| 109 | 
            +
                (res.foo == 'bar').should be_true
         | 
| 110 | 
            +
                (res.foo != 'ba1').should be_true
         | 
| 111 | 
            +
                (res.foo == 'ba1').should be_false
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                (res.bar != 'bar').should be_true
         | 
| 114 | 
            +
                (res.bar == 'bar').should be_false
         | 
| 115 | 
            +
                (res.bar != 122).should be_false
         | 
| 116 | 
            +
                (res.bar == 122).should be_true
         | 
| 117 | 
            +
                (res.bar <= 122).should be_true
         | 
| 118 | 
            +
                (res.bar <= 123).should be_true
         | 
| 119 | 
            +
                (res.bar >= 122).should be_true
         | 
| 120 | 
            +
                (res.bar >= 121).should be_true
         | 
| 121 | 
            +
             | 
| 122 | 
            +
                (res.bar > 120).should be_true
         | 
| 123 | 
            +
                (res.bar < 130).should be_true
         | 
| 124 | 
            +
                (res.bar > 129).should be_false
         | 
| 125 | 
            +
                (res.bar < 19).should be_false
         | 
| 126 | 
            +
              end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
              it 'should call functions with no args' do
         | 
| 129 | 
            +
                res = H8::Context.eval "(function() { return 'sono callable'; });"
         | 
| 130 | 
            +
                res.call('a', '1', '2').should == 'sono callable'
         | 
| 131 | 
            +
              end
         | 
| 132 | 
            +
             | 
| 133 | 
            +
              it 'should call functions with args' do
         | 
| 134 | 
            +
                res = H8::Context.eval "(function(a, b) { return a + b; });"
         | 
| 135 | 
            +
                res.call('10', '1').should == '101'
         | 
| 136 | 
            +
                res.call(10, 1).should == 11
         | 
| 137 | 
            +
              end
         | 
| 138 | 
            +
             | 
| 139 | 
            +
              it 'should raise error on syntax' do
         | 
| 140 | 
            +
                expect( -> {
         | 
| 141 | 
            +
                  H8::Context.eval 'this is not a valid js'
         | 
| 142 | 
            +
                }).to raise_error(H8::Error)
         | 
| 143 | 
            +
              end
         | 
| 144 | 
            +
             | 
| 145 | 
            +
              it 'should call member functions only' do
         | 
| 146 | 
            +
                res = H8::Context.eval <<-End
         | 
| 147 | 
            +
                  function cls(base) {
         | 
| 148 | 
            +
                    this.base = base;
         | 
| 149 | 
            +
                    this.someVal = 'hello!';
         | 
| 150 | 
            +
                    this.noArgs = function() { return 'world!'};
         | 
| 151 | 
            +
                    this.doAdd = function(a, b) {
         | 
| 152 | 
            +
                      return a + b + base;
         | 
| 153 | 
            +
                    }
         | 
| 154 | 
            +
                  }
         | 
| 155 | 
            +
                  new cls(100);
         | 
| 156 | 
            +
                End
         | 
| 157 | 
            +
                res.someVal.should == 'hello!'
         | 
| 158 | 
            +
                res.noArgs.should == 'world!'
         | 
| 159 | 
            +
                res.doAdd(10, 1).should == 111
         | 
| 160 | 
            +
              end
         | 
| 161 | 
            +
             | 
| 162 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            # This file was generated by the `rspec --init` command. Conventionally, all
         | 
| 2 | 
            +
            # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
         | 
| 3 | 
            +
            # Require this file using `require "spec_helper"` to ensure that it is only
         | 
| 4 | 
            +
            # loaded once.
         | 
| 5 | 
            +
            #
         | 
| 6 | 
            +
            # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
         | 
| 7 | 
            +
            RSpec.configure do |config|
         | 
| 8 | 
            +
              config.treat_symbols_as_metadata_keys_with_true_values = true
         | 
| 9 | 
            +
              config.run_all_when_everything_filtered = true
         | 
| 10 | 
            +
              config.filter_run :focus
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              # Run specs in random order to surface order dependencies. If you find an
         | 
| 13 | 
            +
              # order dependency and want to debug it, you can fix the order by providing
         | 
| 14 | 
            +
              # the seed, which is printed after each run.
         | 
| 15 | 
            +
              #     --seed 1234
         | 
| 16 | 
            +
              config.order = 'random'
         | 
| 17 | 
            +
            end
         | 
    
        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. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 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- | 
| 11 | 
            +
            date: 2014-11-26 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         | 
| @@ -39,36 +39,63 @@ dependencies: | |
| 39 39 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 40 | 
             
                    version: '0'
         | 
| 41 41 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            -
              name:  | 
| 42 | 
            +
              name: rake-compiler
         | 
| 43 43 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 44 | 
             
                requirements:
         | 
| 45 45 | 
             
                - - ">="
         | 
| 46 46 | 
             
                  - !ruby/object:Gem::Version
         | 
| 47 47 | 
             
                    version: '0'
         | 
| 48 | 
            -
              type: : | 
| 48 | 
            +
              type: :development
         | 
| 49 49 | 
             
              prerelease: false
         | 
| 50 50 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 51 | 
             
                requirements:
         | 
| 52 52 | 
             
                - - ">="
         | 
| 53 53 | 
             
                  - !ruby/object:Gem::Version
         | 
| 54 54 | 
             
                    version: '0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: rspec
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ">="
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: 2.14.0
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: 2.14.0
         | 
| 55 69 | 
             
            description: 'Should be more or less replacement for broken therubyracer gem and riny
         | 
| 56 70 | 
             
              2.1+ '
         | 
| 57 71 | 
             
            email:
         | 
| 58 | 
            -
            - sergeych
         | 
| 72 | 
            +
            - real.sergeych@gmail.com
         | 
| 59 73 | 
             
            executables: []
         | 
| 60 | 
            -
            extensions: | 
| 74 | 
            +
            extensions:
         | 
| 75 | 
            +
            - ext/h8/extconf.rb
         | 
| 61 76 | 
             
            extra_rdoc_files: []
         | 
| 62 77 | 
             
            files:
         | 
| 63 78 | 
             
            - ".gitignore"
         | 
| 79 | 
            +
            - ".rspec"
         | 
| 64 80 | 
             
            - Gemfile
         | 
| 65 81 | 
             
            - LICENSE.txt
         | 
| 66 82 | 
             
            - README.md
         | 
| 67 83 | 
             
            - Rakefile
         | 
| 68 84 | 
             
            - ext/h8/extconf.rb
         | 
| 85 | 
            +
            - ext/h8/h8.cpp
         | 
| 86 | 
            +
            - ext/h8/h8.h
         | 
| 87 | 
            +
            - ext/h8/js_gate.h
         | 
| 88 | 
            +
            - ext/h8/main.cpp
         | 
| 89 | 
            +
            - ext/h8/object_wrap.h
         | 
| 90 | 
            +
            - ext/h8/ruby_wrap.h
         | 
| 69 91 | 
             
            - hybrid8.gemspec
         | 
| 92 | 
            +
            - lib/h8.rb
         | 
| 93 | 
            +
            - lib/h8/context.rb
         | 
| 94 | 
            +
            - lib/h8/value.rb
         | 
| 70 95 | 
             
            - lib/h8/version.rb
         | 
| 71 | 
            -
            -  | 
| 96 | 
            +
            - spec/context_spec.rb
         | 
| 97 | 
            +
            - spec/js_gate_spec.rb
         | 
| 98 | 
            +
            - spec/spec_helper.rb
         | 
| 72 99 | 
             
            homepage: ''
         | 
| 73 100 | 
             
            licenses:
         | 
| 74 101 | 
             
            - MIT
         | 
| @@ -77,6 +104,7 @@ post_install_message: | |
| 77 104 | 
             
            rdoc_options: []
         | 
| 78 105 | 
             
            require_paths:
         | 
| 79 106 | 
             
            - lib
         | 
| 107 | 
            +
            - ext
         | 
| 80 108 | 
             
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 81 109 | 
             
              requirements:
         | 
| 82 110 | 
             
              - - ">="
         | 
| @@ -93,4 +121,7 @@ rubygems_version: 2.2.2 | |
| 93 121 | 
             
            signing_key: 
         | 
| 94 122 | 
             
            specification_version: 4
         | 
| 95 123 | 
             
            summary: Minimalistic and sane v8 bindings
         | 
| 96 | 
            -
            test_files: | 
| 124 | 
            +
            test_files:
         | 
| 125 | 
            +
            - spec/context_spec.rb
         | 
| 126 | 
            +
            - spec/js_gate_spec.rb
         | 
| 127 | 
            +
            - spec/spec_helper.rb
         |