How to use callbacks with nixysa?

129 views
Skip to first unread message

rzimmer

unread,
Apr 6, 2010, 9:28:45 AM4/6/10
to nixysa-users
Hey,

I recently saw in the source of nixysa that there is some sort of
callback support. That's really nice! But I was unable to find a
description of how to use this and reading the source didn't get me
any further.

Could you please provide me with a simple example (IDL file, C++ /
JavaScript method stubs or similiar) showing how to use callbacks?

Thanks in advance!

Antoine Labour

unread,
Apr 6, 2010, 12:58:35 PM4/6/10
to nixysa...@googlegroups.com

In a nutshell, in idl you declare 'callback ReturnType MyCallback(Type1 param1, Type2 param2);' and that corresponds to a C++ class that has a virtual function "ReturnType Run(Type1 param1, Type2 param2);"
When assigning the callback to a field or passing a parameter to a function, a C++ object of that type, wrapping the JS function, gets created and passed, that call the JS function when Run is called.
2 caveats:
- lifetime: the field/function receiving this callback is responsible for destroying (with delete) the newly created object.
- you can't "get" a callback object, i.e. callback fields need to be write only, and you can't return them from functions

Antoine


--
You received this message because you are subscribed to the Google Groups "nixysa-users" group.
To post to this group, send email to nixysa...@googlegroups.com.
To unsubscribe from this group, send email to nixysa-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nixysa-users?hl=en.


rzimmer

unread,
Apr 7, 2010, 4:37:20 AM4/7/10
to nixysa-users
Thanks a lot for your hints!

I managed to get the glue code successfully created. Here is a
exemplary code for future reference:

myclass.idl
=========
[binding_model=by_value, include="myclass.h"]
class MyClass {
MyClass();

callback void MyCallback(std::string arg0, std::string arg1);
void setMyCallback(MyCallback? myCallback);
};

myclass.h
=======
class MyClass {
public:
class MyCallback
{
public:
virtual void Run(const std::string& arg0, const std::string& arg1) =
0;
};

MyClass();
~MyClass();

void setMyCallback(MyCallback* myCallback) { this->myCallback =
myCallback};

private:
MyCallback* myCallback;
};

demo.js
=======
(...)

function callbackFunction(str1, str2) {
console.log(str1);
console.log(str2);
}

var plugin = document.getElementById('plugin');
var myClass = plugin.MyClass();
myClass.setMyCallback(callbackFunction);
(...)

Now you can call myCallback->Run(str1, str2) from anywhere in your C++
code to trigger the callback JavaScript function "callbackFunction".

Note the caveats Antoine mentioned in the post before...


On 6 Apr., 18:58, Antoine Labour <pi...@google.com> wrote:


> On Tue, Apr 6, 2010 at 6:28 AM, rzimmer <Roman.Zim...@sprylab.com> wrote:
> > Hey,
>
> > I recently saw in the source of nixysa that there is some sort of
> > callback support. That's really nice! But I was unable to find a
> > description of how to use this and reading the source didn't get me
> > any further.
>
> > Could you please provide me with a simple example (IDL file, C++ /
> > JavaScript method stubs or similiar) showing how to use callbacks?
>
> > Thanks in advance!
>

> See for examplehttp://src.chromium.org/viewvc/chrome/trunk/src/o3d/plugin/idl/archiv...
> the corresponding C++ classhttp://src.chromium.org/viewvc/chrome/trunk/src/o3d/import/cross/arch...).


>
> In a nutshell, in idl you declare 'callback ReturnType MyCallback(Type1
> param1, Type2 param2);' and that corresponds to a C++ class that has a
> virtual function "ReturnType Run(Type1 param1, Type2 param2);"
> When assigning the callback to a field or passing a parameter to a function,
> a C++ object of that type, wrapping the JS function, gets created and
> passed, that call the JS function when Run is called.
> 2 caveats:
> - lifetime: the field/function receiving this callback is responsible for
> destroying (with delete) the newly created object.
> - you can't "get" a callback object, i.e. callback fields need to be write
> only, and you can't return them from functions
>
> Antoine
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nixysa-users" group.
> > To post to this group, send email to nixysa...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > nixysa-users...@googlegroups.com<nixysa-users%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages