internet-explorer-9bhojscriptchakraiactivescript

Error adding SCRIPTITEM_CODEONLY symbol using IE9 JS engine (Chakra)


We've been using active scripting in our browser extension (BHO) for a while with the old JScript engine (CLSID_JScript), and we recently decided to support the new IE9 script engine (Chakra) as well. One thing we do is add symbols to the engine using AddNamedItem with the SCRIPTITEM_CODEONLY option to create our own modules (namespaces). Unfortunately, we haven't been able to get this to work with Chakra. Even the most trivial example where we add a symbol and immediately retrieve its script dispatch yields an E_OUTOFMEMORY error.

if (SUCCEEDED(hr)) {
  hr = scriptEngine->AddNamedItem(L"test", SCRIPTITEM_CODEONLY);
}
if (SUCCEEDED(hr)) {
   hr = scriptEngine->GetScriptDispatch(L"test", &scriptDispatch);
}

The GetScriptDispatch call returns the error. You can see the whole example on Github.

I set breakpoints on all the IActiveScriptSite methods and the only ones that are called are GetLCID and OnStateChange, so don't think the site implementation is the problem.

I've looked at every example I can find and tried everything I can think of, including setting the engine state to SCRIPTSTATE_CONNECTED manually, implementing any additional interfaces that it QIs for, etc. I even tried returning a valid LCID. Nothing seems to make a difference.

Any idea what gives? I assume this basic example should work in Chakra.


Solution

  • The unfortunate reality is that the set of IActiveScript interfaces that Chakra exposes is not intended for public consumption. (The GUID isn't published into the registry for exactly that reason.) Chakra only implements that portion of the IActiveScript interfaces needed to support Internet Explorer and the Visual Studio editor/debugger, and no effort has been made to ensure the completeness or correctness of the interfaces beyond that which those two clients use.

    It looks like your scenario is one of those that's not implemented to spec, probably because IE/VS doesn't use the interface in this particular way. Sadly, there's almost no chance this would be fixed unless there is a decision to publicly support the IActiveScript interfaces in some future version of IE.

    IE11 has introduced a public API for Chakra, but it is not IActiveScript-based, it's Win32-based. You can get more details here: http://www.panopticoncentral.net/2013/07/02/introducing-jsrt-embedding-javascript-on-windows/. I have no idea if that would help you in your situation or not.

    Sorry, not the answer you were hoping for, I'm sure...