I see. Sorry, I hoped it might work. I am afraid v8 currently doesn't support the thing you need just immediately.
Possible ways out: 1) implement it :) 2) chances are you could do that in one tricky way: you can access PrototypeTemplate of FunctionTemplate and probably you can set interceptor on this prototype template object; caveat: it might be somewhat slower---interceptors inhibit some v8 optimizations, but if it works at all, check if speed is acceptable for you 3) [variation of the previous idea]: create another object with interceptor reflecting Ruby class and put is somewhere in prototype chain of the function (e.g. below prototype). However, I am not sure you'll be able to enumerate those properties with getOwnProperties. And there is another workaround: if you have hooks on property assignment to Ruby classes, you could propagate those changes to JS counterparts. hth and yours, anton. On Tue, Aug 17, 2010 at 1:26 AM, Charles Lowell <[email protected]> wrote: > Anton, > > I have run a few experiments using an ObjectTemplate in place of a > FunctionTemplate, and it looks like it falls short of a number of > ways: > I have an Object with a CallAsFunctionHandler named 'Foo' in this > scope that just returns a plain object. > > Foo.prototype = {bar: 'baz'} > var f = new Foo() > print("foo: ", Foo) //=> [object Object], should be function Foo() {}. > print("typeof Foo: ", typeof Foo) //=> 'object', should be 'function' > print("f.constructor == Foo: ", f.constructor == Foo) //=> false, > should be true > print("f.bar: ", f.bar) //=> undefined, should be 'baz' > print("f instanceof Foo", f instanceof Foo) //=> TypeError: Expecting > a function in instanceof check, but got #<an Object> > > you can see the full program here: > > http://gist.github.com/527745 > > As you can see SetCallAsFunctionHandler() doesn't really do all that > well as a constructor. > > cheers, > Charles > > On Aug 10, 3:05 pm, Charles Lowell <[email protected]> wrote: >> It might, provided typeof is 'function' and the constructor property >> is setup correctly. The other problem is that I currently invoke these >> constructor functions from C++ via NewInstance() and I can't find the >> equivalent of CallAsFunction() or NewInstanceAsFunction() on Object. >> >> I may be able to find a way around this by wiring up the ruby and js >> peers without calling constructors from C++, but IIRC that was a very >> clean solution to make sure all the peered objects, whether >> constructed from javascript or injected directly into the context went >> through the same coupling code. I'll give it a try and let you know >> what I find. >> >> cheers, >> Charles >> >> On Aug 10, 2:31 pm, Anton Muhin <[email protected]> wrote: >> >> >> >> > I don't know for sure, but won't SetCallAsFunctionHandler help you? >> >> > yours, >> > anton. >> >> > On Tue, Aug 10, 2010 at 11:11 PM, Charles Lowell >> >> > <[email protected]> wrote: >> >> > > On Aug 10, 8:09 am, Anton Muhin <[email protected]> wrote: >> > >> Sorry, wrong address again. >> >> > >> On Tue, Aug 10, 2010 at 5:06 PM, Anton Muhin <[email protected]> wrote: >> > >> > Charles, >> >> > >> > I probably miss something here, but why you need FunctionTemplates at >> > >> > all, could not you use only ObjectTemplates? >> >> > > How do I implement constructor functions with ObjectTemplate ? >> >> > >> > yours, >> > >> > anton. >> >> > >> > On Mon, Aug 9, 2010 at 10:58 PM, Charles Lowell >> > >> > <[email protected]> wrote: >> > >> >> Anton, >> >> > >> >> Thanks for the reply. Perhaps it will make it clearer if I provide >> > >> >> some context. I'm asking in the context of The Ruby Racer, which >> > >> >> allows for embedding V8 into Ruby, and in particular what ruby >> > >> >> classes >> > >> >> look like from javascript. >> >> > >> >> Currently, we model ruby instances reflected into javascript as >> > >> >> instances created off of a FunctionTemplate which corresponds to that >> > >> >> instance's class. The FunctionTemplate corresponding to the ruby >> > >> >> class >> > >> >> has property interceptors which it sets via >> > >> >> FunctionTemplate::InstanceTemplate()->Set{Named/Indexed} >> > >> >> PropertyHandler. >> >> > >> >> The problem comes when we actually embed the ruby classes themselves >> > >> >> into javascript. This is done by having the class's >> > >> >> FunctionTemplate's >> > >> >> CallHandler invoke the ruby constructor of the class, and then >> > >> >> associat it with a javascript instance created by calling >> > >> >> GetFunction().NewInstance(). That way, the constructor can be >> > >> >> (optionally) called directly from javascript, and the object's >> > >> >> 'constructor' will point to the right place. >> >> > >> >> However, ruby classes (like javascript constructors) are objects too >> > >> >> with their own properties and methods and so I would like to be able >> > >> >> to intercept access to properties against the constructor function >> > >> >> itself in the same way that I intercept property access on objects. >> > >> >> In >> > >> >> one sentence: I want to delegate Function object properties to a ruby >> > >> >> object in the same way that I delegate regular object properties to a >> > >> >> ruby object. >> >> > >> >> Perhaps a little code would shed some light. >> >> > >> >> class SomeClass >> > >> >> #instance method >> > >> >> def one >> > >> >> 1 >> > >> >> end >> > >> >> end >> >> > >> >> #a class method >> > >> >> def SomeClass.two >> > >> >> 2 >> > >> >> end >> >> > >> >> cxt = V8::Context.new >> > >> >> cxt['SomeClass'] = SomeClass >> > >> >> cxt.eval(<<--JS) >> > >> >> var s = new SomeClass() >> > >> >> s.one //=> 1 >> > >> >> s.constructor == SomeClass //=> true >> > >> >> SomeClass.two //=> currently undefined, I would like this to invoke >> > >> >> SomeClass.two() >> > >> >> JS >> >> > >> >> It could be that there is an obvious solution which I've missed, but >> > >> >> does that help frame the question? >> >> > >> >> cheers, >> > >> >> Charles >> >> > >> >> On Aug 9, 11:18 am, Anton Muhin <[email protected]> wrote: >> > >> >>> Sorry, from the right address. >> >> > >> >>> On Mon, Aug 9, 2010 at 8:16 PM, Anton Muhin <[email protected]> >> > >> >>> wrote: >> > >> >>> > Charles, good day, >> >> > >> >>> > On Mon, Aug 9, 2010 at 7:56 PM, Charles Lowell >> > >> >>> > <[email protected]> wrote: >> > >> >>> >> While we're on the subject of property interceptors and API >> > >> >>> >> asymmetries, I can't figure out to add named and indexed getter/ >> > >> >>> >> setter, etc.. to Function objects inside the javascript runtime. >> > >> >>> >> I've >> > >> >>> >> searched extensively for a way to do this, but nothing as >> > >> >>> >> reliable as >> > >> >>> >> ObjectTemplate::SetNamedPropertyHandler() >> >> > >> >>> > I don't quite understand what do you mean by 'inside the >> > >> >>> > javascript >> > >> >>> > runtime'? Do you want to invoke JS function? Or add native >> > >> >>> > interceptor to JS function (to be invoked as a constructor)? >> >> > >> >>> > AFAIK currently one can only add interceptors (both named and >> > >> >>> > indexed) >> > >> >>> > to FunctionTemplates. >> >> > >> >>> > BTW, what are you trying to achieve? If you could tell us, maybe >> > >> >>> > there could be found some way out. >> >> > >> >>> > yours, >> > >> >>> > anton. >> >> > >> >>> >> On Aug 6, 12:49 pm, Anton Muhin <[email protected]> wrote: >> > >> >>> >>> From the right address. >> >> > >> >>> >>> On Fri, Aug 6, 2010 at 9:39 PM, Anton Muhin <[email protected]> >> > >> >>> >>> wrote: >> > >> >>> >>> > Charles, >> >> > >> >>> >>> > I just didn't have to time to implement the same >> > >> >>> >>> > functionality for >> > >> >>> >>> > indexed interceptor. Filing a bug and cc'ing you. >> >> > >> >>> >>> > I'll try to implement it, but it might require some time to >> > >> >>> >>> > roll in to >> > >> >>> >>> > make transition to new api smooth. >> >> > >> >>> >>> > yours, >> > >> >>> >>> > anton. >> >> > >> >>> >>> > On Fri, Aug 6, 2010 at 9:31 PM, Charles Lowell >> > >> >>> >>> > <[email protected]> wrote: >> > >> >>> >>> >> Hi, >> >> > >> >>> >>> >> I've noticed a small asymmetry in the interfaces for >> > >> >>> >>> >> IndexedPropertyQuery and NamedPropertyQuery. Specifically >> > >> >>> >>> >> that the >> > >> >>> >>> >> indexed version returns just a Boolean instead of and >> > >> >>> >>> >> Integer encoding >> > >> >>> >>> >> property attributes. Is this intentional? Rhino, for example >> > >> >>> >>> >> allows >> > >> >>> >>> >> for specifying attributes for indexed values as well. >> >> > >> >>> >>> >> cheers, >> > >> >>> >>> >> Charles >> >> > >> >>> >>> >> -- >> > >> >>> >>> >> v8-users mailing list >> > >> >>> >>> >> [email protected] >> > >> >>> >>> >>http://groups.google.com/group/v8-users >> >> > >> >>> >> -- >> > >> >>> >> v8-users mailing list >> > >> >>> >> [email protected] >> > >> >>> >>http://groups.google.com/group/v8-users >> >> > >> >> -- >> > >> >> v8-users mailing list >> > >> >> [email protected] >> > >> >>http://groups.google.com/group/v8-users >> >> > > -- >> > > v8-users mailing list >> > > [email protected] >> > >http://groups.google.com/group/v8-users > > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
