v8js_variables.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.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. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Jani Taskinen <[email protected]> |
  16. | Author: Patrick Reilly <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id:$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. extern "C" {
  24. #include "php.h"
  25. }
  26. #include "php_v8js_macros.h"
  27. #include <v8.h>
  28. static v8::Handle<v8::Value> php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::AccessorInfo &info) /* {{{ */
  29. {
  30. v8::String::Utf8Value variable_name(info.Data()->ToString());
  31. const char *variable_name_string = ToCString(variable_name);
  32. uint variable_name_string_len = strlen(variable_name_string);
  33. zval **variable;
  34. TSRMLS_FETCH();
  35. zend_is_auto_global(variable_name_string, variable_name_string_len TSRMLS_CC);
  36. if (zend_hash_find(&EG(symbol_table), variable_name_string, variable_name_string_len + 1, (void **) &variable) == SUCCESS) {
  37. return zval_to_v8js(*variable TSRMLS_CC);
  38. }
  39. return v8::Undefined();
  40. }
  41. /* }}} */
  42. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate> php_obj, zval *array TSRMLS_DC) /* {{{ */
  43. {
  44. char *property_name;
  45. uint property_name_len;
  46. ulong index;
  47. zval **item;
  48. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
  49. zend_hash_get_current_data(Z_ARRVAL_P(array), (void **) &item) != FAILURE;
  50. zend_hash_move_forward(Z_ARRVAL_P(array))
  51. ) {
  52. switch (Z_TYPE_PP(item))
  53. {
  54. /*
  55. case IS_OBJECT:
  56. case IS_ARRAY:
  57. */
  58. case IS_STRING:
  59. break;
  60. default:
  61. continue; /* Ignore invalid values */
  62. }
  63. if (zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &property_name, &property_name_len, &index, 0, NULL) != HASH_KEY_IS_STRING) {
  64. continue; /* Ignore invalid property names */
  65. }
  66. /* Set the variable fetch callback for given symbol on named property */
  67. php_obj->SetAccessor(V8JS_STRL(property_name, property_name_len - 1), php_v8js_fetch_php_variable, NULL, V8JS_STRL(Z_STRVAL_PP(item), Z_STRLEN_PP(item)), v8::PROHIBITS_OVERWRITING, v8::ReadOnly);
  68. }
  69. }
  70. /* }}} */
  71. /*
  72. * Local variables:
  73. * tab-width: 4
  74. * c-basic-offset: 4
  75. * End:
  76. * vim600: noet sw=4 ts=4 fdm=marker
  77. * vim<600: noet sw=4 ts=4
  78. */