@awayfl/avm2
Version:
Virtual machine for executing AS3 code
100 lines (99 loc) • 4.2 kB
JavaScript
import { __extends } from "tslib";
import { ensureBoxedReceiver } from '../run/ensureBoxedReceiver';
import { isNullOrUndefined, assert } from '@awayjs/graphics';
import { Errors } from '../errors';
import { checkValue } from '../run/checkValue';
import { ASObject } from './ASObject';
import { addPrototypeFunctionAlias } from './addPrototypeFunctionAlias';
import { release, defineNonEnumerableProperty } from '@awayfl/swf-loader';
import { sliceArguments } from '../run/writers';
var ASFunction = /** @class */ (function (_super) {
__extends(ASFunction, _super);
function ASFunction() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._prototypeInitialzed = false;
return _this;
}
ASFunction.classInitializer = function () {
var proto = this.dPrototype;
var asProto = ASFunction.prototype;
addPrototypeFunctionAlias(proto, '$BgtoString', asProto.toString);
addPrototypeFunctionAlias(proto, '$Bgcall', asProto.call);
addPrototypeFunctionAlias(proto, '$Bgapply', asProto.apply);
defineNonEnumerableProperty(proto, 'value', asProto.native_functionValue);
};
ASFunction.prototype.setReceiver = function (receiver) {
this.receiver = receiver;
};
ASFunction.prototype.axConstruct = function (args) {
var prototype = this.prototype;
// AS3 allows setting null/undefined prototypes. In order to make our value checking work,
// we need to set a null-prototype that has the right inheritance chain. Since AS3 doesn't
// have `__proto__` or `getPrototypeOf`, this is completely hidden from content.
if (isNullOrUndefined(prototype)) {
prototype = this.sec.AXFunctionUndefinedPrototype;
}
release || assert(typeof prototype === 'object');
release || checkValue(prototype);
var object = Object.create(prototype);
object.__ctorFunction = this;
this.value.apply(object, args);
return object;
};
ASFunction.prototype.axIsInstanceOf = function (obj) {
return obj && obj.__ctorFunction === this;
};
ASFunction.prototype.native_functionValue = function () {
// Empty base function.
};
Object.defineProperty(ASFunction.prototype, "prototype", {
get: function () {
if (!this._prototypeInitialzed) {
this._prototype = Object.create(this.sec.AXObject.tPrototype);
this._prototypeInitialzed = true;
}
return this._prototype;
},
set: function (prototype) {
if (isNullOrUndefined(prototype)) {
prototype = undefined;
}
else if (typeof prototype !== 'object' || this.sec.isPrimitive(prototype)) {
this.sec.throwError('TypeError', Errors.PrototypeTypeError);
}
this._prototypeInitialzed = true;
this._prototype = prototype;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ASFunction.prototype, "length", {
get: function () {
if (this.value.methodInfo) {
return this.value.methodInfo.parameters.length;
}
return this.value.length;
},
enumerable: false,
configurable: true
});
ASFunction.prototype.toString = function () {
return 'function Function() {}';
};
ASFunction.prototype.call = function (thisArg) {
thisArg = ensureBoxedReceiver(this.sec, thisArg, this);
return this.value.apply(thisArg, sliceArguments(arguments, 1));
};
ASFunction.prototype.apply = function (thisArg, argArray) {
thisArg = ensureBoxedReceiver(this.sec, thisArg, this);
return this.value.apply(thisArg, argArray ? argArray.value : undefined);
};
ASFunction.prototype.axCall = function (thisArg) {
return this.value.apply(thisArg, sliceArguments(arguments, 1));
};
ASFunction.prototype.axApply = function (thisArg, argArray) {
return this.value.apply(thisArg, argArray);
};
return ASFunction;
}(ASObject));
export { ASFunction };