@awayfl/avm2
Version:
Virtual machine for executing AS3 code
400 lines (399 loc) • 18.7 kB
JavaScript
import { __extends } from "tslib";
import { ASObject } from '../nat/ASObject';
import { addPrototypeFunctionAlias } from '../nat/addPrototypeFunctionAlias';
/*
* Copyright 2014 Mozilla Foundation
*
* 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.
*/
var ASDate = /** @class */ (function (_super) {
__extends(ASDate, _super);
function ASDate(yearOrTimevalue, month, date, hour, minute, second, millisecond) {
if (date === void 0) { date = 1; }
if (hour === void 0) { hour = 0; }
if (minute === void 0) { minute = 0; }
if (second === void 0) { second = 0; }
if (millisecond === void 0) { millisecond = 0; }
var _this = _super.call(this) || this;
switch (arguments.length) {
case 0:
_this.value = new Date();
break;
case 1:
_this.value = new Date(yearOrTimevalue);
break;
case 2:
_this.value = new Date(yearOrTimevalue, month);
break;
case 3:
_this.value = new Date(yearOrTimevalue, month, date);
break;
case 4:
_this.value = new Date(yearOrTimevalue, month, date, hour);
break;
case 5:
_this.value = new Date(yearOrTimevalue, month, date, hour, minute);
break;
case 6:
_this.value = new Date(yearOrTimevalue, month, date, hour, minute, second);
break;
default:
_this.value = new Date(yearOrTimevalue, month, date, hour, minute, second, millisecond);
break;
}
return _this;
}
ASDate.classInitializer = function () {
var proto = this.dPrototype;
var asProto = ASDate.prototype;
addPrototypeFunctionAlias(proto, '$BgtoString', asProto.toString);
addPrototypeFunctionAlias(proto, '$BgtoLocaleString', asProto.toString);
addPrototypeFunctionAlias(proto, '$BgvalueOf', asProto.valueOf);
addPrototypeFunctionAlias(proto, '$BgtoDateString', asProto.toDateString);
addPrototypeFunctionAlias(proto, '$BgtoTimeString', asProto.toTimeString);
addPrototypeFunctionAlias(proto, '$BgtoLocaleString', asProto.toLocaleString);
addPrototypeFunctionAlias(proto, '$BgtoLocaleDateString', asProto.toLocaleDateString);
addPrototypeFunctionAlias(proto, '$BgtoLocaleTimeString', asProto.toLocaleTimeString);
addPrototypeFunctionAlias(proto, '$BgtoUTCString', asProto.toUTCString);
// NB: The default AS implementation of |toJSON| is not ES5-compliant, but
// the native JS one obviously is.
addPrototypeFunctionAlias(proto, '$BgtoJSON', asProto.toJSON);
addPrototypeFunctionAlias(proto, '$BggetUTCFullYear', asProto.getUTCFullYear);
addPrototypeFunctionAlias(proto, '$BggetUTCMonth', asProto.getUTCMonth);
addPrototypeFunctionAlias(proto, '$BggetUTCDate', asProto.getUTCDate);
addPrototypeFunctionAlias(proto, '$BggetUTCDay', asProto.getUTCDay);
addPrototypeFunctionAlias(proto, '$BggetUTCHours', asProto.getUTCHours);
addPrototypeFunctionAlias(proto, '$BggetUTCMinutes', asProto.getUTCMinutes);
addPrototypeFunctionAlias(proto, '$BggetUTCSeconds', asProto.getUTCSeconds);
addPrototypeFunctionAlias(proto, '$BggetUTCMilliseconds', asProto.getUTCMilliseconds);
addPrototypeFunctionAlias(proto, '$BggetFullYear', asProto.getFullYear);
addPrototypeFunctionAlias(proto, '$BggetMonth', asProto.getMonth);
addPrototypeFunctionAlias(proto, '$BggetDate', asProto.getDate);
addPrototypeFunctionAlias(proto, '$BggetDay', asProto.getDay);
addPrototypeFunctionAlias(proto, '$BggetHours', asProto.getHours);
addPrototypeFunctionAlias(proto, '$BggetMinutes', asProto.getMinutes);
addPrototypeFunctionAlias(proto, '$BggetSeconds', asProto.getSeconds);
addPrototypeFunctionAlias(proto, '$BggetMilliseconds', asProto.getMilliseconds);
addPrototypeFunctionAlias(proto, '$BggetTimezoneOffset', asProto.getTimezoneOffset);
addPrototypeFunctionAlias(proto, '$BggetTime', asProto.getTime);
addPrototypeFunctionAlias(proto, '$BgsetFullYear', asProto.setFullYear);
addPrototypeFunctionAlias(proto, '$BgsetMonth', asProto.setMonth);
addPrototypeFunctionAlias(proto, '$BgsetDate', asProto.setDate);
addPrototypeFunctionAlias(proto, '$BgsetHours', proto.setHours);
addPrototypeFunctionAlias(proto, '$BgsetMinutes', asProto.setMinutes);
addPrototypeFunctionAlias(proto, '$BgsetSeconds', asProto.setSeconds);
addPrototypeFunctionAlias(proto, '$BgsetMilliseconds', asProto.setMilliseconds);
addPrototypeFunctionAlias(proto, '$BgsetUTCFullYear', asProto.setUTCFullYear);
addPrototypeFunctionAlias(proto, '$BgsetUTCMonth', asProto.setUTCMonth);
addPrototypeFunctionAlias(proto, '$BgsetUTCDate', asProto.setUTCDate);
addPrototypeFunctionAlias(proto, '$BgsetUTCHours', asProto.setUTCHours);
addPrototypeFunctionAlias(proto, '$BgsetUTCMinutes', asProto.setUTCMinutes);
addPrototypeFunctionAlias(proto, '$BgsetUTCSeconds', asProto.setUTCSeconds);
addPrototypeFunctionAlias(proto, '$BgsetUTCMilliseconds', asProto.setUTCMilliseconds);
};
ASDate.parse = function (date) {
return Date.parse(date);
};
ASDate.UTC = function (year, month, date, hour, minute, second, millisecond) {
if (date === void 0) { date = 1; }
if (hour === void 0) { hour = 0; }
if (minute === void 0) { minute = 0; }
if (second === void 0) { second = 0; }
if (millisecond === void 0) { millisecond = 0; }
return Date.parse.apply(null, arguments);
};
ASDate.axCoerce = function (value) {
return this.axConstruct([value]);
};
ASDate.prototype.toString = function () {
if (!(this.value instanceof Date)) {
return 'Invalid Date';
}
// JS formats dates differently, so a little surgery is required here:
// We need to move the year to the end, get rid of the timezone name, and remove leading 0
// from the day.
var dateStr = this.value.toString();
var parts = dateStr.split(' ');
// Detect invalid dates. Not 100% sure all JS engines always print 'Invalid Date' here,
// so we just check how many parts the resulting string has, with some margin for error.
if (parts.length < 4) {
return 'Invalid Date';
}
parts.length = 6; // Get rid of the timezone, which might contain spaces.
parts.push(parts.splice(3, 1)[0]); // Move Year to the end.
if (parts[2][0] === '0') {
parts[2] = parts[2][1];
}
return parts.join(' ');
};
ASDate.prototype.toDateString = function () {
if (!(this.value instanceof Date)) {
return 'Invalid Date';
}
var dateStr = this.value.toDateString();
var parts = dateStr.split(' ');
// Detect invalid dates. Not 100% sure all JS engines always print 'Invalid Date' here,
// so we just check how many parts the resulting string has, with some margin for error.
if (parts.length < 4) {
return 'Invalid Date';
}
if (parts[2][0] === '0') {
parts[2] = parts[2][1];
}
return parts.join(' ');
};
ASDate.prototype.toJSON = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toString(); };
ASDate.prototype.valueOf = function () { return !(this.value instanceof Date) ? NaN : this.value.valueOf(); };
ASDate.prototype.setTime = function (value) {
if (value === void 0) { value = 0; }
return !(this.value instanceof Date) ? NaN : this.value.setTime(value);
};
ASDate.prototype.toTimeString = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toTimeString(); };
ASDate.prototype.toLocaleString = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toLocaleString(); };
ASDate.prototype.toLocaleDateString = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toLocaleDateString(); };
ASDate.prototype.toLocaleTimeString = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toLocaleTimeString(); };
ASDate.prototype.toUTCString = function () { return !(this.value instanceof Date) ? 'Invalid Date' : this.value.toUTCString(); };
ASDate.prototype.getUTCFullYear = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCFullYear(); };
ASDate.prototype.getUTCMonth = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCMonth(); };
ASDate.prototype.getUTCDate = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCDate(); };
ASDate.prototype.getUTCDay = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCDay(); };
ASDate.prototype.getUTCHours = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCHours(); };
ASDate.prototype.getUTCMinutes = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCMinutes(); };
ASDate.prototype.getUTCSeconds = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCSeconds(); };
ASDate.prototype.getUTCMilliseconds = function () { return !(this.value instanceof Date) ? NaN : this.value.getUTCMilliseconds(); };
ASDate.prototype.getFullYear = function () { return !(this.value instanceof Date) ? NaN : this.value.getFullYear(); };
ASDate.prototype.getMonth = function () { return !(this.value instanceof Date) ? NaN : this.value.getMonth(); };
ASDate.prototype.getDate = function () { return !(this.value instanceof Date) ? NaN : this.value.getDate(); };
ASDate.prototype.getDay = function () { return !(this.value instanceof Date) ? NaN : this.value.getDay(); };
ASDate.prototype.getHours = function () { return !(this.value instanceof Date) ? NaN : this.value.getHours(); };
ASDate.prototype.getMinutes = function () { return !(this.value instanceof Date) ? NaN : this.value.getMinutes(); };
ASDate.prototype.getSeconds = function () { return !(this.value instanceof Date) ? NaN : this.value.getSeconds(); };
ASDate.prototype.getMilliseconds = function () { return !(this.value instanceof Date) ? NaN : this.value.getMilliseconds(); };
ASDate.prototype.getTimezoneOffset = function () { return !(this.value instanceof Date) ? NaN : this.value.getTimezoneOffset(); };
ASDate.prototype.getTime = function () { return !(this.value instanceof Date) ? NaN : this.value.getTime(); };
ASDate.prototype.setFullYear = function (year, month, day) {
return !(this.value instanceof Date) ? NaN : this.value.setFullYear(year, month, day);
};
ASDate.prototype.setMonth = function (month, day) {
return !(this.value instanceof Date) ? NaN : this.value.setMonth(month, day);
};
ASDate.prototype.setDate = function (day) {
return !(this.value instanceof Date) ? NaN : this.value.setDate(day);
};
ASDate.prototype.setHours = function (hour, minute, second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setHours(hour, minute, second, millisecond);
};
ASDate.prototype.setMinutes = function (minute, second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setMinutes(minute, second, millisecond);
};
ASDate.prototype.setSeconds = function (second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setSeconds(second, millisecond);
};
ASDate.prototype.setMilliseconds = function (millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setMilliseconds(millisecond);
};
ASDate.prototype.setUTCFullYear = function (year, month, day) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCFullYear(year, month, day);
};
ASDate.prototype.setUTCMonth = function (month, day) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCMonth(month, day);
};
ASDate.prototype.setUTCDate = function (day) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCDate(day);
};
ASDate.prototype.setUTCHours = function (hour, minute, second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCHours(hour, minute, second, millisecond);
};
ASDate.prototype.setUTCMinutes = function (minute, second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCMinutes(minute, second, millisecond);
};
ASDate.prototype.setUTCSeconds = function (second, millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCSeconds(second, millisecond);
};
ASDate.prototype.setUTCMilliseconds = function (millisecond) {
return !(this.value instanceof Date) ? NaN : this.value.setUTCMilliseconds(millisecond);
};
Object.defineProperty(ASDate.prototype, "fullYear", {
get: function () {
return this.value.getFullYear();
},
set: function (value) {
this.value.setFullYear(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "month", {
get: function () {
return this.value.getMonth();
},
set: function (value) {
this.value.setMonth(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "date", {
get: function () {
return this.value.getDate();
},
set: function (value) {
this.value.setDate(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "hours", {
get: function () {
return this.value.getHours();
},
set: function (value) {
this.value.setHours(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "minutes", {
get: function () {
return this.value.getMinutes();
},
set: function (value) {
this.value.setMinutes(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "seconds", {
get: function () {
return this.value.getSeconds();
},
set: function (value) {
this.value.setSeconds(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "milliseconds", {
get: function () {
return this.value.getMilliseconds();
},
set: function (value) {
this.value.setMilliseconds(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "fullYearUTC", {
get: function () {
return this.value.getUTCFullYear();
},
set: function (value) {
this.value.setUTCFullYear(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "monthUTC", {
get: function () {
return this.value.getUTCMonth();
},
set: function (value) {
this.value.setUTCMonth(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "dateUTC", {
get: function () {
return this.value.getUTCDate();
},
set: function (value) {
this.value.setUTCDate(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "hoursUTC", {
get: function () {
return this.value.getUTCHours();
},
set: function (value) {
this.value.setUTCHours(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "minutesUTC", {
get: function () {
return this.value.getUTCMinutes();
},
set: function (value) {
this.value.setUTCMinutes(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "secondsUTC", {
get: function () {
return this.value.getUTCSeconds();
},
set: function (value) {
this.value.setUTCSeconds(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "millisecondsUTC", {
get: function () {
return this.value.getUTCMilliseconds();
},
set: function (value) {
this.value.setUTCMilliseconds(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "time", {
get: function () {
return this.value.getTime();
},
set: function (value) {
this.value.setTime(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "timezoneOffset", {
get: function () {
return this.value.getTimezoneOffset();
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "day", {
get: function () {
return this.value.getDay();
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASDate.prototype, "dayUTC", {
get: function () {
return this.value.getUTCDay();
},
enumerable: false,
configurable: true
});
return ASDate;
}(ASObject));
export { ASDate };