#include <v8.h>
using namespace v8;
int x = 9527;
void XGetter(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
info.GetReturnValue().Set(Integer::New(x));
}
void XSetter(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
{
x = value->Int32Value();
}
int main()
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->SetAccessor(String::New("x"), XGetter, XSetter);
Handle<Context> context = Context::New(isolate, NULL, global);
Context::Scope context_scope(context);
Handle<Script> script = Script::Compile(String::New("++x"));
Handle<Value> value = script->Run();
String::AsciiValue ascii(value);
printf("%s\n", *ascii);
return 0;
}
注意setter和getter的类型
void XSetter(Local<String> property, Local<Value> value, const PropertyCallbackInfo<void>& info)
void XGetter(Local<String> property, const PropertyCallbackInfo<Value>& info)