Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 100a94e9c552e04bcc725194f244f854e99e8972 @charliesome committed Jan 10, 2012
Showing with 188 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +13 −0 README.md
  3. +7 −0 Rakefile
  4. +14 −0 coffee-script-pure.gemspec
  5. +22 −0 coffee-script.LICENSE
  6. +46 −0 lib/coffee-script-pure.rb
  7. BIN lib/coffee-script.tsz
  8. +67 −0 test/test_coffee_script_pure.rb
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2012 Charlie Somerville
+
+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.
@@ -0,0 +1,13 @@
+# coffee-script-pure
+
+**coffee-script-pure** is a CoffeeScript compiler running in pure Ruby. It adopts the same protocol as [ruby-coffee-script](https://github.com/josh/ruby-coffee-script):
+
+ require "coffee-script-pure"
+ CoffeeScript.compile "console.log 'hello world'", bare: true
+ # => "\nconsole.log('hello world');\n"
+
+coffee-script-pure uses the [Twostroke](https://github.com/charliesome/twostroke) - a Javascript interpreter written in Ruby. A compiled and deflated copy of the CoffeeScript compiler is bundled with coffee-script-pure. The Twostroke VM and CoffeeScript environment is initialized the first time you compile script.
+
+## Installation
+
+ gem install coffee-script-pure
@@ -0,0 +1,7 @@
+require 'rake/testtask'
+
+task :default => :test
+
+Rake::TestTask.new #do |t|
+# t.warning = true
+#end
@@ -0,0 +1,14 @@
+Gem::Specification.new do |s|
+ s.name = "coffee-script-pure"
+ s.version = "0.0.1"
+ s.date = "2010-01-10"
+ s.homepage = "https://github.com/charliesome/coffee-script-pure"
+ s.summary = "A CoffeeScript compiler is pure Ruby"
+ s.description = "coffee-script pure uses the Twostroke Javascript interpreter to run the CoffeeScript compiler inside Ruby"
+ s.files = ["lib/coffee-script-pure.rb", "lib/coffee-script.tsz"]
+ s.authors = ["Charlie Somerville"]
+ s.email = "charlie@charliesomerville.com"
+ s.required_ruby_version = ">= 1.9.2"
+
+ s.add_dependency "twostroke", "~> 0.2.0"
+end
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Jeremy Ashkenas
+
+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.
@@ -0,0 +1,46 @@
+require "twostroke"
+require "zlib"
+
+module CoffeeScriptPure
+ class EngineError < StandardError; end
+ class CompilationError < StandardError; end
+
+ def self.instance
+ twostroke_vm.global_scope.get_var "CoffeeScript"
+ end
+
+ def self.twostroke_vm
+ unless defined? @@twostroke_vm
+ # twostroke is dog slow at parsing/compiling,
+ # so here's some bytecode I prepared earlier!
+ path = File.expand_path "../coffee-script.tsz", __FILE__
+ bytecode = Marshal.load Zlib.inflate File.read path
+
+ @@twostroke_vm = Twostroke::Runtime::VM.new bytecode
+ Twostroke::Runtime::Lib.setup_environment @@twostroke_vm
+ @@twostroke_vm.execute
+ end
+ @@twostroke_vm
+ end
+
+ def self.version
+ @@version ||= instance.get("VERSION").string
+ end
+
+ def self.compile(src, opts = {})
+ src = src.read if src.respond_to? :read
+ js_src = Twostroke::Runtime::Types::String.new src
+ js_opts = Twostroke::Runtime::Types::Object.new
+ if opts[:bare] or opts[:no_wrap]
+ js_opts.put "bare", Twostroke::Runtime::Types::Boolean.true
+ end
+ ex = catch :exception do
+ retn = instance.get("compile").call(nil, instance, [js_src, js_opts])
+ return retn.string
+ end
+ # if we're here, an exception was thrown javascript-side
+ to_str = ex.get("toString").call(nil, ex, [])
+ mesg = Twostroke::Runtime::Types.to_string(to_str).string
+ raise CompilationError, mesg
+ end
+end
Binary file not shown.
@@ -0,0 +1,67 @@
+# Modified ruby-coffee-script test suite
+
+# Copyright (c) 2010 Joshua 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 'coffee-script-pure'
+require 'test/unit'
+require 'stringio'
+
+class TestCoffeeScriptPure < Test::Unit::TestCase
+ def test_compile
+ assert_equal "(function() {\n\n puts('Hello, World!');\n\n}).call(this);\n",
+ CoffeeScriptPure.compile("puts 'Hello, World!'\n")
+ end
+
+ def test_compile_with_io
+ io = StringIO.new("puts 'Hello, World!'\n")
+ assert_equal "(function() {\n\n puts('Hello, World!');\n\n}).call(this);\n",
+ CoffeeScriptPure.compile(io)
+ end
+
+ def test_compile_with_bare_true
+ assert_equal "\nputs('Hello, World!');\n",
+ CoffeeScriptPure.compile("puts 'Hello, World!'\n", :bare => true)
+ end
+
+ def test_compile_with_bare_false
+ assert_equal "(function() {\n\n puts('Hello, World!');\n\n}).call(this);\n",
+ CoffeeScriptPure.compile("puts 'Hello, World!'\n", :bare => false)
+ end
+
+ def test_compile_with_no_wrap_true
+ assert_equal "\nputs('Hello, World!');\n",
+ CoffeeScriptPure.compile("puts 'Hello, World!'\n", :no_wrap => true)
+ end
+
+ def test_compile_with_no_wrap
+ assert_equal "(function() {\n\n puts('Hello, World!');\n\n}).call(this);\n",
+ CoffeeScriptPure.compile("puts 'Hello, World!'\n", :no_wrap => false)
+ end
+
+ def test_compilation_error
+ assert_raise CoffeeScriptPure::CompilationError do
+ CoffeeScriptPure.compile("unless")
+ end
+ end
+end

0 comments on commit 100a94e

Please sign in to comment.