snarl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jon Crosby
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ = Snarl
2
+
3
+ Snarl is JavaScript in disguise, bridging Ruby and Rhino. JRuby required.
4
+
5
+ == Synopsis
6
+
7
+ context = Snarl::JavascriptContext.new
8
+ context.eval("1+1") # => 2
9
+
10
+ context.eval("var increment = function(i){ return i+1 };")
11
+ context.eval("increment(3)") # => 4
12
+
13
+ context.load("/path/to/js/lib.js") # allows use of lib.js
14
+
15
+ context.put("foo", "bar")
16
+ context.get("foo") # => "bar"
17
+
18
+ == Testing
19
+
20
+ Run "rake"
21
+
22
+ == Copyright
23
+
24
+ Copyright (c) 2009 Jon Crosby. See LICENSE for details.
@@ -0,0 +1,55 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "snarl"
7
+ gem.summary = %Q{JavaScript in Disguise}
8
+ gem.description = %Q{Snarl is JavaScript in disguise, bridging Ruby and Rhino. JRuby required.}
9
+ gem.email = "jon@joncrosby.me"
10
+ gem.homepage = "http://github.com/jcrosby/snarl"
11
+ gem.authors = ["Jon Crosby"]
12
+ gem.rubyforge_project = "snarl"
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ Jeweler::RubyforgeTasks.new do |rubyforge|
17
+ rubyforge.doc_task = "rdoc"
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ task :default => [:check_dependencies, :spec]
24
+
25
+ desc "Run all examples (or a specific spec with TASK=xxxx)"
26
+ Spec::Rake::SpecTask.new('spec') do |t|
27
+ t.spec_opts = ["-c"]
28
+ t.spec_files = begin
29
+ if ENV["TASK"]
30
+ ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
31
+ else
32
+ FileList['spec/**/*_spec.rb']
33
+ end
34
+ end
35
+ end
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "thing #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
46
+
47
+ class Jeweler
48
+ module Commands
49
+ class InstallGem
50
+ def use_sudo?
51
+ false # favor security as a default
52
+ end
53
+ end
54
+ end
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,47 @@
1
+ module Snarl
2
+ class JavascriptContext
3
+ import org.mozilla.javascript.Context
4
+ import org.mozilla.javascript.Scriptable
5
+ import org.mozilla.javascript.ScriptableObject
6
+ import org.mozilla.javascript.Undefined
7
+ import org.mozilla.javascript.NativeArray
8
+ import org.mozilla.javascript.NativeObject
9
+
10
+ def initialize
11
+ @context = Context.enter
12
+ @scope = @context.init_standard_objects
13
+ @context.evaluate_string(@scope, "window = {};", "<eval>", 1, nil)
14
+ end
15
+
16
+ def load(file)
17
+ @context.evaluate_string(@scope, File.read(file), file, 1, nil)
18
+ end
19
+
20
+ def eval(script)
21
+ unwrap(@context.evaluate_string(@scope, script, "<eval>", 1, nil))
22
+ end
23
+
24
+ def get(name)
25
+ ScriptableObject.getProperty(@scope, name)
26
+ end
27
+
28
+ def put(name, value)
29
+ ScriptableObject.putProperty(@scope, name, value)
30
+ end
31
+
32
+ private
33
+
34
+ def unwrap(object)
35
+ case object
36
+ when Java::OrgMozillaJavascript::NativeArray
37
+ objects = @context.class.js_to_java(object, java.lang.Object[]).to_a
38
+ objects.map { |o| unwrap(o) }
39
+ when Java::OrgMozillaJavascript::NativeObject
40
+ object.get_all_ids.inject({}) { |m,o| m[o] = object.get(o, @scope); m }
41
+ else
42
+ object
43
+ end
44
+ end
45
+
46
+ end
47
+ end
Binary file
@@ -0,0 +1,6 @@
1
+ require 'java'
2
+ require 'js.jar'
3
+ require 'javascript_context'
4
+
5
+ module Snarl
6
+ end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "A JavaScript Context" do
4
+
5
+ before(:each) do
6
+ @context = Snarl::JavascriptContext.new
7
+ end
8
+
9
+ it "evaluates JavaScript expressions" do
10
+ @context.eval("1+1").should == 2
11
+ end
12
+
13
+ it "loads files into its context for future execution" do
14
+ file = '.jstest'
15
+ open(file, 'w+') { |f| f << "foo = function(i){ return i+1 };" }
16
+ @context.load(file)
17
+ @context.eval("foo(3)").should == 4
18
+ File.delete(file)
19
+ end
20
+
21
+ it "sets a value using put" do
22
+ @context.put("foo", "bar")
23
+ @context.eval("foo").should == "bar"
24
+ end
25
+
26
+ it "obtains a value using get" do
27
+ @context.eval("foo = 'bar'")
28
+ @context.get("foo").should == "bar"
29
+ end
30
+
31
+ it "passes objects in and out using get and put" do
32
+ @context.put("foo", {'foo' => 'bar'})
33
+ @context.get("foo").should == {'foo' => 'bar'}
34
+ end
35
+
36
+ it "converts native arrays into ruby arrays" do
37
+ @context.eval("[1,2]").should == [1,2]
38
+ end
39
+
40
+ it "converts native objects into ruby objects" do
41
+ @context.eval("foo = function(){return {'foo':'bar'}}; foo();").should == {'foo' => 'bar'}
42
+ end
43
+
44
+ it "converts nested objects into ruby objects" do
45
+ @context.eval("foo = function(){return [{'foo':'bar'},{'a':'b'}]}; foo();").should == [{'foo' => 'bar'},{'a' => 'b'}]
46
+ end
47
+
48
+ it "converts boolean types into ruby booleans" do
49
+ @context.eval("true").should be_true
50
+ @context.eval("false").should be_false
51
+ end
52
+
53
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__)) + '/../lib'
2
+ require 'snarl'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snarl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Crosby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-08 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: Snarl is JavaScript in disguise, bridging Ruby and Rhino. JRuby required.
26
+ email: jon@joncrosby.me
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/javascript_context.rb
42
+ - lib/js.jar
43
+ - lib/snarl.rb
44
+ - spec/javascript_context_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/jcrosby/snarl
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: snarl
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: JavaScript in Disguise
74
+ test_files:
75
+ - spec/javascript_context_spec.rb
76
+ - spec/spec_helper.rb