Today I am going to show you how to embed Chakra JavaScript engine in Windows Phone 8.1 app. Please note that at the time of writing this app won’t pass Microsoft Windows Store certification requirements. I won’t be surprised though if Microsoft reconsider their requirements in future.
Last year Microsoft released JsRT which exposes C-style API for embedding Chakra JavaScript engine. To use the API you only need to include jsrt.h
and add a reference to jsrt.lib
. On my machine the header file is located at
C:\Program Files (x86)\Windows Kits\8.1\Include\um\jsrt.h
and the lib files (for x86 and x64 accordingly) are located at
C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\jsrt.lib C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64\jsrt.lib
Curiously, there is no jsrt.lib
for ARM architecture. It is even more interesting that JsRT is not exposed in Windows Phone SDK. E.g. you won’t find jsrt.h
file in
C:\Program Files (x86)\Windows Phone Kits\8.1\Include
neither you will find jsrt.lib
in
C:\Program Files (x86)\Windows Phone Kits\8.1\lib\ARM C:\Program Files (x86)\Windows Phone Kits\8.1\lib\x86
However this shouldn’t discourage us. The first thing we should check is that JsRT API is exposed on Windows Phone 8.1. I know it is there because IE11 shares same source code for desktop and mobile and because Windows Phone 8.1 supports WinRT programming model. Anyway, let’s check it.
Find flash.vhd
file. On my machine it is located at
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.1\Emulation\Images
Use Disk Management and attach flash.vhd
file via Action->Attach VHD menu. Navigate to \Windows\System32
folder on MainOS partition and copy JSCRIPT9.DLL
somewhere. Open Visual Studio command prompt and run the following command
dumpbin /exports JSCRIPT9.DLL >jscript9.def
Open jscript9.def
file in your favorite editor and make sure you see the full JsRT API listed here. Edit the file so it becomes like this one https://gist.github.com/anonymous/88e44e8931cc8d118da9. Run the following command from the Visual Studio command prompt
lib /def:jscript9.def /out:jsrt.lib /machine:ARM
This will generate import library so you can use all exports defined in JSCRIPT9.DLL
library. We are almost ready.
We generated jsrt.lib
import library for ARM architecture, what’s next? In order to use jsrt.h
header in our Windows Phone 8.1 project we must edit it a little bit. First copy it and its dependencies to your project. Here is the list of all the files you should copy
- activdbg.h
- activprof.h
- ActivScp.h
- DbgProp.h
- jsrt.h
In case you don’t want JavaScript debugging support you can copy jsrt.h
file only and replace all pointers to the interfaces from ActiveScript API with void*
. Once you copy the the header files you must edit them to switch to Windows Phone API. To do so, you have to replace WINAPI_PARTITION_DESKTOP
with WINAPI_PARTITION_PHONE_APP
. It may sound like a lot of work but it is just a few lines change. You can see the change here.
That’s it. Now you can use the new header and lib files in your project. You can find the full source code at https://github.com/mslavchev/chakra-wp81.
In closing I would like to remind you that at present this app won’t pass Windows Store certification requirements. Here is the list of the requirement violations
Supported API test (FAILED) This API is not supported for this application type - Api=CoGetClassObject. Module=api-ms-win-core-com-l1-1-1.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsCreateContext. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsCreateRuntime. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsDisposeRuntime. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsRunScript. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsSetCurrentContext. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsStartDebugging. Module=jscript9.dll. File=ChakraDemoApp.exe. This API is not supported for this application type - Api=JsStringToPointer. Module=jscript9.dll. File=ChakraDemoApp.exe.
Hopefully Microsoft will revisit their requirements.
Thanks for sharing this!!!
Yes, agree to your point, Microsoft should formally make this public in their WP8.1 SDK as it is already accessible via IE11, just like the way Apple did with iOS7 JavaScriptCore Objective-C framework
Hi Mihail,
Came across your blog while looking for a solution on using “ChakraCore in Windows 8 Store app”. Can you throw us some light whether we can go with using “ChakraCore” as java script engine for windows 8.1 store apps, as app certification is failing with errors like,
“API RegCloseKey in advapi32.dll is not supported for this application type. ChakraCore.dll calls this API.”
All the certification errors we are observing are with ChakraCore.dll.
Thanks,
Pramod.
In short, on Windows Phone 8.x you can access only a subset of Win32 and COM API. See https://msdn.microsoft.com/en-us/library/windows/apps/br205762.aspx. The JsRT API is not “blessed” on Windows Phone 8.x and is meant for internal use only. This constraint was lifted in Windows 10.