rubyjs / therubyrhino Goto Github PK
View Code? Open in Web Editor NEWEmbed the Mozilla Rhino Javascript interpreter into Ruby
Embed the Mozilla Rhino Javascript interpreter into Ruby
I've posted a canned example at:
https://gist.github.com/1780772
Run test.js on the provided test_data.json
As per the output in the gist, the elements of the 'sections' array in the top-level hash are present in Ruby but not in the object passed to JavaScript.
This works fine in therubyracer (V8).
I can reproduce this with jruby 1.6.4 and 1.6.6.
I'm guessing there is a good reason for it, but I'm curious what it is?
As soon as I try to require "rhino"
, I get this error:
NoMethodError: undefined method `useMozillaStackStyle' for Java::OrgMozillaJavascript::RhinoException:Class
from /Users/adam/.rvm/gems/jruby-1.7.8/gems/therubyrhino-1.73.5/lib/rhino/error.rb:71:in `JSError'
from /Users/adam/.rvm/gems/jruby-1.7.8/gems/therubyrhino-1.73.5/lib/rhino/error.rb:3:in `Rhino'
from /Users/adam/.rvm/gems/jruby-1.7.8/gems/therubyrhino-1.73.5/lib/rhino/error.rb:1:in `(root)'
from org/jruby/RubyKernel.java:1084:in `require'
from /Users/adam/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1:in `(root)'
from /Users/adam/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55:in `require'
from org/jruby/RubyKernel.java:1084:in `require'
from /Users/adam/.rvm/gems/jruby-1.7.8/gems/therubyrhino-1.73.5/lib/rhino.rb:26:in `(root)'
from /Users/adam/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1:in `(root)'
from org/jruby/RubyKernel.java:1123:in `eval'
from /Users/adam/.rvm/rubies/jruby-1.7.8/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:135:in `require'
from (irb):1:in `evaluate'
from org/jruby/RubyKernel.java:1519:in `loop'
from org/jruby/RubyKernel.java:1284:in `catch'
from org/jruby/RubyKernel.java:1284:in `catch'
from /Users/adam/.rvm/rubies/jruby-1.7.8/bin/irb:13:in `(root)'
This happens for in both version 1.73.5 and version 2.0.3 of therubyrhino. The only way to get it to work is to comment out the offending line. Any idea why this might be happening to me?
Hey guys,
just upgraded jruby to 1.7.13 and my less compiler, which uses the ruby rhino for its js execution started acting up. Not sure whether it is an error in jruby or the ruby rhino. In any case, delegation of method calls to javascript objects seems to be affected.
2014-08-06 12:30:00 +0200: Rack app error: java.lang.NullPointerException
org.jruby.java.proxies.ArrayJavaProxy.to_a(org/jruby/java/proxies/ArrayJavaProxy.java:145)
RUBY.method_missing(/Users/slawo/.rvm/gems/jruby-1.7.13/gems/therubyrhino-2.0.3/lib/rhino/rhino_ext.rb:115)
org.mozilla.javascript.optimizer.OptRuntime.callName(org/mozilla/javascript/optimizer/OptRuntime.java:63)
org.mozilla.javascript.gen._Users_slawo__rvm_gems_jruby_1_7_13_gems_less_2_5_0_lib_less_js_lib_less_parser_js_2._c_anonymous_31(/Users/slawo/.rvm/gems/jruby-1.7.13/gems/less-2.5.0/lib/less/js/lib/less/parser.js:669)
The error does not happen with jruby 1.7.11/12.
I am running on Oracle Java on Mac OSX:
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
There have been numerous reports of therubyrhino being too slow to use in day-to-day development, specifically when precompiling assets. I wonder if it is because Rhino is not configure to optimize code?
http://www.mozilla.org/rhino/opt.html
I do not see anything in therubyrhino codebase that would alter whatever Rhino has as its default, so it is possible that it is only running in interpreted mode. Interpreted mode would almost certainly be much slower to execute.
This bug is to evaluate the available optimization modes and determine whether therubyrhino should set a higher optimization level by default than whatever Rhino has as its default. This may alleviate problems users are having with Rhino-based asset compilation running too slowly for normal use.
Hey guys,
I just recently started using therubyrhino as I'm trying to use JRuby more in production. I noticed that it's much slower then therubyracer (v8). I was wondering if there is something I can do to speed this up?
Also, I'm using this to compile handlebars.js templates via an API, and just for testing, I turned off all caching and hit the endpoint continually, after about 300 requests, I get lots of Java::JavaLang::OutOfMemoryError errors... any suggestions? I know I can tune the amount of Perm memory used by the Java process, however, I just wanted to make sure there isn't a memory leak or that I'm doing something wrong that throws off the GC.
Here is the method: https://gist.github.com/921174ac19790da21b42
Thx.
Peter
This call to getJSTimeValue
fails in jruby-9.2:
module Rhino
module To
def to_ruby(object)
case object
# ...
when JS::NativeDate then Time.at(object.getJSTimeValue / 1000)
$ jruby --version jruby 9.1.17.0 (2.3.3) 2018-04-20 d8b1ff9 OpenJDK 64-Bit Server VM 25.252-b09 on 1.8.0_252-8u252-b09-1~18.04-b09 +jit [linux-x86_64] $ jruby -e 'require "rhino"; p eval_js "new Date()"' 2020-04-27 16:10:20 +0100 $ jruby -e 'require "rhino"; p Gem.loaded_specs["therubyrhino"].version' # $ rvm use jruby-9.2 Using /home/glopes/.rvm/gems/jruby-9.2.11.1 $ jruby --version jruby 9.2.11.1 (2.5.7) 2020-03-25 b1f55b1a40 OpenJDK 64-Bit Server VM 25.252-b09 on 1.8.0_252-8u252-b09-1~18.04-b09 +jit [linux-x86_64] $ jruby -e 'require "rhino"; p eval_js "new Date()"' NoMethodError: undefined method `getJSTimeValue' for org.mozilla.javascript.NativeDate@27ce24aa:Java::OrgMozillaJavascript::NativeDate Did you mean? get_associated_value method_missing at org/jruby/RubyBasicObject.java:1708 method_missing at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/rhino_ext.rb:122 to_ruby at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/wormhole.rb:10 eval at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:146 do_open at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:267 open at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:252 eval at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:140 eval_js at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/object.rb:13 do_open at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:267 open at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:252 open at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/context.rb:43 eval_js at /home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino-2.1.2/lib/rhino/object.rb:12 at -e:1 $ jruby -e 'require "rhino"; p Gem.loaded_specs["therubyrhino"].version' #
The result is similar to that described in #27, but I checked with lsof and object.java_class.class_loader.getURLs.to_a that the loaded rhino jar file was the correct one (/home/glopes/.rvm/gems/jruby-9.2.11.1/gems/therubyrhino_jar-1.7.8/jar/rhino-1.7.8.jar)
This seems related to a change in how jruby handles access to package-protected methods. For instance, with
Pkg/MyClass.java
package Pkg;
class MyClass {
private double val = 42.0;
public MyClass() {}
double getVal() { return this.val; }
}
and
require 'java'
$CLASSPATH << __dir__
module J
include_package 'Pkg'
end
obj = J::MyClass.new
p obj.getVal
This yields
NoMethodError: undefined method `getVal' for #<Java::Pkg::MyClass:0x543c6f6d>
Did you mean? eval
<main> at script.rb:10
in 9.2 and the result 42.0
in 9.1
Using jruby 1.9.2 and the gem therubyrhino (with or without :require => 'rhino') is in my Gemfile. Bundle show shows:
Upon startup of my rails3.1.rc4 app I get:
/home/gucki/.rvm/gems/ruby-1.9.2-p180/gems/execjs-1.2.0/lib/execjs/runtimes.rb:46:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
I noticed you've fixed the typo in rhino_ext.rb, however the rubygem for version 1.73.4 still contains this, as you can see from the trace, https://gist.github.com/3103608. I get that error, anytime I try to set any variables or mixing with less in my rails app.
Is there a working version I could roll back to, or any other suggestions for a workaround / fix without editing the gem directly?
Thanks.
I'm putting this here for documentation as much as anything. It's not clear to me if Mozilla Rhino will ever be made ECMAscript 6 compliant or be kept up to date in general...
I've found that using therubyrhino in a rails 4.2 application on JRuby that rake assets:precompile
will fail even though my javascript executes without error in the browser.
This appears to be because Mozilla Rhino is only JS 1.7 compliant, whereas my browser can execute v6.
E.g.:
jirb
irb(main):001:0> require 'rhino'
=> true
irb(main):002:0> eval_js "7 * 6"
=> 42
irb(main):003:0> eval_js "var a = 1"
=> nil
irb(main):004:0> eval_js "let a = 1"
Rhino::JSError: missing ; before statement
Not sure if the solution is to have JRuby move to another Java JS engine (are there any?) or somehow make rake assets:precompile
not do syntax checking, or get Mozilla Rhino updated.
Hello,
tried to do the following
require 'rhino'
o = Object.new
o.eval_js("new Date()")
Triggers
NoMethodError: undefined method `getJSTimeValue' for org.mozilla.javascript.NativeDate@7bb22a8c:Java::OrgMozillaJavascript::NativeDate
from org/jruby/RubyBasicObject.java:1497:in `method_missing'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/rhino_ext.rb:126:in `method_missing'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/wormhole.rb:10:in `to_ruby'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:139:in `eval'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:260:in `do_open'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:245:in `open'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:133:in `eval'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/object.rb:13:in `eval_js'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:260:in `do_open'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:245:in `open'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/context.rb:43:in `open'
from /home/admin/logstash/vendor/bundle/jruby/1.9/gems/therubyrhino-2.0.2/lib/rhino/object.rb:12:in `eval_js'
from (irb):6:in `evaluate'
from org/jruby/RubyKernel.java:1121:in `eval'
from org/jruby/RubyKernel.java:1517:in `loop'
from org/jruby/RubyKernel.java:1282:in `catch'
from org/jruby/RubyKernel.java:1282:in `catch'
from /home/admin/logstash/lib/logstash/runner.rb:168:in `run'
from org/jruby/RubyProc.java:271:in `call'
from /home/admin/logstash/lib/logstash/runner.rb:200:in `run'
from /home/admin/logstash/lib/logstash/runner.rb:74:in `main'
from /home/admin/logstash/lib/logstash/runner.rb:230
Same happens when sharing objects between Ruby (running on JRuby) and a Rhino Js context.
I get that exception when trying load coffeescript.
Apparently, this is a Rhino issue: http://jira.pentaho.com/browse/PDI-2355
From rails/execjs#110 (comment)
Unlike all other execjs
backends, therubyrhino currently does not convert :some_symbol
to "some_symbol"
when passing it to JavaScript.
I think it would make sense to fix it in this gem.
When trying to run rails s
I got the following error:
NoMethodError: undefined method `setBuiltinProtoAndParent' for Java::OrgMozillaJavascript::ScriptRuntime:Class
initialize at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/ruby.rb:74
wrap at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/ruby.rb:66
cache at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/rhino_ext.rb:222
cache at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/ruby.rb:216
wrap at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/ruby.rb:66
to_javascript at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/wormhole.rb:27
args_to_javascript at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/wormhole.rb:36
collect at org/jruby/RubyArray.java:2339
args_to_javascript at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/wormhole.rb:36
call at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/therubyrhino-1.73.2/lib/rhino/rhino_ext.rb:161
call at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/bundler/gems/less.rb-9d1a24f2e73e/lib/less/java_script/rhino_context.rb:69
require at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/bundler/gems/less.rb-9d1a24f2e73e/lib/less/loader.rb:47
Less at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/bundler/gems/less.rb-9d1a24f2e73e/lib/less.rb:15
(root) at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/bundler/gems/less.rb-9d1a24f2e73e/lib/less.rb:9
require at org/jruby/RubyKernel.java:1042
require at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/bundler/gems/less.rb-9d1a24f2e73e/lib/less.rb:68
each at org/jruby/RubyArray.java:1615
require at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/bundler-1.1.3/lib/bundler/runtime.rb:66
each at org/jruby/RubyArray.java:1615
require at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/bundler-1.1.3/lib/bundler/runtime.rb:55
require at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/bundler-1.1.3/lib/bundler.rb:119
(root) at /Users/mauroasprea/nektra/entretenerse2/config/application.rb:13
require at org/jruby/RubyKernel.java:1042
(root) at /Users/mauroasprea/nektra/entretenerse2/config/application.rb:53
tap at org/jruby/RubyKernel.java:1787
(root) at /Users/mauroasprea/.rbenv/versions/jruby-1.6.7/lib/ruby/gems/1.8/gems/railties-3.2.3/lib/rails/commands.rb:50
require at org/jruby/RubyKernel.java:1042
(root) at script/rails:6
jruby-1.6.7
rails 3.2.3
And using suggestion https://gist.github.com/2392346 as per recommendation here cowboyd/less.rb#26 (comment)
jruby 1.7.0.dev (ruby-1.9.3-p139) (2012-04-15 ca158df) (OpenJDK Server VM 1.6.0_23) [linux-i386-java]
Javascript
throw 'Error: hello jruby (<eval>#1)'
Following accessors of ::Rhino::JSError
return nil
:
javascript_backtrace
unwrap
cause
When trying to limit the number of instructions, I got the following exception message:
RuntimeError: setting an instruction_limit has no effect on this context, use Context.open(:restricted => true) to gain a restrictable instance
However, it looks like from the documentation (and from what actually worked) that :restricted
should be :restrictable
.
The current History.txt file hasn't been updated for 10 months. This makes it very hard to get a handle on what changed in each release, the severity of the issues, etc. It'd be nice if this were backfilled, but at the very least if it were kept accurate for ongoing changes.
The problem was already posted and discussed in jruby/jruby#1147. There seems to be a problem with therubyrhino together with test-unit. As far as I understood @headius suggested to post the issue here.
The problem was described by @markj9 (still reproducible in 2.0.3):
gem install 'therubyrhino'
gem install 'test-unit'
ruby -e "require 'rhino'; p Rhino::Context.new"
ruby -e "require 'rhino'; require 'test-unit'; p Rhino::Context.new"
The last command will raise an ArgumentError: wrong number of arguments calling run (1 for 0)
it 'can catch ruby errors in javascript' do
Rhino::Context.open do |cxt|
cxt['boom'] = lambda {|msg| fail NameError, msg}
cxt.eval('(function(){try {boom("tastic")} catch(e) {return e.message}})()').should eql 'tastic'
end
end
expected "tastic"
got "org.jruby.exceptions.RaiseException: (NameError) tastic"
I'm using JRuby to create a new Rails 3.1.3 application which depends on the less-rails-bootstrap gem and therubyrhino (2.0.0).
I've created a view (home/index.html.erb) that just has a single <p>
element and the root route points to home#index.
When I try to start Rails, I get the following (I can't copy and paste this so I'm typing some out):
Rhino::JSError: [ruby LoadError]
call at /home/me/.rvm/gems/jruby-1.6.7@futz/gems/therubyrhino-2.0.0/lib/rhino/rhino_ext.rb:170
call at /home/me/.rvm/gems/jruby-1.6.7@futz/gems/commonjs-0.2.6/lib/commonjs/environment.rb:18
call at /home/me/.rvm/gems/jruby-1.6.7@futz/gems/less-2.2.1/lib/less/loader.rb:22
call at /home/me/.rvm/gems/jruby-1.6.7@futz/gems/less-2.2.1/lib/less.rb:15
call at /home/me/.rvm/gems/jruby-1.6.7@futz/gems/less-2.2.1/lib/less.rb:9
Output from gem env
- RUBYGEMS VERSION: 1.8.15
- RUBY VERSION: 1.9.2 (2012-02-22 patchlevel 312) [java]
- RUBYGEMS PLATFORMS:
- ruby
- universal-java-1.6
I think that's all the relevant bits.
Getting ExecJS errors when rendering a React component with prerender:true
<%= react_component('Hello', {}, {prerender: true}) %>
Error goes away when using latest js.jar from Rhino
I get the follow error when running my jruby rails 3.1 application in Tomcat7:
'java.lang.IllegalStateException: Calling Context.exit without previous Context.enter'
It's displayed in the console output in the browser when trying to process javascript.
Any ideas what could be causing this?
After upgrading thrubyrhino from 1.73.0 to 1.73.1 I get the following deprecation warnings when running my selenium tests using rspec 2.8 and selenium-webdriver 2.18.0:
[DEPRECATION] `Rhino::To.ruby` is deprecated, use `Rhino.to_ruby` instead.
[DEPRECATION] `Rhino::NativeFunction` is no longer used, returning a stub.
[DEPRECATION] `Rhino::NativeObject` is no longer used, returning a stub.
require 'rhino'
p Rhino::VERSION # => 2.0.4
ctx = Rhino::Context.new
ctx.eval('this.bar = 123');
p ctx.eval('(function() { return this.bar })()') # => 123 (as expected)
p ctx.eval('this.bar') # => 123 (as expected)
p ctx.eval('(function() { return this.bar })').call # => nil ???
Seems like there a different this
when you invoke a function through #call.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.