示例#1
0
 public static bool duk_get_primitive_array(IntPtr ctx, int idx, out byte[] o)
 {
     if (DuktapeDLL.duk_is_array(ctx, idx))
     {
         var length = DuktapeDLL.duk_unity_get_length(ctx, idx);
         var nidx   = DuktapeDLL.duk_normalize_index(ctx, idx);
         o = new byte[length];
         for (var i = 0U; i < length; i++)
         {
             DuktapeDLL.duk_get_prop_index(ctx, idx, i);
             byte e;
             e    = (byte)DuktapeDLL.duk_get_int(ctx, -1); // duk_get_primitive(ctx, -1, out e);
             o[i] = e;
             DuktapeDLL.duk_pop(ctx);
         }
         return(true);
     }
     if (DuktapeDLL.duk_is_buffer_data(ctx, idx))
     {
         uint length;
         var  pointer = DuktapeDLL.duk_unity_get_buffer_data(ctx, idx, out length);
         o = new byte[length];
         Marshal.Copy(pointer, o, 0, (int)length);
         return(true);
     }
     duk_get_classvalue <byte[]>(ctx, idx, out o);
     return(true);
 }
 public static bool duk_get_type_array(IntPtr ctx, int idx, out Type[] o)
 {
     if (DuktapeDLL.duk_is_array(ctx, idx))
     {
         var length = DuktapeDLL.duk_unity_get_length(ctx, idx);
         var nidx   = DuktapeDLL.duk_normalize_index(ctx, idx);
         o = new Type[length];
         for (var i = 0U; i < length; i++)
         {
             DuktapeDLL.duk_get_prop_index(ctx, idx, i);
             Type e;
             duk_get_type(ctx, -1, out e);
             o[i] = e;
             DuktapeDLL.duk_pop(ctx);
         }
         return(true);
     }
     duk_get_classvalue <Type[]>(ctx, idx, out o);
     return(true);
 }
示例#3
0
 public static bool duk_get_primitive_array(IntPtr ctx, int idx, out sbyte[] o)
 {
     if (DuktapeDLL.duk_is_array(ctx, idx))
     {
         var length = DuktapeDLL.duk_unity_get_length(ctx, idx);
         var nidx   = DuktapeDLL.duk_normalize_index(ctx, idx);
         o = new sbyte[length];
         for (var i = 0U; i < length; i++)
         {
             DuktapeDLL.duk_get_prop_index(ctx, idx, i);
             sbyte e;
             e    = (sbyte)DuktapeDLL.duk_get_int(ctx, -1); // duk_get_primitive(ctx, -1, out e);
             o[i] = e;
             DuktapeDLL.duk_pop(ctx);
         }
         return(true);
     }
     duk_get_classvalue <sbyte[]>(ctx, idx, out o);
     return(true);
 }
示例#4
0
        // public static bool duk_get_object(IntPtr ctx, int idx, out object o)
        // {
        //     if (DuktapeDLL.duk_is_null_or_undefined(ctx, idx)) // or check for object?
        //     {
        //         o = null;
        //         return true;
        //     }
        //     var jstype = DuktapeDLL.duk_get_type(ctx, idx);
        //     Debug.LogFormat("duk_get_object({0})", jstype);
        //     switch (jstype)
        //     {
        //         case duk_type_t.DUK_TYPE_STRING:
        //             o = DuktapeDLL.duk_get_string(ctx, idx);
        //             return true;
        //         default: break;
        //     }
        //     return duk_get_cached_object(ctx, idx, out o);
        // }

        public static bool duk_get_classvalue_array <T>(IntPtr ctx, int idx, out T[] o)
            where T : class
        {
            if (DuktapeDLL.duk_is_array(ctx, idx))
            {
                var length = DuktapeDLL.duk_unity_get_length(ctx, idx);
                o   = new T[length];
                idx = DuktapeDLL.duk_normalize_index(ctx, idx);
                for (var i = 0U; i < length; i++)
                {
                    DuktapeDLL.duk_get_prop_index(ctx, idx, i);
                    T e;
                    if (duk_get_classvalue(ctx, -1, out e))
                    {
                        o[i] = e;
                    }
                }
                return(true);
            }
            o = null;
            return(false);
        }
示例#5
0
        public static bool duk_get_classvalue(IntPtr ctx, int idx, out DuktapeArray o)
        {
            object obj;

            if (duk_get_cached_object(ctx, idx, out obj))
            {
                if (obj is DuktapeArray)
                {
                    o = (DuktapeArray)obj;
                    return(true);
                }
            }
            if (DuktapeDLL.duk_is_array(ctx, idx))
            {
                DuktapeDLL.duk_dup(ctx, idx);
                var refid = DuktapeDLL.duk_unity_ref(ctx);
                o = new DuktapeArray(ctx, refid);
                return(true);
            }
            o = null;
            return(false);
        }
