1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

with a working -n --no-wrap option to disable the top-level function safety wrapper

This commit is contained in:
Jeremy Ashkenas 2009-12-24 15:31:00 -08:00
parent 46f81c2de1
commit e27756cee8
6 changed files with 48 additions and 9 deletions

View file

@ -12,9 +12,9 @@ module CoffeeScript
VERSION = '0.1.1' # Keep in sync with the gemspec.
# 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)
Parser.new.parse(script).compile
Parser.new.parse(script).compile(options)
end
end

View file

@ -112,7 +112,7 @@ Usage:
# Compile a single source file to JavaScript.
def compile(script, source='')
begin
CoffeeScript.compile(script)
CoffeeScript.compile(script, :no_wrap => @options[:no_wrap])
rescue CoffeeScript::ParseError => e
STDERR.puts e.message(source)
exit(1) unless @options[:watch]
@ -160,6 +160,9 @@ Usage:
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
ENV['VERBOSE'] = 'true'
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|
install_bundle
exit

View file

@ -75,16 +75,17 @@ module CoffeeScript
end
# If this is the top-level Expressions, wrap everything in a safety closure.
def root_compile
code = compile(:indent => TAB, :scope => Scope.new)
def root_compile(o={})
indent = o[:no_wrap] ? '' : TAB
code = compile(:indent => indent, :scope => Scope.new)
code.gsub!(STRIP_TRAILING_WHITESPACE, '')
"(function(){\n#{code}\n})();"
o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
end
# The extra fancy is to handle pushing down returns and assignments
# recursively to the final lines of inner statements.
def compile(options={})
return root_compile unless options[:scope]
return root_compile(options) unless options[:scope]
code = @expressions.map { |node|
o = super(options)
if last?(node) && (o[:return] || o[:assign])

29
test/fixtures/each_no_wrap.js vendored Normal file
View 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;
};

View file

@ -10,8 +10,9 @@ class ExecutionTest < Test::Unit::TestCase
starting_place = File.expand_path(Dir.pwd)
Dir.chdir('/Users/jashkenas/Desktop/Beauty/Code/v8')
sources.each do |source|
# puts `./shell #{source}`
assert `./shell #{source}`.chomp.to_sym == :true
suceeded = `./shell #{source}`.chomp.to_sym == :true
puts "failed: #{source}" unless suceeded
assert suceeded
end
ensure
Dir.chdir(starting_place)

View file

@ -72,4 +72,9 @@ class ParserTest < Test::Unit::TestCase
assert nodes.compile == File.read('test/fixtures/each.js')
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