Re: [v8-users] How to test an Argument for InstanceOf() type
On Wed, Apr 24, 2013 at 1:07 AM, Mike Moening mike.moen...@gmail.com wrote: Ben, Can you expand on your example? I'm sure this is piece of cake for you, but I'm not getting what you mean. All the samples I can find show using ObjectTemplate and the SetCallAsFunctionHandler() to handle construction from the C++ side. var o = new MyObject(); var x = new MyObject(); o.doSomething(x); In the above example doSomething() needs to simply validate its 1st argument is of type MyObject. That's it. Apparently HasInstance() (from FunctionTemplate) is for this purpose. But ObjectTemplate doesn't have a HasInstance() method. My object has to do more than just be constructed. It's got other methods too. How can I fire a C++ constructor function with a FunctionTemplate and have other methods on the object? Without a more concrete example I'm lost. I do appreciate the willingness to help. THANK YOU. This is in a nutshell how you use FunctionTemplate to create a constructor + prototype methods: LocalFunctionTemplate t = FunctionTemplate::New(MyObject::New); t-SetClassName(String::New(MyObject)); t-InstanceTemplate()-SetInternalFieldCount(1); LocalFunctionTemplate fn = FunctionTemplate::New(MyObject::Foo); t-PrototypeTemplate()-Set(String::New(foo), fn); my_object_template = PersistentFunctionTemplate::New(t); The constructor looks something like this: HandleValue MyObject::New(const Arguments args) { HandleScope handle_scope; if (args.IsConstructCall() === false) { // Handle non-constructor invocation, i.e. `MyObject()` LocalFunction constructor = my_object_template-GetFunction(); return handle_scope.Close(constructor-NewInstance(0, NULL)); } MyObject* instance = new MyObject; args.This()-SetAlignedPointerInInternalField(0, instance); return args.This(); } The foo() method: HandleValue MyObject::Foo(const Arguments args) { HandleScope handle_scope; void* pointer = args.This()-GetAlignedPointerFromInternalField(0); MyObject* instance = static_castMyObject*(pointer); // do whatever return handle_scope.Close(...); } The trick I mentioned in my other post is that you add a second internal field that points to some atom (usually a char class_name_id[] = MyClassName) that you check for in your prototype methods so you can be sure that args.This() is what you expect it to be. Be sure to check args.This()-InternalFieldCount() as well. We don't use that trick in node.js actually but that's because native objects are not exposed directly, there's (almost) always some pure JS object sitting in front of it. It's a pretty good approach when you're not operating in a hostile environment (running untrusted code), it saves a lot of headaches. -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [v8-users] How to test an Argument for InstanceOf() type
On Wed, Apr 24, 2013 at 11:00 AM, Ben Noordhuis i...@bnoordhuis.nl wrote: ...The trick I mentioned in my other post is that you add a second internal field that points to some atom (usually a char class_name_id[] = MyClassName) that you check for in your prototype methods so you can be sure that args.This() is what you expect it to be. Be sure to check args.This()-InternalFieldCount() as well. Another option, as opposed to using a string, is take the address of some internal static value. The value and type of that pointer is largely irrelevant - it's _address_ can be used as the type identifier. In cvv8 we use a mixture: we use the address of a static string (not its value) defined in a template, since comparing the address is much faster than doing a strcmp and the address is guaranteed to be unique within the app's address space. We don't use that trick in node.js actually but that's because native objects are not exposed directly, there's (almost) always some pure JS object sitting in front of it. It's a pretty good approach when you're not operating in a hostile environment (running untrusted code), it saves a lot of headaches. Here's an example where it is important to have such a type-safety net: Assume Foo and Bar are both client-define native types: var x = new Foo(); var y = new Bar(); x.doBar = y.doBar; x.doBar(); // doBar expects that 'this' is-a (Bar*) -- - stephan beal http://wanderinghorse.net/home/stephan/ http://gplus.to/sgbeal -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [v8-users] How to test an Argument for InstanceOf() type
On Wed, Apr 24, 2013 at 12:28 PM, Stephan Beal sgb...@googlemail.com wrote: On Wed, Apr 24, 2013 at 11:00 AM, Ben Noordhuis i...@bnoordhuis.nl wrote: ...The trick I mentioned in my other post is that you add a second internal field that points to some atom (usually a char class_name_id[] = MyClassName) that you check for in your prototype methods so you can be sure that args.This() is what you expect it to be. Be sure to check args.This()-InternalFieldCount() as well. Another option, as opposed to using a string, is take the address of some internal static value. That's not another option - that's the same option. :-) class_name_id decays to a pointer when you pass it to SetAlignedPointerInInternalField(). The fact that it's a string is to make debugging easier. -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [v8-users] How to test an Argument for InstanceOf() type
On Wed, Apr 24, 2013 at 1:18 PM, Ben Noordhuis i...@bnoordhuis.nl wrote: That's not another option - that's the same option. :-) Yeah, technically. My point was only that it could some opaque pointer, e.g.: static int foo = 3; and pass a pointer to that. But yes, having it be a string makes debuggering easier. -- - stephan beal http://wanderinghorse.net/home/stephan/ http://gplus.to/sgbeal -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[v8-users] implementing ObjectTemplate HasInstance() method
After digging into object construction using FunctionTemplate or ObjectTemplate in this post: https://groups.google.com/forum/?fromgroups=#!topic/v8-users/521aXb3Uer4 I believe the ObjectTemplate is missing a HasInstance() method for use in testing arguments to JS functions to be of a certain type. While it is possible to use FunctionTemplate for this purpose (since HasInstance() exists), the FunctionTemplate method of construction is convoluted and messy since it requires two FunctionTemplates or a hack to prevent constructor recursion. My proposal is to create HasInstance() on the ObjectTemplate class. I'm looking for guidance on the best way to accomplish this. *Questions:* 1) How can I get the ObjectTemplate that was used to create an instance of an Object? 2) How can I get the constructor for both the ObjectTemplate and the Object and compare them? My assumption is that I should be comparing the constructors for equality. The internals of v8 use a class called ObjectTemplateInfo which is somewhat confusing to me at this point. Thanks for the help! -- -- v8-users mailing list v8-users@googlegroups.com 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 v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.