| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 part of _js_helper; | |
| 6 | |
| 7 | |
| 8 // TODO(ngeoffray): stop using this method once our optimizers can | |
| 9 // change str1.contains(str2) into str1.indexOf(str2) != -1. | |
| 10 bool contains(String userAgent, String name) { | |
| 11 return JS('int', '#.indexOf(#)', userAgent, name) != -1; | |
| 12 } | |
| 13 | |
| 14 int arrayLength(List array) { | |
| 15 return JS('int', '#.length', array); | |
| 16 } | |
| 17 | |
| 18 arrayGet(List array, int index) { | |
| 19 return JS('var', '#[#]', array, index); | |
| 20 } | |
| 21 | |
| 22 void arraySet(List array, int index, var value) { | |
| 23 JS('var', '#[#] = #', array, index, value); | |
| 24 } | |
| 25 | |
| 26 propertyGet(var object, String property) { | |
| 27 return JS('var', '#[#]', object, property); | |
| 28 } | |
| 29 | |
| 30 bool callHasOwnProperty(var function, var object, String property) { | |
| 31 return JS('bool', '#.call(#, #)', function, object, property); | |
| 32 } | |
| 33 | |
| 34 void propertySet(var object, String property, var value) { | |
| 35 JS('var', '#[#] = #', object, property, value); | |
| 36 } | |
| 37 | |
| 38 getPropertyFromPrototype(var object, String name) { | |
| 39 return JS('var', 'Object.getPrototypeOf(#)[#]', object, name); | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * Returns a String tag identifying the type of the native object, or `null`. | |
| 44 * The tag is not the name of the type, but usually the name of the JavaScript | |
| 45 * constructor function. Initialized by [initHooks]. | |
| 46 */ | |
| 47 Function getTagFunction; | |
| 48 | |
| 49 /** | |
| 50 * If a lookup via [getTagFunction] on an object [object] that has [tag] fails, | |
| 51 * this function is called to provide an alternate tag. This allows us to fail | |
| 52 * gracefully if we can make a good guess, for example, when browsers add novel | |
| 53 * kinds of HTMLElement that we have never heard of. Initialized by | |
| 54 * [initHooks]. | |
| 55 */ | |
| 56 Function alternateTagFunction; | |
| 57 | |
| 58 /** | |
| 59 * Returns the prototype for the JavaScript constructor named by an input tag. | |
| 60 * Returns `null` if there is no such constructor, or if pre-patching of the | |
| 61 * constructor is to be avoided. Initialized by [initHooks]. | |
| 62 */ | |
| 63 Function prototypeForTagFunction; | |
| 64 | |
| 65 | |
| 66 String toStringForNativeObject(var obj) { | |
| 67 // TODO(sra): Is this code dead? | |
| 68 // [getTagFunction] might be uninitialized, but in usual usage, toString has | |
| 69 // been called via an interceptor and initialized it. | |
| 70 String name = getTagFunction == null | |
| 71 ? '<Unknown>' | |
| 72 : JS('String', '#', getTagFunction(obj)); | |
| 73 return 'Instance of $name'; | |
| 74 } | |
| 75 | |
| 76 int hashCodeForNativeObject(object) => Primitives.objectHashCode(object); | |
| 77 | |
| 78 /** | |
| 79 * Sets a JavaScript property on an object. | |
| 80 */ | |
| 81 void defineProperty(var obj, String property, var value) { | |
| 82 JS('void', | |
| 83 'Object.defineProperty(#, #, ' | |
| 84 '{value: #, enumerable: false, writable: true, configurable: true})', | |
| 85 obj, | |
| 86 property, | |
| 87 value); | |
| 88 } | |
| 89 | |
| 90 | |
| 91 // Is [obj] an instance of a Dart-defined class? | |
| 92 bool isDartObject(obj) { | |
| 93 // Some of the extra parens here are necessary. | |
| 94 return JS('bool', '((#) instanceof (#))', | |
| 95 obj, | |
| 96 JS_BUILTIN('depends:none;effects:none;', | |
| 97 JsBuiltin.dartObjectConstructor)); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * A JavaScript object mapping tags to the constructors of interceptors. | |
| 102 * This is a JavaScript object with no prototype. | |
| 103 * | |
| 104 * Example: 'HTMLImageElement' maps to the ImageElement class constructor. | |
| 105 */ | |
| 106 get interceptorsByTag => JS_EMBEDDED_GLOBAL('=Object', INTERCEPTORS_BY_TAG); | |
| 107 | |
| 108 /** | |
| 109 * A JavaScript object mapping tags to `true` or `false`. | |
| 110 * | |
| 111 * Example: 'HTMLImageElement' maps to `true` since, as there are no subclasses | |
| 112 * of ImageElement, it is a leaf class in the native class hierarchy. | |
| 113 */ | |
| 114 get leafTags => JS_EMBEDDED_GLOBAL('=Object', LEAF_TAGS); | |
| 115 | |
| 116 String findDispatchTagForInterceptorClass(interceptorClassConstructor) { | |
| 117 return JS('', r'#.#', | |
| 118 interceptorClassConstructor, NATIVE_SUPERCLASS_TAG_NAME); | |
| 119 } | |
| 120 | |
| 121 /** | |
| 122 * Cache of dispatch records for instances. This is a JavaScript object used as | |
| 123 * a map. Keys are instance tags, e.g. "!SomeThing". The cache permits the | |
| 124 * sharing of one dispatch record between multiple instances. | |
| 125 */ | |
| 126 var dispatchRecordsForInstanceTags; | |
| 127 | |
| 128 /** | |
| 129 * Cache of interceptors indexed by uncacheable tags, e.g. "~SomeThing". | |
| 130 * This is a JavaScript object used as a map. | |
| 131 */ | |
| 132 var interceptorsForUncacheableTags; | |
| 133 | |
| 134 | |
| 135 lookupInterceptor(String tag) { | |
| 136 return propertyGet(interceptorsByTag, tag); | |
| 137 } | |
| 138 | |
| 139 | |
| 140 // Dispatch tag marks are optional prefixes for a dispatch tag that direct how | |
| 141 // the interceptor for the tag may be cached. | |
| 142 | |
| 143 /// No caching permitted. | |
| 144 const UNCACHED_MARK = '~'; | |
| 145 | |
| 146 /// Dispatch record must be cached per instance | |
| 147 const INSTANCE_CACHED_MARK = '!'; | |
| 148 | |
| 149 /// Dispatch record is cached on immediate prototype. | |
| 150 const LEAF_MARK = '-'; | |
| 151 | |
| 152 /// Dispatch record is cached on immediate prototype with a prototype | |
| 153 /// verification to prevent the interceptor being associated with a subclass | |
| 154 /// before a dispatch record is cached on the subclass. | |
| 155 const INTERIOR_MARK = '+'; | |
| 156 | |
| 157 /// A 'discriminator' function is to be used. TBD. | |
| 158 const DISCRIMINATED_MARK = '*'; | |
| 159 | |
| 160 | |
| 161 /** | |
| 162 * Returns the interceptor for a native object, or returns `null` if not found. | |
| 163 * | |
| 164 * A dispatch record is cached according to the specification of the dispatch | |
| 165 * tag for [obj]. | |
| 166 */ | |
| 167 lookupAndCacheInterceptor(obj) { | |
| 168 assert(!isDartObject(obj)); | |
| 169 String tag = getTagFunction(obj); | |
| 170 | |
| 171 // Fast path for instance (and uncached) tags because the lookup is repeated | |
| 172 // for each instance (or getInterceptor call). | |
| 173 var record = propertyGet(dispatchRecordsForInstanceTags, tag); | |
| 174 if (record != null) return patchInstance(obj, record); | |
| 175 var interceptor = propertyGet(interceptorsForUncacheableTags, tag); | |
| 176 if (interceptor != null) return interceptor; | |
| 177 | |
| 178 // This lookup works for derived dispatch tags because we add them all in | |
| 179 // [initNativeDispatch]. | |
| 180 var interceptorClass = lookupInterceptor(tag); | |
| 181 if (interceptorClass == null) { | |
| 182 tag = alternateTagFunction(obj, tag); | |
| 183 if (tag != null) { | |
| 184 // Fast path for instance and uncached tags again. | |
| 185 record = propertyGet(dispatchRecordsForInstanceTags, tag); | |
| 186 if (record != null) return patchInstance(obj, record); | |
| 187 interceptor = propertyGet(interceptorsForUncacheableTags, tag); | |
| 188 if (interceptor != null) return interceptor; | |
| 189 | |
| 190 interceptorClass = lookupInterceptor(tag); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 if (interceptorClass == null) { | |
| 195 // This object is not known to Dart. There could be several reasons for | |
| 196 // that, including (but not limited to): | |
| 197 // | |
| 198 // * A bug in native code (hopefully this is caught during development). | |
| 199 // * An unknown DOM object encountered. | |
| 200 // * JavaScript code running in an unexpected context. For example, on | |
| 201 // node.js. | |
| 202 return null; | |
| 203 } | |
| 204 | |
| 205 interceptor = JS('', '#.prototype', interceptorClass); | |
| 206 | |
| 207 var mark = JS('String|Null', '#[0]', tag); | |
| 208 | |
| 209 if (mark == INSTANCE_CACHED_MARK) { | |
| 210 record = makeLeafDispatchRecord(interceptor); | |
| 211 propertySet(dispatchRecordsForInstanceTags, tag, record); | |
| 212 return patchInstance(obj, record); | |
| 213 } | |
| 214 | |
| 215 if (mark == UNCACHED_MARK) { | |
| 216 propertySet(interceptorsForUncacheableTags, tag, interceptor); | |
| 217 return interceptor; | |
| 218 } | |
| 219 | |
| 220 if (mark == LEAF_MARK) { | |
| 221 return patchProto(obj, makeLeafDispatchRecord(interceptor)); | |
| 222 } | |
| 223 | |
| 224 if (mark == INTERIOR_MARK) { | |
| 225 return patchInteriorProto(obj, interceptor); | |
| 226 } | |
| 227 | |
| 228 if (mark == DISCRIMINATED_MARK) { | |
| 229 // TODO(sra): Use discriminator of tag. | |
| 230 throw new UnimplementedError(tag); | |
| 231 } | |
| 232 | |
| 233 // [tag] was not explicitly an interior or leaf tag, so | |
| 234 var isLeaf = JS('bool', '(#[#]) === true', leafTags, tag); | |
| 235 if (isLeaf) { | |
| 236 return patchProto(obj, makeLeafDispatchRecord(interceptor)); | |
| 237 } else { | |
| 238 return patchInteriorProto(obj, interceptor); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 patchInstance(obj, record) { | |
| 243 setDispatchProperty(obj, record); | |
| 244 return dispatchRecordInterceptor(record); | |
| 245 } | |
| 246 | |
| 247 patchProto(obj, record) { | |
| 248 setDispatchProperty(JS('', 'Object.getPrototypeOf(#)', obj), record); | |
| 249 return dispatchRecordInterceptor(record); | |
| 250 } | |
| 251 | |
| 252 patchInteriorProto(obj, interceptor) { | |
| 253 var proto = JS('', 'Object.getPrototypeOf(#)', obj); | |
| 254 var record = makeDispatchRecord(interceptor, proto, null, null); | |
| 255 setDispatchProperty(proto, record); | |
| 256 return interceptor; | |
| 257 } | |
| 258 | |
| 259 | |
| 260 makeLeafDispatchRecord(interceptor) { | |
| 261 var fieldName = JS_GET_NAME(JsGetName.IS_INDEXABLE_FIELD_NAME); | |
| 262 bool indexability = JS('bool', r'!!#[#]', interceptor, fieldName); | |
| 263 return makeDispatchRecord(interceptor, false, null, indexability); | |
| 264 } | |
| 265 | |
| 266 makeDefaultDispatchRecord(tag, interceptorClass, proto) { | |
| 267 var interceptor = JS('', '#.prototype', interceptorClass); | |
| 268 var isLeaf = JS('bool', '(#[#]) === true', leafTags, tag); | |
| 269 if (isLeaf) { | |
| 270 return makeLeafDispatchRecord(interceptor); | |
| 271 } else { | |
| 272 return makeDispatchRecord(interceptor, proto, null, null); | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 /** | |
| 277 * [proto] should have no shadowing prototypes that are not also assigned a | |
| 278 * dispatch rescord. | |
| 279 */ | |
| 280 setNativeSubclassDispatchRecord(proto, interceptor) { | |
| 281 setDispatchProperty(proto, makeLeafDispatchRecord(interceptor)); | |
| 282 } | |
| 283 | |
| 284 String constructorNameFallback(object) { | |
| 285 return JS('String', '#(#)', _constructorNameFallback, object); | |
| 286 } | |
| 287 | |
| 288 | |
| 289 var initNativeDispatchFlag; // null or true | |
| 290 | |
| 291 void initNativeDispatch() { | |
| 292 if (true == initNativeDispatchFlag) return; | |
| 293 initNativeDispatchFlag = true; | |
| 294 initNativeDispatchContinue(); | |
| 295 } | |
| 296 | |
| 297 void initNativeDispatchContinue() { | |
| 298 | |
| 299 dispatchRecordsForInstanceTags = JS('', 'Object.create(null)'); | |
| 300 interceptorsForUncacheableTags = JS('', 'Object.create(null)'); | |
| 301 | |
| 302 initHooks(); | |
| 303 | |
| 304 // Try to pro-actively patch prototypes of DOM objects. For each of our known | |
| 305 // tags `TAG`, if `window.TAG` is a (constructor) function, set the dispatch | |
| 306 // property if the function's prototype to a dispatch record. | |
| 307 var map = interceptorsByTag; | |
| 308 var tags = JS('JSMutableArray', 'Object.getOwnPropertyNames(#)', map); | |
| 309 | |
| 310 if (JS('bool', 'typeof window != "undefined"')) { | |
| 311 var context = JS('=Object', 'window'); | |
| 312 var fun = JS('=Object', 'function () {}'); | |
| 313 for (int i = 0; i < tags.length; i++) { | |
| 314 var tag = tags[i]; | |
| 315 var proto = prototypeForTagFunction(tag); | |
| 316 if (proto != null) { | |
| 317 var interceptorClass = JS('', '#[#]', map, tag); | |
| 318 var record = makeDefaultDispatchRecord(tag, interceptorClass, proto); | |
| 319 if (record != null) { | |
| 320 setDispatchProperty(proto, record); | |
| 321 // Ensure the modified prototype is still fast by assigning it to | |
| 322 // the prototype property of a function object. | |
| 323 JS('', '#.prototype = #', fun, proto); | |
| 324 } | |
| 325 } | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 // [interceptorsByTag] maps 'plain' dispatch tags. Add all the derived | |
| 330 // dispatch tags to simplify lookup of derived tags. | |
| 331 for (int i = 0; i < tags.length; i++) { | |
| 332 var tag = JS('String', '#[#]', tags, i); | |
| 333 if (JS('bool', '/^[A-Za-z_]/.test(#)', tag)) { | |
| 334 var interceptorClass = propertyGet(map, tag); | |
| 335 propertySet(map, INSTANCE_CACHED_MARK + tag, interceptorClass); | |
| 336 propertySet(map, UNCACHED_MARK + tag, interceptorClass); | |
| 337 propertySet(map, LEAF_MARK + tag, interceptorClass); | |
| 338 propertySet(map, INTERIOR_MARK + tag, interceptorClass); | |
| 339 propertySet(map, DISCRIMINATED_MARK + tag, interceptorClass); | |
| 340 } | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 | |
| 345 /** | |
| 346 * Initializes [getTagFunction] and [alternateTagFunction]. | |
| 347 * | |
| 348 * These functions are 'hook functions', collectively 'hooks'. They initialized | |
| 349 * by applying a series of hooks transformers. Built-in hooks transformers deal | |
| 350 * with various known browser behaviours. | |
| 351 * | |
| 352 * Each hook tranformer takes a 'hooks' input which is a JavaScript object | |
| 353 * containing the hook functions, and returns the same or a new object with | |
| 354 * replacements. The replacements can wrap the originals to provide alternate | |
| 355 * or modified behaviour. | |
| 356 * | |
| 357 * { getTag: function(obj) {...}, | |
| 358 * getUnknownTag: function(obj, tag) {...}, | |
| 359 * prototypeForTag: function(tag) {...}, | |
| 360 * discriminator: function(tag) {...}, | |
| 361 * } | |
| 362 * | |
| 363 * * getTag(obj) returns the dispatch tag, or `null`. | |
| 364 * * getUnknownTag(obj, tag) returns a tag when [getTag] fails. | |
| 365 * * prototypeForTag(tag) returns the prototype of the constructor for tag, | |
| 366 * or `null` if not available or prepatching is undesirable. | |
| 367 * * discriminator(tag) returns a function TBD. | |
| 368 * | |
| 369 * The web site can adapt a dart2js application by loading code ahead of the | |
| 370 * dart2js application that defines hook transformers to be after the built in | |
| 371 * ones. Code defining a transformer HT should use the following pattern to | |
| 372 * ensure multiple transformers can be composed: | |
| 373 * | |
| 374 * (dartNativeDispatchHooksTransformer = | |
| 375 * window.dartNativeDispatchHooksTransformer || []).push(HT); | |
| 376 * | |
| 377 * | |
| 378 * TODO: Implement and describe dispatch tags and their caching methods. | |
| 379 */ | |
| 380 void initHooks() { | |
| 381 // The initial simple hooks: | |
| 382 var hooks = JS('', '#()', _baseHooks); | |
| 383 | |
| 384 // Customize for browsers where `object.constructor.name` fails: | |
| 385 var _fallbackConstructorHooksTransformer = | |
| 386 JS('', '#(#)', _fallbackConstructorHooksTransformerGenerator, | |
| 387 _constructorNameFallback); | |
| 388 hooks = applyHooksTransformer(_fallbackConstructorHooksTransformer, hooks); | |
| 389 | |
| 390 // Customize for browsers: | |
| 391 hooks = applyHooksTransformer(_firefoxHooksTransformer, hooks); | |
| 392 hooks = applyHooksTransformer(_ieHooksTransformer, hooks); | |
| 393 hooks = applyHooksTransformer(_operaHooksTransformer, hooks); | |
| 394 hooks = applyHooksTransformer(_safariHooksTransformer, hooks); | |
| 395 | |
| 396 hooks = applyHooksTransformer(_fixDocumentHooksTransformer, hooks); | |
| 397 | |
| 398 // TODO(sra): Update ShadowDOM polyfil to use | |
| 399 // [dartNativeDispatchHooksTransformer] and remove this hook. | |
| 400 hooks = applyHooksTransformer(_dartExperimentalFixupGetTagHooksTransformer, | |
| 401 hooks); | |
| 402 | |
| 403 // Apply global hooks. | |
| 404 // | |
| 405 // If defined, dartNativeDispatchHookdTransformer should be a single function | |
| 406 // of a JavaScript Array of functions. | |
| 407 | |
| 408 if (JS('bool', 'typeof dartNativeDispatchHooksTransformer != "undefined"')) { | |
| 409 var transformers = JS('', 'dartNativeDispatchHooksTransformer'); | |
| 410 if (JS('bool', 'typeof # == "function"', transformers)) { | |
| 411 transformers = [transformers]; | |
| 412 } | |
| 413 if (JS('bool', '#.constructor == Array', transformers)) { | |
| 414 for (int i = 0; i < JS('int', '#.length', transformers); i++) { | |
| 415 var transformer = JS('', '#[#]', transformers, i); | |
| 416 if (JS('bool', 'typeof # == "function"', transformer)) { | |
| 417 hooks = applyHooksTransformer(transformer, hooks); | |
| 418 } | |
| 419 } | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 var getTag = JS('', '#.getTag', hooks); | |
| 424 var getUnknownTag = JS('', '#.getUnknownTag', hooks); | |
| 425 var prototypeForTag = JS('', '#.prototypeForTag', hooks); | |
| 426 | |
| 427 getTagFunction = (o) => JS('String|Null', '#(#)', getTag, o); | |
| 428 alternateTagFunction = | |
| 429 (o, String tag) => JS('String|Null', '#(#, #)', getUnknownTag, o, tag); | |
| 430 prototypeForTagFunction = | |
| 431 (String tag) => JS('', '#(#)', prototypeForTag, tag); | |
| 432 } | |
| 433 | |
| 434 applyHooksTransformer(transformer, hooks) { | |
| 435 var newHooks = JS('=Object|Null', '#(#)', transformer, hooks); | |
| 436 return JS('', '# || #', newHooks, hooks); | |
| 437 } | |
| 438 | |
| 439 // JavaScript code fragments. | |
| 440 // | |
| 441 // This is a temporary place for the JavaScript code. | |
| 442 // | |
| 443 // TODO(sra): These code fragments are not minified. They could be generated by | |
| 444 // the code emitter, or JS_CONST could be improved to parse entire functions and | |
| 445 // take care of the minification. | |
| 446 | |
| 447 const _baseHooks = const JS_CONST(r''' | |
| 448 function() { | |
| 449 function typeNameInChrome(o) { | |
| 450 var constructor = o.constructor; | |
| 451 if (constructor) { | |
| 452 var name = constructor.name; | |
| 453 if (name) return name; | |
| 454 } | |
| 455 var s = Object.prototype.toString.call(o); | |
| 456 return s.substring(8, s.length - 1); | |
| 457 } | |
| 458 function getUnknownTag(object, tag) { | |
| 459 // This code really belongs in [getUnknownTagGenericBrowser] but having it | |
| 460 // here allows [getUnknownTag] to be tested on d8. | |
| 461 if (/^HTML[A-Z].*Element$/.test(tag)) { | |
| 462 // Check that it is not a simple JavaScript object. | |
| 463 var name = Object.prototype.toString.call(object); | |
| 464 if (name == "[object Object]") return null; | |
| 465 return "HTMLElement"; | |
| 466 } | |
| 467 } | |
| 468 function getUnknownTagGenericBrowser(object, tag) { | |
| 469 if (self.HTMLElement && object instanceof HTMLElement) return "HTMLElement"; | |
| 470 return getUnknownTag(object, tag); | |
| 471 } | |
| 472 function prototypeForTag(tag) { | |
| 473 if (typeof window == "undefined") return null; | |
| 474 if (typeof window[tag] == "undefined") return null; | |
| 475 var constructor = window[tag]; | |
| 476 if (typeof constructor != "function") return null; | |
| 477 return constructor.prototype; | |
| 478 } | |
| 479 function discriminator(tag) { return null; } | |
| 480 | |
| 481 var isBrowser = typeof navigator == "object"; | |
| 482 | |
| 483 return { | |
| 484 getTag: typeNameInChrome, | |
| 485 getUnknownTag: isBrowser ? getUnknownTagGenericBrowser : getUnknownTag, | |
| 486 prototypeForTag: prototypeForTag, | |
| 487 discriminator: discriminator }; | |
| 488 }'''); | |
| 489 | |
| 490 | |
| 491 /** | |
| 492 * Returns the name of the constructor function for browsers where | |
| 493 * `object.constructor.name` is not reliable. | |
| 494 * | |
| 495 * This function is split out of [_fallbackConstructorHooksTransformerGenerator] | |
| 496 * as it is called from both the dispatch hooks and via | |
| 497 * [constructorNameFallback] from objectToString. | |
| 498 */ | |
| 499 const _constructorNameFallback = const JS_CONST(r''' | |
| 500 function getTagFallback(o) { | |
| 501 var constructor = o.constructor; | |
| 502 if (typeof constructor == "function") { | |
| 503 var name = constructor.name; | |
| 504 // If the name is a non-empty string, we use that as the type name of this | |
| 505 // object. There are various cases where that does not work, so we have to | |
| 506 // detect them and fall through to the toString() based implementation. | |
| 507 | |
| 508 if (typeof name == "string" && | |
| 509 | |
| 510 // Sometimes the string is empty. This test also catches minified | |
| 511 // shadow dom polyfil wrapper for Window on Firefox where the faked | |
| 512 // constructor name does not 'stick'. The shortest real DOM object | |
| 513 // names have three characters (e.g. URL, CSS). | |
| 514 name.length > 2 && | |
| 515 | |
| 516 // On Firefox we often get "Object" as the constructor name, even for | |
| 517 // more specialized DOM objects. | |
| 518 name !== "Object" && | |
| 519 | |
| 520 // This can happen in Opera. | |
| 521 name !== "Function.prototype") { | |
| 522 return name; | |
| 523 } | |
| 524 } | |
| 525 var s = Object.prototype.toString.call(o); | |
| 526 return s.substring(8, s.length - 1); | |
| 527 }'''); | |
| 528 | |
| 529 | |
| 530 const _fallbackConstructorHooksTransformerGenerator = const JS_CONST(r''' | |
| 531 function(getTagFallback) { | |
| 532 return function(hooks) { | |
| 533 // If we are not in a browser, assume we are in d8. | |
| 534 // TODO(sra): Recognize jsshell. | |
| 535 if (typeof navigator != "object") return hooks; | |
| 536 | |
| 537 var ua = navigator.userAgent; | |
| 538 // TODO(antonm): remove a reference to DumpRenderTree. | |
| 539 if (ua.indexOf("DumpRenderTree") >= 0) return hooks; | |
| 540 if (ua.indexOf("Chrome") >= 0) { | |
| 541 // Confirm constructor name is usable for dispatch. | |
| 542 function confirm(p) { | |
| 543 return typeof window == "object" && window[p] && window[p].name == p; | |
| 544 } | |
| 545 if (confirm("Window") && confirm("HTMLElement")) return hooks; | |
| 546 } | |
| 547 | |
| 548 hooks.getTag = getTagFallback; | |
| 549 }; | |
| 550 }'''); | |
| 551 | |
| 552 | |
| 553 const _ieHooksTransformer = const JS_CONST(r''' | |
| 554 function(hooks) { | |
| 555 var userAgent = typeof navigator == "object" ? navigator.userAgent : ""; | |
| 556 if (userAgent.indexOf("Trident/") == -1) return hooks; | |
| 557 | |
| 558 var getTag = hooks.getTag; | |
| 559 | |
| 560 var quickMap = { | |
| 561 "BeforeUnloadEvent": "Event", | |
| 562 "DataTransfer": "Clipboard", | |
| 563 "HTMLDDElement": "HTMLElement", | |
| 564 "HTMLDTElement": "HTMLElement", | |
| 565 "HTMLPhraseElement": "HTMLElement", | |
| 566 "Position": "Geoposition" | |
| 567 }; | |
| 568 | |
| 569 function getTagIE(o) { | |
| 570 var tag = getTag(o); | |
| 571 var newTag = quickMap[tag]; | |
| 572 if (newTag) return newTag; | |
| 573 // Patches for types which report themselves as Objects. | |
| 574 if (tag == "Object") { | |
| 575 if (window.DataView && (o instanceof window.DataView)) return "DataView"; | |
| 576 } | |
| 577 return tag; | |
| 578 } | |
| 579 | |
| 580 function prototypeForTagIE(tag) { | |
| 581 var constructor = window[tag]; | |
| 582 if (constructor == null) return null; | |
| 583 return constructor.prototype; | |
| 584 } | |
| 585 | |
| 586 hooks.getTag = getTagIE; | |
| 587 hooks.prototypeForTag = prototypeForTagIE; | |
| 588 }'''); | |
| 589 | |
| 590 const _fixDocumentHooksTransformer = const JS_CONST(r''' | |
| 591 function(hooks) { | |
| 592 var getTag = hooks.getTag; | |
| 593 var prototypeForTag = hooks.prototypeForTag; | |
| 594 function getTagFixed(o) { | |
| 595 var tag = getTag(o); | |
| 596 if (tag == "Document") { | |
| 597 // Some browsers and the polymer polyfill call both HTML and XML documents | |
| 598 // "Document", so we check for the xmlVersion property, which is the empty | |
| 599 // string on HTML documents. Since both dart:html classes Document and | |
| 600 // HtmlDocument share the same type, we must patch the instances and not | |
| 601 // the prototype. | |
| 602 if (!!o.xmlVersion) return "!Document"; | |
| 603 return "!HTMLDocument"; | |
| 604 } | |
| 605 return tag; | |
| 606 } | |
| 607 | |
| 608 function prototypeForTagFixed(tag) { | |
| 609 if (tag == "Document") return null; // Do not pre-patch Document. | |
| 610 return prototypeForTag(tag); | |
| 611 } | |
| 612 | |
| 613 hooks.getTag = getTagFixed; | |
| 614 hooks.prototypeForTag = prototypeForTagFixed; | |
| 615 }'''); | |
| 616 | |
| 617 const _firefoxHooksTransformer = const JS_CONST(r''' | |
| 618 function(hooks) { | |
| 619 var userAgent = typeof navigator == "object" ? navigator.userAgent : ""; | |
| 620 if (userAgent.indexOf("Firefox") == -1) return hooks; | |
| 621 | |
| 622 var getTag = hooks.getTag; | |
| 623 | |
| 624 var quickMap = { | |
| 625 "BeforeUnloadEvent": "Event", | |
| 626 "DataTransfer": "Clipboard", | |
| 627 "GeoGeolocation": "Geolocation", | |
| 628 "Location": "!Location", // Fixes issue 18151 | |
| 629 "WorkerMessageEvent": "MessageEvent", | |
| 630 "XMLDocument": "!Document"}; | |
| 631 | |
| 632 function getTagFirefox(o) { | |
| 633 var tag = getTag(o); | |
| 634 return quickMap[tag] || tag; | |
| 635 } | |
| 636 | |
| 637 hooks.getTag = getTagFirefox; | |
| 638 }'''); | |
| 639 | |
| 640 | |
| 641 const _operaHooksTransformer = const JS_CONST(r''' | |
| 642 function(hooks) { return hooks; } | |
| 643 '''); | |
| 644 | |
| 645 | |
| 646 const _safariHooksTransformer = const JS_CONST(r''' | |
| 647 function(hooks) { return hooks; } | |
| 648 '''); | |
| 649 | |
| 650 | |
| 651 const _dartExperimentalFixupGetTagHooksTransformer = const JS_CONST(r''' | |
| 652 function(hooks) { | |
| 653 if (typeof dartExperimentalFixupGetTag != "function") return hooks; | |
| 654 hooks.getTag = dartExperimentalFixupGetTag(hooks.getTag); | |
| 655 }'''); | |
| OLD | NEW |