• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "napi/native_api.h"
17 #include "napi_memorymanagementuse_test.h"
18 
PrintHello(JSVM_Env env,JSVM_CallbackInfo info)19 static JSVM_Value PrintHello(JSVM_Env env, JSVM_CallbackInfo info)
20 {
21     JSVM_Value output;
22     OH_JSVM_CreateStringUtf8(env, "Hello world!", JSVM_AUTO_LENGTH, &output);
23     return output;
24 }
25 //OH_JSVM_AdjustExternalMemory:changeInBytes 0 +result not null
TestAdjustExternalMemoryCase01(JSVM_Env env,JSVM_CallbackInfo info)26 [[maybe_unused]] JSVM_Value TestAdjustExternalMemoryCase01(JSVM_Env env, JSVM_CallbackInfo info)
27 {
28     int64_t change = 0;
29     int64_t adjustedValue = 0;
30     JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
31     if (status != JSVM_OK) {
32         OH_JSVM_ThrowError(env, nullptr, "TestAdjustExternalMemoryCase01:AdjustExternalMemory Failed.");
33         return nullptr;
34     }
35 
36     bool setValue = true;
37     JSVM_Value retValue = nullptr;
38     OH_JSVM_GetBoolean(env, setValue, &retValue);
39     return retValue;
40 }
41 //changeInBytes 1024*1024 +result not null
TestAdjustExternalMemoryCase02(JSVM_Env env,JSVM_CallbackInfo info)42 [[maybe_unused]] JSVM_Value TestAdjustExternalMemoryCase02(JSVM_Env env, JSVM_CallbackInfo info)
43 {
44     int64_t change = 1024 * 1024;
45     int64_t adjustedValue = 0;
46     JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
47     if (status != JSVM_OK) {
48         OH_JSVM_ThrowError(env, nullptr, "TestAdjustExternalMemoryCase02:AdjustExternalMemory Failed.");
49         return nullptr;
50     }
51 
52     bool setValue = true;
53     JSVM_Value retValue = nullptr;
54     OH_JSVM_GetBoolean(env, setValue, &retValue);
55     return retValue;
56 }
57 //changeInBytes 1024*1024*1024 +result not null
TestAdjustExternalMemoryCase03(JSVM_Env env,JSVM_CallbackInfo info)58 [[maybe_unused]] JSVM_Value TestAdjustExternalMemoryCase03(JSVM_Env env, JSVM_CallbackInfo info)
59 {
60     int64_t change = 1024 * 1024 * 1024;
61     int64_t adjustedValue = 0;
62     JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
63     if (status != JSVM_OK) {
64         OH_JSVM_ThrowError(env, nullptr, "TestAdjustExternalMemoryCase03:AdjustExternalMemory Failed.");
65         return nullptr;
66     }
67 
68     bool setValue = true;
69     JSVM_Value retValue = nullptr;
70     OH_JSVM_GetBoolean(env, setValue, &retValue);
71     return retValue;
72 }
73 //changeInBytes 1024*1024 +result NULL
TestAdjustExternalMemoryCase04(JSVM_Env env,JSVM_CallbackInfo info)74 [[maybe_unused]] JSVM_Value TestAdjustExternalMemoryCase04(JSVM_Env env, JSVM_CallbackInfo info)
75 {
76     int64_t change = 0;
77     JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, nullptr);
78     if (status == JSVM_OK) {
79         OH_JSVM_ThrowError(env, nullptr, "TestAdjustExternalMemoryCase04:AdjustExternalMemory Execution abnormal.");
80         return nullptr;
81     }
82 
83     bool setValue = true;
84     JSVM_Value retValue = nullptr;
85     OH_JSVM_GetBoolean(env, setValue, &retValue);
86     return retValue;
87 }
88 //OH_JSVM_MemoryPressureNotification:JSVM_MEMORY_PRESSURE_LEVEL_NONE    no pressure
TestMemoryPressureNotificationCase01(JSVM_Env env,JSVM_CallbackInfo info)89 [[maybe_unused]] JSVM_Value TestMemoryPressureNotificationCase01(JSVM_Env env, JSVM_CallbackInfo info)
90 {
91     JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_NONE);
92     if (status != JSVM_OK) {
93         OH_JSVM_ThrowError(env, nullptr, "TestMemoryPressureNotificationCase01:MemoryPressureNotification Failed.");
94         return nullptr;
95     }
96 
97     bool setValue = true;
98     JSVM_Value retValue = nullptr;
99     OH_JSVM_GetBoolean(env, setValue, &retValue);
100     return retValue;
101 }
102 //JSVM_MEMORY_PRESSURE_LEVEL_MODERATE    moderate pressure
TestMemoryPressureNotificationCase02(JSVM_Env env,JSVM_CallbackInfo info)103 [[maybe_unused]] JSVM_Value TestMemoryPressureNotificationCase02(JSVM_Env env, JSVM_CallbackInfo info)
104 {
105     JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_MODERATE);
106     if (status != JSVM_OK) {
107         OH_JSVM_ThrowError(env, nullptr, "TestMemoryPressureNotificationCase02:MemoryPressureNotification Failed.");
108         return nullptr;
109     }
110 
111     bool setValue = true;
112     JSVM_Value retValue = nullptr;
113     OH_JSVM_GetBoolean(env, setValue, &retValue);
114     return retValue;
115 }
116 //JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL    critical pressure
TestMemoryPressureNotificationCase03(JSVM_Env env,JSVM_CallbackInfo info)117 [[maybe_unused]] JSVM_Value TestMemoryPressureNotificationCase03(JSVM_Env env, JSVM_CallbackInfo info)
118 {
119     JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
120     if (status != JSVM_OK) {
121         OH_JSVM_ThrowError(env, nullptr, "TestMemoryPressureNotificationCase03:MemoryPressureNotification Failed.");
122         return nullptr;
123     }
124 
125     bool setValue = true;
126     JSVM_Value retValue = nullptr;
127     OH_JSVM_GetBoolean(env, setValue, &retValue);
128     return retValue;
129 }
130 // CreateAndUseSnapshot call back
131 static JSVM_CallbackStruct param[] = {
132     {.data = nullptr, .callback = PrintHello},
133 };
134 static JSVM_CallbackStruct *method = param;
135 // CreateAndUseSnapshot method alias,JS call
136 static JSVM_PropertyDescriptor descriptor[] = {
137     {"printHello", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
138 };
139 //API:AdjustExternalMemory allocation of 1GB memory -> MemoryPressureNotification(JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL)
TestMemoryManagementuseCase01(JSVM_Env env,JSVM_CallbackInfo info)140 [[maybe_unused]] JSVM_Value TestMemoryManagementuseCase01(JSVM_Env env, JSVM_CallbackInfo info)
141 {
142     int64_t change = 1024 * 1024 * 1024;
143     int64_t adjustedValue = 0;
144     JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
145     if (status != JSVM_OK) {
146         OH_JSVM_ThrowError(env, nullptr, "TestMemoryManagementuseCase01:AdjustExternalMemory Failed.");
147         return nullptr;
148     }
149 
150     status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
151     if (status != JSVM_OK) {
152         OH_JSVM_ThrowError(env, nullptr, "TestMemoryManagementuseCase01:MemoryPressureNotification Failed.");
153         return nullptr;
154     }
155 
156     bool setValue = true;
157     JSVM_Value retValue = nullptr;
158     OH_JSVM_GetBoolean(env, setValue, &retValue);
159     return retValue;
160 }