private void EmitClassParser(ClassNode cnode, StringBuilder sb, int level, int baseSize) { string padding = new String(' ', level * 2); sb.AppendLine(); sb.AppendLine(padding + "parse: function(buffer) {"); sb.AppendLine(padding + " var object = {};"); sb.AppendLine(); if (cnode.Parent != null) { //sb.AppendLine(padding + "\tHeader.Deserialize( stream );"); } var offset = 0; foreach (PropNode prop in cnode.childNodes) { string typestr = EmitType(prop.Type); int size = CodeGenerator.GetTypeSize(prop); string defflags = prop.Flags; string symname = "object." + prop.Name; if (defflags != null && defflags == "const") { continue; } if (size == 0) { if (prop.Flags != null && prop.Flags == "proto") { if (prop.FlagsOpt != null) { sb.AppendLine(padding + " object." + prop.Name + " = " + typestr + ".parse(buffer.slice(" + offset + ", " + offset + " + object." + prop.FlagsOpt + "));"); } else { //sb.AppendLine(padding + "\t" + GetUpperName(prop.Name) + " = ProtoBuf.Serializer.Deserialize<" + typestr + ">( stream );"); } } else { //sb.AppendLine(padding + "\t" + GetUpperName(prop.Name) + ".Deserialize( stream );"); } } else { if (!readerTypeMap.ContainsKey(typestr)) { typestr = CodeGenerator.GetTypeOfSize(size, SupportsUnsignedTypes()); } string call = "buffer.read" + readerTypeMap[typestr] + "(" + offset + ")"; if (!String.IsNullOrEmpty(prop.FlagsOpt)) { call = "buffer.slice(" + offset + ", " + offset + " + " + prop.FlagsOpt + ")"; } if (prop.Flags == "protomask" || prop.Flags == "protomaskgc") { call = call + " & ~protoMask"; } sb.AppendLine(padding + " " + symname + " = " + call + ";"); } offset += size; } sb.AppendLine(); sb.AppendLine(padding + " return object;"); sb.AppendLine(padding + "}"); }
private void EmitClassSerializer(ClassNode cnode, StringBuilder sb, int level, int baseSize) { string padding = new String(' ', level * 2); //sb.AppendLine(); sb.AppendLine(padding + "serialize: function(object) {"); // first emit variable length members List <String> varLengthProps = new List <String>(); varLengthProps.Add(baseSize.ToString()); //if (cnode.Parent != null) //{ // sb.AppendLine(padding + "\tHeader.Serialize(stream);"); // varLengthProps.Add("(int)msHeader.Length"); // openedStreams.Add("msHeader"); // sb.AppendLine(); //} foreach (PropNode prop in cnode.childNodes) { string typestr = EmitType(prop.Type); int size = CodeGenerator.GetTypeSize(prop); if (size == 0) { if (prop.Flags != null && prop.Flags == "proto") { if (baseSize == 0) { // early exit sb.AppendLine(padding + " return " + typestr + ".serialize(object." + prop.Name + " || {});"); sb.AppendLine(padding + "},"); return; } sb.AppendLine(padding + " var buf" + GetUpperName(prop.Name) + " = " + typestr + ".serialize(object." + prop.Name + " || {});"); if (prop.FlagsOpt != null) { sb.AppendLine(padding + " object." + prop.FlagsOpt + " = buf" + GetUpperName(prop.Name) + ".length;"); } } else { //sb.AppendLine(padding + "\tMemoryStream ms" + GetUpperName(prop.Name) + " = " + GetUpperName(prop.Name) + ".serialize();"); } varLengthProps.Add("buf" + GetUpperName(prop.Name) + ".length"); } } sb.AppendLine(padding + " var buffer = new Buffer(" + String.Join(" + ", varLengthProps.ToArray()) + ");"); sb.AppendLine(); if (cnode.Parent != null) { //sb.AppendLine(padding + "\tmsHeader.CopyTo( msBuffer );"); } // next emit writers var offset = 0; foreach (PropNode prop in cnode.childNodes) { string typestr = EmitType(prop.Type); int size = CodeGenerator.GetTypeSize(prop); string propName = "object." + prop.Name; if (prop.Flags != null) { if (prop.Flags == "proto") { sb.AppendLine(padding + " buf" + GetUpperName(prop.Name) + ".copy(buffer, " + offset + ");"); continue; } else if (prop.Flags == "const") { continue; } } if (prop.Flags == "protomask" || prop.Flags == "protomaskgc") { propName = propName + " | protoMask"; } if (!readerTypeMap.ContainsKey(typestr)) { typestr = CodeGenerator.GetTypeOfSize(size, SupportsUnsignedTypes()); } Symbol defsym = prop.Default; string ctor = EmitType(defsym); if (defsym == null) { ctor = "0"; } string call = "buffer.write" + readerTypeMap[typestr] + "(" + propName + " || " + ctor + ", " + offset + ");"; if (!String.IsNullOrEmpty(prop.FlagsOpt)) { call = propName + " && " + propName + ".copy(buffer, " + offset + ");"; } sb.AppendLine(padding + " " + call); offset += size; } sb.AppendLine(); sb.AppendLine(padding + " return buffer;"); sb.AppendLine(padding + "},"); }