microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

JS Debugging -output window #12

Closed SrinivasGourarum closed 8 years ago

SrinivasGourarum commented 8 years ago

When debugging the Script, the output window does not contain any of the information that would appear when debugging the managed code like log information. Is there an any api that can be called from c# so that the output can have the logs while debugging the JS code? Debug.Write (https://msdn.microsoft.com/en-us/library/k9k4wete(v=vs.94).aspx) works when written in the JS code.

liminzhu commented 8 years ago

Would you mind specifying what is the information that you are looking for from the output window?

SrinivasGourarum commented 8 years ago

The kind of logs that we write to the output window to log for example, if an object is successfully created. This information shows up while debugging the managed code(setting debugger type to "managed" in visual studio). However, while debugging the script, this information is not shown in the output window. It would be helpful, to know if the call in the Js code has been successful which could be validated by the log written in the c# code.

liminzhu commented 8 years ago

Oh I see and thanks for clarifying :). Since the managed debugger and the script debugger are separate entitles, there's no direct API to call in C# to log in JS debugging environment. But given the Debug object is available in JS, you can write a helper function similar to below to log from C# while debugging JS,

// log info in output window in script debugging
public void log(string info) {
    JavaScriptValue global;
    Native.JsGetGlobalObject(out global);

    // Retrieve the Debug.writeln function
    JavaScriptValue debug = getProperty(global, "Debug");
    JavaScriptValue writeln = getProperty(debug, "writeln");

    // construct arguments and call Debug.writeln
    JavaScriptValue logInfo;
    Native.JsPointerToString(info, (UIntPtr) info.Length, out logInfo);
    JavaScriptValue[] args = new JavaScriptValue[2] { global, logInfo };
    JavaScriptValue output;
    Native.JsCallFunction(writeln, args, 2, out output);
}

// helper method to get property
public JavaScriptValue getProperty(JavaScriptValue obj, string name)
{
    JavaScriptValue property;
    JavaScriptPropertyId id;
    Native.JsGetPropertyIdFromName(name, out id);
    Native.JsGetProperty(obj, id, out property);
    return property;
}
SrinivasGourarum commented 8 years ago

The proposed solution is working. Thank you.