As I said it is artificial example :) The key is how original object is
created...

I need an object that can be called as a function and at the same time have
properties which I can set getters for... I didn't find the way doing
so besides:

creating v8::Object like this
    V8Obj* a = new V8Obj;
    v8::Handle<v8::FunctionTemplate> myObjTempl =
v8::FunctionTemplate::New();
    myObjTempl->SetClassName(v8::String::New("myObj"));
    v8::Handle<v8::ObjectTemplate> myObjPropTempl =
myObjTempl->InstanceTemplate();
    myObjPropTempl->SetInternalFieldCount(1);
    v8::Handle<v8::Function> myObjCtor = myObjTempl->GetFunction();
    v8::Handle<v8::Object> myObj = myObjCtor->NewInstance();
    myObj->SetInternalField(0,v8::External::New(&a));

Putting "signal" property to myObj:

        v8::Handle<v8::FunctionTemplate> signalTempl =
v8::FunctionTemplate::New();
        v8::Handle<v8::ObjectTemplate> signalPropTempl =
signalTempl->InstanceTemplate();
        signalPropTempl->SetAccessor(v8::String::New("length"),
V8Obj::LenghtGetter); - "length" getter
        signalPropTempl->SetCallAsFunctionHandler(V8Obj::signalInvoke); -
callback
        v8::Handle<v8::Function> signalCtor = signalTempl->GetFunction();
        signalCtor->SetName(v8::String::New("signal"));
        v8::Handle<v8::Object> signalObj = signalCtor->NewInstance();

publish...
    global->Set(v8::String::New("myObj"), myObj);
    myObj->Set(v8::String::New("signal"), signalObj);

    V8RUN("myObj.signal();");
    V8RUN("myObj.signal.length");

V8RUN() - just compiles and runs the script.

signal() could have a parameter created the same way as signal() and I need
to call that object as function from the callback. As you can see in this
case IsFunction() for such object wouldn't be true...

Thanks
Vlad





On Tue, Jun 1, 2010 at 1:46 PM, Stephan Beal <[email protected]> wrote:

> On Tue, Jun 1, 2010 at 10:30 PM, vlad vladbph <[email protected]> wrote:
>
>> Thanks Stephan,
>> But if you look at the way I create an object in the original e-mail
>> casting to a function fails in the callback - when I get the object from
>> args[0]...
>>
>
> As far as i can see:
>
> static v8::Handle<v8::Value> func(const v8::Arguments& args) {
>    if (args.IsConstructCall())
>        return v8::Undefined();
>
>    v8::Local<v8::Object> obj = args[0]->ToObject();
>    int* pThis = static_cast<int*>(v8::Local<v8::External>::Cast(obj-
> >GetInternalField(0))->Value()); <-- CRASH
>    return v8::Undefined();
> }
>
> you're never casting to a function there. You're fetching the internal
> field with no regard for the type of args[0] (other than that it be-a
> Object). (Which, as far as i can tell, should work for what you're doing.)
>
> :-?
>
> i don't know why the V8RUN() bit is failing to behave as you expect - as
> far as i can tell, the code "should" do what you're describing. But without
> seeing definitions of V8RUN() and the g object, i can only speculate as to
> what's really happening there.
>
> i've never used the SetCallAsFunctionHandler() API, but i'm guessing that
> understanding it's behaviour is key to answering your question. i don't
> personally see much point in creating a non-function object which behaves
> like-a function. Why not create a Function in the first place, using
> FunctionTemplate::New(func)->GetFunction()?
>
> --
> ----- stephan beal
> http://wanderinghorse.net/home/stephan/
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>

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

Reply via email to