* ALooper callback. * Responsible for checking if the callback is still scheduled and entering the isolate to trigger it */
| 265 | * Responsible for checking if the callback is still scheduled and entering the isolate to trigger it |
| 266 | */ |
| 267 | int Timers::PumpTimerLoopCallback(int fd, int events, void *data) { |
| 268 | int timerId; |
| 269 | read(fd, &timerId, sizeof(int)); |
| 270 | |
| 271 | auto thiz = static_cast<Timers *>(data); |
| 272 | auto isolate = thiz->isolate_; |
| 273 | if (thiz->stopped || isolate == nullptr || isolate->IsDead()) { |
| 274 | return 0; |
| 275 | } |
| 276 | // thread safety is important! |
| 277 | v8::Locker locker(thiz->isolate_); |
| 278 | v8::Isolate::Scope isolate_scope(isolate); |
| 279 | v8::HandleScope handleScope(isolate); |
| 280 | auto it = thiz->timerMap_.find(timerId); |
| 281 | if (it != thiz->timerMap_.end()) { |
| 282 | auto task = it->second; |
| 283 | // task is no longer in queue to be executed |
| 284 | task->queued_ = false; |
| 285 | thiz->nesting = task->nestingLevel_; |
| 286 | if (task->repeats_) { |
| 287 | // the reason we're doing this in kind of a convoluted way is to follow more closely the chromium implementation than the node implementation |
| 288 | // imagine an interval of 1000ms |
| 289 | // node's setInterval drifts slightly (1000, 2001, 3001, 4002, some busy work 5050, 6050) |
| 290 | // chromium will be consistent: (1000, 2001, 3000, 4000, some busy work 5050, 6000) |
| 291 | task->startTime_ = task->dueTime_; |
| 292 | thiz->addTask(task); |
| 293 | } |
| 294 | v8::Local<v8::Function> cb = task->callback_.Get(isolate); |
| 295 | Runtime* runtime = Runtime::GetRuntime(isolate); |
| 296 | v8::Local<v8::Context> context = runtime->GetContext(); |
| 297 | Context::Scope context_scope(context); |
| 298 | TryCatch tc(isolate); |
| 299 | auto argc = task->args_.get() == nullptr ? 0 : task->args_->size(); |
| 300 | if (argc > 0) { |
| 301 | Local<Value> argv[argc]; |
| 302 | for (int i = 0; i < argc; i++) { |
| 303 | argv[i] = task->args_->at(i)->Get(isolate); |
| 304 | } |
| 305 | cb->Call(context, context->Global(), argc, argv); |
| 306 | } else { |
| 307 | cb->Call(context, context->Global(), 0, nullptr); |
| 308 | } |
| 309 | // task is not queued, so it's either a setTimeout or a cleared setInterval |
| 310 | // ensure we remove it |
| 311 | if (!task->queued_) { |
| 312 | thiz->removeTask(task); |
| 313 | } |
| 314 | |
| 315 | thiz->nesting = 0; |
| 316 | |
| 317 | if (tc.HasCaught()) { |
| 318 | NativeScriptException(tc).ReThrowToJava(); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | } |
| 323 | thiz->bufferFull.notify_one(); |
| 324 | return 1; |
nothing calls this directly
no test coverage detected