@awayfl/avm2
Version:
Virtual machine for executing AS3 code
76 lines (75 loc) • 2.85 kB
JavaScript
import { ScriptInfo } from './ScriptInfo';
import { createGlobalNative } from './createGlobalNative';
import { flashlog, release } from '@awayfl/swf-loader';
import { interpret } from '../../int';
import { getNative } from '../../nat/getNative';
import { getMethodOrAccessorNative } from '../../nat/getMethodOrAccessorNative';
import { assert } from '@awayjs/graphics';
export function createMethodForTrait(methodTraitInfo, scope, forceNativeMethods) {
if (forceNativeMethods === void 0) { forceNativeMethods = false; }
if (methodTraitInfo.method) {
return methodTraitInfo.method;
}
var methodInfo = methodTraitInfo.methodInfo;
var method;
if (methodInfo.flags & 32 /* METHOD.Native */) {
var metadata = methodInfo.getNativeMetadata();
if (metadata || methodTraitInfo.holder instanceof ScriptInfo) {
if (metadata) {
method = getNative(metadata.values[0]);
}
else {
var mn = methodTraitInfo.multiname;
method = getNative(mn.uri + '.' + mn.name);
}
method = createGlobalNative(method, scope.object.sec);
}
else {
method = getMethodOrAccessorNative(methodTraitInfo);
}
if (method && !release) {
method.toString = function () {
return 'Native ' + methodTraitInfo.toString();
};
method.isInterpreted = false;
}
}
else {
if (forceNativeMethods)
method = getMethodOrAccessorNative(methodTraitInfo, false);
if (!method) {
method = interpret(methodInfo, scope, null);
if (!release) {
method.toString = function () {
return 'Interpreted ' + methodTraitInfo.toString();
};
method.isInterpreted = true;
}
}
}
if (!release && flashlog && methodInfo.trait) {
method = (function (wrapped, methodInfo) {
var traceMsg = methodInfo.toFlashlogString();
var result = function () {
flashlog.writeAS3Trace(traceMsg);
return wrapped.apply(this, arguments);
};
result.toString = wrapped.toString;
result.isInterpreted = wrapped.isInterpreted;
return result;
})(method, methodInfo);
}
assert(method, 'Not found method:' + methodTraitInfo.toString());
methodTraitInfo.method = method;
method.methodInfo = methodInfo;
if (!release) {
try {
Object.defineProperty(method, 'name', { value: methodInfo.name });
}
catch (e) {
// Ignore errors in browsers that don't allow overriding Function#length;
}
}
method.methodInfo = methodInfo;
return method;
}