コード例 #1
0
        // Return true if this JSValue is strictly equal to JSValue after they are converted to the same type.
        //
        // Null is not converted to any other type before comparison.
        // Object and Array types are converted first to a String type using AsString() before comparing
        // with other types, and then we apply the same rules as for the String type.
        // String is converted to Double before comparing with Boolean, Int64, or Double.
        // Boolean is converted to 1.0 and +0.0 when comparing with String, Int64, or Double.
        // Int64 is converted to Double before comparing with other types.
        //
        // The behavior is similar to JavaScript == operator except for Object and Array for which
        // this functions does a deep structured comparison using JSEquals instead
        // of pointer equality.
        public bool JSEquals(JSValue other)
        {
            if (Type == other.Type)
            {
                switch (Type)
                {
                case JSValueType.Object:
                    return(JSValueObject.JSEquals(ObjectValue, other.ObjectValue));

                case JSValueType.Array:
                    return(JSValueArray.JSEquals(ArrayValue, other.ArrayValue));

                default:
                    return(Equals(other));
                }
            }
            else if (Type == JSValueType.Null || other.Type == JSValueType.Null)
            {
                return(false);
            }

            // If one of the types Boolean, Int64, or Double, then compare as Numbers,
            // otherwise compare as strings.
            JSValueType greatestType = Type > other.Type ? Type : other.Type;

            if (greatestType >= JSValueType.Boolean)
            {
                return(AsJSNumber() == other.AsJSNumber());
            }
            else
            {
                return(AsJSString() == other.AsJSString());
            }
        }
コード例 #2
0
 /// <summary>
 /// Calls the native V8 proxy library to create an error string for use within the V8 JavaScript environment.
 /// <para>Note: The error flag exists in the associated proxy object only.  If the handle is passed along to another operation, only the string message will get passed.</para>
 /// </summary>
 public InternalHandle CreateError(string message, JSValueType errorType)
 {
     if (errorType >= 0)
     {
         throw new InvalidOperationException("Invalid error type.");
     }
     return(V8NetProxy.CreateError(_NativeV8EngineProxy, message, errorType));
 }
コード例 #3
0
        public Array ToArray(Type arrayType)
        {
            JSNumberType subType     = 0;
            JSValueType  type        = JSValueType.Number;
            Type         elementType = arrayType.GetElementType();

            if (typeof(sbyte) == elementType)
            {
                subType = JSNumberType.SByte;
            }
            else if (typeof(byte) == elementType)
            {
                subType = JSNumberType.Byte;
            }
            else if (typeof(short) == elementType)
            {
                subType = JSNumberType.Int16;
            }
            else if (typeof(ushort) == elementType)
            {
                subType = JSNumberType.UInt16;
            }
            else if (typeof(int) == elementType)
            {
                subType = JSNumberType.Int32;
            }
            else if (typeof(uint) == elementType)
            {
                subType = JSNumberType.UInt32;
            }
            else if (typeof(long) == elementType)
            {
                subType = JSNumberType.Int64;
            }
            else if (typeof(ulong) == elementType)
            {
                subType = JSNumberType.UInt64;
            }
            else if (typeof(float) == elementType)
            {
                subType = JSNumberType.Single;
            }
            else if (typeof(double) == elementType)
            {
                subType = JSNumberType.Double;
            }
            else if (typeof(bool) == elementType)
            {
                type = JSValueType.Boolean;
            }
            else if (typeof(string) == elementType)
            {
                type = JSValueType.String;
            }
            return(Entry.Parameters.ToArray(this.Handle, this.Index, type, subType));
        }
コード例 #4
0
        public virtual void Assign(JSValue value)
        {
#if DEBUG
            if (_valueType == JSValueType.Property)
            {
                throw new InvalidOperationException("Try to assign to property.");
            }
#endif
            if ((_attributes & (JSValueAttributesInternal.ReadOnly | JSValueAttributesInternal.SystemObject)) != 0)
            {
                return;
            }
            this._valueType = value._valueType | JSValueType.Undefined;
            this._iValue    = value._iValue;
            this._dValue    = value._dValue;
            this._oValue    = value._oValue;
        }
コード例 #5
0
        public bool IsType(Type type)
        {
            JSValueType jsType = Entry.Parameters.GetJsType(this.Handle, this.Index);

            if (typeof(string) == type)
            {
                return(JSValueType.String == jsType);
            }
            if (typeof(bool) == type)
            {
                return(JSValueType.Boolean == jsType);
            }
            if ((type.IsValueType && type.IsPrimitive) || type.IsEnum)
            {
                return(JSValueType.Number == jsType);
            }
            if (typeof(Type) == type)
            {
                return(JSValueType.Constructor == jsType || JSValueType.String == jsType || JSValueType.Object == jsType);
            }
            if (type.IsArray && JSValueType.Array == jsType)
            {
                return(true);
            }
            if (type.IsSubclassOf(typeof(Delegate)) && (JSValueType.Function == jsType || JSValueType.Constructor == jsType))
            {
                return(true);
            }
            if (JSValueType.Object == jsType || JSValueType.Constructor == jsType)
            {
                Type realType = Entry.Parameters.GetType(this.Handle, this.Index);
                if (null != realType)
                {
                    return(realType == type || realType.IsSubclassOf(type));
                }
            }
            return(false);
        }
