Skip to content

Commit d0cec4a

Browse files
committed
Switch ddc to use @js instead of @JsName. Also update dart:js to support new public methods added for 1.13
BUG= [email protected] Review URL: https://codereview.chromium.org/1448993002 .
1 parent d8b5015 commit d0cec4a

File tree

13 files changed

+130
-75
lines changed

13 files changed

+130
-75
lines changed

pkg/dev_compiler/lib/runtime/dart/_js_helper.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ dart_library.library('dart/_js_helper', null, /* Imports */[
3030
dart.setSignature(Native, {
3131
constructors: () => ({Native: [Native, [core.String]]})
3232
});
33-
class JsName extends core.Object {
34-
JsName(opts) {
35-
let name = opts && 'name' in opts ? opts.name : null;
36-
this.name = name;
37-
}
38-
}
39-
dart.setSignature(JsName, {
40-
constructors: () => ({JsName: [JsName, [], {name: core.String}]})
41-
});
4233
class JsPeerInterface extends core.Object {
4334
JsPeerInterface(opts) {
4435
let name = opts && 'name' in opts ? opts.name : null;
@@ -1189,7 +1180,6 @@ dart_library.library('dart/_js_helper', null, /* Imports */[
11891180
exports.NoThrows = NoThrows;
11901181
exports.NoInline = NoInline;
11911182
exports.Native = Native;
1192-
exports.JsName = JsName;
11931183
exports.JsPeerInterface = JsPeerInterface;
11941184
exports.SupportJsExtensionMethods = SupportJsExtensionMethods;
11951185
exports.defineProperty = defineProperty;

pkg/dev_compiler/lib/runtime/dart/js.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,31 @@ dart_library.library('dart/js', null, /* Imports */[
390390
return value;
391391
}
392392
dart.fn(_putIfAbsent, core.Object, [dart.dynamic, dart.dynamic, dart.functionType(dart.dynamic, [dart.dynamic])]);
393+
function allowInterop(f) {
394+
return f;
395+
}
396+
dart.fn(allowInterop, core.Function, [core.Function]);
397+
dart.defineLazyProperties(exports, {
398+
get _interopCaptureThisExpando() {
399+
return new (core.Expando$(core.Function))();
400+
},
401+
set _interopCaptureThisExpando(_) {}
402+
});
403+
function allowInteropCaptureThis(f) {
404+
let ret = exports._interopCaptureThisExpando.get(f);
405+
if (ret == null) {
406+
ret = dart.as(function() {
407+
let args = [this];
408+
for (let arg of arguments) {
409+
args.push(arg);
410+
}
411+
return f(...args);
412+
}, core.Function);
413+
exports._interopCaptureThisExpando.set(f, ret);
414+
}
415+
return ret;
416+
}
417+
dart.fn(allowInteropCaptureThis, core.Function, [core.Function]);
393418
let __CastType0$ = dart.generic(function(E) {
394419
let __CastType0 = dart.typedef('__CastType0', () => dart.functionType(dart.dynamic, [E]));
395420
return __CastType0;
@@ -400,4 +425,6 @@ dart_library.library('dart/js', null, /* Imports */[
400425
exports.JsFunction = JsFunction;
401426
exports.JsArray$ = JsArray$;
402427
exports.JsArray = JsArray;
428+
exports.allowInterop = allowInterop;
429+
exports.allowInteropCaptureThis = allowInteropCaptureThis;
403430
});

pkg/dev_compiler/lib/src/codegen/js_codegen.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
248248
void visitLibraryDirective(LibraryDirective node) {
249249
assert(_jsModuleValue == null);
250250

251-
var jsName = findAnnotation(node.element, isJsNameAnnotation);
251+
var jsName = findAnnotation(node.element, isJSAnnotation);
252252
_jsModuleValue =
253253
getConstantField(jsName, 'name', types.stringType)?.toStringValue();
254254
}
@@ -409,7 +409,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
409409
if (jsTypeName != null && jsTypeName != dartClassName) {
410410
// We export the JS type as if it was a Dart type. For example this allows
411411
// `dom.InputElement` to actually be HTMLInputElement.
412-
// TODO(jmesserly): if we had the JsName on the Element, we could just
412+
// TODO(jmesserly): if we had the JS name on the Element, we could just
413413
// generate it correctly when we refer to it.
414414
if (isPublic(dartClassName)) _addExport(dartClassName);
415415
return js.statement('let # = #;', [dartClassName, jsTypeName]);
@@ -421,7 +421,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
421421
JS.Statement visitClassDeclaration(ClassDeclaration node) {
422422
var classElem = node.element;
423423
var type = classElem.type;
424-
var jsName = findAnnotation(classElem, isJsNameAnnotation);
424+
var jsName = findAnnotation(classElem, isJSAnnotation);
425425

426426
if (jsName != null) return _emitJsType(node.name.name, jsName);
427427

@@ -2721,7 +2721,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
27212721
// TODO(jmesserly): ideally we'd check the method and see if it is marked
27222722
// `external`, but that doesn't work because it isn't in the element model.
27232723
bool _useNativeJsIndexer(DartType type) =>
2724-
findAnnotation(type.element, isJsNameAnnotation) != null;
2724+
findAnnotation(type.element, isJSAnnotation) != null;
27252725

27262726
/// Gets the target of a [PropertyAccess], [IndexExpression], or
27272727
/// [MethodInvocation]. These three nodes can appear in a [CascadeExpression].

pkg/dev_compiler/lib/src/codegen/js_interop.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ bool isJsRestAnnotation(DartObjectImpl value) =>
2424
bool isJsSpreadInvocation(MethodInvocation i) =>
2525
_isJsLibType('spread', i.methodName?.bestElement);
2626

27-
// TODO(jmesserly): Move JsName, JsPeerInterface to package:js (see issue #135).
28-
bool isJsNameAnnotation(DartObjectImpl value) => value.type.name == 'JsName';
27+
// TODO(jmesserly): Move JsPeerInterface to package:js (see issue #135).
28+
bool isJSAnnotation(DartObjectImpl value) => value.type.name == 'JS';
2929

3030
bool isJsPeerInterface(DartObjectImpl value) =>
3131
value.type.name == 'JsPeerInterface';

pkg/dev_compiler/lib/src/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ String resourceOutputPath(Uri resourceUri, Uri entryUri, String runtimeDir) {
330330
///
331331
/// For example if we had the ClassDeclaration node for `FontElement`:
332332
///
333-
/// @JsName('HTMLFontElement')
333+
/// @js.JS('HTMLFontElement')
334334
/// @deprecated
335335
/// class FontElement { ... }
336336
///

pkg/dev_compiler/test/codegen/expect/dom/dom.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ dart_library.library('dom/dom', window, /* Imports */[
55
], function(exports, dart, core) {
66
'use strict';
77
let dartx = dart.dartx;
8-
class JsName extends core.Object {
9-
JsName(opts) {
10-
let name = opts && 'name' in opts ? opts.name : null;
11-
this.name = name;
12-
}
13-
}
14-
dart.setSignature(JsName, {
15-
constructors: () => ({JsName: [JsName, [], {name: core.String}]})
16-
});
178
class Overload extends core.Object {
189
Overload() {
1910
}
@@ -50,7 +41,6 @@ dart_library.library('dom/dom', window, /* Imports */[
5041
}
5142
class CanvasPathMethods extends core.Object {}
5243
// Exports:
53-
exports.JsName = JsName;
5444
exports.Overload = Overload;
5545
exports.overload = overload;
5646
exports.CustomEvent = CustomEvent;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
dart_library.library('js/js', null, /* Imports */[
2+
"dart/_runtime",
3+
'dart/js',
4+
'dart/core'
5+
], /* Lazy imports */[
6+
], function(exports, dart, js, core) {
7+
'use strict';
8+
let dartx = dart.dartx;
9+
dart.export(exports, js, ['allowInterop', 'allowInteropCaptureThis'], []);
10+
class JS extends core.Object {
11+
JS(name) {
12+
if (name === void 0)
13+
name = null;
14+
this.name = name;
15+
}
16+
}
17+
dart.setSignature(JS, {
18+
constructors: () => ({JS: [JS, [], [core.String]]})
19+
});
20+
class _Anonymous extends core.Object {
21+
_Anonymous() {
22+
}
23+
}
24+
dart.setSignature(_Anonymous, {
25+
constructors: () => ({_Anonymous: [_Anonymous, []]})
26+
});
27+
let anonymous = dart.const(new _Anonymous());
28+
// Exports:
29+
exports.JS = JS;
30+
exports.anonymous = anonymous;
31+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Messages from compiling js.dart

pkg/dev_compiler/test/codegen/expect/sunflower/dom.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ dart_library.library('sunflower/dom', window, /* Imports */[
55
], function(exports, dart, core) {
66
'use strict';
77
let dartx = dart.dartx;
8-
class JsName extends core.Object {
9-
JsName(opts) {
10-
let name = opts && 'name' in opts ? opts.name : null;
11-
this.name = name;
12-
}
13-
}
14-
dart.setSignature(JsName, {
15-
constructors: () => ({JsName: [JsName, [], {name: core.String}]})
16-
});
178
class Overload extends core.Object {
189
Overload() {
1910
}
@@ -50,7 +41,6 @@ dart_library.library('sunflower/dom', window, /* Imports */[
5041
}
5142
class CanvasPathMethods extends core.Object {}
5243
// Exports:
53-
exports.JsName = JsName;
5444
exports.Overload = Overload;
5545
exports.overload = overload;
5646
exports.CustomEvent = CustomEvent;

pkg/dev_compiler/test/codegen/expect/sunflower/sunflower.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ <h1>drfibonacci's Sunflower Spectacular</h1>
5252
<script src="../dev_compiler/runtime/dart/_js_mirrors.js"></script>
5353
<script src="../dev_compiler/runtime/dart/js.js"></script>
5454
<script src="circle.js"></script>
55+
<script src="../js/js.js"></script>
5556
<script src="dom.js"></script>
5657
<script src="painter.js"></script>
5758
<script src="sunflower.js"></script>

pkg/dev_compiler/test/codegen/sunflower/dom.dart

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@JsName(name: 'window')
5+
@js.JS('window')
66
library dom;
77

8-
@JsName()
8+
import 'package:js/js.dart' as js;
9+
10+
@js.JS()
911
class Window {}
1012

11-
class JsName {
12-
/// The JavaScript name.
13-
/// Used for classes and libraries.
14-
/// Note that this could be an expression, e.g. `lib.TypeName` in JS, but it
15-
/// should be kept simple, as it will be generated directly into the code.
16-
final String name;
17-
const JsName({this.name});
18-
}
1913
class Overload {
2014
const Overload();
2115
}
@@ -24,7 +18,7 @@ const overload = const Overload();
2418
external Document get document;
2519
external Window get window;
2620

27-
@JsName()
21+
@js.JS()
2822
abstract class Document extends Node {
2923
Element createElement(String name);
3024
Element querySelector(String selector);
@@ -33,7 +27,7 @@ abstract class Document extends Node {
3327
HTMLElement body;
3428
}
3529

36-
@JsName()
30+
@js.JS()
3731
class Blob {
3832
external Blob(blobParts, {String type});
3933
}
@@ -42,19 +36,19 @@ class CustomEvent {
4236
external CustomEvent(String type, {detail, bubbles, cancelable});
4337
}
4438

45-
@JsName()
39+
@js.JS()
4640
abstract class Element extends Node {
4741
void addEventListener(String type, EventListener callback, [bool capture]);
4842
String textContent;
4943
}
5044

51-
@JsName()
45+
@js.JS()
5246
abstract class HTMLElement extends Element {
5347
String innerHTML;
5448
HTMLCollection get children;
5549
}
5650

57-
@JsName()
51+
@js.JS()
5852
abstract class Node {
5953
bool hasChildNodes();
6054
NodeList get childNodes;
@@ -70,7 +64,7 @@ abstract class HTMLCollection {
7064
external Element operator [](num index);
7165
}
7266

73-
@JsName()
67+
@js.JS()
7468
class NodeList {
7569
external NodeList();
7670
external num get length;
@@ -83,26 +77,26 @@ class NodeList {
8377

8478
typedef void EventListener(Event e);
8579

86-
@JsName()
80+
@js.JS()
8781
abstract class Event {}
8882

8983
// TODO(jmesserly): rename these
90-
@JsName(name: 'HTMLInputElement')
84+
@js.JS('HTMLInputElement')
9185
abstract class InputElement extends HTMLElement {
9286
String value;
9387
}
9488

95-
@JsName(name: 'HTMLCanvasElement')
89+
@js.JS('HTMLCanvasElement')
9690
abstract class CanvasElement extends HTMLElement {
9791
RenderingContext getContext(String contextId);
9892
}
9993

100-
@JsName(name: 'HTMLDivElement')
94+
@js.JS('HTMLDivElement')
10195
abstract class DivElement extends HTMLElement {
10296
RenderingContext getContext(String contextId);
10397
}
10498

105-
@JsName(name: 'HTMLScriptElement')
99+
@js.JS('HTMLScriptElement')
106100
abstract class ScriptElement extends HTMLElement {
107101
String type;
108102
}
@@ -113,7 +107,7 @@ abstract class ScriptElement extends HTMLElement {
113107
abstract class RenderingContext {}
114108

115109
// http://www.w3.org/html/wg/drafts/2dcontext/html5_canvas_CR/
116-
@JsName()
110+
@js.JS()
117111
abstract class CanvasRenderingContext2D
118112
implements CanvasDrawingStyles, CanvasPathMethods, RenderingContext {
119113

@@ -229,23 +223,23 @@ abstract class CanvasPathMethods {
229223
[bool anticlockwise]);
230224
}
231225

232-
@JsName()
226+
@js.JS()
233227
abstract class CanvasGradient {
234228
// opaque object
235229
void addColorStop(num offset, String color);
236230
}
237231

238-
@JsName()
232+
@js.JS()
239233
abstract class CanvasPattern {
240234
// opaque object
241235
}
242236

243-
@JsName()
237+
@js.JS()
244238
abstract class TextMetrics {
245239
num get width;
246240
}
247241

248-
@JsName()
242+
@js.JS()
249243
abstract class ImageData {
250244
int get width;
251245
int get height;

0 commit comments

Comments
 (0)