Important: Please read the Qt Code of Conduct -

Getting JavaScript stack trace in C++ function ?

  • I am making C++ calls from JavaScript which is all working fine, what I want to explore and add is a way to identify the JavaScript call stack when the C++ call is made.

    I know I can do this in JavaScript, but I if possible I want to add this functionality to specific C++ function calls so the caller doesn't have to do it.

    This is for logging purposes, I want if possible to get the JavaScript file name, function name (if called from a function) and line number in the file.

    I know I can do this from JavaScript, but if possible I want to do it in the C++.

  • @SPlatten said in Getting JavaScript stack trace in C++ function ?:

    JavaScript call stack when the C++ call is made

    Based on nothing but intuition, so take with a pinch of salt, I would have said this is "not possible", or at least difficult and I don't know how you would do it. Till someone else tells you otherwise....

  • @JonB, that was exactly my thoughts, no harm in asking tho as I'm never surprised to learn something new. From another post is sounds like its possible using the Google V8 API which provides this kind of functionality for Node modules written in C/C++. However this isn't a node module.

    I have written node modules before.

  • @JonB It looks like what I am asking is possible through the QScriptContext Class and the backtrace function.

    I will investigate this now.

    mmm...According to the documentation:

    It should be available, I've added script to my pro file, but after including the header QScriptContext, its underlined with 'QScriptContext ' file not found to the right of it.

    What am I missing? I'm using Qt 5.15.2

  • Lifetime Qt Champion


    You missed that it's part of the deprecated and already removed QtScript module. It's still available as sources IIRC but you'll have to build it yourself.

    If you are using QJSEngine, the error retrieval is described in the class documentation.

  • @SGaist , thank you, this is such a useful feature.

  • @SGaist , it would appear that I can only get the properties if an exception is thrown. Is there another way to get the call stack information, such as File name, function if relevant and line number inside a C++ call that has been called from JavaScript ?

  • Lifetime Qt Champion

    That I do not know, sorry.

Log in to reply