OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** | |
6 * Helpers for lazy static initialization. | |
7 */ | |
8 var static$uninitialized = {}; | |
9 var static$initializing = {}; | |
10 | |
11 function $maybeBindRtt(fun, fRtt, thisObj) { | |
12 if (fRtt) { | |
13 fun.$lookupRTT = function() { | |
14 return fRtt.call(thisObj); | |
15 } | |
16 } | |
17 } | |
18 | |
19 // Optimized versions of closure bindings. | |
20 // Name convention: $bind<number-of-scopes>_<number-of-arguments>(fn, this, scop
es, args) | |
21 function $bind0_0(fn, fRtt, thisObj) { | |
22 var fun = function() { | |
23 return fn.call(thisObj); | |
24 } | |
25 $maybeBindRtt(fun, fRtt, thisObj); | |
26 return fun; | |
27 } | |
28 function $bind0_1(fn, fRtt, thisObj) { | |
29 var fun = function(arg) { | |
30 return fn.call(thisObj, arg); | |
31 } | |
32 $maybeBindRtt(fun, fRtt, thisObj); | |
33 return fun; | |
34 } | |
35 function $bind0_2(fn, fRtt, thisObj) { | |
36 var fun = function(arg1, arg2) { | |
37 return fn.call(thisObj, arg1, arg2); | |
38 } | |
39 $maybeBindRtt(fun, fRtt, thisObj); | |
40 return fun; | |
41 } | |
42 function $bind0_3(fn, fRtt, thisObj) { | |
43 var fun = function(arg1, arg2, arg3) { | |
44 return fn.call(thisObj, arg1, arg2, arg3); | |
45 } | |
46 $maybeBindRtt(fun, fRtt, thisObj); | |
47 return fun; | |
48 } | |
49 function $bind0_4(fn, fRtt, thisObj) { | |
50 var fun = function(arg1, arg2, arg3, arg4) { | |
51 return fn.call(thisObj, arg1, arg2, arg3, arg4); | |
52 } | |
53 $maybeBindRtt(fun, fRtt, thisObj); | |
54 return fun; | |
55 } | |
56 function $bind0_5(fn, fRtt, thisObj) { | |
57 var fun = function(arg1, arg2, arg3, arg4, arg5) { | |
58 return fn.call(thisObj, arg1, arg2, arg3, arg4, arg5); | |
59 } | |
60 $maybeBindRtt(fun, fRtt, thisObj); | |
61 return fun; | |
62 } | |
63 | |
64 function $bind1_0(fn, fRtt, thisObj, scope) { | |
65 var fun = function() { | |
66 return fn.call(thisObj, scope); | |
67 } | |
68 $maybeBindRtt(fun, fRtt, thisObj); | |
69 return fun; | |
70 } | |
71 function $bind1_1(fn, fRtt, thisObj, scope) { | |
72 var fun = function(arg) { | |
73 return fn.call(thisObj, scope, arg); | |
74 } | |
75 $maybeBindRtt(fun, fRtt, thisObj); | |
76 return fun; | |
77 } | |
78 function $bind1_2(fn, fRtt, thisObj, scope) { | |
79 var fun = function(arg1, arg2) { | |
80 return fn.call(thisObj, scope, arg1, arg2); | |
81 } | |
82 $maybeBindRtt(fun, fRtt, thisObj); | |
83 return fun; | |
84 } | |
85 function $bind1_3(fn, fRtt, thisObj, scope) { | |
86 var fun = function(arg1, arg2, arg3) { | |
87 return fn.call(thisObj, scope, arg1, arg2, arg3); | |
88 } | |
89 $maybeBindRtt(fun, fRtt, thisObj); | |
90 return fun; | |
91 } | |
92 function $bind1_4(fn, fRtt, thisObj, scope) { | |
93 var fun = function(arg1, arg2, arg3, arg4) { | |
94 return fn.call(thisObj, scope, arg1, arg2, arg3, arg4); | |
95 } | |
96 $maybeBindRtt(fun, fRtt, thisObj); | |
97 return fun; | |
98 } | |
99 function $bind1_5(fn, fRtt, thisObj, scope) { | |
100 var fun = function(arg1, arg2, arg3, arg4, arg5) { | |
101 return fn.call(thisObj, scope, arg1, arg2, arg3, arg4, arg5); | |
102 } | |
103 $maybeBindRtt(fun, fRtt, thisObj); | |
104 return fun; | |
105 } | |
106 | |
107 function $bind2_0(fn, fRtt, thisObj, scope1, scope2) { | |
108 var fun = function() { | |
109 return fn.call(thisObj, scope1, scope2); | |
110 } | |
111 $maybeBindRtt(fun, fRtt, thisObj); | |
112 return fun; | |
113 } | |
114 function $bind2_1(fn, fRtt, thisObj, scope1, scope2) { | |
115 var fun = function(arg) { | |
116 return fn.call(thisObj, scope1, scope2, arg); | |
117 } | |
118 $maybeBindRtt(fun, fRtt, thisObj); | |
119 return fun; | |
120 } | |
121 function $bind2_2(fn, fRtt, thisObj, scope1, scope2) { | |
122 var fun = function(arg1, arg2) { | |
123 return fn.call(thisObj, scope1, scope2, arg1, arg2); | |
124 } | |
125 $maybeBindRtt(fun, fRtt, thisObj); | |
126 return fun; | |
127 } | |
128 function $bind2_3(fn, fRtt, thisObj, scope1, scope2) { | |
129 var fun = function(arg1, arg2, arg3) { | |
130 return fn.call(thisObj, scope1, scope2, arg1, arg2, arg3); | |
131 } | |
132 $maybeBindRtt(fun, fRtt, thisObj); | |
133 return fun; | |
134 } | |
135 function $bind2_4(fn, fRtt, thisObj, scope1, scope2) { | |
136 var fun = function(arg1, arg2, arg3, arg4) { | |
137 return fn.call(thisObj, scope1, scope2, arg1, arg2, arg3, arg4); | |
138 } | |
139 $maybeBindRtt(fun, fRtt, thisObj); | |
140 return fun; | |
141 } | |
142 function $bind2_5(fn, fRtt, thisObj, scope1, scope2) { | |
143 var fun = function(arg1, arg2, arg3, arg4, arg5) { | |
144 return fn.call(thisObj, scope1, scope2, arg1, arg2, arg3, arg4, arg5); | |
145 } | |
146 $maybeBindRtt(fun, fRtt, thisObj); | |
147 return fun; | |
148 } | |
149 | |
150 function $bind3_0(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
151 var fun = function() { | |
152 return fn.call(thisObj, scope1, scope2, scope3); | |
153 } | |
154 $maybeBindRtt(fun, fRtt, thisObj); | |
155 return fun; | |
156 } | |
157 function $bind3_1(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
158 var fun = function(arg) { | |
159 return fn.call(thisObj, scope1, scope2, scope3, arg); | |
160 } | |
161 $maybeBindRtt(fun, fRtt, thisObj); | |
162 return fun; | |
163 } | |
164 function $bind3_2(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
165 var fun = function(arg1, arg2) { | |
166 return fn.call(thisObj, scope1, scope2, scope3, arg1, arg2); | |
167 } | |
168 $maybeBindRtt(fun, fRtt, thisObj); | |
169 return fun; | |
170 } | |
171 function $bind3_3(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
172 var fun = function(arg1, arg2, arg3) { | |
173 return fn.call(thisObj, scope1, scope2, scope3, arg1, arg2, arg3); | |
174 } | |
175 $maybeBindRtt(fun, fRtt, thisObj); | |
176 return fun; | |
177 } | |
178 function $bind3_4(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
179 var fun = function(arg1, arg2, arg3, arg4) { | |
180 return fn.call(thisObj, scope1, scope2, scope3, arg1, arg2, arg3, arg4); | |
181 } | |
182 $maybeBindRtt(fun, fRtt, thisObj); | |
183 return fun; | |
184 } | |
185 function $bind3_5(fn, fRtt, thisObj, scope1, scope2, scope3) { | |
186 var fun = function(arg1, arg2, arg3, arg4, arg5) { | |
187 return fn.call(thisObj, scope1, scope2, scope3, arg1, arg2, arg3, arg4, arg5
); | |
188 } | |
189 $maybeBindRtt(fun, fRtt, thisObj); | |
190 return fun; | |
191 } | |
192 | |
193 /** | |
194 * Implements extends for dart classes on javascript prototypes. | |
195 * @param {Function} child | |
196 * @param {Function} parent | |
197 */ | |
198 function $inherits(child, parent) { | |
199 if (child.prototype.__proto__) { | |
200 child.prototype.__proto__ = parent.prototype; | |
201 } else { | |
202 function tmp() {}; | |
203 tmp.prototype = parent.prototype; | |
204 child.prototype = new tmp(); | |
205 child.prototype.constructor = child; | |
206 } | |
207 } | |
208 | |
209 /** | |
210 * @param {Function} fn | |
211 * @param {Function=} fRtt | |
212 * @param {Object|undefined} thisObj | |
213 * @param {...*} var_args | |
214 */ | |
215 function $bind(fn, fRtt, thisObj, var_args) { | |
216 var func; | |
217 if (arguments.length > 3) { | |
218 var boundArgs = Array.prototype.slice.call(arguments, 3); | |
219 func = function() { | |
220 // Prepend the bound arguments to the current arguments. | |
221 var newArgs = Array.prototype.slice.call(arguments); | |
222 Array.prototype.unshift.apply(newArgs, boundArgs); | |
223 return fn.apply(thisObj, newArgs); | |
224 }; | |
225 } else { | |
226 func = function() { | |
227 return fn.apply(thisObj, arguments); | |
228 }; | |
229 } | |
230 if(fRtt) { | |
231 func.$lookupRTT = function() { | |
232 return fRtt.apply(thisObj, arguments); | |
233 }; | |
234 } | |
235 return func; | |
236 } | |
237 | |
238 /** | |
239 * Dart null object that should be used by JS implementation to test for | |
240 * Dart null. | |
241 * | |
242 * TODO(ngeoffray): update dartc to generate this variable instead of | |
243 * undefined. | |
244 * @const | |
245 */ | |
246 var $Dart$Null = void 0; | |
247 | |
248 function assert(expr) { | |
249 var val = typeof(expr) == 'function' ? $dartcall(expr, []) : expr; | |
250 if (val !== true) { | |
251 $Dart$ThrowException(native_ExceptionHelper_createAssertionError()); | |
252 } | |
253 } | |
254 | |
255 function BIT_OR$operator(val1, val2) { | |
256 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
257 ? val1 | val2 | |
258 : val1.BIT_OR$operator(val2); | |
259 } | |
260 | |
261 function BIT_XOR$operator(val1, val2) { | |
262 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
263 ? val1 ^ val2 | |
264 : val1.BIT_XOR$operator(val2); | |
265 } | |
266 | |
267 function BIT_AND$operator(val1, val2) { | |
268 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
269 ? val1 & val2 | |
270 : val1.BIT_AND$operator(val2); | |
271 } | |
272 | |
273 function BIT_NOT$operator(val) { | |
274 return (typeof(val) == 'number') ? ~val : val.BIT_NOT$operator(); | |
275 } | |
276 | |
277 function SHL$operator(val1, val2) { | |
278 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
279 ? val1 << val2 | |
280 : val1.SHL$operator(val2); | |
281 } | |
282 | |
283 function SAR$operator(val1, val2) { | |
284 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
285 ? val1 >> val2 | |
286 : val1.SAR$operator(val2); | |
287 } | |
288 | |
289 function SHR$operator(val1, val2) { | |
290 return val1.SHR$operator(val2); | |
291 } | |
292 | |
293 function ADD$operator(val1, val2) { | |
294 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
295 ? val1 + val2 | |
296 : val1.ADD$operator(val2); | |
297 } | |
298 | |
299 function SUB$operator(val1, val2) { | |
300 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
301 ? val1 - val2 | |
302 : val1.SUB$operator(val2); | |
303 } | |
304 | |
305 function MUL$operator(val1, val2) { | |
306 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
307 ? val1 * val2 | |
308 : val1.MUL$operator(val2); | |
309 } | |
310 | |
311 function DIV$operator(val1, val2) { | |
312 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
313 ? val1 / val2 | |
314 : val1.DIV$operator(val2); | |
315 } | |
316 | |
317 function MOD$operator(val1, val2) { | |
318 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
319 ? number$euclideanModulo(val1, val2) | |
320 : val1.MOD$operator(val2); | |
321 } | |
322 | |
323 function TRUNC$operator(val1, val2) { | |
324 if (typeof(val1) == 'number' && typeof(val2) == 'number') { | |
325 var tmp = val1 / val2; | |
326 return (tmp < 0) ? Math.ceil(tmp) : Math.floor(tmp); | |
327 } else { | |
328 return val1.TRUNC$operator(val2); | |
329 } | |
330 } | |
331 | |
332 function negate$operator(val) { | |
333 return (typeof(val) == 'number') ? -val : val.negate$operator(); | |
334 } | |
335 | |
336 function EQ$operator(val1, val2) { | |
337 return (typeof val1 != 'object') | |
338 ? val1 === val2 | |
339 : val1.EQ$operator(val2); | |
340 } | |
341 | |
342 function NE$operator(val1, val2) { | |
343 return (typeof val1 != 'object') | |
344 ? val1 !== val2 | |
345 : !val1.EQ$operator(val2); | |
346 } | |
347 | |
348 function LT$operator(val1, val2) { | |
349 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
350 ? val1 < val2 | |
351 : val1.LT$operator(val2); | |
352 } | |
353 | |
354 function GT$operator(val1, val2) { | |
355 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
356 ? val1 > val2 | |
357 : val1.GT$operator(val2); | |
358 } | |
359 | |
360 function LTE$operator(val1, val2) { | |
361 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
362 ? val1 <= val2 | |
363 : val1.LTE$operator(val2); | |
364 } | |
365 | |
366 function GTE$operator(val1, val2) { | |
367 return (typeof(val1) == 'number' && typeof(val2) == 'number') | |
368 ? val1 >= val2 | |
369 : val1.GTE$operator(val2); | |
370 } | |
371 | |
372 // The following operator-functions are not called from Dart-generated code, but | |
373 // only from handwritten JS code. | |
374 function INDEX$operator(obj, index) { | |
375 return obj.INDEX$operator(index); | |
376 } | |
377 | |
378 function ASSIGN_INDEX$operator(obj, index, newVal) { | |
379 obj.ASSIGN_INDEX$operator(index, newVal); | |
380 } | |
381 | |
382 function $Dart$ThrowException(e) { | |
383 // If e is not a value, we can use V8's captureStackTrace utility method. | |
384 if (e && (typeof e == "object") && Error.captureStackTrace) { | |
385 Error.captureStackTrace(e); | |
386 } | |
387 throw e; | |
388 } | |
389 | |
390 function $toString(x) { | |
391 return native__StringJsUtil_toDartString(x); | |
392 } | |
393 | |
394 // Translate a JavaScript exception to a Dart exception | |
395 // TODO(zundel): cross browser support. This is Chrome specific. | |
396 function $transformBrowserException(e) { | |
397 if (e instanceof TypeError) { | |
398 switch(e.type) { | |
399 case "property_not_function": | |
400 case "called_non_callable": | |
401 if (e.arguments[0] == "undefined") { | |
402 return native_ExceptionHelper_createNullPointerException(); | |
403 } | |
404 return native_ExceptionHelper_createObjectNotClosureException(); | |
405 case "non_object_property_call": | |
406 case "non_object_property_load": | |
407 return native_ExceptionHelper_createNullPointerException(); | |
408 case "undefined_method": | |
409 if (e.arguments[0] == "call" || e.arguments[0] == "apply") { | |
410 return native_ExceptionHelper_createObjectNotClosureException(); | |
411 } | |
412 return native_ExceptionHelper_createNoSuchMethodException( | |
413 "", e.arguments[0], []); | |
414 } | |
415 } | |
416 return e; | |
417 } | |
418 | |
419 // Throws a NoSuchMethodException (used by named-parameter trampolines). | |
420 function $nsme() { | |
421 var e = native_ExceptionHelper_createNoSuchMethodException("", "", []); | |
422 $Dart$ThrowException(e); | |
423 } | |
424 | |
425 // Throws a NoSuchMethodException (used when instantiating via a non-existent cl
ass or ctor). | |
426 function $nsme2(name, args) { | |
427 var e = native_ExceptionHelper_createNoSuchMethodException(name, name, args); | |
428 $Dart$ThrowException(e); | |
429 } | |
430 | |
431 // Shared named-argument object used by call-sites with no named arguments. | |
432 /** @const */ | |
433 var $noargs = {count:0}; | |
434 | |
435 // Used for invoking dart functions from js. | |
436 function $dartcall(fn, args) { | |
437 args.unshift(args.length, $noargs); | |
438 fn.apply(null, args); | |
439 } | |
440 | |
441 // | |
442 // The following methods are used to create canonical constants. | |
443 // | |
444 | |
445 function native_ConstHelper_getConstId(o) { | |
446 return $dart_const_id(o); | |
447 } | |
448 | |
449 // compile time const canonicalization helpers | |
450 function $dart_const_id(o) { | |
451 if (o === $Dart$Null) return ""; | |
452 if (typeof o === "number") return "n" + o; | |
453 if (typeof o === "boolean") return "b" + ((o) ? 1 : 0); | |
454 if (typeof o === "string") return $dart_const_string_id(o); | |
455 if (typeof o === "function") throw "a function is not a constant expression"; | |
456 var result = o.$dartConstId; | |
457 if (result === undefined) { | |
458 throw "internal error: reference to non-canonical constant"; | |
459 } | |
460 return result; | |
461 } | |
462 | |
463 // Array ids have the form: "aID,ID,ID" | |
464 function $dart_const_array_id(o) { | |
465 var ids = []; | |
466 for (var i=o.length-1; i>=0; i--) { | |
467 ids.push($dart_const_id(o[i])); | |
468 } | |
469 return "a" + ids.join(","); | |
470 } | |
471 | |
472 var $CONST_MAP_PREFIX = ":" | |
473 | |
474 // String ids have the form "sID" | |
475 var $string_id = 0; | |
476 var $string_id_cache = {}; | |
477 function $dart_const_string_id(s) { | |
478 var key = $CONST_MAP_PREFIX + s; | |
479 var id = $string_id_cache[key]; | |
480 if (!id) { | |
481 id = "s" + (++$string_id); | |
482 $string_id_cache[key] = id; | |
483 } | |
484 return id; | |
485 } | |
486 | |
487 // A place to store the canonical consts | |
488 var $consts = {}; | |
489 | |
490 function $isDartMap(o) { | |
491 return !!(o && o.$implements$Map$Dart); | |
492 } | |
493 | |
494 // Intern const object "o" | |
495 function $intern(o, type_args) { | |
496 var id; | |
497 // Maps and arrays need special handling | |
498 // TODO(johnlenz): This array check may not be sufficient across iframes. | |
499 if (o instanceof Array) { | |
500 // Dart array literals are implemented as JavaScript native arrays. | |
501 id = $dart_const_array_id(o); | |
502 } else if ($isDartMap(o)) { | |
503 // Dart map literals are currently implemented by a non-const Dart class. | |
504 id = native_ConstHelper_getConstMapId(o); | |
505 } else { | |
506 id = "o" + o.$const_id(); | |
507 } | |
508 if (type_args != null) { | |
509 id += '<'; | |
510 for (var i=type_args.length-1; i >= 0; i--) { | |
511 id += type_args[i]; | |
512 id += "," | |
513 } | |
514 id += '>'; | |
515 } | |
516 var key = $CONST_MAP_PREFIX + id; | |
517 var match = $consts[key]; | |
518 if (match != null) { | |
519 return match; | |
520 } | |
521 o.$dartConstId = id; | |
522 $consts[key] = o; | |
523 return o; | |
524 } | |
525 | |
526 function $Dart$MapLiteralFactory() { | |
527 return native__CoreJsUtil__newMapLiteral(); | |
528 } | |
OLD | NEW |