v8js_variables.cc 3.4 KB

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