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
// requested options. Both compile_scripts and watch_scripts share this method.
compile_script = function compile_script(source, code) {
var js, o;
var __dirname, __filename, js, o;
o = options;
try {
if (o.tokens) {
@ -94,6 +94,8 @@
} else if (o.print || o.eval) {
return print(js);
} else {
__filename = source;
__dirname = path.dirname(source);
return eval(js);
}
}

View File

@ -6,7 +6,8 @@
this.exports = this;
Rewriter = this.Rewriter;
}
// Constants ============================================================
// Constants
// ---------
// 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"];
// CoffeeScript-only keywords -- which we're more relaxed about allowing.
@ -110,7 +111,8 @@
}
return this.literal_token();
};
// Tokenizers ==========================================================
// Tokenizers
// ----------
// Matches identifying literals: variables, keywords, method names, etc.
Lexer.prototype.identifier_token = function identifier_token() {
var id, tag;
@ -313,7 +315,8 @@
this.i += value.length;
return true;
};
// Token Manipulators ==================================================
// Token Manipulators
// ------------------
// As we consume a new IDENTIFIER, look at the previous token to determine
// if it's a special kind of access.
Lexer.prototype.name_access_type = function name_access_type() {
@ -344,7 +347,8 @@
Lexer.prototype.assignment_error = function assignment_error() {
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.
Lexer.prototype.token = function token(tag, value) {
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
else if o.lint then lint 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
if o.watch then puts err.message else throw err

View File

@ -4,7 +4,8 @@ else
this.exports: this
Rewriter: this.Rewriter
# Constants ============================================================
# Constants
# ---------
# Keywords that CoffeScript shares in common with JS.
JS_KEYWORDS: [
@ -115,7 +116,8 @@ exports.Lexer: class Lexer
return if @whitespace_token()
return @literal_token()
# Tokenizers ==========================================================
# Tokenizers
# ----------
# Matches identifying literals: variables, keywords, method names, etc.
identifier_token: ->
@ -261,7 +263,8 @@ exports.Lexer: class Lexer
@i += value.length
true
# Token Manipulators ==================================================
# Token Manipulators
# ------------------
# As we consume a new IDENTIFIER, look at the previous token to determine
# if it's a special kind of access.
@ -290,7 +293,8 @@ exports.Lexer: class Lexer
assignment_error: ->
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.
token: (tag, value) ->