Exemple #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));
        }
Exemple #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;
        }
        public ScriptedViewComponent(LoadedModule module, string dynamicTypeName) : base(module, true)
        {
            var td = TypeManager.RegisterType(dynamicTypeName, typeof(ScriptedViewComponent));

            DefineType(td);
            InitOwnData();
        }
Exemple #5
0
 public AuthorizationModule(LoadedModule module,
                            IFileProvider filesystem,
                            IApplicationRuntime runtime) : base(module)
 {
     _filesystem = filesystem;
     _runtime    = runtime;
 }
Exemple #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();
        }
Exemple #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);
    }
Exemple #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);
    }
        private ReflectedClassType(LoadedModule module, string typeName)
            : base(typeof(object))
        {
            _typeName = typeName;

            ReflectVariables(module);
            ReflectMethods(module);
        }
        public IRuntimeContextInstance NewObject(LoadedModule module, ExternalContextData externalContext = null)
        {
            var scriptContext = CreateUninitializedSDO(module, externalContext);

            InitializeSDO(scriptContext);

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

            scriptContext.Initialize(_machine);

            return(scriptContext);
        }
        private Process InitProcess(IHostApplication host, LoadedModule module)
        {
            Initialize();

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

            return(process);
        }
Exemple #13
0
        internal ReflectableSDO(ScriptDrivenObject instance, LoadedModule module)
        {
            _module   = module;
            _instance = instance;

            GatherProperties();
            GatherMethods();
        }
Exemple #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;
     }
 }
        private LibraryLoader(LoadedModule moduleHandle, RuntimeEnvironment env, ScriptingEngine engine) : base(moduleHandle)
        {
            _env        = env;
            _engine     = engine;
            _customized = true;

            _engine.InitializeSDO(this);
        }
 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);
     }
 }
Exemple #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);
        }
 protected ScriptDrivenObject(LoadedModule module, bool deffered)
     : base(TypeManager.GetTypeByName("Object"))
 {
     _module = module;
     if (!deffered)
     {
         InitOwnData();
     }
 }
Exemple #19
0
        internal ReflectableSDO(ScriptDrivenObject instance, LoadedModule module)
        {
            _module = module;
            _instance = instance;

            GatherProperties();
            GatherMethods();

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

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

            return(scriptContext);
        }
 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;
     }
 }
 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;
     }
 }
Exemple #24
0
        public LoadedModule GetModule(string name)
        {
            LoadedModule module = null;

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

            module.Module.StartupModule(phase);
            module.CurrentPhase = phase;
        }
Exemple #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];
            }
        }
Exemple #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);
 }
        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);
        }
Exemple #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);
        }
Exemple #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;
 }
 internal ScriptDrivenObject(LoadedModule module)
     : base(TypeManager.GetTypeByName("Object"))
 {
     _module = module;
     InitOwnData();
 }
 internal UserScriptContextInstance(LoadedModule module) : base(module)
 {
     _module = module;
 }
 internal UserScriptContextInstance(LoadedModule module, string asObjectOfType)
     : base(module, true)
 {
     DefineType(TypeManager.GetTypeByName(asObjectOfType));
     _module = module;
 }