1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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 "task.h"
17
18 #include "helper/error_helper.h"
19 #include "helper/napi_helper.h"
20 #include "helper/object_helper.h"
21 #include "task_manager.h"
22 #include "utils/log.h"
23
24 namespace Commonlibrary::Concurrent::TaskPoolModule {
25 static constexpr char SETTRANSFERLIST_STR[] = "setTransferList";
26 using namespace Commonlibrary::Concurrent::Common::Helper;
27
TaskConstructor(napi_env env,napi_callback_info cbinfo)28 napi_value Task::TaskConstructor(napi_env env, napi_callback_info cbinfo)
29 {
30 // check argv count
31 size_t argc = NapiHelper::GetCallbackInfoArgc(env, cbinfo);
32 if (argc < 1) {
33 ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR, "taskpool:: create task need more than one param");
34 return nullptr;
35 }
36
37 // check 1st param is func
38 napi_value* args = new napi_value[argc];
39 ObjectScope<napi_value> scope(args, true);
40 napi_value thisVar;
41 napi_get_cb_info(env, cbinfo, &argc, args, &thisVar, nullptr);
42 if (!NapiHelper::IsFunction(args[0])) {
43 ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR, "taskpool:: the first param of task must be function");
44 return nullptr;
45 }
46 CreateTaskByFunc(env, thisVar, args[0], args, argc);
47 return thisVar;
48 }
49
CreateTaskByFunc(napi_env env,napi_value task,napi_value func,napi_value * args,size_t argc)50 void Task::CreateTaskByFunc(napi_env env, napi_value task, napi_value func, napi_value* args, size_t argc)
51 {
52 napi_value argsArray;
53 napi_create_array_with_length(env, argc - 1, &argsArray);
54 for (size_t i = 0; i < argc - 1; i++) {
55 napi_set_element(env, argsArray, i, args[i + 1]);
56 }
57
58 napi_value taskId = NapiHelper::CreateUint32(env, TaskManager::GetInstance().GenerateTaskId());
59
60 napi_value setTransferListFunc;
61 napi_create_function(env, SETTRANSFERLIST_STR, NAPI_AUTO_LENGTH, SetTransferList, NULL, &setTransferListFunc);
62
63 napi_property_descriptor properties[] = {
64 DECLARE_NAPI_PROPERTY(FUNCTION_STR, args[0]),
65 DECLARE_NAPI_PROPERTY(ARGUMENTS_STR, argsArray),
66 DECLARE_NAPI_PROPERTY(TASKID_STR, taskId),
67 DECLARE_NAPI_FUNCTION(SETTRANSFERLIST_STR, SetTransferList),
68 };
69 napi_define_properties(env, task, sizeof(properties) / sizeof(properties[0]), properties);
70 }
71
SetTransferList(napi_env env,napi_callback_info cbinfo)72 napi_value Task::SetTransferList(napi_env env, napi_callback_info cbinfo)
73 {
74 size_t argc = 1;
75 napi_value args[1];
76 napi_value thisVar;
77 napi_get_cb_info(env, cbinfo, &argc, args, &thisVar, nullptr);
78 if (argc > 1) {
79 ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR,
80 "taskpool:: the number of setTransferList parma must be less than 2");
81 return nullptr;
82 }
83 if (argc == 0) {
84 HILOG_DEBUG("taskpool:: set task params not transfer");
85 return nullptr;
86 }
87
88 // setTransferList(ArrayBuffer[]), check ArrayBuffer[]
89 if (!NapiHelper::IsArray(args[0])) {
90 ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR, "taskpool:: setTransferList first param must be array");
91 return nullptr;
92 }
93 uint32_t arrayLength = NapiHelper::GetArrayLength(env, args[0]);
94 for (size_t i = 0; i < arrayLength; i++) {
95 napi_value elementVal;
96 napi_get_element(env, args[0], i, &elementVal);
97 if (!NapiHelper::IsArrayBuffer(elementVal)) {
98 ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR,
99 "taskpool:: the element in array must be arraybuffer");
100 return nullptr;
101 }
102 }
103 HILOG_DEBUG("taskpool:: check setTransferList param success");
104
105 napi_set_named_property(env, thisVar, TRANSFERLIST_STR, args[0]);
106 return nullptr;
107 }
108 } // namespace Commonlibrary::Concurrent::TaskPoolModule