SetFunction() public method

public SetFunction ( string name, Delegate function ) : JintEngine
name string
function Delegate
return JintEngine
Beispiel #1
0
    void Start()
    {
        JintEngine e = new JintEngine();

        e.SetFunction("log",
            new Jint.Delegates.Action<object>((a) => Debug.Log(a)));

        e.SetFunction("add",
            new Jint.Delegates.Func<double, double, double>((a,b) => a + b));

        TextAsset script = (TextAsset)Resources.Load("Test");

        e.Run(script.text);
    }
		protected override void CustomizeEngine(JintEngine jintEngine)
		{
			jintEngine.SetParameter("documentId", docId);
			jintEngine.SetFunction("replicateTo", new Action<string,JsObject>(ReplicateToFunction));
			foreach (var sqlReplicationTable in config.SqlReplicationTables)
			{
				var current = sqlReplicationTable;
				jintEngine.SetFunction("replicateTo" + sqlReplicationTable.TableName, (Action<JsObject>)(cols =>
				{
					var tableName = current.TableName;
					ReplicateToFunction(tableName, cols);
				}));
			}
		}
Beispiel #3
0
        public void init()
        {
            en = new JintEngine();
            en.DisableSecurity();
            en.AllowClr = true;

            en.SetFunction("print", new Action<object>(Console.WriteLine));
            en.SetFunction("alert", new Action<object>(n=>{
                MessageBox.Show(n.ToString());
            }));

            //en.SetFunction("cs_exe", new Action<string>(Exe));
            //en.SetFunction("cs_exe", new Action<string, string>(Exe));
            //en.SetFunction("cs_exeWait", new Action<string, string, string>(ExeWait));
            //en.SetFunction("cs_exe", new Action<string, string, string>(Exe));
            en.SetFunction("cs_exeWait", new Action<string, string, bool, string>(ExeWait));

            en.SetFunction("cs_process", new Action<string, string, bool, string>(ExeWait));
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            JsGlobal dummy;
            JintEngine eng = new JintEngine();
            JsArray root = null;

            root = new JsArray();
            eng.GlobalScope["PersistentRoot"] = root;

            eng.SetDebugMode(true);
            eng.SetFunction("log"
                , new Jint.Delegates.Func<JsInstance, JsInstance>((JsInstance  obj) =>
                {
                    Console.WriteLine(obj==null?"NULL":obj.Value==null?"NULL 2":obj.Value.ToString());
                    return JsUndefined.Instance;
                }));

            //            ((JsFunction)eng.GlobalScope["log"]).Execute(eng.Visitor, null, new JsInstance[] { new JsUndefined() });

             //   Console.WriteLine("\r\n" + eng.Run(File.ReadAllText(@"Scripts\func_jsonifier.js")));
            Console.WriteLine("\r\n" + eng.Run(File.ReadAllText(@"Scripts\test.js")));
            while (true)
            {
                Console.Write("] ");
                var l = Console.ReadLine();
                if (l.Length == 0) break;
                var res = eng.Run(l);
                Console.WriteLine(res == null ? "<.NET NULL>" : res.ToString());
            }
            Console.WriteLine("Saving...");
            //            Console.ReadLine();
            //    root.Modify();
            //   s.Close();
            //    Console.Write("Enter to close");
            //  Console.ReadLine();
        }
