public static bool duk_get_var(IntPtr ctx, int idx, out object o) { var jstype = DuktapeDLL.duk_get_type(ctx, idx); switch (jstype) { case duk_type_t.DUK_TYPE_BOOLEAN: /* ECMAScript boolean: 0 or 1 */ { o = DuktapeDLL.duk_get_boolean(ctx, idx); return(true); } case duk_type_t.DUK_TYPE_NUMBER: /* ECMAScript number: double */ { o = DuktapeDLL.duk_get_number(ctx, idx); return(true); } case duk_type_t.DUK_TYPE_STRING: /* ECMAScript string: CESU-8 / extended UTF-8 encoded */ { o = DuktapeDLL.duk_get_string(ctx, idx); return(true); } case duk_type_t.DUK_TYPE_OBJECT: /* ECMAScript object: includes objects, arrays, functions, threads */ { return(duk_get_cached_object(ctx, idx, out o)); } case duk_type_t.DUK_TYPE_BUFFER: /* fixed or dynamic, garbage collected byte buffer */ { uint length; var pointer = DuktapeDLL.duk_unity_get_buffer_data(ctx, idx, out length); var buffer = new byte[length]; o = buffer; Marshal.Copy(pointer, buffer, 0, (int)length); return(true); } case duk_type_t.DUK_TYPE_POINTER: /* raw void pointer */ case duk_type_t.DUK_TYPE_LIGHTFUNC: /* lightweight function pointer */ throw new NotImplementedException(); case duk_type_t.DUK_TYPE_NONE: /* no value, e.g. invalid index */ o = null; return(false); case duk_type_t.DUK_TYPE_UNDEFINED: /* ECMAScript undefined */ case duk_type_t.DUK_TYPE_NULL: /* ECMAScript null */ o = null; return(true); } o = null; return(false); }
public static bool duk_get_object(IntPtr ctx, int idx, out object o) { var jstype = DuktapeDLL.duk_get_type(ctx, idx); if (jstype == duk_type_t.DUK_TYPE_OBJECT) { return(duk_get_cached_object(ctx, idx, out o)); } // Debug.LogFormat("duk_get_object({0})", jstype); // case duk_type_t.DUK_TYPE_STRING: // o = DuktapeDLL.duk_get_string(ctx, idx); // return true; // default: break; // } // 其他类型不存在对象映射 o = null; return(false); }
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); 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)); }
// not value type (except string/array) public static bool duk_get_classvalue <T>(IntPtr ctx, int idx, out T o) where T : class { object o_t; if (duk_get_cached_object(ctx, idx, out o_t)) { o = o_t as T; if (o_t != null && o == null) { throw new InvalidCastException(string.Format("{0} type mismatch {1}", o_t.GetType(), typeof(T))); // return false; } return(true); } var jsType = DuktapeDLL.duk_get_type(ctx, idx); throw new InvalidCastException(string.Format("{0} type mismatch {1}", jsType, typeof(T))); }
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); } }
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); } }