You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

771 lines
21 KiB

25 years ago
26 years ago
25 years ago
26 years ago
25 years ago
26 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
26 years ago
25 years ago
25 years ago
26 years ago
25 years ago
25 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
26 years ago
25 years ago
26 years ago
25 years ago
25 years ago
25 years ago
26 years ago
26 years ago
26 years ago
25 years ago
25 years ago
25 years ago
26 years ago
26 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2001 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sam Ruby (rubys@us.ibm.com) |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. /*
  20. * This module implements Zend OO syntax overloading support for Java
  21. * components using JNI and reflection.
  22. */
  23. #include "php.h"
  24. #include "zend_compile.h"
  25. #include "php_ini.h"
  26. #include "php_globals.h"
  27. #ifdef PHP_WIN32
  28. #include "win32/winutil.h"
  29. #define DL_ERROR php_win_err()
  30. #else
  31. #define DL_ERROR dlerror()
  32. #endif
  33. #include <jni.h>
  34. #include <stdio.h>
  35. #define IS_EXCEPTION 86
  36. /***************************************************************************/
  37. #ifndef KAFFE
  38. #ifndef JNI_11
  39. #ifndef JNI_12
  40. #ifdef JNI_VERSION_1_2
  41. #define JNI_12
  42. #else
  43. #define JNI_11
  44. #endif
  45. #endif
  46. #endif
  47. #endif
  48. #ifdef PHP_WIN32
  49. #ifdef JNI_12
  50. #define JAVALIB "jvm.dll"
  51. #else
  52. #define JAVALIB "javai.dll"
  53. #endif
  54. #else
  55. #endif
  56. /***************************************************************************/
  57. static int le_jobject = 0;
  58. static char *classpath = 0;
  59. static char *libpath = 0;
  60. static char *javahome = 0;
  61. static char *javalib = 0;
  62. static int iniUpdated = 0;
  63. static void *dl_handle = 0;
  64. ZEND_BEGIN_MODULE_GLOBALS(java)
  65. JavaVM *jvm;
  66. JNIEnv *jenv;
  67. jobject php_reflect;
  68. jclass reflect_class;
  69. ZEND_END_MODULE_GLOBALS(java)
  70. #ifdef ZTS
  71. # define JG(v) TSRMG(java_globals_id, zend_java_globals *, v)
  72. #else
  73. # define JG(v) (java_globals.v)
  74. #endif
  75. ZEND_DECLARE_MODULE_GLOBALS(java)
  76. static zend_class_entry java_class_entry;
  77. static PHP_INI_MH(OnIniUpdate)
  78. {
  79. if (new_value) *(char**)mh_arg1 = new_value;
  80. iniUpdated=1;
  81. return SUCCESS;
  82. }
  83. PHP_INI_BEGIN()
  84. PHP_INI_ENTRY1("java.class.path", NULL, PHP_INI_ALL, OnIniUpdate, &classpath)
  85. #ifndef JNI_11
  86. PHP_INI_ENTRY1("java.home", NULL, PHP_INI_ALL, OnIniUpdate, &javahome)
  87. PHP_INI_ENTRY1("java.library.path", NULL, PHP_INI_ALL, OnIniUpdate, &libpath)
  88. #endif
  89. #ifdef JAVALIB
  90. PHP_INI_ENTRY1("java.library", JAVALIB, PHP_INI_ALL, OnIniUpdate, &javalib)
  91. #else
  92. PHP_INI_ENTRY1("java.library", NULL, PHP_INI_ALL, OnIniUpdate, &javalib)
  93. #endif
  94. PHP_INI_END()
  95. /***************************************************************************/
  96. /*
  97. * Destroy a Java Virtual Machine.
  98. */
  99. void jvm_destroy(TSRMLS_D)
  100. {
  101. if (JG(php_reflect)) (*JG(jenv))->DeleteGlobalRef(JG(jenv), JG(php_reflect));
  102. if (JG(jvm)) {
  103. (*JG(jvm))->DetachCurrentThread(JG(jvm));
  104. (*JG(jvm))->DestroyJavaVM(JG(jvm));
  105. JG(jvm) = 0;
  106. }
  107. if (dl_handle) DL_UNLOAD(dl_handle);
  108. JG(php_reflect) = 0;
  109. JG(jenv) = 0;
  110. }
  111. /*
  112. * Create a Java Virtual Machine.
  113. * - class.path, home, and library.path are read out of the INI file
  114. * - appropriate (pre 1.1, JDK 1.1, and JDK 1.2) initialization is performed
  115. * - net.php.reflect class file is located
  116. */
  117. #ifdef JNI_12
  118. static void addJVMOption(JavaVMInitArgs *vm_args, char *name, char *value)
  119. {
  120. char *option = (char*) malloc(strlen(name) + strlen(value) + 1);
  121. strcpy(option, name);
  122. strcat(option, value);
  123. vm_args->options[vm_args->nOptions++].optionString = option;
  124. }
  125. #endif
  126. static int jvm_create(TSRMLS_D)
  127. {
  128. int rc;
  129. jobject local_php_reflect;
  130. jthrowable error;
  131. jint (JNICALL *JNI_CreateVM)(const void*, const void*, void*);
  132. #ifndef JNI_12
  133. jint (JNICALL *JNI_DefaultArgs)(void*);
  134. #endif
  135. #ifdef JNI_11
  136. JDK1_1InitArgs vm_args;
  137. #else
  138. JavaVMInitArgs vm_args;
  139. #ifdef JNI_12
  140. JavaVMOption options[3];
  141. #endif
  142. #endif
  143. iniUpdated=0;
  144. if (javalib) {
  145. dl_handle = DL_LOAD(javalib);
  146. if (!dl_handle) {
  147. php_error(E_ERROR, "Unable to load Java Library %s, error: %s",
  148. javalib, DL_ERROR);
  149. return -1;
  150. }
  151. }
  152. #ifndef JAVALIB
  153. if (!dl_handle)
  154. JNI_CreateVM = &JNI_CreateJavaVM;
  155. else
  156. #endif
  157. JNI_CreateVM = (jint (JNICALL *)(const void*, const void*, void*))
  158. DL_FETCH_SYMBOL(dl_handle, "JNI_CreateJavaVM");
  159. if (!JNI_CreateVM) {
  160. php_error(E_ERROR, "Unable to locate CreateJavaVM function");
  161. return -1;
  162. }
  163. #ifdef JNI_12
  164. vm_args.version = JNI_VERSION_1_2;
  165. vm_args.ignoreUnrecognized = JNI_FALSE;
  166. vm_args.options = options;
  167. vm_args.nOptions = 0;
  168. if (classpath) addJVMOption(&vm_args, "-Djava.class.path=", classpath);
  169. if (javahome) addJVMOption(&vm_args, "-Djava.home=", javahome);
  170. if (libpath) addJVMOption(&vm_args, "-Djava.library.path=", libpath);
  171. #else
  172. #ifndef JAVALIB
  173. if (!dl_handle)
  174. JNI_DefaultArgs = &JNI_GetDefaultJavaVMInitArgs;
  175. else
  176. #endif
  177. JNI_DefaultArgs = (jint (JNICALL *)(void*))
  178. DL_FETCH_SYMBOL(dl_handle, "JNI_GetDefaultJavaVMInitArgs");
  179. if (!JNI_DefaultArgs) {
  180. php_error(E_ERROR, "Unable to locate GetDefaultJavaVMInitArgs function");
  181. return -1;
  182. }
  183. vm_args.version=0x00010001;
  184. (*JNI_DefaultArgs)(&vm_args);
  185. if (!classpath) classpath = "";
  186. vm_args.classpath = classpath;
  187. #ifdef KAFFE
  188. vm_args.classhome = javahome;
  189. vm_args.libraryhome = libpath;
  190. #endif
  191. #endif
  192. rc = (*JNI_CreateVM)(&JG(jvm), &JG(jenv), &vm_args);
  193. if (rc) {
  194. php_error(E_ERROR, "Unable to create Java Virtual Machine");
  195. return rc;
  196. }
  197. JG(reflect_class) = (*JG(jenv))->FindClass(JG(jenv), "net/php/reflect");
  198. error = (*JG(jenv))->ExceptionOccurred(JG(jenv));
  199. if (error) {
  200. jclass errClass;
  201. jmethodID toString;
  202. jobject errString;
  203. const char *errAsUTF;
  204. jboolean isCopy;
  205. JNIEnv *jenv = JG(jenv);
  206. (*jenv)->ExceptionClear(jenv);
  207. errClass = (*jenv)->GetObjectClass(jenv, error);
  208. toString = (*jenv)->GetMethodID(jenv, errClass, "toString",
  209. "()Ljava/lang/String;");
  210. errString = (*jenv)->CallObjectMethod(jenv, error, toString);
  211. errAsUTF = (*jenv)->GetStringUTFChars(jenv, errString, &isCopy);
  212. php_error(E_ERROR, "%s", errAsUTF);
  213. if (isCopy) (*jenv)->ReleaseStringUTFChars(jenv, error, errAsUTF);
  214. jvm_destroy(TSRMLS_C);
  215. return -1;
  216. }
  217. local_php_reflect = (*JG(jenv))->AllocObject(JG(jenv), JG(reflect_class));
  218. JG(php_reflect) = (*JG(jenv))->NewGlobalRef(JG(jenv), local_php_reflect);
  219. return rc;
  220. }
  221. /***************************************************************************/
  222. static jobject _java_makeObject(pval* arg TSRMLS_DC)
  223. {
  224. JNIEnv *jenv = JG(jenv);
  225. jobject result;
  226. pval **handle;
  227. int type;
  228. jmethodID makeArg;
  229. jclass hashClass;
  230. switch (Z_TYPE_P(arg)) {
  231. case IS_STRING:
  232. result=(*jenv)->NewByteArray(jenv, Z_STRLEN_P(arg));
  233. (*jenv)->SetByteArrayRegion(jenv, (jbyteArray)result, 0,
  234. Z_STRLEN_P(arg), Z_STRVAL_P(arg));
  235. break;
  236. case IS_OBJECT:
  237. zend_hash_index_find(Z_OBJPROP_P(arg), 0, (void*)&handle);
  238. result = zend_list_find(Z_LVAL_PP(handle), &type);
  239. break;
  240. case IS_BOOL:
  241. makeArg = (*jenv)->GetMethodID(jenv, JG(reflect_class), "MakeArg",
  242. "(Z)Ljava/lang/Object;");
  243. result = (*jenv)->CallObjectMethod(jenv, JG(php_reflect), makeArg,
  244. (jboolean)(Z_LVAL_P(arg)));
  245. break;
  246. case IS_LONG:
  247. makeArg = (*jenv)->GetMethodID(jenv, JG(reflect_class), "MakeArg",
  248. "(J)Ljava/lang/Object;");
  249. result = (*jenv)->CallObjectMethod(jenv, JG(php_reflect), makeArg,
  250. (jlong)(Z_LVAL_P(arg)));
  251. break;
  252. case IS_DOUBLE:
  253. makeArg = (*jenv)->GetMethodID(jenv, JG(reflect_class), "MakeArg",
  254. "(D)Ljava/lang/Object;");
  255. result = (*jenv)->CallObjectMethod(jenv, JG(php_reflect), makeArg,
  256. (jdouble)(Z_DVAL_P(arg)));
  257. break;
  258. case IS_ARRAY:
  259. {
  260. jobject jkey, jval;
  261. zval **value;
  262. zval key;
  263. char *string_key;
  264. ulong num_key;
  265. jobject jold;
  266. jmethodID put, init;
  267. hashClass = (*jenv)->FindClass(jenv, "java/util/Hashtable");
  268. init = (*jenv)->GetMethodID(jenv, hashClass, "<init>", "()V");
  269. result = (*jenv)->NewObject(jenv, hashClass, init);
  270. put = (*jenv)->GetMethodID(jenv, hashClass, "put",
  271. "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  272. /* Iterate through hash */
  273. zend_hash_internal_pointer_reset(Z_ARRVAL_P(arg));
  274. while(zend_hash_get_current_data(Z_ARRVAL_P(arg), (void**)&value) == SUCCESS) {
  275. jval = _java_makeObject(*value TSRMLS_CC);
  276. switch (zend_hash_get_current_key(Z_ARRVAL_P(arg), &string_key, &num_key, 0)) {
  277. case HASH_KEY_IS_STRING:
  278. Z_TYPE(key) = IS_STRING;
  279. Z_STRVAL(key) = string_key;
  280. Z_STRLEN(key) = strlen(string_key);
  281. jkey = _java_makeObject(&key TSRMLS_CC);
  282. break;
  283. case HASH_KEY_IS_LONG:
  284. Z_TYPE(key) = IS_LONG;
  285. Z_LVAL(key) = num_key;
  286. jkey = _java_makeObject(&key TSRMLS_CC);
  287. break;
  288. default: /* HASH_KEY_NON_EXISTANT */
  289. jkey = 0;
  290. }
  291. jold = (*jenv)->CallObjectMethod(jenv, result, put, jkey, jval);
  292. if (Z_TYPE_PP(value) != IS_OBJECT) (*jenv)->DeleteLocalRef(jenv, jval);
  293. zend_hash_move_forward(Z_ARRVAL_P(arg));
  294. }
  295. break;
  296. }
  297. default:
  298. result=0;
  299. }
  300. return result;
  301. }
  302. /***************************************************************************/
  303. static jobjectArray _java_makeArray(int argc, pval** argv TSRMLS_DC)
  304. {
  305. JNIEnv *jenv = JG(jenv);
  306. jclass objectClass = (*jenv)->FindClass(jenv, "java/lang/Object");
  307. jobjectArray result = (*jenv)->NewObjectArray(jenv, argc, objectClass, 0);
  308. jobject arg;
  309. int i;
  310. for (i=0; i<argc; i++) {
  311. arg = _java_makeObject(argv[i] TSRMLS_CC);
  312. (*jenv)->SetObjectArrayElement(jenv, result, i, arg);
  313. if (Z_TYPE_P(argv[i]) != IS_OBJECT) (*jenv)->DeleteLocalRef(jenv, arg);
  314. }
  315. return result;
  316. }
  317. static int checkError(pval *value)
  318. {
  319. if (Z_TYPE_P(value) == IS_EXCEPTION) {
  320. php_error(E_WARNING, "%s", Z_STRVAL_P(value));
  321. efree(Z_STRVAL_P(value));
  322. ZVAL_FALSE(value);
  323. return 1;
  324. };
  325. return 0;
  326. }
  327. /***************************************************************************/
  328. /*
  329. * Invoke a method on an object. If method name is "java", create a new
  330. * object instead.
  331. */
  332. void java_call_function_handler(INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference)
  333. {
  334. JNIEnv *jenv;
  335. pval *object = property_reference->object;
  336. zend_overloaded_element *function_name = (zend_overloaded_element *)
  337. property_reference->elements_list->tail->data;
  338. int arg_count = ZEND_NUM_ARGS();
  339. jlong result = 0;
  340. pval **arguments = (pval **) emalloc(sizeof(pval *)*arg_count);
  341. getParametersArray(ht, arg_count, arguments);
  342. if (iniUpdated && JG(jenv)) jvm_destroy(TSRMLS_C);
  343. if (!JG(jenv)) jvm_create(TSRMLS_C);
  344. if (!JG(jenv)) return;
  345. jenv = JG(jenv);
  346. if (!strcmp("java", Z_STRVAL(function_name->element))) {
  347. /* construct a Java object:
  348. First argument is the class name. Any additional arguments will
  349. be treated as constructor parameters. */
  350. jmethodID co = (*jenv)->GetMethodID(jenv, JG(reflect_class), "CreateObject",
  351. "(Ljava/lang/String;[Ljava/lang/Object;J)V");
  352. jstring className;
  353. result = (jlong)(long)object;
  354. if (ZEND_NUM_ARGS() < 1) {
  355. php_error(E_ERROR, "Missing classname in new Java() call");
  356. return;
  357. }
  358. className=(*jenv)->NewStringUTF(jenv, Z_STRVAL_P(arguments[0]));
  359. (*jenv)->CallVoidMethod(jenv, JG(php_reflect), co,
  360. className, _java_makeArray(arg_count-1, arguments+1 TSRMLS_CC), result);
  361. (*jenv)->DeleteLocalRef(jenv, className);
  362. } else {
  363. pval **handle;
  364. int type;
  365. jobject obj;
  366. jstring method;
  367. /* invoke a method on the given object */
  368. jmethodID invoke = (*jenv)->GetMethodID(jenv, JG(reflect_class), "Invoke",
  369. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  370. zend_hash_index_find(Z_OBJPROP_P(object), 0, (void**) &handle);
  371. obj = zend_list_find(Z_LVAL_PP(handle), &type);
  372. method = (*jenv)->NewStringUTF(jenv, Z_STRVAL(function_name->element));
  373. result = (jlong)(long)return_value;
  374. (*jenv)->CallVoidMethod(jenv, JG(php_reflect), invoke,
  375. obj, method, _java_makeArray(arg_count, arguments TSRMLS_CC), result);
  376. (*jenv)->DeleteLocalRef(jenv, method);
  377. }
  378. efree(arguments);
  379. pval_destructor(&function_name->element);
  380. checkError((pval*)(long)result);
  381. }
  382. /***************************************************************************/
  383. PHP_FUNCTION(java_last_exception_get)
  384. {
  385. jlong result = 0;
  386. jmethodID lastEx;
  387. if (ZEND_NUM_ARGS()!=0) WRONG_PARAM_COUNT;
  388. result = (jlong)(long)return_value;
  389. lastEx = (*JG(jenv))->GetMethodID(JG(jenv), JG(reflect_class),
  390. "lastException", "(J)V");
  391. (*JG(jenv))->CallVoidMethod(JG(jenv), JG(php_reflect), lastEx, result);
  392. }
  393. /***************************************************************************/
  394. PHP_FUNCTION(java_last_exception_clear)
  395. {
  396. jlong result = 0;
  397. jmethodID clearEx;
  398. if (ZEND_NUM_ARGS()!=0) WRONG_PARAM_COUNT;
  399. result = (jlong)(long)return_value;
  400. clearEx = (*JG(jenv))->GetMethodID(JG(jenv), JG(reflect_class),
  401. "clearException", "()V");
  402. (*JG(jenv))->CallVoidMethod(JG(jenv), JG(php_reflect), clearEx);
  403. }
  404. /***************************************************************************/
  405. static pval _java_getset_property
  406. (zend_property_reference *property_reference, jobjectArray value TSRMLS_DC)
  407. {
  408. pval presult;
  409. jlong result = 0;
  410. pval **pobject;
  411. jobject obj;
  412. int type;
  413. /* get the property name */
  414. zend_llist_element *element = property_reference->elements_list->head;
  415. zend_overloaded_element *property=(zend_overloaded_element *)element->data;
  416. jstring propName;
  417. JNIEnv *jenv;
  418. jenv = JG(jenv);
  419. propName = (*jenv)->NewStringUTF(jenv, Z_STRVAL(property->element));
  420. /* get the object */
  421. zend_hash_index_find(Z_OBJPROP_P(property_reference->object),
  422. 0, (void **) &pobject);
  423. obj = zend_list_find(Z_LVAL_PP(pobject), &type);
  424. result = (jlong)(long) &presult;
  425. Z_TYPE(presult) = IS_NULL;
  426. if (!obj || (type!=le_jobject)) {
  427. php_error(E_ERROR,
  428. "Attempt to access a Java property on a non-Java object");
  429. } else {
  430. /* invoke the method */
  431. jmethodID gsp = (*jenv)->GetMethodID(jenv, JG(reflect_class), "GetSetProp",
  432. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  433. (*jenv)->CallVoidMethod
  434. (jenv, JG(php_reflect), gsp, obj, propName, value, result);
  435. }
  436. (*jenv)->DeleteLocalRef(jenv, propName);
  437. pval_destructor(&property->element);
  438. return presult;
  439. }
  440. pval java_get_property_handler(zend_property_reference *property_reference)
  441. {
  442. pval presult;
  443. TSRMLS_FETCH();
  444. presult = _java_getset_property(property_reference, 0 TSRMLS_CC);
  445. checkError(&presult);
  446. return presult;
  447. }
  448. int java_set_property_handler(zend_property_reference *property_reference, pval *value)
  449. {
  450. pval presult;
  451. TSRMLS_FETCH();
  452. presult = _java_getset_property(property_reference, _java_makeArray(1, &value TSRMLS_CC) TSRMLS_CC);
  453. return checkError(&presult) ? FAILURE : SUCCESS;
  454. }
  455. /***************************************************************************/
  456. static void _php_java_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  457. {
  458. void *jobject = (void *)rsrc->ptr;
  459. if (JG(jenv)) (*JG(jenv))->DeleteGlobalRef(JG(jenv), jobject);
  460. }
  461. static void alloc_java_globals_ctor(zend_java_globals *java_globals TSRMLS_DC)
  462. {
  463. memset(java_globals, 0, sizeof(zend_java_globals));
  464. }
  465. PHP_MINIT_FUNCTION(java)
  466. {
  467. INIT_OVERLOADED_CLASS_ENTRY(java_class_entry, "java", NULL,
  468. java_call_function_handler,
  469. java_get_property_handler,
  470. java_set_property_handler);
  471. zend_register_internal_class(&java_class_entry TSRMLS_CC);
  472. le_jobject = zend_register_list_destructors_ex(_php_java_destructor, NULL, "java", module_number);
  473. REGISTER_INI_ENTRIES();
  474. if (!classpath) classpath = getenv("CLASSPATH");
  475. if (!libpath) {
  476. libpath=PG(extension_dir);
  477. }
  478. ZEND_INIT_MODULE_GLOBALS(java, alloc_java_globals_ctor, NULL);
  479. return SUCCESS;
  480. }
  481. PHP_MSHUTDOWN_FUNCTION(java)
  482. {
  483. UNREGISTER_INI_ENTRIES();
  484. if (JG(jvm)) jvm_destroy(TSRMLS_C);
  485. return SUCCESS;
  486. }
  487. function_entry java_functions[] = {
  488. PHP_FE(java_last_exception_get, NULL)
  489. PHP_FE(java_last_exception_clear, NULL)
  490. {NULL, NULL, NULL}
  491. };
  492. static PHP_MINFO_FUNCTION(java) {
  493. DISPLAY_INI_ENTRIES();
  494. }
  495. zend_module_entry java_module_entry = {
  496. "java",
  497. java_functions,
  498. PHP_MINIT(java),
  499. PHP_MSHUTDOWN(java),
  500. NULL,
  501. NULL,
  502. PHP_MINFO(java),
  503. STANDARD_MODULE_PROPERTIES
  504. };
  505. ZEND_GET_MODULE(java)
  506. /***************************************************************************/
  507. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromString
  508. (JNIEnv *jenv, jclass self, jlong result, jbyteArray jvalue)
  509. {
  510. jboolean isCopy;
  511. jbyte *value = (*jenv)->GetByteArrayElements(jenv, jvalue, &isCopy);
  512. pval *presult = (pval*)(long)result;
  513. Z_TYPE_P(presult)=IS_STRING;
  514. Z_STRLEN_P(presult)=(*jenv)->GetArrayLength(jenv, jvalue);
  515. Z_STRVAL_P(presult)=emalloc(Z_STRLEN_P(presult)+1);
  516. memcpy(Z_STRVAL_P(presult), value, Z_STRLEN_P(presult));
  517. Z_STRVAL_P(presult)[Z_STRLEN_P(presult)]=0;
  518. if (isCopy) (*jenv)->ReleaseByteArrayElements(jenv, jvalue, value, 0);
  519. }
  520. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromLong
  521. (JNIEnv *jenv, jclass self, jlong result, jlong value)
  522. {
  523. pval *presult = (pval*)(long)result;
  524. Z_TYPE_P(presult)=IS_LONG;
  525. Z_LVAL_P(presult)=(long)value;
  526. }
  527. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromDouble
  528. (JNIEnv *jenv, jclass self, jlong result, jdouble value)
  529. {
  530. pval *presult = (pval*)(long)result;
  531. Z_TYPE_P(presult)=IS_DOUBLE;
  532. Z_DVAL_P(presult)=value;
  533. }
  534. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromBoolean
  535. (JNIEnv *jenv, jclass self, jlong result, jboolean value)
  536. {
  537. pval *presult = (pval*)(long)result;
  538. Z_TYPE_P(presult)=IS_BOOL;
  539. Z_LVAL_P(presult)=value;
  540. }
  541. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromObject
  542. (JNIEnv *jenv, jclass self, jlong result, jobject value)
  543. {
  544. /* wrapper the java object in a pval object */
  545. pval *presult = (pval*)(long)result;
  546. pval *handle;
  547. TSRMLS_FETCH();
  548. if (Z_TYPE_P(presult) != IS_OBJECT) {
  549. object_init_ex(presult, &java_class_entry);
  550. presult->is_ref=1;
  551. presult->refcount=1;
  552. }
  553. ALLOC_ZVAL(handle);
  554. Z_TYPE_P(handle) = IS_LONG;
  555. Z_LVAL_P(handle) =
  556. zend_list_insert((*jenv)->NewGlobalRef(jenv, value), le_jobject);
  557. pval_copy_constructor(handle);
  558. INIT_PZVAL(handle);
  559. zend_hash_index_update(Z_OBJPROP_P(presult), 0, &handle, sizeof(pval *), NULL);
  560. }
  561. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromArray
  562. (JNIEnv *jenv, jclass self, jlong result)
  563. {
  564. array_init( (pval*)(long)result );
  565. }
  566. JNIEXPORT jlong JNICALL Java_net_php_reflect_nextElement
  567. (JNIEnv *jenv, jclass self, jlong array)
  568. {
  569. pval *result;
  570. pval *handle = (pval*)(long)array;
  571. ALLOC_ZVAL(result);
  572. zend_hash_next_index_insert(Z_ARRVAL_P(handle), &result, sizeof(zval *), NULL);
  573. return (jlong)(long)result;
  574. }
  575. JNIEXPORT jlong JNICALL Java_net_php_reflect_hashIndexUpdate
  576. (JNIEnv *jenv, jclass self, jlong array, jlong key)
  577. {
  578. pval *result;
  579. pval *handle = (pval*)(long)array;
  580. ALLOC_ZVAL(result);
  581. zend_hash_index_update(Z_ARRVAL_P(handle), (unsigned long)key,
  582. &result, sizeof(zval *), NULL);
  583. return (jlong)(long)result;
  584. }
  585. JNIEXPORT jlong JNICALL Java_net_php_reflect_hashUpdate
  586. (JNIEnv *jenv, jclass self, jlong array, jbyteArray key)
  587. {
  588. pval *result;
  589. pval pkey;
  590. pval *handle = (pval*)(long)array;
  591. ALLOC_ZVAL(result);
  592. Java_net_php_reflect_setResultFromString(jenv, self, (jlong)(long)&pkey, key);
  593. zend_hash_update(Z_ARRVAL_P(handle), Z_STRVAL(pkey), Z_STRLEN(pkey)+1,
  594. &result, sizeof(zval *), NULL);
  595. return (jlong)(long)result;
  596. }
  597. JNIEXPORT void JNICALL Java_net_php_reflect_setException
  598. (JNIEnv *jenv, jclass self, jlong result, jbyteArray value)
  599. {
  600. pval *presult = (pval*)(long)result;
  601. Java_net_php_reflect_setResultFromString(jenv, self, result, value);
  602. Z_TYPE_P(presult)=IS_EXCEPTION;
  603. }
  604. JNIEXPORT void JNICALL Java_net_php_reflect_setEnv
  605. (JNIEnv *newJenv, jclass self TSRMLS_DC)
  606. {
  607. jobject local_php_reflect;
  608. iniUpdated=0;
  609. JG(jenv)=newJenv;
  610. if (!self) self = (*JG(jenv))->FindClass(JG(jenv), "net/php/reflect");
  611. JG(reflect_class) = self;
  612. if (JG(php_reflect)) (*JG(jenv))->DeleteGlobalRef(JG(jenv), JG(php_reflect));
  613. local_php_reflect = (*JG(jenv))->AllocObject(JG(jenv), JG(reflect_class));
  614. JG(php_reflect) = (*JG(jenv))->NewGlobalRef(JG(jenv), local_php_reflect);
  615. }