UNPKG

@awayfl/avm2

Version:

Virtual machine for executing AS3 code

67 lines (66 loc) 2.66 kB
var _a, _b; import { Settings } from '../../Settings'; import { emitInlineStack } from './emitInlineVars'; import { axCoerceNumber } from '../../run/axCoerceNumber'; import { axCoerceInt } from '../../run/axCoerceInt'; import { axCoerceUint } from '../../run/axCoerceUint'; import { axCoerceBoolean } from '../../run/axCoerceBoolean'; import { axCoerceString } from '../../run/axCoerceString'; var typeToCoerceMapping = (_a = {}, _a['Number'] = function (x) { if (Settings.FOLLOW_AS3_BUG) { return "sec.AXNumber.axCoerce(".concat(x, ")"); } return "(+".concat(x, ")"); }, _a['int'] = function (x) { if (Settings.FOLLOW_AS3_BUG) { return "sec.AXNumber.axCoerce(".concat(x, ") | 0"); } return "(".concat(x, "|0)"); }, _a['uint'] = function (x) { return "(".concat(x, ">>>0)"); }, _a['Boolean'] = function (x) { return "(!!".concat(x, ")"); }, _a['String'] = function (x) { return "(".concat(x, "==null?null:''+ ").concat(x, ")"); }, _a); var staticCoerceMapping = (_b = {}, _b['Number'] = axCoerceNumber, _b['int'] = axCoerceInt, _b['uint'] = axCoerceUint, _b['Boolean'] = axCoerceBoolean, _b['String'] = axCoerceString, _b); export function isPrimitiveType(type) { return type && type.name && (type.name in staticCoerceMapping); } /** * Emit coercion mapping for primitive values: arguments, returns, fast coercion * @param state * @param stackIndexOrName index - when it it stack, name - when is local or not stack value * @param type * @param inline Inline coerce return name without coersion instead of null */ export function emitPrimitiveCoerce(state, stackIndexOrName, type, inline) { if (inline === void 0) { inline = false; } if (!isPrimitiveType(type)) { // no coerce if not required if (!inline) { return null; } return typeof stackIndexOrName === 'string' ? stackIndexOrName : emitInlineStack(state, stackIndexOrName); } if (typeof stackIndexOrName === 'string') { return typeToCoerceMapping[type.name](stackIndexOrName); } var constAlias = state.getConstAliasMeta(stackIndexOrName); // coerce inline for const, const will be transformed to specific type in compiler state if (constAlias && constAlias.kind === "const" /* VAR_KIND.CONST */) { var res = staticCoerceMapping[type.name](constAlias.value); if (typeof res === 'string') return JSON.stringify(res); return '' + res; } return typeToCoerceMapping[type.name](emitInlineStack(state, stackIndexOrName)); }