本文整理汇总了C#中JintEngine类的典型用法代码示例。如果您正苦于以下问题:C# JintEngine类的具体用法?C# JintEngine怎么用?C# JintEngine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JintEngine类属于命名空间,在下文中一共展示了JintEngine类的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)
{
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);
}
}
示例3: TestPerformance
public void TestPerformance()
{
string[] tests = { "3d-cube", "3d-morph", "3d-raytrace", "access-binary-trees", "access-fannkuch", "access-nbody", "access-nsieve", "bitops-3bit-bits-in-byte", "bitops-bits-in-byte", "bitops-bitwise-and", "bitops-nsieve-bits", "controlflow-recursive", "crypto-aes", "crypto-md5", "crypto-sha1", "date-format-tofte", "date-format-xparb", "math-cordic", "math-partial-sums", "math-spectral-norm", "regexp-dna", "string-base64", "string-fasta", "string-tagcloud", "string-unpack-code", "string-validate-input" };
var assembly = Assembly.GetExecutingAssembly();
Stopwatch sw = new Stopwatch();
foreach (var test in tests) {
string script;
try {
script = new StreamReader(assembly.GetManifestResourceStream("Jint.Tests.SunSpider." + test + ".js")).ReadToEnd();
if (String.IsNullOrEmpty(script)) {
continue;
}
}
catch {
Console.WriteLine("{0}: ignored", test);
continue;
}
JintEngine jint = new JintEngine()
//.SetDebugMode(true)
.DisableSecurity();
sw.Reset();
sw.Start();
jint.Run(script);
Console.WriteLine("{0}: {1}ms", test, sw.ElapsedMilliseconds);
}
}
示例4: 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);
}
示例5: RunFile
protected override object RunFile(JintEngine ctx, string fileName)
{
if (Debugger.IsAttached)
return base.RunFile(ctx, fileName);
else
return Timeout.Run(() => base.RunFile(ctx, fileName), ScriptTimeout);
}
示例6: MainPage
// Constructor
public MainPage()
{
InitializeComponent();
engine = new JintEngine();
engine.SetFunction("alert", new Action<string>(t => MessageBox.Show(t)));
}
示例7: JintRuntime
public JintRuntime(JintEngine engine)
{
if (engine == null)
throw new ArgumentNullException("engine");
_engine = engine;
Global = new JsGlobal(this, engine);
GlobalScope = Global.GlobalScope;
}
示例8: ExecuteSunSpider
private static void ExecuteSunSpider()
{
const string fileName = @"..\..\..\Jint.Tests\SunSpider\Tests\access-fannkuch.js";
var jint = new JintEngine();
#if false
jint.ExecuteFile(fileName);
Console.WriteLine("Attach");
Console.ReadLine();
jint.ExecuteFile(fileName);
#else
jint.ExecuteFile(fileName);
var times = new TimeSpan[20];
int timeOffset = 0;
var lowest = new TimeSpan();
// Perform the iterations.
for (int i = 0; ; i++)
{
long memoryBefore = GC.GetTotalMemory(true);
var stopwatch = Stopwatch.StartNew();
jint.ExecuteFile(fileName);
var elapsed = stopwatch.Elapsed;
long memoryAfter = GC.GetTotalMemory(false);
times[timeOffset++] = elapsed;
if (timeOffset == times.Length)
timeOffset = 0;
if (times[times.Length - 1].Ticks != 0)
{
var average = new TimeSpan(times.Sum(p => p.Ticks) / times.Length);
if (lowest.Ticks == 0 || average.Ticks < lowest.Ticks)
lowest = average;
Console.WriteLine(
"This run: {0}, average: {1}, lowest: {2}, memory usage: {3}",
elapsed.ToString("s\\.fffff"),
average.ToString("s\\.fffff"),
lowest.ToString("s\\.fffff"),
NiceMemory(memoryAfter - memoryBefore)
);
}
}
#endif
}
示例9: RunFile
protected override object RunFile(JintEngine ctx, string fileName)
{
base.RunFile(ctx, fileName);
string suitePath = Path.Combine(
Path.GetDirectoryName(fileName),
"underscore-suite.js"
);
return ctx.ExecuteFile(suitePath);
}
示例10: ShouldNotRunInRun
public void ShouldNotRunInRun()
{
var filename = Path.GetTempFileName();
File.WriteAllText(filename, "a='bar'");
var engine = new JintEngine().AddPermission(new FileIOPermission(PermissionState.None));
engine.AllowClr();
engine.SetFunction("load", new Action<string>(p => engine.ExecuteFile(p)));
engine.SetFunction("print", new Action<string>(Console.WriteLine));
engine.Execute("var a='foo'; load('" + EscapeStringLiteral(filename) + "'); print(a);");
}
示例11: Main
static void Main(string[] args)
{
string line;
var jint = new JintEngine();
jint.SetFunction("print", new Action<object>(s => { Console.ForegroundColor = ConsoleColor.Blue; Console.Write(s); Console.ResetColor(); }));
#pragma warning disable 612,618
jint.SetFunction("import", new Action<string>(s => { Assembly.LoadWithPartialName(s); }));
#pragma warning restore 612,618
jint.DisableSecurity();
while (true) {
Console.Write("jint > ");
StringBuilder script = new StringBuilder();
while (String.Empty != (line = Console.ReadLine())) {
if (line.Trim() == "exit") {
return;
}
if (line.Trim() == "reset") {
jint = new JintEngine();
break;
}
if (line.Trim() == "help" || line.Trim() == "?") {
Console.WriteLine(@"Jint Shell");
Console.WriteLine(@"");
Console.WriteLine(@"exit - Quits the application");
Console.WriteLine(@"import(assembly) - assembly: String containing the partial name of a .NET assembly to load");
Console.WriteLine(@"reset - Initialize the current script");
Console.WriteLine(@"print(output) - Prints the specified output to the console");
Console.WriteLine(@"");
break;
}
script.AppendLine(line);
}
Console.SetError(new StringWriter(new StringBuilder()));
try {
jint.Execute(script.ToString());
}
catch (Exception e) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.ResetColor();
}
Console.WriteLine();
}
}
示例12: RunFile
protected override object RunFile(JintEngine ctx, string fileName)
{
string shellPath = Path.Combine(
Path.GetDirectoryName(fileName),
"shell.js"
);
if (File.Exists(shellPath))
ctx.ExecuteFile(shellPath);
return base.RunFile(ctx, fileName);
}
示例13: Compile
private static BoundProgram Compile(string script)
{
var programSyntax = JintEngine.ParseProgram(script);
var engine = new JintEngine();
var visitor = new BindingVisitor(engine.TypeSystem.CreateScriptBuilder(null));
programSyntax.Accept(visitor);
var program = SquelchPhase.Perform(visitor.Program);
DefiniteAssignmentPhase.Perform(program);
return program;
}
示例14: RunInclude
protected override void RunInclude(JintEngine engine, string fileName)
{
string source;
if (!_includeCache.TryGetValue(fileName, out source))
{
source =
GetSpecialInclude(fileName) ??
File.ReadAllText(Path.Combine(_libPath, fileName));
_includeCache.Add(fileName, source);
}
engine.Execute(source, fileName);
}
示例15: CreateContext
protected virtual JintEngine CreateContext(Action<string> errorAction)
{
var ctx = new JintEngine();
Action<string> failAction = Assert.Fail;
Action<string> printAction = message => Trace.WriteLine(message);
Action<string> includeAction = file => RunInclude(ctx, file);
ctx.SetFunction("$FAIL", failAction);
ctx.SetFunction("ERROR", errorAction);
ctx.SetFunction("$ERROR", errorAction);
ctx.SetFunction("$PRINT", printAction);
ctx.SetFunction("$INCLUDE", includeAction);
return ctx;
}