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

Reply via email to