v8js_variables.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2010 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. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: v8js_variables.cc 306926 2010-12-31 14:15:54Z felipe $ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. extern "C" {
  23. #include "php.h"
  24. }
  25. #include "php_v8js_macros.h"
  26. #include <v8.h>
  27. static v8::Handle<v8::Value> php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::AccessorInfo &info) /* {{{ */
  28. {
  29. v8::String::Utf8Value variable_name(info.Data()->ToString());
  30. const char *variable_name_string = ToCString(variable_name);
  31. uint variable_name_string_len = strlen(variable_name_string);
  32. zval **variable;
  33. TSRMLS_FETCH();
  34. zend_is_auto_global(variable_name_string, variable_name_string_len TSRMLS_CC);
  35. if (zend_hash_find(&EG(symbol_table), variable_name_string, variable_name_string_len + 1, (void **) &variable) == SUCCESS) {
  36. return zval_to_v8js(*variable TSRMLS_CC);
  37. }
  38. return v8::Undefined();
  39. }
  40. /* }}} */
  41. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate> php_obj, zval *array TSRMLS_DC) /* {{{ */
  42. {
  43. char *property_name;
  44. uint property_name_len;
  45. ulong index;
  46. zval **item;
  47. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
  48. zend_hash_get_current_data(Z_ARRVAL_P(array), (void **) &item) != FAILURE;
  49. zend_hash_move_forward(Z_ARRVAL_P(array))
  50. ) {
  51. switch (Z_TYPE_PP(item))
  52. {
  53. /*
  54. case IS_OBJECT:
  55. case IS_ARRAY:
  56. */
  57. case IS_STRING:
  58. break;
  59. default:
  60. continue; /* Ignore invalid values */
  61. }
  62. if (zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &property_name, &property_name_len, &index, 0, NULL) != HASH_KEY_IS_STRING) {
  63. continue; /* Ignore invalid property names */
  64. }
  65. /* Set the variable fetch callback for given symbol on named property */
  66. 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);
  67. }
  68. }
  69. /* }}} */
  70. /*
  71. * Local variables:
  72. * tab-width: 4
  73. * c-basic-offset: 4
  74. * End:
  75. * vim600: noet sw=4 ts=4 fdm=marker
  76. * vim<600: noet sw=4 ts=4
  77. */