Exemplo n.º 1
0
        public string RunCommand(string module, string command, string data, Requester requester)
        {
            LoadedModule mod = null;

            if (!loadedModules.TryGetValue(module, out mod))
            {
                throw new BadRequestException($"No module with name {module}");
            }

            var cmdfuncname = $"command_{command}";

            lock (moduleLocks.GetOrAdd(module, s => new object()))
            {
                if (!mod.script.Globals.Keys.Any(x => x.String == cmdfuncname))
                {
                    throw new BadRequestException($"No command '{command}' in module {module}");
                }

                using (mod.dataConnection = new SqliteConnection(config.ModuleDataConnectionString))
                {
                    mod.dataConnection.Open();
                    mod.currentUser    = requester.userId;
                    mod.currentCommand = command;
                    mod.currentData    = data;
                    DynValue res = mod.script.Call(mod.script.Globals[cmdfuncname], requester.userId, data);
                    return(res.String);
                }
            }
        }
        private IRuntimeContextInstance LoadAndCreate(CompilerService compiler, Environment.ICodeSource code, ExternalContextData externalContext)
        {
            var module       = CompileModuleFromSource(compiler, code, externalContext);
            var loadedHandle = new LoadedModule(module);

            return(_engine.NewObject(loadedHandle, externalContext));
        }
Exemplo n.º 3
0
        public ScriptedViewComponent(LoadedModule module, string dynamicTypeName) : base(module, true)
        {
            var td = TypeManager.RegisterType(dynamicTypeName, typeof(ScriptedViewComponent));

            DefineType(td);
            InitOwnData();


            var methId = GetScriptMethod(InvokeMethodNameRu, InvokeMethodNameEn);

            if (methId == -1)
            {
                throw new RuntimeException("Invoke method not found");
            }

            object Invoker(IDictionary <string, object> arguments)
            {
                var args   = MapArguments(methId, arguments);
                var result = CallScriptMethod(methId, args);

                return(CustomMarshaller.ConvertToCLRObject(result));
            }

            _invoker = Invoker;
        }
Exemplo n.º 4
0
        public ScriptedViewComponent(LoadedModule module, string dynamicTypeName) : base(module, true)
        {
            var td = TypeManager.RegisterType(dynamicTypeName, typeof(ScriptedViewComponent));

            DefineType(td);
            InitOwnData();
        }
Exemplo n.º 5
0
 public AuthorizationModule(LoadedModule module,
                            IFileProvider filesystem,
                            IApplicationRuntime runtime) : base(module)
 {
     _filesystem = filesystem;
     _runtime    = runtime;
 }
Exemplo n.º 6
0
        public ScriptedController(ControllerContext context, LoadedModule module) : base(module, true)
        {
            _ctx         = context;
            HttpRequest  = new HttpRequestImpl(_ctx.HttpContext.Request);
            HttpResponse = new HttpResponseImpl(_ctx.HttpContext.Response);

            if (_ctx.RouteData != null)
            {
                RouteValues = new MapImpl();
                foreach (var routeData in _ctx.RouteData.Values)
                {
                    var rv = RouteValues.AsObject();
                    rv.SetIndexedValue(
                        ValueFactory.Create(routeData.Key),
                        CustomMarshaller.ConvertToIValueSafe(routeData.Value, routeData.Value?.GetType())
                        );
                }
            }
            else
            {
                RouteValues = ValueFactory.Create();
            }

            var typeClr = (Type)context.ActionDescriptor.Properties["type"];
            var type    = TypeManager.RegisterType("Контроллер." + typeClr.Name, typeof(ScriptedController));

            DefineType(type);
            InitOwnData();
        }
Exemplo n.º 7
0
    /// <summary>
    /// Parse a single subcommand from a loaded module
    /// </summary>
    /// <param name="module"></param>
    /// <param name="subkey"></param>
    /// <returns></returns>
    public ModuleSubcommandInfo?ParseSubcommandInfo(LoadedModule module, string subkey)
    {
        var subcommands = module.script.Globals.Get(config.SubcommandVariable)?.Table;

        //No point continuing if there's no subcommands
        if (subcommands == null)
        {
            return(null);
        }

        var table = subcommands.Get(subkey)?.Table;

        if (table == null)
        {
            logger.LogWarning($"Key {subkey} in subcommands table didn't map to a value!");
            return(null);
        }

        //This is interesting, because we want to return SOME module info if there was a valid subkey...
        var subcommand = ParseSubcommandInfo(table) ?? new ModuleSubcommandInfo();

        subcommand.FunctionName ??= config.DefaultSubcommandPrepend + subkey;

        return(subcommand);
    }
