event_emitter_caller.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/api/event_emitter_caller.h"
  5. #include "atom/common/api/locker.h"
  6. #include "atom/common/node_includes.h"
  7. namespace mate {
  8. namespace internal {
  9. v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
  10. v8::Local<v8::Object> obj,
  11. const char* method,
  12. ValueVector* args) {
  13. // Perform microtask checkpoint after running JavaScript.
  14. v8::MicrotasksScope script_scope(isolate,
  15. v8::MicrotasksScope::kRunMicrotasks);
  16. // Use node::MakeCallback to call the callback, and it will also run pending
  17. // tasks in Node.js.
  18. v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
  19. isolate, obj, method, args->size(), &args->front(), {0, 0});
  20. // If the JS function throws an exception (doesn't return a value) the result
  21. // of MakeCallback will be empty and therefore ToLocal will be false, in this
  22. // case we need to return "false" as that indicates that the event emitter did
  23. // not handle the event
  24. v8::Local<v8::Value> localRet;
  25. if (ret.ToLocal(&localRet)) {
  26. return localRet;
  27. }
  28. return v8::Boolean::New(isolate, false);
  29. }
  30. } // namespace internal
  31. } // namespace mate