Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Unified Diff: pkg/compiler/lib/src/js_emitter/class_stub_generator.dart

Issue 889703004: dart2js: emit tear-offs in new emitter. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: WIP. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_emitter/new_emitter/model_emitter.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/js_emitter/class_stub_generator.dart
diff --git a/pkg/compiler/lib/src/js_emitter/class_stub_generator.dart b/pkg/compiler/lib/src/js_emitter/class_stub_generator.dart
index d7e1578c9962a0ecc46ba159681e45a0fd10f593..79dbcf607969116f933e64700799161b5a6b3a8a 100644
--- a/pkg/compiler/lib/src/js_emitter/class_stub_generator.dart
+++ b/pkg/compiler/lib/src/js_emitter/class_stub_generator.dart
@@ -112,3 +112,101 @@ class ClassStubGenerator {
return generatedStubs;
}
}
+
+/// Creates two JavaScript functions: `tearOffGetter` and `tearOff`.
+///
+/// `tearOffGetter` is internal and only used by `tearOff`.
+///
+/// `tearOff` takes the following arguments:
+/// * `funcs`: a list of functions. These are the functions representing the
+/// member that is torn off. There can be more than one, since a member
+/// can have several stubs.
+/// Each function must have the `$callName` property set.
+/// * `reflectionInfo`: contains reflective information, and the function
+/// type. TODO(floitsch): point to where this is specified.
+/// * `isStatic`.
+/// * `name`.
+/// * `isIntercepted.
+List<jsAst.Statement> buildTearOffCode(JavaScriptBackend backend) {
+ Namer namer = backend.namer;
+ Compiler compiler = backend.compiler;
+
+ Element closureFromTearOff = backend.findHelper('closureFromTearOff');
+ String tearOffAccessText;
+ jsAst.Expression tearOffAccessExpression;
+ String tearOffGlobalObjectName;
+ String tearOffGlobalObject;
+ if (closureFromTearOff != null) {
+ // We need both the AST that references [closureFromTearOff] and a string
+ // for the NoCsp version that constructs a function.
+ tearOffAccessExpression =
+ backend.emitter.staticFunctionAccess(closureFromTearOff);
+ tearOffAccessText =
+ jsAst.prettyPrint(tearOffAccessExpression, compiler).getText();
+ tearOffGlobalObjectName = tearOffGlobalObject =
+ namer.globalObjectFor(closureFromTearOff);
+ } else {
+ // Default values for mocked-up test libraries.
+ tearOffAccessText =
+ r'''function() { throw 'Helper \'closureFromTearOff\' missing.' }''';
+ tearOffAccessExpression = js(tearOffAccessText);
+ tearOffGlobalObjectName = 'MissingHelperFunction';
+ tearOffGlobalObject = '($tearOffAccessText())';
+ }
+
+ jsAst.Statement tearOffGetter;
+ if (!compiler.useContentSecurityPolicy) {
+ // This template is uncached because it is constructed from code fragments
+ // that can change from compilation to compilation. Some of these could be
+ // avoided, except for the string literals that contain the compiled access
+ // path to 'closureFromTearOff'.
+ tearOffGetter = js.uncachedStatementTemplate('''
+ function tearOffGetter(funcs, reflectionInfo, name, isIntercepted) {
+ return isIntercepted
+ ? new Function("funcs", "reflectionInfo", "name",
+ "$tearOffGlobalObjectName", "c",
+ "return function tearOff_" + name + (functionCounter++)+ "(x) {" +
+ "if (c === null) c = $tearOffAccessText(" +
+ "this, funcs, reflectionInfo, false, [x], name);" +
+ "return new c(this, funcs[0], x, name);" +
+ "}")(funcs, reflectionInfo, name, $tearOffGlobalObject, null)
+ : new Function("funcs", "reflectionInfo", "name",
+ "$tearOffGlobalObjectName", "c",
+ "return function tearOff_" + name + (functionCounter++)+ "() {" +
+ "if (c === null) c = $tearOffAccessText(" +
+ "this, funcs, reflectionInfo, false, [], name);" +
+ "return new c(this, funcs[0], null, name);" +
+ "}")(funcs, reflectionInfo, name, $tearOffGlobalObject, null);
+ }''').instantiate([]);
+ } else {
+ tearOffGetter = js.statement('''
+ function tearOffGetter(funcs, reflectionInfo, name, isIntercepted) {
+ var cache = null;
+ return isIntercepted
+ ? function(x) {
+ if (cache === null) cache = #(
+ this, funcs, reflectionInfo, false, [x], name);
+ return new cache(this, funcs[0], x, name);
+ }
+ : function() {
+ if (cache === null) cache = #(
+ this, funcs, reflectionInfo, false, [], name);
+ return new cache(this, funcs[0], null, name);
+ };
+ }''', [tearOffAccessExpression, tearOffAccessExpression]);
+ }
+
+ jsAst.Statement tearOff = js.statement('''
+ function tearOff(funcs, reflectionInfo, isStatic, name, isIntercepted) {
+ var cache;
+ return isStatic
+ ? function() {
+ if (cache === void 0) cache = #tearOff(
+ this, funcs, reflectionInfo, true, [], name).prototype;
+ return cache;
+ }
+ : tearOffGetter(funcs, reflectionInfo, name, isIntercepted);
+ }''', {'tearOff': tearOffAccessExpression});
+
+ return <jsAst.Statement>[tearOffGetter, tearOff];
+}
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_emitter/new_emitter/model_emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698