-
Notifications
You must be signed in to change notification settings - Fork 0
/
webidl.js
449 lines (409 loc) · 18.1 KB
/
webidl.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
/* https://github.com/lachlanhunt/webidl.js */
;(function(global) {
"use strict";
var floor = Math.floor,
abs = Math.abs,
round = Math.round,
pow = Math.pow,
min = Math.min,
max = Math.max;
function sign(n) {
n = +n;
return (n === 0 ? 1/n : n) < 0 ? -1 : 1;
}
function roundToNearest(value) {
value = ((floor(value) % 2 === 0) && (abs(value % 1) === 0.5)) ?
round(value) - 1 : round(value);
return value === -0 ? 0 : value;
}
/*
* The ECMAScript remainder operator (%) is not useful because it uses truncated rounding.
* This mod function uses floored rounding instead, as needed by WebIDL and ECMAScript algorithms.
*/
Object.defineProperty(Number.prototype, "mod", {
value: function mod(n) {
var a = +this;
return a - n * floor(a/n);
},
configurable: true,
enumerable: false,
writable: true
});
var WebIDL = {
toAny: function toAny(value) {
if (value === undefined || value === null) {
// For undefined, IDL says to use a special object that represents the undefined value.
// Can't do that effectively in JS, just keep it as is.
return value;
} else if (typeof value === "number") {
return WebIDL.toBoolean(value);
} else if (typeof value === "number") {
return WebIDL.toUnrestrictedDouble(value);
} else if (typeof value === "string") {
return WebIDL.toDOMString(value);
} else {
return WebIDL.toObject(value);
}
},
toBoolean: function toBoolean(value) {
return Boolean(value);
},
toByte: function toByte(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < pow(-2, 7) || x > pow(2, 7) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, pow(-2, 7)), pow(2, 7) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 8));
return (x >= pow(2, 7)) ? x - pow(2, 8) : x;
}
},
toOctet: function toOctet(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < 0 || x > pow(2, 8) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, 0), pow(2, 8) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 8));
return x;
}
},
toShort: function toShort(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < pow(-2, 15) || x > pow(2, 15) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, pow(-2, 15)), pow(2, 15) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 16));
return (x >= pow(2, 15)) ? x - pow(2, 16) : x;
}
},
toUnsignedShort: function toUnsignedShort(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < 0 || x > pow(2, 16) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, 0), pow(2, 16) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 16));
return x;
}
},
toLong: function toLong(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < pow(-2, 31) || x > pow(2, 31) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, pow(-2, 31)), pow(2, 31) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 32));
return (x >= pow(2, 31)) ? x - pow(2, 32) : x;
}
},
toUnsignedLong: function toUnsignedLong(value, extAttrs) {
/*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < 0 || x > pow(2, 32) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, 0), pow(2, 32) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 32));
return x;
}
},
toLongLong: function toLongLong(value, extAttrs) {
/*
* Note: Integers above 2^53-1 or below -2^53+1 are approximated due to IEEE 754 limitations
*
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < pow(-2, 63) || x > pow(2, 63) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, pow(-2, 63)), pow(2, 63) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 64));
return (x >= pow(2, 63)) ? x - pow(2, 64) : x;
}
},
toUnsignedLongLong: function toUnsignedLongLong(value, extAttrs) {
/*
* Note: Integers above 2^53 are approximated due to ECMAScript limitations
* Set values to true if the extended attribute appears, false otherwise
* The recognised attributes are:
* extAttrs = {
* EnforceRange: false,
* Clamp: false
* }
*/
var x = Number(value);
if (extAttrs && extAttrs.EnforceRange) {
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot EnforceRange on value");
x = sign(x) * floor(abs(x));
if (x < 0 || x > pow(2, 64) - 1) throw new TypeError("Value is out of range");
return x;
} else if (!isNaN(x) && extAttrs && extAttrs.Clamp) {
x = min(max(x, 0), pow(2, 64) - 1);
return roundToNearest(x);
} else if (isNaN(x) || x === 0 || x === Infinity || x === -Infinity) {
return 0;
} else {
x = sign(x) * floor(abs(x));
x = x.mod(pow(2, 64));
return x;
}
},
toFloat: function toFloat(value) {
var x = WebIDL.toUnrestrictedFloat(value);
if (isNaN(x) || !isFinite(x)) throw new TypeError("Cannot convert value to float");
return x;
},
toUnrestrictedFloat: function toUnrestrictedFloat(value) {
value = Number(value);
// Nothing to be done for +/-0, +/-Infinity or NaN
if (value === 0 || !isFinite(value) || isNaN(value)) return value;
// 53 bit (including hidden bit) significand for 64 bit double precision value
// 24 bit (including hidden bit) significand for 32 bit single precision value
// Only work with the positive value. The sign will be fixed at the end.
var sig52 = abs(value).toString(2),
sig24;
// Radix point
var radixPoint = sig52.indexOf('.');
sig52 = sig52.split('.').join('');
// Leading zeros
var zeros = sig52.indexOf("1"); // The number of 0s before the first 1.
sig52 = sig52.substring(zeros);
// Exponent
// If there's no radix point, the exponent is length-1
// e.g. "101" (length is 3)
// 101 = 1.01 * 2^(length-1) = 1.01*2^2
// If there is a radix point, the exponent is the index-1
// e.g. "10.1". (index is 2)
// 10.1 = 10 * 2^(index-1) = 10.1 * 2^1
// The number of leading zeros are then subtracted.
// e.g. 0.001 = 1.0 * 2^-3
var exp = (radixPoint === -1 ? sig52.length : radixPoint) - 1 - zeros;
var shift; // Number of places to shift the radix point later
// Round off the significand to the nearest even value
var roundingPoint = sig52.length > 24 ? 25 : sig52.length;
// Denormalised values
if (exp < -126) {
zeros -= 126; // Subtract insignificant zeros from significand
var tail = sig52.length - roundingPoint;
var length = zeros + sig52.length - tail;
if (length > 24) {
roundingPoint -= length - 24;
}
}
if (roundingPoint < 1) {
// Underflow
return sign(value) < 0 ? -0 : 0;
} else if (roundingPoint < sig52.length) {
// Shift the radix point to the left and round to nearest
shift = (roundingPoint - sig52.length);
sig24 = roundToNearest(parseInt(sig52, 2) * pow(2, shift)).toString(2);
} else {
// No change
sig24 = sig52;
}
// The exponent represents the number in scientific notation.
// e.g. 1.011 * 2^2 = 101.1 base 2 (5.5 base 10).
// The shift represents the position of the radix point.
// e.g. 101.1 = 1011 * 2^-1
shift = exp - (sig24.length - 1);
// Parse the entire number as a binary integer and then multiply by
// the appropriate power of 2 in order to shift the radix point.
var n = parseInt(sig24, 2) * Math.pow(2, shift);
// Return the value, or +/-Infinity if it overflows.
return sign(value) * (n >= Math.pow(2,128) ? Infinity : n);
},
toDouble: function toDouble(value) {
var x = Number(value);
if (isNaN(x) || x === Infinity || x === -Infinity) throw new TypeError("Cannot convert value to double");
return x;
},
toUnrestrictedDouble: function toUnrestrictedDouble(value) {
return Number(value);
},
toDOMString: function toDOMString(value) {
return String(value);
},
toByteString: function toByteString(value) {
var x = String(value);
for (var i = 0; i < x.length; i++) {
if (x.charCodeAt(i) > 255) throw new TypeError("Cannot convert value to ByteString");
}
return x;
},
toDictionary: function toDictionary(thisObj, value, members) {
// This doesn't yet handle multiple inherited dictionaries.
if (!(value === undefined || value === null || typeof value === "object")) {
throw new TypeError("Cannot convert argument to dictionary");
}
for (var key in members) {
var present = (value === undefined || value === null) ? false
: (key in value);
if (present) {
thisObj[key] = typeConversion[members[key].type](value[key]);
} else if (members[key].defaultValue !== undefined) {
thisObj[key] = members[key].defaultValue;
}
}
return thisObj;
},
toObject: function toObject(value) {
if (typeof value !== "object" || value === null) throw new TypeError("Cannot convert value to Object");
return Object(value);
},
toInterfaceType: function toInterfaceType(value, definition) {
// Not Yet Supported
},
toEnumerationType: function toEnumerationType(value, enumeration) {
var x = String(value);
if (!Array.isArray(enumeration)) enumeration = Array.prototype.slice.call(enumeration);
if (enumeration.indexOf(x) === -1) throw new TypeError("Value is not one of the enumerated types.");
return x;
},
toNullableType: function toNullableType(value, innerType, extAttrs) {
if (value === undefined && extAttrs && extAttrs.TreatUndefinedAs) {
if (extAttrs.TreatUndefinedAs === "EmptyString") {
return "";
} else if (extAttrs.TreatUndefinedAs === "Null") {
return null;
} else {
throw new TypeError("Cannot treat undefined as specified");
}
} else if (typeof value !== "function" && extAttrs && extAttrs.TreatNonCallableAsNull) {
return null;
} else if (value === undefined || value === null) {
return null;
} else {
return typeConversion[innerType](value);
}
}
};
var typeConversion = {
"byte": WebIDL.toByte,
"octet": WebIDL.toOctet,
"short": WebIDL.toShort,
"unsigned short": WebIDL.toUnsignedShort,
"long": WebIDL.toLong,
"unsigned long": WebIDL.toUnsignedLong,
"long long": WebIDL.toLongLong,
"unsigned long long": WebIDL.toUnsignedLongLong,
"float": WebIDL.toFloat,
"unrestricted float": WebIDL.toUnrestrictedFloat,
"double": WebIDL.toDouble,
"unrestricted double": WebIDL.toUnrestrictedDouble,
"boolean": WebIDL.toBoolean,
"DOMString": WebIDL.toDOMString,
"ByteString": WebIDL.toByteString
};
global.WebIDL = WebIDL;
})(this);