From 2af4a2afba88ba7a1f2b22ee0947fbf781967580 Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Wed, 5 Sep 2018 11:32:47 +0200 Subject: [PATCH] Replace iotjs_handlewrap_t struct Replaced the iotjs_handlewrap_t by using the uv_handle_t structure directly. For this a two new functions and two new macros were introduced: * iotjs_uv_handle_create * iotjs_uv_handle_close * IOTJS_UV_HANDLE_DATA * IOTJS_UV_HANDLE_EXTRA_DATA For more information about the methods and macros please see the iotjs_uv_handle.h file. Additionally binary code size is reduced a bit. IoT.js-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- src/iotjs.c | 13 +- src/iotjs_handlewrap.c | 109 --------------- src/iotjs_handlewrap.h | 70 ---------- src/iotjs_uv_handle.c | 68 +++++++++ src/iotjs_uv_handle.h | 75 ++++++++++ src/modules/iotjs_module_crypto.c | 1 - src/modules/iotjs_module_mqtt.c | 2 - src/modules/iotjs_module_tcp.c | 130 +++++------------- src/modules/iotjs_module_tcp.h | 14 -- src/modules/iotjs_module_timer.c | 109 +++------------ src/modules/iotjs_module_timer.h | 42 ------ src/modules/iotjs_module_uart.c | 87 ++++++------ src/modules/iotjs_module_uart.h | 20 ++- src/modules/iotjs_module_udp.c | 100 ++++---------- src/modules/iotjs_module_udp.h | 39 ------ src/modules/iotjs_module_websocket.c | 1 - src/modules/linux/iotjs_module_uart-linux.c | 17 ++- src/modules/nuttx/iotjs_module_uart-nuttx.c | 17 ++- src/modules/tizen/iotjs_module_uart-tizen.c | 16 ++- .../tizenrt/iotjs_module_uart-tizenrt.c | 17 ++- 20 files changed, 333 insertions(+), 614 deletions(-) delete mode 100644 src/iotjs_handlewrap.c delete mode 100644 src/iotjs_handlewrap.h create mode 100644 src/iotjs_uv_handle.c create mode 100644 src/iotjs_uv_handle.h delete mode 100644 src/modules/iotjs_module_timer.h delete mode 100644 src/modules/iotjs_module_udp.h diff --git a/src/iotjs.c b/src/iotjs.c index 27fd447fb2..5a93eb73b2 100644 --- a/src/iotjs.c +++ b/src/iotjs.c @@ -16,7 +16,6 @@ #include "iotjs_def.h" #include "iotjs.h" -#include "iotjs_handlewrap.h" #include "iotjs_js.h" #include "iotjs_string_ext.h" @@ -27,6 +26,8 @@ #include "jerryscript-port.h" #include "jerryscript.h" +#include "iotjs_uv_handle.h" + #include #include #include @@ -186,18 +187,10 @@ static int iotjs_start(iotjs_environment_t* env) { } -static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) { - iotjs_handlewrap_t* handle_wrap = iotjs_handlewrap_from_handle(handle); - IOTJS_ASSERT(handle_wrap != NULL); - - iotjs_handlewrap_close(handle_wrap, NULL); -} - - void iotjs_end(iotjs_environment_t* env) { uv_loop_t* loop = iotjs_environment_loop(env); // Close uv loop. - uv_walk(loop, iotjs_uv_walk_to_close_callback, NULL); + uv_walk(loop, (uv_walk_cb)iotjs_uv_handle_close, NULL); uv_run(loop, UV_RUN_DEFAULT); int res = uv_loop_close(loop); diff --git a/src/iotjs_handlewrap.c b/src/iotjs_handlewrap.c deleted file mode 100644 index 5ef9185c25..0000000000 --- a/src/iotjs_handlewrap.c +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "iotjs_def.h" -#include "iotjs_handlewrap.h" - - -void iotjs_handlewrap_initialize(iotjs_handlewrap_t* handlewrap, - jerry_value_t jobject, uv_handle_t* handle, - JNativeInfoType* native_info) { - // Increase ref count of Javascript object to guarantee it is alive until the - // handle has closed. - jerry_value_t jobjectref = jerry_acquire_value(jobject); - handlewrap->jobject = jobjectref; - jerry_set_object_native_pointer(jobjectref, handlewrap, native_info); - - handlewrap->handle = handle; - handlewrap->on_close_cb = NULL; - - handle->data = handlewrap; - - iotjs_handlewrap_validate(handlewrap); -} - - -void iotjs_handlewrap_destroy(iotjs_handlewrap_t* handlewrap) { - // Handle should have been release before this. - IOTJS_ASSERT(handlewrap->handle == NULL); -} - - -iotjs_handlewrap_t* iotjs_handlewrap_from_handle(uv_handle_t* handle) { - iotjs_handlewrap_t* handlewrap = (iotjs_handlewrap_t*)(handle->data); - iotjs_handlewrap_validate(handlewrap); - return handlewrap; -} - - -iotjs_handlewrap_t* iotjs_handlewrap_from_jobject(jerry_value_t jobject) { - iotjs_handlewrap_t* handlewrap = - (iotjs_handlewrap_t*)(iotjs_jval_get_object_native_handle(jobject, NULL)); - iotjs_handlewrap_validate(handlewrap); - return handlewrap; -} - - -uv_handle_t* iotjs_handlewrap_get_uv_handle(iotjs_handlewrap_t* handlewrap) { - iotjs_handlewrap_validate(handlewrap); - return handlewrap->handle; -} - - -jerry_value_t iotjs_handlewrap_jobject(iotjs_handlewrap_t* handlewrap) { - iotjs_handlewrap_validate(handlewrap); - return handlewrap->jobject; -} - - -static void iotjs_handlewrap_on_close(iotjs_handlewrap_t* handlewrap) { - // The handle closed. - // Calls registered close handler function. - if (handlewrap->on_close_cb) { - handlewrap->on_close_cb(handlewrap->handle); - } - - // Set handle null. - handlewrap->handle = NULL; - - // Decrease ref count of Javascript object. From now the object can be - // reclaimed. - jerry_release_value(handlewrap->jobject); -} - - -static void iotjs_on_handle_closed(uv_handle_t* handle) { - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_handle(handle); - iotjs_handlewrap_on_close(handlewrap); -} - - -void iotjs_handlewrap_close(iotjs_handlewrap_t* handlewrap, - OnCloseHandler on_close_cb) { - if (handlewrap->handle != NULL && !uv_is_closing(handlewrap->handle)) { - handlewrap->on_close_cb = on_close_cb; - uv_close(handlewrap->handle, iotjs_on_handle_closed); - } else { - DDLOG("Attempt to close uninitialized or already closed handle"); - } -} - - -void iotjs_handlewrap_validate(iotjs_handlewrap_t* handlewrap) { - IOTJS_ASSERT(handlewrap); - IOTJS_ASSERT((void*)handlewrap == handlewrap->handle->data); - IOTJS_ASSERT((void*)handlewrap == - iotjs_jval_get_object_native_handle(handlewrap->jobject, NULL)); -} diff --git a/src/iotjs_handlewrap.h b/src/iotjs_handlewrap.h deleted file mode 100644 index 4994456bd2..0000000000 --- a/src/iotjs_handlewrap.h +++ /dev/null @@ -1,70 +0,0 @@ -/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IOTJS_HANDLEWRAP_H -#define IOTJS_HANDLEWRAP_H - - -#include - -#include "iotjs_binding.h" - - -typedef void (*OnCloseHandler)(uv_handle_t*); - - -// UV handle wrapper. -// This wrapper connects a Javascript object and a libuv handler. -// This wrapper will increase ref count for the Javascript object and decrease -// it after corresponding handle has closed. Hence the Javascript object will -// not turn into garbage until the handle is open. - -// Javascript object -// -> -// Create a handle wrap, initializing uv handle, increase ref count. -// -> -// The javascript object will be alive until handle has closed. -// -> -// Handle closed, release handle, decrease ref count. -// -> -// The javascript object now can be reclaimed by GC. - -typedef struct { - jerry_value_t jobject; - uv_handle_t* handle; - OnCloseHandler on_close_cb; -} iotjs_handlewrap_t; - - -// jobject: Object that connect with the uv handle -void iotjs_handlewrap_initialize(iotjs_handlewrap_t* handlewrap, - jerry_value_t jobject, uv_handle_t* handle, - JNativeInfoType* native_info); - -void iotjs_handlewrap_destroy(iotjs_handlewrap_t* handlewrap); - -void iotjs_handlewrap_close(iotjs_handlewrap_t* handlewrap, - OnCloseHandler on_close_cb); - -iotjs_handlewrap_t* iotjs_handlewrap_from_handle(uv_handle_t* handle); -iotjs_handlewrap_t* iotjs_handlewrap_from_jobject(jerry_value_t jobject); - -uv_handle_t* iotjs_handlewrap_get_uv_handle(iotjs_handlewrap_t* handlewrap); -jerry_value_t iotjs_handlewrap_jobject(iotjs_handlewrap_t* handlewrap); - -void iotjs_handlewrap_validate(iotjs_handlewrap_t* handlewrap); - - -#endif /* IOTJS_HANDLEWRAP_H */ diff --git a/src/iotjs_uv_handle.c b/src/iotjs_uv_handle.c new file mode 100644 index 0000000000..defa162292 --- /dev/null +++ b/src/iotjs_uv_handle.c @@ -0,0 +1,68 @@ +/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iotjs_def.h" + +#include "iotjs_uv_handle.h" + + +uv_handle_t* iotjs_uv_handle_create(size_t handle_size, + const jerry_value_t jobject, + JNativeInfoType* native_info, + size_t extra_data_size) { + IOTJS_ASSERT(jerry_value_is_object(jobject)); + + /* Make sure that the jerry_value_t is aligned */ + size_t aligned_request_size = IOTJS_ALIGNUP(handle_size, 8u); + + char* request_memory = iotjs_buffer_allocate( + aligned_request_size + sizeof(iotjs_uv_handle_data) + extra_data_size); + uv_handle_t* uv_handle = (uv_handle_t*)request_memory; + uv_handle->data = request_memory + aligned_request_size; + + IOTJS_UV_HANDLE_DATA(uv_handle)->jobject = jobject; + IOTJS_UV_HANDLE_DATA(uv_handle)->on_close_cb = NULL; + jerry_acquire_value(jobject); + + jerry_set_object_native_pointer(jobject, uv_handle, native_info); + + return uv_handle; +} + + +static void iotjs_uv_handle_close_processor(uv_handle_t* handle) { + iotjs_uv_handle_data* handle_data = IOTJS_UV_HANDLE_DATA(handle); + + if (handle_data->on_close_cb != NULL) { + handle_data->on_close_cb(handle); + } + + // Decrease ref count of Javascript object. From now the object can be + // reclaimed. + jerry_release_value(handle_data->jobject); + IOTJS_RELEASE(handle); +} + + +void iotjs_uv_handle_close(uv_handle_t* handle, OnCloseHandler close_handler) { + if (handle == NULL || uv_is_closing(handle)) { + DDLOG("Attempt to close uninitialized or already closed handle"); + return; + } + + iotjs_uv_handle_data* handle_data = IOTJS_UV_HANDLE_DATA(handle); + handle_data->on_close_cb = close_handler; + uv_close(handle, iotjs_uv_handle_close_processor); +} diff --git a/src/iotjs_uv_handle.h b/src/iotjs_uv_handle.h new file mode 100644 index 0000000000..570ff28a29 --- /dev/null +++ b/src/iotjs_uv_handle.h @@ -0,0 +1,75 @@ +/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOTJS_UV_HANDLE +#define IOTJS_UV_HANDLE + +#include + +#include "iotjs_binding.h" + +typedef void (*OnCloseHandler)(uv_handle_t*); + +typedef struct { + jerry_value_t jobject; + OnCloseHandler on_close_cb; +} iotjs_uv_handle_data; + +#define IOTJS_ALIGNUP(value, alignment) \ + (((value) + ((alignment)-1)) & ~((alignment)-1)) + +/** + * Allocate and initialize an uv_handle_t structure with a jerry callback and + * extra data. + * + * The allocated memory has the following layout: + * + * |-------------| <- start of uv_handle_t* + * | uv_handle_t | + * | | + * |-------------| + * | PADDING | <- alignment padding + * |-------------| <- start of the iotjs_uv_handle_data struct + * | handle_data | + * |-------------| <- start of the extra data if required + * | extra | + * |-------------| + * + */ +uv_handle_t* iotjs_uv_handle_create(size_t handle_size, + const jerry_value_t jobject, + JNativeInfoType* native_info, + size_t extra_data_size); +void iotjs_uv_handle_close(uv_handle_t* handle, OnCloseHandler close_handler); + +/** + * Returns a pointer to the handle data struct referenced + * by the uv_handle_t->data member. + */ +#define IOTJS_UV_HANDLE_DATA(UV_HANDLE) \ + ((iotjs_uv_handle_data*)((UV_HANDLE)->data)) + +/** + * Returns a char* pointer for any extra data. + * + * IMPORTANT! + * Make sure that the extra data is correctly allocated by using the + * iotjs_uv_handle_create method call. + */ +#define IOTJS_UV_HANDLE_EXTRA_DATA(UV_HANDLE) \ + ((char*)((char*)((UV_HANDLE)->data) + sizeof(iotjs_uv_handle_data))) + + +#endif /* IOTJS_UV_HANDLE */ diff --git a/src/modules/iotjs_module_crypto.c b/src/modules/iotjs_module_crypto.c index baa7842e69..127e5c31a0 100644 --- a/src/modules/iotjs_module_crypto.c +++ b/src/modules/iotjs_module_crypto.c @@ -42,7 +42,6 @@ #include "iotjs_def.h" #include "iotjs_module_crypto.h" -#include "iotjs_handlewrap.h" #include "iotjs_module_buffer.h" /* These enum values are the same as the ones in crypto.js as well as the diff --git a/src/modules/iotjs_module_mqtt.c b/src/modules/iotjs_module_mqtt.c index c8359478ee..832892c2c1 100644 --- a/src/modules/iotjs_module_mqtt.c +++ b/src/modules/iotjs_module_mqtt.c @@ -21,8 +21,6 @@ #include "iotjs_module_mqtt.h" -#include "iotjs_handlewrap.h" - static void iotjs_mqttclient_destroy(iotjs_mqttclient_t *mqttclient) { IOTJS_RELEASE(mqttclient->buffer); IOTJS_RELEASE(mqttclient); diff --git a/src/modules/iotjs_module_tcp.c b/src/modules/iotjs_module_tcp.c index e0c2931135..48566b1de8 100644 --- a/src/modules/iotjs_module_tcp.c +++ b/src/modules/iotjs_module_tcp.c @@ -17,52 +17,20 @@ #include "iotjs_def.h" #include "iotjs_module_tcp.h" -#include "iotjs_handlewrap.h" #include "iotjs_module_buffer.h" +#include "iotjs_uv_handle.h" #include "iotjs_uv_request.h" +static const jerry_object_native_info_t this_module_native_info = { NULL }; -IOTJS_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(tcpwrap); - -iotjs_tcpwrap_t* iotjs_tcpwrap_create(jerry_value_t jtcp) { - iotjs_tcpwrap_t* tcpwrap = IOTJS_ALLOC(iotjs_tcpwrap_t); - - iotjs_handlewrap_initialize(&tcpwrap->handlewrap, jtcp, - (uv_handle_t*)(&tcpwrap->handle), - &this_module_native_info); +void iotjs_tcp_object_init(jerry_value_t jtcp) { + // uv_tcp_t* can be handled as uv_handle_t* or even as uv_stream_t* + uv_handle_t* handle = iotjs_uv_handle_create(sizeof(uv_tcp_t), jtcp, + &this_module_native_info, 0); const iotjs_environment_t* env = iotjs_environment_get(); - uv_tcp_init(iotjs_environment_loop(env), &tcpwrap->handle); - - return tcpwrap; -} - - -static void iotjs_tcpwrap_destroy(iotjs_tcpwrap_t* tcpwrap) { - iotjs_handlewrap_destroy(&tcpwrap->handlewrap); - IOTJS_RELEASE(tcpwrap); -} - - -iotjs_tcpwrap_t* iotjs_tcpwrap_from_handle(uv_tcp_t* tcp_handle) { - uv_handle_t* handle = (uv_handle_t*)(tcp_handle); - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_handle(handle); - iotjs_tcpwrap_t* tcpwrap = (iotjs_tcpwrap_t*)handlewrap; - IOTJS_ASSERT(iotjs_tcpwrap_tcp_handle(tcpwrap) == tcp_handle); - return tcpwrap; -} - - -iotjs_tcpwrap_t* iotjs_tcpwrap_from_jobject(jerry_value_t jtcp) { - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_jobject(jtcp); - return (iotjs_tcpwrap_t*)handlewrap; -} - - -uv_tcp_t* iotjs_tcpwrap_tcp_handle(iotjs_tcpwrap_t* tcpwrap) { - uv_handle_t* handle = iotjs_handlewrap_get_uv_handle(&tcpwrap->handlewrap); - return (uv_tcp_t*)handle; + uv_tcp_init(iotjs_environment_loop(env), (uv_tcp_t*)handle); } @@ -89,17 +57,14 @@ JS_FUNCTION(TCP) { DJS_CHECK_THIS(); jerry_value_t jtcp = JS_GET_THIS(); - iotjs_tcpwrap_create(jtcp); + iotjs_tcp_object_init(jtcp); return jerry_create_undefined(); } // Socket close result handler. void AfterClose(uv_handle_t* handle) { - iotjs_handlewrap_t* wrap = iotjs_handlewrap_from_handle(handle); - - // tcp object. - jerry_value_t jtcp = iotjs_handlewrap_jobject(wrap); + jerry_value_t jtcp = IOTJS_UV_HANDLE_DATA(handle)->jobject; // callback function. jerry_value_t jcallback = @@ -113,10 +78,9 @@ void AfterClose(uv_handle_t* handle) { // Close socket JS_FUNCTION(Close) { - JS_DECLARE_THIS_PTR(handlewrap, wrap); + JS_DECLARE_PTR(jthis, uv_handle_t, uv_handle); - // close uv handle, `AfterClose` will be called after socket closed. - iotjs_handlewrap_close(wrap, AfterClose); + iotjs_uv_handle_close(uv_handle, AfterClose); return jerry_create_undefined(); } @@ -126,7 +90,7 @@ JS_FUNCTION(Close) { // [0] address // [1] port JS_FUNCTION(Bind) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_tcp_t, tcp_handle); DJS_CHECK_ARGS(2, string, number); @@ -137,8 +101,7 @@ JS_FUNCTION(Bind) { int err = uv_ip4_addr(iotjs_string_data(&address), port, &addr); if (err == 0) { - err = uv_tcp_bind(iotjs_tcpwrap_tcp_handle(tcp_wrap), - (const sockaddr*)(&addr), 0); + err = uv_tcp_bind(tcp_handle, (const sockaddr*)(&addr), 0); } iotjs_string_destroy(&address); @@ -157,7 +120,7 @@ static void AfterConnect(uv_connect_t* req, int status) { // [1] port // [2] callback JS_FUNCTION(Connect) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_tcp_t, tcp_handle); DJS_CHECK_ARGS(3, string, number, function); @@ -174,8 +137,7 @@ JS_FUNCTION(Connect) { iotjs_uv_request_create(sizeof(uv_connect_t), jcallback, 0); // Create connection request. - err = uv_tcp_connect((uv_connect_t*)req_connect, - iotjs_tcpwrap_tcp_handle(tcp_wrap), + err = uv_tcp_connect((uv_connect_t*)req_connect, tcp_handle, (const sockaddr*)(&addr), AfterConnect); if (err) { @@ -194,11 +156,7 @@ JS_FUNCTION(Connect) { // * uv_stream_t* handle - server handle // * int status - status code static void OnConnection(uv_stream_t* handle, int status) { - // Server tcp wrapper. - iotjs_tcpwrap_t* tcp_wrap = iotjs_tcpwrap_from_handle((uv_tcp_t*)handle); - - // Tcp object - jerry_value_t jtcp = iotjs_handlewrap_jobject(&tcp_wrap->handlewrap); + jerry_value_t jtcp = IOTJS_UV_HANDLE_DATA(handle)->jobject; // `onconnection` callback. jerry_value_t jonconnection = @@ -222,14 +180,11 @@ static void OnConnection(uv_stream_t* handle, int status) { IOTJS_ASSERT(!jerry_value_is_error(jclient_tcp)); IOTJS_ASSERT(jerry_value_is_object(jclient_tcp)); - iotjs_tcpwrap_t* tcp_wrap_client = - (iotjs_tcpwrap_t*)(iotjs_jval_get_object_native_handle( - jclient_tcp, &this_module_native_info)); + uv_handle_t* client_handle = (uv_handle_t*) + iotjs_jval_get_object_native_handle(jclient_tcp, + &this_module_native_info); - uv_stream_t* client_handle = - (uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap_client)); - - int err = uv_accept(handle, client_handle); + int err = uv_accept(handle, (uv_stream_t*)client_handle); if (err) { jerry_release_value(args[0]); return; @@ -249,13 +204,11 @@ static void OnConnection(uv_stream_t* handle, int status) { JS_FUNCTION(Listen) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_tcp_t, tcp_handle); DJS_CHECK_ARGS(1, number); int backlog = JS_GET_ARG(0, number); - - int err = uv_listen((uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap)), - backlog, OnConnection); + int err = uv_listen((uv_stream_t*)tcp_handle, backlog, OnConnection); return jerry_create_number(err); } @@ -267,7 +220,7 @@ void AfterWrite(uv_write_t* req, int status) { JS_FUNCTION(Write) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_stream_t, tcp_handle); DJS_CHECK_ARGS(2, object, function); const jerry_value_t jbuffer = JS_GET_ARG(0, object); @@ -281,9 +234,7 @@ JS_FUNCTION(Write) { jerry_value_t arg1 = JS_GET_ARG(1, object); uv_req_t* req_write = iotjs_uv_request_create(sizeof(uv_write_t), arg1, 0); - int err = uv_write((uv_write_t*)req_write, - (uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap)), &buf, - 1, AfterWrite); + int err = uv_write((uv_write_t*)req_write, tcp_handle, &buf, 1, AfterWrite); if (err) { iotjs_uv_request_destroy((uv_req_t*)req_write); @@ -304,10 +255,7 @@ void OnAlloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { - iotjs_tcpwrap_t* tcp_wrap = iotjs_tcpwrap_from_handle((uv_tcp_t*)handle); - - // tcp handle - jerry_value_t jtcp = iotjs_handlewrap_jobject(&tcp_wrap->handlewrap); + jerry_value_t jtcp = IOTJS_UV_HANDLE_DATA(handle)->jobject; // socket object jerry_value_t jsocket = @@ -353,10 +301,9 @@ void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { JS_FUNCTION(ReadStart) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_stream_t, tcp_handle); - int err = uv_read_start((uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap)), - OnAlloc, OnRead); + int err = uv_read_start(tcp_handle, OnAlloc, OnRead); return jerry_create_number(err); } @@ -368,20 +315,18 @@ static void AfterShutdown(uv_shutdown_t* req, int status) { JS_FUNCTION(Shutdown) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_stream_t, tcp_handle); DJS_CHECK_ARGS(1, function); jerry_value_t arg0 = JS_GET_ARG(0, object); - uv_req_t* req_shutdown = - iotjs_uv_request_create(sizeof(uv_shutdown_t), arg0, 0); + uv_shutdown_t* req_shutdown = + (uv_shutdown_t*)iotjs_uv_request_create(sizeof(uv_shutdown_t), arg0, 0); - int err = uv_shutdown((uv_shutdown_t*)req_shutdown, - (uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap)), - AfterShutdown); + int err = uv_shutdown(req_shutdown, tcp_handle, AfterShutdown); if (err) { - iotjs_uv_request_destroy(req_shutdown); + iotjs_uv_request_destroy((uv_req_t*)req_shutdown); } return jerry_create_number(err); @@ -392,14 +337,14 @@ JS_FUNCTION(Shutdown) { // [0] enable // [1] delay JS_FUNCTION(SetKeepAlive) { - JS_DECLARE_THIS_PTR(tcpwrap, tcp_wrap); + JS_DECLARE_PTR(jthis, uv_tcp_t, tcp_handle); DJS_CHECK_ARGS(2, number, number); int enable = JS_GET_ARG(0, number); unsigned delay = JS_GET_ARG(1, number); - int err = uv_tcp_keepalive(iotjs_tcpwrap_tcp_handle(tcp_wrap), enable, delay); + int err = uv_tcp_keepalive(tcp_handle, enable, delay); return jerry_create_number(err); } @@ -452,15 +397,14 @@ void AddressToJS(jerry_value_t obj, const sockaddr* addr) { JS_FUNCTION(GetSockeName) { - DJS_CHECK_ARGS(1, object); + JS_DECLARE_PTR(jthis, uv_tcp_t, tcp_handle); - iotjs_tcpwrap_t* wrap = iotjs_tcpwrap_from_jobject(JS_GET_THIS()); - IOTJS_ASSERT(wrap != NULL); + DJS_CHECK_ARGS(1, object); sockaddr_storage storage; int addrlen = sizeof(storage); sockaddr* const addr = (sockaddr*)(&storage); - int err = uv_tcp_getsockname(iotjs_tcpwrap_tcp_handle(wrap), addr, &addrlen); + int err = uv_tcp_getsockname(tcp_handle, addr, &addrlen); if (err == 0) AddressToJS(JS_GET_ARG(0, object), addr); return jerry_create_number(err); diff --git a/src/modules/iotjs_module_tcp.h b/src/modules/iotjs_module_tcp.h index 6ed5966868..bb9dbb1d64 100644 --- a/src/modules/iotjs_module_tcp.h +++ b/src/modules/iotjs_module_tcp.h @@ -19,7 +19,6 @@ #include "iotjs_binding.h" -#include "iotjs_handlewrap.h" typedef struct sockaddr sockaddr; @@ -28,19 +27,6 @@ typedef struct sockaddr_in6 sockaddr_in6; typedef struct sockaddr_storage sockaddr_storage; -typedef struct { - iotjs_handlewrap_t handlewrap; - uv_tcp_t handle; -} iotjs_tcpwrap_t; - - -iotjs_tcpwrap_t* iotjs_tcpwrap_create(jerry_value_t jtcp); - -iotjs_tcpwrap_t* iotjs_tcpwrap_from_handle(uv_tcp_t* handle); -iotjs_tcpwrap_t* iotjs_tcpwrap_from_jobject(jerry_value_t jtcp); - -uv_tcp_t* iotjs_tcpwrap_tcp_handle(iotjs_tcpwrap_t* tcpwrap); - void AddressToJS(jerry_value_t obj, const sockaddr* addr); diff --git a/src/modules/iotjs_module_timer.c b/src/modules/iotjs_module_timer.c index af64138494..fca9b03916 100644 --- a/src/modules/iotjs_module_timer.c +++ b/src/modules/iotjs_module_timer.c @@ -14,71 +14,25 @@ */ #include "iotjs_def.h" -#include "iotjs_module_timer.h" +#include "iotjs_uv_handle.h" -static void iotjs_timerwrap_destroy(iotjs_timerwrap_t* timerwrap); -static void iotjs_timerwrap_on_timeout(iotjs_timerwrap_t* timerwrap); -IOTJS_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(timerwrap); +static const jerry_object_native_info_t this_module_native_info = { NULL }; -iotjs_timerwrap_t* iotjs_timerwrap_create(const jerry_value_t jtimer) { - iotjs_timerwrap_t* timerwrap = IOTJS_ALLOC(iotjs_timerwrap_t); - uv_timer_t* uv_timer = IOTJS_ALLOC(uv_timer_t); +void iotjs_timer_object_init(jerry_value_t jtimer) { + uv_handle_t* handle = iotjs_uv_handle_create(sizeof(uv_timer_t), jtimer, + &this_module_native_info, 0); - iotjs_handlewrap_initialize(&timerwrap->handlewrap, jtimer, - (uv_handle_t*)(uv_timer), - &this_module_native_info); - - // Initialize timer handler. const iotjs_environment_t* env = iotjs_environment_get(); - uv_timer_init(iotjs_environment_loop(env), uv_timer); - - return timerwrap; + uv_timer_init(iotjs_environment_loop(env), (uv_timer_t*)handle); } -static void iotjs_timerwrap_destroy(iotjs_timerwrap_t* timerwrap) { - iotjs_handlewrap_destroy(&timerwrap->handlewrap); - - IOTJS_RELEASE(timerwrap); -} - -static void TimoutHandlerDestroy(uv_handle_t* handle) { - IOTJS_RELEASE(handle); -} - -// This function is called from uv when timeout expires. static void TimeoutHandler(uv_timer_t* handle) { - // Find timer wrap from handle. - iotjs_timerwrap_t* timer_wrap = iotjs_timerwrap_from_handle(handle); - - // Call the timeout handler. - iotjs_timerwrap_on_timeout(timer_wrap); -} - - -int iotjs_timerwrap_start(iotjs_timerwrap_t* timerwrap, uint64_t timeout, - uint64_t repeat) { - // Start uv timer. - uv_timer_t* uv_timer = - (uv_timer_t*)iotjs_handlewrap_get_uv_handle(&timerwrap->handlewrap); - return uv_timer_start(uv_timer, TimeoutHandler, timeout, repeat); -} - - -int iotjs_timerwrap_stop(iotjs_timerwrap_t* timerwrap) { - if (!uv_is_closing(iotjs_handlewrap_get_uv_handle(&timerwrap->handlewrap))) { - iotjs_handlewrap_close(&timerwrap->handlewrap, TimoutHandlerDestroy); - } - - return 0; -} - + IOTJS_ASSERT(handle != NULL); -static void iotjs_timerwrap_on_timeout(iotjs_timerwrap_t* timerwrap) { - // Call javascript timeout handler function. - jerry_value_t jobject = iotjs_timerwrap_jobject(timerwrap); + jerry_value_t jobject = IOTJS_UV_HANDLE_DATA(handle)->jobject; jerry_value_t jcallback = iotjs_jval_get_property(jobject, IOTJS_MAGIC_STRING_HANDLETIMEOUT); iotjs_invoke_callback(jcallback, jobject, NULL, 0); @@ -86,36 +40,9 @@ static void iotjs_timerwrap_on_timeout(iotjs_timerwrap_t* timerwrap) { } -uv_timer_t* iotjs_timerwrap_handle(iotjs_timerwrap_t* timerwrap) { - return (uv_timer_t*)iotjs_handlewrap_get_uv_handle(&timerwrap->handlewrap); -} - - -jerry_value_t iotjs_timerwrap_jobject(iotjs_timerwrap_t* timerwrap) { - jerry_value_t jobject = iotjs_handlewrap_jobject(&timerwrap->handlewrap); - IOTJS_ASSERT(jerry_value_is_object(jobject)); - return jobject; -} - - -iotjs_timerwrap_t* iotjs_timerwrap_from_handle(uv_timer_t* timer_handle) { - uv_handle_t* handle = (uv_handle_t*)(timer_handle); - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_handle(handle); - iotjs_timerwrap_t* timerwrap = (iotjs_timerwrap_t*)handlewrap; - IOTJS_ASSERT(iotjs_timerwrap_handle(timerwrap) == timer_handle); - return timerwrap; -} - - -iotjs_timerwrap_t* iotjs_timerwrap_from_jobject(const jerry_value_t jtimer) { - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_jobject(jtimer); - return (iotjs_timerwrap_t*)handlewrap; -} - - JS_FUNCTION(Start) { // Check parameters. - JS_DECLARE_THIS_PTR(timerwrap, timer_wrap); + JS_DECLARE_PTR(jthis, uv_timer_t, timer_handle); DJS_CHECK_ARGS(2, number, number); // parameters. @@ -123,18 +50,21 @@ JS_FUNCTION(Start) { uint64_t repeat = JS_GET_ARG(1, number); // Start timer. - int res = iotjs_timerwrap_start(timer_wrap, timeout, repeat); + int res = uv_timer_start(timer_handle, TimeoutHandler, timeout, repeat); return jerry_create_number(res); } JS_FUNCTION(Stop) { - JS_DECLARE_THIS_PTR(timerwrap, timer_wrap); + JS_DECLARE_PTR(jthis, uv_handle_t, timer_handle); // Stop timer. - int res = iotjs_timerwrap_stop(timer_wrap); - return jerry_create_number(res); + if (!uv_is_closing(timer_handle)) { + iotjs_uv_handle_close(timer_handle, NULL); + } + + return jerry_create_number(0); } @@ -143,12 +73,7 @@ JS_FUNCTION(Timer) { const jerry_value_t jtimer = JS_GET_THIS(); - iotjs_timerwrap_t* timer_wrap = iotjs_timerwrap_create(jtimer); - - jerry_value_t jobject = iotjs_timerwrap_jobject(timer_wrap); - IOTJS_ASSERT(jerry_value_is_object(jobject)); - IOTJS_ASSERT(iotjs_jval_get_object_native_handle(jtimer, NULL) != NULL); - + iotjs_timer_object_init(jtimer); return jerry_create_undefined(); } diff --git a/src/modules/iotjs_module_timer.h b/src/modules/iotjs_module_timer.h deleted file mode 100644 index 28e2214983..0000000000 --- a/src/modules/iotjs_module_timer.h +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef IOTJS_MODULE_TIMER_H -#define IOTJS_MODULE_TIMER_H - - -#include "iotjs_binding.h" -#include "iotjs_handlewrap.h" - - -typedef struct { iotjs_handlewrap_t handlewrap; } iotjs_timerwrap_t; - - -iotjs_timerwrap_t* iotjs_timerwrap_create(const jerry_value_t jtimer); - -iotjs_timerwrap_t* iotjs_timerwrap_from_jobject(const jerry_value_t jtimer); -iotjs_timerwrap_t* iotjs_timerwrap_from_handle(uv_timer_t* timer_handle); - -uv_timer_t* iotjs_timerwrap_handle(iotjs_timerwrap_t* timerwrap); -jerry_value_t iotjs_timerwrap_jobject(iotjs_timerwrap_t* timerwrap); - -// Start timer. -int iotjs_timerwrap_start(iotjs_timerwrap_t* timerwrap, uint64_t timeout, - uint64_t repeat); -// Stop & close timer. -int iotjs_timerwrap_stop(iotjs_timerwrap_t* timerwrap); - - -#endif /* IOTJS_MODULE_TIMER_H */ diff --git a/src/modules/iotjs_module_uart.c b/src/modules/iotjs_module_uart.c index ba7e4e97ca..4446f05b24 100644 --- a/src/modules/iotjs_module_uart.c +++ b/src/modules/iotjs_module_uart.c @@ -18,43 +18,38 @@ #include "iotjs_def.h" #include "iotjs_module_buffer.h" #include "iotjs_module_uart.h" +#include "iotjs_uv_handle.h" #include "iotjs_uv_request.h" -IOTJS_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(uart); +static void iotjs_uart_object_destroy(uv_handle_t* handle); -static iotjs_uart_t* uart_create(const jerry_value_t juart) { - iotjs_uart_t* uart = IOTJS_ALLOC(iotjs_uart_t); - iotjs_uart_create_platform_data(uart); +static const jerry_object_native_info_t this_module_native_info = { + .free_cb = (jerry_object_native_free_callback_t)iotjs_uart_object_destroy, +}; - iotjs_handlewrap_initialize(&uart->handlewrap, juart, - (uv_handle_t*)(&uart->poll_handle), - &this_module_native_info); - uart->device_fd = -1; - return uart; -} +void iotjs_uart_object_destroy(uv_handle_t* handle) { + iotjs_uart_t* uart = (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(handle); -static void iotjs_uart_destroy(iotjs_uart_t* uart) { - iotjs_handlewrap_destroy(&uart->handlewrap); iotjs_uart_destroy_platform_data(uart->platform_data); - IOTJS_RELEASE(uart); } + static void uart_worker(uv_work_t* work_req) { iotjs_periph_data_t* worker_data = (iotjs_periph_data_t*)IOTJS_UV_REQUEST_EXTRA_DATA(work_req); - iotjs_uart_t* uart = (iotjs_uart_t*)worker_data->data; + uv_handle_t* uart_poll_handle = (uv_handle_t*)worker_data->data; switch (worker_data->op) { case kUartOpOpen: - worker_data->result = iotjs_uart_open(uart); + worker_data->result = iotjs_uart_open(uart_poll_handle); break; case kUartOpWrite: - worker_data->result = iotjs_uart_write(uart); + worker_data->result = iotjs_uart_write(uart_poll_handle); break; case kUartOpClose: - iotjs_handlewrap_close(&uart->handlewrap, iotjs_uart_handlewrap_close_cb); + iotjs_uv_handle_close(uart_poll_handle, iotjs_uart_handle_close_cb); worker_data->result = true; break; default: @@ -69,9 +64,9 @@ static void iotjs_uart_read_cb(uv_poll_t* req, int status, int events) { if (i > 0) { buf[i] = '\0'; DDDLOG("%s - read length: %d", __func__, i); + jerry_value_t juart = IOTJS_UV_HANDLE_DATA(req)->jobject; jerry_value_t jemit = - iotjs_jval_get_property(iotjs_handlewrap_jobject(&uart->handlewrap), - IOTJS_MAGIC_STRING_EMIT); + iotjs_jval_get_property(juart, IOTJS_MAGIC_STRING_EMIT); IOTJS_ASSERT(jerry_value_is_function(jemit)); jerry_value_t str = @@ -83,8 +78,8 @@ static void iotjs_uart_read_cb(uv_poll_t* req, int status, int events) { jerry_value_t jargs[] = { str, jbuf }; jerry_value_t jres = - jerry_call_function(jemit, iotjs_handlewrap_jobject(&uart->handlewrap), - jargs, 2); + jerry_call_function(jemit, IOTJS_UV_HANDLE_DATA(req)->jobject, jargs, + 2); IOTJS_ASSERT(!jerry_value_is_error(jres)); jerry_release_value(jres); @@ -94,12 +89,12 @@ static void iotjs_uart_read_cb(uv_poll_t* req, int status, int events) { } } -void iotjs_uart_register_read_cb(iotjs_uart_t* uart) { - uv_poll_t* poll_handle = &uart->poll_handle; +void iotjs_uart_register_read_cb(uv_poll_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); uv_loop_t* loop = iotjs_environment_loop(iotjs_environment_get()); - uv_poll_init(loop, poll_handle, uart->device_fd); - poll_handle->data = uart; - uv_poll_start(poll_handle, UV_READABLE, iotjs_uart_read_cb); + uv_poll_init(loop, uart_poll_handle, uart->device_fd); + uv_poll_start(uart_poll_handle, UV_READABLE, iotjs_uart_read_cb); } static jerry_value_t uart_set_configuration(iotjs_uart_t* uart, @@ -156,8 +151,15 @@ JS_FUNCTION(UartCons) { DJS_CHECK_ARG_IF_EXIST(1, function); // Create UART object - jerry_value_t juart = JS_GET_THIS(); - iotjs_uart_t* uart = uart_create(juart); + const jerry_value_t juart = JS_GET_THIS(); + uv_handle_t* uart_poll_handle = + iotjs_uv_handle_create(sizeof(uv_poll_t), juart, &this_module_native_info, + sizeof(iotjs_uart_t)); + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); + // TODO: merge platform data allocation into the handle allocation. + iotjs_uart_create_platform_data(uart); + uart->device_fd = -1; jerry_value_t jconfig = JS_GET_ARG(0, object); @@ -180,8 +182,9 @@ JS_FUNCTION(UartCons) { // If the callback doesn't exist, it is completed synchronously. // Otherwise, it will be executed asynchronously. if (!jerry_value_is_null(jcallback)) { - iotjs_periph_call_async(uart, jcallback, kUartOpOpen, uart_worker); - } else if (!iotjs_uart_open(uart)) { + iotjs_periph_call_async(uart_poll_handle, jcallback, kUartOpOpen, + uart_worker); + } else if (!iotjs_uart_open(uart_poll_handle)) { return JS_CREATE_ERROR(COMMON, iotjs_periph_error_str(kUartOpOpen)); } @@ -189,27 +192,31 @@ JS_FUNCTION(UartCons) { } JS_FUNCTION(Write) { - JS_DECLARE_THIS_PTR(uart, uart); + JS_DECLARE_PTR(jthis, uv_poll_t, uart_poll_handle); DJS_CHECK_ARGS(1, string); DJS_CHECK_ARG_IF_EXIST(1, function); + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); uart->buf_data = JS_GET_ARG(0, string); uart->buf_len = iotjs_string_size(&uart->buf_data); - iotjs_periph_call_async(uart, JS_GET_ARG_IF_EXIST(1, function), kUartOpWrite, - uart_worker); + iotjs_periph_call_async(uart_poll_handle, JS_GET_ARG_IF_EXIST(1, function), + kUartOpWrite, uart_worker); return jerry_create_undefined(); } JS_FUNCTION(WriteSync) { - JS_DECLARE_THIS_PTR(uart, uart); + JS_DECLARE_PTR(jthis, uv_handle_t, uart_poll_handle); DJS_CHECK_ARGS(1, string); + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); uart->buf_data = JS_GET_ARG(0, string); uart->buf_len = iotjs_string_size(&uart->buf_data); - bool result = iotjs_uart_write(uart); + bool result = iotjs_uart_write(uart_poll_handle); iotjs_string_destroy(&uart->buf_data); if (!result) { @@ -220,19 +227,19 @@ JS_FUNCTION(WriteSync) { } JS_FUNCTION(Close) { - JS_DECLARE_THIS_PTR(uart, uart); + JS_DECLARE_PTR(jthis, uv_poll_t, uart_poll_handle); DJS_CHECK_ARG_IF_EXIST(0, function); - iotjs_periph_call_async(uart, JS_GET_ARG_IF_EXIST(0, function), kUartOpClose, - uart_worker); + iotjs_periph_call_async(uart_poll_handle, JS_GET_ARG_IF_EXIST(0, function), + kUartOpClose, uart_worker); return jerry_create_undefined(); } JS_FUNCTION(CloseSync) { - JS_DECLARE_THIS_PTR(uart, uart); + JS_DECLARE_PTR(jthis, uv_handle_t, uart_poll_handle); - iotjs_handlewrap_close(&uart->handlewrap, iotjs_uart_handlewrap_close_cb); + iotjs_uv_handle_close(uart_poll_handle, iotjs_uart_handle_close_cb); return jerry_create_undefined(); } diff --git a/src/modules/iotjs_module_uart.h b/src/modules/iotjs_module_uart.h index f558952e17..1cec3bb844 100644 --- a/src/modules/iotjs_module_uart.h +++ b/src/modules/iotjs_module_uart.h @@ -18,7 +18,6 @@ #define IOTJS_MODULE_UART_H #include "iotjs_def.h" -#include "iotjs_handlewrap.h" #include "iotjs_module_periph_common.h" @@ -27,25 +26,24 @@ typedef struct iotjs_uart_platform_data_s iotjs_uart_platform_data_t; typedef struct { - iotjs_handlewrap_t handlewrap; - iotjs_uart_platform_data_t* platform_data; int device_fd; unsigned baud_rate; uint8_t data_bits; iotjs_string_t buf_data; unsigned buf_len; - uv_poll_t poll_handle; -} iotjs_uart_t; -jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, - const jerry_value_t jconfig); + iotjs_uart_platform_data_t* platform_data; +} iotjs_uart_t; -void iotjs_uart_register_read_cb(iotjs_uart_t* uart); -bool iotjs_uart_open(iotjs_uart_t* uart); -bool iotjs_uart_write(iotjs_uart_t* uart); -void iotjs_uart_handlewrap_close_cb(uv_handle_t* handle); +void iotjs_uart_handle_close_cb(uv_handle_t* handle); +void iotjs_uart_register_read_cb(uv_poll_t* uart_poll_handle); void iotjs_uart_create_platform_data(iotjs_uart_t* uart); +jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, + const jerry_value_t jconfig); void iotjs_uart_destroy_platform_data(iotjs_uart_platform_data_t* pdata); +bool iotjs_uart_open(uv_handle_t* uart_poll_handle); +bool iotjs_uart_write(uv_handle_t* uart_poll_handle); + #endif /* IOTJS_MODULE_UART_H */ diff --git a/src/modules/iotjs_module_udp.c b/src/modules/iotjs_module_udp.c index b21bc5a883..29612aa35e 100644 --- a/src/modules/iotjs_module_udp.c +++ b/src/modules/iotjs_module_udp.c @@ -15,54 +15,21 @@ #include "iotjs_def.h" -#include "iotjs_module_udp.h" - -#include "iotjs_handlewrap.h" #include "iotjs_module_buffer.h" #include "iotjs_module_tcp.h" +#include "iotjs_uv_handle.h" #include "iotjs_uv_request.h" -IOTJS_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(udpwrap); +static const jerry_object_native_info_t this_module_native_info = { NULL }; -iotjs_udpwrap_t* iotjs_udpwrap_create(jerry_value_t judp) { - iotjs_udpwrap_t* udpwrap = IOTJS_ALLOC(iotjs_udpwrap_t); - iotjs_handlewrap_initialize(&udpwrap->handlewrap, judp, - (uv_handle_t*)(&udpwrap->handle), - &this_module_native_info); +void iotjs_udp_object_init(jerry_value_t judp) { + uv_handle_t* handle = iotjs_uv_handle_create(sizeof(uv_udp_t), judp, + &this_module_native_info, 0); const iotjs_environment_t* env = iotjs_environment_get(); - uv_udp_init(iotjs_environment_loop(env), &udpwrap->handle); - - return udpwrap; -} - - -static void iotjs_udpwrap_destroy(iotjs_udpwrap_t* udpwrap) { - iotjs_handlewrap_destroy(&udpwrap->handlewrap); - IOTJS_RELEASE(udpwrap); -} - - -iotjs_udpwrap_t* iotjs_udpwrap_from_handle(uv_udp_t* udp_handle) { - uv_handle_t* handle = (uv_handle_t*)(udp_handle); - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_handle(handle); - iotjs_udpwrap_t* udpwrap = (iotjs_udpwrap_t*)handlewrap; - IOTJS_ASSERT(iotjs_udpwrap_udp_handle(udpwrap) == udp_handle); - return udpwrap; -} - - -iotjs_udpwrap_t* iotjs_udpwrap_from_jobject(jerry_value_t judp) { - iotjs_handlewrap_t* handlewrap = iotjs_handlewrap_from_jobject(judp); - return (iotjs_udpwrap_t*)handlewrap; -} - - -uv_udp_t* iotjs_udpwrap_udp_handle(iotjs_udpwrap_t* udpwrap) { - uv_handle_t* handle = iotjs_handlewrap_get_uv_handle(&udpwrap->handlewrap); - return (uv_udp_t*)handle; + uv_udp_init(iotjs_environment_loop(env), (uv_udp_t*)handle); } @@ -70,14 +37,14 @@ JS_FUNCTION(UDP) { DJS_CHECK_THIS(); jerry_value_t judp = JS_GET_THIS(); - iotjs_udpwrap_create(judp); + iotjs_udp_object_init(judp); return jerry_create_undefined(); } JS_FUNCTION(Bind) { - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); DJS_CHECK_ARGS(2, string, number); iotjs_string_t address = JS_GET_ARG(0, string); @@ -98,8 +65,7 @@ JS_FUNCTION(Bind) { uv_ip4_addr(iotjs_string_data(&address), port, (sockaddr_in*)(&addr)); if (err == 0) { - err = uv_udp_bind(iotjs_udpwrap_udp_handle(udp_wrap), - (const sockaddr*)(&addr), flags); + err = uv_udp_bind(udp_handle, (const sockaddr*)(&addr), flags); } jerry_release_value(reuse_addr); @@ -126,10 +92,8 @@ static void OnRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, return; } - iotjs_udpwrap_t* udp_wrap = iotjs_udpwrap_from_handle(handle); - // udp handle - jerry_value_t judp = iotjs_handlewrap_jobject(&udp_wrap->handlewrap); + jerry_value_t judp = IOTJS_UV_HANDLE_DATA(handle)->jobject; IOTJS_ASSERT(jerry_value_is_object(judp)); // onmessage callback @@ -169,10 +133,9 @@ static void OnRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, JS_FUNCTION(RecvStart) { - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); - int err = - uv_udp_recv_start(iotjs_udpwrap_udp_handle(udp_wrap), OnAlloc, OnRecv); + int err = uv_udp_recv_start(udp_handle, OnAlloc, OnRecv); // UV_EALREADY means that the socket is already bound but that's okay if (err == UV_EALREADY) @@ -183,9 +146,9 @@ JS_FUNCTION(RecvStart) { JS_FUNCTION(RecvStop) { - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); - int r = uv_udp_recv_stop(iotjs_udpwrap_udp_handle(udp_wrap)); + int r = uv_udp_recv_stop(udp_handle); return jerry_create_number(r); } @@ -217,7 +180,7 @@ static void OnSend(uv_udp_send_t* req, int status) { // [2] ip // [3] callback function JS_FUNCTION(Send) { - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); DJS_CHECK_ARGS(3, object, number, string); IOTJS_ASSERT(jerry_value_is_function(jargv[3]) || jerry_value_is_undefined(jargv[3])); @@ -243,8 +206,7 @@ JS_FUNCTION(Send) { uv_ip4_addr(iotjs_string_data(&address), port, (sockaddr_in*)(&addr)); if (err == 0) { - err = uv_udp_send((uv_udp_send_t*)req_send, - iotjs_udpwrap_udp_handle(udp_wrap), &buf, 1, + err = uv_udp_send((uv_udp_send_t*)req_send, udp_handle, &buf, 1, (const sockaddr*)(&addr), OnSend); } @@ -260,36 +222,35 @@ JS_FUNCTION(Send) { // Close socket JS_FUNCTION(Close) { - JS_DECLARE_THIS_PTR(handlewrap, wrap); + JS_DECLARE_PTR(jthis, uv_handle_t, uv_handle); - iotjs_handlewrap_close(wrap, NULL); + iotjs_uv_handle_close(uv_handle, NULL); return jerry_create_undefined(); } JS_FUNCTION(GetSockeName) { + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); DJS_CHECK_ARGS(1, object); - iotjs_udpwrap_t* wrap = iotjs_udpwrap_from_jobject(JS_GET_THIS()); - IOTJS_ASSERT(wrap != NULL); sockaddr_storage storage; int addrlen = sizeof(storage); sockaddr* const addr = (sockaddr*)(&storage); - int err = uv_udp_getsockname(iotjs_udpwrap_udp_handle(wrap), addr, &addrlen); + int err = uv_udp_getsockname(udp_handle, addr, &addrlen); if (err == 0) AddressToJS(JS_GET_ARG(0, object), addr); return jerry_create_number(err); } -#define IOTJS_UV_SET_SOCKOPT(fn) \ - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); \ - DJS_CHECK_ARGS(1, number); \ - \ - int flag = JS_GET_ARG(0, number); \ - int err = fn(iotjs_udpwrap_udp_handle(udp_wrap), flag); \ - \ +#define IOTJS_UV_SET_SOCKOPT(fn) \ + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); \ + DJS_CHECK_ARGS(1, number); \ + \ + int flag = JS_GET_ARG(0, number); \ + int err = fn(udp_handle, flag); \ + \ return jerry_create_number(err); @@ -344,7 +305,7 @@ static jerry_value_t SetMembership(const jerry_value_t jthis, const jerry_length_t jargc, uv_membership membership) { #if !defined(__NUTTX__) - JS_DECLARE_THIS_PTR(udpwrap, udp_wrap); + JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle); DJS_CHECK_ARGS(1, string); iotjs_string_t address = JS_GET_ARG(0, string); @@ -360,9 +321,8 @@ static jerry_value_t SetMembership(const jerry_value_t jthis, iface_cstr = iotjs_string_data(&iface); } - int err = uv_udp_set_membership(iotjs_udpwrap_udp_handle(udp_wrap), - iotjs_string_data(&address), iface_cstr, - membership); + int err = uv_udp_set_membership(udp_handle, iotjs_string_data(&address), + iface_cstr, membership); if (!isUndefinedOrNull) iotjs_string_destroy(&iface); diff --git a/src/modules/iotjs_module_udp.h b/src/modules/iotjs_module_udp.h deleted file mode 100644 index e9bd54ccb1..0000000000 --- a/src/modules/iotjs_module_udp.h +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef IOTJS_MODULE_UDP_H -#define IOTJS_MODULE_UDP_H - - -#include "iotjs_def.h" -#include "iotjs_handlewrap.h" - - -typedef struct { - iotjs_handlewrap_t handlewrap; - uv_udp_t handle; -} iotjs_udpwrap_t; - - -iotjs_udpwrap_t* iotjs_udpwrap_create(jerry_value_t judp); - -iotjs_udpwrap_t* iotjs_udpwrap_from_handle(uv_udp_t* handle); -iotjs_udpwrap_t* iotjs_udpwrap_from_jobject(jerry_value_t judp); - -uv_udp_t* iotjs_udpwrap_udp_handle(iotjs_udpwrap_t* udpwrap); - - -#endif /* IOTJS_MODULE_UDP_H */ diff --git a/src/modules/iotjs_module_websocket.c b/src/modules/iotjs_module_websocket.c index 6a507a65a7..690f27bd92 100644 --- a/src/modules/iotjs_module_websocket.c +++ b/src/modules/iotjs_module_websocket.c @@ -17,7 +17,6 @@ #include #include "iotjs_def.h" -#include "iotjs_handlewrap.h" #include "iotjs_module_buffer.h" #include "iotjs_module_crypto.h" #include "iotjs_module_websocket.h" diff --git a/src/modules/linux/iotjs_module_uart-linux.c b/src/modules/linux/iotjs_module_uart-linux.c index 7a33f1f294..52fc13871d 100644 --- a/src/modules/linux/iotjs_module_uart-linux.c +++ b/src/modules/linux/iotjs_module_uart-linux.c @@ -18,6 +18,7 @@ #include #include +#include "iotjs_uv_handle.h" #include "modules/iotjs_module_uart.h" struct iotjs_uart_platform_data_s { @@ -100,7 +101,9 @@ jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, return jerry_create_undefined(); } -bool iotjs_uart_open(iotjs_uart_t* uart) { +bool iotjs_uart_open(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); int fd = open(iotjs_string_data(&uart->platform_data->device_path), O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { @@ -119,12 +122,15 @@ bool iotjs_uart_open(iotjs_uart_t* uart) { tcsetattr(fd, TCSANOW, &options); uart->device_fd = fd; - iotjs_uart_register_read_cb(uart); + iotjs_uart_register_read_cb((uv_poll_t*)uart_poll_handle); return true; } -bool iotjs_uart_write(iotjs_uart_t* uart) { +bool iotjs_uart_write(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); + int bytesWritten = 0; unsigned offset = 0; int fd = uart->device_fd; @@ -155,8 +161,9 @@ bool iotjs_uart_write(iotjs_uart_t* uart) { return true; } -void iotjs_uart_handlewrap_close_cb(uv_handle_t* handle) { - iotjs_uart_t* uart = (iotjs_uart_t*)handle->data; +void iotjs_uart_handle_close_cb(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); if (close(uart->device_fd) < 0) { DLOG(iotjs_periph_error_str(kUartOpClose)); diff --git a/src/modules/nuttx/iotjs_module_uart-nuttx.c b/src/modules/nuttx/iotjs_module_uart-nuttx.c index 5a3bf2f5dc..e7792a1c71 100644 --- a/src/modules/nuttx/iotjs_module_uart-nuttx.c +++ b/src/modules/nuttx/iotjs_module_uart-nuttx.c @@ -15,6 +15,8 @@ #include "modules/iotjs_module_uart.h" +#include "iotjs_uv_handle.h" + struct iotjs_uart_platform_data_s { iotjs_string_t device_path; }; @@ -39,7 +41,9 @@ jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, return jerry_create_undefined(); } -bool iotjs_uart_open(iotjs_uart_t* uart) { +bool iotjs_uart_open(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); int fd = open(iotjs_string_data(&uart->platform_data->device_path), O_RDWR | O_NOCTTY | O_NDELAY); @@ -48,12 +52,14 @@ bool iotjs_uart_open(iotjs_uart_t* uart) { } uart->device_fd = fd; - iotjs_uart_register_read_cb(uart); + iotjs_uart_register_read_cb((uv_poll_t*)uart); return true; } -bool iotjs_uart_write(iotjs_uart_t* uart) { +bool iotjs_uart_write(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); int bytesWritten = 0; unsigned offset = 0; int fd = uart->device_fd; @@ -83,8 +89,9 @@ bool iotjs_uart_write(iotjs_uart_t* uart) { return true; } -void iotjs_uart_handlewrap_close_cb(uv_handle_t* handle) { - iotjs_uart_t* uart = (iotjs_uart_t*)handle->data; +void iotjs_uart_handle_close_cb(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); if (close(uart->device_fd) < 0) { DLOG(iotjs_periph_error_str(kUartOpClose)); diff --git a/src/modules/tizen/iotjs_module_uart-tizen.c b/src/modules/tizen/iotjs_module_uart-tizen.c index d7325eb02c..79b433a813 100644 --- a/src/modules/tizen/iotjs_module_uart-tizen.c +++ b/src/modules/tizen/iotjs_module_uart-tizen.c @@ -15,6 +15,7 @@ #include +#include "iotjs_uv_handle.h" #include "modules/iotjs_module_uart.h" struct _peripheral_uart_s { @@ -108,7 +109,9 @@ jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, return jerry_create_undefined(); } -bool iotjs_uart_open(iotjs_uart_t* uart) { +bool iotjs_uart_open(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); iotjs_uart_platform_data_t* platform_data = uart->platform_data; IOTJS_ASSERT(platform_data); @@ -137,12 +140,14 @@ bool iotjs_uart_open(iotjs_uart_t* uart) { } uart->device_fd = platform_data->uart_h->fd; - iotjs_uart_register_read_cb(uart); + iotjs_uart_register_read_cb((uv_poll_t*)uart_poll_handle); return true; } -bool iotjs_uart_write(iotjs_uart_t* uart) { +bool iotjs_uart_write(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); iotjs_uart_platform_data_t* platform_data = uart->platform_data; IOTJS_ASSERT(platform_data); if (!platform_data->uart_h) { @@ -163,8 +168,9 @@ bool iotjs_uart_write(iotjs_uart_t* uart) { return true; } -void iotjs_uart_handlewrap_close_cb(uv_handle_t* handle) { - iotjs_uart_t* uart = (iotjs_uart_t*)handle->data; +void iotjs_uart_handle_close_cb(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); if (peripheral_uart_close(uart->platform_data->uart_h) != PERIPHERAL_ERROR_NONE) { diff --git a/src/modules/tizenrt/iotjs_module_uart-tizenrt.c b/src/modules/tizenrt/iotjs_module_uart-tizenrt.c index c14f524c4b..08b0c2c50f 100644 --- a/src/modules/tizenrt/iotjs_module_uart-tizenrt.c +++ b/src/modules/tizenrt/iotjs_module_uart-tizenrt.c @@ -19,6 +19,8 @@ #include "modules/iotjs_module_uart.h" +#include "iotjs_uv_handle.h" + struct iotjs_uart_platform_data_s { iotjs_string_t device_path; }; @@ -43,7 +45,9 @@ jerry_value_t iotjs_uart_set_platform_config(iotjs_uart_t* uart, return jerry_create_undefined(); } -bool iotjs_uart_open(iotjs_uart_t* uart) { +bool iotjs_uart_open(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); int fd = open(iotjs_string_data(&uart->platform_data->device_path), O_RDWR | O_NOCTTY | O_NDELAY); @@ -52,12 +56,14 @@ bool iotjs_uart_open(iotjs_uart_t* uart) { } uart->device_fd = fd; - iotjs_uart_register_read_cb(uart); + iotjs_uart_register_read_cb((uv_poll_t*)uart_poll_handle); return true; } -bool iotjs_uart_write(iotjs_uart_t* uart) { +bool iotjs_uart_write(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); int bytesWritten = 0; unsigned offset = 0; int fd = uart->device_fd; @@ -87,8 +93,9 @@ bool iotjs_uart_write(iotjs_uart_t* uart) { return true; } -void iotjs_uart_handlewrap_close_cb(uv_handle_t* handle) { - iotjs_uart_t* uart = (iotjs_uart_t*)handle->data; +void iotjs_uart_handle_close_cb(uv_handle_t* uart_poll_handle) { + iotjs_uart_t* uart = + (iotjs_uart_t*)IOTJS_UV_HANDLE_EXTRA_DATA(uart_poll_handle); if (close(uart->device_fd) < 0) { DLOG(iotjs_periph_error_str(kUartOpClose));