Beispiel #5
0
		internal static JintEngine CreateEngine(ScriptedPatchRequest patch)
		{
			var wrapperScript = String.Format(@"
function ExecutePatchScript(docInner){{
  (function(doc){{
	{0}{1}
  }}).apply(docInner);
}};
", patch.Script, patch.Script.EndsWith(";") ? String.Empty : ";");

			var jintEngine = new JintEngine()
				.AllowClr(false)
				.SetDebugMode(false)
				.SetMaxRecursions(50)
				.SetMaxSteps(10*1000);


			jintEngine.Run(GetFromResources("Raven.Database.Json.Map.js"));

			jintEngine.Run(GetFromResources("Raven.Database.Json.lodash.js"));

			jintEngine.Run(GetFromResources("Raven.Database.Json.RavenDB.js"));

			jintEngine.SetFunction("LoadDocument", ((Func<string, object>)(value =>
			{
				var loadedDoc = loadDocumentStatic(value);
				if (loadedDoc == null)
					return null;
				loadedDoc[Constants.DocumentIdFieldName] = value;
				return ToJsObject(jintEngine.Global, loadedDoc);
			})));

			jintEngine.Run(wrapperScript);

			return jintEngine;
		}
Beispiel #6
0
		private JintEngine CreateEngine(ScriptedPatchRequest patch)
		{
			var scriptWithProperLines = NormalizeLineEnding(patch.Script);
			var wrapperScript = String.Format(@"
function ExecutePatchScript(docInner){{
  (function(doc){{
	{0}
  }}).apply(docInner);
}};
", scriptWithProperLines);

			var jintEngine = new JintEngine()
				.AllowClr(false)
#if DEBUG
				.SetDebugMode(true)
#else
                .SetDebugMode(false)
#endif
                .SetMaxRecursions(50)
				.SetMaxSteps(maxSteps);

            AddScript(jintEngine, "Raven.Database.Json.lodash.js");
			AddScript(jintEngine, "Raven.Database.Json.ToJson.js");
			AddScript(jintEngine, "Raven.Database.Json.RavenDB.js");

			jintEngine.SetFunction("LoadDocument", ((Func<string, object>)(value =>
			{
				var loadedDoc = loadDocumentStatic(value);
				if (loadedDoc == null)
					return null;
				loadedDoc[Constants.DocumentIdFieldName] = value;
				return ToJsObject(jintEngine.Global, loadedDoc);
			})));

            jintEngine.Run(wrapperScript);

			return jintEngine;
		}
Beispiel #7
0
			protected override void CustomizeEngine(JintEngine jintEngine)
			{
				jintEngine.SetParameter("documentId", docId);
				jintEngine.SetFunction("sqlReplicate", (Action<string, string, JsObject>)((table, pkName, cols) =>
				{
					var itemToReplicates = dictionary.GetOrAdd(table);
					itemToReplicates.Add(new ItemToReplicate
					{
						PkName = pkName,
						DocumentId = docId,
						Columns = ToRavenJObject(cols)
					});
				}));
			}
        void init()
        {
            PythonEngine.Inst.init();

            jint = new JintEngine();

            string absUserDir = Directory.GetCurrentDirectory() + "/Js";
            jint.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess, absUserDir));

            //set some simple funcitons.
            Func<object, int> alert = delegate(object s)
            {
                MessageBox.Show(s.ToString());
                return 0;
            };

            Func<string, double, string, double[]> ema = delegate(string label, double days, string outLabel)
            {
                if (arrays.ContainsKey(label))
                {
                    double[] inputs = arrays[label];
                    double[] outputs = MathUtil.calcEMA(inputs, days);
                    addArray(outLabel, outputs);
                    return outputs;
                }
                return null;
            };




            Func<string, string, string, double[]> diff = delegate(string label0, string label1, string outLabel)
            {
                if (arrays.ContainsKey(label0) && arrays.ContainsKey(label1))
                {
                    double[] inputs0 = arrays[label0];
                    double[] inputs1 = arrays[label1];
                    double[] outputs = MathUtil.calcDiff(inputs0, inputs1);

                    addArray(outLabel, outputs);
                    return outputs;
                }
                return null;
            };

            //tell canvas to line
            Func<string, double, double, double, double, double, double, int> line = delegate(string label, double part, double r, double g, double b, double a, double thickness)
            {
                if (!arrays.ContainsKey(label))
                {
                    return 1;

                }

                Color c = new Color()
                {
                    R = (byte)r,
                    G = (byte)g,
                    B = (byte)b,
                    A = (byte)a
                };

                canvas.addDrawingObj(label, arrays[label], (int)part, c, thickness, DrawingObjectType.Line);
                return 0;
            };

            Func<string, double, double, double, double, double, double, int> zeroBars = delegate(string label, double part, double r, double g, double b, double a, double thickness)
            {
                if (!arrays.ContainsKey(label))
                {
                    return 1;

                }

                Color c = new Color()
                {
                    R = (byte)r,
                    G = (byte)g,
                    B = (byte)b,
                    A = (byte)a
                };
                canvas.addDrawingObj(label, arrays[label], (int)part, c, thickness, DrawingObjectType.zVLines);

                return 0;
            };
            ////kdj('close','high','low',9,'k','d','j');

            Func<string, string, string, double, string, string, string, int> kdj
                = delegate(string close, string high, string low, double days, string k, string d, string j)
            {
                if (arrays.ContainsKey(close) && arrays.ContainsKey(high) && arrays.ContainsKey(low))
                {
                    double[] inputs0 = arrays[close];
                    double[] inputs1 = arrays[high];
                    double[] inputs2 = arrays[low];

                    double[] kArray;
                    double[] dArray;
                    double[] jArray;
                    MathUtil.calcKDJ((int)days, inputs0, inputs1, inputs2, out kArray, out dArray, out jArray);

                    addArray(k, kArray);
                    addArray(d, dArray);
                    addArray(j, jArray);

                    return 0;
                }
                return 1;
            };


            //draw candle line.
            Func<string, double, string, string, string, string, int> candleLine = delegate(string id, double part, string open, string close, string high, string low)
            {
                if (!arrays.ContainsKey(open) || !arrays.ContainsKey(close)
                    || !arrays.ContainsKey(high) || !arrays.ContainsKey(low))
                {
                    return 1;
                }

                canvas.addKLineObj(id, (int)part, arrays[open], arrays[close], arrays[high], arrays[low]);
                return 0;
            };

            Func<string, object, int> addConfig = delegate(string id, object value)
            {
                addConfigVal(id, value);
                return 0;
            };


            Func<string, int> runScript = delegate(string scriptName)
            {
                runJsScript(scriptName);
                return 0;
            };


            Func<string, double, string, double, double, double, double, double, int> vLines = delegate(string id, double part, string label, double r, double g, double b, double a, double thickness)
            {
                if (!arrays.ContainsKey(label))
                {
                    return 1;

                }

                Color c = new Color()
                {
                    R = (byte)r,
                    G = (byte)g,
                    B = (byte)b,
                    A = (byte)a
                };

                //
                canvas.addDrawingObj(id, arrays[label], (int)part, c, thickness, DrawingObjectType.vLines);
                return 0;
            };

            //
            Func<string, string, int> setDrawItemEventHandler = delegate(string id, string handlerName)
            {
                canvas.setDrawItemEventHandler(id, handlerName);
                return 0;

            };




            jint.SetFunction("addConfig", addConfig);
            jint.SetFunction("runScript", runScript);

            jint.SetFunction("setDrawItemEventHandler", setDrawItemEventHandler);
            

            jint.SetFunction("alert", alert);
            jint.SetFunction("ema", ema);
            jint.SetFunction("kdj", kdj);
            jint.SetFunction("diff", diff);
            jint.SetFunction("line", line);
            jint.SetFunction("zeroBars", zeroBars);
            jint.SetFunction("candleLine", candleLine);
            jint.SetFunction("vLines", vLines);


          
        }
Beispiel #9
0
        public virtual void InjectDefaultCode()
        {
            Interpreter = new JintEngine();
            Interpreter.DisableSecurity();

            Interpreter.SetFunction("AddHook", new AddHookDelegate(AddHook));
            Interpreter.SetFunction("AddShortcut", new AddHookDelegate(AddShortcut));

            Interpreter.SetFunction("Color", new NewColorDelegate(NewColor));
            Interpreter.SetFunction("Vector2", new NewVectorDelegate(NewVector));
            Interpreter.SetFunction("AddVectors", new VectorOperationsDelegate(AddVectors));
            Interpreter.SetFunction("SubtractVectors", new VectorOperationsDelegate(SubtractVectors));
            Interpreter.SetFunction("MultiplyVectors", new VectorOperationsDelegate(MultiplyVectors));
            Interpreter.SetFunction("DivideVectors", new VectorOperationsDelegate(DivideVectors));
        }