Skip to content

Commit

Permalink
fixes #69, avoid module name inside module scope
Browse files Browse the repository at this point in the history
  • Loading branch information
John Messerly committed Mar 2, 2015
1 parent a72f8d6 commit a260ce7
Show file tree
Hide file tree
Showing 30 changed files with 1,027 additions and 1,016 deletions.
35 changes: 23 additions & 12 deletions pkg/dev_compiler/lib/src/codegen/js_codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const String _jsNamedParameterName = r'opt$';
class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
final LibraryInfo libraryInfo;
final TypeRules rules;
final String _libraryName;

/// The variable for the target of the current `..` cascade expression.
SimpleIdentifier _cascadeTarget;
Expand All @@ -48,8 +47,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {

JSCodegenVisitor(LibraryInfo libraryInfo, TypeRules rules)
: libraryInfo = libraryInfo,
rules = rules,
_libraryName = jsLibraryName(libraryInfo.library);
rules = rules;

Element get currentLibrary => libraryInfo.library;

Expand All @@ -71,18 +69,18 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {

// TODO(jmesserly): make these immutable in JS?
for (var name in _exports) {
body.add(js.statement('#.# = #;', [_libraryName, name, name]));
body.add(js.statement('$_EXPORTS.# = #;', [name, name]));
}

var name = _libraryName;
var name = jsLibraryName(libraryInfo.library);
return new JS.Block([
js.statement('var #;', name),
js.statement('''
(function (#) {
(function ($_EXPORTS) {
'use strict';
#;
})(# || (# = {}));
''', [name, body, name, name])
''', [body, name, name])
]);
}

Expand Down Expand Up @@ -641,7 +639,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
var name = node.name;
if (e.enclosingElement is CompilationUnitElement &&
(e.library != libraryInfo.library || _needsModuleGetter(e))) {
return js.call('#.#', [jsLibraryName(e.library), name]);
return js.call('#.#', [_libraryName(e.library), name]);
} else if (currentClass != null && _needsImplicitThis(e)) {
return js.call('this.#', name);
}
Expand Down Expand Up @@ -671,7 +669,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {

JS.Expression result;
if (lib != currentLibrary && lib != null) {
result = js.call('#.#', [jsLibraryName(lib), name]);
result = js.call('#.#', [_libraryName(lib), name]);
} else {
result = new JS.VariableUse(name);
}
Expand Down Expand Up @@ -954,7 +952,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
}

void _flushLazyFields(List<JS.Statement> body) {
var code = _emitLazyFields(_libraryName, _lazyFields);
var code = _emitLazyFields(_EXPORTS, _lazyFields);
if (code != null) body.add(code);
_lazyFields.clear();
}
Expand Down Expand Up @@ -984,8 +982,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {

void _flushLibraryProperties(List<JS.Statement> body) {
if (_properties.isEmpty) return;
body.add(js.statement('dart.copyProperties(#, { # });', [
_libraryName,
body.add(js.statement('dart.copyProperties($_EXPORTS, { # });', [
_properties.map(_emitTopLevelProperty)
]));
_properties.clear();
Expand Down Expand Up @@ -1624,6 +1621,14 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
FunctionBody _functionBody(node) =>
node is FunctionDeclaration ? node.functionExpression.body : node.body;

/// Choose a canonical name from the library element.
/// This never uses the library's name (the identifier in the `library`
/// declaration) as it doesn't have any meaningful rules enforced.
String _libraryName(LibraryElement library) {
if (library == libraryInfo.library) return _EXPORTS;
return jsLibraryName(library);
}

String _maybeBindThis(node) {
if (currentClass == null) return '';
var visitor = _BindThisVisitor._instance;
Expand All @@ -1632,6 +1637,12 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
return visitor._bindThis ? '.bind(this)' : '';
}

/// The name for the library's exports inside itself.
/// This much be a constant because we interpolate it into template strings,
/// and otherwise it would break caching for them.
/// `exports` was chosen as the most similar to ES module patterns.
static const String _EXPORTS = 'exports';

static bool _needsImplicitThis(Element e) =>
e is PropertyAccessorElement && !e.variable.isStatic ||
e is ClassMemberElement && !e.isStatic && e is! ConstructorElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var BenchmarkBase;
(function(BenchmarkBase) {
(function(exports) {
'use strict';
class Expect extends dart.Object {
static equals(expected, actual) {
Expand Down Expand Up @@ -64,6 +64,6 @@ var BenchmarkBase;
}
}
// Exports:
BenchmarkBase.Expect = Expect;
BenchmarkBase.BenchmarkBase = BenchmarkBase;
exports.Expect = Expect;
exports.BenchmarkBase = BenchmarkBase;
})(BenchmarkBase || (BenchmarkBase = {}));
70 changes: 35 additions & 35 deletions pkg/dev_compiler/test/codegen/expect/DeltaBlue/DeltaBlue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var DeltaBlue;
(function(DeltaBlue) {
(function(exports) {
'use strict';
// Function main: () → dynamic
function main() {
Expand Down Expand Up @@ -48,7 +48,7 @@ var DeltaBlue;
}
addConstraint() {
this.addToGraph();
DeltaBlue.planner.incrementalAdd(this);
exports.planner.incrementalAdd(this);
}
satisfy(mark) {
this.chooseMethod(dart.as(mark, core.int));
Expand All @@ -64,14 +64,14 @@ var DeltaBlue;
if (overridden !== null)
overridden.markUnsatisfied();
out.determinedBy = this;
if (!dart.notNull(DeltaBlue.planner.addPropagate(this, dart.as(mark, core.int))))
if (!dart.notNull(exports.planner.addPropagate(this, dart.as(mark, core.int))))
core.print("Cycle encountered");
out.mark = dart.as(mark, core.int);
return overridden;
}
destroyConstraint() {
if (this.isSatisfied())
DeltaBlue.planner.incrementalRemove(this);
exports.planner.incrementalRemove(this);
this.removeFromGraph();
}
isInput() {
Expand Down Expand Up @@ -373,7 +373,7 @@ var DeltaBlue;
}
// Function chainTest: (int) → void
function chainTest(n) {
DeltaBlue.planner = new Planner();
exports.planner = new Planner();
let prev = null, first = null, last = null;
for (let i = 0; i <= n; i++) {
let v = new Variable("v", 0);
Expand All @@ -387,7 +387,7 @@ var DeltaBlue;
}
new StayConstraint(last, STRONG_DEFAULT);
let edit = new EditConstraint(first, PREFERRED);
let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]));
let plan = exports.planner.extractPlanFromConstraints(new List.from([edit]));
for (let i = 0; i < 100; i++) {
first.value = i;
plan.execute();
Expand All @@ -399,7 +399,7 @@ var DeltaBlue;
}
// Function projectionTest: (int) → void
function projectionTest(n) {
DeltaBlue.planner = new Planner();
exports.planner = new Planner();
let scale = new Variable("scale", 10);
let offset = new Variable("offset", 1000);
let src = null, dst = null;
Expand Down Expand Up @@ -431,39 +431,39 @@ var DeltaBlue;
// Function change: (Variable, int) → void
function change(v, newValue) {
let edit = new EditConstraint(v, PREFERRED);
let plan = DeltaBlue.planner.extractPlanFromConstraints(new List.from([edit]));
let plan = exports.planner.extractPlanFromConstraints(new List.from([edit]));
for (let i = 0; i < 10; i++) {
v.value = newValue;
plan.execute();
}
edit.destroyConstraint();
}
DeltaBlue.planner = null;
exports.planner = null;
// Exports:
DeltaBlue.main = main;
DeltaBlue.DeltaBlue = DeltaBlue;
DeltaBlue.Strength = Strength;
DeltaBlue.REQUIRED = REQUIRED;
DeltaBlue.STRONG_PREFERRED = STRONG_PREFERRED;
DeltaBlue.PREFERRED = PREFERRED;
DeltaBlue.STRONG_DEFAULT = STRONG_DEFAULT;
DeltaBlue.NORMAL = NORMAL;
DeltaBlue.WEAK_DEFAULT = WEAK_DEFAULT;
DeltaBlue.WEAKEST = WEAKEST;
DeltaBlue.Constraint = Constraint;
DeltaBlue.UnaryConstraint = UnaryConstraint;
DeltaBlue.StayConstraint = StayConstraint;
DeltaBlue.EditConstraint = EditConstraint;
DeltaBlue.NONE = NONE;
DeltaBlue.FORWARD = FORWARD;
DeltaBlue.BACKWARD = BACKWARD;
DeltaBlue.BinaryConstraint = BinaryConstraint;
DeltaBlue.ScaleConstraint = ScaleConstraint;
DeltaBlue.EqualityConstraint = EqualityConstraint;
DeltaBlue.Variable = Variable;
DeltaBlue.Planner = Planner;
DeltaBlue.Plan = Plan;
DeltaBlue.chainTest = chainTest;
DeltaBlue.projectionTest = projectionTest;
DeltaBlue.change = change;
exports.main = main;
exports.DeltaBlue = DeltaBlue;
exports.Strength = Strength;
exports.REQUIRED = REQUIRED;
exports.STRONG_PREFERRED = STRONG_PREFERRED;
exports.PREFERRED = PREFERRED;
exports.STRONG_DEFAULT = STRONG_DEFAULT;
exports.NORMAL = NORMAL;
exports.WEAK_DEFAULT = WEAK_DEFAULT;
exports.WEAKEST = WEAKEST;
exports.Constraint = Constraint;
exports.UnaryConstraint = UnaryConstraint;
exports.StayConstraint = StayConstraint;
exports.EditConstraint = EditConstraint;
exports.NONE = NONE;
exports.FORWARD = FORWARD;
exports.BACKWARD = BACKWARD;
exports.BinaryConstraint = BinaryConstraint;
exports.ScaleConstraint = ScaleConstraint;
exports.EqualityConstraint = EqualityConstraint;
exports.Variable = Variable;
exports.Planner = Planner;
exports.Plan = Plan;
exports.chainTest = chainTest;
exports.projectionTest = projectionTest;
exports.change = change;
})(DeltaBlue || (DeltaBlue = {}));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _foreign_helper;
(function(_foreign_helper) {
(function(exports) {
'use strict';
// Function JS: (String, String, [dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic]) → dynamic
function JS(typeDescription, codeTemplate, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) {
Expand Down Expand Up @@ -122,35 +122,35 @@ var _foreign_helper;
return a + b;
}
// Exports:
_foreign_helper.JS = JS;
_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT = JS_CURRENT_ISOLATE_CONTEXT;
_foreign_helper.IsolateContext = IsolateContext;
_foreign_helper.JS_CALL_IN_ISOLATE = JS_CALL_IN_ISOLATE;
_foreign_helper.DART_CLOSURE_TO_JS = DART_CLOSURE_TO_JS;
_foreign_helper.RAW_DART_FUNCTION_REF = RAW_DART_FUNCTION_REF;
_foreign_helper.JS_SET_CURRENT_ISOLATE = JS_SET_CURRENT_ISOLATE;
_foreign_helper.JS_CREATE_ISOLATE = JS_CREATE_ISOLATE;
_foreign_helper.JS_DART_OBJECT_CONSTRUCTOR = JS_DART_OBJECT_CONSTRUCTOR;
_foreign_helper.JS_INTERCEPTOR_CONSTANT = JS_INTERCEPTOR_CONSTANT;
_foreign_helper.JS_OPERATOR_IS_PREFIX = JS_OPERATOR_IS_PREFIX;
_foreign_helper.JS_OPERATOR_AS_PREFIX = JS_OPERATOR_AS_PREFIX;
_foreign_helper.JS_OBJECT_CLASS_NAME = JS_OBJECT_CLASS_NAME;
_foreign_helper.JS_NULL_CLASS_NAME = JS_NULL_CLASS_NAME;
_foreign_helper.JS_FUNCTION_CLASS_NAME = JS_FUNCTION_CLASS_NAME;
_foreign_helper.JS_IS_INDEXABLE_FIELD_NAME = JS_IS_INDEXABLE_FIELD_NAME;
_foreign_helper.JS_CURRENT_ISOLATE = JS_CURRENT_ISOLATE;
_foreign_helper.JS_SIGNATURE_NAME = JS_SIGNATURE_NAME;
_foreign_helper.JS_TYPEDEF_TAG = JS_TYPEDEF_TAG;
_foreign_helper.JS_FUNCTION_TYPE_TAG = JS_FUNCTION_TYPE_TAG;
_foreign_helper.JS_FUNCTION_TYPE_VOID_RETURN_TAG = JS_FUNCTION_TYPE_VOID_RETURN_TAG;
_foreign_helper.JS_FUNCTION_TYPE_RETURN_TYPE_TAG = JS_FUNCTION_TYPE_RETURN_TYPE_TAG;
_foreign_helper.JS_FUNCTION_TYPE_REQUIRED_PARAMETERS_TAG = JS_FUNCTION_TYPE_REQUIRED_PARAMETERS_TAG;
_foreign_helper.JS_FUNCTION_TYPE_OPTIONAL_PARAMETERS_TAG = JS_FUNCTION_TYPE_OPTIONAL_PARAMETERS_TAG;
_foreign_helper.JS_FUNCTION_TYPE_NAMED_PARAMETERS_TAG = JS_FUNCTION_TYPE_NAMED_PARAMETERS_TAG;
_foreign_helper.JS_GET_NAME = JS_GET_NAME;
_foreign_helper.JS_EMBEDDED_GLOBAL = JS_EMBEDDED_GLOBAL;
_foreign_helper.JS_GET_FLAG = JS_GET_FLAG;
_foreign_helper.JS_EFFECT = JS_EFFECT;
_foreign_helper.JS_CONST = JS_CONST;
_foreign_helper.JS_STRING_CONCAT = JS_STRING_CONCAT;
exports.JS = JS;
exports.JS_CURRENT_ISOLATE_CONTEXT = JS_CURRENT_ISOLATE_CONTEXT;
exports.IsolateContext = IsolateContext;
exports.JS_CALL_IN_ISOLATE = JS_CALL_IN_ISOLATE;
exports.DART_CLOSURE_TO_JS = DART_CLOSURE_TO_JS;
exports.RAW_DART_FUNCTION_REF = RAW_DART_FUNCTION_REF;
exports.JS_SET_CURRENT_ISOLATE = JS_SET_CURRENT_ISOLATE;
exports.JS_CREATE_ISOLATE = JS_CREATE_ISOLATE;
exports.JS_DART_OBJECT_CONSTRUCTOR = JS_DART_OBJECT_CONSTRUCTOR;
exports.JS_INTERCEPTOR_CONSTANT = JS_INTERCEPTOR_CONSTANT;
exports.JS_OPERATOR_IS_PREFIX = JS_OPERATOR_IS_PREFIX;
exports.JS_OPERATOR_AS_PREFIX = JS_OPERATOR_AS_PREFIX;
exports.JS_OBJECT_CLASS_NAME = JS_OBJECT_CLASS_NAME;
exports.JS_NULL_CLASS_NAME = JS_NULL_CLASS_NAME;
exports.JS_FUNCTION_CLASS_NAME = JS_FUNCTION_CLASS_NAME;
exports.JS_IS_INDEXABLE_FIELD_NAME = JS_IS_INDEXABLE_FIELD_NAME;
exports.JS_CURRENT_ISOLATE = JS_CURRENT_ISOLATE;
exports.JS_SIGNATURE_NAME = JS_SIGNATURE_NAME;
exports.JS_TYPEDEF_TAG = JS_TYPEDEF_TAG;
exports.JS_FUNCTION_TYPE_TAG = JS_FUNCTION_TYPE_TAG;
exports.JS_FUNCTION_TYPE_VOID_RETURN_TAG = JS_FUNCTION_TYPE_VOID_RETURN_TAG;
exports.JS_FUNCTION_TYPE_RETURN_TYPE_TAG = JS_FUNCTION_TYPE_RETURN_TYPE_TAG;
exports.JS_FUNCTION_TYPE_REQUIRED_PARAMETERS_TAG = JS_FUNCTION_TYPE_REQUIRED_PARAMETERS_TAG;
exports.JS_FUNCTION_TYPE_OPTIONAL_PARAMETERS_TAG = JS_FUNCTION_TYPE_OPTIONAL_PARAMETERS_TAG;
exports.JS_FUNCTION_TYPE_NAMED_PARAMETERS_TAG = JS_FUNCTION_TYPE_NAMED_PARAMETERS_TAG;
exports.JS_GET_NAME = JS_GET_NAME;
exports.JS_EMBEDDED_GLOBAL = JS_EMBEDDED_GLOBAL;
exports.JS_GET_FLAG = JS_GET_FLAG;
exports.JS_EFFECT = JS_EFFECT;
exports.JS_CONST = JS_CONST;
exports.JS_STRING_CONCAT = JS_STRING_CONCAT;
})(_foreign_helper || (_foreign_helper = {}));
Loading

0 comments on commit a260ce7

Please sign in to comment.