private static void RegisterSimpleEnum(Type enumType, RuntimeEnvironment environment)
        {
            var enumTypeAttribute = (EnumerationTypeAttribute)enumType.GetCustomAttributes(typeof(EnumerationTypeAttribute), false)[0];

            var type = TypeManager.RegisterType("Перечисление" + enumTypeAttribute.Name, typeof(EnumerationContext));

            if (enumTypeAttribute.Alias != null)
            {
                TypeManager.RegisterAliasFor(type, "Enum" + enumTypeAttribute.Alias);
            }

            var enumValueType = TypeManager.RegisterType(enumTypeAttribute.Name, enumType);

            var instance = new EnumerationContext(type, enumValueType);

            var wrapperTypeUndefined = typeof(CLREnumValueWrapper <>);
            var wrapperType          = wrapperTypeUndefined.MakeGenericType(new Type [] { enumType });
            var constructor          = wrapperType.GetConstructor(new Type [] { typeof(EnumerationContext), enumType, typeof(DataType) });

            foreach (var field in enumType.GetFields())
            {
                foreach (var contextFieldAttribute in field.GetCustomAttributes(typeof(EnumItemAttribute), false))
                {
                    var contextField = (EnumItemAttribute)contextFieldAttribute;
                    var osValue      = (EnumerationValue)constructor.Invoke(new object [] { instance, field.GetValue(null), DataType.Enumeration });

                    if (contextField.Alias == null)
                    {
                        if (StringComparer
                            .InvariantCultureIgnoreCase
                            .Compare(field.Name, contextField.Name) != 0)
                        {
                            instance.AddValue(contextField.Name, field.Name, osValue);
                        }
                        else
                        {
                            instance.AddValue(contextField.Name, osValue);
                        }
                    }
                    else
                    {
                        instance.AddValue(contextField.Name, contextField.Alias, osValue);
                    }
                }
            }

            if (enumTypeAttribute.CreateGlobalProperty)
            {
                GlobalsManager.RegisterInstance(enumType, instance);
                environment.InjectGlobalProperty(instance, enumTypeAttribute.Name, true);
                if (enumTypeAttribute.Alias != null)
                {
                    environment.InjectGlobalProperty(instance, enumTypeAttribute.Alias, true);
                }
            }
        }
        public ApplicationInstance CreateApp()
        {
            var codeSrc = new FileInfoCodeSource(_scripts.GetFileInfo("main.os"));

            _webEng.Environment.InjectObject(new WebGlobalContext(this, codeSrc, _webEng));

            var templateFactory = new DefaultTemplatesFactory();

            var storage = new TemplateStorage(templateFactory);

            _webEng.Environment.InjectObject(storage);
            _webEng.Engine.UpdateContexts();
            GlobalsManager.RegisterInstance(storage);

            return(ApplicationInstance.Create(codeSrc, _webEng));
        }
        private static void RegisterSystemEnum(Type enumType, RuntimeEnvironment environment)
        {
            var method = enumType.GetMethod(INSTANCE_RETRIEVER_NAME, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

            System.Diagnostics.Trace.Assert(method != null, "System enum must have a static method " + INSTANCE_RETRIEVER_NAME);

            var instance = (IValue)method.Invoke(null, null);

            GlobalsManager.RegisterInstance(instance);
            var enumMetadata = (SystemEnumAttribute)enumType.GetCustomAttributes(typeof(SystemEnumAttribute), false)[0];

            environment.InjectGlobalProperty(instance, enumMetadata.GetName(), true);
            if (enumMetadata.GetAlias() != String.Empty)
            {
                environment.InjectGlobalProperty(instance, enumMetadata.GetAlias(), true);
            }
        }
        private static void RegisterGlobalContext(Type contextType, RuntimeEnvironment environment)
        {
            var attribData = (GlobalContextAttribute)contextType.GetCustomAttributes(typeof(GlobalContextAttribute), false)[0];

            if (attribData.ManualRegistration)
            {
                return;
            }

            var method = contextType.GetMethod(INSTANCE_RETRIEVER_NAME, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

            System.Diagnostics.Trace.Assert(method != null, "Global context must have a static method " + INSTANCE_RETRIEVER_NAME);
            var instance = (IAttachableContext)method.Invoke(null, null);

            GlobalsManager.RegisterInstance(instance);
            environment.InjectObject(instance, false);
        }
        public HostedScriptEngine()
        {
            _engine = new ScriptingEngine();
            _env    = new RuntimeEnvironment();
            _engine.AttachAssembly(System.Reflection.Assembly.GetExecutingAssembly(), _env);

            _globalCtx = new SystemGlobalContext();
            _globalCtx.EngineInstance = _engine;

            _env.InjectObject(_globalCtx, false);

            InitializationCallback = (eng, env) =>
            {
                var templateFactory = new DefaultTemplatesFactory();
                var storage         = new TemplateStorage(templateFactory);
                env.InjectObject(storage);
                GlobalsManager.RegisterInstance(storage);
            };

            _engine.Environment = _env;
        }
        public Process CreateProcess(Stream sourceStream, IHostApplication host)
        {
            var appDump         = DeserializeAppDump(sourceStream);
            var engine          = new HostedScriptEngine();
            var src             = new BinaryCodeSource();
            var templateStorage = new TemplateStorage(new StandaloneTemplateFactory());

            engine.SetGlobalEnvironment(host, src);
            engine.InitializationCallback = (e, env) =>
            {
                e.Environment.InjectObject(templateStorage);
                GlobalsManager.RegisterInstance(templateStorage);
            };
            engine.Initialize();

            LoadResources(templateStorage, appDump.Resources);
            LoadScripts(engine, appDump.Scripts);

            var process = engine.CreateProcess(host, appDump.Scripts[0].Image, src);

            return(process);
        }
        public int Run()
        {
            if (_sourceStream == null && CommandLineArguments != null && CommandLineArguments.Length > 1)
            {
                var firstArg = CommandLineArguments[0];
                if (firstArg == "-loadDump")
                {
                    var path = CommandLineArguments[1];
                    CommandLineArguments = CommandLineArguments.Skip(2).ToArray();
                    using (var dumpStream = new FileStream(path, FileMode.Open))
                    {
                        _sourceStream = GetCodeStream(dumpStream);
                    }

                    return(Run()); //ну да, говнокод и лапша, время жмет
                }
            }

            if (_sourceStream == null)
            {
                _sourceStream = LocateCode();
            }

            var engine = new HostedScriptEngine();
            var src    = new BinaryCodeSource();

            engine.SetGlobalEnvironment(this, src);

            try
            {
                var templateStorage = new TemplateStorage(new StandaloneTemplateFactory());
                engine.InitializationCallback = (e, env) =>
                {
                    e.Environment.InjectObject(templateStorage);
                    GlobalsManager.RegisterInstance(templateStorage);
                };

                engine.Initialize();

                var serializer = new BinaryFormatter();
                var appDump    = (ApplicationDump)serializer.Deserialize(_sourceStream);
                foreach (var resource in appDump.Resources)
                {
                    templateStorage.RegisterTemplate(resource.ResourceName, DeserializeTemplate(resource.Data));
                }
                var module = appDump.Scripts[0].Image;
                for (int i = 1; i < appDump.Scripts.Length; i++)
                {
                    engine.LoadUserScript(appDump.Scripts[i]);
                }

                var process = engine.CreateProcess(this, module, src);

                return(process.Start());
            }
            catch (ScriptInterruptionException e)
            {
                return(e.ExitCode);
            }
            catch (Exception e)
            {
                ShowExceptionInfo(e);
                return(1);
            }
        }