-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathruntime.js
543 lines (484 loc) · 15.7 KB
/
runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Copyright 2012 Traceur Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* The traceur runtime.
*/
(function(global) {
'use strict';
if (global.$traceurRuntime) {
// Prevents from being executed multiple times.
return;
}
var $create = Object.create;
var $defineProperty = Object.defineProperty;
var $defineProperties = Object.defineProperties;
var $freeze = Object.freeze;
var $getOwnPropertyNames = Object.getOwnPropertyNames;
var $getPrototypeOf = Object.getPrototypeOf;
var $hasOwnProperty = Object.prototype.hasOwnProperty;
var $getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
function nonEnum(value) {
return {
configurable: true,
enumerable: false,
value: value,
writable: true
};
}
var method = nonEnum;
function polyfillString(String) {
// Harmony String Extras
// http://wiki.ecmascript.org/doku.php?id=harmony:string_extras
$defineProperties(String.prototype, {
startsWith: method(function(s) {
return this.lastIndexOf(s, 0) === 0;
}),
endsWith: method(function(s) {
var t = String(s);
var l = this.length - t.length;
return l >= 0 && this.indexOf(t, l) === l;
}),
contains: method(function(s) {
return this.indexOf(s) !== -1;
}),
toArray: method(function() {
return this.split('');
}),
codePointAt: method(function(position) {
/*! http://mths.be/codepointat v0.1.0 by @mathias */
var string = String(this);
var size = string.length;
// `ToInteger`
var index = position ? Number(position) : 0;
if (isNaN(index)) {
index = 0;
}
// Account for out-of-bounds indices:
if (index < 0 || index >= size) {
return undefined;
}
// Get the first code unit
var first = string.charCodeAt(index);
var second;
if ( // check if it’s the start of a surrogate pair
first >= 0xD800 && first <= 0xDBFF && // high surrogate
size > index + 1 // there is a next code unit
) {
second = string.charCodeAt(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
}
return first;
})
});
$defineProperties(String, {
// 21.1.2.4 String.raw(callSite, ...substitutions)
raw: method(function(callsite) {
var raw = callsite.raw;
var len = raw.length >>> 0; // ToUint
if (len === 0)
return '';
var s = '';
var i = 0;
while (true) {
s += raw[i];
if (i + 1 === len)
return s;
s += arguments[++i];
}
}),
// 21.1.2.2 String.fromCodePoint(...codePoints)
fromCodePoint: method(function() {
// http://mths.be/fromcodepoint v0.1.0 by @mathias
var codeUnits = [];
var floor = Math.floor;
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
}
return String.fromCharCode.apply(null, codeUnits);
})
});
}
// ### Symbols
//
// Symbols are emulated using an object which is an instance of SymbolValue.
// Calling Symbol as a function returns a symbol value object.
//
// If options.symbols is enabled then all property accesses are transformed
// into runtime calls which uses the internal string as the real property
// name.
//
// If options.symbols is disabled symbols just toString as their internal
// representation, making them work but leak as enumerable properties.
var counter = 0;
/**
* Generates a new unique string.
* @return {string}
*/
function newUniqueString() {
return '__$' + Math.floor(Math.random() * 1e9) + '$' + ++counter + '$__';
}
// The string used for the real property.
var symbolInternalProperty = newUniqueString();
var symbolDescriptionProperty = newUniqueString();
// Used for the Symbol wrapper
var symbolDataProperty = newUniqueString();
// All symbol values are kept in this map. This is so that we can get back to
// the symbol object if all we have is the string key representing the symbol.
var symbolValues = Object.create(null);
function isSymbol(symbol) {
return typeof symbol === 'object' && symbol instanceof SymbolValue;
}
function typeOf(v) {
if (isSymbol(v))
return 'symbol';
return typeof v;
}
/**
* Creates a new unique symbol object.
* @param {string=} string Optional string used for toString.
* @constructor
*/
function Symbol(description) {
var value = new SymbolValue(description);
if (!(this instanceof Symbol))
return value;
// new Symbol should throw.
//
// There are two ways to get a wrapper to a symbol. Either by doing
// Object(symbol) or call a non strict function using a symbol value as
// this. To correctly handle these two would require a lot of work for very
// little gain so we are not doing those at the moment.
throw new TypeError('Symbol cannot be new\'ed');
}
$defineProperty(Symbol.prototype, 'constructor', nonEnum(Symbol));
$defineProperty(Symbol.prototype, 'toString', method(function() {
var symbolValue = this[symbolDataProperty];
if (!getOption('symbols'))
return symbolValue[symbolInternalProperty];
if (!symbolValue)
throw TypeError('Conversion from symbol to string');
var desc = symbolValue[symbolDescriptionProperty];
if (desc === undefined)
desc = '';
return 'Symbol(' + desc + ')';
}));
$defineProperty(Symbol.prototype, 'valueOf', method(function() {
var symbolValue = this[symbolDataProperty];
if (!symbolValue)
throw TypeError('Conversion from symbol to string');
if (!getOption('symbols'))
return symbolValue[symbolInternalProperty];
return symbolValue;
}));
function SymbolValue(description) {
var key = newUniqueString();
$defineProperty(this, symbolDataProperty, {value: this});
$defineProperty(this, symbolInternalProperty, {value: key});
$defineProperty(this, symbolDescriptionProperty, {value: description});
$freeze(this);
symbolValues[key] = this;
}
$defineProperty(SymbolValue.prototype, 'constructor', nonEnum(Symbol));
$defineProperty(SymbolValue.prototype, 'toString', {
value: Symbol.prototype.toString,
enumerable: false
});
$defineProperty(SymbolValue.prototype, 'valueOf', {
value: Symbol.prototype.valueOf,
enumerable: false
});
$freeze(SymbolValue.prototype);
Symbol.iterator = Symbol();
function toProperty(name) {
if (isSymbol(name))
return name[symbolInternalProperty];
return name;
}
// Override getOwnPropertyNames to filter out private name keys.
function getOwnPropertyNames(object) {
var rv = [];
var names = $getOwnPropertyNames(object);
for (var i = 0; i < names.length; i++) {
var name = names[i];
if (!symbolValues[name])
rv.push(name);
}
return rv;
}
function getOwnPropertyDescriptor(object, name) {
return $getOwnPropertyDescriptor(object, toProperty(name));
}
function getOwnPropertySymbols(object) {
var rv = [];
var names = $getOwnPropertyNames(object);
for (var i = 0; i < names.length; i++) {
var symbol = symbolValues[names[i]];
if (symbol)
rv.push(symbol);
}
return rv;
}
// Override Object.prototpe.hasOwnProperty to always return false for
// private names.
function hasOwnProperty(name) {
return $hasOwnProperty.call(this, toProperty(name));
}
function getOption(name) {
return global.traceur && global.traceur.options[name];
}
function setProperty(object, name, value) {
var sym, desc;
if (isSymbol(name)) {
sym = name;
name = name[symbolInternalProperty];
}
object[name] = value;
if (sym && (desc = $getOwnPropertyDescriptor(object, name)))
$defineProperty(object, name, {enumerable: false});
return value;
}
function defineProperty(object, name, descriptor) {
if (isSymbol(name)) {
// Symbols should not be enumerable. We need to create a new descriptor
// before calling the original defineProperty because the property might
// be made non configurable.
if (descriptor.enumerable) {
descriptor = Object.create(descriptor, {
enumerable: {value: false}
});
}
name = name[symbolInternalProperty];
}
$defineProperty(object, name, descriptor);
return object;
}
function polyfillObject(Object) {
$defineProperty(Object, 'defineProperty', {value: defineProperty});
$defineProperty(Object, 'getOwnPropertyNames',
{value: getOwnPropertyNames});
$defineProperty(Object, 'getOwnPropertyDescriptor',
{value: getOwnPropertyDescriptor});
$defineProperty(Object.prototype, 'hasOwnProperty',
{value: hasOwnProperty});
Object.getOwnPropertySymbols = getOwnPropertySymbols;
// Object.is
// Unlike === this returns true for (NaN, NaN) and false for (0, -0).
function is(left, right) {
if (left === right)
return left !== 0 || 1 / left === 1 / right;
return left !== left && right !== right;
}
$defineProperty(Object, 'is', method(is));
// Object.assign (19.1.3.1)
function assign(target, source) {
var props = $getOwnPropertyNames(source);
var p, length = props.length;
for (p = 0; p < length; p++) {
target[props[p]] = source[props[p]];
}
return target;
}
$defineProperty(Object, 'assign', method(assign));
// Object.mixin (19.1.3.15)
function mixin(target, source) {
var props = $getOwnPropertyNames(source);
var p, descriptor, length = props.length;
for (p = 0; p < length; p++) {
descriptor = $getOwnPropertyDescriptor(source, props[p]);
$defineProperty(target, props[p], descriptor);
}
return target;
}
$defineProperty(Object, 'mixin', method(mixin));
}
function polyfillArray(Array) {
// Make arrays iterable.
// TODO(arv): This is not very robust to changes in the private names
// option but fortunately this is not something that is expected to change
// at runtime outside of tests.
defineProperty(Array.prototype, Symbol.iterator, method(function() {
var index = 0;
var array = this;
return {
next: function() {
if (index < array.length) {
return {value: array[index++], done: false};
}
return {value: undefined, done: true};
}
};
}));
}
/**
* @param {Function} canceller
* @constructor
*/
function Deferred(canceller) {
this.canceller_ = canceller;
this.listeners_ = [];
}
function notify(self) {
while (self.listeners_.length > 0) {
var current = self.listeners_.shift();
var currentResult = undefined;
try {
try {
if (self.result_[1]) {
if (current.errback)
currentResult = current.errback.call(undefined, self.result_[0]);
} else {
if (current.callback)
currentResult = current.callback.call(undefined, self.result_[0]);
}
current.deferred.callback(currentResult);
} catch (err) {
current.deferred.errback(err);
}
} catch (unused) {}
}
}
function fire(self, value, isError) {
if (self.fired_)
throw new Error('already fired');
self.fired_ = true;
self.result_ = [value, isError];
notify(self);
}
Deferred.prototype = {
constructor: Deferred,
fired_: false,
result_: undefined,
createPromise: function() {
return {then: this.then.bind(this), cancel: this.cancel.bind(this)};
},
callback: function(value) {
fire(this, value, false);
},
errback: function(err) {
fire(this, err, true);
},
then: function(callback, errback) {
var result = new Deferred(this.cancel.bind(this));
this.listeners_.push({
deferred: result,
callback: callback,
errback: errback
});
if (this.fired_)
notify(this);
return result.createPromise();
},
cancel: function() {
if (this.fired_)
throw new Error('already finished');
var result;
if (this.canceller_) {
result = this.canceller_(this);
if (!result instanceof Error)
result = new Error(result);
} else {
result = new Error('cancelled');
}
if (!this.fired_) {
this.result_ = [result, true];
notify(this);
}
}
};
// System.get/set and @traceur/module gets overridden in @traceur/modules to
// be more correct.
function ModuleImpl(url, func, self) {
this.url = url;
this.func = func;
this.self = self;
this.value_ = null;
}
ModuleImpl.prototype = {
get value() {
if (this.value_)
return this.value_;
return this.value_ = this.func.call(this.self);
}
};
var modules = {
'@traceur/module': {
ModuleImpl: ModuleImpl,
registerModule: function(url, func, self) {
modules[url] = new ModuleImpl(url, func, self);
},
getModuleImpl: function(url) {
return modules[url].value;
}
}
};
var System = {
get: function(name) {
var module = modules[name];
if (module instanceof ModuleImpl)
return modules[name] = module.value;
return module;
},
set: function(name, object) {
modules[name] = object;
}
};
function setupGlobals(global) {
if (!global.Symbol)
global.Symbol = Symbol;
if (!global.Symbol.iterator)
global.Symbol.iterator = Symbol();
polyfillString(global.String);
polyfillObject(global.Object);
polyfillArray(global.Array);
global.System = System;
// TODO(arv): Don't export this.
global.Deferred = Deferred;
}
setupGlobals(global);
// This file is sometimes used without traceur.js so make it a new global.
global.$traceurRuntime = {
Deferred: Deferred,
setProperty: setProperty,
setupGlobals: setupGlobals,
toProperty: toProperty,
typeof: typeOf,
};
})(typeof global !== 'undefined' ? global : this);