Exemple #1
0
        static internal IntPtr ReturnResultToJavascript(object instance)
        {
            if (null == instance)
            {
                return(IntPtr.Zero);
            }

            Type   type   = instance.GetType();
            IntPtr handle = Entry.Object.Create(instance);

            if (!type.IsValueType && Entry.IsCustomSuperClassInstance(instance))
            {
                ICustomSuperClass super = instance as ICustomSuperClass;
                super.Initialize();
            }
            return(handle);
        }
            static internal IntPtr Create(object instance)
            {
                if (null == instance)
                {
                    return(IntPtr.Zero);
                }

                Type type = instance.GetType();

                if (type.IsValueType)
                {
                    if (type.IsPrimitive)
                    {
                        if (instance is bool)
                        {
                            return(Create((bool)instance));
                        }
                        if (instance is sbyte)
                        {
                            return(Create((sbyte)instance));
                        }
                        if (instance is byte)
                        {
                            return(Create((byte)instance));
                        }
                        if (instance is short)
                        {
                            return(Create((short)instance));
                        }
                        if (instance is ushort)
                        {
                            return(Create((ushort)instance));
                        }
                        if (instance is int)
                        {
                            return(Create((int)instance));
                        }
                        if (instance is uint)
                        {
                            return(Create((uint)instance));
                        }
                        if (instance is long)
                        {
                            return(Create((long)instance));
                        }
                        if (instance is ulong)
                        {
                            return(Create((ulong)instance));
                        }
                        if (instance is float)
                        {
                            return(Create((float)instance));
                        }
                        if (instance is double)
                        {
                            return(Create((double)instance));
                        }
                    }
                    int    size   = Marshal.SizeOf(type);
                    IntPtr handle = Entry.Object.Create(type.GetSafeFullName(), size);
                    Marshal.StructureToPtr(instance, Entry.Object.GetField(handle), true);
                    return(handle);
                }
                else
                {
                    if (instance is string)
                    {
                        return(Create(instance as string));
                    }
                    else if (instance is Array)
                    {
                        Array array = instance as Array;
                        if (0 == array.Length)
                        {
                            return(General_Typescript_Object_CreateArrayWithLength(sInstance.Context, 0));
                        }

                        IntPtr[] elements = new IntPtr[array.Length];
                        for (int i = 0; i < array.Length; ++i)
                        {
                            elements[i] = Create(array.GetValue(i));
                        }
                        return(General_Typescript_Object_CreateArrayWithElements(sInstance.Context, elements, array.Length));
                    }

                    IntPtr handle = IntPtr.Zero;
                    if (!sCSharpToNativeHandles.TryGetValue(instance, out handle))
                    {
                        if (instance is ICustomSuperClass)
                        {
                            ICustomSuperClass super = instance as ICustomSuperClass;
                            handle = Entry.Object.CreateInstance(super.TypeFullName);
                            super.SetJsHandle(handle);
                        }
                        else
                        {
                            handle = Entry.Object.CreateInstance(type.GetSafeFullName());
                        }
                        sCSharpToNativeHandles.Add(instance, handle);
                        sNativeToCSharpHandles.Add(handle, instance);
                    }
                    return(handle);
                }
            }