On Fri, Jan 23, 2009 at 7:25 PM, vlad <[email protected]> wrote: > > Thank you very much for the info, Matthias. Although, I have to admit > I haven't been able to get it working properly, yet. > > Is there a way to pass arguments when creating new instances using > this method?
This is your guide: http://v8.googlecode.com/svn/trunk/include/v8.h */ class EXPORT Function : public Object { public: Local<Object> NewInstance() const; Local<Object> NewInstance(int argc, Handle<Value> argv[]) const; Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]); void SetName(Handle<String> name); Handle<Value> GetName() const; static Function* Cast(Value* obj); private: Function(); }; > I'm running in to issues where I define the constructor > for a function: > > ... = FunctionTemplate::New(constructor_callback); > > > Handle<Value> constructor_callback(const Arguments& args[]) > { > if (args.Length() != 3) > { > return throwArgLengthException(3); > } > } > > > ------ > Now, when I call NewInstance() on the ObjectTemplate, I guess it calls > the callback function. That in turn complains that the object needs 3 > arguments (as it should). > > On Jan 22, 3:53 pm, Matthias Ernst <[email protected]> wrote: >> Create a static ObjectTemplate for "Points" and set accessors and >> functions on it. >> From the function, you return an pointTemplate->NewInstance(). >> >> Here's an example I have handy for "Prices", the object being backed >> by a C++ "Price" object: >> >> +Handle<ObjectTemplate> PriceTemplate() { >> + Handle<ObjectTemplate> result = ObjectTemplate::New(); >> + result->SetAccessor(String::New("amount"), GetPriceAmount); >> + result->SetAccessor(String::New("currency"), GetPriceCurrency); >> + >> + result->SetInternalFieldCount(1); >> + return Persistent<ObjectTemplate>::New(result); >> +} >> + >> +Handle<Object> WrapPrice(const Price *p) { >> + static Handle<ObjectTemplate> price_template = PriceTemplate(); >> + >> + Handle<Object> result = price_template->NewInstance(); >> + result->SetInternalField(0, External::New(const_cast<Price *>(p))); >> + return result; >> +} >> >> On Thu, Jan 22, 2009 at 5:43 PM, vlad <[email protected]> wrote: >> >> > Also, I've been looking over the Google Chrome source code, but can't >> > find examples of this. If anyone knows where I can find example under >> > the Chrome source tree, please speak up. >> >> > On Jan 22, 10:56 am, vlad <[email protected]> wrote: >> >> I am still having problems returning an object from a getter, or from >> >> any function from that matter. >> >> >> I can't just call the same constructor callback that I use to create >> >> the object, because v8::Arguments constructor is private. And it >> >> doesn't seem like proper approach either. >> >> I also tried the solution that luiskarlos sudgested, but that's not it >> >> either. Some is returned, but there's no way to tell V8 that the >> >> return object is of X type -- it always seems to believe that the >> >> returned object is of the type doing the returning. Plus, if this were >> >> the way to return an object instance, then it would mean that you have >> >> to REdeclare destructor callbacks for those object instances. >> >> I would have though that FunctionTemplate would have some method of >> >> instantiating an instance, but I don't see any methods of doing this. >> >> >> Can someone please shine some light on this? >> >> >> How can one return an object instance of type X from a function or >> >> method? > > > --~--~---------~--~----~------------~-------~--~----~ v8-users mailing list [email protected] http://groups.google.com/group/v8-users -~----------~----~----~----~------~----~------~--~---
