v8js_variables.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <string>
  29. struct php_v8js_accessor_ctx
  30. {
  31. char *variable_name_string;
  32. uint variable_name_string_len;
  33. v8::Isolate *isolate;
  34. };
  35. static v8::Handle<v8::Value> php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::AccessorInfo &info) /* {{{ */
  36. {
  37. v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data());
  38. php_v8js_accessor_ctx *ctx = static_cast<php_v8js_accessor_ctx *>(data->Value());
  39. zval **variable;
  40. TSRMLS_FETCH();
  41. zend_is_auto_global(ctx->variable_name_string, ctx->variable_name_string_len TSRMLS_CC);
  42. if (zend_hash_find(&EG(symbol_table), ctx->variable_name_string, ctx->variable_name_string_len + 1, (void **) &variable) == SUCCESS) {
  43. return zval_to_v8js(*variable, ctx->isolate TSRMLS_CC);
  44. }
  45. return v8::Undefined();
  46. }
  47. /* }}} */
  48. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate> php_obj, zval *array, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  49. {
  50. char *property_name;
  51. uint property_name_len;
  52. ulong index;
  53. zval **item;
  54. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(array));
  55. zend_hash_get_current_data(Z_ARRVAL_P(array), (void **) &item) != FAILURE;
  56. zend_hash_move_forward(Z_ARRVAL_P(array))
  57. ) {
  58. switch (Z_TYPE_PP(item))
  59. {
  60. /*
  61. case IS_OBJECT:
  62. case IS_ARRAY:
  63. */
  64. case IS_STRING:
  65. break;
  66. default:
  67. continue; /* Ignore invalid values */
  68. }
  69. if (zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &property_name, &property_name_len, &index, 0, NULL) != HASH_KEY_IS_STRING) {
  70. continue; /* Ignore invalid property names */
  71. }
  72. // Create context to store accessor data
  73. php_v8js_accessor_ctx *ctx = (php_v8js_accessor_ctx *)emalloc(sizeof(php_v8js_accessor_ctx));
  74. ctx->variable_name_string = estrdup(Z_STRVAL_PP(item));
  75. ctx->variable_name_string_len = Z_STRLEN_PP(item);
  76. ctx->isolate = isolate;
  77. /* Set the variable fetch callback for given symbol on named property */
  78. php_obj->SetAccessor(V8JS_STRL(property_name, property_name_len - 1), php_v8js_fetch_php_variable, NULL, v8::External::New(ctx), v8::PROHIBITS_OVERWRITING, v8::ReadOnly);
  79. }
  80. }
  81. /* }}} */
  82. /*
  83. * Local variables:
  84. * tab-width: 4
  85. * c-basic-offset: 4
  86. * End:
  87. * vim600: noet sw=4 ts=4 fdm=marker
  88. * vim<600: noet sw=4 ts=4
  89. */