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

Side by Side Diff: lib/compiler/implementation/js_backend/native_emitter.dart

Issue 11188004: Add a content-security-policy (CSP) flag. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor comment changes and rebase. Created 8 years, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class NativeEmitter { 5 class NativeEmitter {
6 6
7 CodeEmitterTask emitter; 7 CodeEmitterTask emitter;
8 CodeBuffer nativeBuffer; 8 CodeBuffer nativeBuffer;
9 9
10 // Classes that participate in dynamic dispatch. These are the 10 // Classes that participate in dynamic dispatch. These are the
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 String get hashCodeHelperName { 87 String get hashCodeHelperName {
88 Element element = compiler.findHelper( 88 Element element = compiler.findHelper(
89 const SourceString('hashCodeForNativeObject')); 89 const SourceString('hashCodeForNativeObject'));
90 return backend.namer.isolateAccess(element); 90 return backend.namer.isolateAccess(element);
91 } 91 }
92 92
93 String get defineNativeClassName 93 String get defineNativeClassName
94 => '${backend.namer.CURRENT_ISOLATE}.\$defineNativeClass'; 94 => '${backend.namer.CURRENT_ISOLATE}.\$defineNativeClass';
95 95
96 String get defineNativeClassFunction { 96 String get defineNativeClassFunction {
97 if (emitter.useContentSecurityPolicy) {
98 return """
99 function(cls, methods) {
100 for (var method in methods) {
101 $dynamicName(method)[cls] = methods[method];
102 }
103 }""";
104 }
97 return """ 105 return """
98 function(cls, fields, methods) { 106 function(cls, fields, methods) {
99 var generateGetterSetter = ${emitter.generateGetterSetterFunction}; 107 var generateGetterSetter = ${emitter.generateGetterSetterFunction};
100 for (var i = 0; i < fields.length; i++) { 108 for (var i = 0; i < fields.length; i++) {
101 generateGetterSetter(fields[i], methods); 109 generateGetterSetter(fields[i], methods);
102 } 110 }
103 for (var method in methods) { 111 for (var method in methods) {
104 $dynamicName(method)[cls] = methods[method]; 112 $dynamicName(method)[cls] = methods[method];
105 } 113 }
106 }"""; 114 }""";
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 assert(classElement.backendMembers.isEmpty()); 158 assert(classElement.backendMembers.isEmpty());
151 String quotedName = classElement.nativeName.slowToString(); 159 String quotedName = classElement.nativeName.slowToString();
152 if (isNativeLiteral(quotedName)) { 160 if (isNativeLiteral(quotedName)) {
153 generateNativeLiteral(classElement); 161 generateNativeLiteral(classElement);
154 // The native literal kind needs to be dealt with specially when 162 // The native literal kind needs to be dealt with specially when
155 // generating code for it. 163 // generating code for it.
156 return; 164 return;
157 } 165 }
158 166
159 CodeBuffer fieldBuffer = new CodeBuffer(); 167 CodeBuffer fieldBuffer = new CodeBuffer();
160 List<String> checkedSetters = 168 List<String> explicitGettersSetters;
161 emitter.emitClassFields(classElement, fieldBuffer); 169
170 if (emitter.useContentSecurityPolicy) {
171 explicitGettersSetters =
172 emitter.emitClassConstructorGettersSetters(classElement, fieldBuffer);
173 // We don't need the constructor for native classes.
174 fieldBuffer.clear();
175 } else {
176 explicitGettersSetters =
177 emitter.emitClassFields(classElement, fieldBuffer);
178 fieldBuffer.add(", ");
179 }
162 180
163 CodeBuffer methodBuffer = new CodeBuffer(); 181 CodeBuffer methodBuffer = new CodeBuffer();
164 emitter.emitInstanceMembers(classElement, methodBuffer, false); 182 emitter.emitInstanceMembers(classElement, methodBuffer, false);
165 183
166 if (methodBuffer.isEmpty() && fieldBuffer.isEmpty()) return; 184 if (methodBuffer.isEmpty()
185 && fieldBuffer.isEmpty()
186 && explicitGettersSetters.isEmpty()) return;
167 187
168 String nativeName = toNativeName(classElement); 188 String nativeName = toNativeName(classElement);
169 nativeBuffer.add("$defineNativeClassName('$nativeName', ["); 189 nativeBuffer.add("$defineNativeClassName('$nativeName', ");
170 nativeBuffer.add(fieldBuffer); 190 nativeBuffer.add(fieldBuffer);
171 nativeBuffer.add('], {'); 191 nativeBuffer.add('{');
172 if (!checkedSetters.isEmpty()) { 192 if (!explicitGettersSetters.isEmpty()) {
173 nativeBuffer.add('${Strings.join(checkedSetters, ",\n")}'); 193 nativeBuffer.add('${Strings.join(explicitGettersSetters, ",\n")}');
174 nativeBuffer.add(',\n'); 194 nativeBuffer.add(',\n');
175 } 195 }
176 nativeBuffer.add(methodBuffer); 196 nativeBuffer.add(methodBuffer);
177 nativeBuffer.add('\n});\n\n'); 197 nativeBuffer.add('\n});\n\n');
178 198
179 classesWithDynamicDispatch.add(classElement); 199 classesWithDynamicDispatch.add(classElement);
180 } 200 }
181 201
182 List<ClassElement> getDirectSubclasses(ClassElement cls) { 202 List<ClassElement> getDirectSubclasses(ClassElement cls) {
183 List<ClassElement> result = directSubtypes[cls]; 203 List<ClassElement> result = directSubtypes[cls];
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 if (!first) targetBuffer.add(",\n"); 471 if (!first) targetBuffer.add(",\n");
452 targetBuffer.add(" $name: $function"); 472 targetBuffer.add(" $name: $function");
453 first = false; 473 first = false;
454 }); 474 });
455 targetBuffer.add("\n});\n\n"); 475 targetBuffer.add("\n});\n\n");
456 } 476 }
457 targetBuffer.add('$nativeBuffer'); 477 targetBuffer.add('$nativeBuffer');
458 targetBuffer.add('\n'); 478 targetBuffer.add('\n');
459 } 479 }
460 } 480 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698