1# JSVM-API Development Specifications 2 3## Lifecycle Management 4 5**[Rule]** Properly use **OH_JSVM_OpenHandleScope** and **OH_JSVM_CloseHandleScope** to minimize the lifecycle of **JSVM_Value** and prevent memory leaks. 6 7Each **JSVM_Value** belongs to a specific **HandleScope** instance, which is created by **OH_JSVM_OpenHandleScope** and closed by **OH_JSVM_CloseHandleScope**. After a **HandleScope** instance is closed, the corresponding **JSVM_Value** will be automatically released. 8 9> **NOTE** 10> 11> - **JSVM_Value** can be created only after **HandleScope** is opened; otherwise, the application may crash. Node-API does not have this restriction. 12> - **JSVM_Value** cannot be used after the corresponding **HandleScope** is closed. To hold **JSVM_Value** persistently, call **OH_JSVM_CreateReference** to convert **JSVM_Value** to **JSVM_Ref**. 13> - The scopes (including **JSVM_VMScope**, **JSVM_EnvScope**, and **JSVM_HandleScope**) must be closed in reverse order. The scope opened first must be closed last. Otherwise, the application may crash. 14 15**Example (scope closing error)** 16``` 17// If JSVM_VMScope is not closed in reverse order, the application may crash. 18JSVM_VM vm; 19JSVM_CreateVMOptions options; 20OH_JSVM_CreateVM(&options, &vm); 21 22JSVM_VMScope vmScope1, vmScope2; 23OH_JSVM_OpenVMScope(vm, &vmScope1); 24OH_JSVM_OpenVMScope(vm, &vmScope2); 25 26// You need to close vmScope2 and then vmScope1. 27OH_JSVM_CloseVMScope(vm, vmScope1); 28OH_JSVM_CloseVMScope(vm, vmScope2); 29OH_JSVM_DestroyVM(vm); 30``` 31 32 33**Encapsulation in C++** 34 35``` 36class HandleScopeWrapper { 37 public: 38 HandleScopeWrapper(JSVM_Env env) : env(env) { 39 OH_JSVM_OpenHandleScope(env, &handleScope); 40 } 41 42 ~HandleScopeWrapper() { 43 OH_JSVM_CloseHandleScope(env, handleScope); 44 } 45 46 HandleScopeWrapper(const HandleScopeWrapper&) = delete; 47 HandleScopeWrapper& operator=(const HandleScopeWrapper&) = delete; 48 HandleScopeWrapper(HandleScopeWrapper&&) = delete; 49 void* operator new(size_t) = delete; 50 void* operator new[](size_t) = delete; 51 52 protected: 53 JSVM_Env env; 54 JSVM_HandleScope handleScope; 55}; 56``` 57 58**Example** 59 60```c++ 61// When JSVM-API is frequently called to create JS objects in the for loop, use handle_scope to release resources in a timely manner when they are no longer used. 62// In the following example, the lifecycle of the local variable res ends at the end of each loop. To prevent memory leaks, scope is used to release the JS object in a timely manner. 63// After each for loop ends, trigger the destructor function of HandleScopeWrapper to release the JS object held by scope. 64for (int i = 0; i < 100000; i++) 65{ 66 HandleScopeWrapper scope(env); 67 JSVM_Value res; 68 OH_JSVM_CreateObject(env, &res); 69 if (i == 1000) { 70 // After the loop exits, the HandleScopeWrapper destructor is automatically called to release resources. 71 break; 72 } 73} 74``` 75 76## Context Sensitive in Multiple JSVM Instances 77 78**[Rule]** Do not use JSVM-API to access JS objects across JSVM instances. 79 80A JSVM instance is an independent running environment. Operations such as creating and accessing a JS object must be performed in the same JSVM instance. If an object is operated in different JSVM instances, the application may crash. A JSVM instance is represented as a **JSVM_Env** in APIs. 81 82**Example (incorrect)** 83 84```c++ 85// Create a string object with value of "bar" in env1. 86OH_JSVM_CreateStringUtf8(env1, "value1", JSVM_AUTO_LENGTH , &string); 87// Create an object in env2 and set the string object to this object. 88JSVM_Status status = OH_JSVM_CreateObject(env2, &object); 89if (status != JSVM_OK) 90{ 91 return; 92} 93 94status = OH_JSVM_SetNamedProperty(env2, object, "string1", string); 95if (status != JSVM_OK) 96{ 97 return; 98} 99``` 100 101A JS object belongs to a specific **JSVM_Env**. Therefore, you cannot set an object of env1 to an object of env2. If the object of env1 is accessed in env2, the application may crash. 102 103## JSVM Instance Shared by Multiple Threads 104 105[**Rule**] When multiple threads use the same JSVM instance, use a lock to ensure that the JSVM instance can be executed in only one thread at a time. If multiple threads use the JSVM instance at the same time, the application may crash. 106 107> **NOTE** 108> 109> - You can use **OH_JSVM_IsLocked** to check whether the calling thread holds the lock of the JSVM instance instead of setting a loop to wait for other threads to release the lock. 110> - Nested use of **OH_JSVM_AcquireLock** in the same thread will not cause deadlock. 111> - When using **OH_JSVM_ReleaseLock**, you need to check whether it is at the outermost layer to prevent the inner layer from releasing the lock of the entire thread when **OH_JSVM_AcquireLock** is nested in the same thread. 112> - After **OH_JSVM_AcquireLock** is called, use **OH_JSVM_OpenHandleScope** to enable the JSVM instance to enter the thread. After **OH_JSVM_ReleaseLock** is called, use **OH_JSVM_ReleaseLock** to enable the JSVM instance to exit the thread. 113> - A JSVM instance cannot be nested across threads. If you need to temporarily change the thread that uses the JSVM instance, ensure that **JSVM_Value** is saved as **JSVM_Ref**. After the lock is released, **JSVM_Value** cannot be accessed. 114> - The sequence of obtaining resources is as follows: Lock -> VMScope -> EnvScope -> HandleScope. The sequence of releasing resources is the opposite. An incorrect sequence may cause the application to crash. 115 116 117 118**Encapsulation in C++** 119 120``` 121class LockWrapper { 122 public: 123 // Constructor, lock acquisition, VMScope, and EnvScope. 124 LockWrapper(JSVM_Env env) : env(env) { 125 OH_JSVM_IsLocked(env, &isLocked); 126 if (!isLocked) { 127 OH_JSVM_AcquireLock(env); 128 OH_JSVM_GetVM(env, &vm); 129 OH_JSVM_OpenVMScope(vm, &vmScope); 130 OH_JSVM_OpenEnvScope(env, &envScope); 131 } 132 } 133 134 // Destructor used to release EnvScope, VMScope, and lock. 135 ~LockWrapper() { 136 if (!isLocked) { 137 OH_JSVM_CloseEnvScope(env, envScope); 138 OH_JSVM_CloseVMScope(vm, vmScope); 139 OH_JSVM_ReleaseLock(env); 140 } 141 } 142 143 LockWrapper(const LockWrapper&) = delete; 144 LockWrapper& operator=(const LockWrapper&) = delete; 145 LockWrapper(LockWrapper&&) = delete; 146 void* operator new(size_t) = delete; 147 void* operator new[](size_t) = delete; 148 149 private: 150 JSVM_Env env; 151 JSVM_EnvScope envScope; 152 JSVM_VMScope vmScope; 153 JSVM_VM vm; 154 bool isLocked; 155}; 156``` 157 158 159 160**Example (correct)** 161 162``` 163// This example demonstrates how to use a JSVM across threads. 164// Thread t1 obtains the lock and call JSVM-API. 165// Thread t2 is blocked when thread t1 obtained the lock, continues to call JSVM-API when t1 releases the lock. 166static napi_value Add([[maybe_unused]] napi_env _env, [[maybe_unused]] napi_callback_info _info) { 167 static JSVM_VM vm; 168 static JSVM_Env env; 169 if (aa == 0) { 170 OH_JSVM_Init(nullptr); 171 aa++; 172 // create vm 173 JSVM_CreateVMOptions options; 174 memset(&options, 0, sizeof(options)); 175 OH_JSVM_CreateVM(&options, &vm); 176 // Create env. 177 OH_JSVM_CreateEnv(vm, 0, nullptr, &env); 178 } 179 180 std::thread t1([]() { 181 LockWrapper lock(env); 182 JSVM_HandleScope handleScope; 183 OH_JSVM_OpenHandleScope(env, &handleScope); 184 JSVM_Value value; 185 JSVM_Status rst = OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value 186 if (rst == JSVM_OK) { 187 OH_LOG_INFO(LOG_APP, "JSVM:t1 OH_JSVM_CreateInt32 suc"); 188 } else { 189 OH_LOG_ERROR(LOG_APP, "JSVM:t1 OH_JSVM_CreateInt32 fail"); 190 } 191 int32_t num1; 192 OH_JSVM_GetValueInt32(env, value, &num1); 193 OH_LOG_INFO(LOG_APP, "JSVM:t1 num1 = %{public}d", num1); 194 OH_JSVM_CloseHandleScope(env, handleScope); 195 }); 196 std::thread t2([]() { 197 LockWrapper lock(env); 198 JSVM_HandleScope handleScope; 199 OH_JSVM_OpenHandleScope(env, &handleScope); 200 JSVM_Value value; 201 JSVM_Status rst = OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value 202 if (rst == JSVM_OK) { 203 OH_LOG_INFO(LOG_APP, "JSVM:t2 OH_JSVM_CreateInt32 suc"); 204 } else { 205 OH_LOG_ERROR(LOG_APP, "JSVM:t2 OH_JSVM_CreateInt32 fail"); 206 } 207 int32_t num1; 208 OH_JSVM_GetValueInt32(env, value, &num1); 209 OH_LOG_INFO(LOG_APP, "JSVM:t2 num1 = %{public}d", num1); 210 OH_JSVM_CloseHandleScope(env, handleScope); 211 }); 212 t1.detach(); 213 t2.detach(); 214 return nullptr; 215} 216``` 217 218## Obtaining Arguments Passed from JS 219 220**[Rule]** When **argv** in **OH_JSVM_GetCbInfo** is not **nullptr**, the length of **argv** must be greater than or equal to **argc**. 221 222If **argv** is not **nullptr**, the arguments actually passed from JS will be written to **argv** in **OH_JSVM_GetCbInfo** based on the value of **argc**. If there are more arguments than the provided count, only the requested number of arguments are copied. If there are fewer arguments provided than the claimed, the rest of **argv** is filled with values that represent **undefined**. 223 224**Example (incorrect)** 225 226```cpp 227static JSVM_Value IncorrectDemo1(JSVM_Env env, JSVM_CallbackInfo info) { 228 // argc is not correctly initialized and is set to a random value. If the length of argv is less than the number of arguments specified by argc, data overwriting occurs. 229 size_t argc; 230 JSVM_Value argv[10] = {nullptr}; 231 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 232 return nullptr; 233} 234 235static JSVM_Value IncorrectDemo2(JSVM_Env env, JSVM_CallbackInfo info) { 236 // If the number of arguments specified by argc is greater than the length of argv, out-of-bounds write will occur when data is written to argv in napi_get_cb_info. 237 size_t argc = 5; 238 JSVM_Value argv[3] = {nullptr}; 239 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 240 return nullptr; 241} 242``` 243 244**Example (correct)** 245 246```cpp 247static JSVM_Value GetArgvDemo1(napi_env env, JSVM_CallbackInfo info) { 248 size_t argc = 0; 249 // Pass in nullptr to argv to obtain the actual number of arguments passed by JS. 250 OH_JSVM_GetCbInfo(env, info, &argc, nullptr, nullptr, nullptr); 251 // If 0 is passed by JS, the subsequent logic is not executed. 252 if (argc == 0) { 253 return nullptr; 254 } 255 // Create an array to obtain the arguments passed by JS. 256 JSVM_Value* argv = new JSVM_Value[argc]; 257 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 258 // Service code. 259 // ... ... 260 // argv is an object created by new and must be manually released when it is not required. 261 delete argv; 262 return nullptr; 263} 264 265static JSVM_Value GetArgvDemo2(napi_env env, JSVM_CallbackInfo info) { 266 size_t argc = 2; 267 JSVM_Value* argv[2] = {nullptr}; 268 // The arguments (of the quantity specified by argc) passed from JS or undefined will be written to argv of OH_JSVM_GetCbInfo. 269 OH_JSVM_GetCbInfo(env, info, &argc, nullptr, nullptr, nullptr); 270 // Service code. 271 // ... ... 272 return nullptr; 273} 274``` 275 276## Exception Handling 277 278**[Suggestion]** Any exception occurred in a JSVM-API call should be handled in a timely manner. Otherwise, unexpected behavior may occur. 279 280 Exception handling can be classified into the following types based on the primary/secondary relationship: 281 2821. If a C++ exception occurs when the JSVM executes a C++ callback function (JS is primary and native is secondary), the exception needs to be thrown to the JSVM. The following example demonstrates the implementation of the C++ callback function in three difference scenarios. 283 284 > **NOTE**<br>If the JSVM-API call in a C++ callback fails, you can either throw an exception to the JSVM or not. To throw an exception, ensure that there is no pending exception in the JSVM. If you choose not to throw an exception, JS **try-catch** can capture such JS exceptions. For details, see **NativeFunctionExceptionDemo3**. 285 286 ```c++ 287 // JSVM is primary, and native is secondary. 288 void DoSomething() { 289 throw("Do something failed"); 290 } 291 292 // Demo 1: Throw the exception captured in C++ to the JSVM. 293 JSVM_Value NativeFunctionExceptionDemo1(JSVM_Env env, JSVM_CallbackInfo info) { 294 try { 295 DoSomething(); 296 } catch (const char *ex) { 297 OH_JSVM_ThrowError(env, nullptr, ex); 298 return nullptr; 299 } 300 return nullptr; 301 } 302 303 // Demo 2: Throw an exception to the JSVM when a JSVM-API call fails. 304 JSVM_Value NativeFunctionExceptionDemo2(JSVM_Env env, JSVM_CallbackInfo info) { 305 JSVM_Value JSBool = nullptr; 306 bool value = false; 307 auto status = OH_JSVM_GetValueBool(env, JSBool, &value); 308 if (status != JSVM_OK) { 309 OH_JSVM_ThrowError(env, nullptr, "Get bool value failed"); 310 return nullptr; 311 } 312 return nullptr; 313 } 314 315 // Demo 3: Add an exception to the JSVM for handling when the JSVM-API call fails. In this case, you do not need to throw exceptions to the JSVM. 316 JSVM_Value NativeFunctionExceptionDemo3(JSVM_Env env, JSVM_CallbackInfo info) { 317 std::string sourcecodestr = R"JS( 318 throw Error('Error throw from js'); 319 )JS"; 320 JSVM_Value sourcecodevalue = nullptr; 321 OH_JSVM_CreateStringUtf8(env, sourcecodestr.c_str(), sourcecodestr.size(), &sourcecodevalue); 322 JSVM_Script script; 323 auto status = OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script); 324 JSVM_Value result; 325 // Execute the JS script and throw a JS exception during the execution. 326 status = OH_JSVM_RunScript(env, script, &result); 327 if (status != JSVM_OK) { 328 bool isPending = false; 329 // If an exception already exists, do not throw the exception to the JSVM. 330 // If a new exception needs to be thrown, handle the pending exception in the JSVM first. 331 if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { 332 return nullptr; 333 } 334 OH_JSVM_ThrowError(env, nullptr, "Runscript failed"); 335 return nullptr; 336 } 337 return nullptr; 338 } 339 340 // Bind NativeFunction to the JSVM. The process is omitted here. 341 std::string sourcecodestr = R"JS( 342 // The console log needs to be implemented. 343 try { 344 // Call the native function. 345 NativeFunction() 346 } catch (e) { 347 // Handle the exception in C/C++. 348 consolelog(e.message) 349 } 350 )JS"; 351 JSVM_Value sourcecodevalue = nullptr; 352 OH_JSVM_CreateStringUtf8(env, sourcecodestr.c_str(), sourcecodestr.size(), &sourcecodevalue); 353 JSVM_Script script; 354 auto status = OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script); 355 OH_LOG_INFO(LOG_APP, "JSVM API TEST: %{public}d", (uint32_t)status); 356 JSVM_Value result; 357 // Execute the JS script to call native methods from JS. 358 status = OH_JSVM_RunScript(env, script, &result); 359 ``` 360 3612. If JSVM-API (native is primary and JS is secondary) fails to be called from C++, clear the pending exceptions in JSVM to prevent the impact on subsequent JSVM-API execution and set a branch to handling C++ exception (or throw a C++ exception). 362 ``` 363 std::string sourcecodestr = R"JS( 364 throw Error('Error throw from js'); 365 )JS"; 366 JSVM_Value sourcecodevalue = nullptr; 367 OH_JSVM_CreateStringUtf8(env, sourcecodestr.c_str(), sourcecodestr.size(), &sourcecodevalue); 368 JSVM_Script script; 369 auto status = OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script); 370 // Exception handling. 371 if (status != JSVM_OK) { 372 JSVM_Value error = nullptr; 373 // Obtain and clear the exception. 374 CALL_JSVM(OH_JSVM_GetAndClearLastException((env), &error)); 375 // Handle the exception, for example, printing information. The code is omitted here. 376 // Throw a C++ exception or stop function execution. 377 throw "JS Compile Error"; 378 } 379 JSVM_Value result; 380 // Execute the JS script and throw a JS exception during the execution. 381 status = OH_JSVM_RunScript(env, script, &result); 382 383 // Exception handling. 384 if (status != JSVM_OK) { 385 JSVM_Value error = nullptr; 386 // Obtain and clear the exception. 387 CALL_JSVM(OH_JSVM_GetAndClearLastException((env), &error)); 388 // Handle the exception, for example, printing information. The code is omitted here. 389 // Throw a C++ exception or stop function execution. 390 throw "JS RunScript Error"; 391 } 392 393 ``` 394 395## Binding Object Context 396 397**[Rule]**: The JS function and object generated by JSVM-API can be accessed from JS only after they are bound to the context. The **const char *** parameter in **OH_JSVM_CreateFunction** is the property **name** of the created function, without pointing to the function in the context. This rule also applies to the class and object generated by JSVM-API. 398 399**Example** 400 401``` 402JSVM_Value JSFunc = nullptr; 403const char *name = "NativeFunction"; 404JSVM_CallbackStruct cb = {NativeFunction, nullptr}; 405// Create a JS function, whose property "name" is set to "NativeFunction". 406OH_JSVM_CreateFunction(env, name, strlen(name), &cb, &JSFunc); 407// Bind the function to the context. 408// Obtain the global object of the context. 409JSVM_Value global = nullptr; 410OH_JSVM_GetGlobal(env, &global); 411// Create the JS string "FunctionNameInJSContext." 412JSVM_Value key = nullptr; 413OH_JSVM_CreateStringLatin1(env, "FunctionNameInJSContext", JSVM_AUTO_LENGTH, &key); 414// Set the global property "FunctionNameInJSContext" to "JSFunc". This binds the function to the context. 415OH_JSVM_SetProperty(env, global, key, JSFunc); 416// Call the function from JS. 417std::string sourcecodestr = R"JS( 418 // The console log needs to be implemented. 419 FunctionNameInJSContext() // The call is successful. 420 consolelog(FunctionNameInJSContext.name) // Print "NativeFunction." 421 try { 422 NativeFunction() // If the function cannot be found, throw an exception. 423 } catch (e) { 424 consolelog(e.message) 425 } 426)JS"; 427``` 428