https://github.com/kares/dienashorner
Ein Next-Generation-JavaScript-Engine für die J(Ruby)VM
https://github.com/kares/dienashorner
execjs nashorn rhino
Last synced: about 1 year ago
JSON representation
Ein Next-Generation-JavaScript-Engine für die J(Ruby)VM
- Host: GitHub
- URL: https://github.com/kares/dienashorner
- Owner: kares
- License: apache-2.0
- Created: 2016-03-15T08:51:11.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-07T14:47:54.000Z (about 8 years ago)
- Last Synced: 2025-04-27T12:09:20.462Z (about 1 year ago)
- Topics: execjs, nashorn, rhino
- Language: JavaScript
- Homepage: https://blogs.oracle.com/nashorn/
- Size: 443 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dia nashorner
Embed the ~~Mozilla Rhino~~ [Nashorn][0] JavaScript interpreter into Ruby.
Nashorn JavaScript engine is part of [OpenJDK][4] (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
```ruby
require 'nashorn'
```
* evaluate some simple JavaScript
```ruby
Nashorn.eval 'true + 100' #=> 101
Nashorn.eval_js '"4" + 2' #=> "42"
```
```ruby
include Nashorn
eval_js "'1' + '' * 2" #=> "10"
```
* if you need more control, use a `Context`
```ruby
Nashorn::Context.open do |js|
js['foo'] = "bar"
js.eval('foo') # => "bar"
end
```
* evaluate a Ruby function from JavaScript
```ruby
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
```ruby
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
```ruby
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
```ruby
File.open('___.js') { |file| eval_js file }
```
```ruby
Nashorn::Context.open { |js| js.load('___.js') }
```
### Configurable Ruby access
Ported over from [Rhino](https://github.com/cowboyd/therubyrhino#configurable-ruby-access)
### 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][3] compatible runtime, its best to load it
(`require 'nashorn/execjs/load'`) before ExecJS's auto-detection takes place :
```ruby
gem 'execjs', require: false
gem 'dienashorner', platform: :jruby, require: [ 'nashorn/execjs/load', 'execjs' ]
```
when auto-detection is not used, set the runtime manually :
```ruby
require 'execjs/module'
require 'nashorn/execjs/load'
ExecJS.runtime = ExecJS::NashornRuntime.new
```
### Less.rb
[Less.rb](https://github.com/cowboyd/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).
[0]: http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html
[3]: https://github.com/rails/execjs
[4]: http://openjdk.java.net/projects/nashorn/