OLD | NEW |
(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 } |
OLD | NEW |