JavaScript Runtime Hosting (IE11)

Post your working scripts, libraries and tools
lexikos
Posts: 8577
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

JavaScript Runtime Hosting (IE11)

29 Dec 2014, 03:34

This is a short proof-of-concept, showing how to use the new (IE11) JavaScript Runtime Host API. It requires IE11 and AutoHotkey v1.1.17+.

Code: Select all

js =
(
host.echo(JSON.stringify({foo:'bar'}));
42 // The script's result :)
)
class Host {
    echo(s) {
        MsgBox %s%
    }
}

#NoEnv
; Initialize JsRT
if !DllCall("LoadLibrary", "str", "jscript9")
    throw "Failed to load jscript9.dll"
JsRuntimeVersionEdge := -1
if DllCall("jscript9\JsCreateRuntime", "int", 0, "int", JsRuntimeVersionEdge
    , "ptr", 0, "ptr*", runtime) != 0
    throw "Failed to initialize JsRT; do you really have IE11?"
DllCall("jscript9\JsCreateContext", "ptr", runtime, "ptr", 0, "ptr*", context)
DllCall("jscript9\JsSetCurrentContext", "ptr", context)

; Get the Global object for adding stuff
DllCall("jscript9\JsGetGlobalObject", "ptr*", globalObject)
; Get a property ID for the name "host"
DllCall("jscript9\JsGetPropertyIdFromName", "wstr", "host", "ptr*", hostPropertyId)
; Get a JsValueRef for our Host object
hostRef := ToJsValue(Host)
; Pass our Host object to the script engine
DllCall("jscript9\JsSetProperty", "ptr", globalObject, "ptr", hostPropertyId
    , "ptr", hostRef, "int", true)

; Run the script
e := DllCall("jscript9\JsRunScript", "wstr", js, "uptr", 0, "wstr", "source.js"
    , "ptr*", result)
; Show the result
if !e {
    DllCall("jscript9\JsConvertValueToString", "ptr", result, "ptr*", stringRef)
    MsgBox % "Result: " FromJsValue(stringRef)
} else
    MsgBox 48,, % format("Error {:#x}", e)

; Clean up
DllCall("jscript9\JsSetCurrentContext", "ptr", 0)
DllCall("jscript9\JsDisposeRuntime", "ptr", runtime)

ToJsValue(val) {
    VarSetCapacity(variant, 24, 0)
    ref := ComObject(0x400C, &variant) ; VT_BYREF|VT_VARIANT
    ref[] := val
    DllCall("jscript9\JsVariantToValue", "ptr", &variant, "ptr*", valref)
    ref[] := 0
    return valref
}

FromJsValue(valref) {
    VarSetCapacity(variant, 24, 0)
    DllCall("jscript9\JsValueToVariant", "ptr", valref, "ptr", &variant)
    ref := ComObject(0x400C, &variant), val := ref[], ref[] := 0
    return val
}
Why?
  1. The newer 'Chakra' version of the JScript engine can't officially be hosted any other way, except via a WebBrowser control. Unofficially, it's possible to use it with Active Scripting hosts (like ActiveScript.ahk) or cscript.exe file.js //E:{16d51579-a30b-4c8b-a276-0ff4dc41e755}, but what you get isn't the real deal: ScriptEngineMajorVersion() returns 11, but it's lacking IE11 language features and a few older APIs, like the JSON object.
    Update: ActiveScript now includes an IActiveScript-like wrapper for the 'Chakra' JScript engine, class JsRT.
  2. It allegedly has better performance than the older engines available to the ScriptControl, ActiveScript.ahk and the Windows Script Host.
  3. It looked interesting. ;)
The API seems to be quite powerful and relatively easy to use from AutoHotkey (or C).

AutoHotkey v1.1.17+ is required for passing objects (like the Host object) to the runtime, and because of ref[] in ToJsValue/FromJsValue. There are alternatives for older versions of AutoHotkey.
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
GitHub: F4Jonatas
Contact:

Re: JavaScript Runtime Hosting (IE11)

01 Dec 2016, 12:15

How to use these variables: "JS_INVALID_REFERENCE", "JS_INVALID_RUNTIME_HANDLE", "JS_SOURCE_CONTEXT_NONE"?
How to collect variable name in js and delte?

Thank You!
:salute:
lexikos
Posts: 8577
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: JavaScript Runtime Hosting (IE11)

02 Dec 2016, 17:27

They aren't variables. They are constants. Their values are defined in the official documentation.
How to collect variable name in js and delte?
I have no idea what you're asking.

Btw, ActiveScript includes a wrapper for this JScript engine (JsRT).
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
GitHub: F4Jonatas
Contact:

Re: JavaScript Runtime Hosting (IE11)

02 Dec 2016, 18:07

For example in the above code.
Do you create a "host" object in JS, and how can I delete that object?
lexikos
Posts: 8577
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: JavaScript Runtime Hosting (IE11)

02 Dec 2016, 18:24

Do you create a "host" object in JS,
It's explained in the comments.
how can I delete that object?
You can find the appropriate function by reading the documentation.

ActiveScript is easier to use.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot], leosouza85 and 28 guests