Search code examples
c++internet-explorercomiactivescript

IActiveScript profiling - view script source


I am playing with IE's JavaScript Chakra engine.

I have downloaded example code from MSDN and I am trying to figure out how to get the function source when I have just function IDand script ID.

I start profiling my js script and from profiler (IActiveScriptProfilerCallback2) output I can see what functions are compiled, called...

For example callback for FunctionCompiled looks like this. I get function id and script id, even function name, but I can't figure out how to find function source code.

HRESULT Profiler::FunctionCompiled(PROFILER_TOKEN functionId, PROFILER_TOKEN scriptId, const wchar_t *pwszFunctionName, const wchar_t *pwszFunctionNameHint, IUnknown *pIDebugDocumentContext)
{
    fwprintf(stdout, L"Profiler::FunctionCompiled: 0x%lx, 0x%lx, %s, %s\n", scriptId, functionId, pwszFunctionName, pwszFunctionNameHint);
    return S_OK;
}

Solution

  • I think I solved it.. Here is my ugly ScriptCompiled function.

    HRESULT Profiler::ScriptCompiled(PROFILER_TOKEN scriptId, PROFILER_SCRIPT_TYPE type, IUnknown *pIDebugDocumentContext)
    {
        fwprintf(stdout, L"Profiler::ScriptCompiled: 0x%lx, %u\n", scriptId, type);
    
        if (pIDebugDocumentContext) {
            IDebugDocumentContext *debugDocumentContext = NULL;
            pIDebugDocumentContext->QueryInterface(__uuidof(IDebugDocumentContext), (void**)&debugDocumentContext);
    
            IDebugDocument *debugDocument = NULL;
            debugDocumentContext->GetDocument(&debugDocument);
    
            IDebugDocumentText *debugDocumentText = NULL;
            debugDocument->QueryInterface(__uuidof(IDebugDocumentText), (void**)&debugDocumentText);
    
            ULONG lines, chars;
            debugDocumentText->GetSize(&lines, &chars);
    
            printf("Lines: %d Chars: %d\n", lines, chars);
    
            chars++;
            WCHAR *text = (WCHAR*)malloc(sizeof(WCHAR) * chars);
            ULONG charsRetrieved = 0;
            debugDocumentText->GetText(0, text, NULL, &charsRetrieved, chars);
            text[charsRetrieved] = L'\0';
    
            wprintf(L"%s\n", text);
        }
    
        return S_OK;
    }