v8js_variables.cc 3.1 KB

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