ClearScript is an open source library for using javascript code in C# application. It provides a wrapper around V8 engine to make to-and-from call between javascript and C# code.
During development, one of the major challenge – which no developer can deny – is debugging. If you are writing a C# or C++ code, Visual Studio comes as savior when it comes to debugging. However, debugging pure dynamic, interpreted languages like javascript is not that straight froward. In this post I will discuss how can we debug C# and javascript code together when ClearScript & V8 engine are in action. If you are new to ClearScript or using javascript within C# application, I would recommend you to read my previous post – ClearScript : Step-by-Step – before proceeding further. This post is built on code example provided in the previous post.
Debugging With ClearScript & V8 :
V8 engine by design doesn’t support script debugging. However, it does support TCP/IP based debugging protocol. Any IDE that supports TCP/IP based debugging protocol can be used. One of the most convenient and easy way to debug the javascript V8 engine is using Eclipse. In this article we will use Eclipse IDE for javascript and Visual Studio for C# debugging.
So lets begin with following steps:
1. Install Eclipse : Get it from http://www.eclipse.org/downloads/ if you do not have it already.
2. Next we need a tool / plugin which supports V8 engine TCP/IP based debugging protocol. Google Chrome Developer Tools come handy for this purpose. So lets get the tool in eclipse by following below steps:
- Start Eclipse and go to “Help” -> “Install New Software…”.
- Paste the URL – http://chromedevtools.googlecode.com/svn/update/dev/ – into the “Work with:” field.
- Select “Google Chrome Developer Tools” and complete the dialog.

- Re-launch Eclipse.
3. In C# code, while creating the instance of V8Engine, set the debugging flag on.
//Create V8Engine with debugging flag enabled V8ScriptEngine _v8Engine = new V8ScriptEngine( V8ScriptEngineFlags.EnableDebugging);
This enables the V8 engine TCP/IP- based debugging and initializes the debugging at default port number 9222. The application can choose a port by providing it in constructor as follows:
//Create V8Engine with custom debugging port number int debugPort = 8888; V8ScriptEngine _v8Engine = new V8ScriptEngine( V8ScriptEngineFlags.EnableDebugging, debugPort);
4. Start the C# application in debugging mode ( attached with Visual Studio ). Within visual studio, you can continue with your C# code debugging as usual. Once your C# code initializes the V8 engine, loads the javascript and also does the basic setup of call backs etc, switch to Eclipse IDE. For example, in previous post put a break point at HelloWorld call in C# Main method and execute until there in Visual Studio. At this point V8 engine is initialized and started with TCP/IP debugging enabled on port 9222. Also, it has the javascript content loaded from HelloWorld.js file and callback to C# registered.
5. In Eclipse, go to “Run” menu and select “Debug Configuration” option.

6. Right Click on “Standalone V8 VM” and select “New” option.
7. In the screen, choose a configuration name ( e.g, DemoDebugConfiguration). Leave the port number as 9222 if you application has started with default TCP/IP debugging port number while creating V8Engine, otherwise provide the correct port number.
8. Click “Apply” followed by “Debug”.
This will add a new item in your project explorer with name DemoDebugConfiguration. Expand the item and you will see a couple of files with names “Script Document.chromium” etc. Each file contain the javascript snippet active with loaded V8 engine.
Now, open any of the files and add break points to the places in code which you want to monitor during debug. Switch back to Visual Studio and resume debugging. Eclipse will automatically prompt when a break point in javascript code is hit.
At this point you can continue to debug in Eclipse and Visual Studio will automatically prompt if a call from javascript will hit a break point in C# code.
This way you can continue to switch between Visual Studio and Eclipse and keep a watch on whats happening with your C# and javascript code together.




