Then you must have missed lines 3583-3586 [0].
** A FunctionTemplate has a corresponding instance template which is* used
to create object instances when the function is used as a* constructor.
Properties added to the instance template are added to* each object
instance.*
Then in your JS side example you explain you expect to use the function like
var v = new Value();
v.__proto__;
So, based on you example, and the doc, I would modify your code to do
v8::Local<v8::FunctionTemplate> result =
v8::FunctionTemplate::New(isolate,valueCstor);
result->SetClassName(className);
result->InstanceTemplate()->SetInternalFieldCount(VALUE_TEMP_ID_COUNT);
result->*Instance*Template()->Set(
v8::String::NewFromUtf8(isolate, "toString"),
v8::FunctionTemplate::New(isolate, valueToString));
Consider that you're attempting to access an instance of your FUNCTION, and
its "as-built" prototype (__proto__) and not it's "as-designed" prototype
(FUNCTION.prototype). When you build "[object Native]" types like this,
you don't inherit from JS's Object for free which is why there is no
default valueOf() or toString().
[0] https://code.google.com/p/v8/source/browse/trunk/include/v8.h#3583
On Wed, Oct 29, 2014 at 9:51 AM, Jane Chen <[email protected]> wrote:
> Hi Louis,
>
> Thanks for reminding me to read the header file, and I did.
>
> I have a reason to put my methods on the prototype -- I want them to be
> inherited by "subclasses".
>
> Now I'm looking at the SetHiddenPrototype and its api tests trying to
> understand how it behaves. If anyone can shed some light on it for me, it
> would be greatly appreciated.
>
> Jane
>
> On Wednesday, October 29, 2014 12:51:43 AM UTC-7, Louis P. Santillan wrote:
>>
>> I don't think you're using the FunctionTemplate correctly. See [0].
>>
>> [0] https://code.google.com/p/v8/source/browse/trunk/include/v8.h#3572
>>
>> On Tue, Oct 28, 2014 at 11:41 PM, Jane Chen <[email protected]> wrote:
>> > Found FunctionTemplate::HasInstance(). Please let me know if there's
>> a
>> > better way to do it.
>> >
>> > On Tuesday, October 21, 2014 8:03:40 PM UTC-7, Jane Chen wrote:
>> >>
>> >> I'm using 3.24.
>> >>
>> >> I create a FunctionTemplate with my own callback function for
>> toString:
>> >>
>> >> v8::Local<v8::FunctionTemplate> result =
>> >> v8::FunctionTemplate::New(isolate,valueCstor);
>> >> result->SetClassName(className);
>> >> result->InstanceTemplate()->SetInternalFieldCount(VALUE_TEMP_ID_COUNT);
>>
>> >> result->PrototypeTemplate()->Set(
>> >> v8::String::NewFromUtf8(isolate, "toString"),
>> >> v8::FunctionTemplate::New(isolate, valueToString));
>> >>
>> >> Everything works fine with functions created through the above, except
>> >> when I try to get the prototype of it:
>> >>
>> >> var v = new Value();
>> >> v.__proto__;
>> >>
>> >> Removing the highlighted lines fixes the issue.
>> >>
>> >> Here's the stack:
>> >>
>> >> Stacktrace (dead0000-dead0001) 0x3cc891104221 0x3cc891104149:
>> >> ==== JS stack trace =========================================
>> >>
>> >> Security context: 0x1fa4ac285451 <JS Object>#0#
>> >> 1: DefaultNumber [native runtime.js:381] (this=0x1fa4ac263541 <JS
>> >> Object>#1#,a=0x3cc891104221 <Map(elements=0)>#2#)
>> >> 2: ToNumber(aka ToNumber) [native runtime.js:317]
>> (this=0x1fa4ac204121
>> >> <undefined>,a=0x3cc891104221 <Map(elements=0)>#2#)
>> >> 3: ToInteger [native runtime.js:357] (this=0x1fa4ac285561 <JS
>> Global
>> >> Object>#3#,a=0x3cc891104221 <Map(elements=0)>#2#)
>> >> 7: DefaultString [native runtime.js:396] (this=0x1fa4ac263541 <JS
>> >> Object>#1#,a=0xb357401bcf9 <JS Object>#4#)
>> >> 8: ToString [native runtime.js:334] (this=0x1fa4ac285561 <JS
>> Global
>> >> Object>#3#,a=0xb357401bcf9 <JS Object>#4#)
>> >>
>> >> ==== Details ================================================
>> >>
>> >> [1]: DefaultNumber [native runtime.js:381] (this=0x1fa4ac263541 <JS
>> >> Object>#1#,a=0x3cc891104221 <Map(elements=0)>#2#) {
>> >> // stack-allocated locals
>> >> var b = 0x1fa4ac204121 <undefined>
>> >> var c = 0x1fa4ac204121 <undefined>
>> >> var d = 0x1fa4ac204121 <undefined>
>> >> var e = 0x1fa4ac204121 <undefined>
>> >> // expression stack (top to bottom)
>> >> [05] : 0x8824290ee39 <String[7]: valueOf>
>> >> [04] : 0x3cc891104221 <Map(elements=0)>#2#
>> >> --------- s o u r c e c o d e ---------
>> >> function DefaultNumber(a){?var
>> >> b=a.valueOf;?if((%_ClassOf(b)==='Function')){?var
>> >> c=%_CallFunction(a,b);?if(%IsPrimitive(c))return c;?}?var
>> >> d=a.toString;?if((%_ClassOf(d)==='Function')){?var
>> >> e=%_CallFunction(a,d);?if(%IsPrimitive(e))return e;?}?throw
>> >> %MakeTypeError('cannot_convert_to_primitive',[]);?}
>> >> -----------------------------------------
>> >> }
>> >>
>> >> [2]: ToNumber(aka ToNumber) [native runtime.js:317]
>> (this=0x1fa4ac204121
>> >> <undefined>,a=0x3cc891104221 <Map(elements=0)>#2#) {
>> >> // expression stack (top to bottom)
>> >> [04] : 0x3cc891104221 <Map(elements=0)>#2#
>> >> [03] : 0x1fa4ac263541 <JS Object>#1#
>> >> [02] : 0x1fa4ac282c49 <JS Function DefaultNumber (SharedFunctionInfo
>> >> 0x1fa4ac21f9c1)>#5#
>> >> [01] : 0x1fa4ac204121 <undefined>
>> >> [00] : 0x1fa4ac273191 <JS Function ToNumber (SharedFunctionInfo
>> >> 0x1fa4ac21f161)>#6#
>> >> --------- s o u r c e c o d e ---------
>> >> function ToNumber(a){?if((typeof(a)==='number'))return
>> >> a;?if((typeof(a)==='string')){?return
>> >> %_HasCachedArrayIndex(a)?%_GetCachedArrayIndex(a)?:%
>> StringToNumber(a);?}?if((typeof(a)==='boolean'))return
>> >> a?1:0;?if((typeof(a)==='undefined'))return
>> >> $NaN;?if((typeof(a)==='symbol'))return
>> >> $NaN;?return((a===null))?0:ToNumber...
>> >>
>> >> -----------------------------------------
>> >> }
>> >>
>> >> [3]: ToInteger [native runtime.js:357] (this=0x1fa4ac285561 <JS Global
>> >> Object>#3#,a=0x3cc891104221 <Map(elements=0)>#2#) {
>> >> // expression stack (top to bottom)
>> >> [02] : 0x3cc891104221 <Map(elements=0)>#2#
>> >> [01] : 0x1fa4ac204121 <undefined>
>> >> [00] : 0x1fa4ac273191 <JS Function ToNumber (SharedFunctionInfo
>> >> 0x1fa4ac21f161)>#6#
>> >> --------- s o u r c e c o d e ---------
>> >> function ToInteger(a){?if(%_IsSmi(a))return a;?return
>> >> %NumberToInteger(ToNumber(a));?}
>> >> -----------------------------------------
>> >> }
>> >>
>> >> [7]: DefaultString [native runtime.js:396] (this=0x1fa4ac263541 <JS
>> >> Object>#1#,a=0xb357401bcf9 <JS Object>#4#) {
>> >> // stack-allocated locals
>> >> var b = 0x1fa4ac287621 <JS Function toString (SharedFunctionInfo
>> >> 0x1fa4ac287591)>#7#
>> >> var c = 0x1fa4ac204121 <undefined>
>> >> var d = 0x1fa4ac204121 <undefined>
>> >> var e = 0x1fa4ac204121 <undefined>
>> >> // expression stack (top to bottom)
>> >> [05] : 0x1fa4ac287621 <JS Function toString (SharedFunctionInfo
>> >> 0x1fa4ac287591)>#7#
>> >> [04] : 0xb357401bcf9 <JS Object>#4#
>> >> --------- s o u r c e c o d e ---------
>> >> function DefaultString(a){?var
>> >> b=a.toString;?if((%_ClassOf(b)==='Function')){?var
>> >> c=%_CallFunction(a,b);?if(%IsPrimitive(c))return c;?}?var
>> >> d=a.valueOf;?if((%_ClassOf(d)==='Function')){?var
>> >> e=%_CallFunction(a,d);?if(%IsPrimitive(e))return e;?}?throw
>> >> %MakeTypeError('cannot_convert_to_primitive',[]);?}
>> >> -----------------------------------------
>> >> }
>> >>
>> >> [8]: ToString [native runtime.js:334] (this=0x1fa4ac285561 <JS Global
>> >> Object>#3#,a=0xb357401bcf9 <JS Object>#4#) {
>> >> // expression stack (top to bottom)
>> >> [04] : 0xb357401bcf9 <JS Object>#4#
>> >> [03] : 0x1fa4ac263541 <JS Object>#1#
>> >> [02] : 0x1fa4ac279a39 <JS Function DefaultString (SharedFunctionInfo
>> >> 0x1fa4ac21fa51)>#8#
>> >> [01] : 0x1fa4ac263541 <JS Object>#1#
>> >> [00] : 0x1fa4ac26db11 <JS Function ToString (SharedFunctionInfo
>> >> 0x1fa4ac21f2d9)>#9#
>> >> --------- s o u r c e c o d e ---------
>> >> function ToString(a){?if((typeof(a)==='string'))return
>> >> a;?if((typeof(a)==='number'))return
>> >> %_NumberToString(a);?if((typeof(a)==='boolean'))return
>> >> a?'true':'false';?if((typeof(a)==='undefined'))return'
>> undefined';?return((a===null))?'null':%ToString(%DefaultString(a));?}
>> >> -----------------------------------------
>> >> }
>> >>
>> >> ==== Key ============================================
>> >>
>> >> #0# 0x1fa4ac285451: 0x1fa4ac285451 <JS Object>
>> >> #1# 0x1fa4ac263541: 0x1fa4ac263541 <JS Object>
>> >> #2# 0x3cc891104221: 0x3cc891104221 <Map(elements=0)>
>> >> #3# 0x1fa4ac285561: 0x1fa4ac285561 <JS Global Object>
>> >> #4# 0xb357401bcf9: 0xb357401bcf9 <JS Object>
>> >> #5# 0x1fa4ac282c49: 0x1fa4ac282c49 <JS Function DefaultNumber
>> >> (SharedFunctionInfo 0x1fa4ac21f9c1)>
>> >> #6# 0x1fa4ac273191: 0x1fa4ac273191 <JS Function ToNumber
>> >> (SharedFunctionInfo 0x1fa4ac21f161)>
>> >> #7# 0x1fa4ac287621: 0x1fa4ac287621 <JS Function toString
>> >> (SharedFunctionInfo 0x1fa4ac287591)>
>> >> #8# 0x1fa4ac279a39: 0x1fa4ac279a39 <JS Function DefaultString
>> >> (SharedFunctionInfo 0x1fa4ac21fa51)>
>> >> #9# 0x1fa4ac26db11: 0x1fa4ac26db11 <JS Function ToString
>> >> (SharedFunctionInfo 0x1fa4ac21f2d9)>
>> >> =====================
>> >>
>> >> Questions: Is this a bug or am I doing something wrong? Anyway I can
>> >> work around it?
>> >>
>> >> Thanks,
>> >> Jane
>> >
>> > --
>> > --
>> > 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/d/optout.
>>
> --
> --
> 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/d/optout.
>
--
--
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/d/optout.