Exemplo n.º 8
0
    /// <summary>
    /// Retrieve a pre-parsed list of all subcommands for the given module
    /// </summary>
    /// <param name="module"></param>
    /// <returns></returns>
    public Dictionary <string, ModuleSubcommandInfo?>?ParseAllSubcommands(LoadedModule module)
    {
        if (module == null)
        {
            return(null);
        }

        var subcommands = module.script.Globals.Get(config.SubcommandVariable)?.Table;

        //There is a subcommand variable, which means we may need to parse the input and call
        //a different function than the default!
        if (subcommands == null)
        {
            return(null);
        }

        var result = new Dictionary <string, ModuleSubcommandInfo?>();

        foreach (var key in subcommands.Keys)
        {
            var subname = key.String;

            if (subname == null)
            {
                logger.LogWarning($"Key {key} in subcommands table wasn't string value!");
                continue;
            }

            result.Add(subname, ParseSubcommandInfo(module, subname));
        }

        return(result);
    }
Exemplo n.º 9
0
        private ReflectedClassType(LoadedModule module, string typeName)
            : base(typeof(object))
        {
            _typeName = typeName;

            ReflectVariables(module);
            ReflectMethods(module);
        }
Exemplo n.º 10
0
        public IRuntimeContextInstance NewObject(LoadedModule module, ExternalContextData externalContext = null)
        {
            var scriptContext = CreateUninitializedSDO(module, externalContext);

            InitializeSDO(scriptContext);

            return(scriptContext);
        }
Exemplo n.º 11
0
        internal IRuntimeContextInstance NewObject(LoadedModule module)
        {
            var scriptContext = new Machine.Contexts.UserScriptContextInstance(module);

            scriptContext.Initialize(_machine);

            return(scriptContext);
        }
Exemplo n.º 12
0
        private Process InitProcess(IHostApplication host, LoadedModule module)
        {
            Initialize();

            var process = new Process(host, module, _engine);

            return(process);
        }
Exemplo n.º 13
0
        internal ReflectableSDO(ScriptDrivenObject instance, LoadedModule module)
        {
            _module   = module;
            _instance = instance;

            GatherProperties();
            GatherMethods();
        }
Exemplo n.º 14
0
 /// <summary>
 /// Assuming we have an already-setup loaded module, update the in-memory cache
 /// </summary>
 /// <param name="name"></param>
 /// <param name="module"></param>
 protected void UpdateLoadedModule(string name, LoadedModule module)
 {
     lock (moduleLocks.GetOrAdd(name, s => new object()))
     {
         //loadedModules is a concurrent dictionary and thus adding is thread-safe
         loadedModules[name] = module;
     }
 }
Exemplo n.º 15
0
        private LibraryLoader(LoadedModule moduleHandle, RuntimeEnvironment env, ScriptingEngine engine) : base(moduleHandle)
        {
            _env        = env;
            _engine     = engine;
            _customized = true;

            _engine.InitializeSDO(this);
        }
Exemplo n.º 16
0
 private void CatchupModuleStartup(LoadedModule Module)
 {
     for (byte i = (byte)Module.CurrentPhase; i < (byte)MostRecentStartup; i++)
     {
         Logger.Info("Catching Module {0} up to Phase {1}", Module.Module.GetType().FullName, ((StartupPhase)i).ToString());
         StartupModuleToPhase(Module, (StartupPhase)i);
     }
 }
Exemplo n.º 17
0
        public ScriptedMiddlewareActivator(RequestDelegate next, IFileProvider scripts, IApplicationRuntime runtime, string scriptName)
        {
            _next    = next;
            _runtime = runtime;
            var codeSrc = new FileInfoCodeSource(scripts.GetFileInfo(scriptName));
            var image   = ScriptedMiddleware.CompileModule(runtime.Engine.GetCompilerService(), codeSrc);

            _module = new LoadedModule(image);
        }
Exemplo n.º 18
0
 protected ScriptDrivenObject(LoadedModule module, bool deffered)
     : base(TypeManager.GetTypeByName("Object"))
 {
     _module = module;
     if (!deffered)
     {
         InitOwnData();
     }
 }
Exemplo n.º 19
0
        internal ReflectableSDO(ScriptDrivenObject instance, LoadedModule module)
        {
            _module = module;
            _instance = instance;

            GatherProperties();
            GatherMethods();

        }
Exemplo n.º 20
0
 internal ScriptDrivenObject(LoadedModule module, bool deffered)
     : base(TypeManager.GetTypeByName("Object"))
 {
     _module = module;
     if (!deffered)
     {
         InitOwnData();
     }
 }
Exemplo n.º 21
0
        internal IRuntimeContextInstance NewObject(LoadedModule module)
        {
            var scriptContext = new Machine.Contexts.UserScriptContextInstance(module, "Сценарий");

            scriptContext.AddProperty("ЭтотОбъект", scriptContext);
            scriptContext.InitOwnData();
            InitializeSDO(scriptContext);

            return(scriptContext);
        }
Exemplo n.º 22
0
 private void ReflectMethods(LoadedModule module)
 {
     _methods = new System.Reflection.MethodInfo[module.Methods.Length];
     for (int i = 0; i < _methods.Length; i++)
     {
         var reflected = CreateMethodInfo(module.Methods[i].Signature);
         reflected.SetDispId(i);
         _methods[i] = reflected;
     }
 }
