Code Monkey home page Code Monkey logo

dienashorner's Introduction

dia nashorner

Embed the Mozilla Rhino Nashorn JavaScript interpreter into Ruby.

Nashorn JavaScript engine is part of OpenJDK (since 8u40), try jjs -v.

Requirements

Java (Open/Oracle JRE) >= 8

jruby -S gem install dienashorner # make sure you have JRuby >= 1.7.x

Features

  • Evaluate JavaScript bits from the Ruby side
  • Embed Ruby objects into the JavaScript world
require 'nashorn'
  • evaluate some simple JavaScript
Nashorn.eval 'true + 100' #=> 101
Nashorn.eval_js '"4" + 2' #=> "42"
include Nashorn
eval_js "'1' + '' * 2" #=> "10"
  • if you need more control, use a Context
Nashorn::Context.open do |js|
  js['foo'] = "bar"
  js.eval('foo') # => "bar"
end
  • evaluate a Ruby function from JavaScript
Nashorn::Context.open do |js|
  js['say'] = lambda { |word, times| word * times }
  js.eval("say('Szia', 3) + '!'") #=> SziaSziaSzia!
end
  • embed a Ruby object into your JavaScript environment
class MyMath
  def plus(a, b); a + b + 1 end
end

Nashorn::Context.open do |js|
  js["math"] = MyMath.new
  js.eval("math.plus(20, 21)") #=> 42
end
  • make a Ruby object a JavaScript (global) environment
math = MyMath.new
Nashorn::Context.open(:with => math) do |js|
  js.eval("plus(20, 21)") #=> 42
end

Context Options

Mostly the same as with jjs e.g. Nashorn::Context.open(:strict => true).

Loading .js

  File.open('___.js') { |file| eval_js file }
  Nashorn::Context.open { |js| js.load('___.js') }

Configurable Ruby access

Ported over from Rhino

Rhino Compatibility

Nashorn was inspired (and crafted) from Rhino a.k.a therubyrhino JRuby gem. Far from being a drop-in replacement although there's require 'nashorn/rhino'.

ExecJS

dienashorner gem ships with an ExecJS compatible runtime, its best to load it (require 'nashorn/execjs/load') before ExecJS's auto-detection takes place :

gem 'execjs', require: false
gem 'dienashorner', platform: :jruby, require: [ 'nashorn/execjs/load', 'execjs' ]

when auto-detection is not used, set the runtime manually :

require 'execjs/module'
require 'nashorn/execjs/load'
ExecJS.runtime = ExecJS::NashornRuntime.new

Less.rb

Less.rb seems to be working (with hacks), for now you will need to :require 'nashorn/rhino/less' before require 'less'.

Copyright

Copyright (c) 2017 Karol Bucek. Apache License v2 (see LICENSE for details).

dienashorner's People

Contributors

kares avatar lephyrius avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

lephyrius

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.