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 /// This library defines runtime operations on objects used by the code | 5 /// This library defines runtime operations on objects used by the code |
6 /// generator. | 6 /// generator. |
7 part of dart._runtime; | 7 part of dart._runtime; |
8 | 8 |
9 class _Invocation extends Invocation { | 9 class InvocationImpl extends Invocation { |
10 final Symbol memberName; | 10 final Symbol memberName; |
11 final List positionalArguments; | 11 final List positionalArguments; |
12 final Map<Symbol, dynamic> namedArguments; | 12 final Map<Symbol, dynamic> namedArguments; |
13 final bool isMethod; | 13 final bool isMethod; |
14 final bool isGetter; | 14 final bool isGetter; |
15 final bool isSetter; | 15 final bool isSetter; |
16 | 16 |
17 _Invocation(String memberName, this.positionalArguments, | 17 InvocationImpl(String memberName, this.positionalArguments, |
18 {namedArguments, | 18 {namedArguments, |
19 this.isMethod: false, | 19 this.isMethod: false, |
20 this.isGetter: false, | 20 this.isGetter: false, |
21 this.isSetter: false}) | 21 this.isSetter: false}) |
22 : memberName = _dartSymbol(memberName), | 22 : memberName = _dartSymbol(memberName), |
23 namedArguments = _namedArgsToSymbols(namedArguments); | 23 namedArguments = _namedArgsToSymbols(namedArguments); |
24 | 24 |
25 static Map<Symbol, dynamic> _namedArgsToSymbols(namedArgs) { | 25 static Map<Symbol, dynamic> _namedArgsToSymbols(namedArgs) { |
26 if (namedArgs == null) return {}; | 26 if (namedArgs == null) return {}; |
27 return new Map.fromIterable( | 27 return new Map.fromIterable( |
28 getOwnPropertyNames(namedArgs), | 28 getOwnPropertyNames(namedArgs), |
29 key: _dartSymbol, | 29 key: _dartSymbol, |
30 value: (k) => JS('', '#[#]', namedArgs, k)); | 30 value: (k) => JS('', '#[#]', namedArgs, k)); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 dload(obj, field) { | 34 dload(obj, field) { |
35 var f = _canonicalMember(obj, field); | 35 var f = _canonicalMember(obj, field); |
36 _trackCall(obj, f); | 36 _trackCall(obj, f); |
37 if (f != null) { | 37 if (f != null) { |
38 if (hasMethod(obj, f)) return bind(obj, f, JS('', 'void 0')); | 38 if (hasMethod(obj, f)) return bind(obj, f, JS('', 'void 0')); |
39 return JS('', '#[#]', obj, f); | 39 return JS('', '#[#]', obj, f); |
40 } | 40 } |
41 return noSuchMethod(obj, | 41 return noSuchMethod(obj, |
42 new _Invocation(field, JS('', '[]'), isGetter: true)); | 42 new InvocationImpl(field, JS('', '[]'), isGetter: true)); |
43 } | 43 } |
44 | 44 |
45 dput(obj, field, value) { | 45 dput(obj, field, value) { |
46 var f = _canonicalMember(obj, field); | 46 var f = _canonicalMember(obj, field); |
47 _trackCall(obj, f); | 47 _trackCall(obj, f); |
48 if (f != null) { | 48 if (f != null) { |
49 return JS('', '#[#] = #', obj, f, value); | 49 return JS('', '#[#] = #', obj, f, value); |
50 } | 50 } |
51 return noSuchMethod(obj, | 51 return noSuchMethod(obj, |
52 new _Invocation(field, JS('', '[#]', value), isSetter: true)); | 52 new InvocationImpl(field, JS('', '[#]', value), isSetter: true)); |
53 } | 53 } |
54 | 54 |
55 /// Check that a function of a given type can be applied to | 55 /// Check that a function of a given type can be applied to |
56 /// actuals. | 56 /// actuals. |
57 _checkApply(type, actuals) => JS('', '''(() => { | 57 _checkApply(type, actuals) => JS('', '''(() => { |
58 if ($actuals.length < $type.args.length) return false; | 58 if ($actuals.length < $type.args.length) return false; |
59 let index = 0; | 59 let index = 0; |
60 for(let i = 0; i < $type.args.length; ++i) { | 60 for(let i = 0; i < $type.args.length; ++i) { |
61 if (!$instanceOfOrNull($actuals[i], $type.args[i])) return false; | 61 if (!$instanceOfOrNull($actuals[i], $type.args[i])) return false; |
62 ++index; | 62 ++index; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 $_trackCall($obj, $name); | 97 $_trackCall($obj, $name); |
98 | 98 |
99 let originalTarget = obj === void 0 ? f : obj; | 99 let originalTarget = obj === void 0 ? f : obj; |
100 | 100 |
101 function callNSM() { | 101 function callNSM() { |
102 let namedArgs = null; | 102 let namedArgs = null; |
103 if (args.length > 0 && | 103 if (args.length > 0 && |
104 args[args.length - 1].__proto__ == Object.prototype) { | 104 args[args.length - 1].__proto__ == Object.prototype) { |
105 namedArgs = args.pop(); | 105 namedArgs = args.pop(); |
106 } | 106 } |
107 return $noSuchMethod(originalTarget, new $_Invocation( | 107 return $noSuchMethod(originalTarget, new $InvocationImpl( |
108 $name, $args, {namedArguments: namedArgs, isMethod: true})); | 108 $name, $args, {namedArguments: namedArgs, isMethod: true})); |
109 } | 109 } |
110 if (!($f instanceof Function)) { | 110 if (!($f instanceof Function)) { |
111 // We're not a function (and hence not a method either) | 111 // We're not a function (and hence not a method either) |
112 // Grab the `call` method if it's not a function. | 112 // Grab the `call` method if it's not a function. |
113 if ($f != null) { | 113 if ($f != null) { |
114 $ftype = $getMethodType($f, 'call'); | 114 $ftype = $getMethodType($f, 'call'); |
115 $f = $f.call; | 115 $f = $f.call; |
116 } | 116 } |
117 if (!($f instanceof Function)) { | 117 if (!($f instanceof Function)) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } else { | 217 } else { |
218 _callMethodStats[name] = 1; | 218 _callMethodStats[name] = 1; |
219 } | 219 } |
220 } | 220 } |
221 | 221 |
222 /// Shared code for dsend, dindex, and dsetindex. | 222 /// Shared code for dsend, dindex, and dsetindex. |
223 _callMethod(obj, name, typeArgs, args, displayName) { | 223 _callMethod(obj, name, typeArgs, args, displayName) { |
224 var symbol = _canonicalMember(obj, name); | 224 var symbol = _canonicalMember(obj, name); |
225 if (symbol == null) { | 225 if (symbol == null) { |
226 return noSuchMethod(obj, | 226 return noSuchMethod(obj, |
227 new _Invocation(displayName, args, isMethod: true)); | 227 new InvocationImpl(displayName, args, isMethod: true)); |
228 } | 228 } |
229 var f = obj != null ? JS('', '#[#]', obj, symbol) : null; | 229 var f = obj != null ? JS('', '#[#]', obj, symbol) : null; |
230 var ftype = getMethodType(obj, symbol); | 230 var ftype = getMethodType(obj, symbol); |
231 return _checkAndCall(f, ftype, obj, typeArgs, args, displayName); | 231 return _checkAndCall(f, ftype, obj, typeArgs, args, displayName); |
232 } | 232 } |
233 | 233 |
234 dsend(obj, method, @rest args) => _callMethod(obj, method, null, args, method); | 234 dsend(obj, method, @rest args) => _callMethod(obj, method, null, args, method); |
235 | 235 |
236 dgsend(obj, typeArgs, method, @rest args) => | 236 dgsend(obj, typeArgs, method, @rest args) => |
237 _callMethod(obj, method, typeArgs, args, method); | 237 _callMethod(obj, method, typeArgs, args, method); |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 if (obj != null && getExtensionType(obj) != null) { | 725 if (obj != null && getExtensionType(obj) != null) { |
726 return JS('', 'dartx.#', name); | 726 return JS('', 'dartx.#', name); |
727 } | 727 } |
728 | 728 |
729 // Check for certain names that we can't use in JS | 729 // Check for certain names that we can't use in JS |
730 if (name == 'constructor' || name == 'prototype') { | 730 if (name == 'constructor' || name == 'prototype') { |
731 name = '+' + name; | 731 name = '+' + name; |
732 } | 732 } |
733 return name; | 733 return name; |
734 } | 734 } |
OLD | NEW |