コード例 #1
0
ファイル: Program.cs プロジェクト: mkloubert/MJKBlog
        private static void Test_RemObjectsScript()
        {
            // Funktionen in der JavaScript-Engine
            // werden über Delegates definiert
            var myWriteLineAction = new Action <object>(
                (obj) =>
            {
                Console.WriteLine(obj);
            });

            using (var javascript = new EcmaScriptComponent())
            {
                // Variabel 'a' mit dem Wert 5979
                javascript.Globals.SetVariable("a", 5979);
                // Funktion 'myWriteLine' (s.o.)
                javascript.Globals.SetVariable("myWriteLine", myWriteLineAction);

                // die eigene Klasse 'A' als
                // 'KlasseA' registrieren
                javascript.ExposeType(typeof(A), "KlasseA");

                // Quelltext festlegen
                javascript.Source = @"
myWriteLine(a);    // 5979

a = 23979;
myWriteLine(a);    // 23979

var objA = new KlasseA();
objA.test();
";

                javascript.Run();
            }
        }
コード例 #2
0
        public static void ScriptRun(EcmaScriptComponent script, String code, string[] argNames, params object[] arguments)
        {
            if (String.IsNullOrWhiteSpace(code))
            {
                return;
            }

            script.Source = code;
            int index = 0;

            foreach (var arg in arguments)
            {
                if (!script.Globals.GetVariable(argNames[index]).Equals(arg))
                {
                    script.Globals.SetVariable(argNames[index++], arg);
                }
            }

            try
            {
                script.Run();
            }
            catch (Exception ex)
            {
                Syncfusion.Windows.Forms.MessageBoxAdv.Show(String.Format("Ошибка выполнения сценария. Строка: {0}, позиция: {1}. Сообщение: {2}",
                                                                          script.DebugLastPos.StartRow, script.DebugLastPos.StartCol, ex.ToString()),
                                                            "Внимание", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
コード例 #3
0
        public ScenarioFindReplaceResultsForm(FindReplaceScriptEditor find_replaceScriptEditor)
        {
            InitializeComponent();
            this.find_replaceScriptEditor = find_replaceScriptEditor;
            this.SelectRecordCondition    = find_replaceScriptEditor.SelectRecordCondition;
            this.SelectFieldCondition     = find_replaceScriptEditor.SelectFieldCondition;
            this.SelectSubfieldCondition  = find_replaceScriptEditor.SelectSubfieldCondition;

            this.FindTextStr    = find_replaceScriptEditor.FindTextString;
            this.ReplaceTextStr = find_replaceScriptEditor.ReplaceTextString;

            workMode     = (WorkMode)find_replaceScriptEditor.activeTabIndex;
            templateMode = (TemplateMode)find_replaceScriptEditor.cmbTemplateType.SelectedIndex;

            applyActions = new StringCollection();
            applyActions.Add("ПОДТВЕРДИТЬ");
            applyActions.Add("ОТМЕНИТЬ");

            applyActionsToAll = new StringCollection();
            applyActionsToAll.Add("ПОДТВЕРДИТЬ ВСЕ");
            applyActionsToAll.Add("ОТМЕНИТЬ ВСЕ");


            ScriptEngine = new EcmaScriptComponent();
            RemObjectUtils.ExposeAssembly(ScriptEngine, typeof(ManagedClient.ManagedClient64).Assembly);
            ScriptEngine.Globals.SetVariable("client", find_replaceScriptEditor.client);
            ScriptEngine.Globals.SetVariable("curDatabase", find_replaceScriptEditor.curDatabase);
        }
コード例 #4
0
 public static void ExposeAssembly
 (
     EcmaScriptComponent script,
     Assembly assembly
 )
 {
     foreach (Type type in assembly.GetExportedTypes())
     {
         script.ExposeType(type, null);
     }
 }
コード例 #5
0
 public static void SetMutableVariable
 (
     this EcmaScriptComponent script,
     string variableName,
     object variableValue
 )
 {
     script.Globals.CreateMutableBinding(variableName, false);
     // Задаём значение переменной
     script.Globals.SetMutableBinding(variableName, variableValue, false);
 }
コード例 #6
0
        public static T GetUnwrappedVariable <T>
        (
            this EcmaScriptComponent script,
            string variableName
        )
        {
            EcmaScriptObjectWrapper wrapped
                = (EcmaScriptObjectWrapper)script.Globals.GetVariable(variableName);

            if (ReferenceEquals(wrapped, null))
            {
                return(default(T));
                // Или бросаемся исключениями
            }
            return((T)wrapped.Value);
        }
コード例 #7
0
        public static bool ScriptEval(EcmaScriptComponent script, String expression, string[] argNames, params object[] arguments)
        {
            script.Source = expression;
            bool result = false;

            try
            {
                int index = 0;
                foreach (var arg in arguments)
                {
                    script.Globals.SetVariable(argNames[index++], arg);
                }

                script.Run();
                result = (bool)script.RunResult;
            }
            catch (Exception ex)
            {
            }

            return(result);
        }
コード例 #8
0
        protected override void OnExecute(ScriptExecutorBase.OnExecuteContext context)
        {
            using (var comp = new EcmaScriptComponent())
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(context.Source))
                    {
                        // nothing to do
                        return;
                    }

                    const string NAME_INCLUDE       = "include";
                    const string NAME_INCLUDE_FILES = "include_files";
                    const string NAME_LOAD_MODULES  = "load_modules";

                    var hasCustomInclude      = false;
                    var hasCustomIncludeFiles = false;
                    var hasCustomLoadModules  = false;

                    Action <string> checkForDefaultFuncNames = (name) =>
                    {
                        switch (name)
                        {
                        case NAME_INCLUDE:
                            hasCustomInclude = true;
                            break;

                        case NAME_INCLUDE_FILES:
                            hasCustomIncludeFiles = true;
                            break;

                        case NAME_LOAD_MODULES:
                            hasCustomLoadModules = true;
                            break;
                        }
                    };

                    // exposed types
                    foreach (var item in this._EXPOSED_TYPES)
                    {
                        var type = item.Key;
                        var name = item.Value;

                        comp.ExposeType(type,
                                        name == null ? type.Name : name);
                    }

                    // variables
                    foreach (var item in this._VARS)
                    {
                        var varName = item.Key;
                        checkForDefaultFuncNames(varName);

                        var value = item.Value;

                        comp.Globals
                        .SetVariable(varName,
                                     value);
                    }

                    // functions
                    foreach (var item in this._FUNCS)
                    {
                        var funcName = item.Key;
                        checkForDefaultFuncNames(funcName);

                        var func = item.Value;

                        comp.Globals
                        .SetVariable(funcName,
                                     func);
                    }

                    // include
                    if (!hasCustomInclude)
                    {
                        comp.Globals
                        .SetVariable(NAME_INCLUDE,
                                     new SimplePredicate((args) =>
                        {
                            try
                            {
                                // each argument is script code to include
                                foreach (var a in args)
                                {
                                    var src = a.AsString(true);
                                    comp.Include(null, src);
                                }

                                return(true);
                            }
                            catch
                            {
                                return(false);
                            }
                        }));
                    }

                    // include_files
                    if (!hasCustomIncludeFiles)
                    {
                        comp.Globals
                        .SetVariable(NAME_INCLUDE_FILES,
                                     new SimplePredicate((args) =>
                        {
                            try
                            {
                                // each argument is a script file
                                foreach (var a in args)
                                {
                                    var srcFile = a.AsString(true);
                                    var file    = new FileInfo(srcFile);

                                    var src = File.ReadAllText(file.FullName, Encoding.UTF8);
                                    comp.Include(file.FullName, src);
                                }

                                return(true);
                            }
                            catch
                            {
                                return(false);
                            }
                        }));
                    }

                    // load_modules
                    if (!hasCustomLoadModules)
                    {
                        comp.Globals
                        .SetVariable(NAME_LOAD_MODULES,
                                     new SimplePredicate((args) =>
                        {
                            try
                            {
                                // each argument is a module file
                                foreach (var a in args)
                                {
                                    var modFile = a.AsString(true);
                                    var asmFile = new FileInfo(modFile);

                                    var asm = Assembly.LoadFile(asmFile.FullName);

                                    IDictionary <string, Delegate> functionsToRegister;
                                    IDictionary <Type, string> typesToExpose;
                                    this.ExportTypesAndFunctions(asm,
                                                                 out functionsToRegister,
                                                                 out typesToExpose);

                                    // expose types
                                    foreach (var item in typesToExpose)
                                    {
                                        string name = item.Value;
                                        if (name == null)
                                        {
                                            // use type name
                                            name = item.Key.Name;
                                        }

                                        comp.ExposeType(item.Key,
                                                        name);
                                    }

                                    // register functions
                                    foreach (var item in functionsToRegister)
                                    {
                                        comp.Globals
                                        .SetVariable(item.Key,
                                                     item.Value);
                                    }
                                }

                                return(true);
                            }
                            catch
                            {
                                return(false);
                            }
                        }));
                    }

                    comp.Source = context.Source;
                    comp.Run();
                }
                finally
                {
                    context.ScriptResult = comp.RunResult;
                }
            }
        }
コード例 #9
0
 public static void ScriptRun(EcmaScriptComponent script, String code)
 {
     ScriptRun(script, code, new string[0], new object[0]);
 }