Hi, All,

There are two object involved:

Prototype
   ^
   |
   |
Object


I set interceptor on prototype template, and have set internal field on
Object.

Is there a way to access these internal field of Object in intercrptor of
Prototype?



For Accessor, there is a way to define signature, then the VM will pass the
right object to accessor, how about inteceptors?


Please check the following sample, it will crash if I try a access the
internal filed.


#include <v8.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>

using namespace v8;

v8::Handle<v8::Value> Getter(v8::Local<v8::String> propertyName,const
v8::AccessorInfo &info) {

   v8::HandleScope handleScope;
    v8::Handle<v8::External> external =
v8::Handle<v8::External>::Cast(info.Holder()->GetInternalField(0));
    void * message = external->Value();
    const char * str =  (const char *)message;
    printf(str);
    //return something that is not NULL back
    return v8::True();
}

Handle<Value> CreateObjectWithInteceptor(const Arguments& args)
{
    HandleScope handleScope;
    const char * test = "With Inteceptor";
    v8::Handle<v8::External> external = v8::External::New((void *)test);
    args.Holder()->SetInternalField(0, external);
    return args.Holder();
}

int main()
{
HandleScope handleScope;

  Handle<FunctionTemplate> functionTemplate =
FunctionTemplate::New(CreateObjectWithInteceptor);
functionTemplate->SetClassName(String::New("CreateObjectWithInteceptor"));
 v8::Local<v8::Signature> default_signature =
v8::Signature::New(functionTemplate);
 Handle<ObjectTemplate> instanceTemplate =
functionTemplate->InstanceTemplate();
instanceTemplate->SetInternalFieldCount(1); // create an internal field for
the C++ object
Handle<ObjectTemplate> prot = functionTemplate->PrototypeTemplate();
prot->SetNamedPropertyHandler(Getter);


Handle<ObjectTemplate> globals = ObjectTemplate::New();
  globals->Set(String::New("object"), instanceTemplate); // add our print
function
Persistent<Context> context = Context::New(0, globals);
Context::Scope contextScope(context);

Handle<Script> script = Script::Compile(v8::String::New("object.getValue"));
Handle<Value> result = script->Run();
context.Dispose();
return 0;
}


Cheers~
Xiang

-- 
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev

Reply via email to