v8js_generator_export.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Stefan Siegl <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. #include <assert.h>
  16. #include "php_v8js_macros.h"
  17. #ifdef V8JS_GENERATOR_EXPORT_SUPPORT
  18. v8::Local<v8::Value> v8js_wrap_generator(v8::Isolate *isolate, v8::Local<v8::Value> wrapped_object) /* {{{ */
  19. {
  20. v8::Local<v8::Value> result;
  21. assert(!wrapped_object.IsEmpty());
  22. assert(wrapped_object->IsObject());
  23. v8::TryCatch try_catch;
  24. v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "(\
  25. function(wrapped_object) { \
  26. return (function*() { \
  27. for(;;) { \
  28. if(!wrapped_object.valid()) { \
  29. return; \
  30. } \
  31. yield wrapped_object.current(); \
  32. wrapped_object.next(); \
  33. } \
  34. })(); \
  35. })");
  36. v8::Local<v8::Script> script = v8::Script::Compile(source);
  37. if(script.IsEmpty()) {
  38. zend_error(E_ERROR, "Failed to compile Generator object wrapper");
  39. return result;
  40. }
  41. v8::Local<v8::Value> wrapper_fn_val = script->Run();
  42. if(wrapper_fn_val.IsEmpty() || !wrapper_fn_val->IsFunction()) {
  43. zend_error(E_ERROR, "Failed to create Generator object wrapper function");
  44. return result;
  45. }
  46. v8::Local<v8::Function> wrapper_fn = v8::Local<v8::Function>::Cast(wrapper_fn_val);
  47. v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>)));
  48. new(&jsArgv[0]) v8::Local<v8::Value>;
  49. jsArgv[0] = v8::Local<v8::Value>::New(isolate, wrapped_object);
  50. result = wrapper_fn->Call(V8JS_GLOBAL(isolate), 1, jsArgv);
  51. return result;
  52. }
  53. /* }}} */
  54. #endif /* V8JS_GENERATOR_EXPORT_SUPPORT */
  55. /*
  56. * Local variables:
  57. * tab-width: 4
  58. * c-basic-offset: 4
  59. * End:
  60. * vim600: noet sw=4 ts=4 fdm=marker
  61. * vim<600: noet sw=4 ts=4
  62. */