Example #1
0
        /// <summary>
        /// Constants
        /// </summary>

        internal Slot GetOrMakeConstant(object value)
        {
            Debug.Assert(!(value is CompilerConstant));

            Slot ret;

            if (_constants.TryGetValue(value, out ret))
            {
                return(ret);
            }

            Type type = value.GetType();

            // Create a name like "c$3.141592$712"
            string name = value.ToString();

            if (name.Length > 20)
            {
                name = name.Substring(0, 20);
            }
            name = "c$" + name + "$" + _constants.Count;

            FieldBuilder fb = _myType.DefineField(name, type, FieldAttributes.Static | FieldAttributes.InitOnly);

            ret = new StaticFieldSlot(fb);

            TypeInitializer.EmitConstantNoCache(value);
            _initGen.EmitFieldSet(fb);

            _constants[value] = ret;
            return(ret);
        }
Example #2
0
        private static void MakeInitialization(LanguageInfo li, Dictionary <SymbolId, Slot> fields)
        {
            li.TypeGen.TypeBuilder.AddInterfaceImplementation(typeof(IModuleDictionaryInitialization));
            CodeGen cg = li.TypeGen.DefineExplicitInterfaceImplementation(typeof(IModuleDictionaryInitialization).GetMethod("InitializeModuleDictionary"));

            Label ok = cg.DefineLabel();

            cg.ContextSlot.EmitGet(cg);
            cg.Emit(OpCodes.Brfalse_S, ok);
            cg.EmitReturn();
            cg.MarkLabel(ok);

            cg.EmitArgGet(0);
            cg.ContextSlot.EmitSet(cg);

            foreach (KeyValuePair <SymbolId, Slot> kv in fields)
            {
                Slot             slot    = kv.Value;
                ModuleGlobalSlot builtin = slot as ModuleGlobalSlot;

                Debug.Assert(builtin != null);
                if (builtin != null)
                {
                    cg.EmitCodeContext();
                    cg.EmitSymbolId(kv.Key);
                    builtin.EmitWrapperAddr(cg);
                    cg.EmitCall(typeof(RuntimeHelpers), "InitializeModuleFieldBoxed");
                }

                StaticFieldSlot sfs = slot as StaticFieldSlot;
                if (sfs != null)
                {
                    cg.EmitCodeContext();
                    cg.EmitSymbolId(kv.Key);
                    sfs.EmitGetAddr(cg);
                    cg.EmitCall(typeof(RuntimeHelpers).GetMethod("InitializeFieldBoxed").MakeGenericMethod(sfs.Type));
                }
            }

            cg.EmitReturn();
            cg.Finish();
        }
Example #3
0
        internal Slot GetOrMakeCompilerConstant(CompilerConstant value)
        {
            Slot ret;

            if (_constants.TryGetValue(value, out ret))
            {
                return(ret);
            }

            string name = "c$" + value.Name + "$" + _constants.Count;

            FieldBuilder fb = _myType.DefineField(name, value.Type, FieldAttributes.Static | FieldAttributes.InitOnly);

            ret = new StaticFieldSlot(fb);

            value.EmitCreation(TypeInitializer);
            _initGen.EmitFieldSet(fb);

            _constants[value] = ret;
            return(ret);
        }
    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++;
      }
    }
Example #5
0
        internal Slot GetOrMakeCompilerConstant(CompilerConstant value) {
            Slot ret;
            if (_constants.TryGetValue(value, out ret)) {
                return ret;
            }

            string name = "c$" + value.Name + "$" + _constants.Count;

            FieldBuilder fb = _myType.DefineField(name, value.Type, FieldAttributes.Static | FieldAttributes.InitOnly);
            ret = new StaticFieldSlot(fb);

            value.EmitCreation(TypeInitializer);
            _initGen.EmitFieldSet(fb);

            _constants[value] = ret;
            return ret;
        }
Example #6
0
        /// <summary>
        /// Constants
        /// </summary>

        internal Slot GetOrMakeConstant(object value) {
            Debug.Assert(!(value is CompilerConstant));

            Slot ret;
            if (_constants.TryGetValue(value, out ret)) {
                return ret;
            }

            Type type = value.GetType();

            // Create a name like "c$3.141592$712"
            string name = value.ToString();
            if (name.Length > 20) {
                name = name.Substring(0, 20);
            }
            name = "c$" + name + "$" + _constants.Count;

            FieldBuilder fb = _myType.DefineField(name, type, FieldAttributes.Static | FieldAttributes.InitOnly);
            ret = new StaticFieldSlot(fb);

            TypeInitializer.EmitConstantNoCache(value);
            _initGen.EmitFieldSet(fb);

            _constants[value] = ret;
            return ret;
        }