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

Side by Side Diff: tests/compiler/dart2js_native/static_methods_test.dart

Issue 971053003: Sets the native name of static native methods by using the @JSName with the @Native tag of the encl… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 | « pkg/compiler/lib/src/native/enqueue.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 // Accessing static native methods names:
6 // plain declaration -> use @Native tag as 'scope' for declared name.
7 // identifier @JSName -> use @Native tag as 'scope' for @JSName.
8 // other @JSName -> use @JSName as an expression.
9
10 import "package:expect/expect.dart";
11 import 'dart:_js_helper' show Native, JSName, convertDartClosureToJS;
12
13
14 typedef int Callback(String s);
15
16 @Native("CC") // Tag can be different to class name.
17 class AA {
18 // This name is not an identifier, so completely defines how to access method.
19 @JSName('CC.foo')
20 static int foo(String s) native;
21
22 // This name is not an identifier, so completely defines how to access method.
23 @JSName('CC.bar')
24 static int bar(Callback c) native;
25 static int baz(Callback c) { return bar(c); }
26
27 // Compiler should automatically use the tag and the declared name, i.e. call
28 // `CC.lepton`.
29 static int lepton(Callback c) native;
30 static int electron(c) => lepton(c);
31
32 // Compiler should automatically use the tag and JSName, i.e. call
33 // `CC.baryon`.
34 @JSName('baryon')
35 static int _baryon(Callback c) native;
36 static int proton(c) => _baryon(c);
37 }
38
39 void setup() native r"""
40 // This code is all inside 'setup' and so not accessible from the global scope.
41
42 function CC(){}
43
44 CC.foo = function(s) { return s.length; }
45 CC.bar = function(f) { return f("Bye"); }
46 CC.lepton = function(f) { return f("Lepton"); }
47 CC.baryon = function(f) { return f("three quarks"); }
48
49 self.CC = CC;
50 """;
51
52 main() {
53 setup();
54
55 // TODO(sra): Investigate why this line is necessary to get a correctly
56 // compiled convertDartClosureToJS. Without this line, the compiler crashes.
57 convertDartClosureToJS(main, 1);
58
59 Expect.equals(5, AA.foo("Hello"));
60
61 Expect.equals(3, AA.bar((s) => s.length));
62 Expect.equals(3, AA.baz((s) => s.length));
63
64 Expect.equals(6, AA.lepton((s) => s.length));
65 Expect.equals(6, AA.electron((s) => s.length));
66
67 Expect.equals(12, AA._baryon((s) => s.length));
68 Expect.equals(12, AA.proton((s) => s.length));
69 Expect.throws(() => AA.baryon((s) => s.length)); // Not defined on AA.
70 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/native/enqueue.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698