示例#6
0
        protected static bool duk_match_type(IntPtr ctx, int idx, Type type)
        {
            if (type == null)
            {
                return(true);
            }
            if (type == typeof(object))
            {
                return(true);
            }
            if (type == typeof(Type))
            {
                Type otype;
                return(duk_get_type(ctx, idx, out otype)); // 只要求匹配 Type 本身, 不比较具体 Type
                // return otype == type;
            }
            var jstype = DuktapeDLL.duk_get_type(ctx, idx);

            switch (jstype)
            {
            // case duk_type_t.DUK_TYPE_NONE:
            case duk_type_t.DUK_TYPE_OBJECT:     // objects, arrays, functions, threads
                if (DuktapeDLL.duk_is_array(ctx, idx))
                {
                    if (!type.IsArray && !_assignableFromArray.Contains(type))
                    {
                        return(false);
                    }
                }
                else if (DuktapeDLL.duk_is_function(ctx, idx))
                {
                    //TODO: 完善处理 delegate
                    return(type == typeof(DuktapeFunction) || type.BaseType == typeof(MulticastDelegate));
                }
                else if (DuktapeDLL.duk_is_thread(ctx, idx))
                {
                    return(false);
                }
                int refid;
                //TODO: 根据记录在jsobject 上的 分类标记 做进一步分支 (类型, 实例, 或特定优化类型)
                if (duk_get_native_refid(ctx, idx, out refid))
                {
                    var cache = DuktapeVM.GetObjectCache(ctx);
                    return(cache.MatchObjectType(refid, type));
                }
                return(true);

            case duk_type_t.DUK_TYPE_NUMBER:
                return(type.IsPrimitive || type.IsEnum);

            case duk_type_t.DUK_TYPE_STRING:
                return(type == typeof(string));

            case duk_type_t.DUK_TYPE_UNDEFINED:
            case duk_type_t.DUK_TYPE_NULL:
                return(!type.IsValueType && !type.IsPrimitive);

            case duk_type_t.DUK_TYPE_BOOLEAN:
                return(type == typeof(bool));

            case duk_type_t.DUK_TYPE_BUFFER:
                return(type == typeof(byte[]) || type == typeof(sbyte[]) /* || type == typeof(DuktapeBuffer) */);

            case duk_type_t.DUK_TYPE_POINTER:
            // return type == typeof(DuktapePointer);
            case duk_type_t.DUK_TYPE_LIGHTFUNC:
            default:
                return(false);
            }
        }
示例#7
0
        protected static bool duk_match_type(IntPtr ctx, int idx, Type type)
        {
            if (type == null)
            {
                return(true);
            }
            if (type == typeof(object))
            {
                return(true);
            }
            if (type == typeof(Type))
            {
                Type otype;
                return(duk_get_type(ctx, idx, out otype)); // 只要求匹配 Type 本身, 不比较具体 Type
                // return otype == type;
            }
            var jstype = DuktapeDLL.duk_get_type(ctx, idx);

            switch (jstype)
            {
            // case duk_type_t.DUK_TYPE_NONE:
            case duk_type_t.DUK_TYPE_OBJECT:     // objects, arrays, functions, threads
                if (DuktapeDLL.duk_is_array(ctx, idx))
                {
                    if (!type.IsArray && !_assignableFromArray.Contains(type))
                    {
                        return(false);
                    }
                }
                else if (DuktapeDLL.duk_is_function(ctx, idx))
                {
                    //TODO: 完善处理 delegate
                    return(type == typeof(DuktapeFunction) || type.BaseType == typeof(MulticastDelegate));
                }
                else if (DuktapeDLL.duk_is_thread(ctx, idx))
                {
                    return(false);
                }

                int refid;
                if (duk_get_native_refid(ctx, idx, out refid))
                {
                    var cache = DuktapeVM.GetObjectCache(ctx);
                    return(cache.MatchObjectType(refid, type));
                }

                int typeid;
                if (DuktapeDLL.duk_unity_get_type_refid(ctx, idx, out typeid))
                {
                    var vm    = DuktapeVM.GetVM(ctx);
                    var eType = vm.GetExportedType(typeid);
                    if (eType != null)
                    {
                        // Debug.LogFormat("match type? {0} {1} {2}", eType, type, typeid);
                        return(eType == type);
                    }
                    // Debug.LogFormat("match type {0} with typeid {1}", type, typeid);
                }
                return(type.IsSubclassOf(typeof(DuktapeValue)));

            case duk_type_t.DUK_TYPE_NUMBER:
                return(type.IsPrimitive || type.IsEnum);

            case duk_type_t.DUK_TYPE_STRING:
                return(type == typeof(string));

            case duk_type_t.DUK_TYPE_UNDEFINED:
            case duk_type_t.DUK_TYPE_NULL:
                return(!type.IsValueType && !type.IsPrimitive);

            case duk_type_t.DUK_TYPE_BOOLEAN:
                return(type == typeof(bool));

            case duk_type_t.DUK_TYPE_BUFFER:
                return(type == typeof(byte[]) || type == typeof(sbyte[]) /* || type == typeof(DuktapeBuffer) */);

            case duk_type_t.DUK_TYPE_POINTER:
            // return type == typeof(DuktapePointer);
            case duk_type_t.DUK_TYPE_LIGHTFUNC:
            default:
                return(false);
            }
        }