1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development 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 #include "napi_executetasks_test.h"
16 const size_t BUF_SIZE_255 = 255;
17 const size_t NUM_SIZE_3 = 3;
18 //OH_JSVM_PumpMessageLoop
TestPumpMessageLoopTest1(JSVM_Env env,JSVM_CallbackInfo info)19 [[maybe_unused]] JSVM_Value TestPumpMessageLoopTest1(JSVM_Env env, JSVM_CallbackInfo info)
20 {
21 size_t argc = 1;
22 JSVM_Value argv[1] = {nullptr};
23 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
24
25 bool bRst = false;
26 JSVM_Status status = OH_JSVM_PumpMessageLoop(nullptr, &bRst);
27 if (status != JSVM_OK) {
28 OH_JSVM_ThrowError(env, nullptr, "TestPumpMessageLoopTest1: OH_JSVM_PumpMessageLoop Failed");
29 return nullptr;
30 }
31 if (bRst) {
32 OH_JSVM_ThrowError(env, nullptr, "TestPumpMessageLoopTest1: bRst is true");
33 return nullptr;
34 }
35
36 bool result = true;
37 JSVM_Value value = nullptr;
38 OH_JSVM_GetBoolean(env, result, &value);
39 return value;
40 }
41 //OH_JSVM_PerformMicrotaskCheckpoint
42 //vm is null
TestPerformMicrotaskCheckpointTest1(JSVM_Env env,JSVM_CallbackInfo info)43 [[maybe_unused]] JSVM_Value TestPerformMicrotaskCheckpointTest1(JSVM_Env env, JSVM_CallbackInfo info)
44 {
45 size_t argc = 1;
46 JSVM_Value argv[1] = {nullptr};
47 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
48
49 bool bRst = false;
50 JSVM_VM vm = nullptr;
51 JSVM_CreateVMOptions optionVm;
52 memset_s(&optionVm, sizeof(optionVm), 0, sizeof(optionVm));
53 JSVM_Status status = OH_JSVM_CreateVM(&optionVm, &vm);
54 if (status != JSVM_OK) {
55 OH_JSVM_ThrowError(env, nullptr,
56 "TestPerformMicrotaskCheckpointTest1: OH_JSVM_PerformMicrotaskCheckpoint Failed");
57 return nullptr;
58 }
59 status = OH_JSVM_PerformMicrotaskCheckpoint(vm);
60 if (status != JSVM_OK) {
61 OH_JSVM_ThrowError(env, nullptr,
62 "TestPerformMicrotaskCheckpointTest1: OH_JSVM_PerformMicrotaskCheckpoint Failed");
63 return nullptr;
64 }
65
66 bool result = true;
67 JSVM_Value value = nullptr;
68 OH_JSVM_GetBoolean(env, result, &value);
69 return value;
70 }
71 // pump -- perform
ConsoleInfo(JSVM_Env env,JSVM_CallbackInfo info)72 [[maybe_unused]] JSVM_Value ConsoleInfo(JSVM_Env env, JSVM_CallbackInfo info)
73 {
74 size_t argc = 1;
75 JSVM_Value args[1];
76 char log[BUF_SIZE_255] = "";
77 size_t logLength;
78 OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL);
79
80 OH_JSVM_GetValueStringUtf8(env, args[0], log, BUF_SIZE_255, &logLength);
81 log[BUF_SIZE_255] = 0;
82 printf("JSVM API TEST: %s", log);
83 return nullptr;
84 }
TestExecuteTasksCombinationTest1(JSVM_Env env,JSVM_CallbackInfo info)85 [[maybe_unused]] JSVM_Value TestExecuteTasksCombinationTest1(JSVM_Env env, JSVM_CallbackInfo info)
86 {
87 size_t argc = 1;
88 JSVM_Value argv[1] = {nullptr};
89 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
90
91 // create vm
92 JSVM_VM vm;
93 JSVM_CreateVMOptions options;
94 memset_s(&options, sizeof(options), 0, sizeof(options));
95 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
96 if (status != JSVM_OK) {
97 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CreateVM Failed");
98 return nullptr;
99 }
100 JSVM_VMScope vm_scope;
101 status = OH_JSVM_OpenVMScope(vm, &vm_scope);
102 if (status != JSVM_OK) {
103 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_OpenVMScope Failed");
104 return nullptr;
105 }
106 // consoleinfo
107 JSVM_CallbackStruct param[] = {
108 {.data = nullptr, .callback = ConsoleInfo},
109 };
110 JSVM_PropertyDescriptor descriptor[] = {
111 {"consoleinfo", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
112 };
113 JSVM_Env env1;
114 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
115 if (status != JSVM_OK) {
116 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CreateEnv Failed");
117 return nullptr;
118 }
119 JSVM_EnvScope envScope;
120 status = OH_JSVM_OpenEnvScope(env1, &envScope);
121 if (status != JSVM_OK) {
122 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_OpenEnvScope Failed");
123 return nullptr;
124 }
125 JSVM_HandleScope handleScope;
126 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
127 if (status != JSVM_OK) {
128 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_OpenHandleScope Failed");
129 return nullptr;
130 }
131 const char *strTask = R"JS(
132 new Promise((resolve, reject) => {
133 resolve(1)
134 })
135 .then(function(obj) {
136 consoleinfo("Called with instance " + obj);
137 }).catch(function(err) {
138 consoleinfo("Called with error ");
139 });
140 )JS";
141 JSVM_Value sourceCodeValue;
142 status = OH_JSVM_CreateStringUtf8(env1, strTask, JSVM_AUTO_LENGTH, &sourceCodeValue);
143 if (status != JSVM_OK) {
144 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
145 return nullptr;
146 }
147 JSVM_Script script;
148 status = OH_JSVM_CompileScript(env1, sourceCodeValue, nullptr, 0, true, nullptr, &script);
149 if (status != JSVM_OK) {
150 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CompileScript Failed");
151 return nullptr;
152 }
153 JSVM_Value result;
154 status = OH_JSVM_RunScript(env1, script, &result);
155 if (status != JSVM_OK) {
156 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_RunScript Failed");
157 return nullptr;
158 }
159 bool rst = false;
160 for (int i = 0; i < NUM_SIZE_3; i++) { // 3: cycles
161 //如果消息队列中没有任务启动,则rst设置为false。
162 JSVM_Status flag1 = OH_JSVM_PumpMessageLoop(vm, &rst);
163 JSVM_Status flag2 = OH_JSVM_PerformMicrotaskCheckpoint(vm);
164 if (rst && (flag1 == JSVM_OK) && (flag2 == JSVM_OK)) {
165 sleep(NUM_SIZE_3);
166 break;
167 }
168 }
169 // close and destroy
170 status = OH_JSVM_CloseHandleScope(env1, handleScope);
171 if (status != JSVM_OK) {
172 OH_JSVM_ThrowError(env1, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CloseHandleScope Failed");
173 return nullptr;
174 }
175 status = OH_JSVM_CloseEnvScope(env1, envScope);
176 if (status != JSVM_OK) {
177 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CloseEnvScope Failed");
178 return nullptr;
179 }
180 status = OH_JSVM_DestroyEnv(env1);
181 if (status != JSVM_OK) {
182 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_DestroyEnv Failed");
183 return nullptr;
184 }
185 status = OH_JSVM_CloseVMScope(vm, vm_scope);
186 if (status != JSVM_OK) {
187 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_CloseVMScope Failed");
188 return nullptr;
189 }
190 status = OH_JSVM_DestroyVM(vm);
191 if (status != JSVM_OK) {
192 OH_JSVM_ThrowError(env, nullptr, "TestExecuteTasksCombinationTest1: OH_JSVM_DestroyVM Failed");
193 return nullptr;
194 }
195
196 bool result1 = true;
197 JSVM_Value value = nullptr;
198 OH_JSVM_GetBoolean(env, result1, &value);
199 return value;
200 }