This project has moved and is read-only. For the latest updates, please go here.

Calling VBScript function by dispatchId

Jan 12, 2014 at 10:15 AM

There is legacy application that wrote on delphi. It uses with a VB Script Engine.
The delphi code call VBScript function by dispatchId that cached during init script engine.
Is there a way in ClearScript call function by disptach id

Jan 12, 2014 at 1:18 PM

ClearScript does not support invocation of script items via dispatch ID.

However, the VBScript functions have names, right? Why not use them? We're guessing that the Delphi application had no choice but to deal with dispatch IDs because no higher-level interface to VBScript code existed at the time.

Internally all JScript/VBScript invocation involves dispatch IDs, but ClearScript is designed to shield developers from engine-specific details like that. We'll gladly consider exposing them, but first we'd like to get a better understanding of the use case.

Jan 12, 2014 at 2:55 PM
This application wrote before years. I think dispatch id is used for performance reason. The application allows to customize specific business model, extend and interact with application, it uses VBScript is very extensive.
Additional question about thead-safe.
Is the VB scripter thread-safe?
Jan 14, 2014 at 4:57 AM
Edited Jan 14, 2014 at 5:15 AM
So at startup the application looks up dispatch IDs by name and caches them for invocation purposes. Is that correct? Again, we're not sure it makes sense to expose dispatch IDs in ClearScript. The performance hit of invoking by name is likely to be negligible in a modern .NET environment.

As for your other question, no, VBScript has never been thread-safe. Not only that, its thread affinity means that you can only use a given VBScript instance on the thread that created it. ClearScript enforces this rule.
Jan 14, 2014 at 6:56 AM
Thanks for your answers.
Can you more explain why for .NET application calling by name to be negligible?

About thread-safe I know, that a reason why in our legacy code there is a logic that check thread id and clone scripter in case of calling from a different thread.
Is there some work around in ClearScript?

Jan 14, 2014 at 12:42 PM
Can you more explain why for .NET application calling by name to be negligible?

Invoking a VBScript function from .NET is relatively expensive, as the ClearScript and COM interop layers incur a lot of overhead. Adding a string lookup shouldn't affect it significantly. This is all speculation, of course, until we compare some actual performance measurements. We'll take a closer look at this.

About thread-safe I know, that a reason why in our legacy code there is a logic that check thread id and clone scripter in case of calling from a different thread. Is there some work around in ClearScript?

No, ClearScript throws an exception if you attempt to use a VBScriptEngine instance from the wrong thread, but nothing stops you from implementing an engine cloning scheme like the one in your legacy application.

Good luck!
Jan 16, 2014 at 9:27 AM

How can I cloning scheme with ClearScript?

Jan 16, 2014 at 12:14 PM
You can create multiple instances of VBScriptEngine and run the same scripts in them. How does your legacy application do cloning?
Jan 16, 2014 at 1:17 PM
Legacy application use with the Clone method of the IActiveScript.
Creating new empty VBScriptEngine is not solution, initializing of the new scripter is not simply.
Jan 16, 2014 at 1:28 PM
Ah, very interesting. Your legacy application does some advanced Windows Script hosting!

Engine cloning via IActiveScript::Clone is not something that we've investigated. It does sound like a very powerful capability - one that even the mighty V8 does not currently provide. We will definitely add support for it in a future ClearScript release.

Thank you!
Mar 1, 2014 at 12:47 PM
Yes, our legacy application has a host implementation.
It use with clone method in case if there is access to script engine from thread that not is base thread.
The our site implementation maintain a list of the active scripts created from different threads.
Is there any plans to support clone option?

Mar 2, 2014 at 11:44 AM
Edited Mar 2, 2014 at 11:45 AM

We would definitely like to add support for cloning. Originally we thought it would "just work", but we've found some complications that we're still investigating. Unfortunately full support for cloning may require significant work, so we can't say when it'll be available.

Mar 3, 2014 at 2:49 PM
Ok, I will to wait your implementation. I tried to implement that as in our legacy application, but that does not work as inspected.
Mar 4, 2014 at 3:27 PM
Edited Mar 4, 2014 at 3:35 PM
Small question about debugging.
I created my lite variation of the script engine based on your code :
It's work fine, but script debugging is not worked as expected, the host application is locked by debugger
Which code responsibility for breaking in debugger?
Mar 4, 2014 at 7:06 PM
Hi ifle,

From the screenshot it looks like you've successfully attached the script debugger to your application and can view the script documents. You should now be able to use the Visual Studio UI to set breakpoints. If you're using JavaScript, you can also use the debugger statement to break into the debugger.

Mar 4, 2014 at 8:38 PM
Script that I run (2+3/0) from my test console application throw the divide by zero exception, but VS did not catch it.
Mar 5, 2014 at 2:45 PM
Is it possible that the error-generating script ran before you attached the debugger?
Mar 5, 2014 at 3:34 PM
No, debugger attached before error-generating script.
Mar 6, 2014 at 5:21 PM
The fact that the debugger attaches successfully and displays your script documents indicates that you have things mostly working. However, because you're using a modified version of ClearScript, we have no way to reproduce the issue and can't be of much help.
Mar 6, 2014 at 7:21 PM
I understand. Thanks .
Is there any reason always to create a debug document in WindowsScriptEngine::Parse function? It should not be dependent to EnableDebugging flag?
Mar 7, 2014 at 10:29 PM
It isn't required, but having the host manage these documents gives it a bit more control over how they're presented in the debugger.
May 26, 2014 at 9:57 AM
I would like again ask about debugging of script. In my modificated version of clearscript when I set breakpoint to attached script, a VS is freezes. How to debug of the debug infrastructure of clearscript? I tried debug it as visual studio extension but without any success.
May 26, 2014 at 1:27 PM
Ignore prev post. I've found a problem with debugging