Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to identify which script is being worked in

44 views
Skip to first unread message

harry.b...@gmail.com

unread,
Aug 13, 2020, 6:10:55 AM8/13/20
to
Hi,

I'm trying to update our current embedded Javascript engine from Spidermonkey 1.8.5 to 76.

The issue that I'm currently facing is how to determine which script is being worked in when multiple scripts share the same global object. Previously, we would store some information in the context private data so as to identify the correct script. However, this is now proving unsuitable given that we can only have one context.

I was just wondering if there is a straight forward way of determining which script is currently being worked in when a per-instance or per-class function has been called?

Thanks
Harry

tcam...@mozilla.com

unread,
Aug 13, 2020, 8:20:30 AM8/13/20
to
I'm presume the context is from within a C++ method with a signature like |(JSContext* cx, unsigned argc, JS::Value* vp)| that you are triggering from JS. In that case you should be able follow a chain similar to CallArgs::calleev() -> JS_GetFunctionScript -> JS_GetScriptFilename.

(Depending on your use case, you may want to consider targetting version 78. It will become the ESR version and security patches will continue to be generated for it, as well more embedders will target that version).

Harry Buttery

unread,
Aug 28, 2020, 7:15:01 AM8/28/20
to
Hi,

Thanks for your help, this has fixed my problem.

Further to my original question however, I've now found that I require to know what script I'm currently working in when my WarningReporter function is called. I understand that the second argument of the WarningReporter is a JSErrorReport *, which has a 'filename' property. The Mozilla JSAPI Reference has this to say about the property:

'In the event of an error, filename will either contain the name of the external source file or URL containing the script (SCRIPT SRC=) or NULL, indicating that a script embedded in the current HTML page caused the error.'

On the face of it, this appears to be able to solve the problem. However, in the context of our embedding (which only ever deals with scripts, not htmls), I was wondering if this property will always return a filename or, if it returned NULL, what that would mean/imply? Baring in mind, that we can have multiple scripts that may or may not share the same global object(s). If this property can't reliably help identify which script is being worked in, is there another way to identify the working script?

Thanks again.

tcam...@mozilla.com

unread,
Aug 28, 2020, 11:14:23 AM8/28/20
to
The `filename` property should match the filename you passed to `CompileOptions` when your embedding compiles the script. It sounds like you have the right values there already. One case to test out in your setup is when happens when there is a warning/error in an `eval` in a script. I believe the filename will render as something like "foo.js > eval".

Harry Buttery

unread,
Sep 2, 2020, 11:56:35 AM9/2/20
to
> The `filename` property should match the filename you passed to `CompileOptions` when your embedding compiles the script. It sounds like you have the right values there already. One case to test out in your setup is when happens when there is a warning/error in an `eval` in a script. I believe the filename will render as something like "foo.js > eval".

Thanks for your help once again. I have yet to test my changes but I'm hoping this has sorted it.

> I'm presume the context is from within a C++ method with a signature like |(JSContext* cx, unsigned argc, JS::Value* vp)| that you are triggering from JS. In that case you should be able follow a chain similar to CallArgs::calleev() -> JS_GetFunctionScript -> JS_GetScriptFilename.

Sadly, I think I spoke too soon when I said that this had fixed my problem. I've run some tests and unfortunately JS_GetFunctionScript() returns NULL as the calling functions are native.

I did a little bit of searching around for an alternative and came across JS::DescribeScriptedCaller() and JS::GetScriptedCaller(), though I'm not sure if these are appropriate to use - I found a case from several years ago where someone used JS_DescribeScriptedCaller(), so I assumed that JS::DescribeScriptedCaller() was the updated version (except with some different arguments). Do you know if either of these would be appropriate? I just need to be able to get the script JSScript pointer and/or the script filename.
0 new messages