On Mon, Feb 17, 2014 at 10:51 AM, Danny Dorfman <[email protected]> wrote: > Hello there, > > I have a piece of native C++ code that creates "special" objects. Objects, > that behave differently under certain conditions. > My question is, how to make these special objects resume their normal > functionality. I need something that says (in pseudo-code) > > Getter() { > if (my condition) { > return a specially calculated value / I know how to do this / > } else > return the regular JS value / How do I do this? / > } > } > Setter() { > if (my condition) { > cause a special side effect / I know how to do this / > } else > set the regular JS value / How do I do this? / > } > } > > > Here is a sample code I've written to investigate this issue (based on > "hello world"). > > 1 #include <v8.h> > 2 #include <stdio.h> > 3 using namespace v8; > 4 > 5 void GetAlfa(Local<String> property, const PropertyCallbackInfo<Value>& > info) > 6 { > 7 } > 8 void SetAlfa(Local<String> property, Local<Value> value, const > PropertyCallbackInfo<void>& info) > 9 { > 10 } > 11 > 12 int main(int argc, char* argv[]) { > 13 Isolate* isolate = Isolate::GetCurrent(); > 14 HandleScope handle_scope(isolate); > 15 Handle<Context> context = Context::New(isolate); > 16 Context::Scope context_scope(context); > 17 Handle<ObjectTemplate> specific_templ = ObjectTemplate::New(); > 18 specific_templ->SetAccessor(String::New("alfa"), GetAlfa, SetAlfa); > 19 Handle<Object> specific_obj = specific_templ->NewInstance(); > 20 context->Global()->Set(String::New("specific"), specific_obj); > 21 Handle<String> source = String::New("specific.alfa=7; specific.alfa"); > 22 Handle<Script> script = Script::Compile(source); > 23 Handle<Value> result = script->Run(); > 24 String::AsciiValue ascii(result); > 25 printf("%s\n", *ascii); > 26 return 0; > 27 } > > > Currently the code returns "undefined". Is there a way to make it return "7" > without removing the getter and setter, and without going to External or > static storage? > I did some primitive testing, and ended up in infinite get- or set-loops. > > Regards, > Danny
You can pass a Handle<Value> as the fourth argument to SetAccessor() that you can retrieve in the callback with info.Data(). In your main program, pass in a Handle<Object>, maintain a Persistent<Object> reference to the object and update a state field when appropriate. In your callback, cast the return value of info.Data() to Handle<Object>, check the state field and act accordingly. Your accessor is a property of the ObjectTemplate so changes to the state object will affect all instances. If that's an issue, move the accessor to the actual instance. Hope that helps. -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
