Use Javascript in. NET as the scripting language (the latest version of v8)

Source: Internet
Author: User

Modify: Re-compile the latest V8 version (v3.6) (v2.6 was used previously), and optimize the Javascript.net source code again to enhance stability. At the same time, the latest version of V8 engine provides better execution efficiency.

 

Noesis. Javascript Release is downloaded separately. (If you have downloaded Efreda. Script, you only need to re-reference the latest version ).

Download the latest version of Efreda. Script (v0.2.0) source code

The source code of V8 and Javascript. NET is not put on. It is relatively large. V8 has 22 MB. If compiled versions are put, there are Lib files with more than 200 MB. If you need to compile the source code, you can download the source code from Google's V8 SVN. After compilation, You can reset the header file and library path in Javascript.net and compile Javascript.net again. If you need a compilation method, contact me.

 

Preface

I have written an article about using v8sharp as the Wraper of v8Engine in. NET to interwork with Javascript in. NET. However, v8Sharp has several major problems. First, passing Chinese strings in parameters produces garbled characters. Second, the. NET object cannot be passed to Javascript as a parameter in. NET. In this way, the practical value of v8Sharp is reduced a lot. Fortunately, v8Engine is very good, and there are a lot of. NET open source Wrapper, so we found this Javascript. NET, Wrapper with high scalability.

 

V8Engine 

V8Engine is a JS interpretation engine used by Google Chrome. Its execution efficiency is quite high. According to my own test, it is a JS engine higher than that used by browsers such as IE8 and FF3. The latest version is comparable to IE9 and FF4. Moreover, Javascript itself is a C Style programming language. For developers who have been using C, C ++, and C # For a long time, Javascript is more friendly than LUA and other scripting languages.

 

Javascript. NET 

As mentioned above, the scalability is quite strong because it is very simple, both in source code and in usage. You only need to simply wrap the package to implement most of the useful functions. The official website's Getting Started Guide only mentions that it can execute JS code, but does not mention how to execute JS-defined methods. In fact, when calling its Run method, JS Code has been compiled into the context. If you pre-compile the JS-defined method once, you can directly call the JS method using the function name in the future. I just used this feature to make some simple packaging for this class library, implementing mutual calls between. NET and JS functions. However, there is a Bug in the latest official version. when JS is called in. NET (JS is executed through the Run method), the "(Unknown Location)" exception will occur randomly. It is actually caused by Stack overflow. I slightly modified the official source code, corrected this problem. In the source code provided later, Noesis. javascript. dll is the compiled version that fixes the problem. If you need the source code of Noesis. Javascript. dll, contact me.

How to use 

I just Wrapped Javascript. NET in a simple way and provided Example in the source code. Here is a rough description.

The configuration file has the following attributes:

  • StartEngine: whether to start the JS engine. If it is set to False, the engine will not be started, nor any method in the class library will be called (an exception will be thrown ).
  • RelativePath: indicates whether the path is relative. You can place the JS file in the application root directory or any location. If the path is in the root directory, you can set this attribute to True, enter the name of the folder where the script file is located. For details, refer to Example.
  • ScriptPath: script file path. Enter the directory name or full physical path based on RelativePath.
  • CreateGACMapping: whether to create a global assembly ing. This operation is time-consuming. It takes about 5-10 seconds to create a ing when starting the script engine. The advantage is that in the JS method, it can be simply instantiated by namespace + class name and Assembly name.. NET object. For more information, see the following code snippet.
  • CreateMappingAsyn: whether to asynchronously create a ing. Set this parameter to True to avoid blocking the main thread. When Mapping ends, the JS engine triggers an event. For more information, see the Example code.
Listen to the Mapping end event:

1 JScriptManager. MappingComplete + = (sender, e) =>

2 {
3 Console. Write (" ing created ");

4 };

 

General JS call methods:

1 function normalMethod (msg)

2 {
3 msg = "Hello, return from js:" + msg;
4 return msg;
5}

C # code:

1 string rtv = (string) JScriptManager. Call ("normalMethod", msg );

2 Console. WriteLine (rtv );

 

Passing a. NET object as a parameter of the JS method:

1 function callDotNet (speaker)

2 {
3 speaker. Print ("output from js ");
4}

C # code:

1 public class Speaker

2 {
3 public void Print (string msg)
4 {
5 Console. WriteLine (msg );
6}
7}

1 JScriptManager. Call ("callDotNet", new Speaker ());

 

Instantiate a. NET object using a strong naming method in JS (you do not need to create a GAC ing ):

1 function testCreateByFullName ()

2 {
3 var proc = $. Create ("System. Diagnostics. Process, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089", null );
4 proc. StartInfo. FileName = "calc ";
5 proc. Start ();
6}

 

Create a. NET object by fully qualified name + Assembly name in JS (GAC ing required ):

1 function testCreateByShortName ()

2 {
3 var proc = $. Create ("System. Diagnostics. Process", "System", null );
4 proc. StartInfo. FileName = "explorer ";
5 proc. StartInfo. Arguments = "about: blank ";
6 proc. Start ();
7}

 

Call the. NET static method in JS:

1 function testStaticMethod ()

2 {
3 var arg = new Array ();
4 arg [0] =-25;
5 var rtv = $. StaticMethod ("System. Math", "mscorlib", "Abs", arg );
6 return rtv;
7}

 

The Example provided in the source code contains the usage method described above. The final result of running is to output two strings on the console and start the calculator provided by Windows and the default browser.

 

Source code download

Javascript. NET Fix source code (corrected the problem of Stack overflow. for compilation, Python 2.6.1 and Python 3.x compilation must be installed with exceptions)

 

PS: the source code is the Proj of VS2010.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.