Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit c171dafa71124f3e9eefccfd1a82922f276857fb @rkh committed May 28, 2010
Showing with 145 additions and 0 deletions.
  1. +29 −0 README.md
  2. +83 −0 spec/embedded_javascript_spec.rb
  3. 0 spec/example.js
  4. +8 −0 spec/johnson_spec.rb
  5. +8 −0 spec/lyndon_spec.rb
  6. +8 −0 spec/rhino_spec.rb
  7. +1 −0 spec/spec_helper.rb
  8. +8 −0 spec/v8_spec.rb
@@ -0,0 +1,29 @@
+This rather experiments what API subset different embedded JavaScript engines have in common than being a spec.
+
+ libs = {
+ 'johnson' => proc { Johnson::Runtime }, # 'lyndon' => proc { Lyndon::Runtime },
+ 'v8' => proc { V8::Context }, 'rhino' => proc { Rhino::Context }
+ }
+
+ lib, runtime = libs.detect do |path, block|
+ begin
+ require path
+ true
+ rescue LoadError
+ false
+ end
+ end
+
+ fail "no lib found" unless lib
+ js = runtime.call.new
+
+ js.load "my_file.js"
+
+ js.evaluate "x = 10"
+ js[:x] + 20 # => 30
+
+ js[:x] = 42
+ js.evaluate "x - 19" # => 23
+
+ block = js.evaluate "(function(val) { return val + 2; })"
+ block.call js[:x] # => 42
@@ -0,0 +1,83 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+shared_examples_for "embedded javascript" do
+ before do
+ begin
+ require library
+ rescue LoadError
+ pending "not installed"
+ end
+ @context = context_class.new
+ end
+
+ describe :evaluate do
+ statements = %w[true false 42 1.5 1+1]
+ statements << "true ? 10 : 20" << "x = 10; x"
+ statements << '"foo"' << "'bar'"
+
+ statements.each do |statement|
+ it "should convert #{statement}" do
+ @context.evaluate(statement).should == eval(statement)
+ end
+ end
+
+ it "returns a javascript function's return value" do
+ @context.evaluate("(function() { return 42; })()").should == 42
+ end
+
+ it "wraps and javascript objects" do
+ object = @context.evaluate("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})")
+ object.should_not be_nil
+ object['foo'].should == 'bar'
+ object['baz'].should == 'bang'
+ object['5'].should == 5
+ object['embedded'].tap do |embedded|
+ embedded.should_not be_nil
+ embedded['badda'].should == 'bing'
+ end
+ end
+
+ it "converts js Date to ruby Time or Date" do
+ value = @context.evaluate "new Date()"
+ (value.is_a? Time or value.is_a? Date).should be_true
+ end
+
+ it "wraps functions in proc-like objects" do
+ value = @context.evaluate("(function() { return 42 })")
+ value.should respond_to(:call)
+ # FIXME: therubyracer requires an argument to be passed
+ value.call({}).should == 42
+ end
+ end
+
+ describe :[] do
+ it "converts ruby Time to js Date" do
+ @context[:value] = Time.now
+ @context.evaluate("value instanceof Date").should be_true
+ end
+
+ it "allowes setting javascript vars from ruby" do
+ @context[:foo] = 42
+ @context.evaluate("foo").should == 42
+ end
+
+ it "allowes accessing javascript vars" do
+ @context.evaluate "x = 10"
+ @context[:x].should == 10
+ end
+
+ it "maps procs to functions" do
+ @context[:foo] = lambda { 42 }
+ @context.evaluate("foo()").should == 42
+ end
+
+ it "wraps any ruby objects" do
+ obj = Object.new
+ @context[:foo] = obj
+ @context.evaluate("foo").should == obj
+ end
+ end
+
+ describe :load do
+ end
+end
No changes.
@@ -0,0 +1,8 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe 'Johnson' do
+ def library; 'johnson' end
+ def context_class; Johnson::Runtime end
+ def error_class; Johnson::Error end
+ it_should_behave_like "embedded javascript"
+end
@@ -0,0 +1,8 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe 'Lyndon' do
+ def library; 'lyndon' end
+ def context_class; Lyndon::Runtime end
+ def error_class; end
+ it_should_behave_like "embedded javascript"
+end
@@ -0,0 +1,8 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe 'Rhino' do
+ def library; 'rhino' end
+ def context_class; Rhino::Context end
+ def error_class; Rhino::JavascriptError end
+ it_should_behave_like "embedded javascript"
+end
@@ -0,0 +1 @@
+require File.expand_path('../embedded_javascript_spec', __FILE__)
@@ -0,0 +1,8 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe 'V8' do
+ def library; 'v8' end
+ def context_class; V8::Context end
+ def error_class; V8::JavascriptError end
+ it_should_behave_like "embedded javascript"
+end

0 comments on commit c171daf

Please sign in to comment.