From d416c184db7d6034df616db1146c890e345a9e01 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 7 Jan 2010 21:10:25 -0500 Subject: [PATCH] separating out the --no-wrap and the --globals arguments, which shouldn't be jammed together --- lib/coffee_script/command_line.rb | 6 +++- .../narwhal/coffee-script.coffee | 10 +++---- .../narwhal/lib/coffee-script.js | 30 +++++++++---------- .../narwhal/lib/coffee-script/loader.js | 4 +-- lib/coffee_script/nodes.rb | 3 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index fc0f5d37..30ad1e11 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -139,6 +139,7 @@ Usage: begin options = {} options[:no_wrap] = true if @options[:no_wrap] + options[:globals] = true if @options[:globals] CoffeeScript.compile(script, options) rescue CoffeeScript::ParseError, SyntaxError => e STDERR.puts "#{source}: #{e.message}" @@ -193,9 +194,12 @@ Usage: opts.on('-v', '--verbose', 'print at every step of code generation') do |v| ENV['VERBOSE'] = 'true' end - opts.on('-n', '--no-wrap', 'raw output, no safety wrapper or vars') do |n| + opts.on('-n', '--no-wrap', 'raw output, no function safety wrapper') do |n| @options[:no_wrap] = true end + opts.on('-g', '--globals', 'attach all top-level variable as globals') do |n| + @options[:globals] = true + end opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i| install_bundle exit diff --git a/lib/coffee_script/narwhal/coffee-script.coffee b/lib/coffee_script/narwhal/coffee-script.coffee index d687c42b..d3bc2b33 100644 --- a/lib/coffee_script/narwhal/coffee-script.coffee +++ b/lib/coffee_script/narwhal/coffee-script.coffee @@ -29,7 +29,7 @@ exports.run: args => while true try system.stdout.write('coffee> ').flush() - result: exports.evalCS(Readline.readline()) + result: exports.evalCS(Readline.readline(), ['--globals']) print(result) if result isnt undefined catch e print(e) @@ -41,15 +41,15 @@ exports.compileFile: path => coffee.stdout.read() # Compile a string of CoffeeScript into JavaScript. -exports.compile: source => - coffee: OS.popen([coffeePath, "--eval", "--no-wrap"]) +exports.compile: source, flags => + coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or [])) coffee.stdin.write(source).flush().close() checkForErrors(coffee) coffee.stdout.read() # Evaluating a string of CoffeeScript first compiles it externally. -exports.evalCS: source => - eval(exports.compile(source)) +exports.evalCS: source, flags => + eval(exports.compile(source, flags)) # Make a factory for the CoffeeScript environment. exports.makeNarwhalFactory: path => diff --git a/lib/coffee_script/narwhal/lib/coffee-script.js b/lib/coffee_script/narwhal/lib/coffee-script.js index 99a2c40c..7e9fb632 100644 --- a/lib/coffee_script/narwhal/lib/coffee-script.js +++ b/lib/coffee_script/narwhal/lib/coffee-script.js @@ -20,25 +20,24 @@ // Run a simple REPL, round-tripping to the CoffeeScript compiler for every // command. exports.run = function run(args) { - var __a, __b, __c, i, path, result; + var __a, __b, i, result; if (args.length) { __a = args; - __b = []; - for (i in __a) { - if (__a.hasOwnProperty(i)) { - path = __a[i]; - exports.evalCS(File.read(path)); - __c = delete args[i]; - __b.push(__c); - } + __b = function(path, i) { + exports.evalCS(File.read(path)); + delete args[i]; + }; + if (__a instanceof Array) { + for (i=0; i<__a.length; i++) __b(__a[i], i); + } else { + for (i in __a) { if (__a.hasOwnProperty(i)) __b(__a[i], i); } } - __b; return true; } while (true) { try { system.stdout.write('coffee> ').flush(); - result = exports.evalCS(Readline.readline()); + result = exports.evalCS(Readline.readline(), ['--globals']); if (result !== undefined) { print(result); } @@ -46,6 +45,7 @@ print(e); } } + return null; }; // Compile a given CoffeeScript file into JavaScript. exports.compileFile = function compileFile(path) { @@ -55,16 +55,16 @@ return coffee.stdout.read(); }; // Compile a string of CoffeeScript into JavaScript. - exports.compile = function compile(source) { + exports.compile = function compile(source, flags) { var coffee; - coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]); + coffee = OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags || [])); coffee.stdin.write(source).flush().close(); checkForErrors(coffee); return coffee.stdout.read(); }; // Evaluating a string of CoffeeScript first compiles it externally. - exports.evalCS = function evalCS(source) { - return eval(exports.compile(source)); + exports.evalCS = function evalCS(source, flags) { + return eval(exports.compile(source, flags)); }; // Make a factory for the CoffeeScript environment. exports.makeNarwhalFactory = function makeNarwhalFactory(path) { diff --git a/lib/coffee_script/narwhal/lib/coffee-script/loader.js b/lib/coffee_script/narwhal/lib/coffee-script/loader.js index 24b7ef27..9fc8354b 100644 --- a/lib/coffee_script/narwhal/lib/coffee-script/loader.js +++ b/lib/coffee_script/narwhal/lib/coffee-script/loader.js @@ -8,9 +8,9 @@ // Reload the coffee-script environment from source. reload: function reload(topId, path) { coffeescript = coffeescript || require('coffee-script'); - return (factories[topId] = function() { + return factories[topId] = function() { return coffeescript.makeNarwhalFactory(path); - }); + }; }, // Ensure that the coffee-script environment is loaded. load: function load(topId, path) { diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index d9229959..1370299b 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -127,7 +127,7 @@ module CoffeeScript indent = o[:no_wrap] ? '' : TAB @indent = indent o.merge!(:indent => indent, :scope => Scope.new(nil, self)) - code = o[:no_wrap] ? compile_node(o) : compile_with_declarations(o) + code = o[:globals] ? compile_node(o) : compile_with_declarations(o) code.gsub!(STRIP_TRAILING_WHITESPACE, '') o[:no_wrap] ? code : "(function(){\n#{code}\n})();" end @@ -499,6 +499,7 @@ module CoffeeScript o[:top] = true o[:indent] = idt(1) o.delete(:no_wrap) + o.delete(:globals) name = o.delete(:immediate_assign) if @params.last.is_a?(ParamSplatNode) splat = @params.pop