Skip to content

Commit 13d3c5d

Browse files
committed
Add runtime using GraalJS on TruffleRuby
* Use Truffle inner contexts to provide correct isolation between ExecJS::Context * To run the tests: TRUFFLERUBYOPT="--jvm --polyglot" bundle exec rake test:graaljs TESTOPTS="--seed=0 --verbose" * Full command without subprocess: TRUFFLERUBYOPT="--jvm --polyglot" jt -u jvm-js ruby -w -Ilib:test -I $PWD/vendor/bundle/truffleruby/*/gems/rake-13.0.1/lib $PWD/vendor/bundle/truffleruby/*/gems/rake-13.0.1/lib/rake/rake_test_loader.rb test/test_execjs.rb --seed=0 --verbose * Try command: TRUFFLERUBYOPT="--jvm --polyglot" jt -u jvm-js ruby -Ilib -rexecjs -e 'p ExecJS.eval("2 + 3")'
1 parent 73a6717 commit 13d3c5d

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
ruby: [ '3.0', '2.7', '2.6', '2.5', 'jruby', 'truffleruby' ]
10+
ruby: [ '3.0', '2.7', '2.6', '2.5', 'jruby', 'truffleruby', 'truffleruby+graalvm-head' ]
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout
@@ -18,12 +18,17 @@ jobs:
1818
uses: ruby/setup-ruby@v1
1919
with:
2020
ruby-version: ${{ matrix.ruby }}
21+
2122
- name: Update Rubygems
2223
run: gem update --system
2324
- name: Install bundler
2425
run: gem install bundler -v '2.2.16'
2526
- name: Install dependencies
2627
run: bundle install
28+
29+
- name: Set TRUFFLERUBYOPT
30+
run: echo "TRUFFLERUBYOPT=--jvm --polyglot" >> $GITHUB_ENV
31+
if: matrix.ruby == 'truffleruby+graalvm-head'
2732
- name: Run test
2833
run: rake
2934
- name: Install gem

lib/execjs/graaljs_runtime.rb

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
require "execjs/runtime"
2+
3+
module ExecJS
4+
class GraalJSRuntime < Runtime
5+
class Context < Runtime::Context
6+
def initialize(runtime, source = "", options = {})
7+
@context = Polyglot::InnerContext.new
8+
@context.eval('js', 'delete this.console')
9+
@js_object = @context.eval('js', 'Object')
10+
11+
source = encode(source)
12+
unless source.empty?
13+
translate do
14+
eval_in_context(source)
15+
end
16+
end
17+
end
18+
19+
def exec(source, options = {})
20+
source = encode(source)
21+
source = "(function(){#{source}})()" if /\S/.match?(source)
22+
23+
translate do
24+
eval_in_context(source)
25+
end
26+
end
27+
28+
def eval(source, options = {})
29+
source = encode(source)
30+
source = "(#{source})" if /\S/.match?(source)
31+
32+
translate do
33+
eval_in_context(source)
34+
end
35+
end
36+
37+
def call(source, *args)
38+
source = encode(source)
39+
source = "(#{source})" if /\S/.match?(source)
40+
41+
translate do
42+
function = eval_in_context(source)
43+
function.call(*convert_ruby_to_js(args))
44+
end
45+
end
46+
47+
private
48+
49+
def translate
50+
begin
51+
convert_js_to_ruby yield
52+
rescue ::RuntimeError => e
53+
if e.message.start_with?('SyntaxError:')
54+
error_class = ExecJS::RuntimeError
55+
else
56+
error_class = ExecJS::ProgramError
57+
end
58+
59+
backtrace = e.backtrace.map { |line| line.sub('(eval)', '(execjs)') }
60+
raise error_class, e.message, backtrace
61+
end
62+
end
63+
64+
def convert_js_to_ruby(value)
65+
case value
66+
when true, false, Integer, Float
67+
value
68+
else
69+
if value.nil?
70+
nil
71+
elsif value.respond_to?(:call)
72+
nil
73+
elsif value.respond_to?(:to_str)
74+
value.to_str
75+
elsif value.respond_to?(:to_ary)
76+
value.to_ary.map do |e|
77+
if e.respond_to?(:call)
78+
nil
79+
else
80+
convert_js_to_ruby(e)
81+
end
82+
end
83+
else
84+
object = value
85+
h = {}
86+
object.instance_variables.each do |member|
87+
v = object[member]
88+
unless v.respond_to?(:call)
89+
h[member.to_s] = convert_js_to_ruby(v)
90+
end
91+
end
92+
h
93+
end
94+
end
95+
end
96+
97+
def convert_ruby_to_js(value)
98+
case value
99+
when nil, true, false, Integer, Float, String
100+
value
101+
when Array
102+
value.map { |e| convert_ruby_to_js(e) }
103+
when Hash
104+
h = @js_object.new
105+
value.each_pair do |k,v|
106+
h[convert_ruby_to_js(k)] = convert_ruby_to_js(v)
107+
end
108+
h
109+
else
110+
raise TypeError, "Unknown how to convert to JS: #{value.inspect}"
111+
end
112+
end
113+
114+
class_eval <<-'RUBY', "(execjs)", 1
115+
def eval_in_context(code); @context.eval('js', code); end
116+
RUBY
117+
end
118+
119+
def name
120+
"GraalVM (Graal.js)"
121+
end
122+
123+
def available?
124+
return @available if defined?(@available)
125+
126+
unless RUBY_ENGINE == "truffleruby"
127+
return @available = false
128+
end
129+
130+
unless defined?(Polyglot::InnerContext)
131+
warn "TruffleRuby #{RUBY_ENGINE_VERSION} does not have support for inner contexts, use a more recent version", uplevel: 0
132+
return @available = false
133+
end
134+
135+
unless Polyglot.languages.include? "js"
136+
warn "The language 'js' is not available, you likely need to `export TRUFFLERUBYOPT='--jvm --polyglot'`", uplevel: 0
137+
warn "Note that you need TruffleRuby+GraalVM and not just the TruffleRuby standalone to use #{self.class}", uplevel: 0
138+
return @available = false
139+
end
140+
141+
@available = true
142+
end
143+
end
144+
end

lib/execjs/runtimes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "execjs/external_runtime"
55
require "execjs/ruby_rhino_runtime"
66
require "execjs/mini_racer_runtime"
7+
require "execjs/graaljs_runtime"
78

89
module ExecJS
910
module Runtimes
@@ -13,6 +14,8 @@ module Runtimes
1314

1415
RubyRhino = RubyRhinoRuntime.new
1516

17+
GraalJS = GraalJSRuntime.new
18+
1619
MiniRacer = MiniRacerRuntime.new
1720

1821
Node = ExternalRuntime.new(
@@ -82,6 +85,7 @@ def self.names
8285
def self.runtimes
8386
@runtimes ||= [
8487
RubyRhino,
88+
GraalJS,
8589
Duktape,
8690
MiniRacer,
8791
Node,

0 commit comments

Comments
 (0)