However: If you do instance->Set(Number::New(13), Number::New(14)), instance[13] will return 14. With the accessor set on the instance template, the accessor is called instead. The prototype accessor is only called if the property lookup actually reaches the prototype.
What to make of it? The instance template certainly gives you more control and the implementation cost is the same AFAIU, so I see no benefit in using the prototype template. The prototype template might be interesting if you're considering inheritance between FunctionTemplates. Matthias On Wed, Mar 31, 2010 at 1:37 PM, Matthias Ernst <[email protected]> wrote: > > > On Wed, Mar 31, 2010 at 12:07 PM, Stephan Beal <[email protected]>wrote: > >> Hello, all! >> >> i just spent 3 hours trying to figure out why my >> ObjectTemplate::SetIndexedPropertyHandler()-assigned handlers were >> never being called, finally found a hint at the solution in some 3rd- >> party code[1], and now i have a question... >> > > Works for me. > > #include <iostream> > #include "third_party/v8/include/v8.h" > > using v8::AccessorInfo; > using v8::Context; > using v8::FunctionTemplate; > using v8::Handle; > using v8::HandleScope; > using v8::Number; > using v8::Script; > using v8::String; > using v8::V8; > using v8::Value; > > Handle<Value> GetValue(uint32_t index, const AccessorInfo& info) { > return Number::New(index); > } > > int main(int argc, char **argv) { > V8::SetFlagsFromCommandLine(&argc, argv, true); > > HandleScope handle_scope; > > Handle<Context> context = Context::New(); > Context::Scope context_scope(context); > > Handle<FunctionTemplate> ft = FunctionTemplate::New(); > ft->PrototypeTemplate()->SetIndexedPropertyHandler(GetValue); > > context->Global()->Set(String::New("F"), ft->GetFunction()); > cout << *String::AsciiValue( > Script::Compile(String::New("new F()[13]"))->Run()) << endl; > > context->Global()->Set(String::New("instance1"), > ft->InstanceTemplate()->NewInstance()); > cout << *String::AsciiValue( > Script::Compile(String::New("instance1[13]"))->Run()) << endl; > > context->Global()->Set(String::New("instance2"), > ft->GetFunction()->NewInstance()); > cout << *String::AsciiValue( > Script::Compile(String::New("instance2[13]"))->Run()) << endl; > > return 0; > } > > > >> >> Apparently SetIndexedPropertyHandler() must be called on classTemplate- >> >InstanceTemplate() and not classTemplate->PrototypeTemplate() (where >> all other class properties except InternalFieldCount are set, >> including the AccessorGetter/Setter bindings). >> >> Why? >> >> What's the difference between the InstanceTemplate and the >> PrototypeTemplate? >> >> >> :-? >> >> >> [1]=http://www.sand-labs.org/svn/trunk/WebCore/bindings/v8/ >> V8Collection.h >> >> -- >> v8-users mailing list >> [email protected] >> http://groups.google.com/group/v8-users >> >> To unsubscribe, reply using "remove me" as the subject. >> > > -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
