@awayfl/avm2
Version:
Virtual machine for executing AS3 code
154 lines (153 loc) • 5.54 kB
JavaScript
import { __extends } from "tslib";
import { ASObject } from '../nat/ASObject';
import { addPrototypeFunctionAlias } from '../nat/addPrototypeFunctionAlias';
import { assert } from '@awayjs/graphics';
import { release } from '@awayfl/swf-loader';
import { Settings } from '../Settings';
var USE_WEAK = ('WeakRef' in self) && Settings.USE_WEAK_REF;
/*
* 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.
*/
/**
* TODO: We need a more robust Dictionary implementation that doesn't only give you back
* string keys when enumerating.
*/
var Dictionary = /** @class */ (function (_super) {
__extends(Dictionary, _super);
function Dictionary(weakKeys) {
if (weakKeys === void 0) { weakKeys = false; }
var _this = _super.call(this) || this;
_this.map = new WeakMap();
_this.keys = new Set();
_this.primitiveMap = Object.create(null);
if ((_this.weakKeys = !!weakKeys && USE_WEAK))
_this.refs = new WeakMap();
return _this;
}
Dictionary.makePrimitiveKey = function (key) {
if (typeof key === 'string' || typeof key === 'number')
return key;
release || assert(typeof key === 'object' || typeof key === 'function', typeof key);
return undefined;
};
Dictionary.prototype.toJSON = function () {
return 'Dictionary';
};
Dictionary.prototype.axGetProperty = function (mn) {
if (this === this.axClass.dPrototype) {
return _super.prototype.axGetProperty.call(this, mn);
}
var key = Dictionary.makePrimitiveKey(mn.name);
if (key !== undefined) {
return this.primitiveMap[key];
}
return this.map.get(Object(mn.name));
};
Dictionary.prototype.axSetProperty = function (mn, value, bc) {
if (this === this.axClass.dPrototype) {
_super.prototype.axSetProperty.call(this, mn, value, bc);
return;
}
var key = Dictionary.makePrimitiveKey(mn.name);
if (key !== undefined) {
this.primitiveMap[key] = value;
return;
}
var okey = Object(mn.name);
if (!this.map.has(okey)) {
if (this.weakKeys) {
var wkey = new self.WeakRef(okey);
this.refs.set(okey, wkey);
this.keys.add(wkey);
}
else {
this.keys.add(okey);
}
}
this.map.set(okey, value);
};
// TODO: Not implemented yet.
// public axCallProperty(mn: Multiname, args: any []) {
// release || release || notImplemented("axCallProperty");
// }
Dictionary.prototype.axHasPropertyInternal = function (mn) {
if (this === this.axClass.dPrototype) {
return _super.prototype.axHasProperty.call(this, mn);
}
var key = Dictionary.makePrimitiveKey(mn.name);
if (key !== undefined) {
return key in this.primitiveMap;
}
return this.map.has(Object(mn.name));
};
Dictionary.prototype.axDeleteProperty = function (mn) {
if (this === this.axClass.dPrototype) {
return _super.prototype.axDeleteProperty.call(this, mn);
}
var key = Dictionary.makePrimitiveKey(mn.name);
if (key !== undefined) {
delete this.primitiveMap[key];
return;
}
var okey = Object(mn.name);
if (!this.map.has(okey))
return false;
this.map.delete(okey);
if (this.weakKeys) {
this.keys.delete(this.refs.get(okey));
this.refs.delete(okey);
}
else {
this.keys.delete(okey);
}
return true;
};
Dictionary.prototype.axGetPublicProperty = function (nm) {
if (this === this.axClass.dPrototype) {
return _super.prototype.axGetPublicProperty.call(this, nm);
}
var key = Dictionary.makePrimitiveKey(nm);
if (key !== undefined) {
return this.primitiveMap[key];
}
return this.map.get(Object(nm));
};
Dictionary.prototype.axGetEnumerableKeys = function () {
var _this = this;
if (this === this.axClass.dPrototype) {
return _super.prototype.axGetEnumerableKeys.call(this);
}
var enumerableKeys = [];
for (var k in this.primitiveMap)
enumerableKeys.push(k);
this.keys.forEach(function (value) {
var k = _this.weakKeys ? value.deref() : value;
if (k) {
enumerableKeys.push(k);
}
else {
_this.keys.delete(value);
}
});
return enumerableKeys;
};
Dictionary.classInitializer = function () {
var proto = this.dPrototype;
var asProto = Dictionary.prototype;
addPrototypeFunctionAlias(proto, '$BgtoJSON', asProto.toJSON);
};
return Dictionary;
}(ASObject));
export { Dictionary };