Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Apr 6, 2013
2 parents 0cadcdc + 8be65de commit 170f311
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 37 deletions.
13 changes: 7 additions & 6 deletions lib/coffee-script/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/coffee-script/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions lib/coffee-script/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/coffee-script/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 56 additions & 1 deletion lib/coffee-script/repl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/browser.coffee
Expand Up @@ -15,27 +15,29 @@ CoffeeScript.eval = (code, options = {}) ->
# Running code does not provide access to this scope.
CoffeeScript.run = (code, options = {}) ->
options.bare = on
options.shiftLine = on
Function(compile code, options)()

# If we're not in a browser environment, we're finished with the public API.
return unless window?

# Include source maps where possible. If we've got a base64 encoder, and a
# JSON serializer, we're good to go.
if btoa? and JSON?
# Include source maps where possible. If we've got a base64 encoder, a
# JSON serializer, and tools for escaping unicode characters, we're good to go.
# Ported from https://developer.mozilla.org/en-US/docs/DOM/window.btoa
if btoa? and JSON? and unescape? and encodeURIComponent?
compile = (code, options = {}) ->
options.sourceMap = true
options.inline = true
{js, v3SourceMap} = CoffeeScript.compile code, options
"#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa v3SourceMap}\n//@ sourceURL=coffeescript"
"#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa unescape encodeURIComponent v3SourceMap}\n//@ sourceURL=coffeescript"

# Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, callback, options = {}) ->
options.sourceFiles = [url]
xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP')
else
new XMLHttpRequest()
new window.XMLHttpRequest()
xhr.open 'GET', url, true
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
xhr.onreadystatechange = ->
Expand All @@ -51,7 +53,7 @@ CoffeeScript.load = (url, callback, options = {}) ->
# all script tags with a content-type of `text/coffeescript`.
# This happens on page load.
runScripts = ->
scripts = document.getElementsByTagName 'script'
scripts = window.document.getElementsByTagName 'script'
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
coffees = (s for s in scripts when s.type in coffeetypes)
index = 0
Expand All @@ -71,6 +73,6 @@ runScripts = ->

# Listen for window load, both in decent browsers and in IE.
if window.addEventListener
addEventListener 'DOMContentLoaded', runScripts, no
window.addEventListener 'DOMContentLoaded', runScripts, no
else
attachEvent 'onload', runScripts
window.attachEvent 'onload', runScripts
3 changes: 2 additions & 1 deletion src/coffee-script.coffee
Expand Up @@ -36,7 +36,8 @@ exports.compile = compile = (code, options = {}) ->
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options

currentLine = 0
currentLine += 1 if options.header or options.inline
currentLine += 1 if options.header
currentLine += 1 if options.shiftLine
currentColumn = 0
js = ""
for fragment in fragments
Expand Down
11 changes: 6 additions & 5 deletions src/command.coffee
Expand Up @@ -14,6 +14,7 @@ CoffeeScript = require './coffee-script'
{EventEmitter} = require 'events'

exists = fs.exists or path.exists
useWinPathSep = path.sep is '\\'

# Allow CoffeeScript to emit Node.js events.
helpers.extend CoffeeScript, new EventEmitter
Expand Down Expand Up @@ -256,7 +257,7 @@ removeSource = (source, base, removeJs) ->

# Get the corresponding output JavaScript path for a source file.
outputPath = (source, base, extension=".js") ->
basename = helpers.baseFileName source, yes, path.sep
basename = helpers.baseFileName source, yes, useWinPathSep
srcDir = path.dirname source
baseDir = if base is '.' then srcDir else srcDir.substring base.length
dir = if opts.output then path.join opts.output, baseDir else srcDir
Expand All @@ -274,7 +275,7 @@ writeJs = (base, sourcePath, js, jsPath, generatedSourceMap = null) ->
compile = ->
if opts.compile
js = ' ' if js.length <= 0
if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, path.sep}\n*/\n"
if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, useWinPathSep}\n*/\n"
fs.writeFile jsPath, js, (err) ->
if err
printLine err.message
Expand Down Expand Up @@ -343,13 +344,13 @@ compileOptions = (filename, base) ->
jsPath
sourceRoot: path.relative jsDir, cwd
sourceFiles: [path.relative cwd, filename]
generatedFile: helpers.baseFileName(jsPath, no, path.sep)
generatedFile: helpers.baseFileName(jsPath, no, useWinPathSep)
}
else
answer = helpers.merge answer,
sourceRoot: ""
sourceFiles: [helpers.baseFileName filename, no, path.sep]
generatedFile: helpers.baseFileName(filename, yes, path.sep) + ".js"
sourceFiles: [helpers.baseFileName filename, no, useWinPathSep]
generatedFile: helpers.baseFileName(filename, yes, useWinPathSep) + ".js"
answer

# Start up a new Node.js instance with the arguments in `--nodejs` passed to
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.coffee
Expand Up @@ -118,7 +118,8 @@ exports.locationDataToString = (obj) ->
"No location data"

# A `.coffee.md` compatible version of `basename`, that returns the file sans-extension.
exports.baseFileName = (file, stripExt = no, pathSep = '/') ->
exports.baseFileName = (file, stripExt = no, useWinPathSep = no) ->
pathSep = if useWinPathSep then /\\|\// else /\//
parts = file.split(pathSep)
file = parts[parts.length - 1]
return file unless stripExt
Expand Down
2 changes: 1 addition & 1 deletion src/nodes.coffee
Expand Up @@ -1504,7 +1504,7 @@ exports.While = class While extends Base
set = ''
{body} = this
if body.isEmpty()
body = ''
body = @makeCode ''
else
if @returns
body.makeReturn rvar = o.scope.freeVariable 'results'
Expand Down

0 comments on commit 170f311

Please sign in to comment.