@awayfl/avm2
Version:
Virtual machine for executing AS3 code
194 lines (193 loc) • 7.4 kB
JavaScript
/*
* 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.
*/
import { __extends } from "tslib";
import { assert, DataBuffer } from '@awayjs/graphics';
import { checkValue } from '../run/checkValue';
import { axCoerceName } from '../run/axCoerceName';
import { ASObject } from '../nat/ASObject';
import { release, notImplemented, unexpected, isNumeric, defineNonEnumerableProperty } from '@awayfl/swf-loader';
import { AMF0, AMF3 } from '../amf';
export var AMFEncoding;
(function (AMFEncoding) {
AMFEncoding[AMFEncoding["AMF0"] = 0] = "AMF0";
AMFEncoding[AMFEncoding["AMF3"] = 3] = "AMF3";
AMFEncoding[AMFEncoding["DEFAULT"] = 3] = "DEFAULT";
})(AMFEncoding || (AMFEncoding = {}));
var ObjectEncoding = /** @class */ (function (_super) {
__extends(ObjectEncoding, _super);
function ObjectEncoding() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(ObjectEncoding, "dynamicPropertyWriter", {
get: function () {
release || release || notImplemented('public ObjectEncoding::get dynamicPropertyWriter');
return null;
},
set: function (value /* flash.net.IDynamicPropertyWriter */) {
release || release || notImplemented('public ObjectEncoding::set dynamicPropertyWriter');
},
enumerable: false,
configurable: true
});
ObjectEncoding.AMF0 = AMFEncoding.AMF0;
ObjectEncoding.AMF3 = AMFEncoding.AMF3;
ObjectEncoding.DEFAULT = AMFEncoding.DEFAULT;
return ObjectEncoding;
}(ASObject));
export { ObjectEncoding };
var ByteArrayDataProvider = /** @class */ (function () {
function ByteArrayDataProvider() {
}
return ByteArrayDataProvider;
}());
export { ByteArrayDataProvider };
var ByteArray = /** @class */ (function (_super) {
__extends(ByteArray, _super);
function ByteArray(source) {
var _this = _super.call(this) || this;
if (ByteArrayDataProvider.symbolForConstructor) {
source = ByteArrayDataProvider.symbolForConstructor;
ByteArrayDataProvider.symbolForConstructor = null;
}
var buffer;
var length = 0;
if (source) {
if (source instanceof ArrayBuffer) {
buffer = source.slice(0);
}
else if (Array.isArray(source)) {
buffer = new Uint8Array(source).buffer;
}
else if ('buffer' in source) {
if (source.buffer instanceof ArrayBuffer) {
buffer = new Uint8Array(source.buffer).buffer;
}
else if (source.buffer instanceof Uint8Array) {
var begin = source.buffer.byteOffset;
buffer = source.buffer.buffer.slice(begin, begin + source.buffer.length);
}
else {
release || assert(source.buffer instanceof ArrayBuffer);
buffer = source.buffer.slice();
}
}
else {
unexpected('Source type.');
}
length = buffer.byteLength;
}
else {
buffer = new ArrayBuffer(ByteArray.INITIAL_SIZE);
}
_this._buffer = buffer;
_this._length = length;
_this._position = 0;
_this._resetViews();
_this._objectEncoding = ByteArray.defaultObjectEncoding;
_this._littleEndian = false; // AS3 is bigEndian by default.
_this._bitBuffer = 0;
_this._bitLength = 0;
return _this;
}
ByteArray.classInitializer = function () {
var proto = DataBuffer.prototype;
defineNonEnumerableProperty(proto, '$BgtoJSON', proto.toJSON);
};
ByteArray.prototype.setArrayBuffer = function (buffer) {
this._buffer = buffer;
this._length = this._buffer.byteLength;
this._resetViews();
};
Object.defineProperty(ByteArray, "defaultObjectEncoding", {
get: function () {
return this._defaultObjectEncoding;
},
set: function (version /*uint*/) {
version = version >>> 0;
this._defaultObjectEncoding = version;
},
enumerable: false,
configurable: true
});
ByteArray.prototype.toJSON = function () {
return 'ByteArray';
};
ByteArray.prototype.readObject = function () {
switch (this._objectEncoding) {
case ObjectEncoding.AMF0:
return AMF0.read(this);
case ObjectEncoding.AMF3:
return AMF3.read(this);
default:
unexpected('Object Encoding');
}
};
ByteArray.prototype.writeObject = function (object) {
switch (this._objectEncoding) {
case ObjectEncoding.AMF0:
return AMF0.write(this, object);
case ObjectEncoding.AMF3:
return AMF3.write(this, object);
default:
unexpected('Object Encoding');
}
};
ByteArray.prototype.axGetPublicProperty = function (nm) {
if (typeof nm === 'number' || isNumeric(nm = axCoerceName(nm))) {
return this.axGetNumericProperty(nm);
}
return this['$Bg' + nm];
};
ByteArray.prototype.axGetNumericProperty = function (nm) {
return this.getValue(nm);
};
ByteArray.prototype.axSetPublicProperty = function (nm, value) {
release || checkValue(value);
if (typeof nm === 'number' || isNumeric(nm = axCoerceName(nm))) {
this.axSetNumericProperty(nm, value);
return;
}
this['$Bg' + nm] = value;
};
ByteArray.prototype.axSetNumericProperty = function (nm, value) {
this.setValue(nm, value);
};
ByteArray.prototype.axGetProperty = function (mn) {
var name = mn.name;
if (typeof name === 'number' || isNumeric(name = axCoerceName(name))) {
release || assert(mn.isRuntimeName());
return this.getValue(+name);
}
return _super.prototype.axGetProperty.call(this, mn);
};
ByteArray.prototype.axSetProperty = function (mn, value, bc) {
release || checkValue(value);
var name = mn.name;
if (typeof name === 'number' || isNumeric(name = axCoerceName(name))) {
release || assert(mn.isRuntimeName());
this.setValue(+name, value);
return;
}
_super.prototype.axSetProperty.call(this, mn, value, bc);
};
ByteArray.classNatives = [DataBuffer];
ByteArray.instanceNatives = [DataBuffer.prototype];
/* The initial size of the backing, in bytes. Doubled every OOM. */
ByteArray.INITIAL_SIZE = 128;
ByteArray._defaultObjectEncoding = ObjectEncoding.DEFAULT;
return ByteArray;
}(ASObject));
export { ByteArray };