v8js_generator_export.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. v8::Local<v8::Value> v8js_wrap_generator(v8::Isolate *isolate, v8::Local<v8::Value> wrapped_object) /* {{{ */
  18. {
  19. v8::Local<v8::Value> result;
  20. assert(!wrapped_object.IsEmpty());
  21. assert(wrapped_object->IsObject());
  22. v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
  23. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
  24. v8::TryCatch try_catch(isolate);
  25. v8::Local<v8::String> source = V8JS_SYM("(\
  26. function(wrapped_object) { \
  27. return (function*() { \
  28. for(;;) { \
  29. if(!wrapped_object.valid()) { \
  30. return; \
  31. } \
  32. yield wrapped_object.current(); \
  33. wrapped_object.next(); \
  34. } \
  35. })(); \
  36. })");
  37. v8::MaybeLocal<v8::Script> script = v8::Script::Compile(v8_context, source);
  38. if(script.IsEmpty()) {
  39. zend_error(E_ERROR, "Failed to compile Generator object wrapper");
  40. return result;
  41. }
  42. v8::MaybeLocal<v8::Value> wrapper_fn_val = script.ToLocalChecked()->Run(v8_context);
  43. if(wrapper_fn_val.IsEmpty() || !wrapper_fn_val.ToLocalChecked()->IsFunction()) {
  44. zend_error(E_ERROR, "Failed to create Generator object wrapper function");
  45. return result;
  46. }
  47. v8::Local<v8::Function> wrapper_fn = v8::Local<v8::Function>::Cast(wrapper_fn_val.ToLocalChecked());
  48. v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>)));
  49. new(&jsArgv[0]) v8::Local<v8::Value>;
  50. jsArgv[0] = v8::Local<v8::Value>::New(isolate, wrapped_object);
  51. wrapper_fn->Call(v8_context, V8JS_GLOBAL(isolate), 1, jsArgv).ToLocal(&result);
  52. return result;
  53. }
  54. /* }}} */
  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. */