@awayfl/avm2
Version:
Virtual machine for executing AS3 code
90 lines (89 loc) • 3.83 kB
JavaScript
import { ClassInfo } from '../abc/lazy/ClassInfo';
import { InstanceInfo } from '../abc/lazy/InstanceInfo';
import { MethodTraitInfo } from '../abc/lazy/MethodTraitInfo';
import { namespaceTypeNames } from '../abc/lazy/NamespaceType';
import { TRAITNames } from '../abc/lazy/TRAIT';
var SCRIPT_ID = 0;
var validTest = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
export function validateName(name) {
return validTest.test(name);
}
export var CLASSES_NAMES_COLLISIONS = {};
export function nextScriptID() {
return SCRIPT_ID++;
}
export function reconstructMetadata(methodInfo, id) {
var _a, _b, _c;
var prefix = ('' + (id)).padLeft('0', 4);
var funcName = (methodInfo.name).replace(/([^a-z0-9]+)/gi, '_');
var pathIsDefault = true;
var fullPath = "__root__/".concat(prefix, "_").concat(funcName || 'unknown');
var path = fullPath;
var methodName = funcName;
var isMemeber = true;
var methodType = 'Public';
var superClass = undefined;
if (methodInfo.trait) {
if (methodInfo.trait.holder instanceof ClassInfo) {
path = methodInfo.trait.holder.instanceInfo.getClassName().replace(/\./g, '/');
isMemeber = false;
}
else if (methodInfo.trait.holder instanceof InstanceInfo) {
path = methodInfo.trait.holder.getClassName().replace(/\./g, '/');
superClass = (_a = methodInfo.trait.holder.superName) === null || _a === void 0 ? void 0 : _a.toFQNString(false).replace(/\./g, '/');
}
if (methodInfo.trait instanceof MethodTraitInfo) {
methodName = methodInfo.trait.multiname.name;
methodType = namespaceTypeNames[methodInfo.trait.multiname.namespace.type];
}
if (methodInfo.trait && methodInfo.trait.kind === 2 /* TRAIT.Getter */) {
methodName = 'get_' + methodName;
}
if (methodInfo.trait && methodInfo.trait.kind === 3 /* TRAIT.Setter */) {
methodName = 'set_' + methodName;
}
if (methodInfo.isConstructor) {
//constructor
methodName = 'constructor';
}
else {
// member
methodName = isMemeber ? ('m_' + methodName) : methodName;
}
pathIsDefault = false;
fullPath = path + '/' + methodName;
if (CLASSES_NAMES_COLLISIONS[fullPath] !== undefined) {
var index = CLASSES_NAMES_COLLISIONS[fullPath] = CLASSES_NAMES_COLLISIONS[fullPath] + 1;
fullPath += '$' + index;
}
else {
CLASSES_NAMES_COLLISIONS[fullPath] = 0;
}
}
// for instances
if (methodInfo.instanceInfo) {
path = methodInfo.instanceInfo.getClassName().replace(/\./g, '/');
methodName = methodInfo.isConstructor ? 'constructor' : funcName;
superClass = (_b = methodInfo.instanceInfo.superName) === null || _b === void 0 ? void 0 : _b.toFQNString(false).replace(/\./g, '/');
pathIsDefault = false;
fullPath = path + '/' + methodName;
}
var hookMethodPath = "".concat(path).concat(isMemeber ? '::' : '.').concat(methodName);
// reconstruct path to owner method
if (methodInfo.parentInfo && pathIsDefault) {
fullPath = methodInfo.parentInfo.meta.filePath + '/' + "".concat(prefix, "_").concat(methodName);
hookMethodPath = fullPath;
}
return {
index: methodInfo.index(),
filePath: fullPath,
classPath: hookMethodPath,
name: methodName,
type: methodType,
superClass: superClass,
returnType: ((_c = methodInfo.typeName) === null || _c === void 0 ? void 0 : _c.toString()) || '*',
isValidName: validateName(methodName),
isValidPath: validateName(path.replace('/', '_')),
kind: methodInfo.trait && TRAITNames[methodInfo.trait.kind]
};
}