v8js_variables.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. +----------------------------------------------------------------------+
  12. */
  13. /* $Id:$ */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. extern "C" {
  18. #include "php.h"
  19. }
  20. #include "php_v8js_macros.h"
  21. #include <v8.h>
  22. #include <string>
  23. struct php_v8js_accessor_ctx
  24. {
  25. char *variable_name_string;
  26. uint variable_name_string_len;
  27. v8::Isolate *isolate;
  28. };
  29. static void php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  30. {
  31. v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data());
  32. php_v8js_accessor_ctx *ctx = static_cast<php_v8js_accessor_ctx *>(data->Value());
  33. zval **variable;
  34. TSRMLS_FETCH();
  35. zend_is_auto_global(ctx->variable_name_string, ctx->variable_name_string_len TSRMLS_CC);
  36. if (zend_hash_find(&EG(symbol_table), ctx->variable_name_string, ctx->variable_name_string_len + 1, (void **) &variable) == SUCCESS) {
  37. info.GetReturnValue().Set(zval_to_v8js(*variable, ctx->isolate TSRMLS_CC));
  38. return;
  39. }
  40. }
  41. /* }}} */
  42. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate> php_obj, zval *array, v8::Isolate *isolate 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. // Create context to store accessor data
  67. php_v8js_accessor_ctx *ctx = (php_v8js_accessor_ctx *)emalloc(sizeof(php_v8js_accessor_ctx));
  68. ctx->variable_name_string = estrdup(Z_STRVAL_PP(item));
  69. ctx->variable_name_string_len = Z_STRLEN_PP(item);
  70. ctx->isolate = isolate;
  71. /* Set the variable fetch callback for given symbol on named property */
  72. 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);
  73. }
  74. }
  75. /* }}} */
  76. /*
  77. * Local variables:
  78. * tab-width: 4
  79. * c-basic-offset: 4
  80. * End:
  81. * vim600: noet sw=4 ts=4 fdm=marker
  82. * vim<600: noet sw=4 ts=4
  83. */