Skip to content

Commit

Permalink
Set runtime options by emitting code.
Browse files Browse the repository at this point in the history
Removes `traceur.options` in favor of `$traceurRuntime.options`.
Only --symbols needs runtime options

Review URL: https://api.github.com/repos/google/traceur-compiler/issues/1646

Closes #1646.
  • Loading branch information
johnjbarton committed Jan 21, 2015
1 parent 20fc024 commit 81880cd
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 51 deletions.
2 changes: 1 addition & 1 deletion demo/await.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<div id="box">Traceur<br>Rocks!</div>

<script>
traceur.options.asyncFunctions = true;
$traceurRuntime.options.asyncFunctions = true;
</script>
<script type="module">

Expand Down
2 changes: 1 addition & 1 deletion demo/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var vm = require('vm');
var util = require('util');

var traceur = require('../src/node/traceur.js');
traceur.options.freeVariableChecker = false;
$traceurRuntime.options.freeVariableChecker = false;

// Debug functions.

Expand Down
2 changes: 1 addition & 1 deletion src/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class Options {
if (this[key] !== ref[key]) {
mismatches.push({
key: key,
now: traceur.options[key],
now: $traceurRuntime.options[key],
v01: ref[key]
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebPageTranscoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class WebPageTranscoder {
}

addFileFromScriptElement(scriptElement, name, content) {
var options = traceur.options;
var options = $traceurRuntime.options;
var nameInfo = {
address: name,
referrerName: window.location.href,
Expand Down
19 changes: 19 additions & 0 deletions src/codegeneration/SymbolTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import {
BinaryExpression,
MemberLookupExpression,
Module,
ForInStatement,
Script,
UnaryExpression
} from '../syntax/trees/ParseTrees.js';
import {ExplodeExpressionTransformer} from './ExplodeExpressionTransformer.js';
Expand All @@ -39,6 +41,7 @@ import {
parseExpression,
parseStatement
} from './PlaceholderParser.js';
import {prependStatements} from './PrependStatements.js';


class ExplodeSymbolExpression extends ExplodeExpressionTransformer {
Expand Down Expand Up @@ -80,6 +83,8 @@ function isSafeTypeofString(tree) {
return true;
}

var runtimeOption = parseStatement `$traceurRuntime.options.symbols = true`;

/**
* This transformer is used with symbol values to ensure that symbols can be
* used as member expressions.
Expand All @@ -96,6 +101,20 @@ function isSafeTypeofString(tree) {
*/
export class SymbolTransformer extends TempVarTransformer {

transformModule(tree) {
return new Module(tree.location,
prependStatements(this.transformList(tree.scriptItemList),
runtimeOption),
this.moduleName_);
}

transformScript(tree) {
return new Script(tree.location,
prependStatements(this.transformList(tree.scriptItemList),
runtimeOption),
this.moduleName_);
}

/**
* Helper for the case where we only want to transform the operand of
* the typeof expression.
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
}

function getOption(name) {
return global.traceur && global.traceur.options[name];
return global.$traceurRuntime.options[name];
}

function defineProperty(object, name, descriptor) {
Expand Down Expand Up @@ -370,6 +370,7 @@
isPrivateName: isPrivateName,
isSymbolString: isSymbolString,
keys: $keys,
options: {},
setupGlobals: setupGlobals,
toObject: toObject,
toProperty: toProperty,
Expand Down
2 changes: 2 additions & 0 deletions test/feature/Symbol/Inherited.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Options: --symbols
'use strict';

assert.equal($traceurRuntime.options.symbols, true);

var s = Symbol();
var p = {};
Object.defineProperty(p, s, {
Expand Down
21 changes: 10 additions & 11 deletions test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
}

var Options = traceur.get('./Options.js').Options;
$traceurRuntime.options = new Options();

function setOptions(load, options) {
var traceurOptions = new Options(options.traceurOptions);
Expand All @@ -133,12 +134,12 @@
function featureTest(name, url, fileLoader) {

teardown(function() {
traceur.options.reset();
$traceurRuntime.options.reset();
});

test(name, function(done) {
var baseURL = './';
var options;
var options = null;
function translateSynchronous(load) {
var source = load.source;
// Only top level file can set options.
Expand Down Expand Up @@ -218,24 +219,22 @@

function cloneTest(name, url, loader) {
teardown(function() {
traceur.options.reset();
$traceurRuntime.options.reset();
});

function doTest(source) {
var options = parseProlog(source);
if (options.skip || options.shouldHaveErrors) {
var prologOptions = parseProlog(source);
if (prologOptions.skip || prologOptions.shouldHaveErrors) {
return;
}

traceur.options.reset();
if (options.traceurOptions)
traceur.options.setFromObject(options.traceurOptions);

var options = new Options(prologOptions.traceurOptions);
var reporter = new traceur.util.CollectingErrorReporter();

function parse(source) {
var file = new traceur.syntax.SourceFile(name, source);
var parser = new traceur.syntax.Parser(file, reporter);
var parser =
new traceur.syntax.Parser(file, reporter, options);
var isModule = /\.module\.js$/.test(url);
if (isModule)
return parser.parseModule();
Expand All @@ -246,7 +245,7 @@
var tree = parse(source);

if (reporter.hadError()) {
fail('Error compiling ' + name + '.\n' +
fail('cloneTest Error compiling ' + name + '.\n' +
reporter.errorsAsString());
return;
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ suite('Compiler', function() {
setup(function() {
Compiler = get('src/Compiler.js').Compiler;
versionLockedOptions = get('src/Options.js').versionLockedOptions;
traceur.options.reset();
});

test('Compiler synchronous', function() {
Expand Down Expand Up @@ -51,7 +50,8 @@ suite('Compiler', function() {
new Options({blockBinding: !versionLockedOptions.blockBinding});
assert.equal(checkDiff.diff(versionLockedOptions).length, 1);

var mismatches = traceur.options.diff(versionLockedOptions);
var options = new Options();
var mismatches = options.diff(versionLockedOptions);
if (mismatches.length)
console.error('Options changed ', mismatches);
assert.equal(mismatches.length, 0);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/node/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
suite('api.js', function() {

setup(function() {
traceur.options.reset();
$traceurRuntime.options.reset();
});

teardown(function() {
traceur.options.reset();
$traceurRuntime.options.reset();
});

test('api compile script function declaration', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/node/generated-code-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite('context test', function() {
if (tempMapName && fs.existsSync(tempMapName))
fs.unlinkSync(tempMapName);
tempMapName = null;
traceur.options.reset();
$traceurRuntime.options.reset();
});

function forwardSlash(s) {
Expand Down Expand Up @@ -236,7 +236,7 @@ suite('context test', function() {
}];
var cwd = process.cwd();
traceur.System.baseURL = cwd;
recursiveCompile(tempFileName, rootSources, traceur.options)
recursiveCompile(tempFileName, rootSources, $traceurRuntime.options)
.then(function () {
assert.equal(process.cwd(), cwd);
done();
Expand Down
12 changes: 6 additions & 6 deletions test/unit/runtime/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ suite('Loader.js', function() {

teardown(function() {
assert.isFalse(reporter.hadError());
traceur.options.reset();
$traceurRuntime.options.reset();
System.baseURL = baseURL;
});

Expand Down Expand Up @@ -73,13 +73,13 @@ suite('Loader.js', function() {
test('traceur@', function() {
var traceur = System.get('traceur@');
var optionsModule = $traceurRuntime.ModuleStore.getForTesting('src/Options.js');
assert.equal(traceur.options, optionsModule.options);
assert.equal(traceur.util.Options, optionsModule.Options);
});

test('Loader.PreCompiledModule', function(done) {
var traceur = System.get('traceur@');
System.import('traceur@', {}).then(function(module) {
assert.equal(traceur.options, module.options);
assert.equal(traceur.util.options, module.util.options);
done();
}).catch(done);
});
Expand All @@ -99,7 +99,7 @@ suite('Loader.js', function() {
var metadata = {traceurOptions: {sourceMaps: true}};
loader.script(src, {name: name, metadata: metadata}).then(
function(result) {
traceur.options.sourceMaps = false;
$traceurRuntime.options.sourceMaps = false;
var normalizedName = System.normalize(name);
var sourceMap = loader.getSourceMap(normalizedName);
assert(sourceMap, 'the sourceMap is defined');
Expand Down Expand Up @@ -244,7 +244,7 @@ suite('Loader.js', function() {
// TODO: Update Traceur loader implementation to support new instantiate output
/* test('LoaderDefine.Instantiate', function(done) {
var loader = getLoader();
traceur.options.modules = 'instantiate';
$traceurRuntime.options.modules = 'instantiate';
var name = './test_instantiate.js';
var src = 'export {name as a} from \'./test_a.js\';\n' +
'export var dd = 8;\n';
Expand Down Expand Up @@ -388,7 +388,7 @@ suite('Loader.js', function() {
var src = " import {name} from './test_a.js';";

var loader = getLoader();
traceur.options.sourceMaps = true;
$traceurRuntime.options.sourceMaps = true;

loader.module(src, {}).then(function (mod) {
// TODO(jjb): where is the test that the source map exists?
Expand Down
17 changes: 9 additions & 8 deletions test/unit/semantics/ConstChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ suite('ConstChecker.js', function() {
return $traceurRuntime.ModuleStore.getForTesting(name);
}

teardown(function() {
traceur.options.reset();
});

var Parser = traceur.syntax.Parser;
var SourceFile = traceur.syntax.SourceFile;
var ErrorReporter = get('src/util/CollectingErrorReporter.js').CollectingErrorReporter;
var validateConst = get('src/semantics/ConstChecker.js').validate;
var Options = get('src/Options.js').Options;

var options;

function makeTest(name, code, expectedErrors, mode) {
test(name, function() {
traceur.options.arrayComprehension = true;
traceur.options.blockBinding = true;
traceur.options.generatorComprehension = true;
options = new Options();
options.arrayComprehension = true;
options.blockBinding = true;
options.generatorComprehension = true;
var reporter = new ErrorReporter();
var parser = new Parser(new SourceFile('SOURCE', code), reporter);
var parser =
new Parser(new SourceFile('SOURCE', code), reporter, options);
var tree = mode === 'script' ?
parser.parseScript() : parser.parseModule();
assert.deepEqual(reporter.errors, []);
Expand Down
17 changes: 8 additions & 9 deletions test/unit/semantics/FreeVariableChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ suite('FreeVariableChecker.js', function() {
var SourceFile = traceur.syntax.SourceFile;
var ErrorReporter = get('src/util/CollectingErrorReporter.js').CollectingErrorReporter;
var validateFreeVars = get('src/semantics/FreeVariableChecker.js').validate;
var Options = get('src/Options.js').Options;

var options;

function makeTest(name, code, expectedErrors, global, mode) {
test(name, function() {
var reporter = new ErrorReporter();
var parser = new Parser(new SourceFile('CODE', code), reporter);
var parser = new Parser(new SourceFile('CODE', code), reporter, options);
var tree = mode === 'module' ?
parser.parseModule() : parser.parseScript();
assert.deepEqual(reporter.errors, []);
Expand All @@ -37,14 +40,10 @@ suite('FreeVariableChecker.js', function() {
}

setup(function() {
traceur.options.reset();
traceur.options.arrayComprehension = true;
traceur.options.blockBinding = true;
traceur.options.generatorComprehension = true;
});

teardown(function() {
traceur.options.reset();
options = new Options();
options.arrayComprehension = true;
options.blockBinding = true;
options.generatorComprehension = true;
});

makeTest('basic', 'x', ['CODE:1:1: x is not defined']);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/semantics/VariableBinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
suite('VariableBinder.js', function() {

teardown(function() {
traceur.options.experimental = false;
$traceurRuntime.options.experimental = false;
});

var ErrorReporter = traceur.util.ErrorReporter;
Expand All @@ -38,7 +38,7 @@ suite('VariableBinder.js', function() {
}

test('BoundIdentifiersInBlock', function() {
traceur.options.blockBinding = true;
$traceurRuntime.options.blockBinding = true;
assert.equal('f', idsToString(variablesInBlock(parse(
'{ function f(x) { var y; }; }'), false)));
assert.equal('', idsToString(variablesInBlock(parse(
Expand Down
7 changes: 4 additions & 3 deletions test/unit/syntax/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ suite('parser.js', function() {
};

teardown(function() {
traceur.options.reset();
$traceurRuntime.options.reset();
});

test('Module', function() {
Expand All @@ -35,13 +35,14 @@ suite('parser.js', function() {
});

test('handleComment', function() {
traceur.options.commentCallback = true;
var options = new traceur.util.Options();
options.commentCallback = true;

var program = '// AAA\n' +
'var b = \'c\';\n' +
'/* DDD */ function e() {}\n';
var sourceFile = new traceur.syntax.SourceFile('Name', program);
var parser = new traceur.syntax.Parser(sourceFile, errorReporter);
var parser = new traceur.syntax.Parser(sourceFile, errorReporter, options);
var comments = [];
parser.handleComment = function(sourceRange) {
comments.push(sourceRange);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/system/ScriptTypeTextTraceur.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<script src="../../../bin/traceur.js"></script>
<script src="../../../src/bootstrap.js"></script>
<script>
traceur.options.experimental = true;
$traceurRuntime.options.experimental = true;
</script>
</head>
<body>
Expand Down

0 comments on commit 81880cd

Please sign in to comment.