If I understand you correctly your question is: given a function
object as a callback argument, how do you create an instance of that
function?  You can do something along these lines:

Handle<Value> my_function(const Arguments& args) {
  Handle<Object> arg = args[0];
  if (!arg->IsFunction()) {
    return Undefined();
    // you could also throw an exception here
  }
  Handle<Function> cons = Handle<Function>::Cast(arg);
  return arg->NewInstance();
}

'F->NewInstance()' is the C++ version of javascript's 'new F()'.


-- Christian

On Tue, Nov 25, 2008 at 1:38 PM, George Moschovitis
<[EMAIL PROTECTED]> wrote:
>
> Hello everyone,
>
> since this is my first post on this group, let me start by sending
> thanks to the V8 developers for releasing this great JavaScript
> engine. With that out of the way, let's move to my question:
>
> I have a C++ function that works more or less like this:
>
> Handle<Value> my_function(const Arguments & args) {
>    HandleScope handle_scope;
>    ..
>    Handle<Object> obj = Object::New();
>    ..
>
>    return obj;
> }
>
> This is accessible from JavaScript as myFunction(), ie:
>
> var obj = myFunction();
>
> I would like to convert this function to work like this:
>
> var User = function() {
>    ..
> };
> var user = myFunction(User); // should return a User 'instance'
> instead of a 'plain' Object
>
> How should I change this line:
>
>    Handle<Object> obj = Object::New();
>
> to take args[0] into account?
>
>
> thanks in advance for your help,
>
> George.
> >
>

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

Reply via email to