|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +#include "async-wrap.h" |
| 23 | +#include "async-wrap-inl.h" |
| 24 | +#include "env.h" |
| 25 | +#include "env-inl.h" |
| 26 | +#include "util.h" |
| 27 | +#include "util-inl.h" |
| 28 | + |
| 29 | +#include "v8.h" |
| 30 | + |
| 31 | +using v8::Function; |
| 32 | +using v8::Handle; |
| 33 | +using v8::Local; |
| 34 | +using v8::Object; |
| 35 | +using v8::TryCatch; |
| 36 | +using v8::Value; |
| 37 | + |
| 38 | +namespace node { |
| 39 | + |
| 40 | +Handle<Value> AsyncWrap::MakeDomainCallback(const Handle<Function> cb, |
| 41 | + int argc, |
| 42 | + Handle<Value>* argv) { |
| 43 | + CHECK(env()->context() == env()->isolate()->GetCurrentContext()); |
| 44 | + |
| 45 | + Local<Object> context = object(); |
| 46 | + Local<Object> process = env()->process_object(); |
| 47 | + Local<Value> domain_v = context->Get(env()->domain_string()); |
| 48 | + Local<Object> domain; |
| 49 | + |
| 50 | + TryCatch try_catch; |
| 51 | + try_catch.SetVerbose(true); |
| 52 | + |
| 53 | + bool has_domain = domain_v->IsObject(); |
| 54 | + if (has_domain) { |
| 55 | + domain = domain_v.As<Object>(); |
| 56 | + |
| 57 | + if (domain->Get(env()->disposed_string())->IsTrue()) |
| 58 | + return Undefined(env()->isolate()); |
| 59 | + |
| 60 | + Local<Function> enter = |
| 61 | + domain->Get(env()->enter_string()).As<Function>(); |
| 62 | + if (enter->IsFunction()) { |
| 63 | + enter->Call(domain, 0, nullptr); |
| 64 | + if (try_catch.HasCaught()) |
| 65 | + return Undefined(env()->isolate()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Local<Value> ret = cb->Call(context, argc, argv); |
| 70 | + |
| 71 | + if (try_catch.HasCaught()) { |
| 72 | + return Undefined(env()->isolate()); |
| 73 | + } |
| 74 | + |
| 75 | + if (has_domain) { |
| 76 | + Local<Function> exit = |
| 77 | + domain->Get(env()->exit_string()).As<Function>(); |
| 78 | + if (exit->IsFunction()) { |
| 79 | + exit->Call(domain, 0, nullptr); |
| 80 | + if (try_catch.HasCaught()) |
| 81 | + return Undefined(env()->isolate()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Environment::TickInfo* tick_info = env()->tick_info(); |
| 86 | + |
| 87 | + if (tick_info->in_tick()) { |
| 88 | + return ret; |
| 89 | + } |
| 90 | + |
| 91 | + if (tick_info->length() == 0) { |
| 92 | + env()->isolate()->RunMicrotasks(); |
| 93 | + } |
| 94 | + |
| 95 | + if (tick_info->length() == 0) { |
| 96 | + tick_info->set_index(0); |
| 97 | + return ret; |
| 98 | + } |
| 99 | + |
| 100 | + tick_info->set_in_tick(true); |
| 101 | + |
| 102 | + env()->tick_callback_function()->Call(process, 0, nullptr); |
| 103 | + |
| 104 | + tick_info->set_in_tick(false); |
| 105 | + |
| 106 | + if (try_catch.HasCaught()) { |
| 107 | + tick_info->set_last_threw(true); |
| 108 | + return Undefined(env()->isolate()); |
| 109 | + } |
| 110 | + |
| 111 | + return ret; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb, |
| 116 | + int argc, |
| 117 | + Handle<Value>* argv) { |
| 118 | + if (env()->using_domains()) |
| 119 | + return MakeDomainCallback(cb, argc, argv); |
| 120 | + |
| 121 | + CHECK(env()->context() == env()->isolate()->GetCurrentContext()); |
| 122 | + |
| 123 | + Local<Object> context = object(); |
| 124 | + Local<Object> process = env()->process_object(); |
| 125 | + |
| 126 | + TryCatch try_catch; |
| 127 | + try_catch.SetVerbose(true); |
| 128 | + |
| 129 | + Local<Value> ret = cb->Call(context, argc, argv); |
| 130 | + |
| 131 | + if (try_catch.HasCaught()) { |
| 132 | + return Undefined(env()->isolate()); |
| 133 | + } |
| 134 | + |
| 135 | + Environment::TickInfo* tick_info = env()->tick_info(); |
| 136 | + |
| 137 | + if (tick_info->in_tick()) { |
| 138 | + return ret; |
| 139 | + } |
| 140 | + |
| 141 | + if (tick_info->length() == 0) { |
| 142 | + env()->isolate()->RunMicrotasks(); |
| 143 | + } |
| 144 | + |
| 145 | + if (tick_info->length() == 0) { |
| 146 | + tick_info->set_index(0); |
| 147 | + return ret; |
| 148 | + } |
| 149 | + |
| 150 | + tick_info->set_in_tick(true); |
| 151 | + |
| 152 | + env()->tick_callback_function()->Call(process, 0, nullptr); |
| 153 | + |
| 154 | + tick_info->set_in_tick(false); |
| 155 | + |
| 156 | + if (try_catch.HasCaught()) { |
| 157 | + tick_info->set_last_threw(true); |
| 158 | + return Undefined(env()->isolate()); |
| 159 | + } |
| 160 | + |
| 161 | + return ret; |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace node |
0 commit comments