Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b5c6696fe002^
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 54d5d4afb2b6
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Jan 25, 2016

  1. Copy the full SHA
    b5c6696 View commit details
  2. Copy the full SHA
    54d5d4a View commit details
95 changes: 95 additions & 0 deletions lib/ruby/truffle/truffle/truffle/execjs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

# Designed for ExecJS 2.6.0, but I'll allow it to try to work with anything for now

require 'execjs/runtime'
require 'json'

module ExecJS

class TruffleRuntime < Runtime

JS_MIME_TYPE = 'application/javascript'

class Context < Runtime::Context

STRINGIFY = Truffle::Interop.eval(JS_MIME_TYPE, 'JSON.stringify')
PARSE = Truffle::Interop.eval(JS_MIME_TYPE, 'JSON.parse')

def initialize(runtime, source = '')
exec source
end

def exec(source, options = {})
Truffle::Interop.eval JS_MIME_TYPE, source
nil
end

def eval(source, options = {})
unbox(Truffle::Interop.eval(JS_MIME_TYPE, source))
end

def call(identifier, *args)
function = Truffle::Interop.eval(JS_MIME_TYPE, identifier)
unbox(function.call(function, *args.map { |arg| box(arg) }))
end

private

def unbox(value)
if Truffle::Interop.boxed_primitive?(value)
Truffle::Interop.unbox_value(value)
else
JSON.parse(Truffle::Interop.java_string_to_ruby(STRINGIFY.call(STRINGIFY, value)))
end
end

def box(value)
if Truffle::Interop.boxed_primitive?(value) || value.is_a?(String)
value
else
PARSE.call(PARSE, JSON.generate(value))
end
end

end

def name
'Truffle'
end

def available?
defined?(Truffle::Interop) && Truffle::Interop.eval(JS_MIME_TYPE, 'true')
rescue RubyTruffleError
false
end

end

# Monkey patches

module Runtimes

@runtimes = nil

class << self

alias_method :original_runtimes, :runtimes

def runtimes
@runtimes ||= ([TruffleRuntime.new] + original_runtimes)
end

end

end

self.runtime = Runtimes.autodetect

end
16 changes: 16 additions & 0 deletions test/truffle/integration/execjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

# Run with for example JRUBY_OPTS='-J-cp ..../trufflejs.jar'

if [[ $JRUBY_OPTS != *"trufflejs.jar"* ]]
then
echo 'No trufflejs.jar found in $JRUBY_OPTS - skipping ExecJS integration test'
exit 0
fi

bin/jruby -X-T bin/gem install execjs -v 2.6.0
ruby -X+T -Ilib/ruby/gems/shared/gems/execjs-2.6.0/lib test/truffle/integration/execjs/checkruntime.rb
ruby -X+T -Ilib/ruby/gems/shared/gems/execjs-2.6.0/lib test/truffle/integration/execjs/simple.rb
ruby -X+T -Ilib/ruby/gems/shared/gems/execjs-2.6.0/lib test/truffle/integration/execjs/coffeescript.rb
4 changes: 4 additions & 0 deletions test/truffle/integration/execjs/checkruntime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'execjs'
require 'truffle/execjs'

exit 1 unless ExecJS.runtime.name == 'Truffle'
12 changes: 12 additions & 0 deletions test/truffle/integration/execjs/coffeescript.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/truffle/integration/execjs/coffeescript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Example from the ExecJS README.md

# Copyright (c) 2015-2016 Sam Stephenson
# Copyright (c) 2015-2016 Josh Peek
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require "execjs"
require "truffle/execjs"
#require "open-uri"
source = File.read(File.join(File.dirname(__FILE__), 'coffeescript.js')) # open("http://coffeescript.org/extras/coffee-script.js").read

context = ExecJS.compile(source)
exit 1 unless context.call("CoffeeScript.compile", "square = (x) -> x * x", bare: true) == "var square;\n\nsquare = function(x) {\n return x * x;\n};\n"
# => "var square;\nsquare = function(x) {\n return x * x;\n};"
27 changes: 27 additions & 0 deletions test/truffle/integration/execjs/simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Example from the ExecJS README.md

# Copyright (c) 2015-2016 Sam Stephenson
# Copyright (c) 2015-2016 Josh Peek
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require "execjs"
require "truffle/execjs"
exit 1 unless ExecJS.eval("'red yellow blue'.split(' ')") == ["red", "yellow", "blue"]