1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "iotjs_def.h"
17 #include "iotjs_module_timer.h"
20 // Only for test driver
21 JHANDLER_FUNCTION(IsAliveExceptFor) {
22 JHANDLER_CHECK(iotjs_jhandler_get_arg_length(jhandler) == 1);
23 const iotjs_environment_t* env = iotjs_environment_get();
24 uv_loop_t* loop = iotjs_environment_loop(env);
26 const iotjs_jval_t* arg0 = iotjs_jhandler_get_arg(jhandler, 0);
28 if (iotjs_jval_is_null(arg0)) {
29 int alive = uv_loop_alive(loop);
31 iotjs_jhandler_return_boolean(jhandler, alive);
33 JHANDLER_CHECK(iotjs_jval_is_object(arg0));
36 iotjs_jval_get_property(arg0, IOTJS_MAGIC_STRING_HANDLER);
38 iotjs_timerwrap_t* timer_wrap = iotjs_timerwrap_from_jobject(&jtimer);
39 iotjs_jval_destroy(&jtimer);
41 bool has_active_reqs = uv_loop_has_active_reqs(loop);
42 bool has_closing_handler = loop->closing_handles != NULL;
45 bool alive = !has_active_reqs && !has_closing_handler;
47 int active_handlers = loop->active_handles;
48 if (active_handlers == 1) {
49 const uv_timer_t* timer_handle = iotjs_timerwrap_handle(timer_wrap);
50 int timer_alive = uv_is_active((uv_handle_t*)timer_handle);
53 // If the timer handler we set for test driver is alive,
54 // then it can be safely terminated.
60 iotjs_jhandler_return_boolean(jhandler, ret);
65 iotjs_jval_t InitTestdriver() {
66 iotjs_jval_t testdriver = iotjs_jval_create_object();
67 iotjs_jval_set_method(&testdriver, IOTJS_MAGIC_STRING_ISALIVEEXCEPTFOR,