Giter Club home page Giter Club logo

hybrid8's Introduction

Hybrid8, aka H8

Warning this gem has yet limited functionality and is under main development. Versions suitable for an open beta test will start from 0.1.*.

Therubyracer gem alternative to effectively work with ruby 2.1+ in multithreaded environment in an effective and GC-safe way. Should be out of the box replacement for most scenarios.

Special features:

  • care about wrapped Ruby objects lifespan (are kept until either ruby or javascript context reference wrapped object). In no way GC will not reclaim ruby object that is in use by the javascript context

  • care about wrapped Javascript objects lifetime the same. Referenced JS items will not be recycled while there are ruby objects referencing it. It also means that once created H8::Context will not be reclaimed as long as there is at least one active wrapped object returned from the script.

Installation

Prerequisites

You should have installed libv8, use latest version with v8::Isolate and v8::Locker. This version may not find you installation, contact me if you have problems, I'll tune it up.

Setting up

Add this line to your application's Gemfile:

gem 'hybrid8'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hybrid8

Usage

Is generally like therubyracer gem. Create context, set variables, run scripts.

require 'h8'

res = H8::Context.eval "({foo: 'hello', bar: 'world'});"
puts "#{res.foo} #{res.bar}"

another way to access attributes:

puts res['foo']

The same works with arrays:

res = H8::Context.eval "['foo', bar'];"
puts res[1]

To set context variables:

cxt = H8::Context.new some: 'value'
cxt[:pi] = 3.1415

You can return function and call it from ruby:

fun = cxt.eval "(function pi_n(n) { return pi * n; })"
p fun(2)

The same you can return objects and call its member functions - if a member is a function, it will be called with given arguments:

res = H8::Context.eval <<-End
  function cls(base) {
    this.base = base;
    this.someVal = 'hello!';
    this.noArgs = function() { return 'world!'};
    this.doAdd = function(a, b) {
      return a + b + base;
    }
  }
  new cls(100);
End
res.someVal.should == 'hello!'
res.noArgs.should == 'world!'
res.doAdd(10, 1).should == 111

Contributing

Note that at this early point of development you better first talk to me to not to reinvent the wheel.

  1. Fork it ( https://github.com/[my-github-username]/hybrid8/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

hybrid8's People

Contributors

porzione avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.