Permalink
Browse files

v2.0.0: READ FULL COMMIT MESSAGE

1) Updated traceur to latest trunk. This breaks stuff! READ COMMIT HISTORY
	`new()` is now `constructor()`
	`class method()` is now `static method()`
	more that i dont know yet.
2) Project has transitioned to be a compiler focus.
	New binaries installed, traceur and traceurc (working ver, not googles official traceurc, diff usage)
3) Restructured project files around.
	example files -> examples/
	traceur bootstrap -> split out into lib/ folder
	new bin/ dir
	example out/ dir from compiling examples/
  • Loading branch information...
aikar committed Sep 30, 2011
1 parent 672b4a1 commit 4e80f9938b1c94776fabea87589a965c75225dc9
Showing with 980 additions and 277 deletions.
  1. +29 −30 README.markdown
  2. +4 −0 bin/traceur
  3. +156 −0 bin/traceurc
  4. +4 −0 examples/test.js
  5. +26 −0 examples/testtraceur.js
  6. 0 lib/runtime.js
  7. +8 −0 lib/traceur.js
  8. +52 −0 lib/transform.js
  9. +3 −0 out/examples/test.js
  10. +39 −0 out/examples/testtraceur.js
  11. +446 −0 out/node_modules/traceur-runtime.js
  12. +5 −7 package.json
  13. +0 −5 test.js
  14. +0 −20 testtraceur.js
  15. +0 −64 traceur.js
  16. +7 −7 traceur/demo/generators.js
  17. +2 −1 traceur/demo/repl.html
  18. +14 −14 traceur/presentation/index.html
  19. +2 −1 traceur/presentation/script.js
  20. +5 −5 traceur/src/codegeneration/ClassTransformer.js
  21. +68 −9 traceur/src/codegeneration/DestructuringTransformer.js
  22. +11 −11 traceur/src/codegeneration/{ForEachTransformer.js → ForOfTransformer.js}
  23. +7 −7 traceur/src/codegeneration/GeneratorTransformPass.js
  24. +5 −26 traceur/src/codegeneration/ParseTreeFactory.js
  25. +4 −4 traceur/src/codegeneration/ParseTreeTransformer.js
  26. +7 −7 traceur/src/codegeneration/ParseTreeWriter.js
  27. +5 −5 traceur/src/codegeneration/ProgramTransformer.js
  28. +3 −3 traceur/src/codegeneration/generator/BreakContinueTransformer.js
  29. +3 −3 traceur/src/codegeneration/generator/CPSTransformer.js
  30. +1 −1 traceur/src/codegeneration/module/ModuleDefinitionVisitor.js
  31. +1 −1 traceur/src/filecompiler.js
  32. +19 −3 traceur/src/runtime/runtime.js
  33. +4 −4 traceur/src/semantics/ClassAnalyzer.js
  34. +1 −1 traceur/src/semantics/FreeVariableChecker.js
  35. +3 −3 traceur/src/semantics/VariableBinder.js
  36. +2 −2 traceur/src/semantics/symbols/AggregateSymbol.js
  37. +1 −1 traceur/src/semantics/symbols/MethodSymbol.js
  38. +2 −2 traceur/src/syntax/ParseTreeValidator.js
  39. +2 −2 traceur/src/syntax/ParseTreeVisitor.js
  40. +25 −25 traceur/src/syntax/Parser.js
  41. +1 −0 traceur/src/syntax/PredefinedName.js
  42. +1 −1 traceur/src/syntax/trees/ParseTree.js
  43. +1 −1 traceur/src/syntax/trees/ParseTrees.js
  44. +1 −1 traceur/src/traceur.js
