예제 #1
0
        public override void EmitCreation(CodeGen cg)
        {
            if (cg.IsDynamicMethod)
            {
                throw new NotSupportedException("no can do");
            }
            else
            {
                var tg = cg.TypeGen;
                var ag = tg.AssemblyGen;
                index = tg.ConstantCounter;
                var snippets = ScriptDomainManager.CurrentManager.Snippets;
                if (snippets.Assembly == ag || snippets.DebugAssembly == ag)
                {
                    var sym = Runtime.Builtins.GenSym("$z$" + index);

                    Runtime.Builtins.SetSymbolValueFast(sym, value);

                    cg.EmitSymbolId((SymbolId)sym);
                    cg.EmitCall(typeof(Runtime.Builtins), "SymbolValue");
                }
                else
                {
                    fs = tg.AddStaticField(typeof(object), FieldAttributes.Private, "$z$" + index) as StaticFieldSlot;
                    tg.SerializedConstants.Add(this);

                    var tcg = tg.TypeInitializer;

                    if (index == 0)
                    {
                        arrloc = tcg.DeclareLocal(typeof(object[]));
                        // first
                        // setup deserializtion to array, then assign to fields
                        tcg.EmitType(tg.TypeBuilder);
                        tcg.EmitCall(typeof(Runtime.Helpers), "DeserializeAssemblyConstants");

                        tcg.Emit(OpCodes.Stloc, arrloc);

                        tg.CreatingType += (sender, ea) =>
                        {
                            var constants = new List <object>();

                            foreach (SerializedConstant sc in tg.SerializedConstants)
                            {
                                constants.Add(sc.value);
                            }

                            var constantsarray = constants.ToArray();

                            MemoryStream s = new MemoryStream();

                            bf.Serialize(s, constantsarray);
                            s.Position = 0;

                            var mb = tg.TypeBuilder.Module as ModuleBuilder;
                            PAL.SerializeConstants(s, mb, compress);
                        };
                    }

                    tcg.Emit(OpCodes.Ldloc, ((SerializedConstant)tg.SerializedConstants[0]).arrloc);

                    tcg.EmitInt(index);
                    tcg.Emit(OpCodes.Ldelem_Ref);

                    fs.EmitSet(tcg);
                    fs.EmitGet(cg);
                }

                tg.ConstantCounter++;
            }
        }
예제 #2
0
        public override void EmitCreation(CodeGen cg)
        {
            if (cg.IsDynamicMethod)
            {
                throw new NotSupportedException("no can do");
            }
            else
            {
                var tg = cg.TypeGen;
                var ag = tg.AssemblyGen;
                index = tg.ConstantCounter;
                var snippets = ScriptDomainManager.CurrentManager.Snippets;
                if (snippets.Assembly == ag || snippets.DebugAssembly == ag)
                {
                    var sym = Runtime.Builtins.GenSym("s11n:" + index);

                    Runtime.Builtins.SetSymbolValueFast(sym, value);

                    cg.EmitSymbolId((SymbolId)sym);
                    cg.EmitCall(typeof(Runtime.Builtins), "SymbolValue");
                }
                else
                {
                    fs = tg.AddStaticField(typeof(object), "s11n:" + index) as StaticFieldSlot;
                    tg.SerializedConstants.Add(this);


                    var tcg = tg.TypeInitializer;

                    if (index == 0)
                    {
                        arrloc = tcg.DeclareLocal(typeof(object[]));
                        // first
                        // setup deserializtion to array, then assign to fields
                        tcg.EmitType(tg.TypeBuilder);
                        tcg.EmitCall(typeof(Runtime.Helpers), "DeserializeAssemblyConstants");

                        tcg.Emit(OpCodes.Stloc, arrloc);

                        tg.CreatingType += (sender, ea) =>
                        {
                            var constants = new List <object>();

                            foreach (SerializedConstant sc in tg.SerializedConstants)
                            {
                                constants.Add(sc.value);
                            }

                            var constantsarray = constants.ToArray();

                            MemoryStream s = new MemoryStream();

                            bf.Serialize(s, constantsarray);
                            s.Position   = 0;
                            totallength += s.Length;
                            var mb = tg.TypeBuilder.Module as ModuleBuilder;

                            if (compress)
                            {
                                var cms     = new MemoryStream();
                                var cs      = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Compress, true);
                                var content = s.ToArray();
                                cs.Write(content, 0, content.Length);
                                cs.Close();

                                mb.DefineManifestResource("SerializedConstants.gz", cms, System.Reflection.ResourceAttributes.Public);
                            }
                            else
                            {
                                mb.DefineManifestResource("SerializedConstants", s, System.Reflection.ResourceAttributes.Public);
                            }
                        };
                    }

                    tcg.Emit(OpCodes.Ldloc, ((SerializedConstant)tg.SerializedConstants[0]).arrloc);

                    tcg.EmitInt(index);
                    tcg.Emit(OpCodes.Ldelem_Ref);

                    fs.EmitSet(tcg);
                    fs.EmitGet(cg);
                }


                tg.ConstantCounter++;
            }
        }