> On Fri, 21 Nov 2008 11:44:47 pm august wrote:
> > hi,
> >
> >     I'm looking to create something similar to the event model in a web
> >     browser's DOM.
> >
> >     I have a C++ object that can blit stuff to screen and I'd like to tie
> >     in a callback from the javascript side for mouse events like so:
> >
> >     var obj = CppObject();
> >     obj.onmousedown = function ( e ) {  // do stuff with e };
> >
> >
> >     any ideas on how to do this?   The "obj.onmousedown" will be called
> >     from the C++ side of things as that is where the events are polled.
> >
> >     any tips would be much appreciated.  thank you - august.
> >
> 
> 
> Local<Value>
> callMethod(
>     Local<Object>   object,
>     const char*           methodName,
>     int                   argc,
>     Handle<Value>   argv[]
>     )
> {
>     Local<Value>    func   = object->Get(String::New(methodName));
> 
>     if (func->IsFunction())
>     {
>       Local<Function> f = Local<Function>::Cast(func);
> 
>       return f->Call(object, argc, argv);
>     }
>     else
>     {
>       return Local<Value>();
>     }
> }


thanks for this.   I am doing something very similar.  In my C++ object,
I have made storage for the function and for the object with:

      v8::Persistent<v8::Object> self;
                v8::Persistent<v8::Function> onmousedown;

These are set when the C++ object is created and when the onmousedown
callback accessor is set.

Then when I get the event, I do:

void callOnMouseDown( int x, int y) {
        if ( onmousedown->IsFunction() ) {
                int argc = 2;
                v8::Handle<v8::Value>argv[2];
                argv[0]         = v8::Number::New(x);
                argv[1] = v8::Number::New( y );
                onmousedown->Call( self, argc, argv);
        }
}


My problem is that I get a segfault if the onmousedown is called when
it hasn't been set in the javascript.   Is there anyway to tell from the
C++ side if the onmousedown function has been set in the ecmascript?

Or, is there an alltogether better way of doing this?

many thanks -august.

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to