defining __filename and __dirname correctly as local variables for eval'd scripts

This commit is contained in:
Jeremy Ashkenas 2010-02-28 19:56:00 -05:00
parent 30cf63ec92
commit 45bad556ab
4 changed files with 23 additions and 10 deletions

View File

@ -78,7 +78,7 @@
// Compile a single source script, containing the given code, according to the // Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method. // requested options. Both compile_scripts and watch_scripts share this method.
compile_script = function compile_script(source, code) { compile_script = function compile_script(source, code) {
var js, o; var __dirname, __filename, js, o;
o = options; o = options;
try { try {
if (o.tokens) { if (o.tokens) {
@ -94,6 +94,8 @@
} else if (o.print || o.eval) { } else if (o.print || o.eval) {
return print(js); return print(js);
} else { } else {
__filename = source;
__dirname = path.dirname(source);
return eval(js); return eval(js);
} }
} }

View File

@ -6,7 +6,8 @@
this.exports = this; this.exports = this;
Rewriter = this.Rewriter; Rewriter = this.Rewriter;
} }
// Constants ============================================================ // Constants
// ---------
// Keywords that CoffeScript shares in common with JS. // Keywords that CoffeScript shares in common with JS.
JS_KEYWORDS = ["if", "else", "true", "false", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", "delete", "instanceof", "typeof", "switch", "super", "extends", "class"]; JS_KEYWORDS = ["if", "else", "true", "false", "new", "return", "try", "catch", "finally", "throw", "break", "continue", "for", "in", "while", "delete", "instanceof", "typeof", "switch", "super", "extends", "class"];
// CoffeeScript-only keywords -- which we're more relaxed about allowing. // CoffeeScript-only keywords -- which we're more relaxed about allowing.
@ -110,7 +111,8 @@
} }
return this.literal_token(); return this.literal_token();
}; };
// Tokenizers ========================================================== // Tokenizers
// ----------
// Matches identifying literals: variables, keywords, method names, etc. // Matches identifying literals: variables, keywords, method names, etc.
Lexer.prototype.identifier_token = function identifier_token() { Lexer.prototype.identifier_token = function identifier_token() {
var id, tag; var id, tag;
@ -313,7 +315,8 @@
this.i += value.length; this.i += value.length;
return true; return true;
}; };
// Token Manipulators ================================================== // Token Manipulators
// ------------------
// As we consume a new IDENTIFIER, look at the previous token to determine // As we consume a new IDENTIFIER, look at the previous token to determine
// if it's a special kind of access. // if it's a special kind of access.
Lexer.prototype.name_access_type = function name_access_type() { Lexer.prototype.name_access_type = function name_access_type() {
@ -344,7 +347,8 @@
Lexer.prototype.assignment_error = function assignment_error() { Lexer.prototype.assignment_error = function assignment_error() {
throw new Error('SyntaxError: Reserved word "' + this.value() + '" on line ' + this.line + ' can\'t be assigned'); throw new Error('SyntaxError: Reserved word "' + this.value() + '" on line ' + this.line + ' can\'t be assigned');
}; };
// Helpers ============================================================= // Helpers
// -------
// Add a token to the results, taking note of the line number. // Add a token to the results, taking note of the line number.
Lexer.prototype.token = function token(tag, value) { Lexer.prototype.token = function token(tag, value) {
return this.tokens.push([tag, value, this.line]); return this.tokens.push([tag, value, this.line]);

View File

@ -80,7 +80,10 @@ compile_script: (source, code) ->
if o.compile then write_js source, js if o.compile then write_js source, js
else if o.lint then lint js else if o.lint then lint js
else if o.print or o.eval then print js else if o.print or o.eval then print js
else eval js else
__filename: source
__dirname: path.dirname source
eval js
catch err catch err
if o.watch then puts err.message else throw err if o.watch then puts err.message else throw err

View File

@ -4,7 +4,8 @@ else
this.exports: this this.exports: this
Rewriter: this.Rewriter Rewriter: this.Rewriter
# Constants ============================================================ # Constants
# ---------
# Keywords that CoffeScript shares in common with JS. # Keywords that CoffeScript shares in common with JS.
JS_KEYWORDS: [ JS_KEYWORDS: [
@ -115,7 +116,8 @@ exports.Lexer: class Lexer
return if @whitespace_token() return if @whitespace_token()
return @literal_token() return @literal_token()
# Tokenizers ========================================================== # Tokenizers
# ----------
# Matches identifying literals: variables, keywords, method names, etc. # Matches identifying literals: variables, keywords, method names, etc.
identifier_token: -> identifier_token: ->
@ -261,7 +263,8 @@ exports.Lexer: class Lexer
@i += value.length @i += value.length
true true
# Token Manipulators ================================================== # Token Manipulators
# ------------------
# As we consume a new IDENTIFIER, look at the previous token to determine # As we consume a new IDENTIFIER, look at the previous token to determine
# if it's a special kind of access. # if it's a special kind of access.
@ -290,7 +293,8 @@ exports.Lexer: class Lexer
assignment_error: -> assignment_error: ->
throw new Error 'SyntaxError: Reserved word "' + @value() + '" on line ' + @line + ' can\'t be assigned' throw new Error 'SyntaxError: Reserved word "' + @value() + '" on line ' + @line + ' can\'t be assigned'
# Helpers ============================================================= # Helpers
# -------
# Add a token to the results, taking note of the line number. # Add a token to the results, taking note of the line number.
token: (tag, value) -> token: (tag, value) ->