本文整理汇总了C#中JintEngine.Run方法的典型用法代码示例。如果您正苦于以下问题:C# JintEngine.Run方法的具体用法?C# JintEngine.Run怎么用?C# JintEngine.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JintEngine
的用法示例。
在下文中一共展示了JintEngine.Run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var assembly = Assembly.Load("Jint.Tests");
Stopwatch sw = new Stopwatch();
// string script = new StreamReader(assembly.GetManifestResourceStream("Jint.Tests.Parse.coffeescript-debug.js")).ReadToEnd();
JintEngine jint = new JintEngine()
// .SetDebugMode(true)
.DisableSecurity()
.SetFunction("print", new Action<string>(Console.WriteLine))
.SetFunction("write", new Action<string>(t => Console.WriteLine(t)))
.SetFunction("stop", new Action(delegate() { Console.WriteLine(); }));
sw.Reset();
sw.Start();
try
{
Console.WriteLine(jint.Run("2+3"));
Console.WriteLine("after0");
Console.WriteLine(jint.Run(")(---"));
Console.WriteLine("after1");
Console.WriteLine(jint.Run("FOOBAR"));
Console.WriteLine("after2");
}
catch (JintException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
}
示例2: Main
static void Main(string[] args)
{
var assembly = Assembly.Load("Jint.Tests");
Stopwatch sw = new Stopwatch();
string script = new StreamReader(assembly.GetManifestResourceStream("Jint.Tests.Parse.coffeescript-format.js")).ReadToEnd();
JintEngine jint = new JintEngine()
// .SetDebugMode(true)
.DisableSecurity()
.SetFunction("print", new Action<string>(Console.WriteLine))
.SetFunction("stop", new Action( delegate() { Console.WriteLine(); }));
sw.Reset();
sw.Start();
jint.Run(script);
try
{
var result = jint.Run("CoffeeScript.compile('number = 42', {bare: true})");
}
catch (JintException e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
}
示例3: Main
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
// string script = new StreamReader(assembly.GetManifestResourceStream("Jint.Tests.Parse.coffeescript-debug.js")).ReadToEnd();
JintEngine jint = new JintEngine()
// .SetDebugMode(true)
.DisableSecurity()
.SetFunction("print", new Action<string>(Console.WriteLine))
.SetFunction("write", new Action<string>(t => Console.WriteLine(t)))
.SetFunction("stop", new Action(delegate() { Console.WriteLine(); }));
sw.Reset();
jint.SetMaxRecursions(50);
jint.SetMaxSteps(10*1000);
sw.Start();
try
{
Console.WriteLine(
jint.Run(File.ReadAllText(@"C:\Work\ravendb-1.2\SharedLibs\Sources\jint-22024d8a6e7a\Jint.Play\test.js")));
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
}
}
示例4: ExecuteSunSpiderScript
private static void ExecuteSunSpiderScript(string scriptName)
{
const string prefix = "Jint.Tests.SunSpider.";
var script = prefix + scriptName;
var assembly = Assembly.GetExecutingAssembly();
var program = new StreamReader(assembly.GetManifestResourceStream(script)).ReadToEnd();
var jint = new JintEngine(Options.Ecmascript5); // The SunSpider scripts doesn't work with strict mode
var sw = new Stopwatch();
sw.Start();
jint.Run(program);
Console.WriteLine(sw.Elapsed);
}
示例5: Main
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
// string script = new StreamReader(assembly.GetManifestResourceStream("Jint.Tests.Parse.coffeescript-debug.js")).ReadToEnd();
JintEngine jint = new JintEngine()
// .SetDebugMode(true)
.DisableSecurity()
.SetFunction("print", new Action<string>(Console.WriteLine))
.SetFunction("write", new Action<string>(t => Console.WriteLine(t)))
.SetFunction("stop", new Action(delegate() { Console.WriteLine(); }));
sw.Reset();
sw.Start();
Console.WriteLine(jint.Run("Math.floor(1.5)"));
Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
}
示例6: Test
protected object Test(Options options, string script, Action<JintEngine> action)
{
var jint = new JintEngine(options)
.AllowClr()
.SetFunction("assert", new Action<object, object>(Assert.AreEqual))
.SetFunction("fail", new Action<string>(Assert.Fail))
.SetFunction("istrue", new Action<bool>(Assert.IsTrue))
.SetFunction("isfalse", new Action<bool>(Assert.IsFalse))
.SetFunction("print", new Action<string>(Console.WriteLine))
.SetFunction("alert", new Action<string>(Console.WriteLine))
.SetFunction("loadAssembly", new Action<string>(assemblyName => Assembly.Load(assemblyName)))
.DisableSecurity();
action(jint);
var sw = new Stopwatch();
sw.Start();
var result = jint.Run(script);
Console.WriteLine(sw.Elapsed);
return result;
}
示例7: ShouldHandleClrArrays
public void ShouldHandleClrArrays() {
var values = new int[] { 2, 3, 4, 5, 6, 7 };
var jint = new JintEngine()
.SetDebugMode(true)
.SetParameter("a", values)
.AllowClr();
Assert.AreEqual(3, jint.Run("a[1];"));
jint.Run("a[1] = 4");
Assert.AreEqual(4, jint.Run("a[1];"));
Assert.AreEqual(4, values[1]);
}
示例8: ShouldNotAccessClr
public void ShouldNotAccessClr() {
const string script = @"
var sb = new System.Text.StringBuilder();
sb.Append('hi, mom');
sb.Append(3);
sb.Append(true);
return sb.ToString();
";
var engine = new JintEngine();
Assert.AreEqual("hi, mom3True", engine.Run(script));
}
示例9: SecurityExceptionsShouldNotBeCaught
public void SecurityExceptionsShouldNotBeCaught() {
const string script = @"
try {
var sb = new System.Text.StringBuilder();
fail('should not have reached this code');
}
catch (e) {
fail('should not have reached this code');
}
";
var engine = new JintEngine();
engine.Run(script);
}
示例10: ShouldThrowErrorWhenAssigningUndeclaredVariableInStrictMode
public void ShouldThrowErrorWhenAssigningUndeclaredVariableInStrictMode() {
var engine = new JintEngine(Options.Ecmascript5 | Options.Strict)
.SetFunction("assert", new Action<object, object>(Assert.AreEqual));
var x = engine.Run(@"
try {
x = 1;
return x;
} catch(e) {
return 'error';
}
");
Assert.AreEqual("error", x);
}
示例11: ShouldReturnDelegateForFunctions
public void ShouldReturnDelegateForFunctions() {
const string script = "var ccat=function (arg1,arg2){ return arg1+' '+arg2; }";
JintEngine engine = new JintEngine().SetFunction("print", new Action<string>(Console.WriteLine));
engine.Run(script);
Assert.AreEqual("Nicolas Penin", engine.CallFunction("ccat", "Nicolas", "Penin"));
}
示例12: ShouldHandleArrayIndexOf
public void ShouldHandleArrayIndexOf()
{
var jint = new JintEngine()
.SetDebugMode(true)
.SetFunction("assert", new Action<object, object>(Assert.AreEqual))
.SetFunction("print", new Action<string>(System.Console.WriteLine));
jint.Run(@"
var days = ['mon', 'tue', 'wed'];
assert(1, days.indexOf('tue'));
");
}
示例13: ObjectShouldBePassedToDelegates
public void ObjectShouldBePassedToDelegates() {
var engine = new JintEngine();
engine.SetFunction("render", new Action<object>(s => Console.WriteLine(s)));
const string script =
@"
var contact = {
'Name': 'John Doe',
'PhoneNumbers': [
{
'Location': 'Home',
'Number': '555-555-1234'
},
{
'Location': 'Work',
'Number': '555-555-9999 Ext. 123'
}
]
};
render(contact.Name);
render(contact.toString());
render(contact);
";
engine.Run(script);
}
示例14: ShouldRunInRun
public void ShouldRunInRun() {
var filename = Path.GetTempFileName();
File.WriteAllText(filename, "a='bar'");
var engine = new JintEngine().AddPermission(new FileIOPermission(PermissionState.Unrestricted));
engine.AllowClr();
engine.SetFunction("load", new Action<string>(delegate(string fileName) { using (var reader = File.OpenText(fileName)) { engine.Run(reader); } }));
engine.SetFunction("print", new Action<string>(Console.WriteLine));
engine.Run("var a='foo'; load('" + JintEngine.EscapteStringLiteral(filename) + "'); print(a);");
File.Delete(filename);
}
示例15: ClrNullShouldBeConverted
public void ClrNullShouldBeConverted() {
var jint = new JintEngine()
.SetDebugMode(true)
.SetFunction("assert", new Action<object, object>(Assert.AreEqual))
.SetFunction("print", new Action<string>(System.Console.WriteLine))
.SetParameter("foo", null);
// strict equlity ecma 262.3 11.9.6 x === y: If type of (x) is null return true.
jint.Run(@"
assert(true, foo == null);
assert(true, foo === null);
");
}