mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
with a working -n --no-wrap option to disable the top-level function safety wrapper
This commit is contained in:
@@ -12,9 +12,9 @@ module CoffeeScript
|
|||||||
VERSION = '0.1.1' # Keep in sync with the gemspec.
|
VERSION = '0.1.1' # Keep in sync with the gemspec.
|
||||||
|
|
||||||
# Compile a script (String or IO) to JavaScript.
|
# Compile a script (String or IO) to JavaScript.
|
||||||
def self.compile(script)
|
def self.compile(script, options={})
|
||||||
script = script.read if script.respond_to?(:read)
|
script = script.read if script.respond_to?(:read)
|
||||||
Parser.new.parse(script).compile
|
Parser.new.parse(script).compile(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ Usage:
|
|||||||
# Compile a single source file to JavaScript.
|
# Compile a single source file to JavaScript.
|
||||||
def compile(script, source='')
|
def compile(script, source='')
|
||||||
begin
|
begin
|
||||||
CoffeeScript.compile(script)
|
CoffeeScript.compile(script, :no_wrap => @options[:no_wrap])
|
||||||
rescue CoffeeScript::ParseError => e
|
rescue CoffeeScript::ParseError => e
|
||||||
STDERR.puts e.message(source)
|
STDERR.puts e.message(source)
|
||||||
exit(1) unless @options[:watch]
|
exit(1) unless @options[:watch]
|
||||||
@@ -160,6 +160,9 @@ Usage:
|
|||||||
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
|
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
|
||||||
ENV['VERBOSE'] = 'true'
|
ENV['VERBOSE'] = 'true'
|
||||||
end
|
end
|
||||||
|
opts.on('-n', '--no-wrap', 'suppress the top-level safety function wrapper') do |n|
|
||||||
|
@options[:no_wrap] = true
|
||||||
|
end
|
||||||
opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i|
|
opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i|
|
||||||
install_bundle
|
install_bundle
|
||||||
exit
|
exit
|
||||||
|
|||||||
@@ -75,16 +75,17 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
# If this is the top-level Expressions, wrap everything in a safety closure.
|
# If this is the top-level Expressions, wrap everything in a safety closure.
|
||||||
def root_compile
|
def root_compile(o={})
|
||||||
code = compile(:indent => TAB, :scope => Scope.new)
|
indent = o[:no_wrap] ? '' : TAB
|
||||||
|
code = compile(:indent => indent, :scope => Scope.new)
|
||||||
code.gsub!(STRIP_TRAILING_WHITESPACE, '')
|
code.gsub!(STRIP_TRAILING_WHITESPACE, '')
|
||||||
"(function(){\n#{code}\n})();"
|
o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The extra fancy is to handle pushing down returns and assignments
|
# The extra fancy is to handle pushing down returns and assignments
|
||||||
# recursively to the final lines of inner statements.
|
# recursively to the final lines of inner statements.
|
||||||
def compile(options={})
|
def compile(options={})
|
||||||
return root_compile unless options[:scope]
|
return root_compile(options) unless options[:scope]
|
||||||
code = @expressions.map { |node|
|
code = @expressions.map { |node|
|
||||||
o = super(options)
|
o = super(options)
|
||||||
if last?(node) && (o[:return] || o[:assign])
|
if last?(node) && (o[:return] || o[:assign])
|
||||||
|
|||||||
29
test/fixtures/each_no_wrap.js
vendored
Normal file
29
test/fixtures/each_no_wrap.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
// The cornerstone, an each implementation.
|
||||||
|
// Handles objects implementing forEach, arrays, and raw objects.
|
||||||
|
_.each = function(obj, iterator, context) {
|
||||||
|
var index = 0;
|
||||||
|
try {
|
||||||
|
if (obj.forEach) {
|
||||||
|
obj.forEach(iterator, context);
|
||||||
|
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
|
var a = obj;
|
||||||
|
for (var b=0, c=a.length; b<c; b++) {
|
||||||
|
var item = a[b];
|
||||||
|
var i = b;
|
||||||
|
iterator.call(context, item, i, obj);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var d = _.keys(obj);
|
||||||
|
for (var e=0, f=d.length; e<f; e++) {
|
||||||
|
var key = d[e];
|
||||||
|
iterator.call(context, obj[key], key, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (e !== breaker) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
@@ -10,8 +10,9 @@ class ExecutionTest < Test::Unit::TestCase
|
|||||||
starting_place = File.expand_path(Dir.pwd)
|
starting_place = File.expand_path(Dir.pwd)
|
||||||
Dir.chdir('/Users/jashkenas/Desktop/Beauty/Code/v8')
|
Dir.chdir('/Users/jashkenas/Desktop/Beauty/Code/v8')
|
||||||
sources.each do |source|
|
sources.each do |source|
|
||||||
# puts `./shell #{source}`
|
suceeded = `./shell #{source}`.chomp.to_sym == :true
|
||||||
assert `./shell #{source}`.chomp.to_sym == :true
|
puts "failed: #{source}" unless suceeded
|
||||||
|
assert suceeded
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
Dir.chdir(starting_place)
|
Dir.chdir(starting_place)
|
||||||
|
|||||||
@@ -72,4 +72,9 @@ class ParserTest < Test::Unit::TestCase
|
|||||||
assert nodes.compile == File.read('test/fixtures/each.js')
|
assert nodes.compile == File.read('test/fixtures/each.js')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_no_wrap
|
||||||
|
nodes = @par.parse(File.read('test/fixtures/each.cs'))
|
||||||
|
assert nodes.compile(:no_wrap => true) == File.read('test/fixtures/each_no_wrap.js')
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user