Intercept call functions in Trace Monkey?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Intercept call functions in Trace Monkey?

Paul Leger
Hi,

         I had intercepted some call functions or methods in Spider
Monkey without using the debugger library. To attain this, i modified
the "jsinterp.c" to add some hooks that allowed i could intercept
calls. Using that, i could executed an extra piece of code, which
could access to the JSStackFrame object.


         In Trace Monkey, i had not been able to intercept all calls.
Mainly, i had not been able to intercept calls associated to global
objects. Is there a function that allows to do the following?

• CallbackFunction(JSContext *cx, JSBool (*myfunctions) (JSStackFrame
*) )

In a determined context (cx), each time that a function is executed i
can execute my own function, which can access to JSStackFrame.

For example:

var paul = new Person("paul");
var myName = paul.getName();

My objective final is to express in Javascript some as this:

intercept( "All Objects", "get*", function() { print("A get call has
happened") });


Another question, can i know which property is associated a function
in an object?

For example

JSProperty *getPropertyOfFunction(JSObject *obj,JSfunction *fun);

Thank in advance.
Greetings.
Paul Leger.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Intercept call functions in TraceMonkey?

Jason Orendorff-2
(Apologies if this posts twice.  Google ate my previous reply.)

On 1/15/09 6:28 AM, Paul Saint Leger Morales wrote:
>           I had intercepted some call functions or methods in Spider
> Monkey without using the debugger library. To attain this, i modified
> the "jsinterp.c" to add some hooks that allowed i could intercept
> calls. Using that, i could executed an extra piece of code, which
> could access to the JSStackFrame object.

Wow!  That sounds like a lot of trouble.  Why can't you use the debugger
API?

>           In Trace Monkey, i had not been able to intercept all calls.
> Mainly, i had not been able to intercept calls associated to global
> objects. Is there a function that allows to do the following?
>
> • CallbackFunction(JSContext *cx, JSBool (*myfunctions) (JSStackFrame
> *) )
>
> In a determined context (cx), each time that a function is executed i
> can execute my own function, which can access to JSStackFrame.

Yes, in the debugger API:

   // jsprvtd.h
   typedef void *
   (* JSInterpreterHook)(JSContext *cx, JSStackFrame *fp, JSBool before,
                         JSBool *ok, void *closure);

   // jsdbgapi.h
   JSBool
   JS_SetCallHook(JSRuntime *rt, JSInterpreterHook hook, void *closure);

For details, see the comment at JSInterpreterHook in jsprvtd.h.

> Another question, can i know which property is associated a function
> in an object?

Not always. Sometimes there is no such property:

   // here the anonymous function isn't a property of any object
   myarray.sort(function (a, b) { return a<b; });

There is of course JS_GetFunctionId, which isn't quite what you asked for.

If you don't mind being slow (and firing off getters like mad), you can
do this in JavaScript:

   function getPropertyOfFunction(obj, fun) {
       for (var name in obj)
           if (obj[name] === fun)
               return name;
       return null;
   }

If you must avoid triggering getters, you could try calling
js_DecompileValueGenerator, passing the function object.  (I'm not 100%
sure js_DecompileValueGenerator will work for this out of the box.  You
may need to hack on it some.)  When this works, it will return the part
of the call expression up to the parentheses, which might be "Math.sin"
or "obj.method" or "isNaN" or "f" ...or the complete source code of a
function-expression.

Other, crazier plans might involve hacking the interpreter in
JSOP_CALLPROP and friends (the opcodes that do method lookups) to stash
the information you need somewhere.

-j
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Intercept call functions in TraceMonkey?

Paul Leger
On Jan 16, 2:16 pm, Jason Orendorff <[hidden email]> wrote:

> (Apologies if this posts twice.  Google ate my previous reply.)
>
> On 1/15/09 6:28 AM, Paul Saint Leger Morales wrote:
>
> >           I had intercepted some call functions or methods in Spider
> > Monkey without using the debugger library. To attain this, i modified
> > the "jsinterp.c" to add some hooks that allowed i could intercept
> > calls. Using that, i could executed an extra piece of code, which
> > could access to the JSStackFrame object.
>
> Wow!  That sounds like a lot of trouble.  Why can't you use the debugger
> API?
>
> >           In Trace Monkey, i had not been able to intercept all calls.
> > Mainly, i had not been able to intercept calls associated to global
> > objects. Is there a function that allows to do the following?
>
> > • CallbackFunction(JSContext *cx, JSBool (*myfunctions) (JSStackFrame
> > *) )
>
> > In a determined context (cx), each time that a function is executed i
> > can execute my own function, which can access to JSStackFrame.
>
> Yes, in the debugger API:
>
>    // jsprvtd.h
>    typedef void *
>    (* JSInterpreterHook)(JSContext *cx, JSStackFrame *fp, JSBool before,
>                          JSBool *ok, void *closure);
>
>    // jsdbgapi.h
>    JSBool
>    JS_SetCallHook(JSRuntime *rt, JSInterpreterHook hook, void *closure);
>
> For details, see the comment at JSInterpreterHook in jsprvtd.h.
>
> > Another question, can i know which property is associated a function
> > in an object?
>
> Not always. Sometimes there is no such property:
>
>    // here the anonymous function isn't a property of any object
>    myarray.sort(function (a, b) { return a<b; });
>
> There is of course JS_GetFunctionId, which isn't quite what you asked for.
>
> If you don't mind being slow (and firing off getters like mad), you can
> do this in JavaScript:
>
>    function getPropertyOfFunction(obj, fun) {
>        for (var name in obj)
>            if (obj[name] === fun)
>                return name;
>        return null;
>    }
>
> If you must avoid triggering getters, you could try calling
> js_DecompileValueGenerator, passing the function object.  (I'm not 100%
> sure js_DecompileValueGenerator will work for this out of the box.  You
> may need to hack on it some.)  When this works, it will return the part
> of the call expression up to the parentheses, which might be "Math.sin"
> or "obj.method" or "isNaN" or "f" ...or the complete source code of a
> function-expression.
>
> Other, crazier plans might involve hacking the interpreter in
> JSOP_CALLPROP and friends (the opcodes that do method lookups) to stash
> the information you need somewhere.
>
> -j

Hi,
                First of all, thank you very much for answers my
questions.

> Wow!  That sounds like a lot of trouble.  Why can't you use the debugger
> API?

I want to declare or use hooks that cannot obstruct with the work of
the debugger service. For instance, i want that people can use my
services (provide by my hooks) together with the debugging service. I
think  (thought) that someone wants to use the debugging API, so it is
necessary to enable the debugging service. In other words, if your
extension uses at least a function of debugging API, this extension
must enable the debugging service, and for this reason, this extension
is not compatible with  debugging service of JavaScript . Summarizing
my idea, I could not debug this extension (the part that is written in
JavaScript) because my extension used a function of the debugging API.
Can you understand my idea? (maybe, i cannot express very well my
idea).

Maybe, i am wrong. may you resolve my doubts? Another question if i
only use a function of the debugging API the execution of my script
code is as slow as i use all functions of the debugging API?. This
doubt is related to the subject of enabling or not the debugging
service.

Again, thank in advance. Sorry if i did not express very well.
Paul Leger.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine