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.

786 lines
21 KiB

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