1 /*
2 * Copyright (c) 2020 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 "sample_module.h"
17 #if (ENABLE_MODULE_REQUIRE_TEST == 1)
18 #if (TARGET_SIMULATOR != 1)
19 #include "js_async_work.h"
20 #endif
21 #include "ace_log.h"
22
23 namespace OHOS {
24 namespace ACELite {
TestCallFunc(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)25 JSIValue SampleModule::TestCallFunc(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
26 {
27 return JSI::CreateUndefined();
28 }
29
TestCallback(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)30 JSIValue SampleModule::TestCallback(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
31 {
32 JSIValue undefValue = JSI::CreateUndefined();
33 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
34 return undefValue;
35 }
36
37 JSIValue callback = JSI::GetNamedProperty(args[0], CB_CALLBACK);
38 JSIValue result = JSI::CreateObject();
39 const double defaultVal = 111;
40 JSI::SetNumberProperty(result, "x", defaultVal);
41 JSIValue argv[ARGC_ONE] = {result};
42 JSI::CallFunction(callback, thisVal, argv, ARGC_ONE);
43 JSI::ReleaseValueList(callback, result);
44 return undefValue;
45 }
46
TestStandardCallback(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)47 JSIValue SampleModule::TestStandardCallback(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
48 {
49 JSIValue undefValue = JSI::CreateUndefined();
50 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
51 return undefValue;
52 }
53 JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
54 JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
55 JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
56 // callbacks can be invoked on demand
57 JSI::CallFunction(success, thisVal, nullptr, 0);
58 JSI::CallFunction(complete, thisVal, nullptr, 0);
59 JSI::ReleaseValueList(success, fail, complete);
60 return undefValue;
61 }
62
63 #if (TARGET_SIMULATOR != 1)
64 struct AsyncParams : public MemoryHeap {
AsyncParamsOHOS::ACELite::AsyncParams65 AsyncParams() : result(nullptr), callback(nullptr), context(nullptr) {}
66
67 JSIValue result;
68 JSIValue callback;
69 JSIValue context;
70 };
71
Execute(void * data)72 static void Execute(void *data)
73 {
74 AsyncParams *params = static_cast<AsyncParams *>(data);
75 if (params == nullptr) {
76 return;
77 }
78 JSIValue callback = params->callback;
79 JSIValue result = params->result;
80 JSIValue context = params->context;
81
82 JSI::CallFunction(callback, context, &result, ARGC_ONE);
83 JSI::ReleaseValueList(callback, result, context);
84 delete params;
85 params = nullptr;
86 }
87 #endif
88
TestCallbackWithArgs(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)89 JSIValue SampleModule::TestCallbackWithArgs(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
90 {
91 JSIValue undefValue = JSI::CreateUndefined();
92 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
93 return undefValue;
94 }
95 JSIValue callback = JSI::GetNamedProperty(args[0], CB_CALLBACK);
96 double num = JSI::GetNumberProperty(args[0], "param1");
97 char *str = JSI::GetStringProperty(args[0], "param2");
98
99 JSIValue result = JSI::CreateObject();
100 JSI::SetNumberProperty(result, "param1", num);
101 if (str != nullptr) {
102 JSI::SetStringProperty(result, "param2", str);
103 JSI::ReleaseString(str);
104 }
105 #if (TARGET_SIMULATOR != 1)
106 AsyncParams *params = new AsyncParams();
107 if (params == nullptr) {
108 JSI::ReleaseValueList(result, callback);
109 return undefValue;
110 }
111 params->result = result;
112 params->callback = callback;
113 params->context = JSI::AcquireValue(thisVal);
114 if (!(JsAsyncWork::DispatchAsyncWork(Execute, static_cast<void *>(params)))) {
115 JSI::ReleaseValueList(result, callback, params->context);
116 delete params;
117 params = nullptr;
118 }
119 return undefValue;
120 #else // TARGET_SIMULATOR
121 JSI::CallFunction(callback, thisVal, &result, ARGC_ONE);
122 JSI::ReleaseValueList(result, callback);
123 return undefValue;
124 #endif // TARGET_SIMULATOR
125 }
126
TestStandardCallbackWithArgs(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)127 JSIValue SampleModule::TestStandardCallbackWithArgs(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
128 {
129 JSIValue undefValue = JSI::CreateUndefined();
130 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
131 return undefValue;
132 }
133 JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
134 JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
135 JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
136 double num = JSI::GetNumberProperty(args[0], "param1");
137 char *str = JSI::GetStringProperty(args[0], "param2");
138
139 JSIValue result = JSI::CreateObject();
140 JSI::SetNumberProperty(result, "param1", num);
141 if (str != nullptr) {
142 JSI::SetStringProperty(result, "param2", str);
143 JSI::ReleaseString(str);
144 }
145
146 // callbacks can be invoked on demand
147 JSIValue argv[ARGC_ONE] = {result};
148 JSI::CallFunction(success, thisVal, argv, ARGC_ONE);
149 JSI::CallFunction(fail, thisVal, argv, ARGC_ONE);
150 JSI::CallFunction(complete, thisVal, argv, ARGC_ONE);
151 JSI::ReleaseValueList(success, fail, complete, result);
152 return undefValue;
153 }
154
TestGeneralFunc(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)155 JSIValue SampleModule::TestGeneralFunc(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
156 {
157 JSIValue result;
158 if ((args == nullptr) || (argsNum < ARGC_TWO)) {
159 return JSI::CreateUndefined();
160 }
161
162 char *param1 = JSI::ValueToString(args[0]);
163 if (param1 == nullptr) {
164 return JSI::CreateUndefined();
165 }
166 double param2 = JSI::ValueToNumber(args[ARGC_ONE]);
167 result = JSI::CreateObject();
168 JSI::SetStringProperty(result, "prop1", param1);
169 JSI::SetNumberProperty(result, "prop2", param2);
170 JSI::ReleaseString(param1);
171
172 JSPropertyDescriptor desc;
173 const char * const propName = "prop3";
174 desc.getter = Getter;
175 desc.setter = Setter;
176 JSI::DefineNamedProperty(result, propName, desc);
177 return result;
178 }
179
180 int8_t SampleModule::counter_ = 0;
181
Setter(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)182 JSIValue SampleModule::Setter(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
183 {
184 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
185 return JSI::CreateUndefined();
186 }
187 double newValue = JSI::ValueToNumber(args[0]);
188 counter_ = (int8_t)newValue;
189
190 HILOG_DEBUG(HILOG_MODULE_ACE, "SampleModule: Setter called, setting: %{public}d", counter_);
191 return JSI::CreateUndefined();
192 }
193
Getter(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)194 JSIValue SampleModule::Getter(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
195 {
196 counter_++;
197 HILOG_DEBUG(HILOG_MODULE_ACE, "SampleModule: Getter called, returning: %{public}d", counter_);
198 return JSI::CreateNumber(counter_);
199 }
200
OnDestroy()201 void SampleModule::OnDestroy()
202 {
203 HILOG_DEBUG(HILOG_MODULE_ACE, "SampleModule: OnDestroy called");
204 }
205
OnTerminate()206 void SampleModule::OnTerminate()
207 {
208 HILOG_DEBUG(HILOG_MODULE_ACE, "SampleModule: OnTerminate called");
209 }
210 } // namespace ACELite
211 } // namespace OHOS
212
213 #endif // ENABLE_MODULE_REQUIRE_TEST
214