/// <summary> /// Evaluates an expression using the specified language, with an optional array of named items. /// </summary> /// <param name="language">The language.</param> /// <param name="expression">The expression. May not be null.</param> /// <param name="namedItems">The named items array.</param> /// <returns>The result of the evaluation.</returns> public static object Eval(string language, string expression, params KeyValuePair <string, object>[] namedItems) { if (language == null) { throw new ArgumentNullException("language"); } if (expression == null) { throw new ArgumentNullException("expression"); } using (ScriptEngineJs engine = new ScriptEngineJs(language)) { if (namedItems != null) { foreach (KeyValuePair <string, object> kvp in namedItems) { engine.SetNamedItem(kvp.Key, kvp.Value); } } return(engine.Eval(expression)); } }
public async static void DoCommand() { await Program.Log("command"); var message = Recieved.Message; string[] m = message.Content.Split(); response = await message.Channel.SendMessageAsync("Evaluating...", allowedMentions : AllowedMentions.None); string code; try { code = message.Content.Substring(m[0].Length + m[1].Length + 2, message.Content.Length - m[0].Length - m[1].Length - 2); } catch (Exception) { await response.ModifyAsync(m => m.Content = "❌ Add code to evaluate!"); return; } result = null; counter = 0; try { timer = new Timer(1000); timer.Elapsed += CheckIfTimedOut; timer.AutoReset = true; timer.Enabled = true; switch (m[1]) { case "cs": result = Z.Expressions.Eval.Execute(code).ToString(); break; case "py": var py = new PythonScript(); result = py.RunFromString <string>(code, "output"); break; case "js": result = ScriptEngineJs.Eval("jscript", code).ToString(); break; default: await response.ModifyAsync(m => m.Content = "❌ Unkown language!"); timer.Enabled = false; timer.Stop(); timer.Dispose(); return; } } catch (Exception e) { result = e.Message; } if (result.Length <= 2000) { await response.ModifyAsync(m => m.Content = $"```{result}```"); } else { await response.ModifyAsync(m => m.Content = "❌ 2000+ characters!"); } timer.Enabled = false; timer.Stop(); timer.Dispose(); }
internal ParsedScript(ScriptEngineJs engine, IntPtr dispatch) { _engine = engine; _dispatch = Marshal.GetObjectForIUnknown(dispatch); }