UNPKG

@awayfl/avm2

Version:

Virtual machine for executing AS3 code

107 lines (106 loc) 4.26 kB
import { __extends } from "tslib"; import { ASObject } from './ASObject'; import { axCoerceString } from '../run/axCoerceString'; import { axIsCallable } from '../run/axIsCallable'; import { Errors } from '../errors'; import { transformJSValueToAS } from './transformJSValueToAS'; import { transformASValueToJS } from './transformASValueToJS'; import { walk } from './walk'; var ASJSON = /** @class */ (function (_super) { __extends(ASJSON, _super); function ASJSON() { return _super !== null && _super.apply(this, arguments) || this; } ASJSON.parse = function (text, reviver) { if (reviver === void 0) { reviver = null; } text = axCoerceString(text); if (reviver !== null && !axIsCallable(reviver)) { this.sec.throwError('TypeError', Errors.CheckTypeFailedError, reviver, 'Function'); } if (text === null) { this.sec.throwError('SyntaxError', Errors.JSONInvalidParseInput); } var unfiltered; try { unfiltered = transformJSValueToAS(this.sec, JSON.parse(text), true); } catch (e) { this.sec.throwError('SyntaxError', Errors.JSONInvalidParseInput); } if (reviver === null) { return unfiltered; } return walk(this.sec, { '': unfiltered }, '', reviver.value); }; ASJSON.stringify = function (value, replacer, space) { // We deliberately deviate from ECMA-262 and throw on // invalid replacer parameter. if (replacer === void 0) { replacer = null; } if (space === void 0) { space = null; } var sec = typeof replacer === 'object' ? replacer === null || replacer === void 0 ? void 0 : replacer.sec : null; if (replacer !== null) { if (!sec || !(sec.AXFunction.axIsType(replacer) || sec.AXArray.axIsType(replacer))) { this.sec.throwError('TypeError', Errors.JSONInvalidReplacer); } } var gap; if (typeof space === 'string') { gap = space.length > 10 ? space.substring(0, 10) : space; } else if (typeof space === 'number') { gap = ' '.substring(0, Math.min(10, space | 0)); } else { // We follow ECMA-262 and silently ignore invalid space parameter. gap = ''; } if (replacer === null) { return this.stringifySpecializedToString(value, null, null, gap); } else if (sec.AXArray.axIsType(replacer)) { return this.stringifySpecializedToString(value, this.computePropertyList(replacer.value), null, gap); } else { // replacer is Function return this.stringifySpecializedToString(value, null, replacer.value, gap); } }; // ECMA-262 5th ed, section 15.12.3 stringify, step 4.b ASJSON.computePropertyList = function (r) { var propertyList = []; var alreadyAdded = Object.create(null); for (var i = 0, length_1 = r.length; i < length_1; i++) { if (!r.hasOwnProperty(i)) { continue; } var v = r[i]; var item = null; if (typeof v === 'string') { item = v; } else if (typeof v === 'number') { item = axCoerceString(v); } if (item !== null && !alreadyAdded[item]) { alreadyAdded[item] = true; propertyList.push(item); } } return propertyList; }; ASJSON.stringifySpecializedToString = function (value, replacerArray, replacerFunction, gap) { // In AS3 |JSON.stringify(undefined)| returns "null", while JS returns |undefined|. // TODO: Is there anything to be done in case of a |replacerFunction| function? if (value === undefined) { return 'null'; } var jsValue = transformASValueToJS(this.sec, value, true, replacerFunction); try { return JSON.stringify(jsValue, null, gap); } catch (e) { this.sec.throwError('TypeError', Errors.JSONCyclicStructure); } }; return ASJSON; }(ASObject)); export { ASJSON };