invocation.monkey2 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. Namespace mojo3d.jsonifier
  2. Class Invocation
  3. Method New( scope:TypeInfo,decl:DeclInfo,inst:Variant,args:Variant[] )
  4. _scope=scope
  5. _decl=decl
  6. _inst=inst
  7. _args=args
  8. End
  9. Method New( scope:TypeInfo,decl:String,inst:Variant,args:Variant[] )
  10. Init( scope,decl,inst,args )
  11. End
  12. Method New( name:String,inst:Variant,args:Variant[] )
  13. Local i:=name.FindLast( "." )
  14. Init( TypeInfo.GetType( name.Slice( 0,i ) ),name.Slice( i+1 ),inst,args )
  15. End
  16. Property Scope:TypeInfo()
  17. Return _scope
  18. End
  19. Property Decl:DeclInfo()
  20. Return _decl
  21. End
  22. Property Inst:Variant()
  23. Return _inst
  24. End
  25. Property Args:Variant[]()
  26. Return _args
  27. End
  28. Method Execute:Variant()
  29. Return _decl.Invoke( _inst,_args )
  30. End
  31. Private
  32. Field _scope:TypeInfo
  33. Field _decl:DeclInfo
  34. Field _inst:Variant
  35. Field _args:Variant[]
  36. Function FindDecl:DeclInfo( scope:TypeInfo,name:String,args:Variant[] )
  37. For Local decl:=Eachin scope.GetDecls( name )
  38. Local ftype:=decl.Type
  39. If ftype.ParamTypes.Length<>args.Length Continue
  40. Local match:=True
  41. For Local i:=0 Until args.Length
  42. If args[i]
  43. If args[i].Type.ExtendsType( ftype.ParamTypes[i] ) Continue
  44. If args[i].Type.Kind="Class" And ftype.ParamTypes[i].Kind="Class"
  45. Local obj:=Cast<Object>( args[i] )
  46. If Not obj Or obj.DynamicType.ExtendsType( ftype.ParamTypes[i] ) Continue
  47. Endif
  48. Else
  49. If ftype.ParamTypes[i].Kind="Class" Continue
  50. Endif
  51. match=False
  52. Exit
  53. Next
  54. If match Return decl
  55. Next
  56. Return Null
  57. End
  58. Method Init( scope:TypeInfo,name:String,inst:Variant,args:Variant[] )
  59. _scope=scope
  60. _inst=inst
  61. _args=args
  62. _decl=FindDecl( _scope,name,args )
  63. If Not _decl
  64. For Local type:=Eachin GetTypeExtensions( _scope )
  65. _decl=FindDecl( type,name,args )
  66. If Not _decl Continue
  67. _scope=type
  68. Exit
  69. Next
  70. Assert( _decl )
  71. Endif
  72. End
  73. End