Exemplo n.º 23
0
 private void ReflectVariables(LoadedModule module)
 {
     _properties = new PropertyInfo[module.ExportedProperies.Length];
     _fields     = new FieldInfo[0];
     for (int i = 0; i < module.ExportedProperies.Length; i++)
     {
         var reflected = CreatePropInfo(module.ExportedProperies[i]);
         _properties[i] = reflected;
     }
 }
Exemplo n.º 24
0
        public LoadedModule GetModule(string name)
        {
            LoadedModule module = null;

            if (!loadedModules.TryGetValue(name, out module))
            {
                return(null);
            }
            return(module);
        }
Exemplo n.º 25
0
        private void StartupModuleToPhase(LoadedModule module, StartupPhase phase)
        {
            if (MostRecentStartup <= module.CurrentPhase)
            {
                return;
            }

            module.Module.StartupModule(phase);
            module.CurrentPhase = phase;
        }
Exemplo n.º 26
0
        public bool RemoveModule(string name)
        {
            LoadedModule removedModule = null;

            //Don't want to remove a module out from under an executing command
            lock (moduleLocks.GetOrAdd(name, s => new object()))
            {
                return(loadedModules.TryRemove(name, out removedModule));
            }
        }
        public Type Reflect <T>(LoadedModule module, string asTypeName) where T : ScriptDrivenObject
        {
            var typeBuilder = new ClassBuilder <T>();
            var type        = typeBuilder
                              .SetTypeName(asTypeName)
                              .SetModule(module)
                              .ExportDefaults()
                              .Build();

            return(type);
        }
        public UserScriptContextInstance(LoadedModule module, string asObjectOfType, IValue[] args = null)
            : base(module, true)
        {
            DefineType(TypeManager.GetTypeByName(asObjectOfType));
            _module = module;

            ConstructorParams = args;
            if (args == null)
            {
                ConstructorParams = new IValue[0];
            }
        }
Exemplo n.º 29
0
 public override bool Generate(CommandExecutionContext context)
 {
     foreach (var module in context.Runtime.DataTarget.EnumerateModules())
     {
         var loadedModule = new LoadedModule
         {
             Name      = Path.GetFileName(module.FileName),
             Size      = module.FileSize,
             Path      = module.FileName,
             Version   = module.Version.ToString(),
             IsManaged = module.IsManaged
         };
         Modules.Add(loadedModule);
     }
     return(true);
 }
Exemplo n.º 30
0
        private ScriptDrivenObject CreateUninitializedSDO(LoadedModule module, ExternalContextData externalContext = null)
        {
            var scriptContext = new Machine.Contexts.UserScriptContextInstance(module);

            scriptContext.AddProperty("ЭтотОбъект", "ThisObject", scriptContext);
            if (externalContext != null)
            {
                foreach (var item in externalContext)
                {
                    scriptContext.AddProperty(item.Key, item.Value);
                }
            }

            scriptContext.InitOwnData();
            return(scriptContext);
        }
Exemplo n.º 31
0
        internal IRuntimeContextInstance NewObject(LoadedModule module, ExternalContextData externalContext = null)
        {
            var scriptContext = new Machine.Contexts.UserScriptContextInstance(module, "Сценарий");

            scriptContext.AddProperty("ЭтотОбъект", "ThisObject", scriptContext);
            if (externalContext != null)
            {
                foreach (var item in externalContext)
                {
                    scriptContext.AddProperty(item.Key, item.Value);
                }
            }

            scriptContext.InitOwnData();
            InitializeSDO(scriptContext);

            return(scriptContext);
        }
        private void LoadAndRegister(Type type, CompilerService compiler, string typeName, Environment.ICodeSource code)
        {
            if (_loadedModules.ContainsKey(typeName))
            {
                return;
            }

            var module = CompileModuleFromSource(compiler, code, null);
            var loaded = new LoadedModule(module);

            _loadedModules.Add(typeName, loaded);
            using (var md5Hash = MD5.Create())
            {
                var hash = GetMd5Hash(md5Hash, code.Code);
                _fileHashes.Add(typeName, hash);
            }

            TypeManager.RegisterType(typeName, type);
        }
Exemplo n.º 33
0
 public override bool Generate(CommandExecutionContext context)
 {
     foreach (var module in context.Runtime.DataTarget.EnumerateModules())
     {
         var loadedModule = new LoadedModule
         {
             Name = Path.GetFileName(module.FileName),
             Size = module.FileSize,
             Path = module.FileName,
             Version = module.Version.ToString(),
             IsManaged = module.IsManaged
         };
         Modules.Add(loadedModule);
     }
     return true;
 }
Exemplo n.º 34
0
 internal ScriptDrivenObject(LoadedModule module)
     : base(TypeManager.GetTypeByName("Object"))
 {
     _module = module;
     InitOwnData();
 }
Exemplo n.º 35
0
 internal UserScriptContextInstance(LoadedModule module) : base(module)
 {
     _module = module;
 }
Exemplo n.º 36
0
 internal UserScriptContextInstance(LoadedModule module, string asObjectOfType)
     : base(module, true)
 {
     DefineType(TypeManager.GetTypeByName(asObjectOfType));
     _module = module;
 }