コード例 #6
0
 public static extern HandleProxy *CreateError(NativeV8EngineProxy *engine, string message, JSValueType errorType);
コード例 #7
0
ファイル: JSValue.cs プロジェクト: maximvegorov/YaJS.NET
 protected JSValue(JSValueType type)
 {
     Type = type;
 }
コード例 #8
0
 public static bool Is(this JSValue self, JSValueType type)
 {
     return(self != null && self._valueType == type);
 }
コード例 #9
0
            static internal Array ToArray(IntPtr handle, int index, JSValueType type, JSNumberType subType)
            {
                if (JSValueType.String == type)
                {
                    int      count = General_Typescript_Parameter_GetArrayLength(sInstance.Context, handle, index);
                    string[] array = new string[count];
                    foreach (int i in Enumerable.Range(0, count))
                    {
#if UNITY_STANDALONE_WIN || UNITY_ANDROID
                        array[i] = Marshal.PtrToStringAnsi(General_Typescript_Parameter_GetArrayString(sInstance.Context, handle, index, i));
#else
                        array[i] = General_Typescript_Parameter_GetArrayString(sInstance.Context, handle, index, i);
#endif
                    }
                    return(array);
                }
                else
                {
                    IntPtr result = General_Typescript_Parameter_ToArray(sInstance.Context, handle, index, (int)type, (int)subType);
                    int    count  = Marshal.ReadInt32(result);
                    result += sizeof(int);
                    if (JSValueType.Boolean == type)
                    {
                        bool[] array = new bool[count];
                        foreach (int i in Enumerable.Range(0, count))
                        {
                            array[i] = 0 != Marshal.ReadByte(result);
                            result  += sizeof(byte);
                        }
                        return(array);
                    }
                    else if (JSValueType.Number == type)
                    {
                        Array array = null;
                        switch (subType)
                        {
                        case JSNumberType.SByte:
                            sbyte[] arraySByte = new sbyte[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arraySByte[i] = (sbyte)Marshal.ReadByte(result);
                                result       += sizeof(byte);
                            }
                            array = arraySByte;
                            break;

                        case JSNumberType.Byte:
                            byte[] arrayByte = new byte[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayByte[i] = Marshal.ReadByte(result);
                                result      += sizeof(byte);
                            }
                            array = arrayByte;
                            break;

                        case JSNumberType.Int16:
                            short[] arrayInt16 = new short[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayInt16[i] = (short)Marshal.ReadInt16(result);
                                result       += sizeof(short);
                            }
                            array = arrayInt16;
                            break;

                        case JSNumberType.UInt16:
                            ushort[] arrayUInt16 = new ushort[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayUInt16[i] = (ushort)Marshal.ReadInt16(result);
                                result        += sizeof(ushort);
                            }
                            array = arrayUInt16;
                            break;

                        case JSNumberType.Int32:
                            int[] arrayInt32 = new int[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayInt32[i] = (int)Marshal.ReadInt32(result);
                                result       += sizeof(int);
                            }
                            array = arrayInt32;
                            break;

                        case JSNumberType.UInt32:
                            uint[] arrayUInt32 = new uint[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayUInt32[i] = (uint)Marshal.ReadInt32(result);
                                result        += sizeof(uint);
                            }
                            array = arrayUInt32;
                            break;

                        case JSNumberType.Int64:
                            long[] arrayInt64 = new long[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayInt64[i] = (long)Marshal.ReadInt64(result);
                                result       += sizeof(long);
                            }
                            array = arrayInt64;
                            break;

                        case JSNumberType.UInt64:
                            ulong[] arrayUInt64 = new ulong[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayUInt64[i] = (ulong)Marshal.ReadInt64(result);
                                result        += sizeof(ulong);
                            }
                            array = arrayUInt64;
                            break;

                        case JSNumberType.Single:
                            float[] arraySingle = new float[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arraySingle[i] = (float)Marshal.PtrToStructure <System.Single>(result);
                                result        += sizeof(float);
                            }
                            array = arraySingle;
                            break;

                        case JSNumberType.Double:
                            double[] arrayDouble = new double[count];
                            foreach (int i in Enumerable.Range(0, count))
                            {
                                arrayDouble[i] = (double)Marshal.PtrToStructure <System.Double>(result);
                                result        += sizeof(double);
                            }
                            array = arrayDouble;
                            break;
                        }
                        return(array);
                    }
                }
                return(null);
            }
コード例 #10
0
ファイル: ProxyLoader.cs プロジェクト: killf/v8dotnet
 public static extern HandleProxy* CreateError(NativeV8EngineProxy* engine, string message, JSValueType errorType);
コード例 #11
0
 protected JSNumberValue(JSValueType type)
     : base(type)
 {
 }