Home > Backend Development > C++ > How to Parse and Execute JavaScript Code within a C# Application?

How to Parse and Execute JavaScript Code within a C# Application?

Mary-Kate Olsen
Release: 2024-12-29 10:37:10
Original
288 people have browsed it

How to Parse and Execute JavaScript Code within a C# Application?

How to Parse and Execute JavaScript in C#

In this code example, we wrap the Windows Script Engines to support 32-bit and 64-bit environments.

For your specific case, it means depending on the .JS code, you may have to emulate/implement some HTML DOM elements such as 'document', 'window', etc. (using the 'named items' feature, with the MyItem class. That's exactly what Internet Explorer does).

Here are some sample of what you can do with it:

1) Direct expressions evaluation:

Console.WriteLine(ScriptEngine.Eval("jscript", "1+2/3"));
Copy after login

will display 1.66666666666667

2) Function call, with optional arguments:

using (ScriptEngine engine = new ScriptEngine("jscript"))
{
  ParsedScript parsed = engine.Parse("function MyFunc(x){return 1+2+x}");
  Console.WriteLine(parsed.CallMethod("MyFunc", 3));
}
Copy after login

Will display 6

3) Function call with named items, and optional arguments:

using (ScriptEngine engine = new ScriptEngine("jscript"))
{
    ParsedScript parsed = engine.Parse("function MyFunc(x){return 1+2+x+My.Num}");
    MyItem item = new MyItem();
    item.Num = 4;
    engine.SetNamedItem("My", item);
    Console.WriteLine(parsed.CallMethod("MyFunc", 3));
}

[ComVisible(true)] // Script engines are COM components.
public class MyItem
{
    public int Num { get; set; }
}
Copy after login

Will display 10.

Edit: We have added the possibility to use a CLSID instead of a script language name, so we can re-use the new and fast IE9 "chakra" javascript engine, like this:

using (ScriptEngine engine = new ScriptEngine("{16d51579-a30b-4c8b-a276-0ff4dc41e755}"))
{
    // continue with chakra now
}
Copy after login

Here is the full source:

(see provided code)

Usage:

  1. Create a ScriptEngine object for the desired scripting language.
  2. Use the Eval or Parse methods to evaluate or parse script text.
  3. To call methods or access properties, use the ParsedScript object returned by Parse.
  4. To set named items, use the SetNamedItem method of the ScriptEngine.

Notes:

  • The ScriptEngine class is a wrapper around the Windows Script Engines.
  • The ParsedScript class represents a parsed script that can be evaluated.
  • Named items allow you to pass objects to the script engine.

The above is the detailed content of How to Parse and Execute JavaScript Code within a C# Application?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template