jpvar.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by Jonas Maebe,
  4. members of the Free Pascal development team.
  5. This file implements support infrastructure for procvars under the JVM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. constructor FpcBaseProcVarType.create(inst: jlobject; const methodName: unicodestring; const argTypes: array of JLClass);
  13. begin
  14. method.data:=inst;
  15. setFpcBaseProcVarTypeBySignature(methodName,argtypes);
  16. end;
  17. constructor FpcBaseProcVarType.create(const meth: tmethod);
  18. begin
  19. method:=meth;
  20. end;
  21. procedure FpcBaseProcVarType.setFpcBaseProcVarTypeBySignature(const methodName: unicodestring; const argTypes: array of JLClass);
  22. var
  23. owningClass: JLClass;
  24. begin
  25. { class method or instance method }
  26. if method.data is JLClass then
  27. owningClass:=JLClass(method.data)
  28. else
  29. owningClass:=method.data.getClass;
  30. method.code:=owningClass.getDeclaredMethod(methodName,argTypes);
  31. { required to enable calling private methods in one class from another
  32. class -- can cause security exceptions if the security manager doesn't
  33. allow this though... }
  34. if not method.code.isAccessible then
  35. method.code.setAccessible(true);
  36. end;
  37. function FpcBaseProcVarType.getClassProcArgs(const args: array of jlobject): TJLObjectDynArray;
  38. var
  39. arglen: longint;
  40. begin
  41. { add the self pointer as first argument (Java class methods don't take an
  42. implicit self parameters, Pascal ones do) }
  43. arglen:=length(args);
  44. setlength(result,arglen+1);
  45. JLSystem.ArrayCopy(JLObject(@args),0,JLObject(result),1,arglen);
  46. result[0]:=method.data;
  47. end;
  48. procedure FpcBaseProcVarType.fpcDeepCopy(result: FpcBaseProcVarType);
  49. begin
  50. result.method:=method;
  51. end;
  52. function FpcBaseProcVarType.clone: JLObject;
  53. begin
  54. result:=inherited;
  55. FpcBaseProcVarType(result).method:=method;
  56. end;
  57. procedure FpcBaseProcVarType.invokeProc(const args: array of jlobject);
  58. begin
  59. { caching the length would be faster, but that would have to be done
  60. in a synchronised way. Doing it at construction time and in fpcDeepCopy/
  61. clone is not enough, because the method field can be manipulated
  62. directly }
  63. if length(method.code.getParameterTypes)=length(args) then
  64. method.code.invoke(method.data,args)
  65. else
  66. method.code.invoke(method.data,getClassProcArgs(args));
  67. end;
  68. function FpcBaseProcVarType.invokeBooleanFunc(const args: array of jlobject): jboolean;
  69. begin
  70. if length(method.code.getParameterTypes)=length(args) then
  71. result:=JLBoolean(method.code.invoke(method.data,args)).booleanValue
  72. else
  73. result:=JLBoolean(method.code.invoke(method.data,getClassProcArgs(args))).booleanValue
  74. end;
  75. function FpcBaseProcVarType.invokeCharFunc(const args: array of jlobject): jchar;
  76. begin
  77. if length(method.code.getParameterTypes)=length(args) then
  78. result:=JLCharacter(method.code.invoke(method.data,args)).charValue
  79. else
  80. result:=JLCharacter(method.code.invoke(method.data,getClassProcArgs(args))).charValue;
  81. end;
  82. function FpcBaseProcVarType.invokeByteFunc(const args: array of jlobject): jbyte;
  83. begin
  84. if length(method.code.getParameterTypes)=length(args) then
  85. result:=JLByte(method.code.invoke(method.data,args)).byteValue
  86. else
  87. result:=JLByte(method.code.invoke(method.data,getClassProcArgs(args))).byteValue
  88. end;
  89. function FpcBaseProcVarType.invokeShortFunc(const args: array of jlobject): jshort;
  90. begin
  91. if length(method.code.getParameterTypes)=length(args) then
  92. result:=JLShort(method.code.invoke(method.data,args)).shortValue
  93. else
  94. result:=JLShort(method.code.invoke(method.data,getClassProcArgs(args))).shortValue
  95. end;
  96. function FpcBaseProcVarType.invokeIntFunc(const args: array of jlobject): jint;
  97. begin
  98. if length(method.code.getParameterTypes)=length(args) then
  99. result:=JLInteger(method.code.invoke(method.data,args)).intValue
  100. else
  101. result:=JLInteger(method.code.invoke(method.data,getClassProcArgs(args))).intValue
  102. end;
  103. function FpcBaseProcVarType.invokeLongFunc(const args: array of jlobject): jlong;
  104. begin
  105. if length(method.code.getParameterTypes)=length(args) then
  106. result:=JLLong(method.code.invoke(method.data,args)).longValue
  107. else
  108. result:=JLLong(method.code.invoke(method.data,getClassProcArgs(args))).longValue;
  109. end;
  110. function FpcBaseProcVarType.invokeSingleFunc(const args: array of jlobject): jfloat;
  111. begin
  112. if length(method.code.getParameterTypes)=length(args) then
  113. result:=JLFloat(method.code.invoke(method.data,args)).floatValue
  114. else
  115. result:=JLFloat(method.code.invoke(method.data,getClassProcArgs(args))).floatValue
  116. end;
  117. function FpcBaseProcVarType.invokeDoubleFunc(const args: array of jlobject): jdouble;
  118. begin
  119. if length(method.code.getParameterTypes)=length(args) then
  120. result:=JLDouble(method.code.invoke(method.data,args)).doubleValue
  121. else
  122. result:=JLDouble(method.code.invoke(method.data,getClassProcArgs(args))).doubleValue
  123. end;
  124. function FpcBaseProcVarType.invokeObjectFunc(const args: array of jlobject): jlobject;
  125. begin
  126. if length(method.code.getParameterTypes)=length(args) then
  127. result:=method.code.invoke(method.data,args)
  128. else
  129. result:=method.code.invoke(method.data,getClassProcArgs(args))
  130. end;
  131. function FpcBaseNestedProcVarType.getNestedArgs(const args: array of jlobject): TJLObjectDynArray;
  132. var
  133. arglen: longint;
  134. begin
  135. { add the parentfp struct pointer as last argument (delphi nested cc
  136. "calling convention") }
  137. arglen:=length(args);
  138. setlength(result,arglen+1);
  139. JLSystem.ArrayCopy(JLObject(@args),0,JLObject(result),0,arglen);
  140. result[arglen]:=nestedfpstruct;
  141. end;
  142. constructor FpcBaseNestedProcVarType.create(inst, context: jlobject; const methodName: unicodestring; const argTypes: array of JLClass);
  143. begin
  144. inherited create(inst,methodName,argTypes);
  145. nestedfpstruct:=context;
  146. end;
  147. procedure FpcBaseNestedProcVarType.fpcDeepCopy(result: FpcBaseProcVarType);
  148. begin
  149. inherited fpcDeepCopy(result);
  150. FpcBaseNestedProcVarType(result).nestedfpstruct:=nestedfpstruct;
  151. end;
  152. function FpcBaseNestedProcVarType.clone: JLObject;
  153. begin
  154. result:=inherited;
  155. FpcBaseNestedProcVarType(result).nestedfpstruct:=nestedfpstruct;
  156. end;
  157. procedure FpcBaseNestedProcVarType.invokeProc(const args: array of jlobject);
  158. begin
  159. inherited invokeProc(getNestedArgs(args));
  160. end;
  161. function FpcBaseNestedProcVarType.invokeBooleanFunc(const args: array of jlobject): jboolean;
  162. begin
  163. result:=inherited invokeBooleanFunc(getNestedArgs(args));
  164. end;
  165. function FpcBaseNestedProcVarType.invokeCharFunc(const args: array of jlobject): jchar;
  166. begin
  167. result:=inherited invokeCharFunc(getNestedArgs(args));
  168. end;
  169. function FpcBaseNestedProcVarType.invokeByteFunc(const args: array of jlobject): jbyte;
  170. begin
  171. result:=inherited invokeByteFunc(getNestedArgs(args));
  172. end;
  173. function FpcBaseNestedProcVarType.invokeShortFunc(const args: array of jlobject): jshort;
  174. begin
  175. result:=inherited invokeShortFunc(getNestedArgs(args));
  176. end;
  177. function FpcBaseNestedProcVarType.invokeIntFunc(const args: array of jlobject): jint;
  178. begin
  179. result:=inherited invokeIntFunc(getNestedArgs(args));
  180. end;
  181. function FpcBaseNestedProcVarType.invokeLongFunc(const args: array of jlobject): jlong;
  182. begin
  183. result:=inherited invokeLongFunc(getNestedArgs(args));
  184. end;
  185. function FpcBaseNestedProcVarType.invokeSingleFunc(const args: array of jlobject): jfloat;
  186. begin
  187. result:=inherited invokeSingleFunc(getNestedArgs(args));
  188. end;
  189. function FpcBaseNestedProcVarType.invokeDoubleFunc(const args: array of jlobject): jdouble;
  190. begin
  191. result:=inherited invokeDoubleFunc(getNestedArgs(args));
  192. end;
  193. function FpcBaseNestedProcVarType.invokeObjectFunc(const args: array of jlobject): jlobject;
  194. begin
  195. result:=inherited invokeObjectFunc(getNestedArgs(args));
  196. end;