On Mon, Nov 16, 2015 at 8:43 PM, Louis Santillan <[email protected]> wrote:
> On Mon, Nov 16, 2015 at 5:47 PM, Arthur O'Dwyer <[email protected]>
wrote:
>> On Thursday, April 14, 2011 at 12:23:46 AM UTC-7, crtmpserver wrote:
> [SNIP]
>> Four years later, I have the same question.
>> It seems inconceivable that V8 wouldn't know the type of the value
>> internally; I'm just trying to get at that type through V8's C++ API.
>>
>> The two questions behind my question, in case anyone can help with these,
>> are:
>>
>> (1) I'm trying to stringize an arbitrary Javascript object — if it's
>> JSONable I'd like to JSON.stringify it, but if that throws, I'd at least
>> like to be able to say what type it was. For example, "unserializable
object
>> of type RegExp" or "unserializable object of type Bicycle" or whatever.
>
> What's the question?  Have to use the Value->IsXXXX() methods to
> detect the type.  No way around it.

There are at least two subquestions here, and of course any question can
"unpack" into as many little questions as you want. One subquestion is, how
do I use V8 to get the equivalent of "class name" or "constructor name",
i.e., what in Javascript might be spelled (theValue.__proto__.
constructor.name)?  I might end up just writing a bit of Javascript,
compiling it with V8, and passing my V8 object to it; but it would be nice
if there were a way to do this through the C++ API.

Another subquestion is, you wrote a 50-line function "getType" as part of
your reply; where can I find something like this function already written
and maintained for me?  Obviously I don't want to write my own 50-line
function just to extract the type from a v8::Value; I want to call an API
method, something like theValue->GetType().


>> (2) I'm trying to expose a C++ function into Javascript. My callback is
>> defined as
>>
>> void foo(const v8::FunctionCallbackInfo<v8::Value>& args)
>> {
>>     if (args.Length() != 1) throw "expected a single numeric argument";
>>     if (args[0]->IsNumber()) throw "expected a single numeric argument";
>>     // now do something with the numeric value we got
>> }
>>
>> However, the above doesn't seem to accept values that aren't numeric
>> literals, e.g.
>>
>>     var x = 42;
>>     foo(x);  // throws because x apparently isn't IsNumber??
>>
>> If there were a simple way to ask V8 "okay, so it's not IsNumber... what
is
>> it, then?", then I would have debugged this issue by now.

Actually, just now I used a variation on Louis's "getType" function to
determine that in the above snippet `x` is all of IsNumber, IsInt32, and
IsUint32... and then I tried my code again and it worked as expected! So
there must have been some other bug somewhere, from which I was distracted
by my inability to determine the type of `x`, such that I didn't even
notice when that other bug quietly got fixed.

I'm going to start using the following function for debugging purposes, but
*man* it would be nice if V8 provided something like this out of the box.
This is just Too Much Typing.

uint64_t GetTypeFlags(const v8::Local<v8::Value>& v)
{
    uint64_t result = 0;
    if (v->IsArgumentsObject()   ) result |= 0x0000000000000001;
    if (v->IsArrayBuffer()       ) result |= 0x0000000000000002;
    if (v->IsArrayBufferView()   ) result |= 0x0000000000000004;
    if (v->IsArray()             ) result |= 0x0000000000000008;
    if (v->IsBooleanObject()     ) result |= 0x0000000000000010;
    if (v->IsBoolean()           ) result |= 0x0000000000000020;
    if (v->IsDataView()          ) result |= 0x0000000000000040;
    if (v->IsDate()              ) result |= 0x0000000000000080;
    if (v->IsExternal()          ) result |= 0x0000000000000100;
    if (v->IsFalse()             ) result |= 0x0000000000000200;
    if (v->IsFloat32Array()      ) result |= 0x0000000000000400;
    if (v->IsFloat64Array()      ) result |= 0x0000000000000800;
    if (v->IsFunction()          ) result |= 0x0000000000001000;
    if (v->IsGeneratorFunction() ) result |= 0x0000000000002000;
    if (v->IsGeneratorObject()   ) result |= 0x0000000000004000;
    if (v->IsInt16Array()        ) result |= 0x0000000000008000;
    if (v->IsInt32Array()        ) result |= 0x0000000000010000;
    if (v->IsInt32()             ) result |= 0x0000000000020000;
    if (v->IsInt8Array()         ) result |= 0x0000000000040000;
    if (v->IsMapIterator()       ) result |= 0x0000000000080000;
    if (v->IsMap()               ) result |= 0x0000000000100000;
    if (v->IsName()              ) result |= 0x0000000000200000;
    if (v->IsNativeError()       ) result |= 0x0000000000400000;
    if (v->IsNull()              ) result |= 0x0000000000800000;
    if (v->IsNumberObject()      ) result |= 0x0000000001000000;
    if (v->IsNumber()            ) result |= 0x0000000002000000;
    if (v->IsObject()            ) result |= 0x0000000004000000;
    if (v->IsPromise()           ) result |= 0x0000000008000000;
    if (v->IsRegExp()            ) result |= 0x0000000010000000;
    if (v->IsSetIterator()       ) result |= 0x0000000020000000;
    if (v->IsSet()               ) result |= 0x0000000040000000;
    if (v->IsStringObject()      ) result |= 0x0000000080000000;
    if (v->IsString()            ) result |= 0x0000000100000000;
    if (v->IsSymbolObject()      ) result |= 0x0000000200000000;
    if (v->IsSymbol()            ) result |= 0x0000000400000000;
    if (v->IsTrue()              ) result |= 0x0000000800000000;
    if (v->IsTypedArray()        ) result |= 0x0000001000000000;
    if (v->IsUint16Array()       ) result |= 0x0000002000000000;
    if (v->IsUint32Array()       ) result |= 0x0000004000000000;
    if (v->IsUint32()            ) result |= 0x0000008000000000;
    if (v->IsUint8Array()        ) result |= 0x0000010000000000;
    if (v->IsUint8ClampedArray() ) result |= 0x0000020000000000;
    if (v->IsUndefined()         ) result |= 0x0000040000000000;
    if (v->IsWeakMap()           ) result |= 0x0000080000000000;
    if (v->IsWeakSet()           ) result |= 0x0000100000000000;
    return result;
}

-- 
-- 
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.

Reply via email to