dienashorner 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd7cd7502f49f9a24841e42881064fce73a09df1
4
- data.tar.gz: 3b5f2dc911bf0384853232e290f25405a900786c
3
+ metadata.gz: 55418407ed597875c9e30b3c4e984cee94217f91
4
+ data.tar.gz: b0032e9cc1fc2cc6e4f44a33639335a87b22525e
5
5
  SHA512:
6
- metadata.gz: 514de8502051b964befd3bb7edd902969f9a15251cbb4042a9be361f07b46a451807da9681e9cb574a24451dcaf967d0e6a3473ae9b83d3997899431185865b4
7
- data.tar.gz: ff10758e53b1b206bac06d2104a6041b68ce150b4f353e640a96b92fe7fa24a36aaec12dc927208a6433038e84c44f2bdd79b69496b86b755d1d42cf6442e6c9
6
+ metadata.gz: 31cd85e1027c9cf4a43336f853a9ee5e21cb806cc2475e7ae48d6592477ce1c45b651f8230760b538356d64ad95f162ad1417f025dc3ad582d20bca2bf6ba13b
7
+ data.tar.gz: f8dc1ce3afc7b7c40650b2f3f53b0b22e0f51c3f133b9ee97107bd6c0b1f2ead26e9e7aedb3a45bc671dcda6fb649a69a2bea1101891b1c9b0f52a1b86dbd81a
@@ -0,0 +1,15 @@
1
+ rvm:
2
+ - jruby-1.7.24
3
+ - jruby-9.0.5.0
4
+ jdk:
5
+ - oraclejdk8
6
+
7
+ script: "if [[ -v COMMAND ]]; then $COMMAND; else rake spec; fi"
8
+
9
+ env:
10
+ global:
11
+ - JAVA_OPTS="-Xmx512M"
12
+ matrix:
13
+ - COMMAND="ruby -Ilib spec/nashorn/test_execjs.rb"
14
+ - EMPTY='' # COMMAND="rake spec"
15
+
data/Gemfile CHANGED
@@ -7,7 +7,7 @@ group :test do
7
7
  #gem 'redjs', :git => 'git://github.com/cowboyd/redjs.git', :group => :test,
8
8
  # :ref => "0d844f066666f967a78b20beb164c52d9ac3f5ca"
9
9
  gem 'less', '~> 2.6.0', :require => false
10
- gem 'execjs', '2.6.0', :require => false
10
+ gem 'execjs', '~> 2.7.0', :require => false
11
11
  end
12
12
 
13
13
  gem 'rake', :require => false, :group => :development
data/README.md CHANGED
@@ -143,8 +143,8 @@ Far from being a drop-in replacement although there's `require 'nashorn/rhino'`.
143
143
 
144
144
  ### Less.rb
145
145
 
