Index: sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart |
=================================================================== |
--- sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart (revision 15381) |
+++ sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart (working copy) |
@@ -1681,6 +1681,52 @@ |
"""); |
} |
+ /** |
+ * Emit the code for doing a type check on [cls]. [cls] must |
+ * be an interceptor class. |
+ */ |
+ void emitTypeCheck(ClassElement cls, CodeBuffer buffer) { |
ahe
2012/11/27 14:29:45
This is not really emitting a type check, which wo
ngeoffray
2012/11/27 15:50:54
I renamed the method emitInterceptorCheck.
|
+ JavaScriptBackend backend = compiler.backend; |
+ assert(backend.isInterceptorClass(cls)); |
+ String then = 'return ${namer.isolateAccess(cls)}.prototype;'; |
ahe
2012/11/27 14:29:45
Weird variable name. How about interceptorObject?
ngeoffray
2012/11/27 15:50:54
Done.
|
+ if (cls == backend.jsBoolClass) { |
+ buffer.add("if (typeof receiver == 'boolean') $then"); |
ahe
2012/11/27 14:29:45
This pattern really hurts my eyes :-)
How about:
ngeoffray
2012/11/27 15:50:54
Done.
|
+ } else if (cls == backend.jsIntClass) { |
+ buffer.add("if (typeof receiver == 'number' " |
+ "&& Math.floor(receiver) == receiver) $then"); |
+ } else if (cls == backend.jsDoubleClass || cls == backend.jsNumberClass) { |
+ buffer.add("if (typeof receiver == 'number') $then"); |
+ } else if (cls == backend.jsArrayClass) { |
+ buffer.add("if (receiver != null " |
+ "&& receiver.constructor == Array) $then"); |
+ } else if (cls == backend.jsStringClass) { |
+ buffer.add("if (typeof receiver == 'string') $then"); |
+ } else if (cls == backend.jsNullClass) { |
+ buffer.add("if (receiver == null) $then"); |
+ } else if (cls == backend.jsFunctionClass) { |
+ buffer.add("if (typeof receiver == 'function') $then"); |
+ } |
+ } |
+ |
+ /** |
+ * Emit all versions of the [:getInterceptor:] method. |
+ */ |
+ void emitGetInterceptors(CodeBuffer buffer) { |
ahe
2012/11/27 14:29:45
How about "emitGetInterceptorMethods"?
ngeoffray
2012/11/27 15:50:54
Done.
|
+ JavaScriptBackend backend = compiler.backend; |
+ String objectName = namer.isolateAccess(backend.objectInterceptorClass); |
+ backend.specializedGetInterceptors.forEach( |
+ (String key, Collection<ClassElement> classes) { |
+ buffer.add('$isolateProperties.$key = function(receiver) {'); |
+ for (ClassElement cls in classes) { |
+ if (compiler.codegenWorld.instantiatedClasses.contains(cls)) { |
+ buffer.add('\n '); |
+ emitTypeCheck(cls, buffer); |
+ } |
+ } |
+ buffer.add('\n return $objectName.prototype;\n};\n'); |
+ }); |
+ } |
+ |
String assembleProgram() { |
measure(() { |
mainBuffer.add(HOOKS_API_USAGE); |
@@ -1705,6 +1751,7 @@ |
// Static field initializations require the classes and compile-time |
// constants to be set up. |
emitStaticNonFinalFieldInitializations(mainBuffer); |
+ emitGetInterceptors(mainBuffer); |
emitLazilyInitializedStaticFields(mainBuffer); |
isolateProperties = isolatePropertiesName; |