@awayfl/avm2
Version:
Virtual machine for executing AS3 code
274 lines (273 loc) • 11.5 kB
JavaScript
import { __extends } from "tslib";
import { ASObject } from './ASObject';
import { addPrototypeFunctionAlias } from './addPrototypeFunctionAlias';
import { assert } from '@awayjs/graphics';
import { release } from '@awayfl/swf-loader';
import { Errors } from '../errors';
import { axCoerceString } from '../run/axCoerceString';
import { transformJStoASRegExpMatchArray } from './transformJStoASRegExpMatchArray';
import { as3Compatibility } from './as3Compatibility';
import { as3ToLowerCase } from './as3ToLowerCase';
var ASString = /** @class */ (function (_super) {
__extends(ASString, _super);
function ASString(value) {
var _this = _super.call(this) || this;
_this.value = value;
return _this;
}
ASString.classInitializer = function () {
var proto = this.dPrototype;
var asProto = ASString.prototype;
addPrototypeFunctionAlias(proto, '$BgindexOf', asProto.generic_indexOf);
addPrototypeFunctionAlias(proto, '$BglastIndexOf', asProto.generic_lastIndexOf);
addPrototypeFunctionAlias(proto, '$BgcharAt', asProto.generic_charAt);
addPrototypeFunctionAlias(proto, '$BgcharCodeAt', asProto.generic_charCodeAt);
addPrototypeFunctionAlias(proto, '$Bgconcat', asProto.generic_concat);
addPrototypeFunctionAlias(proto, '$BglocaleCompare', asProto.generic_localeCompare);
addPrototypeFunctionAlias(proto, '$Bgmatch', asProto.generic_match);
addPrototypeFunctionAlias(proto, '$Bgreplace', asProto.generic_replace);
addPrototypeFunctionAlias(proto, '$Bgsearch', asProto.generic_search);
addPrototypeFunctionAlias(proto, '$Bgslice', asProto.generic_slice);
addPrototypeFunctionAlias(proto, '$Bgsplit', asProto.generic_split);
addPrototypeFunctionAlias(proto, '$Bgsubstring', asProto.generic_substring);
addPrototypeFunctionAlias(proto, '$Bgsubstr', asProto.generic_substr);
addPrototypeFunctionAlias(proto, '$BgtoLowerCase', asProto.generic_toLowerCase);
addPrototypeFunctionAlias(proto, '$BgtoLocaleLowerCase', asProto.generic_toLowerCase);
addPrototypeFunctionAlias(proto, '$BgtoUpperCase', asProto.generic_toUpperCase);
addPrototypeFunctionAlias(proto, '$BgtoLocaleUpperCase', asProto.generic_toUpperCase);
addPrototypeFunctionAlias(proto, '$BgtoString', asProto.toString);
addPrototypeFunctionAlias(proto, '$BgtoString', asProto.public_toString);
addPrototypeFunctionAlias(proto, '$BgvalueOf', asProto.public_valueOf);
addPrototypeFunctionAlias(this, '$BgfromCharCode', ASString.fromCharCode);
};
ASString.fromCharCode = function () {
var charcodes = [];
for (var _i = 0; _i < arguments.length; _i++) {
charcodes[_i] = arguments[_i];
}
return String.fromCharCode.apply(null, charcodes);
};
ASString.prototype.indexOf = function (char, i) {
return this.value.indexOf(char, i);
};
ASString.prototype.lastIndexOf = function (char, i) {
return this.value.lastIndexOf(char, i);
};
ASString.prototype.charAt = function (index) {
return this.value.charAt(index);
};
ASString.prototype.charCodeAt = function (index) {
return this.value.charCodeAt(index);
};
ASString.prototype.concat = function () {
// eslint-disable-next-line prefer-spread, prefer-rest-params
return this.value.concat.apply(this.value, arguments);
};
ASString.prototype.localeCompare = function (other) {
if (arguments.length > 1) {
this.sec.throwError('ArgumentError', Errors.WrongArgumentCountError, 'Function/<anonymous>()', 0, 2);
}
var value = this.value;
release || assert(typeof this.value === 'string');
other = String(other);
if (other === value) {
return 0;
}
var len = Math.min(value.length, other.length);
for (var j = 0; j < len; j++) {
if (value[j] !== other[j]) {
return value.charCodeAt(j) - other.charCodeAt(j);
}
}
return value.length > other.length ? 1 : -1;
};
ASString.prototype.__getRegExp = function (pattern) {
if (this.sec.AXRegExp.axIsType(pattern)) {
return pattern;
}
else {
return this.sec.AXRegExp.axConstruct([axCoerceString(pattern)]);
}
};
ASString.prototype.match = function (pattern) {
var regExp = this.__getRegExp(pattern);
var result = regExp.internalStringMatch(this.value);
if (!result) {
return null;
}
try {
return transformJStoASRegExpMatchArray(this.sec, result);
}
catch (e) {
return null;
}
};
ASString.prototype.replace = function (pattern, repl) {
if (this.sec.AXFunction.axIsType(repl)) {
repl = repl.value;
}
try {
if (!this.sec.AXRegExp.axIsType(pattern)) {
pattern = axCoerceString(pattern);
return this.value.replace(pattern, repl);
}
return this.__getRegExp(pattern).internalStringReplace(this.value, repl);
}
catch (e) {
return this.value;
}
};
ASString.prototype.search = function (pattern) {
try {
if (!this.sec.AXRegExp.axIsType(pattern)) {
return this.value.search(axCoerceString(pattern));
}
return this.__getRegExp(pattern).internalStringSearch(this.value);
}
catch (e) {
return -1;
}
};
ASString.prototype.slice = function (start, end) {
start = arguments.length < 1 ? 0 : start | 0;
end = arguments.length < 2 ? 0xffffffff : end | 0;
return this.value.slice(start, end);
};
ASString.prototype.split = function (separator /* : string | ASRegExp */, limit) {
if (this.sec.AXRegExp.axIsType(separator)) {
separator = separator.value;
}
else {
separator = axCoerceString(separator);
}
limit = limit === undefined ? -1 : limit | 0;
try {
return this.sec.createArray(this.value.split(separator, limit));
}
catch (e) {
return this.sec.createArrayUnsafe([this.value]);
}
};
ASString.prototype.substring = function (start, end) {
return this.value.substring(start, end);
};
ASString.prototype.substr = function (from, length) {
if (length == -1) {
length = this.value.length - from - 1;
}
return this.value.substr(from, length);
};
ASString.prototype.toLocaleLowerCase = function () {
return this.value.toLowerCase();
};
ASString.prototype.toLowerCase = function () {
if (as3Compatibility) {
return as3ToLowerCase(this.value);
}
return this.value.toLowerCase();
};
ASString.prototype.toLocaleUpperCase = function () {
return this.value.toUpperCase();
};
ASString.prototype.toUpperCase = function () {
return this.value.toUpperCase();
};
// The String.prototype versions of these methods are generic, so the implementation is
// different.
ASString.prototype.generic_indexOf = function (char, i) {
var receiver = this == undefined ? '' : this;
return String.prototype.indexOf.call(receiver, char, i);
};
ASString.prototype.generic_lastIndexOf = function (char, i) {
var receiver = this == undefined ? '' : this;
return String.prototype.lastIndexOf.call(receiver, char, i);
};
ASString.prototype.generic_charAt = function (index) {
var receiver = this == undefined ? '' : this;
return String.prototype.charAt.call(receiver, index);
};
ASString.prototype.generic_charCodeAt = function (index) {
var receiver = this == undefined ? '' : this;
return String.prototype.charCodeAt.call(receiver, index);
};
ASString.prototype.generic_concat = function () {
var receiver = this == undefined ? '' : this;
// eslint-disable-next-line prefer-rest-params
return String.prototype.concat.apply(receiver, arguments);
};
ASString.prototype.generic_localeCompare = function (other) {
var receiver = this.sec.AXString.axBox(String(this));
// eslint-disable-next-line prefer-spread, prefer-rest-params
return receiver.localeCompare.apply(receiver, arguments);
};
ASString.prototype.generic_match = function (pattern) {
return this.sec.AXString.axBox(String(this)).match(pattern);
};
ASString.prototype.generic_replace = function (pattern, repl) {
return this.sec.AXString.axBox(String(this)).replace(pattern, repl);
};
ASString.prototype.generic_search = function (pattern) {
return this.sec.AXString.axBox(String(this)).search(pattern);
};
ASString.prototype.generic_slice = function (start, end) {
var receiver = this == undefined ? '' : this;
return String.prototype.slice.call(receiver, start, end);
};
ASString.prototype.generic_split = function (separator, limit) {
limit = arguments.length < 2 ? 0xffffffff : limit | 0;
return this.sec.AXString.axBox(String(this)).split(separator, limit);
};
ASString.prototype.generic_substring = function (start, end) {
var receiver = this == undefined ? '' : this;
return String.prototype.substring.call(receiver, start, end);
};
ASString.prototype.generic_substr = function (from, length) {
var receiver = this == undefined ? '' : this;
return String.prototype.substr.call(receiver, from, length);
};
ASString.prototype.generic_toLowerCase = function () {
var receiver = this == undefined ? '' : this;
if (as3Compatibility) {
return as3ToLowerCase(String(receiver));
}
String.prototype.toLowerCase.call(receiver);
};
ASString.prototype.generic_toUpperCase = function () {
var receiver = this == undefined ? '' : this;
return String.prototype.toUpperCase.call(receiver);
};
ASString.prototype.toString = function () {
return this.value.toString();
};
ASString.prototype.public_toString = function () {
if (this === this.sec.AXString.dPrototype) {
return '';
}
if (this.axClass !== this.sec.AXString) {
this.sec.throwError('TypeError', Errors.InvokeOnIncompatibleObjectError, 'String.prototype.toString');
}
return this.value.toString();
};
ASString.prototype.valueOf = function () {
return this.value.valueOf();
};
ASString.prototype.public_valueOf = function () {
if (this === this.sec.AXString.dPrototype) {
return '';
}
if (this.axClass !== this.sec.AXString) {
this.sec.throwError('TypeError', Errors.InvokeOnIncompatibleObjectError, 'String.prototype.valueOf');
}
return this.value.valueOf();
};
Object.defineProperty(ASString.prototype, "length", {
get: function () {
return this.value.length;
},
enumerable: false,
configurable: true
});
ASString.classNatives = [String];
return ASString;
}(ASObject));
export { ASString };