Sage/streamlinejs — pull request #156 by goffrie (2013-08-03 19:33:54+00:00)
Title: More source-map related improvements

This pull request:

1. Fixes a little bug where non-'_' callbacks weren't being handled by `removeFast` in callbacks mode.
2. Adds a stack trace handler to the `underscored.js`, so just like CoffeeScript, if you run your program with `_node` or `_coffee` it will produce stack traces matching the original file, using source maps. This is explicitly designed to be (relatively) interoperable with other overloaders of `Error.prepareStackTrace`, so `_coffee` will give you proper source locations for both `._coffee` and `.coffee` (and of course `._js` and `.js`) files.
3. Modifies the emitted line/frame number information in callbacks mode to account for source maps. This means that the 'async stack' generated by streamline will have the original line and column numbers (where possible), instead of taking line numbers from the JS intermediate. The filename won't be right except when using the require.extensions handler, though (still uses __filename).
4. Adds simple source maps to fibers mode. This basically modifies the `catchup` function to work a "token" at a time (using a dead-simple regex, fairly hacky) and build up a SourceNode instead of a plain string, just like the callbacks compiler.
5. Fixes a bug I introduced in the callbacks compiler where syntax like `{ "quoted key": 123 }` caused an error in the `indent` function.
6. Improves the callbacks-source maps to cover more stuff, so now they look like [this](http://sokra.github.io/source-map-visualization/#base64,LyoqKiBHZW5lcmF0ZWQgYnkgc3RyZWFtbGluZSAwLjcuMGEgKGNhbGxiYWNrcykgLSBETyBOT1QgRURJVCAqKiovCiJ1c2Ugc3RyaWN0IjsKdmFyIF9fcnQ9cmVxdWlyZSgnc3RyZWFtbGluZS9saWIvY2FsbGJhY2tzL3J1bnRpbWUnKS5ydW50aW1lKF9fZmlsZW5hbWUpLF9fZnVuYz1fX3J0Ll9fZnVuYyxfX2NiPV9fcnQuX19jYixfX3RyYXA9X19ydC5fX3RyYXAsX190cnlDYXRjaD1fX3J0Ll9fdHJ5Q2F0Y2g7CihmdW5jdGlvbiBtYWluKF8pIHsKICB2YXIgZnMsIGZsb3dzLCBmaWxlRnVubmVsLCBwLCB0MDsKICBmdW5jdGlvbiBkdShfLCBwYXRoKSB7CiAgICB2YXIgdG90YWwsIHN0YXQsIGZpbGVzOwogICAgdmFyIF9fZnJhbWUgPSB7CiAgICAgIG5hbWU6ICJkdSIsCiAgICAgIGxpbmU6IDE5CiAgICB9OwogICAgcmV0dXJuIF9fZnVuYyhfLCB0aGlzLCBhcmd1bWVudHMsIGR1LCAwLCBfX2ZyYW1lLCBmdW5jdGlvbiBfXyRkdSgpIHsKICAgICAgdG90YWwgPSAwOwogICAgICByZXR1cm4gZnMuc3RhdChwYXRoLCBfX2NiKF8sIF9fZnJhbWUsIDIsIDEzLCBmdW5jdGlvbiBfX18oX18wLCBfXzMpIHsKICAgICAgICBzdGF0ID0gX18zOwogICAgICAgIHJldHVybiAoZnVuY3Rpb24gX18kZHUoX190aGVuKSB7CiAgICAgICAgICBpZiAoc3RhdC5pc0ZpbGUoKSkgewogICAgICAgICAgICByZXR1cm4gZmlsZUZ1bm5lbChfX2NiKF8sIF9fZnJhbWUsIDQsIDQsIF9fdGhlbiwgdHJ1ZSksIGZ1bmN0aW9uIF9fMShfKSB7CiAgICAgICAgICAgICAgdmFyIF9fZnJhbWUgPSB7CiAgICAgICAgICAgICAgICBuYW1lOiAiX18xIiwKICAgICAgICAgICAgICAgIGxpbmU6IDIzCiAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICByZXR1cm4gX19mdW5jKF8sIHRoaXMsIGFyZ3VtZW50cywgX18xLCAwLCBfX2ZyYW1lLCBmdW5jdGlvbiBfXyRfXzEoKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gZnMucmVhZEZpbGUocGF0aCwgX19jYihfLCBfX2ZyYW1lLCAxLCAxNSwgZnVuY3Rpb24gX19fKF9fMCwgX18xKSB7CiAgICAgICAgICAgICAgICAgIHRvdGFsICs9IF9fMS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIF8oKTsKICAgICAgICAgICAgICAgIH0sIHRydWUpKTsKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfSk7CiAgICAgICAgICB9CiAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIHJldHVybiAoZnVuY3Rpb24gX18kZHUoX190aGVuKSB7CiAgICAgICAgICAgICAgaWYgKHN0YXQuaXNEaXJlY3RvcnkoKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIGZzLnJlYWRkaXIocGF0aCwgX19jYihfLCBfX2ZyYW1lLCA4LCAxNiwgZnVuY3Rpb24gX19fKF9fMCwgX180KSB7CiAgICAgICAgICAgICAgICAgIGZpbGVzID0gX180OwogICAgICAgICAgICAgICAgICByZXR1cm4gZmlsZXMubWFwKGZ1bmN0aW9uKGZpbGUpIHsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gZHUobnVsbCwgKChwYXRoICsgIi8iKSArIGZpbGUpKTsKICAgICAgICAgICAgICAgICAgfSkucmVkdWNlXyhfX2NiKF8sIF9fZnJhbWUsIDExLCAxMywgZnVuY3Rpb24gX19fKF9fMCwgX181KSB7CiAgICAgICAgICAgICAgICAgICAgdG90YWwgKz0gX181OwogICAgICAgICAgICAgICAgICAgIGNvbnNvbGUubG9nKCgocGF0aCArICI6ICIpICsgdG90YWwpKTsKICAgICAgICAgICAgICAgICAgICBfX3RoZW4oKTsKICAgICAgICAgICAgICAgICAgfSwgdHJ1ZSksIGZ1bmN0aW9uIF9fMihfLCB2YWwsIGZ1dHVyZSkgewogICAgICAgICAgICAgICAgICAgIHZhciBfX2ZyYW1lID0gewogICAgICAgICAgICAgICAgICAgICAgbmFtZTogIl9fMiIsCiAgICAgICAgICAgICAgICAgICAgICBsaW5lOiAzMAogICAgICAgICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIF9fZnVuYyhfLCB0aGlzLCBhcmd1bWVudHMsIF9fMiwgMCwgX19mcmFtZSwgZnVuY3Rpb24gX18kX18yKCkgewogICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGZ1dHVyZShfX2NiKF8sIF9fZnJhbWUsIDEsIDE5LCBmdW5jdGlvbiBfX18oX18wLCBfXzIpIHsKICAgICAgICAgICAgICAgICAgICAgICAgdmFyIF9fMSA9ICh2YWwgKyBfXzIpOwogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gXyhudWxsLCBfXzEpOwogICAgICAgICAgICAgICAgICAgICAgfSwgdHJ1ZSkpOwogICAgICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAgICAgICB9LCAwKTsKICAgICAgICAgICAgICAgIH0sIHRydWUpKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgY29uc29sZS5sb2coKHBhdGggKyAiOiBvZGQgZmlsZSIpKTsKICAgICAgICAgICAgICAgIF9fdGhlbigpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICA7CiAgICAgICAgICAgIH0pKF9fdGhlbik7CiAgICAgICAgICB9CiAgICAgICAgICA7CiAgICAgICAgfSkoZnVuY3Rpb24gX18kZHUoKSB7CiAgICAgICAgICByZXR1cm4gXyhudWxsLCB0b3RhbCk7CiAgICAgICAgfSk7CiAgICAgIH0sIHRydWUpKTsKICAgIH0pOwogIH07CiAgdmFyIF9fZnJhbWUgPSB7CiAgICBuYW1lOiAibWFpbiIsCiAgICBsaW5lOiAxCiAgfTsKICByZXR1cm4gX19mdW5jKF8sIHRoaXMsIGFyZ3VtZW50cywgbWFpbiwgMCwgX19mcmFtZSwgZnVuY3Rpb24gX18kbWFpbigpIHsKICAgIGZzID0gcmVxdWlyZSgiZnMiKTsKICAgIGZsb3dzID0gcmVxdWlyZSgic3RyZWFtbGluZS9saWIvdXRpbC9mbG93cyIpOwogICAgZmlsZUZ1bm5lbCA9IGZsb3dzLmZ1bm5lbCgyMCk7CiAgICByZXR1cm4gKGZ1bmN0aW9uIF9fXyhfX3RoZW4pIHsKICAgICAgKGZ1bmN0aW9uIF9fXyhfKSB7CiAgICAgICAgX190cnlDYXRjaChfLCBmdW5jdGlvbiBfXyRtYWluKCkgewogICAgICAgICAgcCA9ICgocHJvY2Vzcy5hcmd2Lmxlbmd0aCA+IDIpID8gcHJvY2Vzcy5hcmd2WzJdIDogIi4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