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.

 long long getType( const v8::FunctionCallbackInfo<v8::Value>& args )
{
    if( args[0]->IsUndefined() )         return( 1 << 0 );
    if( args[0]->IsNull() )              return( 1 << 1 );
    if( args[0]->IsTrue() )              return( 1 << 2 );
    if( args[0]->IsFalse() )             return( 1 << 3 );
    if( args[0]->IsName() )              return( 1 << 4 );
    if( args[0]->IsString() )            return( 1 << 5 );
    if( args[0]->IsSymbol() )            return( 1 << 6 );
    if( args[0]->IsFunction() )          return( 1 << 7 );
    if( args[0]->IsArray() )             return( 1 << 8 );
    if( args[0]->IsObject() )            return( 1 << 9 );
    if( args[0]->IsBoolean() )           return( 1 << 10 );
    if( args[0]->IsNumber() )            return( 1 << 11 );
    if( args[0]->IsExternal() )          return( 1 << 12 );
    if( args[0]->IsInt32() )             return( 1 << 13 );
    if( args[0]->IsUint32() )            return( 1 << 14 );
    if( args[0]->IsDate() )              return( 1 << 15 );
    if( args[0]->IsArgumentsObject() )   return( 1 << 16 );
    if( args[0]->IsBooleanObject() )     return( 1 << 17 );
    if( args[0]->IsNumberObject() )      return( 1 << 18 );
    if( args[0]->IsStringObject() )      return( 1 << 19 );
    if( args[0]->IsSymbolObject() )      return( 1 << 20 );
    if( args[0]->IsNativeError() )       return( 1 << 21 );
    if( args[0]->IsRegExp() )            return( 1 << 22 );
    if( args[0]->IsGeneratorFunction() ) return( 1 << 23 );
    if( args[0]->IsGeneratorObject() )   return( 1 << 24 );
    if( args[0]->IsPromise() )           return( 1 << 25 );
    if( args[0]->IsMap() )               return( 1 << 26 );
    if( args[0]->IsSet() )               return( 1 << 27 );
    if( args[0]->IsMapIterator() )       return( 1 << 28 );
    if( args[0]->IsSetIterator() )       return( 1 << 29 );
    if( args[0]->IsWeakMap() )           return( 1 << 30 );
    if( args[0]->IsWeakSet() )           return( 1 << 31 );
    if( args[0]->IsArrayBuffer() )       return( 1 << 32 );
    if( args[0]->IsArrayBufferView() )   return( 1 << 33 );
    if( args[0]->IsTypedArray() )        return( 1 << 34 );
    if( args[0]->IsUint8Array() )        return( 1 << 35 );
    if( args[0]->IsUint8ClampedArray() ) return( 1 << 36 );
    if( args[0]->IsInt8Array() )         return( 1 << 37 );
    if( args[0]->IsUint16Array() )       return( 1 << 38 );
    if( args[0]->IsInt16Array() )        return( 1 << 39 );
    if( args[0]->IsUint32Array() )       return( 1 << 40 );
    if( args[0]->IsInt32Array() )        return( 1 << 41 );
    if( args[0]->IsFloat32Array() )      return( 1 << 42 );
    if( args[0]->IsFloat64Array() )      return( 1 << 43 );
    if( args[0]->IsFloat32x4() )         return( 1 << 44 );
    if( args[0]->IsDataView() )          return( 1 << 45 );
    if( args[0]->IsSharedArrayBuffer() ) return( 1 << 46 );
   return( 0 );
}


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

Your logic is not right.

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
}

You could also try

void foo(const v8::FunctionCallbackInfo<v8::Value>& args)
{
    if (args.Length() != 1) throw "expected a single numeric argument";
    if (!(args[0]->IsNumber()) && !(args[0]->IsInt32()) &&
!(args[0]->IsUint32())) throw "expected a single numeric argument";
    // now do something with the numeric value we got
}

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