OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
6 | 6 |
7 /// This class should morph into something that makes it easy to build | 7 /// This class should morph into something that makes it easy to build |
8 /// JavaScript representations of libraries, class-sides, and instance-sides. | 8 /// JavaScript representations of libraries, class-sides, and instance-sides. |
9 /// Initially, it is just a placeholder for code that is moved from | 9 /// Initially, it is just a placeholder for code that is moved from |
10 /// [CodeEmitterTask]. | 10 /// [CodeEmitterTask]. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 String invocationName = namer.invocationName(selector); | 43 String invocationName = namer.invocationName(selector); |
44 if (alreadyGenerated.contains(invocationName)) return; | 44 if (alreadyGenerated.contains(invocationName)) return; |
45 alreadyGenerated.add(invocationName); | 45 alreadyGenerated.add(invocationName); |
46 | 46 |
47 bool isInterceptedMethod = backend.isInterceptedMethod(member); | 47 bool isInterceptedMethod = backend.isInterceptedMethod(member); |
48 | 48 |
49 // If the method is intercepted, we need to also pass the actual receiver. | 49 // If the method is intercepted, we need to also pass the actual receiver. |
50 int extraArgumentCount = isInterceptedMethod ? 1 : 0; | 50 int extraArgumentCount = isInterceptedMethod ? 1 : 0; |
51 // Use '$receiver' to avoid clashes with other parameter names. Using | 51 // Use '$receiver' to avoid clashes with other parameter names. Using |
52 // '$receiver' works because [:namer.safeName:] used for getting parameter | 52 // '$receiver' works because namer.safeVariableName used for getting |
53 // names never returns a name beginning with a single '$'. | 53 // parameter names never returns a name beginning with a single '$'. |
54 String receiverArgumentName = r'$receiver'; | 54 String receiverArgumentName = r'$receiver'; |
55 | 55 |
56 // The parameters that this stub takes. | 56 // The parameters that this stub takes. |
57 List<jsAst.Parameter> parametersBuffer = | 57 List<jsAst.Parameter> parametersBuffer = |
58 new List<jsAst.Parameter>(selector.argumentCount + extraArgumentCount); | 58 new List<jsAst.Parameter>(selector.argumentCount + extraArgumentCount); |
59 // The arguments that will be passed to the real method. | 59 // The arguments that will be passed to the real method. |
60 List<jsAst.Expression> argumentsBuffer = | 60 List<jsAst.Expression> argumentsBuffer = |
61 new List<jsAst.Expression>( | 61 new List<jsAst.Expression>( |
62 parameters.parameterCount + extraArgumentCount); | 62 parameters.parameterCount + extraArgumentCount); |
63 | 63 |
64 int count = 0; | 64 int count = 0; |
65 if (isInterceptedMethod) { | 65 if (isInterceptedMethod) { |
66 count++; | 66 count++; |
67 parametersBuffer[0] = new jsAst.Parameter(receiverArgumentName); | 67 parametersBuffer[0] = new jsAst.Parameter(receiverArgumentName); |
68 argumentsBuffer[0] = js('#', receiverArgumentName); | 68 argumentsBuffer[0] = js('#', receiverArgumentName); |
69 emitter.interceptorEmitter.interceptorInvocationNames.add(invocationName); | 69 emitter.interceptorEmitter.interceptorInvocationNames.add(invocationName); |
70 } | 70 } |
71 | 71 |
72 int optionalParameterStart = positionalArgumentCount + extraArgumentCount; | 72 int optionalParameterStart = positionalArgumentCount + extraArgumentCount; |
73 // Includes extra receiver argument when using interceptor convention | 73 // Includes extra receiver argument when using interceptor convention |
74 int indexOfLastOptionalArgumentInParameters = optionalParameterStart - 1; | 74 int indexOfLastOptionalArgumentInParameters = optionalParameterStart - 1; |
75 | 75 |
76 int parameterIndex = 0; | 76 int parameterIndex = 0; |
77 parameters.orderedForEachParameter((ParameterElement element) { | 77 parameters.orderedForEachParameter((ParameterElement element) { |
78 String jsName = backend.namer.safeName(element.name); | 78 String jsName = backend.namer.safeVariableName(element.name); |
79 assert(jsName != receiverArgumentName); | 79 assert(jsName != receiverArgumentName); |
80 if (count < optionalParameterStart) { | 80 if (count < optionalParameterStart) { |
81 parametersBuffer[count] = new jsAst.Parameter(jsName); | 81 parametersBuffer[count] = new jsAst.Parameter(jsName); |
82 argumentsBuffer[count] = js('#', jsName); | 82 argumentsBuffer[count] = js('#', jsName); |
83 } else { | 83 } else { |
84 int index = names.indexOf(element.name); | 84 int index = names.indexOf(element.name); |
85 if (index != -1) { | 85 if (index != -1) { |
86 indexOfLastOptionalArgumentInParameters = count; | 86 indexOfLastOptionalArgumentInParameters = count; |
87 // The order of the named arguments is not the same as the | 87 // The order of the named arguments is not the same as the |
88 // one in the real method (which is in Dart source order). | 88 // one in the real method (which is in Dart source order). |
(...skipping 21 matching lines...) Expand all Loading... |
110 | 110 |
111 var body; // List or jsAst.Statement. | 111 var body; // List or jsAst.Statement. |
112 if (member.hasFixedBackendName) { | 112 if (member.hasFixedBackendName) { |
113 body = emitter.nativeEmitter.generateParameterStubStatements( | 113 body = emitter.nativeEmitter.generateParameterStubStatements( |
114 member, isInterceptedMethod, invocationName, | 114 member, isInterceptedMethod, invocationName, |
115 parametersBuffer, argumentsBuffer, | 115 parametersBuffer, argumentsBuffer, |
116 indexOfLastOptionalArgumentInParameters); | 116 indexOfLastOptionalArgumentInParameters); |
117 } else if (member.isInstanceMember) { | 117 } else if (member.isInstanceMember) { |
118 if (needsSuperGetter(member)) { | 118 if (needsSuperGetter(member)) { |
119 ClassElement superClass = member.enclosingClass; | 119 ClassElement superClass = member.enclosingClass; |
120 String methodName = namer.getNameOfInstanceMember(member); | 120 String methodName = namer.instanceMethodName(member); |
121 // When redirecting, we must ensure that we don't end up in a subclass. | 121 // When redirecting, we must ensure that we don't end up in a subclass. |
122 // We thus can't just invoke `this.foo$1.call(filledInArguments)`. | 122 // We thus can't just invoke `this.foo$1.call(filledInArguments)`. |
123 // Instead we need to call the statically resolved target. | 123 // Instead we need to call the statically resolved target. |
124 // `<class>.prototype.bar$1.call(this, argument0, ...)`. | 124 // `<class>.prototype.bar$1.call(this, argument0, ...)`. |
125 body = js.statement( | 125 body = js.statement( |
126 'return #.#.call(this, #);', | 126 'return #.#.call(this, #);', |
127 [backend.emitter.prototypeAccess(superClass, | 127 [backend.emitter.prototypeAccess(superClass, |
128 hasBeenInstantiated: true), | 128 hasBeenInstantiated: true), |
129 methodName, | 129 methodName, |
130 argumentsBuffer]); | 130 argumentsBuffer]); |
131 } else { | 131 } else { |
132 body = js.statement( | 132 body = js.statement( |
133 'return this.#(#);', | 133 'return this.#(#);', |
134 [namer.getNameOfInstanceMember(member), argumentsBuffer]); | 134 [namer.instanceMethodName(member), argumentsBuffer]); |
135 } | 135 } |
136 } else { | 136 } else { |
137 body = js.statement('return #(#)', | 137 body = js.statement('return #(#)', |
138 [emitter.staticFunctionAccess(member), argumentsBuffer]); | 138 [emitter.staticFunctionAccess(member), argumentsBuffer]); |
139 } | 139 } |
140 | 140 |
141 jsAst.Fun function = js('function(#) { #; }', [parametersBuffer, body]); | 141 jsAst.Fun function = js('function(#) { #; }', [parametersBuffer, body]); |
142 | 142 |
143 addStub(selector, function); | 143 addStub(selector, function); |
144 } | 144 } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 MemberInfo info = analyzeMemberMethod(member); | 260 MemberInfo info = analyzeMemberMethod(member); |
261 if (info != null) { | 261 if (info != null) { |
262 addMemberMethodFromInfo(info, builder); | 262 addMemberMethodFromInfo(info, builder); |
263 } | 263 } |
264 } | 264 } |
265 | 265 |
266 MemberInfo analyzeMemberMethod(FunctionElement member) { | 266 MemberInfo analyzeMemberMethod(FunctionElement member) { |
267 if (member.isAbstract) return null; | 267 if (member.isAbstract) return null; |
268 jsAst.Expression code = backend.generatedCode[member]; | 268 jsAst.Expression code = backend.generatedCode[member]; |
269 if (code == null) return null; | 269 if (code == null) return null; |
270 String name = namer.getNameOfMember(member); | 270 String name = namer.methodPropertyName(member); |
271 | 271 |
272 FunctionSignature parameters = member.functionSignature; | 272 FunctionSignature parameters = member.functionSignature; |
273 bool needsStubs = !parameters.optionalParameters.isEmpty; | 273 bool needsStubs = !parameters.optionalParameters.isEmpty; |
274 bool canTearOff = false; | 274 bool canTearOff = false; |
275 bool isClosure = false; | 275 bool isClosure = false; |
276 bool isNotApplyTarget = !member.isFunction || | 276 bool isNotApplyTarget = !member.isFunction || |
277 member.isConstructor || | 277 member.isConstructor || |
278 member.isAccessor; | 278 member.isAccessor; |
279 String tearOffName; | 279 String tearOffName; |
280 | 280 |
281 | 281 |
282 final bool canBeReflected = backend.isAccessibleByReflection(member) || | 282 final bool canBeReflected = backend.isAccessibleByReflection(member) || |
283 // During incremental compilation, we have to assume that reflection | 283 // During incremental compilation, we have to assume that reflection |
284 // *might* get enabled. | 284 // *might* get enabled. |
285 compiler.hasIncrementalSupport; | 285 compiler.hasIncrementalSupport; |
286 | 286 |
287 if (isNotApplyTarget) { | 287 if (isNotApplyTarget) { |
288 canTearOff = false; | 288 canTearOff = false; |
289 } else if (member.isInstanceMember) { | 289 } else if (member.isInstanceMember) { |
290 if (member.enclosingClass.isClosure) { | 290 if (member.enclosingClass.isClosure) { |
291 canTearOff = false; | 291 canTearOff = false; |
292 isClosure = true; | 292 isClosure = true; |
293 } else { | 293 } else { |
294 // Careful with operators. | 294 // Careful with operators. |
295 canTearOff = | 295 canTearOff = |
296 compiler.codegenWorld.hasInvokedGetter(member, compiler.world) || | 296 compiler.codegenWorld.hasInvokedGetter(member, compiler.world) || |
297 (canBeReflected && !member.isOperator); | 297 (canBeReflected && !member.isOperator); |
298 assert(!needsSuperGetter(member) || canTearOff); | 298 assert(!needsSuperGetter(member) || canTearOff); |
299 tearOffName = namer.getterName(member); | 299 tearOffName = namer.getterForElement(member); |
300 } | 300 } |
301 } else { | 301 } else { |
302 canTearOff = | 302 canTearOff = |
303 compiler.codegenWorld.staticFunctionsNeedingGetter.contains(member) || | 303 compiler.codegenWorld.staticFunctionsNeedingGetter.contains(member) || |
304 canBeReflected; | 304 canBeReflected; |
305 tearOffName = namer.getStaticClosureName(member); | 305 tearOffName = namer.staticClosureName(member); |
306 } | 306 } |
307 final bool canBeApplied = compiler.enabledFunctionApply && | 307 final bool canBeApplied = compiler.enabledFunctionApply && |
308 compiler.world.getMightBePassedToApply(member); | 308 compiler.world.getMightBePassedToApply(member); |
309 | 309 |
310 final bool hasSuperAlias = backend.isAliasedSuperMember(member); | 310 final bool hasSuperAlias = backend.isAliasedSuperMember(member); |
311 | 311 |
312 final bool needStructuredInfo = | 312 final bool needStructuredInfo = |
313 canTearOff || canBeReflected || canBeApplied || hasSuperAlias; | 313 canTearOff || canBeReflected || canBeApplied || hasSuperAlias; |
314 | 314 |
315 | 315 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 // P+1. First metadata (if reflectable). | 393 // P+1. First metadata (if reflectable). |
394 // ... | 394 // ... |
395 // TODO(ahe): Consider one of the parameter counts can be replaced by the | 395 // TODO(ahe): Consider one of the parameter counts can be replaced by the |
396 // length property of the JavaScript function object. | 396 // length property of the JavaScript function object. |
397 | 397 |
398 List<jsAst.Expression> expressions = <jsAst.Expression>[]; | 398 List<jsAst.Expression> expressions = <jsAst.Expression>[]; |
399 | 399 |
400 // Create the optional aliasing entry if this method is called via super. | 400 // Create the optional aliasing entry if this method is called via super. |
401 if (backend.isAliasedSuperMember(member)) { | 401 if (backend.isAliasedSuperMember(member)) { |
402 expressions.add(new jsAst.LiteralString( | 402 expressions.add(new jsAst.LiteralString( |
403 '"${namer.getNameOfAliasedSuperMember(member)}"')); | 403 '"${namer.aliasedSuperMemberPropertyName(member)}"')); |
404 } | 404 } |
405 | 405 |
406 expressions.add(code); | 406 expressions.add(code); |
407 | 407 |
408 final bool onlyNeedsSuperAlias = | 408 final bool onlyNeedsSuperAlias = |
409 !(canTearOff || canBeReflected || canBeApplied || needsStubs); | 409 !(canTearOff || canBeReflected || canBeApplied || needsStubs); |
410 | 410 |
411 if (onlyNeedsSuperAlias) { | 411 if (onlyNeedsSuperAlias) { |
412 jsAst.ArrayInitializer arrayInit = | 412 jsAst.ArrayInitializer arrayInit = |
413 new jsAst.ArrayInitializer(expressions); | 413 new jsAst.ArrayInitializer(expressions); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 assert(needsStubs != null); | 581 assert(needsStubs != null); |
582 assert(canTearOff != null); | 582 assert(canTearOff != null); |
583 assert(isClosure != null); | 583 assert(isClosure != null); |
584 assert(tearOffName != null || !canTearOff); | 584 assert(tearOffName != null || !canTearOff); |
585 assert(canBeReflected != null); | 585 assert(canBeReflected != null); |
586 assert(canBeApplied != null); | 586 assert(canBeApplied != null); |
587 assert(hasSuperAlias != null); | 587 assert(hasSuperAlias != null); |
588 assert(needStructuredInfo != null); | 588 assert(needStructuredInfo != null); |
589 } | 589 } |
590 } | 590 } |
OLD | NEW |