146
- [Less.rb](https://github.com/cowboyd/less.rb) seems to be working, for now you
147
- will simply need to :`require 'nashorn/rhino/less'` before a `require 'less'`.
146
+ [Less.rb](https://github.com/cowboyd/less.rb) seems to be working (with hacks),
147
+ for now you will need to :`require 'nashorn/rhino/less'` before `require 'less'`.
148
148
 
149
149
 
150
150
  ### ExecJS
@@ -168,4 +168,4 @@ Copyright (c) 2016 Karol Bucek. Apache License v2 (see LICENSE for details).
168
168
 
169
169
  [0]: http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html
170
170
  [3]: https://github.com/rails/execjs
171
- [4]: http://openjdk.java.net/projects/nashorn/
171
+ [4]: http://openjdk.java.net/projects/nashorn/
@@ -18,7 +18,7 @@ module Nashorn
18
18
  # Undefined = Java::JdkNashornInternalRuntime::Undefined rescue nil
19
19
  end
20
20
 
21
- def eval_js(source, options = {})
21
+ def eval_js(source, options = nil)
22
22
  factory = JS::NashornScriptEngineFactory.new
23
23
  factory.getScriptEngine.eval(source)
24
24
  end
@@ -85,6 +85,8 @@ module Nashorn
85
85
 
86
86
  def factory; @native.getFactory end
87
87
 
88
+ attr_reader :scope
89
+
88
90
  # Read a value from the global scope of this context
89
91
  def [](key)
90
92
  @scope[key]
@@ -96,19 +98,23 @@ module Nashorn
96
98
  @scope[key] = val
97
99
  end
98
100
 
101
+ # @private
102
+ FILENAME = javax.script.ScriptEngine.FILENAME
103
+
99
104
  # Evaluate a String/IO of JavaScript in this context.
100
- def eval(source, source_name = "<eval>", line_number = 1)
105
+ def eval(source, filename = nil, line = nil)
101
106
  open do
102
107
  if IO === source || StringIO === source
103
108
  source = IOReader.new(source)
104
109
  else
105
110
  source = source.to_s
106
111
  end
112
+ @native.put(FILENAME, filename) if filename
107
113
  Nashorn.to_rb @native.eval(source, @scope)
108
114
  end
109
115
  end
110
116
 
111
- def evaluate(source); eval(source) end
117
+ def evaluate(source, filename = nil); eval(source, filename) end
112
118
 
113
119
  # Read the contents of <tt>filename</tt> and evaluate it as JavaScript.
114
120
  #
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  require 'execjs/runtime'
2
3
 
3
4
  module ExecJS
4
5
  class NashornRuntime < Runtime
5
6
  class Context < Runtime::Context
6
7
 
7
- def initialize(runtime, source = nil)
8
+ def initialize(_, source = nil, _ = nil)
8
9
  source = encode(source) if source
9
10
  @nashorn_context = ::Nashorn::Context.new
10
11
  @nashorn_context.eval(source) if source
@@ -17,7 +18,7 @@ module ExecJS
17
18
  eval "(function(){#{source}})()", options if /\S/ =~ source
18
19
  end
19
20
 
20
- def eval(source, options = nil) # options not used
21
+ def eval(source, _ = nil) # options not used
21
22
  source = encode(source)
22
23
  unbox @nashorn_context.eval("(#{source})") if /\S/ =~ source
23
24
  rescue Exception => e
@@ -25,10 +26,11 @@ module ExecJS
25
26
  end
26
27
 
27
28
  def call(prop, *args)
28
- evaled = @nashorn_context.eval(prop)
29
- unbox evaled.call(*args)
30
- rescue Exception => e
31
- raise wrap_error(e)
29
+ # evaled = @nashorn_context.eval(prop)
30
+ # unbox evaled.call(*args)
31
+ #rescue Exception => e
32
+ # raise wrap_error(e)
33
+ eval "#{prop}.apply(this, #{::JSON.generate(args)})"
32
34
  end
33
35
 
34
36
  def unbox(value)
@@ -167,25 +167,7 @@ module Nashorn
167
167
 
168
168
  # @private NOTE: duplicated from JSObject
169
169
  def call(*args)
170
- # Nashorn.to_rb __call__ nil, *Nashorn.args_to_js(args) # this = nil
171
-
172
-
173
- #if isFunction
174
- # this = Nashorn.to_js args.shift
175
- #
176
- # puts "calling this = #{this.inspect} args = #{args.inspect}"
177
- #
178
- # result = __call__(this, Nashorn.args_to_js(args))
179
- #else
180
-
181
- #puts "calling args = #{args.inspect} Nashorn.args_to_js(args) #{Nashorn.args_to_js(args).inspect}"
182
-
183
- result = __call__ nil, *Nashorn.args_to_js(args)
184
-
185
- #puts "called result = #{result.inspect} #{result.class}"
186
-
187
- Nashorn.to_rb result
188
- #end
170
+ Nashorn.to_rb __call__ nil, *Nashorn.args_to_js(args) # this = nil
189
171
  rescue JS::NashornException => e
190
172
  raise Nashorn::JSError.new(e)
191
173
  end
@@ -61,24 +61,65 @@ module Less
61
61
  private
62
62
 
63
63
  def handle_js_error(e)
64
-
65
- raise e # TODO NOT IMPLEMENTED
66
-
67
64
  #if e.value && ( e.value['message'] || e.value['type'].is_a?(String) )
68
65
  # raise Less::ParseError.new(e, e.value) # LessError
69
66
  #end
70
- #if e.unwrap.to_s =~ /missing opening `\(`/
71
- # raise Less::ParseError.new(e.unwrap.to_s)
72
- #end
67
+ raise Less::ParseError.new(e) if ::Nashorn::JSError.parse_error?(e.cause)
68
+
69
+ msg = e.value.to_s
70
+ raise Less::ParseError.new(msg) if msg.start_with?("missing opening `(`")
73
71
  #if e.message && e.message[0, 12] == "Syntax Error"
74
72
  # raise Less::ParseError.new(e)
75
- #else
76
- # raise Less::Error.new(e)
77
73
  #end
74
+ raise Less::Error.new(e)
78
75
  end
79
76
 
80
77
  end
78
+
79
+ def self.to_js_hash(hash) # TODO this needs to be figured out
80
+ # we can not pass wrapped Ruby Hash objects down as they won't
81
+ # have a prototype (and thus no hasOwnProperty)
82
+ js_hash = Nashorn.eval_js '({})'
83
+ hash.each { |key, val| js_hash[key] = val }
84
+ js_hash
85
+ end
86
+
81
87
  end
82
88
  end
83
89
 
84
90
  Less::JavaScript.context_wrapper = Less::JavaScript::NashornContext
91
+
92
+ require 'less'
93
+
94
+ Less::Parser.class_eval do
95
+
96
+ def initialize(options = {})
97
+ env = {}
98
+ Less.defaults.merge(options).each do |key, val|
99
+ env[key.to_s] =
100
+ case val
101
+ when Symbol, Pathname then val.to_s
102
+ when Array
103
+ val.map!(&:to_s) if key.to_sym == :paths # might contain Pathname-s
104
+ val # keep the original passed Array
105
+ else val # true/false/String/Method
106
+ end
107
+ end
108
+ ###
109
+ env = Less::JavaScript.to_js_hash env
110
+ ###
111
+ @parser = Less::JavaScript.exec { Less['Parser'].new(env) }
112
+ end
113
+
114
+ end
115
+
116
+ Less::Parser::Tree.class_eval do
117
+
118
+ def to_css(opts = {})
119
+ ###
120
+ opts = Less::JavaScript.to_js_hash opts
121
+ ###
122
+ Less::JavaScript.exec { @tree.toCSS(opts) }
123
+ end
124
+
125
+ end
@@ -88,6 +88,11 @@ module Nashorn
88
88
 
89
89
  private
90
90
 
91
+ # @private
92
+ NUMBER_CLASS = Java::JavaLang::Number.java_class
93
+ # @private
94
+ STRING_CLASS = Java::JavaLang::String.java_class
95
+
91
96
  def convert(name)
92
97
  if exclude?(name)
93
98
  nil
@@ -130,6 +135,20 @@ module Nashorn
130
135
  instance.class.equal? @unwrap
131
136
  end
132
137
 
138
+ # @override
139
+ def isInstanceOf(clazz)
140
+ @unwrap.is_a?(clazz)
141
+ end
142
+
143
+ # @override
144
+ def getDefaultValue(hint)
145
+ if hint && NUMBER_CLASS.eql?(hint)
146
+ return hint.to_f if hint.respond_to?(:to_f)
147
+ return hint.to_i if hint.respond_to?(:to_i)
148
+ end
149
+ @unwrap.to_s
150
+ end
151
+
133
152
  # @override
134
153
  def isArray; @unwrap.is_a?(Array) end
135
154
 
@@ -193,6 +212,16 @@ module Nashorn
193
212
  instance.class.equal? @unwrap
194
213
  end
195
214
 
215
+ # @override
216
+ def isInstanceOf(clazz)
217
+ @unwrap.is_a?(clazz)
218
+ end
219
+
220
+ # @override
221
+ def getDefaultValue(hint)
222
+ @unwrap.to_s
223
+ end
224
+
196
225
  # @override
197
226
  def isArray; false end
198
227
 
@@ -1,3 +1,3 @@
1
1
  module Nashorn
2
- VERSION = '0.2.2'
3
- end
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ module NashornHelpers
4
+
5
+ module_function
6
+
7
+ def add_prototype_key(hash, recurse = false)
8
+ hash['prototype'] ||= {}
9
+ hash.keys.each do |key|
10
+ val = hash[key] unless key == 'prototype'
11
+ add_prototype_key(val, recurse) if val.is_a?(Hash)
12
+ end if recurse
13
+ end
14
+
15
+ end
16
+
17
+ shared_examples_for 'JSObject', :shared => true do
18
+
19
+ it "acts like a hash" do
20
+ @object['foo'] = 'bar'
21
+ @object['foo'].should == 'bar'
22
+ end
23
+
24
+ # it "might be converted to a hash with string keys" do
25
+ # @object[42] = '42'
26
+ # @object[:foo] = 'bar'
27
+ # expect = @object.respond_to?(:to_h_properties) ? @object.to_h_properties : {}
28
+ # @object.to_h.should == expect.merge('42' => '42', 'foo' => 'bar')
29
+ # end
30
+
31
+ it "yields properties with each" do
32
+ @object['1'] = 1
33
+ @object['3'] = 3
34
+ @object['2'] = 2
35
+ @object.each do |key, val|
36
+ case key
37
+ when '1' then val.should == 1
38
+ when '2' then val.should == 2
39
+ when '3' then val.should == 3
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ describe "JS Array" do
47
+
48
+ before do
49
+ @object = Nashorn.eval_js '[ 1, 2, 3 ]'
50
+ end
51
+
52
+ it_should_behave_like 'JSObject'
53
+
54
+ it "converts toString" do
55
+ @object.to_s.should == '{"0"=>1, "1"=>2, "2"=>3}'
56
+ end
57
+
58
+ it "converts toString" do
59
+ @object.getDefaultValue(nil).should == '1,2,3'
60
+ end
61
+
62
+ it 'routes rhino methods' do
63
+ @object.proto.should_not be nil
64
+ @object.class_name.should == 'Array'
65
+ end
66
+
67
+ it 'raises on missing method' do
68
+ lambda { @object.aMissingMethod }.should raise_error(NoMethodError)
69
+ end
70
+
71
+ it 'puts JS property' do
72
+ @object.hasMember('foo').should == false
73
+ @object.foo = 'bar'
74
+ @object.hasMember('foo').should == true
75
+ end
76
+
77
+ it 'gets JS property' do
78
+ @object.put('foo', 42)
79
+ @object.foo.should == 42
80
+ end
81
+
82
+ end
83
+
84
+ describe "JS Object" do
85
+
86
+ before do
87
+ @object = Nashorn.eval_js 'Object.create({})'
88
+ end
89
+
90
+ it_should_behave_like 'JSObject'
91
+
92
+ it 'raises on missing method' do
93
+ lambda { @object.aMissingMethod }.should raise_error(NoMethodError)
94
+ end
95
+
96
+ it 'puts JS property' do
97
+ @object.hasMember('foo').should == false
98
+ @object.foo = 'bar'
99
+ @object.hasMember('foo').should == true
100
+ end
101
+
102
+ it 'gets JS property' do
103
+ @object.put('foo', 42)
104
+ @object.foo.should == 42
105
+ end
106
+
107
+ # it 'is == to an empty Hash / Map' do
108
+ # ( @object == {} ).should be true
109
+ # ( @object == java.util.HashMap.new ).should be true
110
+ # end
111
+ #
112
+ # it 'is === to an empty Hash' do
113
+ # ( @object === {} ).should be true
114
+ # end
115
+
116
+ it 'is eql? to an empty Hash / Map' do
117
+ ( @object.eql?( {} ) ).should be true
118
+ ( @object.eql?( java.util.HashMap.new ) ).should be true
119
+ end
120
+
121
+ # it 'is eql? to another native object' do
122
+ # object = @context.newObject(scope)
123
+ # ( @object.eql?( object ) ).should be true
124
+ # ( object.eql?( @object ) ).should be true
125
+ # ( @object == object ).should be true
126
+ # ( object === @object ).should be true
127
+ # end
128
+
129
+ # it 'objects with same values are equal' do
130
+ # #object1 = @object; object1['foo'] = 'bar'; object1['answer'] = 42
131
+ # #object2 = Nashorn.eval_js '({ foo: "bar", answer: 42 })'
132
+ # Nashorn::Context.open do |js|
133
+ # object1 = js.eval '({})'
134
+ # object1['foo'] = 'bar'; object1['answer'] = 42
135
+ # object2 = js.eval '({ foo: "bar", answer: 42 })'
136
+ #
137
+ # ( object1 == object2 ).should be true
138
+ # ( object1.eql?( object2 ) ).should be true
139
+ # end
140
+ # end
141
+
142
+ end
@@ -127,6 +127,14 @@ describe Nashorn::Ruby::Object do
127
127
  rb_object.keys.should_not include('writer')
128
128
  end
129
129
 
130
+ it 'getDefaultValue returns to_s' do
131
+ rb_object = Nashorn::Ruby::Object.wrap object = UII.new
132
+ str = object.to_s # <UII:0x7b9a4292>
133
+ expect( rb_object.getDefaultValue(nil) ).to eql str
134
+ expect( rb_object.getDefaultValue(java.lang.Number.java_class) ).to eql str
135
+ expect( rb_object.getDefaultValue(java.lang.String.java_class) ).to eql str
136
+ end
137
+
130
138
  it "is aliased to RubyObject" do
131
139
  Nashorn::RubyObject.should be(Nashorn::Ruby::Object)
132
140
  end
@@ -1,10 +1,14 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'minitest/autorun'
3
-
4
- require 'json'
5
-
6
- require 'nashorn/execjs/load'
7
- require 'execjs'
2
+ require "minitest/autorun"
3
+ require "execjs/module"
4
+ require "json"
5
+
6
+ begin
7
+ require "execjs"
8
+ rescue ExecJS::RuntimeUnavailable => e
9
+ warn e
10
+ exit 2
11
+ end
8
12
 
9
13
  if defined? Minitest::Test
10
14
  Test = Minitest::Test
@@ -13,26 +17,10 @@ elsif defined? MiniTest::Unit::TestCase
13
17
  end
14
18
 
15
19
  class TestExecJS < Test
16
- # def test_runtime_available
17
- # runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
18
- # assert !runtime.available?
19
- #
20
- # runtime = ExecJS::ExternalRuntime.new(command: "ruby")
21
- # assert runtime.available?
22
- # end
23
- #
24
- # def test_runtime_assignment
25
- # original_runtime = ExecJS.runtime
26
- # runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
27
- # assert_raises(ExecJS::RuntimeUnavailable) { ExecJS.runtime = runtime }
28
- # assert_equal original_runtime, ExecJS.runtime
29
- #
30
- # runtime = ExecJS::ExternalRuntime.new(command: "ruby")
31
- # ExecJS.runtime = runtime
32
- # assert_equal runtime, ExecJS.runtime
33
- # ensure
34
- # ExecJS.runtime = original_runtime
35
- # end
20
+
21
+ def setup
22
+ super
23
+ end
36
24
 
37
25
  def test_context_call
38
26
  context = ExecJS.compile("id = function(v) { return v; }")
@@ -80,6 +68,9 @@ class TestExecJS < Test
80
68
  end
81
69
 
82
70
  define_method("test_compile_return_string_#{index}") do
71
+
72
+ puts "var a = #{input};"
73
+
83
74
  context = ExecJS.compile("var a = #{input};")
84
75
  assert_equal output, context.eval("a")
85
76
  end
@@ -125,7 +116,6 @@ class TestExecJS < Test
125
116
  end
126
117
 
127
118
  define_method("test_strinigfy_value_#{index}") do
128
- # context = ExecJS.compile("function json(obj) { return obj || JSON.stringify(obj); }")
129
119
  context = ExecJS.compile("function json(obj) { return JSON.stringify(obj); }")
130
120
 
131
121
  puts context.inspect; puts "value = #{value.inspect}"
@@ -270,6 +260,9 @@ class TestExecJS < Test
270
260
  ExecJS.eval(")")
271
261
  flunk
272
262
  rescue ExecJS::RuntimeError => e
263
+
264
+ #puts "\n\nEVAL: " + e.inspect + "\n #{e.backtrace.join("\n ")}\n"
265
+
273
266
  assert e
274
267
  assert e.backtrace[0].include?("(execjs):1"), e.backtrace.join("\n")
275
268
  end
@@ -332,7 +325,7 @@ class TestExecJS < Test
332
325
  ExecJS.compile("throw 'hello'")
333
326
  end
334
327
  end
335
- =begin
328
+
336
329
  def test_babel
337
330
  skip if ExecJS.runtime.is_a?(ExecJS::RubyRhinoRuntime)
338
331
 
@@ -370,5 +363,6 @@ class TestExecJS < Test
370
363
  assert_equal "function foo(bar){return bar}",
371
364
  context.call("uglify", "function foo(bar) {\n return bar;\n}")
372
365
  end
373
- =end
366
+
374
367
  end
368
+
@@ -7,6 +7,14 @@ describe Nashorn do
7
7
  expect( Nashorn.eval_js 'true + 100' ).to eql 101
8
8
  end
9
9
 
10
+ it 'getDefaultValue is used for toString' do
11
+ arr = Nashorn.eval('[ 1, 2 ]')
12
+ expect( arr ).to be_a Nashorn::JS::JSObject
13
+ expect( arr.getDefaultValue(nil) ).to eql '1,2'
14
+ expect( arr.getDefaultValue(java.lang.Number.java_class) ).to eql '1,2'
15
+ expect( arr.getDefaultValue(java.lang.String.java_class) ).to eql '1,2'
16
+ end
17
+
10
18
  class NashornStub
11
19
  include Nashorn
12
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dienashorner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Bucek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-17 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -49,6 +49,7 @@ extra_rdoc_files:
49
49
  files:
50
50
  - ".gitignore"
51
51
  - ".rspec"
52
+ - ".travis.yml"
52
53
  - Gemfile
53
54
  - LICENSE
54
55
  - README.md
@@ -70,6 +71,7 @@ files:
70
71
  - lib/nashorn/version.rb
71
72
  - spec/nashorn/access_spec.rb
72
73
  - spec/nashorn/error_spec.rb
74
+ - spec/nashorn/ext_spec.rb
73
75
  - spec/nashorn/integration/bar.js
74
76
  - spec/nashorn/integration/foo.js
75
77
  - spec/nashorn/integration/index.js