Update Iot.js
[platform/upstream/iotjs.git] / src / iotjs_reqwrap.c
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "iotjs_def.h"
17 #include "iotjs_reqwrap.h"
18
19
20 void iotjs_reqwrap_initialize(iotjs_reqwrap_t* reqwrap,
21                               const iotjs_jval_t* jcallback,
22                               uv_req_t* request) {
23   IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_reqwrap_t, reqwrap);
24   IOTJS_ASSERT(iotjs_jval_is_function(jcallback));
25   _this->jcallback = iotjs_jval_create_copied(jcallback);
26   _this->request = request;
27   _this->request->data = reqwrap;
28 }
29
30
31 void iotjs_reqwrap_destroy(iotjs_reqwrap_t* reqwrap) {
32   IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_reqwrap_t, reqwrap);
33   iotjs_jval_destroy(&_this->jcallback);
34 }
35
36
37 static void iotjs_reqwrap_validate(iotjs_reqwrap_t* reqwrap) {
38   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_reqwrap_t, reqwrap);
39   IOTJS_ASSERT(_this->request->data == reqwrap);
40 }
41
42
43 const iotjs_jval_t* iotjs_reqwrap_jcallback(iotjs_reqwrap_t* reqwrap) {
44   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_reqwrap_t, reqwrap);
45   iotjs_reqwrap_validate(reqwrap);
46   return &_this->jcallback;
47 }
48
49
50 uv_req_t* iotjs_reqwrap_req(iotjs_reqwrap_t* reqwrap) {
51   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_reqwrap_t, reqwrap);
52   iotjs_reqwrap_validate(reqwrap);
53   return _this->request;
54 }
55
56
57 iotjs_reqwrap_t* iotjs_reqwrap_from_request(uv_req_t* req) {
58   iotjs_reqwrap_t* reqwrap = req->data;
59   iotjs_reqwrap_validate(reqwrap);
60   return reqwrap;
61 }