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

Side by Side Diff: dart/frog/leg/native_handler.dart

Issue 9568008: Do not allow native syntax on all libraries. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 8 years, 9 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
« no previous file with comments | « dart/frog/leg/compiler.dart ('k') | dart/frog/leg/scanner/class_element_parser.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #library('native'); 5 #library('native');
6 #import('../../lib/uri/uri.dart'); 6 #import('../../lib/uri/uri.dart');
7 #import('leg.dart'); 7 #import('leg.dart');
8 #import('elements/elements.dart'); 8 #import('elements/elements.dart');
9 #import('scanner/scannerlib.dart'); 9 #import('scanner/scannerlib.dart');
10 #import('ssa/ssa.dart'); 10 #import('ssa/ssa.dart');
11 #import('tree/tree.dart'); 11 #import('tree/tree.dart');
12 #import('util/util.dart'); 12 #import('util/util.dart');
13 13
14 void processNativeClasses(Compiler compiler, 14 class NativeHandler extends CompilerTask {
15 CompilationUnitElement compilationUnit) { 15 NativeHandler(Compiler compiler)
16 for (Link<Element> link = compilationUnit.topLevelElements; 16 : allowedLibraries = new Set<LibraryElement>(),
17 !link.isEmpty(); link = link.tail) { 17 super(compiler);
18 Element element = link.head; 18
19 if (element.kind == ElementKind.CLASS) { 19 Set<LibraryElement> allowedLibraries;
20 ClassElement classElement = element; 20
21 if (classElement.isNative()) { 21 void processNativeClasses(CompilationUnitElement compilationUnit) {
22 compiler.registerInstantiatedClass(classElement); 22 return measure(() {
23 // Also parse the node to know all its methods because 23 for (Link<Element> link = compilationUnit.topLevelElements;
24 // otherwise it will only be parsed if there is a call to 24 !link.isEmpty(); link = link.tail) {
25 // one of its constructor. 25 Element element = link.head;
26 element.parseNode(compiler); 26 if (element.kind == ElementKind.CLASS) {
27 // Resolve to setup the inheritance. 27 ClassElement classElement = element;
28 element.resolve(compiler); 28 if (classElement.isNative()) {
29 compiler.registerInstantiatedClass(classElement);
30 // Also parse the node to know all its methods because
31 // otherwise it will only be parsed if there is a call to
32 // one of its constructor.
33 element.parseNode(compiler);
34 // Resolve to setup the inheritance.
35 element.resolve(compiler);
36 }
37 }
29 } 38 }
39 });
40 }
41
42 void checkNativeSupport(LibraryElement library, Uri uri) {
43 String libraryName = uri.toString();
44 if (library.script.name.contains('dart/frog/tests/native/src')
45 || libraryName == 'dart:dom'
46 || libraryName == 'dart:html') {
47 allowedLibraries.add(library);
48 library.define(new ForeignElement(
49 const SourceString('native'), library), compiler);
30 } 50 }
31 } 51 }
32 } 52 }
33 53
34 void checkNativeSupport(Compiler compiler, 54
35 LibraryElement library, 55 void checkAllowedLibrary(ElementListener listener, Token token) {
ahe 2012/03/06 08:12:44 Extracting the compiler from the ElementListener i
ngeoffray 2012/03/06 12:48:49 Unfortunately, this does not work that well. See l
ahe 2012/03/06 13:08:30 It will work fine. We can do a VC and I can explai
36 Uri uri) { 56 Compiler compiler = listener.listener;
37 String libraryName = uri.toString(); 57 LibraryElement currentLibrary = listener.compilationUnitElement.getLibrary();
38 if (library.script.name.contains('dart/frog/tests/native/src') 58 if (!compiler.nativeHandler.allowedLibraries.contains(currentLibrary)) {
39 || libraryName == 'dart:dom' 59 listener.unexpected(token);
40 || libraryName == 'dart:html') {
41 library.define(new ForeignElement(
42 const SourceString('native'), library), compiler);
43 } 60 }
44 } 61 }
45 62
46 Token handleNativeBlockToSkip(Listener listener, Token token) { 63 Token handleNativeBlockToSkip(ElementListener listener, Token token) {
47 token = token.next; 64 checkAllowedLibrary(listener, token);
65 token = token.next;
48 if (token.kind === STRING_TOKEN) { 66 if (token.kind === STRING_TOKEN) {
49 token = token.next; 67 token = token.next;
50 } 68 }
51 if (token.stringValue === '{') { 69 if (token.stringValue === '{') {
52 BeginGroupToken beginGroupToken = token; 70 BeginGroupToken beginGroupToken = token;
53 token = beginGroupToken.endGroup; 71 token = beginGroupToken.endGroup;
54 } 72 }
55 return token; 73 return token;
56 } 74 }
57 75
58 Token handleNativeClassBodyToSkip(Listener listener, Token token) { 76 Token handleNativeClassBodyToSkip(Listener listener, Token token) {
77 checkAllowedLibrary(listener, token);
59 listener.handleIdentifier(token); 78 listener.handleIdentifier(token);
60 token = token.next; 79 token = token.next;
61 if (token.kind !== STRING_TOKEN) { 80 if (token.kind !== STRING_TOKEN) {
62 return listener.unexpected(token); 81 return listener.unexpected(token);
63 } 82 }
64 token = token.next; 83 token = token.next;
65 if (token.stringValue !== '{') { 84 if (token.stringValue !== '{') {
66 return listener.unexpected(token); 85 return listener.unexpected(token);
67 } 86 }
68 BeginGroupToken beginGroupToken = token; 87 BeginGroupToken beginGroupToken = token;
69 token = beginGroupToken.endGroup; 88 token = beginGroupToken.endGroup;
70 return token; 89 return token;
71 } 90 }
72 91
73 Token handleNativeClassBody(Listener listener, Token token) { 92 Token handleNativeClassBody(Listener listener, Token token) {
93 checkAllowedLibrary(listener, token);
74 token = token.next; 94 token = token.next;
75 if (token.kind !== STRING_TOKEN) { 95 if (token.kind !== STRING_TOKEN) {
76 listener.unexpected(token); 96 listener.unexpected(token);
77 } else { 97 } else {
78 token = token.next; 98 token = token.next;
79 } 99 }
80 return token; 100 return token;
81 } 101 }
82 102
83 Token handleNativeFunctionBody(ElementListener listener, Token token) { 103 Token handleNativeFunctionBody(ElementListener listener, Token token) {
104 checkAllowedLibrary(listener, token);
84 Token begin = token; 105 Token begin = token;
85 listener.beginExpressionStatement(token); 106 listener.beginExpressionStatement(token);
86 listener.handleIdentifier(token); 107 listener.handleIdentifier(token);
87 token = token.next; 108 token = token.next;
88 if (token.kind === STRING_TOKEN) { 109 if (token.kind === STRING_TOKEN) {
89 listener.beginLiteralString(token); 110 listener.beginLiteralString(token);
90 listener.endLiteralString(0); 111 listener.endLiteralString(0);
91 listener.pushNode(new NodeList.singleton(listener.popNode())); 112 listener.pushNode(new NodeList.singleton(listener.popNode()));
92 listener.endSend(token); 113 listener.endSend(token);
93 token = token.next; 114 token = token.next;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } else if (!node.arguments.tail.isEmpty()) { 236 } else if (!node.arguments.tail.isEmpty()) {
216 builder.compiler.cancel('More than one argument to native'); 237 builder.compiler.cancel('More than one argument to native');
217 } else { 238 } else {
218 LiteralString jsCode = node.arguments.head; 239 LiteralString jsCode = node.arguments.head;
219 int start = jsCode.value.slowToString()[0] === '@' ? 1 : 0; 240 int start = jsCode.value.slowToString()[0] === '@' ? 1 : 0;
220 builder.push(new HForeign(builder.unquote(jsCode, start), 241 builder.push(new HForeign(builder.unquote(jsCode, start),
221 const SourceString('Object'), 242 const SourceString('Object'),
222 <HInstruction>[])); 243 <HInstruction>[]));
223 } 244 }
224 } 245 }
OLDNEW
« no previous file with comments | « dart/frog/leg/compiler.dart ('k') | dart/frog/leg/scanner/class_element_parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698