4

I have a script host which runs JScript. There are some cases when I have to trace if a method of my exposed objects is called and from where it got called. For that I need to determine where is the script engine currently inside my object's method.

It should be the same information like when my site's OnScriptError is called: source line source char and the cookie which I passed to ParseScriptText.

Is it possible to obtain this information without generating an error?

r4w8173
  • 598
  • 6
  • 15
  • From references over the web it seems that if I add debugging facilities to my host could help. Problem is that the two descriptions I found about adding this feature implies the instantiation of a "process debug manager" COM object which only exists in windows if either Visual Studio or MS Script Debugger is installed. It is very likely that my users won't have either of those. – r4w8173 Dec 15 '10 at 10:26
  • It is strange that I found several similar questions on the web but without any answer. Not even a "this is not possible" type of answer. I wonder how nobody knows about this ... – r4w8173 Dec 16 '10 at 09:00
  • Which COM component are you using? Have you looked into the documentation? – Jens Mühlenhoff Dec 16 '10 at 09:44
  • I have implemented IActiveScriptSite, IActiveScriptSiteDebug and IActiveScriptSiteInterruptPoll in a script host of my own. The only external COM object I am using which is relevant to the question is JScript engine itself. – r4w8173 Dec 16 '10 at 13:24

1 Answers1

0

have you tried something like the code below? This should give you a complete stack trace with function names and argument values, but without line numbers.

Don't know what ParseScriptText is.

(from https://github.com/emwendelin/Javascript-Stacktrace and http://eriwen.com/javascript/stacktrace-update/)

other: function(curr) {
    var ANON = '{anonymous}', fnRE = /function\s*([\w\-$]+)?\s*\(/i,
        stack = [], fn, args, maxStackSize = 10;

    while (curr && stack.length < maxStackSize) {
        fn = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
        args = Array.prototype.slice.call(curr['arguments']);
        stack[stack.length] = fn + '(' + this.stringifyArguments(args) + ')';
        curr = curr.caller;
    }
    return stack;
},
kaboom
  • 609
  • 1
  • 8
  • 14
  • This is not about JavaScript in a browser. This is about Microsoft's JScript engine hosted in your own application. Read the comments on the original question. – r4w8173 Dec 22 '10 at 14:22
  • why shouldn't this work for the JScript? You could add this function to the code and call it later from Delphi. – kaboom Dec 22 '10 at 21:39
  • I thought about that too but the problem is that I cannot determine the chunk of code in which the execution point is. I have more than one block of JScript code just like in HTML. The solution you proposed in a bit more complex form is the current temporary solution but it is not 100% what I would like to achieve. – r4w8173 Dec 23 '10 at 09:05