OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Accessing static native methods names: | 5 // Accessing static native methods names: |
6 // plain declaration -> use @Native tag as 'scope' for declared name. | 6 // plain declaration -> use @Native tag as 'scope' for declared name. |
7 // identifier @JSName -> use @Native tag as 'scope' for @JSName. | 7 // identifier @JSName -> use @Native tag as 'scope' for @JSName. |
8 // other @JSName -> use @JSName as an expression. | 8 // other @JSName -> use @JSName as an expression. |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import 'dart:_js_helper' show Native, JSName, convertDartClosureToJS; | 11 import 'dart:_js_helper' show Native, JSName, convertDartClosureToJS; |
12 | 12 |
13 | |
14 typedef int Callback(String s); | 13 typedef int Callback(String s); |
15 | 14 |
16 @Native("CC") // Tag can be different to class name. | 15 @Native("CC") // Tag can be different to class name. |
17 class AA { | 16 class AA { |
18 // This name is not an identifier, so completely defines how to access method. | 17 // This name is not an identifier, so completely defines how to access method. |
19 @JSName('CC.foo') | 18 @JSName('CC.foo') |
20 static int foo(String s) native; | 19 static int foo(String s) native ; |
21 | 20 |
22 // This name is not an identifier, so completely defines how to access method. | 21 // This name is not an identifier, so completely defines how to access method. |
23 @JSName('CC.bar') | 22 @JSName('CC.bar') |
24 static int bar(Callback c) native; | 23 static int bar(Callback c) native ; |
25 static int baz(Callback c) { return bar(c); } | 24 static int baz(Callback c) { |
| 25 return bar(c); |
| 26 } |
26 | 27 |
27 // Compiler should automatically use the tag and the declared name, i.e. call | 28 // Compiler should automatically use the tag and the declared name, i.e. call |
28 // `CC.lepton`. | 29 // `CC.lepton`. |
29 static int lepton(Callback c) native; | 30 static int lepton(Callback c) native ; |
30 static int electron(c) => lepton(c); | 31 static int electron(c) => lepton(c); |
31 | 32 |
32 // Compiler should automatically use the tag and JSName, i.e. call | 33 // Compiler should automatically use the tag and JSName, i.e. call |
33 // `CC.baryon`. | 34 // `CC.baryon`. |
34 @JSName('baryon') | 35 @JSName('baryon') |
35 static int _baryon(Callback c) native; | 36 static int _baryon(Callback c) native ; |
36 static int proton(c) => _baryon(c); | 37 static int proton(c) => _baryon(c); |
37 } | 38 } |
38 | 39 |
39 void setup() native r""" | 40 void setup() native r""" |
40 // This code is all inside 'setup' and so not accessible from the global scope. | 41 // This code is all inside 'setup' and so not accessible from the global scope. |
41 | 42 |
42 function CC(){} | 43 function CC(){} |
43 | 44 |
44 CC.foo = function(s) { return s.length; } | 45 CC.foo = function(s) { return s.length; } |
45 CC.bar = function(f) { return f("Bye"); } | 46 CC.bar = function(f) { return f("Bye"); } |
(...skipping 13 matching lines...) Expand all Loading... |
59 Expect.equals(5, AA.foo("Hello")); | 60 Expect.equals(5, AA.foo("Hello")); |
60 | 61 |
61 Expect.equals(3, AA.bar((s) => s.length)); | 62 Expect.equals(3, AA.bar((s) => s.length)); |
62 Expect.equals(3, AA.baz((s) => s.length)); | 63 Expect.equals(3, AA.baz((s) => s.length)); |
63 | 64 |
64 Expect.equals(6, AA.lepton((s) => s.length)); | 65 Expect.equals(6, AA.lepton((s) => s.length)); |
65 Expect.equals(6, AA.electron((s) => s.length)); | 66 Expect.equals(6, AA.electron((s) => s.length)); |
66 | 67 |
67 Expect.equals(12, AA._baryon((s) => s.length)); | 68 Expect.equals(12, AA._baryon((s) => s.length)); |
68 Expect.equals(12, AA.proton((s) => s.length)); | 69 Expect.equals(12, AA.proton((s) => s.length)); |
69 Expect.throws(() => AA.baryon((s) => s.length)); // Not defined on AA. | 70 Expect.throws(() => AA.baryon((s) => s.length)); // Not defined on AA. |
70 } | 71 } |
OLD | NEW |