@@ -366,7 +366,10 @@ public static NewReference __subclasscheck__(BorrowedReference tp, BorrowedRefer
366366 /// </summary>
367367 public static NewReference tp_iter ( BorrowedReference tp )
368368 {
369- var type = CheckAndGetEnumType ( tp ) ;
369+ if ( ! TryGetEnumType ( tp , out var type ) )
370+ {
371+ return default ;
372+ }
370373 var values = Enum . GetValues ( type ) ;
371374 return new Iterator ( values . GetEnumerator ( ) , type ) . Alloc ( ) ;
372375 }
@@ -376,7 +379,10 @@ public static NewReference tp_iter(BorrowedReference tp)
376379 /// </summary>
377380 public static int mp_length ( BorrowedReference tp )
378381 {
379- var type = CheckAndGetEnumType ( tp ) ;
382+ if ( ! TryGetEnumType ( tp , out var type ) )
383+ {
384+ return - 1 ;
385+ }
380386 return Enum . GetValues ( type ) . Length ;
381387 }
382388
@@ -385,38 +391,47 @@ public static int mp_length(BorrowedReference tp)
385391 /// </summary>
386392 public static int sq_contains ( BorrowedReference tp , BorrowedReference v )
387393 {
388- var type = CheckAndGetEnumType ( tp ) ;
394+ if ( ! TryGetEnumType ( tp , out var type ) )
395+ {
396+ return - 1 ;
397+ }
389398
390399 if ( ! Converter . ToManaged ( v , type , out var enumValue , false ) &&
391400 ! Converter . ToManaged ( v , typeof ( int ) , out enumValue , false ) &&
392401 ! Converter . ToManaged ( v , typeof ( string ) , out enumValue , false ) )
393402 {
394403 Exceptions . SetError ( Exceptions . TypeError ,
395404 $ "invalid parameter type for sq_contains: should be { Converter . GetTypeByAlias ( v ) } , found { type } ") ;
405+ return - 1 ;
396406 }
407+
397408 return Enum . IsDefined ( type , enumValue ) ? 1 : 0 ;
398409 }
399410
400- private static Type CheckAndGetEnumType ( BorrowedReference tp )
411+ private static bool TryGetEnumType ( BorrowedReference tp , out Type type )
401412 {
413+ type = null ;
402414 var cb = GetManagedObject ( tp ) as ClassBase ;
403415 if ( cb == null )
404416 {
405417 Exceptions . SetError ( Exceptions . TypeError , "invalid object" ) ;
418+ return false ;
406419 }
407420
408421 if ( ! cb . type . Valid )
409422 {
410423 Exceptions . SetError ( Exceptions . TypeError , "invalid type" ) ;
424+ return false ;
411425 }
412426
413- var type = cb . type . Value ;
414- if ( ! type . IsEnum )
427+ if ( ! cb . type . Value . IsEnum )
415428 {
416- Exceptions . SetError ( Exceptions . TypeError , "uniterable object" ) ;
429+ Exceptions . SetError ( Exceptions . TypeError , "uniterable type" ) ;
430+ return false ;
417431 }
418432
419- return type ;
433+ type = cb . type . Value ;
434+ return true ;
420435 }
421436 }
422437}
0 commit comments