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 var dart, _js_helper, _js_primitives; | 5 var dart, _js_helper, _js_primitives; |
6 (function (dart) { | 6 (function (dart) { |
7 'use strict'; | 7 'use strict'; |
8 | 8 |
9 // TODO(vsm): This is referenced (as init.globalState) from | 9 // TODO(vsm): This is referenced (as init.globalState) from |
10 // isolate_helper.dart. Where should it go? | 10 // isolate_helper.dart. Where should it go? |
11 // See: https://github.com/dart-lang/dev_compiler/issues/164 | 11 // See: https://github.com/dart-lang/dev_compiler/issues/164 |
12 dart.globalState = null; | 12 dart.globalState = null; |
13 | 13 |
14 let defineProperty = Object.defineProperty; | 14 let defineProperty = Object.defineProperty; |
15 let getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | 15 let getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
16 let getOwnPropertyNames = Object.getOwnPropertyNames; | 16 let getOwnPropertyNames = Object.getOwnPropertyNames; |
17 let getOwnPropertySymbols = Object.getOwnPropertySymbols; | 17 let getOwnPropertySymbols = Object.getOwnPropertySymbols; |
18 | 18 |
19 function getOwnNamesAndSymbols(obj) { | 19 function getOwnNamesAndSymbols(obj) { |
20 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); | 20 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)); |
21 } | 21 } |
22 | 22 |
23 // Adapted from Angular.js | |
24 let FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; | |
25 let FN_ARG_SPLIT = /,/; | |
26 let FN_ARG = /^\s*(_?)(\S+?)\1\s*$/; | |
27 let STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
28 | |
29 function formalParameterList(fn) { | |
Jennifer Messerly
2015/05/29 23:02:20
dead code
vsm
2015/05/29 23:07:37
We may want this for parameter mirrors - otherwise
| |
30 let fnText,argDecl; | |
31 let args=[]; | |
32 fnText = fn.toString().replace(STRIP_COMMENTS, ''); | |
33 argDecl = fnText.match(FN_ARGS); | |
34 | |
35 let r = argDecl[1].split(FN_ARG_SPLIT); | |
36 for (let arg of r) { | |
37 arg.replace(FN_ARG, function(all, underscore, name){ | |
38 args.push(name); | |
39 }); | |
40 } | |
41 return args; | |
42 } | |
43 | |
44 function dload(obj, field) { | 23 function dload(obj, field) { |
45 field = _canonicalFieldName(obj, field); | 24 field = _canonicalFieldName(obj, field); |
46 if (_getMethodType(obj, field) !== void 0) { | 25 if (_getMethodType(obj, field) !== void 0) { |
47 return dart.bind(obj, field); | 26 return dart.bind(obj, field); |
48 } | 27 } |
49 // TODO(vsm): Implement NSM robustly. An 'in' check breaks on certain | 28 // TODO(vsm): Implement NSM robustly. An 'in' check breaks on certain |
50 // types. hasOwnProperty doesn't chase the proto chain. | 29 // types. hasOwnProperty doesn't chase the proto chain. |
51 // Also, do we want an NSM on regular JS objects? | 30 // Also, do we want an NSM on regular JS objects? |
52 // See: https://github.com/dart-lang/dev_compiler/issues/169 | 31 // See: https://github.com/dart-lang/dev_compiler/issues/169 |
53 var result = obj[field]; | 32 var result = obj[field]; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 dart.dcall = dcall; | 98 dart.dcall = dcall; |
120 | 99 |
121 // TODO(vsm): Automatically build this. | 100 // TODO(vsm): Automatically build this. |
122 // All dynamic methods should check for these. | 101 // All dynamic methods should check for these. |
123 // See: https://github.com/dart-lang/dev_compiler/issues/142 | 102 // See: https://github.com/dart-lang/dev_compiler/issues/142 |
124 var _extensionMethods = { | 103 var _extensionMethods = { |
125 // Lazy - as these symbols may not be loaded yet. | 104 // Lazy - as these symbols may not be loaded yet. |
126 // TODO(vsm): This should record / check the receiver type | 105 // TODO(vsm): This should record / check the receiver type |
127 // as well. E.g., only look for core.$map if the receiver | 106 // as well. E.g., only look for core.$map if the receiver |
128 // is an Iterable. | 107 // is an Iterable. |
129 'map': () => core.$map, | 108 'map': () => core.$map |
130 }; | 109 }; |
131 | 110 |
132 // TODO(leafp): Integrate this with the eventual proper extension | 111 // TODO(leafp): Integrate this with the eventual proper extension |
133 // method system. | 112 // method system. |
134 function _canonicalFieldName(obj, name) { | 113 function _canonicalFieldName(obj, name) { |
135 if (obj[name] === void 0) return _extensionMethods[name](); | 114 if (obj[name] === void 0) return _extensionMethods[name](); |
136 return name; | 115 return name; |
137 } | 116 } |
138 | 117 |
139 function dsend(obj, method/*, ...args*/) { | 118 function dsend(obj, method/*, ...args*/) { |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 function fn(closure/* ...args*/) { | 544 function fn(closure/* ...args*/) { |
566 // Closure and a lazy type constructor | 545 // Closure and a lazy type constructor |
567 if (arguments.length == 2) { | 546 if (arguments.length == 2) { |
568 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); | 547 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); |
569 return closure; | 548 return closure; |
570 } | 549 } |
571 var t; | 550 var t; |
572 if (arguments.length == 1) { | 551 if (arguments.length == 1) { |
573 // No type arguments, it's all dynamic | 552 // No type arguments, it's all dynamic |
574 let len = closure.length; | 553 let len = closure.length; |
575 function build() { | 554 let build = () => { |
576 let args = Array.apply(null, new Array(len)).map(() => core.Object); | 555 let args = Array.apply(null, new Array(len)).map(() => core.Object); |
577 return functionType(core.Object, args); | 556 return functionType(core.Object, args); |
578 } | 557 }; |
579 // We could be called before Object is defined. | 558 // We could be called before Object is defined. |
580 if (core.Object === void 0) return fn(closure, build); | 559 if (core.Object === void 0) return fn(closure, build); |
581 t = build(); | 560 t = build(); |
582 } else { | 561 } else { |
583 // We're passed the piecewise components of the function type, | 562 // We're passed the piecewise components of the function type, |
584 // construct it. | 563 // construct it. |
585 let args = Array.prototype.slice.call(arguments, 1); | 564 let args = Array.prototype.slice.call(arguments, 1); |
586 t = functionType.apply(null, args); | 565 t = functionType.apply(null, args); |
587 } | 566 } |
588 setRuntimeType(closure, t); | 567 setRuntimeType(closure, t); |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1014 } | 993 } |
1015 | 994 |
1016 /// Get the type of a method using the stored signature | 995 /// Get the type of a method using the stored signature |
1017 function _getMethodType(obj, name) { | 996 function _getMethodType(obj, name) { |
1018 if (obj === void 0) return void 0; | 997 if (obj === void 0) return void 0; |
1019 if (obj == null) return void 0; | 998 if (obj == null) return void 0; |
1020 let sigObj = obj.__proto__.constructor[_methodSig]; | 999 let sigObj = obj.__proto__.constructor[_methodSig]; |
1021 if (sigObj === void 0) return void 0; | 1000 if (sigObj === void 0) return void 0; |
1022 let parts = sigObj[name]; | 1001 let parts = sigObj[name]; |
1023 if (parts === void 0) return void 0; | 1002 if (parts === void 0) return void 0; |
1024 let sig = functionType.apply(null, parts); | 1003 return functionType.apply(null, parts); |
1025 return sig; | |
Jennifer Messerly
2015/05/29 23:02:21
it doesn't like unnecessary locals
| |
1026 } | 1004 } |
1027 | 1005 |
1028 /// Get the type of a constructor from a class using the stored signature | 1006 /// Get the type of a constructor from a class using the stored signature |
1029 /// If name is undefined, returns the type of the default constructor | 1007 /// If name is undefined, returns the type of the default constructor |
1030 /// Returns undefined if the constructor is not found. | 1008 /// Returns undefined if the constructor is not found. |
1031 function _getConstructorType(cls, name) { | 1009 function _getConstructorType(cls, name) { |
1032 if(!name) name = cls.name; | 1010 if(!name) name = cls.name; |
1033 if (cls === void 0) return void 0; | 1011 if (cls === void 0) return void 0; |
1034 if (cls == null) return void 0; | 1012 if (cls == null) return void 0; |
1035 let sigCtor = cls[_constructorSig]; | 1013 let sigCtor = cls[_constructorSig]; |
1036 if (sigCtor === void 0) return void 0; | 1014 if (sigCtor === void 0) return void 0; |
1037 let parts = sigCtor[name]; | 1015 let parts = sigCtor[name]; |
1038 if (parts === void 0) return void 0; | 1016 if (parts === void 0) return void 0; |
1039 let sig = functionType.apply(null, parts); | 1017 return functionType.apply(null, parts); |
1040 return sig; | |
1041 } | 1018 } |
1042 dart.classGetConstructorType = _getConstructorType; | 1019 dart.classGetConstructorType = _getConstructorType; |
1043 | 1020 |
1044 /// Given an object and a method name, tear off the method. | 1021 /// Given an object and a method name, tear off the method. |
1045 /// Sets the runtime type of the torn off method appropriately, | 1022 /// Sets the runtime type of the torn off method appropriately, |
1046 /// and also binds the object. | 1023 /// and also binds the object. |
1047 /// TODO(leafp): Consider caching the tearoff on the object? | 1024 /// TODO(leafp): Consider caching the tearoff on the object? |
1048 function bind(obj, name) { | 1025 function bind(obj, name) { |
1049 let f = obj[name].bind(obj); | 1026 let f = obj[name].bind(obj); |
1050 let sig = _getMethodType(obj, name) | 1027 let sig = _getMethodType(obj, name); |
1051 assert(sig); | 1028 assert(sig); |
1052 setRuntimeType(f, sig); | 1029 setRuntimeType(f, sig); |
1053 return f; | 1030 return f; |
1054 } | 1031 } |
1055 dart.bind = bind; | 1032 dart.bind = bind; |
1056 | 1033 |
1057 // Set up the method signature field on the constructor | 1034 // Set up the method signature field on the constructor |
1058 function _setMethodSignature(f, sigF) { | 1035 function _setMethodSignature(f, sigF) { |
1059 defineMemoizedGetter(f, _methodSig, () => { | 1036 defineMemoizedGetter(f, _methodSig, () => { |
1060 let sigObj = sigF(); | 1037 let sigObj = sigF(); |
1061 sigObj.__proto__ = f.__proto__[_methodSig]; | 1038 sigObj.__proto__ = f.__proto__[_methodSig]; |
1062 return sigObj; | 1039 return sigObj; |
1063 }); | 1040 }); |
1064 } | 1041 } |
1065 | 1042 |
1066 // Set up the constructor signature field on the constructor | 1043 // Set up the constructor signature field on the constructor |
1067 function _setConstructorSignature(f, sigF) { | 1044 function _setConstructorSignature(f, sigF) { |
1068 defineMemoizedGetter(f, _constructorSig, sigF); | 1045 defineMemoizedGetter(f, _constructorSig, sigF); |
1069 } | 1046 } |
1070 | 1047 |
1071 // Set up the static signature field on the constructor | 1048 // Set up the static signature field on the constructor |
1072 function _setStaticSignature(f, sigF) { | 1049 function _setStaticSignature(f, sigF) { |
1073 defineMemoizedGetter(f, _staticSig, sigF); | 1050 defineMemoizedGetter(f, _staticSig, sigF); |
1074 } | 1051 } |
1075 | 1052 |
1076 // Set the lazily computed runtime type field on static methods | 1053 // Set the lazily computed runtime type field on static methods |
1077 function _setStaticTypes(f, names) { | 1054 function _setStaticTypes(f, names) { |
1078 for (let name of names) { | 1055 for (let name of names) { |
1079 function getT() { | 1056 defineProperty(f[name], _runtimeType, { get: function() { |
1080 let parts = f[_staticSig][name]; | 1057 let parts = f[_staticSig][name]; |
1081 return functionType.apply(null, parts); | 1058 return functionType.apply(null, parts); |
1082 }; | 1059 }}); |
1083 defineProperty(f[name], _runtimeType, {get : getT}); | |
1084 } | 1060 } |
1085 } | 1061 } |
1086 | 1062 |
1087 /// Set up the type signature of a class (constructor object) | 1063 /// Set up the type signature of a class (constructor object) |
1088 /// f is a constructor object | 1064 /// f is a constructor object |
1089 /// signature is an object containing optional properties as follows: | 1065 /// signature is an object containing optional properties as follows: |
1090 /// methods: A function returning an object mapping method names | 1066 /// methods: A function returning an object mapping method names |
1091 /// to method types. The function is evaluated lazily and cached. | 1067 /// to method types. The function is evaluated lazily and cached. |
1092 /// statics: A function returning an object mapping static method | 1068 /// statics: A function returning an object mapping static method |
1093 /// names to types. The function is evalutated lazily and cached. | 1069 /// names to types. The function is evalutated lazily and cached. |
1094 /// names: An array of the names of the static methods. Used to | 1070 /// names: An array of the names of the static methods. Used to |
1095 /// permit eagerly setting the runtimeType field on the methods | 1071 /// permit eagerly setting the runtimeType field on the methods |
1096 /// while still lazily computing the type descriptor object. | 1072 /// while still lazily computing the type descriptor object. |
1097 function setSignature(f, signature) { | 1073 function setSignature(f, signature) { |
1098 let constructors = | 1074 let constructors = |
1099 ('constructors' in signature) ? signature.constructors : () => ({}); | 1075 ('constructors' in signature) ? signature.constructors : () => ({}); |
1100 let methods = | 1076 let methods = |
1101 ('methods' in signature) ? signature.methods : () => ({}); | 1077 ('methods' in signature) ? signature.methods : () => ({}); |
1102 let statics = | 1078 let statics = |
1103 ('statics' in signature) ? signature.statics : () => ({}); | 1079 ('statics' in signature) ? signature.statics : () => ({}); |
1104 let names = | 1080 let names = |
1105 ('names' in signature) ? signature.names : []; | 1081 ('names' in signature) ? signature.names : []; |
1106 _setConstructorSignature(f, constructors); | 1082 _setConstructorSignature(f, constructors); |
1107 _setMethodSignature(f, methods); | 1083 _setMethodSignature(f, methods); |
1108 _setStaticSignature(f, statics); | 1084 _setStaticSignature(f, statics); |
1109 _setStaticTypes(f, names); | 1085 _setStaticTypes(f, names); |
1110 }; | 1086 } |
1111 dart.setSignature = setSignature; | 1087 dart.setSignature = setSignature; |
1112 | 1088 |
1113 let _value = Symbol('_value'); | 1089 let _value = Symbol('_value'); |
1114 /** | 1090 /** |
1115 * Looks up a sequence of [keys] in [map], recursively, and | 1091 * Looks up a sequence of [keys] in [map], recursively, and |
1116 * returns the result. If the value is not found, [valueFn] will be called to | 1092 * returns the result. If the value is not found, [valueFn] will be called to |
1117 * add it. For example: | 1093 * add it. For example: |
1118 * | 1094 * |
1119 * var map = new Map(); | 1095 * var map = new Map(); |
1120 * putIfAbsent(map, [1, 2, 'hi ', 'there '], () => 'world'); | 1096 * putIfAbsent(map, [1, 2, 'hi ', 'there '], () => 'world'); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1283 _js_primitives.printString = (s) => console.log(s); | 1259 _js_primitives.printString = (s) => console.log(s); |
1284 | 1260 |
1285 // TODO(vsm): Plumb this correctly. | 1261 // TODO(vsm): Plumb this correctly. |
1286 // See: https://github.com/dart-lang/dev_compiler/issues/40 | 1262 // See: https://github.com/dart-lang/dev_compiler/issues/40 |
1287 String.prototype.contains = function(sub) { return this.indexOf(sub) >= 0; } | 1263 String.prototype.contains = function(sub) { return this.indexOf(sub) >= 0; } |
1288 let _split = String.prototype.split; | 1264 let _split = String.prototype.split; |
1289 String.prototype.split = function() { | 1265 String.prototype.split = function() { |
1290 let result = _split.apply(this, arguments); | 1266 let result = _split.apply(this, arguments); |
1291 dart.setType(result, core.List$(core.String)); | 1267 dart.setType(result, core.List$(core.String)); |
1292 return result; | 1268 return result; |
1293 } | 1269 }; |
1294 String.prototype.get = function(i) { | 1270 String.prototype.get = function(i) { |
1295 return this[i]; | 1271 return this[i]; |
1296 } | 1272 }; |
1297 String.prototype.codeUnitAt = function(i) { | 1273 String.prototype.codeUnitAt = function(i) { |
1298 return this.charCodeAt(i); | 1274 return this.charCodeAt(i); |
1299 } | 1275 }; |
1300 String.prototype.replaceAllMapped = function(from, cb) { | 1276 String.prototype.replaceAllMapped = function(from, cb) { |
1301 return this.replace(from.multiple, function() { | 1277 return this.replace(from.multiple, function() { |
1302 // Remove offset & string from the result array | 1278 // Remove offset & string from the result array |
1303 var matches = arguments; | 1279 var matches = arguments; |
1304 matches.splice(-2, 2); | 1280 matches.splice(-2, 2); |
1305 // The callback receives match, p1, ..., pn | 1281 // The callback receives match, p1, ..., pn |
1306 return cb(matches); | 1282 return cb(matches); |
1307 }); | 1283 }); |
1308 } | 1284 }; |
1309 String.prototype['+'] = function(arg) { return this.valueOf() + arg; }; | 1285 String.prototype['+'] = function(arg) { return this.valueOf() + arg; }; |
1310 | 1286 |
1311 Boolean.prototype['!'] = function() { return !this.valueOf(); }; | 1287 Boolean.prototype['!'] = function() { return !this.valueOf(); }; |
1312 Boolean.prototype['&&'] = function(arg) { return this.valueOf() && arg; }; | 1288 Boolean.prototype['&&'] = function(arg) { return this.valueOf() && arg; }; |
1313 Boolean.prototype['||'] = function(arg) { return this.valueOf() || arg; }; | 1289 Boolean.prototype['||'] = function(arg) { return this.valueOf() || arg; }; |
1314 Number.prototype['<'] = function(arg) { return this.valueOf() < arg; }; | 1290 Number.prototype['<'] = function(arg) { return this.valueOf() < arg; }; |
1315 Number.prototype['<='] = function(arg) { return this.valueOf() <= arg; }; | 1291 Number.prototype['<='] = function(arg) { return this.valueOf() <= arg; }; |
1316 Number.prototype['>'] = function(arg) { return this.valueOf() > arg; }; | 1292 Number.prototype['>'] = function(arg) { return this.valueOf() > arg; }; |
1317 Number.prototype['+'] = function(arg) { return this.valueOf() + arg; }; | 1293 Number.prototype['+'] = function(arg) { return this.valueOf() + arg; }; |
1318 | 1294 |
1319 // TODO(vsm): DOM facades? | 1295 // TODO(vsm): DOM facades? |
1320 // See: https://github.com/dart-lang/dev_compiler/issues/173 | 1296 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
1321 NodeList.prototype.get = function(i) { return this[i]; }; | 1297 NodeList.prototype.get = function(i) { return this[i]; }; |
1322 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | 1298 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
1323 DOMTokenList.prototype.get = function(i) { return this[i]; }; | 1299 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
1324 | 1300 |
1325 })(dart || (dart = {})); | 1301 })(dart || (dart = {})); |
OLD | NEW |