UNPKG

@awayfl/avm2

Version:

Virtual machine for executing AS3 code

1,713 lines (1,564 loc) 117 kB
import { assert } from '@awayjs/graphics'; import { release, notImplemented, defineNonEnumerableProperty, isIndex, isNullOrUndefined, isObject, } from '@awayfl/swf-loader'; import { Namespace } from '../abc/lazy/Namespace'; import { Multiname } from '../abc/lazy/Multiname'; import { CONSTANT } from '../abc/lazy/CONSTANT'; import { internPrefixedNamespace } from '../abc/lazy/internPrefixedNamespace'; import { NamespaceType } from '../abc/lazy/NamespaceType'; import { internNamespace } from '../abc/lazy/internNamespace'; import { ASObject } from '../nat/ASObject'; import { addPrototypeFunctionAlias } from '../nat/addPrototypeFunctionAlias'; import { Errors } from '../errors'; import { Bytecode } from '../abc/ops'; import { AXSecurityDomain } from '../run/AXSecurityDomain'; import { axCoerceString } from '../run/axCoerceString'; import { checkValue } from '../run/checkValue'; import { validateCall } from '../run/validateCall'; import { getCurrentScope } from '../run/getCurrentScope'; import { AXQNameClass } from '../run/AXQNameClass'; /* tslint:disable */ /* * 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. */ /* NOTE ON E4X METHOD CALLS E4X specifies some magic when making calls on XML and XMLList values. If a callee is not found on an XMLList value and the list has only one XML child, then the call is delegated to that XML child. If a callee is not found on an XML value and that value has simple content, then the simple content is converted to a string value and the call is made on that string value. Here are the relevant texts from the spec section 11.2.2.1: "If no such property exists and base is an XMLList of size 1, CallMethod delegates the method invocation to the single property it contains. This treatment intentionally blurs the distinction between XML objects and XMLLists of size 1." "If no such property exists and base is an XML object containing no XML valued children (i.e., an attribute, leaf node or element with simple content), CallMethod attempts to delegate the method lookup to the string value contained in the leaf node. This treatment allows users to perform operations directly on the value of a leaf node without having to explicitly select it." NOTE ON E4X ANY NAME AND NAMESPACE E4X allows the names of the form x.*, x.ns::*, x.*::id and x.*::* and their attribute name counterparts x.@*, x.@ns::*, etc. These forms result in Multiname values with the name part equal to undefined in the case of an ANY name, and the namespace set being empty in the case of an ANY namespace. Note also, x.* is shorthand for x.*::* . */ export function isXMLType(val: any, sec: AXSecurityDomain): boolean { return typeof val === 'object' && val && (val.axClass === sec.AXXML || val.axClass === sec.AXXMLList || val.axClass === sec.AXQName || val.axClass === sec.AXNamespace); } export function isXMLCollection(sec: AXSecurityDomain, val: any): boolean { return typeof val === 'object' && val && (val.axClass === sec.AXXML || val.axClass === sec.AXXMLList); } // 10.1 ToString function toString(node, sec: AXSecurityDomain) { if (!node || node.axClass !== sec.AXXML) { return axCoerceString(node); } switch (node._kind) { case ASXMLKind.Text: case ASXMLKind.Attribute: return node._value; default: if (node.hasSimpleContent()) { let s = ''; for (let i = 0; i < node._children.length; i++) { const child = node._children[i]; if (child._kind === ASXMLKind.Comment || child._kind === ASXMLKind.ProcessingInstruction) { continue; } s += toString(child, sec); } return s; } return toXMLString(sec, node); } } // 10.2.1.1 EscapeElementValue ( s ) export function escapeElementValue(sec: AXSecurityDomain, s: any): string { if (isXMLCollection(sec, s)) { return s.toXMLString(); } s = axCoerceString(s); let i = 0, ch; while (i < s.length && (ch = s[i]) !== '&' && ch !== '<' && ch !== '>') { i++; } if (i >= s.length) { return s; } let buf = s.substring(0, i); while (i < s.length) { ch = s[i++]; switch (ch) { case '&': buf += '&amp;'; break; case '<': buf += '&lt;'; break; case '>': buf += '&gt;'; break; default: buf += ch; break; } } return buf; } // 10.2.1.2 EscapeAttributeValue ( s ) export function escapeAttributeValue(s: string): string { s = String(s); let i = 0, ch: string; while (i < s.length && (ch = s[i]) !== '&' && ch !== '<' && ch !== '"' && ch !== '\n' && ch !== '\r' && ch !== '\t') { i++; } if (i >= s.length) { return s; } let buf = s.substring(0, i); while (i < s.length) { ch = s[i++]; switch (ch) { case '&': buf += '&amp;'; break; case '<': buf += '&lt;'; break; case '"': buf += '&quot;'; break; case '\n': buf += '&#xA;'; break; case '\r': buf += '&#xD;'; break; case '\t': buf += '&#x9;'; break; default: buf += ch; break; } } return buf; } function isWhitespace(s: string, index: number): boolean { const ch = s[index]; return ch === ' ' || ch === '\n' || ch === '\r' || ch === '\t'; } function isWhitespaceString(s: string): boolean { release || assert(typeof s === 'string'); for (let i = 0; i < s.length; i++) { const ch = s[i]; if (!(ch === ' ' || ch === '\n' || ch === '\r' || ch === '\t')) { return false; } } return true; } function trimWhitespaces(s: string): string { let i = 0; while (i < s.length && isWhitespace(s, i)) { i++; } if (i >= s.length) { return ''; } let j = s.length - 1; while (isWhitespace(s, j)) { j--; } return i === 0 && j === s.length - 1 ? s : s.substring(i, j + 1); } const indentStringCache: string[] = []; function getIndentString(indent: number): string { if (indent > 0) { if (indentStringCache[indent] !== undefined) { return indentStringCache[indent]; } let s = ''; for (let i = 0; i < indent; i++) { s += ' '; } indentStringCache[indent] = s; return s; } return ''; } function generateUniquePrefix(namespaces: Namespace[]) { let i = 1, newPrefix: string; // eslint-disable-next-line no-constant-condition while (true) { newPrefix = '_ns' + i; if (!namespaces.some(function (ns) { return ns.prefix === newPrefix; })) { return newPrefix; } i++; } } // 10.2 ToXMLString function toXMLString(sec: AXSecurityDomain, node: any) { if (node === null || node === undefined) { throw new TypeError(); } return escapeElementValue(sec, node); } // 10.3 ToXML function toXML(v: any, sec: AXSecurityDomain) { if (v === null) { sec.throwError('TypeError', Errors.ConvertNullToObjectError); } if (v === undefined) { sec.throwError('TypeError', Errors.ConvertUndefinedToObjectError); } if (v.axClass === sec.AXXML) { return v; } if (v.axClass === sec.AXXMLList) { if (v._children.length !== 1) { sec.throwError('TypeError', Errors.XMLMarkupMustBeWellFormed); } return v._children[0]; } // The E4X spec says we must throw a TypeError for non-Boolean, Number, or String objects. // Flash thinks otherwise. const x = sec.xmlParser.parseFromString(axCoerceString(v)); const length = x._children.length; if (length === 0) { return createXML(sec, ASXMLKind.Text); } if (length === 1) { x._children[0]._parent = null; return x._children[0]; } if (length === 2) { x._children[1]._parent = null; return x._children[1]; } sec.throwError('TypeError', Errors.XMLMarkupMustBeWellFormed); } // 10.4 ToXMLList function toXMLList(value: any, targetList: ASXMLList): void { // toXMLList is supposed to just return value if it's an XMLList already. For optimization // purposes, we handle that case at the callsites. release || assert(typeof value !== 'object' || value && value.axClass !== targetList.axClass); if (value === null) { targetList.sec.throwError('TypeError', Errors.ConvertNullToObjectError); } if (value === undefined) { targetList.sec.throwError('TypeError', Errors.ConvertUndefinedToObjectError); } if (value.axClass === targetList.sec.AXXML) { targetList.append(value); return; } // The E4X spec says we must throw a TypeError for non-Boolean, Number, or String objects. // Flash thinks otherwise. const defaultNamespace = getDefaultNamespace(targetList.sec); const parentString = '<parent xmlns="' + escapeAttributeValue(defaultNamespace.uri) + '">' + value + '</parent>'; const x = toXML(parentString, targetList.sec); const children = x._children; if (!children) { return; } for (let i = 0; i < children.length; i++) { const v = children[i]; v._parent = null; targetList.append(v); } } // 10.6 ToXMLName function toXMLName(mn: string | Multiname | AXQNameClass, sec: AXSecurityDomain): Multiname { if (mn === undefined) { return anyMultiname; } let name: string; // convert argument to a value of type AttributeName or a QName object // according to the following: if (typeof mn === 'object' && mn !== null) { if (mn instanceof Multiname) { return mn; } if (mn.axClass === sec.AXQName) { // Object - If the input argument is a QName object, // return its Multiname. return mn.name; } // Object - Otherwise, convert the input argument to a string using ToString. name = String(mn); } else if (typeof mn === 'number') { name = mn + ''; } else if (typeof mn === 'string') { // String - Create a QName object or AttributeName from the String // as specified below in section 10.6.1. See below. if (mn === '*') { name = null; } else { name = mn; } } else { sec.throwError('TypeError', Errors.XMLInvalidName, mn); } // ... then convert the result to a QName object or AttributeName // as specified in section 10.6.1. if (name && name[0] === '@') { // If the first character of s is "@", ToXMLName creates an // AttributeName using the ToAttributeName operator. name = name.substr(1); if (name === '*') { name = null; } return new Multiname(null, 0, CONSTANT.QNameA, [Namespace.PUBLIC], name); } return new Multiname(null, 0, CONSTANT.QName, [Namespace.PUBLIC], name); } function coerceE4XMultiname(mn: Multiname, sec: AXSecurityDomain) { const out = tmpMultiname; out.kind = mn.kind; // Queries of the foo[new QName('bar')] sort create this situation. if (mn.name && mn.name.axClass === sec.AXQName) { mn = mn.name.name; } if (mn.isQName()) { out.name = mn.name; out.namespaces = mn.namespaces; } else { if (mn.isAnyNamespace()) { out.namespaces = mn.namespaces; } else { const defaultNS = getDefaultNamespace(sec); const namespaces = mn.namespaces; let containsDefaultNS = false; for (let i = 0; i < namespaces.length; i++) { const ns = namespaces[i]; if (ns.uri === defaultNS.uri && ns.prefix === defaultNS.prefix && ns.type === defaultNS.type) { containsDefaultNS = true; break; } } if (!containsDefaultNS) { out.namespaces = mn.namespaces.concat(defaultNS); } else { out.namespaces = mn.namespaces; } } } const name = mn.name; if (mn.isAnyName() || name === '*' || name === null) { out.name = null; } else if (name.length > 1 && name[0] === '@') { if (!out.isAttribute()) { if (name === '@*') { out.name = null; } else { out.name = name.substr(1); } out.kind = out.namespaces.length === 1 ? CONSTANT.QNameA : CONSTANT.MultinameA; } else { out.name = name; } } else { out.name = name; } return out; } // 12.1 GetDefaultNamespace function getDefaultNamespace(sec: AXSecurityDomain): Namespace { let scope = getCurrentScope(); while (scope) { if (scope.defaultNamespace) { return scope.defaultNamespace; } scope = scope.parent; } if (!sec.AXNamespace.defaultNamespace) { sec.AXNamespace.defaultNamespace = new Namespace(NamespaceType.Public, 'default', ''); } // The outermost default xml namespace is stored in sec.AXNamespace.defaultNamespace. return sec.AXNamespace.defaultNamespace; } /** * 13.3.5.4 [[GetNamespace]] ( [ InScopeNamespaces ] ) * * The [[GetNamespace]] method is an internal method that returns a Namespace object with a URI * matching the URI of this QName. InScopeNamespaces is an optional parameter. If * InScopeNamespaces is unspecified, it is set to the empty set. If one or more Namespaces * exists in InScopeNamespaces with a URI matching the URI of this QName, one of the matching * Namespaces will be returned. If no such namespace exists in InScopeNamespaces, * [[GetNamespace]] creates and returns a new Namespace with a URI matching that of this QName. * For implementations that preserve prefixes in QNames, [[GetNamespace]] may return a * Namespace that also has a matching prefix. The input argument InScopeNamespaces is a set of * Namespace objects. */ function GetNamespace(mn: Multiname, inScopeNamespaces: Namespace[]) { release || assert(mn.isQName()); const uri = mn.uri; for (let i = 0; inScopeNamespaces && i < inScopeNamespaces.length; i++) { if (uri === inScopeNamespaces[i].uri) { return inScopeNamespaces[i]; } } return mn.namespaces[0]; } // 13.1.2.1 isXMLName ( value ) export function isXMLName(v, sec: AXSecurityDomain) { try { sec.AXQName.Create(v); } catch (e) { return false; } // FIXME scan v to see if it is a valid lexeme and return false if not return true; } const tmpMultiname = new Multiname(null, 0, CONSTANT.QName, [], null, null, true); const anyMultiname = new Multiname(null, 0, CONSTANT.QName, [], null, null, true); release || Object.seal(anyMultiname); export const enum XMLParserErrorCode { NoError = 0, EndOfDocument = -1, UnterminatedCdat = -2, UnterminatedXmlDeclaration = -3, UnterminatedDoctypeDeclaration = -4, UnterminatedComment = -5, MalformedElement = -6, OutOfMemory = -7, UnterminatedAttributeValue = -8, UnterminatedElement = -9, ElementNeverBegun = -10 } export class XMLParserBase { constructor() { } private resolveEntities(s: string): string { return s.replace(/&([^;]+);/g, function (all, entity) { if (entity.substring(0, 2) === '#x') { return String.fromCharCode(parseInt(entity.substring(2), 16)); } else if (entity.substring(0, 1) === '#') { return String.fromCharCode(parseInt(entity.substring(1), 10)); } switch (entity) { case 'lt': return '<'; case 'gt': return '>'; case 'amp': return '&'; case 'quot': return '"'; } // throw "Unknown entity: " + entity; return all; }); } private parseContent(s: string, start: number): {name: string; attributes: {name: string; value: string}[]; parsed: number} { let pos = start; const attributes = []; function skipWs() { while (pos < s.length && isWhitespace(s, pos)) { ++pos; } } while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '>' && s[pos] !== '/') { ++pos; } const name = s.substring(start, pos); skipWs(); while ( pos < s.length && s[pos] !== '>' && s[pos] !== '/' && s[pos] !== '?') { skipWs(); let attrName = '', attrValue = ''; while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '=') { attrName += s[pos]; ++pos; } skipWs(); if (s[pos] !== '=') { return null; } ++pos; skipWs(); const attrEndChar = s[pos]; if (attrEndChar !== '"' && attrEndChar !== '\'') { return null; } const attrEndIndex = s.indexOf(attrEndChar, ++pos); if (attrEndIndex < 0) { return null; } attrValue = s.substring(pos, attrEndIndex); attributes.push({ name: attrName, value: this.resolveEntities(attrValue) }); pos = attrEndIndex + 1; skipWs(); } return { name: name, attributes: attributes, parsed: pos - start }; } private parseProcessingInstruction(s: string, start: number): {name: string; value: string; parsed: number} { let pos = start; function skipWs() { while (pos < s.length && isWhitespace(s, pos)) { ++pos; } } while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '>' && s[pos] !== '/') { ++pos; } const name = s.substring(start, pos); skipWs(); const attrStart = pos; while (pos < s.length && (s[pos] !== '?' || s[pos + 1] != '>')) { ++pos; } const value = s.substring(attrStart, pos); return { name: name, value: value, parsed: pos - start }; } parseXml(s: string): void { let i = 0; while (i < s.length) { const ch = s[i]; let j = i; if (ch === '<') { ++j; const ch2 = s[j]; let q: number; switch (ch2) { case '/': ++j; q = s.indexOf('>', j); if (q < 0) { this.onError(XMLParserErrorCode.UnterminatedElement); return; } this.onEndElement(s.substring(j, q)); j = q + 1; break; case '?': { ++j; const pi = this.parseProcessingInstruction(s, j); if (s.substring(j + pi.parsed, j + pi.parsed + 2) != '?>') { this.onError(XMLParserErrorCode.UnterminatedXmlDeclaration); return; } this.onPi(pi.name, pi.value); j += pi.parsed + 2; break; } case '!': if (s.substring(j + 1, j + 3) === '--') { q = s.indexOf('-->', j + 3); if (q < 0) { this.onError(XMLParserErrorCode.UnterminatedComment); return; } this.onComment(s.substring(j + 3, q)); j = q + 3; } else if (s.substring(j + 1, j + 8) === '[CDATA[') { q = s.indexOf(']]>', j + 8); if (q < 0) { this.onError(XMLParserErrorCode.UnterminatedCdat); return; } this.onCdata(s.substring(j + 8, q)); j = q + 3; } else if (s.substring(j + 1, j + 8) === 'DOCTYPE') { const q2 = s.indexOf('[', j + 8); let complexDoctype = false; q = s.indexOf('>', j + 8); if (q < 0) { this.onError(XMLParserErrorCode.UnterminatedDoctypeDeclaration); return; } if (q2 > 0 && q > q2) { q = s.indexOf(']>', j + 8); if (q < 0) { this.onError(XMLParserErrorCode.UnterminatedDoctypeDeclaration); return; } complexDoctype = true; } const doctypeContent = s.substring(j + 8, q + (complexDoctype ? 1 : 0)); this.onDoctype(doctypeContent); // XXX pull entities ? j = q + (complexDoctype ? 2 : 1); } else { this.onError(XMLParserErrorCode.MalformedElement); return; } break; default: { const content = this.parseContent(s, j); if (content === null) { this.onError(XMLParserErrorCode.MalformedElement); return; } let isClosed = false; if (s.substring(j + content.parsed, j + content.parsed + 2) === '/>') { isClosed = true; } else if (s.substring(j + content.parsed, j + content.parsed + 1) !== '>') { this.onError(XMLParserErrorCode.UnterminatedElement); return; } this.onBeginElement(content.name, content.attributes, isClosed); j += content.parsed + (isClosed ? 2 : 1); break; } } } else { do { // } while (j++ < s.length && s[j] !== '<'); const text = s.substring(i, j); this.onText(this.resolveEntities(text)); } i = j; } } onPi(_name: string, _value: string): void { } onComment(_text: string): void { } onCdata(_text: string): void { } onDoctype(_doctypeContent: string): void { } onText(_text: string): void { } onBeginElement(_name: string, _attributes: {name: string; value: string}[], _isEmpty: boolean): void { } onEndElement(_name: string): void { } onError(_code: XMLParserErrorCode): void { } } export class XMLParser extends XMLParserBase { private currentElement: ASXML; private elementsStack: ASXML[]; private scopes: any[] = []; constructor(public sec: AXSecurityDomain) { super(); } private isWhitespacePreserved(): boolean { const scopes = this.scopes; for (let j = scopes.length - 1; j >= 0; --j) { if (scopes[j].space === 'preserve') { return true; } } return false; } private lookupDefaultNs(): string { const scopes = this.scopes; for (let j = scopes.length - 1; j >= 0; --j) { if ('xmlns' in scopes[j]) { return scopes[j].xmlns; } } return ''; } private lookupNs(prefix: string): string { const scopes = this.scopes; for (let j = scopes.length - 1; j >= 0; --j) { if (prefix in scopes[j].lookup) { return scopes[j].lookup[prefix]; } } return undefined; } private getName(name: string, resolveDefaultNs: boolean): {name: string; localName: string; prefix: string; namespace: string} { const j = name.indexOf(':'); if (j >= 0) { const prefix = name.substring(0, j); const localName = name.substring(j + 1); const namespace = this.lookupNs(prefix); if (namespace === undefined) { this.sec.throwError('TypeError', Errors.XMLPrefixNotBound, prefix, localName); } return { name: namespace + '::' + localName, localName: localName, prefix: prefix, namespace: namespace, }; } else if (resolveDefaultNs) { return { name: name, localName: name, prefix: '', namespace: this.lookupDefaultNs() }; } else { return { name: name, localName: name, prefix: '', namespace: '' }; } } onError(code: XMLParserErrorCode): void { switch (code) { case XMLParserErrorCode.MalformedElement: this.sec.throwError('TypeError', Errors.XMLMalformedElement); return; case XMLParserErrorCode.UnterminatedElement: this.sec.throwError('TypeError', Errors.XMLUnterminatedElement); return; case XMLParserErrorCode.UnterminatedDoctypeDeclaration: this.sec.throwError('TypeError', Errors.XMLUnterminatedDocTypeDecl); return; case XMLParserErrorCode.UnterminatedCdat: this.sec.throwError('TypeError', Errors.XMLUnterminatedCData); return; case XMLParserErrorCode.UnterminatedComment: this.sec.throwError('TypeError', Errors.XMLUnterminatedComment); return; case XMLParserErrorCode.UnterminatedXmlDeclaration: this.sec.throwError('TypeError', Errors.XMLUnterminatedXMLDecl); return; } } onPi(name: string, value: string): void { this.pi(name, value); } onComment(text: string): void { this.comment(text); } onCdata(text: string): void { this.cdata(text); } onDoctype(doctypeContent: string): void { this.doctype(doctypeContent); } onText(text: string): void { this.text(text, this.isWhitespacePreserved()); } onBeginElement(name: string, contentAttributes: {name: string; value: string}[], isEmpty: boolean): void { const scopes = this.scopes; const scope = { namespaces: [], lookup: Object.create(null), inScopes: null }; for (let q = 0; q < contentAttributes.length; ++q) { const attribute = contentAttributes[q]; const attributeName = attribute.name; if (attributeName.substring(0, 6) === 'xmlns:') { const prefix = attributeName.substring(6); const uri = attribute.value; if (this.lookupNs(prefix) !== uri) { scope.lookup[prefix] = trimWhitespaces(uri); const ns = internPrefixedNamespace(NamespaceType.Public, uri, prefix); scope.namespaces.push(ns); } contentAttributes[q] = null; } else if (attributeName === 'xmlns') { const uri = attribute.value; if (this.lookupDefaultNs() !== uri) { scope['xmlns'] = trimWhitespaces(uri); const ns = internNamespace(NamespaceType.Public, uri); scope.namespaces.push(ns); } contentAttributes[q] = null; } else if (attributeName.substring(0, 4) === 'xml:') { const xmlAttrName = attributeName.substring(4); scope[xmlAttrName] = trimWhitespaces(attribute.value); } else { // skip ordinary attributes until all xmlns have been handled } } // build list of all namespaces including ancestors' const inScopeNamespaces: Namespace[] = []; scope.namespaces.forEach(function (ns) { if (!ns.prefix || scope.lookup[ns.prefix] === ns.uri) { inScopeNamespaces.push(ns); } }); scopes[scopes.length - 1].inScopes.forEach(function (ns: Namespace) { if ( (ns.prefix && !(ns.prefix in scope.lookup)) || (!ns.prefix && !('xmlns' in scope))) { inScopeNamespaces.push(ns); } }); scope.inScopes = inScopeNamespaces; scopes.push(scope); const attributes = []; for (let q = 0; q < contentAttributes.length; ++q) { const attribute = contentAttributes[q]; if (attribute) { attributes.push({ name: this.getName(attribute.name, false), value: attribute.value }); } } this.beginElement(this.getName(name, true), attributes, inScopeNamespaces, isEmpty); if (isEmpty) { scopes.pop(); } } onEndElement(name: string): void { this.endElement(this.getName(name, true)); this.scopes.pop(); } beginElement(name, attrs, namespaces: Namespace[], isEmpty: boolean) { const parent = this.currentElement; this.elementsStack.push(parent); this.currentElement = createXML(this.sec, ASXMLKind.Element, name.namespace, name.localName, name.prefix); for (let i = 0; i < attrs.length; ++i) { const rawAttr = attrs[i]; const attr = createXML(this.sec, ASXMLKind.Attribute, rawAttr.name.namespace, rawAttr.name.localName, rawAttr.name.prefix); attr._value = rawAttr.value; attr._parent = this.currentElement; this.currentElement._attributes.push(attr); } for (let i = 0; i < namespaces.length; ++i) { this.currentElement._inScopeNamespaces.push(namespaces[i]); } parent.insert(parent._children.length, this.currentElement); if (isEmpty) { this.currentElement = this.elementsStack.pop(); } } endElement(_name: any) { this.currentElement = this.elementsStack.pop(); } text(text: string, isWhitespacePreserve: boolean) { if (this.sec.AXXML.ignoreWhitespace) { text = trimWhitespaces(text); } // TODO: do an in-depth analysis of what isWhitespacePreserve is about. if (text.length === 0 || isWhitespacePreserve && this.sec.AXXML.ignoreWhitespace) { return; } const node = createXML(this.sec); node._value = text; this.currentElement.insert(this.currentElement._children.length, node); } cdata(text: string) { const node = createXML(this.sec); node._value = text; this.currentElement.insert(this.currentElement._children.length, node); } comment(text: string) { if (this.sec.AXXML.ignoreComments) { return; } const node = createXML(this.sec, ASXMLKind.Comment, '', ''); node._value = text; this.currentElement.insert(this.currentElement._children.length, node); } pi(name: string, value: any) { if (this.sec.AXXML.ignoreProcessingInstructions) { return; } const node = createXML(this.sec, ASXMLKind.ProcessingInstruction, '', name); node._value = value; this.currentElement.insert(this.currentElement._children.length, node); } doctype(_text) { } parseFromString(s: string, _mimeType?: string) { // placeholder const currentElement = this.currentElement = createXML(this.sec, ASXMLKind.Element, '', '', ''); this.elementsStack = []; const defaultNs = getDefaultNamespace(this.sec); const scopes: any[] = [{ namespaces: [], lookup: { 'xmlns': 'http://www.w3.org/2000/xmlns/', 'xml': 'http://www.w3.org/XML/1998/namespace' }, inScopes: [defaultNs], space: 'default', xmlns: defaultNs.uri }]; this.scopes = scopes; this.parseXml(s); this.currentElement = null; if (this.elementsStack.length > 0) { const nm = this.elementsStack.pop()._name.name; this.sec.throwError('TypeError', Errors.XMLUnterminatedElementTag, nm, nm); } this.elementsStack = null; return currentElement; } } export class ASNamespace extends ASObject implements XMLType { public static instanceConstructor: any = ASNamespace; static classInitializer() { defineNonEnumerableProperty(this, '$Bglength', 2); const proto: any = this.dPrototype; const asProto: any = ASNamespace.prototype; defineNonEnumerableProperty(proto, '$BgtoString', asProto.toString); } _ns: Namespace; /** * 13.2.1 The Namespace Constructor Called as a Function * * Namespace () * Namespace (uriValue) * Namespace (prefixValue, uriValue) */ public static axApply(_self: ASNamespace, args: any[]): ASNamespace { const a = args[0]; const b = args[1]; // 1. If (prefixValue is not specified and Type(uriValue) is Object and // uriValue.[[Class]] == "Namespace") if (args.length === 1 && isObject(a) && a.axClass === this.sec.AXNamespace) { // a. Return uriValue return a; } // 2. Create and return a new Namespace object exactly as if the Namespace constructor had // been called with the same arguments (section 13.2.2). switch (args.length) { case 0: return this.sec.AXNamespace.Create(); case 1: return this.sec.AXNamespace.Create(a); default: return this.sec.AXNamespace.Create(a, b); } } static Create(_uriOrPrefix_: any, _uri_: any): ASNamespace { const ns: ASNamespace = Object.create(this.sec.AXNamespace.tPrototype); // The initializer relies on arguments.length being correct. // eslint-disable-next-line prefer-spread, prefer-rest-params ns.axInitializer.apply(ns, arguments); return ns; } static FromNamespace(ns: Namespace) { const result: ASNamespace = Object.create(this.sec.AXNamespace.tPrototype); result._ns = ns; return result; } public static defaultNamespace = Namespace.PUBLIC; axInitializer: (uriOrPrefix_?: any, uri_?: any) => any; /** * 13.2.2 The Namespace Constructor * * Namespace () * Namespace (uriValue) * Namespace (prefixValue, uriValue) */ constructor(uriOrPrefix_?: any, uri_?: any) { super(); // 1. Create a new Namespace object n let uri: string = ''; let prefix: string = ''; // 2. If prefixValue is not specified and uriValue is not specified /* if (arguments.length === 0) { // a. Let n.prefix be the empty string // b. Let n.uri be the empty string } // 3. Else if prefixValue is not specified else */ if (arguments.length === 1) { const uriValue = uriOrPrefix_; if (uriValue instanceof Namespace) { this._ns = uriValue; return; } release || checkValue(uriValue); if (uriValue && typeof uriValue === 'object') { // Non-spec'ed, but very useful: // a. If Type(uriValue) is Object and uriValue.[[Class]] == "Namespace" if (uriValue.axClass === this.sec.AXNamespace) { const uriValueAsNamespace: ASNamespace = uriValue; // i. Let n.prefix = uriValue.prefix prefix = uriValueAsNamespace.prefix; uri = uriValueAsNamespace.uri; // b. Else if Type(uriValue) is Object and uriValue.[[Class]] == "QName" and uriValue.uri // is not null } else if (uriValue.axClass === this.sec.AXQName && (<ASQName>uriValue).uri !== null) { // i. Let n.uri = uriValue.uri uri = uriValue.uri; // NOTE implementations that preserve prefixes in qualified names may also set n.prefix // = uriValue.[[Prefix]] } } else { // c. Else // i. Let n.uri = ToString(uriValue) uri = toString(uriValue, this.sec); // ii. If (n.uri is the empty string), let n.prefix be the empty string if (uri === '') { prefix = ''; } else {// iii. Else n.prefix = undefined prefix = undefined; } } } else { // 4. Else const prefixValue = uriOrPrefix_; const uriValue = uri_; // a. If Type(uriValue) is Object and uriValue.[[Class]] == "QName" and uriValue.uri is not // null if ( isObject(uriValue) && uriValue.axClass === this.sec.AXQName && (<ASQName>uriValue).uri !== null) { // i. Let n.uri = uriValue.uri uri = uriValue.uri; } else {// b. Else // i. Let n.uri = ToString(uriValue) uri = toString(uriValue, this.sec); } // c. If n.uri is the empty string if (uri === '') { // i. If prefixValue is undefined or ToString(prefixValue) is the empty string if (prefixValue === undefined || toString(prefixValue, this.sec) === '') { // 1. Let n.prefix be the empty string prefix = ''; } else { // ii. Else throw a TypeError exception this.sec.throwError('TypeError', Errors.XMLNamespaceWithPrefixAndNoURI, prefixValue); } // d. Else if prefixValue is undefined, let n.prefix = undefined } else if (prefixValue === undefined) { prefix = undefined; // e. Else if isXMLName(prefixValue) == false } else if (isXMLName(prefixValue, this.sec) === false) { // i. Let n.prefix = undefined prefix = undefined; // f. Else let n.prefix = ToString(prefixValue) } else { prefix = toString(prefixValue, this.sec); } } // 5. Return n this._ns = internPrefixedNamespace(NamespaceType.Public, uri, prefix); } // E4X 11.5.1 The Abstract Equality Comparison Algorithm, step 3.c. equals(other: any): boolean { return other && other.axClass === this.axClass && (<ASNamespace>other)._ns.uri === this._ns.uri || typeof other === 'string' && this._ns.uri === other; } get prefix(): any { return this._ns.prefix; } get uri(): string { return this._ns.uri; } toString() { if (this === this.axClass.dPrototype) { return ''; } return this._ns.uri; } valueOf() { if (this === this.axClass.dPrototype) { return ''; } return this._ns.uri; } } export class ASQName extends ASObject implements XMLType { static classInitializer() { defineNonEnumerableProperty(this, '$Bglength', 2); const proto: any = this.dPrototype; const asProto: any = ASQName.prototype; defineNonEnumerableProperty(proto, '$BgtoString', asProto.ecmaToString); } static Create(_nameOrNS_: any, _name_?: any, _isAttribute?: boolean): ASQName { const name: ASQName = Object.create(this.sec.AXQName.tPrototype); // The initializer relies on arguments.length being correct. // eslint-disable-next-line prefer-spread, prefer-rest-params name.axInitializer.apply(name, arguments); return name; } static FromMultiname(mn: Multiname) { const name: ASQName = Object.create(this.sec.AXQName.tPrototype); name.name = mn; return name; } axInitializer: (nameOrNS_?: any, name_?: any) => any; /** * 13.3.1 The QName Constructor Called as a Function * * QName ( ) * QName ( Name ) * QName ( Namespace , Name ) */ public static axApply(_self: ASNamespace, args: any[]): ASQName { const nameOrNS_ = args[0]; const name_ = args[1]; // 1. If Namespace is not specified and Type(Name) is Object and Name.[[Class]] == “QName” if (args.length === 1 && nameOrNS_ && nameOrNS_.axClass === this.sec.AXQName) { // a. Return Name return nameOrNS_; } // 2. Create and return a new QName object exactly as if the QName constructor had been // called with the same arguments (section 13.3.2). switch (args.length) { case 0: return this.sec.AXQName.Create(); case 1: return this.sec.AXQName.Create(nameOrNS_); default: return this.sec.AXQName.Create(nameOrNS_, name_); } } name: Multiname; /** * 13.3.2 The QName Constructor * * new QName () * new QName (Name) * new QName (Namespace, Name) */ constructor(nameOrNS_?: any, name_?: any) { super(); let name: any; let namespace: any; if (arguments.length === 0) { name = ''; } else if (arguments.length === 1) { name = nameOrNS_; } else { // if (arguments.length === 2) { namespace = nameOrNS_; name = name_; } // 1. If (Type(Name) is Object and Name.[[Class]] == "QName") if (name && name.axClass === this.sec.AXQName) { // a. If (Namespace is not specified), return a copy of Name if (arguments.length < 2) { release || assert(name !== tmpMultiname); this.name = (<ASQName>name).name; return; // b. Else let Name = Name.localName } else { name = (<ASQName>name).localName; } } // 2. If (Name is undefined or not specified) if (name === undefined) { // a. Let Name = "" name = ''; // 3. Else let Name = ToString(Name) } else { name = toString(name, this.sec); } // 4. If (Namespace is undefined or not specified) if (namespace === undefined) { // a. If Name = "*" if (name === '*') { // i. Let Namespace = null namespace = null; } else {// b. Else // i. Let Namespace = GetDefaultNamespace() namespace = getDefaultNamespace(this.sec); } } // 5. Let q be a new QName with q.localName = Name const localName = name; let ns: Namespace = null; // 6. If Namespace == null if (namespace !== null) { // a. Let Namespace be a new Namespace created as if by calling the constructor new // Namespace(Namespace) if (namespace.axClass !== this.sec.AXNamespace) { namespace = this.sec.AXNamespace.Create(namespace); } ns = namespace._ns; //// b. Let q.uri = Namespace.uri //uri = namespace.uri; // NOTE implementations that preserve prefixes in qualified names may also set // q.[[Prefix]] to Namespace.prefix //} else { // a. Let q.uri = null // NOTE implementations that preserve prefixes in qualified names may also set q.[[Prefix]] // to undefined //uri = null; } // 8. Return q this.name = new Multiname(null, 0, CONSTANT.QName, [ns], localName); } // E4X 11.5.1 The Abstract Equality Comparison Algorithm, step 3.b. equals(other: any): boolean { return other && other.axClass === this.sec.AXQName && (<ASQName>other).uri === this.uri && (<ASQName>other).name.name === this.name.name || typeof other === 'string' && this.toString() === other; } get localName(): string { return this.name.name != '' ? this.name.name : null; } get uri(): string { const namespaces = this.name.namespaces; return namespaces.length > 1 ? '' : namespaces[0] ? namespaces[0].uri : null; } ecmaToString(): string { if (this && <any> this === this.sec.AXQName.dPrototype) { return ''; } if (!(this && this.axClass === this.sec.AXQName)) { this.sec.throwError('TypeError', Errors.InvokeOnIncompatibleObjectError, 'QName.prototype.toString'); } return this.toString(); } toString() { let uri = this.uri; if (uri === '') { return this.name.name; } if (uri === null) { return '*::' + this.name.name; } uri = uri + ''; const cc = uri.charCodeAt(uri.length - 1); // strip the version mark, if there is one let base_uri = uri; if (cc >= 0xE000 && cc <= 0xF8FF) { base_uri = uri.substr(0, uri.length - 1); } if (base_uri === '') { return this.name.name; } // causes issues in nitrome-games: // return base_uri + "::" + this.name.name; return this.name.name; } valueOf() { return this; } /** * 13.3.5.3 [[Prefix]] * The [[Prefix]] property is an optional internal property that is not directly visible to * users. It may be used by implementations that preserve prefixes in qualified names. The * value of the [[Prefix]] property is a value of type string or undefined. If the [[Prefix]] * property is undefined, the prefix associated with this QName is unknown. */ get prefix(): string { return this.name.namespaces[0] ? this.name.namespaces[0].prefix : null; } } enum ASXML_FLAGS { FLAG_IGNORE_COMMENTS = 0x01, FLAG_IGNORE_PROCESSING_INSTRUCTIONS = 0x02, FLAG_IGNORE_WHITESPACE = 0x04, FLAG_PRETTY_PRINTING = 0x08, ALL = FLAG_IGNORE_COMMENTS | FLAG_IGNORE_PROCESSING_INSTRUCTIONS | FLAG_IGNORE_WHITESPACE | FLAG_PRETTY_PRINTING } // Note: the order of the entries is relevant, because some checks are of // the form `type > ASXMLKind.Element`. export const enum ASXMLKind { Element = 1, Attribute = 2, Text = 3, Comment = 4, ProcessingInstruction = 5 } const ASXMLKindNames = [null, 'element', 'attribute', 'text', 'comment', 'processing-instruction']; export interface XMLType { equals(other: any): boolean; axClass: any; } export class ASXML extends ASObject implements XMLType { public static instanceConstructor: any = ASXML; static classInitializer() { defineNonEnumerableProperty(this, '$Bglength', 1); const proto: any = this.dPrototype; const asProto: any = ASXML.prototype; addPrototypeFunctionAlias(proto, '$BgvalueOf', asProto.valueOf); defineNonEnumerableProperty(proto, '$BghasOwnProperty', asProto.native_hasOwnProperty); defineNonEnumerableProperty(proto, '$BgpropertyIsEnumerable', asProto.native_propertyIsEnumerable); addPrototypeFunctionAlias(<any> this, '$Bgsettings', ASXML.native_settings); addPrototypeFunctionAlias(<any> this, '$BgsetSettings', ASXML.native_setSettings); addPrototypeFunctionAlias(<any> this, '$BgdefaultSettings', ASXML.native_defaultSettings); addPrototypeFunctionAlias(proto, '$BgtoString', asProto.toString); addPrototypeFunctionAlias(proto, '$BgaddNamespace', asProto.addNamespace); addPrototypeFunctionAlias(proto, '$BgappendChild', asProto.appendChild); addPrototypeFunctionAlias(proto, '$Bgattribute', asProto.attribute); addPrototypeFunctionAlias(proto, '$Bgattributes', asProto.attributes); addPrototypeFunctionAlias(proto, '$Bgchild', asProto.child); addPrototypeFunctionAlias(proto, '$BgchildIndex', asProto.childIndex); addPrototypeFunctionAlias(proto, '$Bgchildren', asProto.children); addPrototypeFunctionAlias(proto, '$Bgcomments', asProto.comments); addPrototypeFunctionAlias(proto, '$Bgcontains', asProto.contains); addPrototypeFunctionAlias(proto, '$Bgcopy', asProto.copy); addPrototypeFunctionAlias(proto, '$Bgdescendants', asProto.descendants); addPrototypeFunctionAlias(proto, '$Bgelements', asProto.elements); addPrototypeFunctionAlias(proto, '$BghasComplexContent', asProto.hasComplexContent); addPrototypeFunctionAlias(proto, '$BghasSimpleContent', asProto.hasSimpleContent); addPrototypeFunctionAlias(proto, '$BginScopeNamespaces', asProto.inScopeNamespaces); addPrototypeFunctionAlias(proto, '$BginsertChildAfter', asProto.insertChildAfter); addPrototypeFunctionAlias(proto, '$BginsertChildBefore', asProto.insertChildBefore); addPrototypeFunctionAlias(proto, '$Bglength', asProto.length); addPrototypeFunctionAlias(proto, '$BglocalName', asProto.localName); addPrototypeFunctionAlias(proto, '$Bgname', asProto.name); addPrototypeFunctionAlias(proto, '$Bgnamespace', asProto.namespace); addPrototypeFunctionAlias(proto, '$BgnamespaceDeclarations', asProto.namespaceDeclarations); addPrototypeFunctionAlias(proto, '$BgnodeKind', asProto.nodeKind); addPrototypeFunctionAlias(proto, '$Bgnormalize', asProto.normalize); addPrototypeFunctionAlias(proto, '$Bgparent', asProto.parent); addPrototypeFunctionAlias(proto, '$BgprocessingInstructions', asProto.processingInstructions); addPrototypeFunctionAlias(proto, '$BgprependChild', asProto.prependChild); addPrototypeFunctionAlias(proto, '$BgremoveNamespace', asProto.removeNamespace); addPrototypeFunctionAlias(proto, '$Bgreplace', asProto.replace); addPrototypeFunctionAlias(proto, '$BgsetChildren', asProto.setChildren); addPrototypeFunctionAlias(proto, '$BgsetLocalName', asProto.setLocalName); addPrototypeFunctionAlias(proto, '$BgsetName', asProto.setName); addPrototypeFunctionAlias(proto, '$BgsetNamespace', asProto.setNamespace); addPrototypeFunctionAlias(proto, '$Bgtext', asProto.text); addPrototypeFunctionAlias(proto, '$BgtoXMLString', asProto.toXMLString); addPrototypeFunctionAlias(proto, '$BgtoJSON', asProto.toJSON); } static Create(value?: any): ASXML { const xml: ASXML = Object.create(this.sec.AXXML.tPrototype); xml.axInitializer(value); return xml; } static resetSettings() { this._flags = ASXML_FLAGS.ALL; } axInitializer: (value?: any) => any; static native_settings(): Object { const settings = Object.create(this.sec.AXObject.tPrototype); settings.$BgignoreComments = this.ignoreComments; settings.$BgignoreProcessingInstructions = this.ignoreProcessingInstructions; settings.$BgignoreWhitespace = this.ignoreWhitespace; settings.$BgprettyPrinting = this.prettyPrinting; settings.$BgprettyIndent = this.prettyIndent; return settings; } static native_setSettings(o: any): void { if (isNullOrUndefined(o)) { this.ignoreComments = true; this.ignoreProcessingInstructions = true; this.ignoreWhitespace = true; this.prettyPrinting = true; this.prettyIndent = 2; return; } if (typeof o.$BgignoreComments === 'boolean') { this.ignoreComments = o.$BgignoreComments; } if (typeof o.$BgignoreProcessingInstructions === 'boolean') { this.ignoreProcessingInstructions = o.$BgignoreProcessingInstructions; } if (typeof o.$BgignoreWhitespace === 'boolean') { this.ignoreWhitespace = o.$BgignoreWhitespace; } if (o.$BgprettyPrinting === 'boolean') { this.prettyPrinting = o.$BgprettyPrinting; } if (o.$BgprettyIndent === 'number') { this.prettyIndent = o.$BgprettyIndent; } } static native_defaultSettings(): Object { return { __proto__: this.sec.AXObject.tPrototype, $BgignoreComments: true, $BgignoreProcessingInstructions: true, $BgignoreWhitespace: true, $BgprettyPrinting: true, $BgprettyIndent: 2 }; } private static _flags: ASXML_FLAGS = ASXML_FLAGS.ALL; private static _prettyIndent = 2; _attributes: ASXML[]; _inScopeNamespaces: Namespace[]; // These properties are public so ASXMLList can access them. _kind: ASXMLKind; _name: Multiname; _value: any; _parent: ASXML; _children: ASXML[]; public static axApply(self: ASXML, args: any[]): ASXML { let value = args[0]; // 13.5.1 The XMLList Constructor Called as a Function if (isNullOrUndefined(value)) { value = ''; } return toXML(value, this.sec); } constructor (value?: any) { super(); this._parent = null; if (isNullOrUndefined(value)) { value = ''; } if (typeof value === 'string' && value.length === 0) { this._kind = ASXMLKind.Text; this._value = ''; return; } let x = toXML(value, this.sec); if (isXMLType(value, this.sec)) { x = x._deepCopy(); } this._kind = x._kind; this._name = x._name; this._value = x._value; this._attributes = x._attributes; this._inScopeNamespaces = x._inScopeNamespaces; const children = x._children; this._children = children; if (children) { for (let i = 0; i < children.length; i++) { const child = children[i]; child._parent = this; } } } valueOf() { return this; } // E4X 11.5.1 The Abstract Equality Comparison Algorithm, steps 1-4. equals(other: any): boolean { // Steps 1,2. if (other && other.axClass === this.sec.AXXMLList) { return other.equals(this); } // Step 3. if (other && other.axClass === this.sec.AXXML) { // Step 3.a.i. const otherXML = <ASXML>other; if ((this._kind === ASXMLKind.Text || this._kind === ASXMLKind.Attribute) && otherXML.hasSimpleContent() || (otherXML._kind === ASXMLKind.Text || otherXML._kind === ASXMLKind.Attribute) && this.hasSimpleContent()) { return this.toString() === other.toString(); } // Step 3.a.ii. return this._deepEquals(other); // The rest of step 3 is implemented in {Namespace,QName}.equals and non-E4X parts of the // engine. } // Step 4. return this.hasSimpleContent() && this.toString() === axCoerceString(other); // The remaining steps are implemented by other means in the interpreter/compiler. } init(kind: number, mn: Multiname) { this._name = mn; this._kind = kind; // E4X [[Class]] this._parent = null; switch (<ASXMLKind> kind) { case ASXMLKind.Element: this._inScopeNamespaces = []; this._attributes = []; this._children = []; // child nodes go here break; case ASXMLKind.Comment: case ASXMLKind.ProcessingInstruction: case ASXMLKind.Attribute: case ASXMLKind.Text: this._value = ''; break; default: break; } return this; } // 9.1.1.9 [[Equals]] (V) _deepEquals(V: XMLType) { // Step 1. if (!V || V.axClass !== this.sec.AXXML) { return false; } const other = <ASXML>V; // Step 2. if (this._kind !== other._kind) { return false; } // Steps 3-4. if (!!this._name !== !!other._name || (this._name && !this._name.equalsQName(other._name))) { return false; } // Not in the spec, but a substantial optimization. if (this._kind !== ASXMLKind.Element) { // Step 7. // This only affects non-Element nodes, so moved up here. if (this._value !== other._value) { return false; } return true; } // Step 5. const attributes = this._attributes; const otherAttributes = other._attributes; if (attributes.length !== otherAttributes.length) { return false; } // Step 6. const children = this._children; const otherChildren = other._children; if (children.length !== otherChildren.length) { return false; } // Step 8. attribOuter: for (let i = 0; i < attributes.length; i++) { const attribute = attributes[i]; for (let j = 0; j < otherAttributes.length; j++) { const otherAttribute = otherAttributes[j]; if ( otherAttribute._name.equalsQName(attribute._name) && otherAttribute._value === attribute._value) { continue attribOuter; } } return false; } // Step 9. for (let i = 0; i < children.length; i++) { if (!children[i].equals(otherChildren[i])) { return false; } } // Step 10. return true; } // 9.1.1.7 [[DeepCopy]] ( ) _deepCopy(): ASXML { const kind: ASXMLKind = this._kind; const clone = this.sec.AXXML.Create();