private Node TransformObjectLiteral(ObjectLiteral node) { if (node.IsDestructuring()) { return node; } // createObjectLiteral rewrites its argument as object // creation plus object property entries, so later compiler // stages don't need to know about object literals. decompiler.AddToken(Token.LC); IList<ObjectProperty> elems = node.GetElements(); Node @object = new Node(Token.OBJECTLIT); object[] properties; if (elems.IsEmpty()) { properties = ScriptRuntime.emptyArgs; } else { int size = elems.Count; int i = 0; properties = new object[size]; foreach (ObjectProperty prop in elems) { if (prop.IsGetter()) { decompiler.AddToken(Token.GET); } else { if (prop.IsSetter()) { decompiler.AddToken(Token.SET); } } properties[i++] = GetPropKey(prop.GetLeft()); // OBJECTLIT is used as ':' in object literal for // decompilation to solve spacing ambiguity. if (!(prop.IsGetter() || prop.IsSetter())) { decompiler.AddToken(Token.OBJECTLIT); } Node right = Transform(prop.GetRight()); if (prop.IsGetter()) { right = CreateUnary(Token.GET, right); } else { if (prop.IsSetter()) { right = CreateUnary(Token.SET, right); } } @object.AddChildToBack(right); if (i < size) { decompiler.AddToken(Token.COMMA); } } } decompiler.AddToken(Token.RC); @object.PutProp(Node.OBJECT_IDS_PROP, properties); return @object; }
// only used for destructuring forms internal void DecompileObjectLiteral(ObjectLiteral node) { decompiler.AddToken(Token.LC); IList<ObjectProperty> props = node.GetElements(); int size = props.Count; for (int i = 0; i < size; i++) { ObjectProperty prop = props[i]; bool destructuringShorthand = true.Equals(prop.GetProp(Node.DESTRUCTURING_SHORTHAND)); Decompile(prop.GetLeft()); if (!destructuringShorthand) { decompiler.AddToken(Token.COLON); Decompile(prop.GetRight()); } if (i < size - 1) { decompiler.AddToken(Token.COMMA); } } decompiler.AddToken(Token.RC); }