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

Unified Diff: pkg/compiler/lib/src/patch_parser.dart

Issue 1318043005: Support user generated custom native JS classes. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ptal Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/patch_parser.dart
diff --git a/pkg/compiler/lib/src/patch_parser.dart b/pkg/compiler/lib/src/patch_parser.dart
index 0f3008475c1095209ef5cf8d8f547788361d14dc..fb868ba09116c26844042b84fec5c62aa5811a72 100644
--- a/pkg/compiler/lib/src/patch_parser.dart
+++ b/pkg/compiler/lib/src/patch_parser.dart
@@ -133,6 +133,8 @@ import 'elements/modelx.dart' show
LibraryElementX,
MetadataAnnotationX,
SetterElementX;
+import 'js_backend/js_backend.dart' show
+ JavaScriptBackend;
import 'library_loader.dart' show
LibraryLoader;
import 'parser/listener.dart' show
@@ -304,6 +306,7 @@ void patchElement(Compiler compiler,
patch, MessageKind.PATCH_NON_EXISTING, {'name': patch.name});
return;
}
+
if (!(origin.isClass ||
origin.isConstructor ||
origin.isFunction ||
@@ -359,6 +362,12 @@ checkNativeAnnotation(Compiler compiler, ClassElement cls) {
const NativeAnnotationHandler());
}
+checkJsInteropAnnotation(Compiler compiler, element) {
+ EagerAnnotationHandler.checkAnnotation(compiler, element,
+ const JsInteropAnnotationHandler());
+}
+
+
/// Abstract interface for pre-resolution detection of metadata.
///
/// The detection is handled in two steps:
@@ -394,12 +403,19 @@ abstract class EagerAnnotationHandler<T> {
if (result != null) {
// TODO(johnniwinther): Perform this check in
// [Compiler.onLibrariesLoaded].
- compiler.enqueuer.resolution.addDeferredAction(element, () {
+ // TODO(jacobr): annotation resolution needs to be refactored.
+ actionCallback() {
annotation.ensureResolved(compiler);
handler.validate(
compiler, element, annotation,
compiler.constants.getConstantValue(annotation.constant));
- });
+ }
+ if (compiler.enqueuer.resolution.queueIsClosed) {
+ actionCallback();
+ } else {
+ compiler.enqueuer.resolution.addDeferredAction(element,
+ actionCallback);
+ }
return result;
}
}
@@ -449,6 +465,57 @@ class NativeAnnotationHandler implements EagerAnnotationHandler<String> {
}
}
+/// Annotation handler for pre-resolution detection of `@Js(...)`
+/// annotations.
+class JsInteropAnnotationHandler implements EagerAnnotationHandler<String> {
+ const JsInteropAnnotationHandler();
+
+ // TODO(jacobr): this method is pure technical debt.
+ String dietResolveStringToken(StringToken token) {
+ String value = token.value;
+ if ((value.startsWith('"') && value.endsWith('"')) ||
+ value.startsWith("'") && value.endsWith("'")) {
+ return value.substring(1, value.length - 1);
+ } else {
+ throw 'Invalid @Js(...) annotation. Unexpected name: $value';
+ }
+ }
+
+ String getJsNameAnnotation(MetadataAnnotation annotation) {
+ if (annotation.beginToken != null &&
+ annotation.beginToken.next.value == 'Js') {
+ // Skipping '@', 'Js', and '('.
+ Token argument = annotation.beginToken.next.next.next;
+ return (argument is StringToken) ? dietResolveStringToken(argument) : '';
+ }
+
+ return null;
+ }
+
+ String apply(Compiler compiler,
+ Element element,
+ MetadataAnnotation annotation) {
+ String jsName = getJsNameAnnotation(annotation);
+ if (jsName != null) {
+ element.setJsInterop(jsName);
+ element.declaration.setJsInterop(jsName);
+ }
+ return jsName;
+ }
+
+ @override
+ void validate(Compiler compiler,
+ Element element,
+ MetadataAnnotation annotation,
+ ConstantValue constant) {
+ JavaScriptBackend backend = compiler.backend;
+ if (constant.getType(compiler.coreTypes).element !=
+ backend.jsAnnotationClass) {
+ compiler.internalError(annotation, 'Invalid @Js(...) annotation.');
+ }
+ }
+}
+
/// Annotation handler for pre-resolution detection of `@patch` annotations.
class PatchAnnotationHandler implements EagerAnnotationHandler<PatchVersion> {
const PatchAnnotationHandler();

Powered by Google App Engine
This is Rietveld 408576698