Chromium Code Reviews| Index: dart/frog/leg/native_handler.dart |
| =================================================================== |
| --- dart/frog/leg/native_handler.dart (revision 4858) |
| +++ dart/frog/leg/native_handler.dart (working copy) |
| @@ -11,40 +11,58 @@ |
| #import('tree/tree.dart'); |
| #import('util/util.dart'); |
| -void processNativeClasses(Compiler compiler, |
| - CompilationUnitElement compilationUnit) { |
| - for (Link<Element> link = compilationUnit.topLevelElements; |
| - !link.isEmpty(); link = link.tail) { |
| - Element element = link.head; |
| - if (element.kind == ElementKind.CLASS) { |
| - ClassElement classElement = element; |
| - if (classElement.isNative()) { |
| - compiler.registerInstantiatedClass(classElement); |
| - // Also parse the node to know all its methods because |
| - // otherwise it will only be parsed if there is a call to |
| - // one of its constructor. |
| - element.parseNode(compiler); |
| - // Resolve to setup the inheritance. |
| - element.resolve(compiler); |
| +class NativeHandler extends CompilerTask { |
| + NativeHandler(Compiler compiler) |
| + : allowedLibraries = new Set<LibraryElement>(), |
| + super(compiler); |
| + |
| + Set<LibraryElement> allowedLibraries; |
| + |
| + void processNativeClasses(CompilationUnitElement compilationUnit) { |
| + return measure(() { |
| + for (Link<Element> link = compilationUnit.topLevelElements; |
| + !link.isEmpty(); link = link.tail) { |
| + Element element = link.head; |
| + if (element.kind == ElementKind.CLASS) { |
| + ClassElement classElement = element; |
| + if (classElement.isNative()) { |
| + compiler.registerInstantiatedClass(classElement); |
| + // Also parse the node to know all its methods because |
| + // otherwise it will only be parsed if there is a call to |
| + // one of its constructor. |
| + element.parseNode(compiler); |
| + // Resolve to setup the inheritance. |
| + element.resolve(compiler); |
| + } |
| + } |
| } |
| + }); |
| + } |
| + |
| + void checkNativeSupport(LibraryElement library, Uri uri) { |
| + String libraryName = uri.toString(); |
| + if (library.script.name.contains('dart/frog/tests/native/src') |
| + || libraryName == 'dart:dom' |
| + || libraryName == 'dart:html') { |
| + allowedLibraries.add(library); |
| + library.define(new ForeignElement( |
| + const SourceString('native'), library), compiler); |
| } |
| } |
| } |
| -void checkNativeSupport(Compiler compiler, |
| - LibraryElement library, |
| - Uri uri) { |
| - String libraryName = uri.toString(); |
| - if (library.script.name.contains('dart/frog/tests/native/src') |
| - || libraryName == 'dart:dom' |
| - || libraryName == 'dart:html') { |
| - library.define(new ForeignElement( |
| - const SourceString('native'), library), compiler); |
| + |
| +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
|
| + Compiler compiler = listener.listener; |
| + LibraryElement currentLibrary = listener.compilationUnitElement.getLibrary(); |
| + if (!compiler.nativeHandler.allowedLibraries.contains(currentLibrary)) { |
| + listener.unexpected(token); |
| } |
| } |
| -Token handleNativeBlockToSkip(Listener listener, Token token) { |
| - token = token.next; |
| +Token handleNativeBlockToSkip(ElementListener listener, Token token) { |
| + checkAllowedLibrary(listener, token); |
| + token = token.next; |
| if (token.kind === STRING_TOKEN) { |
| token = token.next; |
| } |
| @@ -56,6 +74,7 @@ |
| } |
| Token handleNativeClassBodyToSkip(Listener listener, Token token) { |
| + checkAllowedLibrary(listener, token); |
| listener.handleIdentifier(token); |
| token = token.next; |
| if (token.kind !== STRING_TOKEN) { |
| @@ -71,6 +90,7 @@ |
| } |
| Token handleNativeClassBody(Listener listener, Token token) { |
| + checkAllowedLibrary(listener, token); |
| token = token.next; |
| if (token.kind !== STRING_TOKEN) { |
| listener.unexpected(token); |
| @@ -81,6 +101,7 @@ |
| } |
| Token handleNativeFunctionBody(ElementListener listener, Token token) { |
| + checkAllowedLibrary(listener, token); |
| Token begin = token; |
| listener.beginExpressionStatement(token); |
| listener.handleIdentifier(token); |