View
@@ -5,42 +5,41 @@ Traceur provides support for Googles Traceur project which adds many enhanced
features to the JavaScript language. For full details on what all Traceur does,
visit <http://code.google.com/p/traceur-compiler/wiki/LanguageFeatures>
-## WARNING
-Traceur has a pretty big impact on performance at this time.
-If you want to use Traceur, I strongly recommend requireing() all files on process start, and understand there will be a sizable delay...
-
-For example, node-optimist at around 500 lines of code takes 176ms to translate (which doesn't even use Traceur syntax!)
-
-Use at own risk
+## NOTICE
+Version 2.0 of this package has transitioned effort to be a compiler and not a
+runtime loader. It still works just the same as a runtime loader, just really,
+DONT DO IT!
## Install
-Traceur is ready to be installed from NPM, but may also be manually added
-to your project with git submodules or a clone. First CD to your project root.
-Ensure a directory named `node_modules` exists.
+Traceur should only be installed globally to your developer system and never be
+included as part of your package. The Traceur syntax transformer is way too slow
+to use in a live system and should only be used as a compiler/tester.
- Install with NPM:
- - `npm install traceur`
-
- - Install with GIT:
- - As a submodule:
- - `git submodule add git://github.com/aikar/traceur node_modules/traceur`
- - `git submodule update --init --recursive`
- - As a plain clone:
- - `git clone git://github.com/aikar/traceur node_modules/traceur`
+ - `npm install traceur -g`
## Usage
-To use Traceur, simply require it at the start of your node.js process.
-You do not need to assign it to a variable or call any special function.
-Simply require('traceur') and then all require() statements after it may use
-Traceur syntax.
-
- require('traceur');
-
- // ./classes/MyClass.js has class MyClass { }
- MyClass = require('./classes/MyClass.js');
-
- var instance = new MyClass;
-
+Visit Traceur website as linked above for language features.
+
+Traceur module provides 2 binaries:
+
+`traceur app.js`:
+This binary will execute node but all require() statements can make use of
+traceur syntax. Note that startup will be slow as the source code is transformed!
+
+This is the same thing as calling require('traceur') then requiring your file.
+
+DO NOT USE THIS FEATURE IN A PRODUCTION APP!
+
+`traceurc inputdir/ outputdir/`: this command takes a directory and finds every
+.js file in the directory and compiles it with traceur then writes it in the same
+dir format as input, to the output dir. A small runtime library is created at
+outputdir/node_modules/traceur-runtime.js that is auto loaded by all modules.
+
+You may need to add this to your package.json so npm wont ignore it...
+(I hate that addition to npm :())
+
+USE THIS FEATURE TO DEPLOY/PUBLISH!
## License
> The MIT License
>
View
@@ -0,0 +1,4 @@
+#!/usr/bin/env node
+var path = require('path');
+require('../')
+require(path.resolve(process.cwd(), process.argv[2]));
View
@@ -0,0 +1,156 @@
+#!/usr/bin/env node
+
+// Copyright 2011 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+(function() {
+ 'use strict';
+ var runtimestub = 'traceur = global.traceur || {};require("traceur-runtime");';
+ if (process.argv.length !== 4) {
+ console.log('Usage: traceurc inputdir/ outdir/');
+ process.exit(1);
+ }
+
+ var fs = require('fs');
+ var path = require('path');
+
+ /**
+ * Reads a script and eval's it into the global scope.
+ * TODO: this is needed for now because of how our scripts are designed.
+ * Change this once we have a module system.
+ * @param {string} filename
+ */
+ function importScript(filename) {
+ filename = path.join(__dirname + '/../traceur/src', filename);
+ var data = fs.readFileSync(filename);
+ if (!data) {
+ throw new Error('Failed to import ' + filename);
+ }
+ data = data.toString('utf8');
+ eval.call(global, data);
+ }
+
+ // Allow traceur.js to use importScript.
+ global.importScript = importScript;
+
+ importScript('./traceur.js');
+ global.traceur.semantics.FreeVariableChecker.checkProgram = function() {}
+ /**
+ * Recursively makes all directoires, similar to mkdir -p
+ * @param {string} dir
+ */
+ function mkdirRecursive(dir) {
+ var parts = path.normalize(dir).split('/');
+
+ dir = '';
+ for (var i = 0; i < parts.length; i++) {
+ dir += parts[i] + '/';
+ if (!path.existsSync(dir)) {
+ fs.mkdirSync(dir, 0x1FF);
+ }
+ }
+ }
+
+ /**
+ * Removes the common prefix of basedir and filedir from filedir
+ * @param {string} basedir
+ * @param {string} filedir
+ */
+ function removeCommonPrefix(basedir, filedir) {
+ var baseparts = basedir.split('/');
+ var fileparts = filedir.split('/');
+
+ var i = 0;
+ while (i < fileparts.length && fileparts[i] === baseparts[i]) {
+ i++;
+ }
+ return fileparts.slice(i).join('/');
+ }
+
+ function compileFiles(outputdir, filenames) {
+ var reporter = new traceur.util.ErrorReporter();
+ var project = new traceur.semantics.symbols.Project();
+
+ console.log('Reading files...');
+ var success = filenames.every(function(filename) {
+ var data = fs.readFileSync(filename);
+ if (!data) {
+ console.log('Failed to read ' + filename);
+ return false;
+ }
+ data = data.toString('utf8');
+ var sourceFile = new traceur.syntax.SourceFile(filename, data);
+ project.addFile(sourceFile);
+ return true;
+ });
+
+ if (!success) {
+ return false;
+ }
+
+ console.log('Compiling...');
+ var results = traceur.codegeneration.Compiler.compile(reporter, project);
+ if (reporter.hadError()) {
+ console.log('Compilation failed.');
+ return false;
+ }
+
+ console.log('Compilation successful');
+
+ results.keys().forEach(function(file) {
+ var tree = results.get(file);
+ var filename = file.name;
+ var result = traceur.codegeneration.ParseTreeWriter.write(tree, false);
+ result = runtimestub + result;
+ // Compute the output path
+ var filedir = fs.realpathSync(path.dirname(filename));
+ filedir = removeCommonPrefix(outputdir, filedir);
+ var outdir = path.join(outputdir, filedir);
+
+ mkdirRecursive(outdir);
+ var outputfile = path.join(outdir, path.basename(filename));
+ fs.writeFileSync(outputfile, new Buffer(result));
+ console.log('Writing of ' + outputfile + ' successful.');
+ });
+
+ return true;
+ }
+ var files = [];
+ function getFileNames(dir) {
+ fs.readdirSync(dir).forEach(function(file) {
+ var full = dir + '/' + file;
+ if (fs.statSync(full).isDirectory()) {
+ getFileNames(full);
+ } else if (path.extname(file) == '.js') {
+ files.push(full);
+ }
+ });
+ }
+ getFileNames(process.argv[2]);
+
+ var outdir = path.resolve(process.cwd(), process.argv[3]);
+ mkdirRecursive(outdir);
+ var outputdir = fs.realpathSync(outdir);
+ if (!compileFiles(outputdir, files)) {
+ process.exit(2);
+ } else {
+ mkdirRecursive(outputdir + '/node_modules/');
+ var runtime = fs.readFileSync(path.join(__dirname + '/../traceur/src/runtime/runtime.js')).toString();
+ // fix bug in traceur
+ runtime = runtime.replace('traceur.syntax.PredefinedName.ITERATOR','"__iterator__"');
+ // make traceur global.
+ runtime = runtime.replace('var traceur =','traceur =');
+ fs.writeFileSync(outputdir + '/node_modules/traceur-runtime.js', runtime);
+ }
+})();
View
@@ -0,0 +1,4 @@
+require('../');
+require('./testtraceur.js');
+
+
View
@@ -0,0 +1,26 @@
+class Foo {
+ constructor(foo = 'RAWR') {
+ console.log(foo);
+ }
+ bar(...baz) {
+ console.log('baz', ...baz)
+ }
+ static baz(bar = 3) {
+ console.log(bar);
+ }
+};
+{
+ let foo = 'bar';
+ let baz = 'baz';
+ console.log(foo, baz);
+}
+cb = (cb)-> {
+ cb(42);
+}
+cb((x) -> {
+ console.error(x * 2);
+})
+Foo.baz();
+var y = new Foo('BLAH');
+var x = new Foo();
+x.bar(1,2,3,4,5);
View
No changes.
View
@@ -0,0 +1,8 @@
+(function() {
+ var fs = require('fs');
+ var orig = require('module').prototype._compile;
+ var transform = require('./transform');
+ require('module').prototype._compile = function(content, filename) {
+ return orig.call(this, transform(content, filename), filename);
+ };
+})();
View
@@ -0,0 +1,52 @@
+var fs = require('fs'),
+ path = require('path'),
+ vm = require('vm');
+
+ function importScript(filename) {
+ filename = path.join(__dirname + '/../traceur/src', filename);
+
+ var data = fs.readFileSync(filename);
+ if (!data) {
+ throw new Error('Failed to import ' + filename);
+ }
+ data = data.toString('utf8');
+ //console.error(data);
+ vm.runInThisContext(data, filename)
+ };
+
+ global.importScript = importScript;
+ importScript('traceur.js');
+
+ //global.traceur = traceurContext.traceur;
+ var traceur = global.traceur;
+
+ // this function is silly and makes traceur unusable. so disable it.
+ traceur.semantics.FreeVariableChecker.checkProgram = function() {}
+
+
+ delete global.importScript;
+
+module.exports = function(data, filename) {
+ var reporter = new traceur.util.ErrorReporter();
+ var project = new traceur.semantics.symbols.Project();
+
+ if (!data) {
+ return false;
+ }
+ data = data.toString('utf8');
+ var sourceFile = new traceur.syntax.SourceFile(filename, data);
+ project.addFile(sourceFile);
+
+ var results = traceur.codegeneration.Compiler.compile(reporter, project);
+ if (reporter.hadError()) {
+ console.error('Compilation failed.');
+ return false;
+ }
+
+ var result = '';
+ results.keys().forEach(function(file) {
+ var tree = results.get(file);
+ result += traceur.codegeneration.ParseTreeWriter.write(tree, false);
+ });
+ return result;
+}
View
@@ -0,0 +1,3 @@
+traceur = global.traceur || {};require("traceur-runtime");
+require('../');
+require('./testtraceur.js');
@@ -0,0 +1,39 @@
+traceur = global.traceur || {};require("traceur-runtime");
+var Foo = traceur.runtime.createClass("Foo", null, null, function() {
+ var foo = arguments.length > 0 ? arguments[0]: 'RAWR';
+ console.log(foo);
+}, undefined, { bar: function() {
+ var baz = Array.prototype.slice.call(arguments, 0);
+ (function($0, $1) {
+ return $0.log.apply($0, $1);
+ })(console, traceur.runtime.spread([false, 'baz', true, baz]));
+ } }, function $static() {
+ this.baz = function() {
+ var bar = arguments.length > 0 ? arguments[0]: 3;
+ console.log(bar);
+ };
+}, null);
+;
+{
+ try {
+ throw undefined;
+ } catch(baz) {
+ try {
+ throw undefined;
+ } catch(foo) {
+ foo = 'bar';
+ baz = 'baz';
+ console.log(foo, baz);
+ }
+ }
+}
+cb = function(cb) {
+ cb(42);
+};
+cb(function(x) {
+ console.error(x * 2);
+});
+Foo.baz();
+var y = new Foo('BLAH');
+var x = new Foo();
+x.bar(1, 2, 3, 4, 5);
Oops, something went wrong.

0 comments on commit 4e80f99

Please sign in to comment.