123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833 |
- {
- This file is part of the Pas2JS run time library.
- Copyright (c) 2018 by Mattias Gaertner
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit RTTI;
- {$mode objfpc}
- {$ModeSwitch advancedrecords}
- interface
- uses
- JS, RTLConsts, Types, SysUtils, TypInfo;
- resourcestring
- SErrInvokeInvalidCodeAddr = 'CodeAddress is not a function';
- SErrTypeIsNotEnumerated = 'Type %s is not an enumerated type';
- type
- { TValue }
- TValue = record
- private
- FTypeInfo: TTypeInfo;
- FData: JSValue;
- function GetIsEmpty: boolean;
- function GetTypeKind: TTypeKind;
- public
- class function Empty: TValue; static;
- generic class function From<T>(const Value: T): TValue; static;
- class function FromArray(TypeInfo: TTypeInfo; const Values: specialize TArray<TValue>): TValue; static;
- class function FromJSValue(v: JSValue): TValue; static;
- class function FromOrdinal(ATypeInfo: TTypeInfo; AValue: JSValue): TValue; static;
- class procedure Make(ABuffer: JSValue; ATypeInfo: PTypeInfo; var Result: TValue); overload; static;
- generic class procedure Make<T>(const Value: T; var Result: TValue); overload; static;
- function AsBoolean: boolean;
- function AsClass: TClass;
- //ToDo: function AsCurrency: Currency;
- function AsExtended: Extended;
- function AsInteger: Integer;
- function AsInterface: IInterface;
- function AsJSValue: JSValue;
- function AsNativeInt: NativeInt;
- function AsObject: TObject;
- function AsOrdinal: NativeInt;
- function AsString: string;
- generic function AsType<T>: T;
- function AsUnicodeString: UnicodeString;
- function Cast(ATypeInfo: TTypeInfo; const EmptyAsAnyType: Boolean = True): TValue; overload;
- generic function Cast<T>(const EmptyAsAnyType: Boolean = True): TValue; overload;
- function IsType(ATypeInfo: TTypeInfo; const EmptyAsAnyType: Boolean = True): Boolean; overload;
- generic function IsType<T>(const EmptyAsAnyType: Boolean = True): Boolean; overload;
- function GetArrayElement(aIndex: SizeInt): TValue;
- function GetArrayLength: SizeInt;
- function IsArray: boolean;
- function IsClass: boolean;
- function IsObject: boolean;
- function IsObjectInstance: boolean;
- function IsOrdinal: boolean;
- function IsType(ATypeInfo: TTypeInfo): boolean;
- function ToString: String;
- function TryCast(ATypeInfo: TTypeInfo; out AResult: TValue; const EmptyAsAnyType: Boolean = True): Boolean;
- procedure SetArrayElement(aIndex: SizeInt; const AValue: TValue);
- procedure SetArrayLength(const Size: SizeInt);
- property IsEmpty: boolean read GetIsEmpty; // check if nil or undefined
- property Kind: TTypeKind read GetTypeKind;
- property TypeInfo: TTypeInfo read FTypeInfo;
- end;
- TRttiType = class;
- TRttiInstanceType = class;
- { TRTTIContext }
- TRTTIContext = record
- private
- FPool: TJSObject; // maps 'modulename.typename' to TRTTIType
- class constructor Init;
- public
- class function Create: TRTTIContext; static;
- procedure Free;
- function FindType(const AQualifiedName: String): TRttiType;
- function GetType(aTypeInfo: PTypeInfo): TRTTIType; overload;
- function GetType(aClass: TClass): TRTTIType; overload;
- end;
- { TRttiObject }
- TRttiObject = class abstract
- private
- FAttributesLoaded: Boolean;
- FAttributes: TCustomAttributeArray;
- protected
- function LoadCustomAttributes: TCustomAttributeArray; virtual;
- public
- destructor Destroy; override;
- //property Handle: Pointer read GetHandle; not supported in pas2js
- function GetAttributes: TCustomAttributeArray;
- property Attributes: TCustomAttributeArray read GetAttributes;
- end;
- { TRttiNamedObject }
- TRttiNamedObject = class(TRttiObject)
- protected
- function GetName: string; virtual;
- public
- property Name: string read GetName;
- end;
- { TRttiMember }
- TMemberVisibility=(
- mvPrivate,
- mvProtected,
- mvPublic,
- mvPublished);
- TRttiMember = class(TRttiNamedObject)
- private
- FTypeInfo: TTypeMember;
- FParent: TRttiType;
- protected
- function GetMemberTypeInfo: TTypeMember;
- function GetName: string; override;
- function GetVisibility: TMemberVisibility; virtual;
- function LoadCustomAttributes: TCustomAttributeArray; override;
- public
- constructor Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- property MemberTypeInfo: TTypeMember read GetMemberTypeInfo;
- property Visibility: TMemberVisibility read GetVisibility;
- property Parent: TRttiType read FParent;
- end;
- { TRttiField }
- TRttiField = class(TRttiMember)
- private
- function GetFieldType: TRttiType;
- function GetFieldTypeInfo: TTypeMemberField;
- public
- constructor Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- function GetValue(Instance: JSValue): TValue;
- procedure SetValue(Instance: JSValue; const AValue: TValue);
- property FieldType: TRttiType read GetFieldType;
- property FieldTypeInfo: TTypeMemberField read GetFieldTypeInfo;
- end;
- TRttiFieldArray = specialize TArray<TRttiField>;
- { TRttiParameter }
- TRttiParameter = class(TRttiNamedObject)
- private
- FParamType: TRttiType;
- FFlags: TParamFlags;
- FName: String;
- protected
- function GetName: string; override;
- public
- property Flags: TParamFlags read FFlags;
- property ParamType: TRttiType read FParamType;
- end;
- TRttiParameterArray = specialize TArray<TRttiParameter>;
- { TRttiMethod }
- TRttiMethod = class(TRttiMember)
- private
- FParameters: TRttiParameterArray;
- FParametersLoaded: Boolean;
- function GetIsAsyncCall: Boolean;
- function GetIsClassMethod: Boolean;
- function GetIsConstructor: Boolean;
- function GetIsDestructor: Boolean;
- function GetIsExternal: Boolean;
- function GetIsSafeCall: Boolean;
- function GetIsStatic: Boolean;
- function GetIsVarArgs: Boolean;
- function GetMethodKind: TMethodKind;
- function GetMethodTypeInfo: TTypeMemberMethod;
- function GetProcedureFlags: TProcedureFlags;
- function GetReturnType: TRttiType;
- procedure LoadParameters;
- public
- function GetParameters: TRttiParameterArray;
- property IsAsyncCall: Boolean read GetIsAsyncCall;
- property IsClassMethod: Boolean read GetIsClassMethod;
- property IsConstructor: Boolean read GetIsConstructor;
- property IsDestructor: Boolean read GetIsDestructor;
- property IsExternal: Boolean read GetIsExternal;
- property IsSafeCall: Boolean read GetIsSafeCall;
- property IsStatic: Boolean read GetIsStatic;
- property MethodKind: TMethodKind read GetMethodKind;
- property MethodTypeInfo: TTypeMemberMethod read GetMethodTypeInfo;
- property ReturnType: TRttiType read GetReturnType;
- end;
- TRttiMethodArray = specialize TArray<TRttiMethod>;
- { TRttiProperty }
- TRttiProperty = class(TRttiMember)
- private
- function GetPropertyTypeInfo: TTypeMemberProperty;
- function GetPropertyType: TRttiType;
- function GetIsWritable: boolean;
- function GetIsReadable: boolean;
- protected
- function GetVisibility: TMemberVisibility; override;
- public
- constructor Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- function GetValue(Instance: JSValue): TValue;
- procedure SetValue(Instance: JSValue; const AValue: TValue);
- property PropertyTypeInfo: TTypeMemberProperty read GetPropertyTypeInfo;
- property PropertyType: TRttiType read GetPropertyType;
- property IsReadable: boolean read GetIsReadable;
- property IsWritable: boolean read GetIsWritable;
- property Visibility: TMemberVisibility read GetVisibility;
- end;
- TRttiPropertyArray = specialize TArray<TRttiProperty>;
- { TRttiType }
- TRttiType = class(TRttiNamedObject)
- private
- FTypeInfo: TTypeInfo;
- //FMethods: specialize TArray<TRttiMethod>;
- function GetAsInstance: TRttiInstanceType;
- function GetQualifiedName: String;
- protected
- function GetName: string; override;
- //function GetHandle: Pointer; override;
- function GetIsInstance: boolean;
- //function GetIsManaged: boolean; virtual;
- function GetIsOrdinal: boolean; virtual;
- function GetIsRecord: boolean; virtual;
- function GetIsSet: boolean; virtual;
- function GetTypeKind: TTypeKind; virtual;
- //function GetTypeSize: integer; virtual;
- //function GetBaseType: TRttiType; virtual;
- function LoadCustomAttributes: TCustomAttributeArray; override;
- public
- constructor Create(ATypeInfo : PTypeInfo);
- function GetField(const AName: string): TRttiField; virtual;
- function GetFields: TRttiFieldArray; virtual;
- function GetMethods: TRttiMethodArray; virtual;
- function GetMethods(const aName: String): TRttiMethodArray; virtual;
- function GetMethod(const aName: String): TRttiMethod; virtual;
- function GetProperty(const AName: string): TRttiProperty; virtual;
- //function GetIndexedProperty(const AName: string): TRttiIndexedProperty; virtual;
- function GetDeclaredProperties: TRttiPropertyArray; virtual;
- //function GetDeclaredIndexedProperties: TRttiIndexedPropertyArray; virtual;
- function GetDeclaredMethods: TRttiMethodArray; virtual;
- function GetDeclaredFields: TRttiFieldArray; virtual;
- property Handle: TTypeInfo read FTypeInfo;
- property IsInstance: boolean read GetIsInstance;
- //property isManaged: boolean read GetIsManaged;
- property IsOrdinal: boolean read GetIsOrdinal;
- property IsRecord: boolean read GetIsRecord;
- property IsSet: boolean read GetIsSet;
- //property BaseType: TRttiType read GetBaseType;
- property AsInstance: TRttiInstanceType read GetAsInstance;
- property TypeKind: TTypeKind read GetTypeKind;
- //property TypeSize: integer read GetTypeSize;
- property QualifiedName: String read GetQualifiedName;
- end;
- TRttiTypeClass = class of TRttiType;
- { TRttiStructuredType }
- TRttiStructuredType = class abstract(TRttiType)
- private
- FFields: TRttiFieldArray;
- FMethods: TRttiMethodArray;
- FProperties: TRttiPropertyArray;
- protected
- function GetAncestor: TRttiStructuredType; virtual;
- function GetStructTypeInfo: TTypeInfoStruct;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- destructor Destroy; override;
- function GetDeclaredFields: TRttiFieldArray; override;
- function GetDeclaredMethods: TRttiMethodArray; override;
- function GetDeclaredProperties: TRttiPropertyArray; override;
- function GetFields: TRttiFieldArray; override;
- function GetMethod(const aName: String): TRttiMethod; override;
- function GetMethods: TRttiMethodArray; override;
- function GetMethods(const aName: String): TRttiMethodArray; override;
- function GetProperties: TRttiPropertyArray;
- function GetProperty(const AName: string): TRttiProperty; override;
- property StructTypeInfo: TTypeInfoStruct read GetStructTypeInfo;
- end;
- { TRttiInstanceType }
- TRttiInstanceType = class(TRttiStructuredType)
- private
- function GetClassTypeInfo: TTypeInfoClass;
- function GetMetaClassType: TClass;
- protected
- function GetAncestor: TRttiStructuredType; override;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property ClassTypeInfo: TTypeInfoClass read GetClassTypeInfo;
- property MetaClassType: TClass read GetMetaClassType;
- end;
- { TRttiInterfaceType }
- TRttiInterfaceType = class(TRttiStructuredType)
- private
- function GetGUID: TGUID;
- function GetInterfaceTypeInfo: TTypeInfoInterface;
- protected
- function GetAncestor: TRttiStructuredType; override;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property GUID: TGUID read GetGUID;
- property InterfaceTypeInfo: TTypeInfoInterface read GetInterfaceTypeInfo;
- end;
- { TRttiRecordType }
- TRttiRecordType = class(TRttiStructuredType)
- private
- function GetRecordTypeInfo: TTypeInfoRecord;
- protected
- function GetIsRecord: Boolean; override;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property RecordTypeInfo: TTypeInfoRecord read GetRecordTypeInfo;
- end;
- { TRttiClassRefType }
- TRttiClassRefType = class(TRttiType)
- private
- function GetClassRefTypeInfo: TTypeInfoClassRef;
- function GetInstanceType: TRttiInstanceType;
- function GetMetaclassType: TClass;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property ClassRefTypeInfo: TTypeInfoClassRef read GetClassRefTypeInfo;
- property InstanceType: TRttiInstanceType read GetInstanceType;
- property MetaclassType: TClass read GetMetaclassType;
- end;
- { TRttiOrdinalType }
- TRttiOrdinalType = class(TRttiType)
- private
- function GetMaxValue: Integer; virtual;
- function GetMinValue: Integer; virtual;
- function GetOrdType: TOrdType;
- function GetOrdinalTypeInfo: TTypeInfoInteger;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property OrdType: TOrdType read GetOrdType;
- property MinValue: Integer read GetMinValue;
- property MaxValue: Integer read GetMaxValue;
- property OrdinalTypeInfo: TTypeInfoInteger read GetOrdinalTypeInfo;
- end;
- { TRttiEnumerationType }
- TRttiEnumerationType = class(TRttiOrdinalType)
- private
- function GetEnumerationTypeInfo: TTypeInfoEnum;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property EnumerationTypeInfo: TTypeInfoEnum read GetEnumerationTypeInfo;
- function GetNames: TStringArray;
- generic class function GetName<T>(AValue: T): String; reintroduce;
- generic class function GetValue<T>(const AValue: String): T;
- end;
- { TRttiDynamicArrayType }
- TRttiDynamicArrayType = class(TRttiType)
- private
- function GetDynArrayTypeInfo: TTypeInfoDynArray;
- function GetElementType: TRttiType;
- public
- constructor Create(ATypeInfo: PTypeInfo);
- property DynArrayTypeInfo: TTypeInfoDynArray read GetDynArrayTypeInfo;
- property ElementType: TRttiType read GetElementType;
- end;
- EInvoke = EJS;
- TVirtualInterfaceInvokeEvent = function(const aMethodName: string;
- const Args: TJSValueDynArray): JSValue of object;
- { TVirtualInterface: A class that can implement any IInterface. Any method
- call is handled by the OnInvoke event. }
- TVirtualInterface = class(TInterfacedObject, IInterface)
- private
- FOnInvoke: TVirtualInterfaceInvokeEvent;
- public
- constructor Create(InterfaceTypeInfo: Pointer); overload; assembler;
- constructor Create(InterfaceTypeInfo: Pointer;
- const InvokeEvent: TVirtualInterfaceInvokeEvent); overload;
- function QueryInterface(const iid: TGuid; out obj): Integer; override;
- property OnInvoke: TVirtualInterfaceInvokeEvent read FOnInvoke write FOnInvoke;
- end;
- procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
- const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
- function Invoke(ACodeAddress: Pointer; const AArgs: TJSValueDynArray;
- ACallConv: TCallConv; AResultType: PTypeInfo; AIsStatic: Boolean;
- AIsConstructor: Boolean): TValue;
- implementation
- var
- GRttiContext: TRTTIContext;
- pas: TJSObject; external name 'pas';
- procedure CreateVirtualCorbaInterface(InterfaceTypeInfo: Pointer;
- const MethodImplementation: TVirtualInterfaceInvokeEvent; out IntfVar); assembler;
- asm
- var IntfType = InterfaceTypeInfo.interface;
- var i = Object.create(IntfType);
- var o = { $name: "virtual", $fullname: "virtual" };
- i.$o = o;
- do {
- var names = IntfType.$names;
- if (!names) break;
- for (var j=0; j<names.length; j++){
- let fnname = names[j];
- i[fnname] = function(){ return MethodImplementation(fnname,arguments); };
- }
- IntfType = Object.getPrototypeOf(IntfType);
- } while(IntfType!=null);
- IntfVar.set(i);
- end;
- { TRttiDynamicArrayType }
- function TRttiDynamicArrayType.GetDynArrayTypeInfo: TTypeInfoDynArray;
- begin
- Result := TTypeInfoDynArray(FTypeInfo);
- end;
- function TRttiDynamicArrayType.GetElementType: TRttiType;
- begin
- Result := GRttiContext.GetType(DynArrayTypeInfo.ElType);
- end;
- constructor TRttiDynamicArrayType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoDynArray) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- { TRttiOrdinalType }
- function TRttiOrdinalType.GetMaxValue: Integer;
- begin
- Result := OrdinalTypeInfo.MaxValue;
- end;
- function TRttiOrdinalType.GetMinValue: Integer;
- begin
- Result := OrdinalTypeInfo.MinValue;
- end;
- function TRttiOrdinalType.GetOrdType: TOrdType;
- begin
- Result := OrdinalTypeInfo.OrdType;
- end;
- function TRttiOrdinalType.GetOrdinalTypeInfo: TTypeInfoInteger;
- begin
- Result := TTypeInfoInteger(FTypeInfo);
- end;
- constructor TRttiOrdinalType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoInteger) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- { TRttiEnumerationType }
- function TRttiEnumerationType.GetEnumerationTypeInfo: TTypeInfoEnum;
- begin
- Result := TTypeInfoEnum(FTypeInfo);
- end;
- function TRttiEnumerationType.GetNames: TStringArray;
- var
- A, NamesSize: Integer;
- begin
- NamesSize := GetEnumNameCount(EnumerationTypeInfo);
- SetLength(Result, NamesSize);
- for A := 0 to Pred(NamesSize) do
- Result[A] := EnumerationTypeInfo.EnumType.IntToName[A + MinValue];
- end;
- generic class function TRttiEnumerationType.GetName<T>(AValue: T): String;
- Var
- P : PTypeInfo;
- begin
- P:=TypeInfo(T);
- if not (TTypeInfo(P).kind=tkEnumeration) then
- raise EInvalidCast.CreateFmt(SErrTypeIsNotEnumerated,[TTypeInfo(P).Name]);
- Result := GetEnumName(TTypeInfoEnum(P), Integer(JSValue(AValue)));
- end;
- generic class function TRttiEnumerationType.GetValue<T>(const AValue: String): T;
- Var
- P : PTypeInfo;
- begin
- P:=TypeInfo(T);
- if not (TTypeInfo(P).kind=tkEnumeration) then
- raise EInvalidCast.CreateFmt(SErrTypeIsNotEnumerated,[TTypeInfo(P).Name]);
- Result := T(JSValue(GetEnumValue(TTypeInfoEnum(TypeInfo(T)), AValue)));
- end;
- constructor TRttiEnumerationType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoEnum) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- { TValue }
- function TValue.GetTypeKind: TTypeKind;
- begin
- if TypeInfo=nil then
- Result:=tkUnknown
- else
- Result:=FTypeInfo.Kind;
- end;
- generic function TValue.AsType<T>: T;
- begin
- Result := T(AsJSValue)
- end;
- generic class function TValue.From<T>(const Value: T): TValue;
- begin
- Make(Value, System.TypeInfo(T), Result);
- end;
- class procedure TValue.Make(ABuffer: JSValue; ATypeInfo: PTypeInfo; var Result: TValue);
- begin
- Result.FData := ABuffer;
- Result.FTypeInfo := ATypeInfo;
- end;
- generic class procedure TValue.Make<T>(const Value: T; var Result: TValue);
- begin
- TValue.Make(Value, System.TypeInfo(T), Result);
- end;
- function TValue.Cast(ATypeInfo: TTypeInfo; const EmptyAsAnyType: Boolean): TValue;
- begin
- if not TryCast(ATypeInfo, Result, EmptyAsAnyType) then
- raise EInvalidCast.Create('');
- end;
- generic function TValue.Cast<T>(const EmptyAsAnyType: Boolean): TValue;
- begin
- Result := Cast(System.TypeInfo(T), EmptyAsAnyType);
- end;
- function TValue.IsType(ATypeInfo: TTypeInfo; const EmptyAsAnyType: Boolean): Boolean;
- var
- AnyValue: TValue;
- begin
- Result := TryCast(ATypeInfo, AnyValue, EmptyAsAnyType);
- end;
- generic function TValue.IsType<T>(const EmptyAsAnyType: Boolean): Boolean;
- begin
- Result := IsType(System.TypeInfo(T), EmptyAsAnyType);
- end;
- function TValue.TryCast(ATypeInfo: TTypeInfo; out AResult: TValue; const EmptyAsAnyType: Boolean): Boolean;
- function ConversionAccepted: TTypeKinds;
- begin
- case TypeInfo.Kind of
- tkString: Exit([tkChar, tkString]);
- tkDouble: Exit([tkInteger, tkDouble]);
- tkEnumeration: Exit([tkInteger, tkEnumeration]);
- else Exit([TypeInfo.Kind]);
- end;
- end;
- begin
- if EmptyAsAnyType and IsEmpty then
- begin
- AResult := TValue.Empty;
- if ATypeInfo <> nil then
- begin
- AResult.FTypeInfo := ATypeInfo;
- case ATypeInfo.Kind of
- tkBool: AResult.FData := False;
- tkChar: AResult.FData := #0;
- tkString: AResult.FData := EmptyStr;
- tkDouble,
- tkEnumeration,
- tkInteger: AResult.FData := 0;
- end;
- Exit(True);
- end;
- end;
- if not EmptyAsAnyType and (FTypeInfo = nil) then
- Exit(False);
- if FTypeInfo = ATypeInfo then
- begin
- AResult := Self;
- Exit(True);
- end;
- if ATypeInfo = nil then
- Exit(False);
- if ATypeInfo = System.TypeInfo(TValue) then
- begin
- TValue.Make(Self, System.TypeInfo(TValue), AResult);
- Exit(True);
- end;
- Result := ATypeInfo.Kind in ConversionAccepted;
- if Result then
- begin
- AResult.FData := FData;
- AResult.FTypeInfo := ATypeInfo;
- end;
- end;
- class function TValue.FromJSValue(v: JSValue): TValue;
- var
- i: NativeInt;
- TypeOfValue: TTypeInfo;
- begin
- case jsTypeOf(v) of
- 'number':
- if JS.isInteger(v) then
- begin
- i:=NativeInt(v);
- if (i>=low(integer)) and (i<=high(integer)) then
- TypeOfValue:=System.TypeInfo(Integer)
- else
- TypeOfValue:=System.TypeInfo(NativeInt);
- end
- else
- TypeOfValue:=system.TypeInfo(Double);
- 'string': TypeOfValue:=System.TypeInfo(String);
- 'boolean': TypeOfValue:=System.TypeInfo(Boolean);
- 'object':
- if v=nil then
- Exit(TValue.Empty)
- else if JS.isClass(v) and JS.isExt(v,TObject) then
- TypeOfValue:=System.TypeInfo(TClass(v))
- else if JS.isObject(v) and JS.isExt(v,TObject) then
- TypeOfValue:=System.TypeInfo(TObject(v))
- else if isRecord(v) then
- TypeOfValue:=System.TypeInfo(TObject(v))
- else if TJSArray.IsArray(V) then
- TypeOfValue:=System.TypeInfo(TObject(v))
- else
- raise EInvalidCast.Create('Type not recognized in FromJSValue!');
- else
- TypeOfValue:=System.TypeInfo(JSValue);
- end;
- Make(v, TypeOfValue, Result);
- end;
- class function TValue.FromArray(TypeInfo: TTypeInfo; const Values: specialize TArray<TValue>): TValue;
- var
- A: Integer;
- DynTypeInfo: TTypeInfoDynArray absolute TypeInfo;
- NewArray: TJSArray;
- ElementType: TTypeInfo;
- begin
- if TypeInfo.Kind <> tkDynArray then
- raise EInvalidCast.Create('Type not an array in FromArray!');
- ElementType := DynTypeInfo.ElType;
- NewArray := TJSArray.new;
- NewArray.Length := Length(Values);
- for A := 0 to High(Values) do
- NewArray[A] := Values[A].Cast(ElementType).AsJSValue;
- Result.FData := NewArray;
- Result.FTypeInfo := TypeInfo;
- end;
- class function TValue.FromOrdinal(ATypeInfo: TTypeInfo; AValue: JSValue): TValue;
- begin
- if (ATypeInfo = nil) or not (ATypeInfo.Kind in [tkBool, tkEnumeration, tkInteger]) then
- raise EInvalidCast.Create('Invalid type in FromOrdinal');
- if ATypeInfo.Kind = tkBool then
- TValue.Make(AValue = True, ATypeInfo, Result)
- else
- TValue.Make(NativeInt(AValue), ATypeInfo, Result);
- end;
- function TValue.IsObject: boolean;
- begin
- Result:=IsEmpty or (TypeInfo.Kind=tkClass);
- end;
- function TValue.AsObject: TObject;
- begin
- if IsObject or (IsClass and not js.isObject(FData)) then
- Result := TObject(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.IsObjectInstance: boolean;
- begin
- Result:=(TypeInfo<>nil) and (TypeInfo.Kind=tkClass);
- end;
- function TValue.IsArray: boolean;
- begin
- case Kind of
- tkDynArray: Exit(True);
- tkArray: Exit(Length(TTypeInfoStaticArray(FTypeInfo).Dims) = 1);
- else Result := False;
- end;
- end;
- function TValue.IsClass: boolean;
- var
- k: TTypeKind;
- begin
- k:=Kind;
- Result := (k = tkClassRef)
- or ((k in [tkClass,tkUnknown]) and not JS.IsObject(FData));
- end;
- function TValue.AsClass: TClass;
- begin
- if IsClass then
- Result := TClass(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.IsOrdinal: boolean;
- var
- k: TTypeKind;
- begin
- k:=Kind;
- Result := (k in [tkInteger, tkBool]) or
- ((k in [tkClass, tkClassRef, tkUnknown]) and not JS.isObject(FData));
- end;
- function TValue.AsOrdinal: NativeInt;
- begin
- if IsOrdinal then
- Result:=NativeInt(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsBoolean: boolean;
- begin
- if (Kind = tkBool) then
- Result:=boolean(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsInteger: Integer;
- begin
- if JS.isInteger(FData) then
- Result:=NativeInt(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsNativeInt: NativeInt;
- begin
- if JS.isInteger(FData) then
- Result:=NativeInt(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsInterface: IInterface;
- var
- k: TTypeKind;
- begin
- k:=Kind;
- if k = tkInterface then
- Result := IInterface(FData)// ToDo
- else if (k in [tkClass, tkClassRef, tkUnknown]) and not JS.isObject(FData) then
- Result := Nil
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsString: string;
- begin
- if js.isString(FData) then
- Result:=String(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.AsUnicodeString: UnicodeString;
- begin
- Result:=AsString;
- end;
- function TValue.AsExtended: Extended;
- begin
- if js.isNumber(FData) then
- Result:=Double(FData)
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.ToString: String;
- begin
- case Kind of
- tkString: Result := AsString;
- tkInteger: Result := IntToStr(AsNativeInt);
- tkBool: Result := BoolToStr(AsBoolean, True);
- else
- Result := '';
- end;
- end;
- function TValue.GetArrayLength: SizeInt;
- begin
- if IsArray then
- Exit(Length(TJSValueDynArray(FData)));
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.GetArrayElement(aIndex: SizeInt): TValue;
- begin
- if IsArray then
- begin
- case Kind of
- tkArray: Result.FTypeInfo:=TTypeInfoStaticArray(FTypeInfo).ElType;
- tkDynArray: Result.FTypeInfo:=TTypeInfoDynArray(FTypeInfo).ElType;
- end;
- Result.FData:=TJSValueDynArray(FData)[aIndex];
- end
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- procedure TValue.SetArrayLength(const Size: SizeInt);
- var
- NewArray: TJSValueDynArray;
- begin
- NewArray := TJSValueDynArray(FData);
- SetLength(NewArray, Size);
- FData := NewArray;
- end;
- procedure TValue.SetArrayElement(aIndex: SizeInt; const AValue: TValue);
- begin
- if IsArray then
- TJSValueDynArray(FData)[aIndex] := AValue.AsJSValue
- else
- raise EInvalidCast.Create(SErrInvalidTypecast);
- end;
- function TValue.IsType(ATypeInfo: TTypeInfo): boolean;
- begin
- Result := ATypeInfo = TypeInfo;
- end;
- function TValue.GetIsEmpty: boolean;
- begin
- if (TypeInfo=nil) or (FData=Undefined) or (FData=nil) then
- exit(true);
- case TypeInfo.Kind of
- tkDynArray:
- Result:=TJSArray(FData).Length=0;
- else
- Result:=false;
- end;
- end;
- function TValue.AsJSValue: JSValue;
- begin
- Result := FData;
- end;
- class function TValue.Empty: TValue;
- begin
- Result.FData := nil;
- Result.FTypeInfo := nil;
- end;
- { TRttiStructuredType }
- function TRttiStructuredType.GetMethods: TRttiMethodArray;
- var
- A, Start: Integer;
- BaseClass: TRttiStructuredType;
- Declared: TRttiMethodArray;
- begin
- BaseClass := Self;
- Result := nil;
- while Assigned(BaseClass) do
- begin
- Declared := BaseClass.GetDeclaredMethods;
- Start := Length(Result);
- SetLength(Result, Start + Length(Declared));
- for A := Low(Declared) to High(Declared) do
- Result[Start + A] := Declared[A];
- BaseClass := BaseClass.GetAncestor;
- end;
- end;
- function TRttiStructuredType.GetMethods(const aName: String): TRttiMethodArray;
- var
- Method: TRttiMethod;
- MethodCount: Integer;
- begin
- MethodCount := 0;
- for Method in GetMethods do
- if aName = Method.Name then
- Inc(MethodCount);
- SetLength(Result, MethodCount);
- for Method in GetMethods do
- if aName = Method.Name then
- begin
- Dec(MethodCount);
- Result[MethodCount] := Method;
- end;
- end;
- function TRttiStructuredType.GetProperties: TRttiPropertyArray;
- var
- A, Start: Integer;
- BaseClass: TRttiStructuredType;
- Declared: TRttiPropertyArray;
- begin
- BaseClass := Self;
- Result := nil;
- while Assigned(BaseClass) do
- begin
- Declared := BaseClass.GetDeclaredProperties;
- Start := Length(Result);
- SetLength(Result, Start + Length(Declared));
- for A := Low(Declared) to High(Declared) do
- Result[Start + A] := Declared[A];
- BaseClass := BaseClass.GetAncestor;
- end;
- end;
- function TRttiStructuredType.GetMethod(const aName: String): TRttiMethod;
- var
- Method: TRttiMethod;
- begin
- for Method in GetMethods do
- if aName = Method.Name then
- Exit(Method);
- end;
- function TRttiStructuredType.GetProperty(const AName: string): TRttiProperty;
- var
- Prop: TRttiProperty;
- begin
- for Prop in GetProperties do
- if Prop.Name = AName then
- Exit(Prop);
- end;
- function TRttiStructuredType.GetDeclaredProperties: TRttiPropertyArray;
- var
- A, PropCount: Integer;
- begin
- if not Assigned(FProperties) then
- begin
- PropCount := StructTypeInfo.PropCount;
- SetLength(FProperties, PropCount);
- for A := 0 to Pred(PropCount) do
- FProperties[A] := TRttiProperty.Create(Self, StructTypeInfo.GetProp(A));
- end;
- Result := FProperties;
- end;
- function TRttiStructuredType.GetAncestor: TRttiStructuredType;
- begin
- Result := nil;
- end;
- function TRttiStructuredType.GetStructTypeInfo: TTypeInfoStruct;
- begin
- Result:=TTypeInfoStruct(FTypeInfo);
- end;
- constructor TRttiStructuredType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoStruct) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- destructor TRttiStructuredType.Destroy;
- var
- Method: TRttiMethod;
- Prop: TRttiProperty;
- begin
- for Method in FMethods do
- Method.Free;
- for Prop in FProperties do
- Prop.Free;
- inherited Destroy;
- end;
- function TRttiStructuredType.GetDeclaredMethods: TRttiMethodArray;
- var
- A, MethodCount: Integer;
- begin
- if not Assigned(FMethods) then
- begin
- MethodCount := StructTypeInfo.MethodCount;
- SetLength(FMethods, MethodCount);
- for A := 0 to Pred(MethodCount) do
- FMethods[A] := TRttiMethod.Create(Self, StructTypeInfo.GetMethod(A));
- end;
- Result := FMethods;
- end;
- function TRttiStructuredType.GetDeclaredFields: TRttiFieldArray;
- var
- A, FieldCount: Integer;
- begin
- if not Assigned(FFields) then
- begin
- FieldCount := StructTypeInfo.FieldCount;
- SetLength(FFields, FieldCount);
- for A := 0 to Pred(FieldCount) do
- FFields[A] := TRttiField.Create(Self, StructTypeInfo.GetField(A));
- end;
- Result := FFields;
- end;
- function TRttiStructuredType.GetFields: TRttiFieldArray;
- var
- A, Start: Integer;
- BaseClass: TRttiStructuredType;
- Declared: TRttiFieldArray;
- begin
- BaseClass := Self;
- Result := nil;
- while Assigned(BaseClass) do
- begin
- Declared := BaseClass.GetDeclaredFields;
- Start := Length(Result);
- SetLength(Result, Start + Length(Declared));
- for A := Low(Declared) to High(Declared) do
- Result[Start + A] := Declared[A];
- BaseClass := BaseClass.GetAncestor;
- end;
- end;
- { TRttiInstanceType }
- function TRttiInstanceType.GetClassTypeInfo: TTypeInfoClass;
- begin
- Result:=TTypeInfoClass(FTypeInfo);
- end;
- function TRttiInstanceType.GetMetaClassType: TClass;
- begin
- Result:=ClassTypeInfo.ClassType;
- end;
- function TRttiInstanceType.GetAncestor: TRttiStructuredType;
- begin
- Result := GRttiContext.GetType(ClassTypeInfo.Ancestor) as TRttiStructuredType;
- end;
- constructor TRttiInstanceType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoClass) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- { TRttiInterfaceType }
- constructor TRttiInterfaceType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoInterface) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- function TRttiInterfaceType.GetGUID: TGUID;
- var
- Guid: String;
- begin
- Guid := String(InterfaceTypeInfo.InterfaceType['$guid']);
- TryStringToGUID(Guid, Result);
- end;
- function TRttiInterfaceType.GetInterfaceTypeInfo: TTypeInfoInterface;
- begin
- Result := TTypeInfoInterface(FTypeInfo);
- end;
- function TRttiInterfaceType.GetAncestor: TRttiStructuredType;
- begin
- Result := GRttiContext.GetType(InterfaceTypeInfo.Ancestor) as TRttiStructuredType;
- end;
- { TRttiRecordType }
- function TRttiRecordType.GetRecordTypeInfo: TTypeInfoRecord;
- begin
- Result := TTypeInfoRecord(FTypeInfo);
- end;
- function TRttiRecordType.GetIsRecord: Boolean;
- begin
- Result := True;
- end;
- constructor TRttiRecordType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoClass) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- { TRttiClassRefType }
- constructor TRttiClassRefType.Create(ATypeInfo: PTypeInfo);
- begin
- if not (TTypeInfo(ATypeInfo) is TTypeInfoClassRef) then
- raise EInvalidCast.Create('');
- inherited Create(ATypeInfo);
- end;
- function TRttiClassRefType.GetClassRefTypeInfo: TTypeInfoClassRef;
- begin
- Result := TTypeInfoClassRef(FTypeInfo);
- end;
- function TRttiClassRefType.GetInstanceType: TRttiInstanceType;
- begin
- Result := GRttiContext.GetType(ClassRefTypeInfo.InstanceType) as TRttiInstanceType;
- end;
- function TRttiClassRefType.GetMetaclassType: TClass;
- begin
- Result := InstanceType.MetaClassType;
- end;
- { TRTTIContext }
- class constructor TRTTIContext.Init;
- begin
- GRttiContext:=TRTTIContext.Create;
- end;
- class function TRTTIContext.Create: TRTTIContext;
- begin
- Result.FPool:=TJSObject.new;
- end;
- procedure TRTTIContext.Free;
- var
- key: string;
- o: TRttiType;
- begin
- for key in FPool do
- if FPool.hasOwnProperty(key) then begin
- o:=TRTTIType(FPool[key]);
- o.Free;
- end;
- FPool:=nil;
- end;
- function TRTTIContext.GetType(aTypeInfo: PTypeInfo): TRTTIType;
- var
- RttiTypeClass: array[TTypeKind] of TRttiTypeClass = (
- nil, // tkUnknown
- TRttiOrdinalType, // tkInteger
- TRttiOrdinalType, // tkChar
- TRttiType, // tkString
- TRttiEnumerationType, // tkEnumeration
- TRttiType, // tkSet
- TRttiOrdinalType, // tkDouble
- TRttiEnumerationType, // tkBool
- TRttiType, // tkProcVar
- nil, // tkMethod
- TRttiType, // tkArray
- TRttiDynamicArrayType, // tkDynArray
- TRttiRecordType, // tkRecord
- TRttiInstanceType, // tkClass
- TRttiClassRefType, // tkClassRef
- TRttiType, // tkPointer
- TRttiType, // tkJSValue
- TRttiType, // tkRefToProcVar
- TRttiInterfaceType, // tkInterface
- TRttiType, // tkHelper
- TRttiInstanceType // tkExtClass
- );
- t: TTypeinfo absolute aTypeInfo;
- Name: String;
- begin
- if aTypeInfo=nil then exit(nil);
- Name:=t.Name;
- if isModule(t.Module) then
- Name:=t.Module.Name+'.'+Name;
- if GRttiContext.FPool.hasOwnProperty(Name) then
- Result:=TRttiType(GRttiContext.FPool[Name])
- else
- begin
- Result := RttiTypeClass[T.Kind].Create(aTypeInfo);
- GRttiContext.FPool[Name]:=Result;
- end;
- end;
- function TRTTIContext.GetType(aClass: TClass): TRTTIType;
- begin
- if aClass=nil then exit(nil);
- Result:=GetType(TypeInfo(aClass));
- end;
- function TRTTIContext.FindType(const AQualifiedName: String): TRttiType;
- var
- ModuleName, TypeName: String;
- Module: TTypeInfoModule;
- TypeFound: PTypeInfo;
- begin
- Result := nil;
- for ModuleName in TJSObject.Keys(pas) do
- if AQualifiedName.StartsWith(ModuleName + '.') then
- begin
- Module := TTypeInfoModule(pas[ModuleName]);
- TypeName := Copy(AQualifiedName, Length(ModuleName) + 2, Length(AQualifiedName));
- if Module.RTTI.HasOwnProperty(TypeName) then
- begin
- TypeFound := PTypeInfo(Module.RTTI[TypeName]);
- Exit(GetType(TypeFound));
- end;
- end;
- end;
- { TRttiObject }
- destructor TRttiObject.Destroy;
- var
- Attribute: TCustomAttribute;
- begin
- for Attribute in FAttributes do
- Attribute.Free;
- FAttributes := nil;
- inherited Destroy;
- end;
- function TRttiObject.LoadCustomAttributes: TCustomAttributeArray;
- begin
- Result := nil;
- end;
- function TRttiObject.GetAttributes: TCustomAttributeArray;
- begin
- if not FAttributesLoaded then
- begin
- FAttributes := LoadCustomAttributes;
- FAttributesLoaded := True;
- end;
- Result := FAttributes;
- end;
- { TRttiNamedObject }
- function TRttiNamedObject.GetName: string;
- begin
- Result:='';
- end;
- { TRttiMember }
- function TRttiMember.GetName: string;
- begin
- Result:=FTypeInfo.Name;
- end;
- function TRttiMember.GetVisibility: TMemberVisibility;
- begin
- Result:=mvPublished;
- end;
- constructor TRttiMember.Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- begin
- if not (ATypeInfo is TTypeMember) then
- raise EInvalidCast.Create('');
- inherited Create();
- FParent := AParent;
- FTypeInfo:=ATypeInfo;
- end;
- function TRttiMember.LoadCustomAttributes: TCustomAttributeArray;
- begin
- Result:=GetRTTIAttributes(FTypeInfo.Attributes);
- end;
- function TRttiMember.GetMemberTypeInfo: TTypeMember;
- begin
- Result := TTypeMember(FTypeInfo);
- end;
- { TRttiField }
- constructor TRttiField.Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- begin
- if not (ATypeInfo is TTypeMemberField) then
- raise EInvalidCast.Create('');
- inherited;
- end;
- function TRttiField.GetFieldType: TRttiType;
- begin
- Result := GRttiContext.GetType(FieldTypeInfo.TypeInfo);
- end;
- function TRttiField.GetFieldTypeInfo: TTypeMemberField;
- begin
- Result := TTypeMemberField(FTypeInfo);
- end;
- function TRttiField.GetValue(Instance: JSValue): TValue;
- var
- JSInstance: TJSObject absolute Instance;
- begin
- Result := TValue.FromJSValue(JSInstance[Name]);
- end;
- procedure TRttiField.SetValue(Instance: JSValue; const AValue: TValue);
- var
- JSInstance: TJSObject absolute Instance;
- begin
- JSInstance[Name] := AValue.Cast(FieldType.Handle, True).ASJSValue;
- end;
- { TRttiParameter }
- function TRttiParameter.GetName: String;
- begin
- Result := FName;
- end;
- { TRttiMethod }
- function TRttiMethod.GetMethodTypeInfo: TTypeMemberMethod;
- begin
- Result := TTypeMemberMethod(FTypeInfo);
- end;
- function TRttiMethod.GetIsClassMethod: Boolean;
- begin
- Result:=MethodTypeInfo.MethodKind in [mkClassFunction,mkClassProcedure];
- end;
- function TRttiMethod.GetIsConstructor: Boolean;
- begin
- Result:=MethodTypeInfo.MethodKind=mkConstructor;
- end;
- function TRttiMethod.GetIsDestructor: Boolean;
- begin
- Result:=MethodTypeInfo.MethodKind=mkDestructor;
- end;
- function TRttiMethod.GetIsExternal: Boolean;
- begin
- Result := pfExternal in GetProcedureFlags;
- end;
- function TRttiMethod.GetIsStatic: Boolean;
- begin
- Result := pfStatic in GetProcedureFlags;
- end;
- function TRttiMethod.GetIsVarArgs: Boolean;
- begin
- Result := pfVarargs in GetProcedureFlags;
- end;
- function TRttiMethod.GetIsAsyncCall: Boolean;
- begin
- Result := pfAsync in GetProcedureFlags;
- end;
- function TRttiMethod.GetIsSafeCall: Boolean;
- begin
- Result := pfSafeCall in GetProcedureFlags;
- end;
- function TRttiMethod.GetMethodKind: TMethodKind;
- begin
- Result:=MethodTypeInfo.MethodKind;;
- end;
- function TRttiMethod.GetProcedureFlags: TProcedureFlags;
- const
- PROCEDURE_FLAGS: array[TProcedureFlag] of NativeInt = (1, 2, 4, 8, 16);
- var
- Flag: TProcedureFlag;
- ProcedureFlags: NativeInt;
- begin
- ProcedureFlags := MethodTypeInfo.ProcSig.Flags;
- Result := [];
- for Flag := Low(PROCEDURE_FLAGS) to High(PROCEDURE_FLAGS) do
- if PROCEDURE_FLAGS[Flag] and ProcedureFlags > 0 then
- Result := Result + [Flag];
- end;
- function TRttiMethod.GetReturnType: TRttiType;
- begin
- Result := GRttiContext.GetType(MethodTypeInfo.ProcSig.ResultType);
- end;
- procedure TRttiMethod.LoadParameters;
- const
- FLAGS_CONVERSION: array[TParamFlag] of NativeInt = (1, 2, 4, 8, 16, 32);
- var
- A: Integer;
- Flag: TParamFlag;
- Param: TProcedureParam;
- RttiParam: TRttiParameter;
- MethodParams: TProcedureParams;
- begin
- FParametersLoaded := True;
- MethodParams := MethodTypeInfo.ProcSig.Params;
- SetLength(FParameters, Length(MethodParams));
- for A := Low(FParameters) to High(FParameters) do
- begin
- Param := MethodParams[A];
- RttiParam := TRttiParameter.Create;
- RttiParam.FName := Param.Name;
- RttiParam.FParamType := GRttiContext.GetType(Param.TypeInfo);
- for Flag := Low(FLAGS_CONVERSION) to High(FLAGS_CONVERSION) do
- if FLAGS_CONVERSION[Flag] and Param.Flags > 0 then
- RttiParam.FFlags := RttiParam.FFlags + [Flag];
- FParameters[A] := RttiParam;
- end;
- end;
- function TRttiMethod.GetParameters: TRttiParameterArray;
- begin
- if not FParametersLoaded then
- LoadParameters;
- Result := FParameters;
- end;
- { TRttiProperty }
- constructor TRttiProperty.Create(AParent: TRttiType; ATypeInfo: TTypeMember);
- begin
- if not (ATypeInfo is TTypeMemberProperty) then
- raise EInvalidCast.Create('');
- inherited;
- end;
- function TRttiProperty.GetPropertyTypeInfo: TTypeMemberProperty;
- begin
- Result := TTypeMemberProperty(FTypeInfo);
- end;
- function TRttiProperty.GetValue(Instance: JSValue): TValue;
- var
- JSObject: TJSObject absolute Instance;
- begin
- TValue.Make(GetJSValueProp(JSObject, PropertyTypeInfo), PropertyType.Handle, Result);
- end;
- procedure TRttiProperty.SetValue(Instance: JSValue; const AValue: TValue);
- var
- JSInstance: TJSObject absolute Instance;
- begin
- SetJSValueProp(JSInstance, PropertyTypeInfo, AValue.Cast(PropertyType.Handle, True).AsJSValue);
- end;
- function TRttiProperty.GetPropertyType: TRttiType;
- begin
- Result := GRttiContext.GetType(PropertyTypeInfo.TypeInfo);
- end;
- function TRttiProperty.GetIsWritable: boolean;
- begin
- Result := PropertyTypeInfo.Setter<>'';
- end;
- function TRttiProperty.GetIsReadable: boolean;
- begin
- Result := PropertyTypeInfo.Getter<>'';
- end;
- function TRttiProperty.GetVisibility: TMemberVisibility;
- begin
- // At this moment only published rtti-property-info is supported by pas2js
- Result := mvPublished;
- end;
- { TRttiType }
- function TRttiType.GetName: string;
- begin
- Result:=FTypeInfo.Name;
- end;
- function TRttiType.GetIsInstance: boolean;
- begin
- Result:=Self is TRttiInstanceType;
- end;
- function TRttiType.GetIsOrdinal: boolean;
- begin
- Result:=false;
- end;
- function TRttiType.GetIsRecord: boolean;
- begin
- Result:=false;
- end;
- function TRttiType.GetIsSet: boolean;
- begin
- Result:=false;
- end;
- function TRttiType.GetTypeKind: TTypeKind;
- begin
- Result:=FTypeInfo.Kind;
- end;
- function TRttiType.GetAsInstance: TRttiInstanceType;
- begin
- Result := Self as TRttiInstanceType;
- end;
- constructor TRttiType.Create(ATypeInfo: PTypeInfo);
- begin
- inherited Create();
- FTypeInfo:=TTypeInfo(ATypeInfo);
- end;
- function TRttiType.LoadCustomAttributes: TCustomAttributeArray;
- begin
- Result:=GetRTTIAttributes(Handle.Attributes);
- end;
- function TRttiType.GetDeclaredProperties: TRttiPropertyArray;
- begin
- Result:=nil;
- end;
- function TRttiType.GetProperty(const AName: string): TRttiProperty;
- begin
- Result:=nil;
- if AName='' then ;
- end;
- function TRttiType.GetMethods: TRttiMethodArray;
- begin
- Result:=nil;
- end;
- function TRttiType.GetMethods(const aName: String): TRttiMethodArray;
- begin
- Result:=nil;
- end;
- function TRttiType.GetMethod(const aName: String): TRttiMethod;
- begin
- Result:=nil;
- if aName='' then ;
- end;
- function TRttiType.GetDeclaredMethods: TRttiMethodArray;
- begin
- Result:=nil;
- end;
- function TRttiType.GetDeclaredFields: TRttiFieldArray;
- begin
- Result:=nil;
- end;
- function TRttiType.GetField(const AName: string): TRttiField;
- var
- AField: TRttiField;
- begin
- Result:=nil;
- for AField in GetFields do
- if AField.Name = AName then
- Exit(AField);
- end;
- function TRttiType.GetFields: TRttiFieldArray;
- begin
- Result := nil;
- end;
- function TRttiType.GetQualifiedName: String;
- begin
- Result := Format('%s.%s', [Handle.Module.Name, Name]);
- end;
- { TVirtualInterface }
- constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer); assembler;
- asm
- var IntfType = InterfaceTypeInfo.interface;
- if (IntfType.$kind !== 'com') rtl.raiseE('EInvalidCast');
- var guid = IntfType.$guid;
- var i = Object.create(IntfType); // needed by IntfVar is IntfType
- i.$o = this;
- // copy IInterface methods: _AddRef, _Release, QueryInterface
- var iinterfaceguid = '{00000000-0000-0000-C000-000000000046}';
- var map = this.$intfmaps[iinterfaceguid];
- for (var key in map){
- var v = map[key];
- if (typeof(v)!=='function') continue;
- i[key] = map[key];
- }
- // all other methods call OnInvoke
- do {
- var names = IntfType.$names;
- if (!names) break;
- for (var j=0; j<names.length; j++){
- let fnname = names[j];
- if (i[fnname]) continue;
- i[fnname] = function(){ return this.$o.FOnInvoke(fnname,arguments); };
- }
- IntfType = Object.getPrototypeOf(IntfType);
- } while(IntfType!=null);
- // create a new list of interface map, supporting IInterface and IntfType
- this.$intfmaps = {};
- this.$intfmaps[iinterfaceguid] = map;
- this.$intfmaps[guid] = {};
- // store the implementation of IntfType (used by the as-operator)
- this.$interfaces = {};
- this.$interfaces[guid] = i;
- end;
- constructor TVirtualInterface.Create(InterfaceTypeInfo: Pointer;
- const InvokeEvent: TVirtualInterfaceInvokeEvent);
- begin
- Create(InterfaceTypeInfo);
- OnInvoke:=InvokeEvent;
- end;
- function TVirtualInterface.QueryInterface(const iid: TGuid; out obj): Integer;
- begin
- Result := inherited QueryInterface(iid, obj);
- end;
- function Invoke(ACodeAddress: Pointer; const AArgs: TJSValueDynArray;
- ACallConv: TCallConv; AResultType: PTypeInfo; AIsStatic: Boolean;
- AIsConstructor: Boolean): TValue;
- begin
- if ACallConv=ccReg then ;
- if AIsStatic then ;
- if AIsConstructor then
- raise EInvoke.Create('not supported');
- if isFunction(ACodeAddress) then
- begin
- Result.FData := TJSFunction(ACodeAddress).apply(nil, AArgs);
- if AResultType<>nil then
- Result.FTypeInfo:=AResultType
- else
- Result.FTypeInfo:=TypeInfo(JSValue);
- end
- else
- raise EInvoke.Create(SErrInvokeInvalidCodeAddr);
- end;
- end.
|