1# Working with Strings Using JSVM-API 2 3## Introduction 4 5This topic walks you through on how to use JSVM-API to convert data between native strings and JavaScript (JS) strings. 6 7## Basic Concepts 8 9As the common data type in programming, string is a sequence of characters used to represent text. It can also be used to build user interface (UI) elements such as labels, buttons, and text boxes, process user input, and validate and format input data. Different encodings support different character sets and languages. Major encoding schemes include the following: 10 11- ASCII<br>ASCII is one of the earliest character encoding schemes. It uses 7 bits to represent English letters, digits, and some basic symbols. It serves as the foundation for encoding schemes. 12- UTF-8<br>UTF-8 is a variable-length encoding scheme that can represent any Unicode character. It uses 8 bits per character and uses byte sequences of different lengths depending on the range of the character. UTF-8 is widely used for web content. 13- UTF-16<br>UTF-16 is a fixed-length or variable-length encoding scheme that uses 16 bits per character. It can represent all Unicode characters and is suitable for larger character sets. 14- ISO-8859-1 (Latin-1)<br>ISO-8859-1 is a single-byte coding scheme that uses 8 bits per character. It is mainly used to represent Latin alphabet characters and commonly used in European languages. 15 16## Available APIs 17 18| API | Description | 19|----------------------------|--------------------------------| 20| OH_JSVM_GetValueStringUtf8 | Obtains the UTF8-encoded string from a JS string.| 21| OH_JSVM_CreateStringUtf8 | Creates a JS string object from a UTF8-encoded C string.| 22| OH_JSVM_GetValueStringUtf16 | Obtains the UTF16-encoded string from a JS string.| 23| OH_JSVM_CreateStringUtf16 | Creates a JS string from a UTF16-encoded C string.| 24| OH_JSVM_GetValueStringLatin1 | Obtains the ISO-8859-1-encoded string from a JS string.| 25| OH_JSVM_CreateStringLatin1 | Creates a JS string object from an ISO-8859-1-encoded C string. ISO-8859-1 is also referred to as Latin-1.| 26 27## Example 28 29If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ code involved in string management. 30 31### OH_JSVM_GetValueStringUtf8 32 33Call **OH_JSVM_GetValueStringUtf8** to convert a JS string into a UTF-8-encoded string. 34 35CPP code: 36 37```cpp 38// hello.cpp 39#include "napi/native_api.h" 40#include "ark_runtime/jsvm.h" 41#include <hilog/log.h> 42#include <cstdlib> 43// Define OH_JSVM_GetValueStringUtf8. 44static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) 45{ 46 size_t argc = 1; 47 JSVM_Value args[1] = {nullptr}; 48 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 49 size_t length = 0; 50 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length); 51 char *buf = (char *)malloc(length + 1); 52 status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length); 53 if (status != JSVM_OK) { 54 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf8 fail"); 55 free(buf); 56 return nullptr; 57 } else { 58 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf8 success: %{public}s", buf); 59 } 60 JSVM_Value result = nullptr; 61 OH_JSVM_CreateStringUtf8(env, buf, length, &result); 62 free(buf); 63 return result; 64} 65// Register the GetValueStringUtf8 callback. 66static JSVM_CallbackStruct param[] = { 67 {.data = nullptr, .callback = GetValueStringUtf8}, 68}; 69static JSVM_CallbackStruct *method = param; 70// Alias for the GetValueStringUtf8 method to be called from JS. 71static JSVM_PropertyDescriptor descriptor[] = { 72 {"getValueStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 73}; 74 75// Call the C++ code from JS. 76const char *srcCallNative = R"JS( 77 let data = "aaBC+-$%^Hello 123"; 78 let script = getValueStringUtf8(data); 79)JS"; 80``` 81 82**Expected output** 83 84 85 86**NOTE**<br>The call fails if the input parameter **arg** of **getValueStringUtf8()** is not of the string type. 87 88### OH_JSVM_CreateStringUtf8 89 90Call **OH_JSVM_CreateStringUtf8** to create a JS string from a UTF8-encoded C string. 91 92CPP code: 93 94```cpp 95// hello.cpp 96#include "napi/native_api.h" 97#include "ark_runtime/jsvm.h" 98#include <hilog/log.h> 99#include <string> 100// Define OH_JSVM_CreateStringUtf8. 101static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) 102{ 103 const char *str = u8"Hello, World!, successes to create UTF-8 string!"; 104 size_t length = strlen(str); 105 JSVM_Value result = nullptr; 106 JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, length, &result); 107 if (status != JSVM_OK) { 108 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string"); 109 return nullptr; 110 } else { 111 OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf8 success: %{public}s", str); 112 } 113 return result; 114} 115// Register the CreateStringUtf8 callback. 116static JSVM_CallbackStruct param[] = { 117 {.data = nullptr, .callback = CreateStringUtf8}, 118}; 119static JSVM_CallbackStruct *method = param; 120// Alias for the CreateStringUtf8 method to be called from JS. 121static JSVM_PropertyDescriptor descriptor[] = { 122 {"createStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 123}; 124 125// Call the C++ code from JS. 126const char *srcCallNative = R"JS( 127 let script = createStringUtf8(); 128)JS"; 129``` 130 131**Expected output** 132 133 134 135### OH_JSVM_GetValueStringUtf16 136 137Call **OH_JSVM_GetValueStringUtf16** to convert a JS string into a UTF-16-encoded string. 138 139CPP code: 140 141```cpp 142// hello.cpp 143#include "napi/native_api.h" 144#include "ark_runtime/jsvm.h" 145#include <hilog/log.h> 146#include <codecvt> 147#include <locale> 148#include <cstdlib> 149 150// Define OH_JSVM_GetValueStringUtf16. 151// Set the maximum length of the string buffer. 152static const int MAX_BUFFER_SIZE = 128; 153static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) { 154 size_t argc = 1; 155 JSVM_Value args[1] = {nullptr}; 156 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 157 JSVM_Value result = nullptr; 158 size_t length = 0; 159 char16_t buffer[MAX_BUFFER_SIZE] = {0}; 160 // Size of the buffer for storing the string. 161 size_t bufferSize = MAX_BUFFER_SIZE; 162 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length); 163 // Convert char16_t to std::u16string. 164 std::u16string u16str = {buffer}; 165 // Convert std::u16string to std::string. 166 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter; 167 std::string str = converter.to_bytes(u16str); 168 // Obtain the string. 169 if (status != JSVM_OK) { 170 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf16 fail"); 171 return nullptr; 172 } else { 173 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf16 success: %{public}s", str.c_str()); 174 } 175 return result; 176} 177// Register the GetValueStringUtf16 callback. 178static JSVM_CallbackStruct param[] = { 179 {.data = nullptr, .callback = GetValueStringUtf16}, 180}; 181static JSVM_CallbackStruct *method = param; 182// Alias for the GetValueStringUtf16 method to be called from JS. 183static JSVM_PropertyDescriptor descriptor[] = { 184 {"getValueStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 185}; 186 187// Call the C++ code from JS. 188const char *srcCallNative = R"JS( 189 let data = "ahello."; 190 let script = getValueStringUtf16(data); 191)JS"; 192``` 193 194**Expected output** 195 196 197 198**NOTE**<br>The call fails if the input parameter **arg** of **getValueStringUtf16()** is not of the string type. 199 200### OH_JSVM_CreateStringUtf16 201 202Call **OH_JSVM_GetValueStringUtf16** to create a JS string from a UTF16-encoded C string. 203 204CPP code: 205 206```cpp 207// hello.cpp 208#include "napi/native_api.h" 209#include "ark_runtime/jsvm.h" 210#include <hilog/log.h> 211#include <codecvt> 212#include <locale> 213#include <cstring> 214 215// Define OH_JSVM_CreateStringUtf16. 216static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) 217{ 218 const char16_t *str = u"Hello, World!, successes to create UTF-16 string!"; 219 std::u16string ustr(str); 220 size_t length = ustr.length(); 221 JSVM_Value result = nullptr; 222 JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result); 223 std::u16string u16str = {str}; 224 // Convert std::u16string to std::string. 225 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter; 226 std::string strResult = converter.to_bytes(u16str); 227 if (status != JSVM_OK) { 228 OH_LOG_ERROR(LOG_APP, "JSVM CreateStringUtf16 fail"); 229 }else { 230 OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf16 success: %{public}s", strResult.c_str()); 231 } 232 return result; 233} 234// Register the CreateStringUtf16 callback. 235static JSVM_CallbackStruct param[] = { 236 {.data = nullptr, .callback = CreateStringUtf16}, 237}; 238static JSVM_CallbackStruct *method = param; 239// Alias for the CreateStringUtf16 method to be called from JS. 240static JSVM_PropertyDescriptor descriptor[] = { 241 {"createStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 242}; 243 244// Call the C++ code from JS. 245const char *srcCallNative = R"JS( 246 let script = createStringUtf16(); 247)JS"; 248``` 249 250**Expected output** 251 252 253 254### OH_JSVM_GetValueStringLatin1 255 256Call **OH_JSVM_GetValueStringLatin1** to convert a JS string into an ISO-8859-1-encoded string. 257 258CPP code: 259 260```cpp 261// hello.cpp 262#include "napi/native_api.h" 263#include "ark_runtime/jsvm.h" 264#include <hilog/log.h> 265#include <cstdlib> 266// Define OH_JSVM_GetValueStringLatin1. 267// Set the maximum length of the string buffer. 268static const int MAX_BUFFER_SIZE = 128; 269static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) 270{ 271 size_t argc = 1; 272 JSVM_Value args[1] = {nullptr}; 273 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 274 char buf[MAX_BUFFER_SIZE]; 275 size_t length; 276 JSVM_Value jsvmRes = nullptr; 277 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length); 278 if (status != JSVM_OK) { 279 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringLatin1 fail"); 280 } else { 281 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringLatin1 success: %{public}s", buf); 282 } 283 OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes); 284 return jsvmRes; 285} 286// Register the GetValueStringLatin1 callback. 287static JSVM_CallbackStruct param[] = { 288 {.data = nullptr, .callback = GetValueStringLatin1}, 289}; 290static JSVM_CallbackStruct *method = param; 291// Alias for the GetValueStringLatin1 method to be called from JS. 292static JSVM_PropertyDescriptor descriptor[] = { 293 {"getValueStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 294}; 295 296// Call the C++ code from JS. 297const char *srcCallNative = R"JS( 298 let data = "中文"; 299 let script = getValueStringLatin1(data); 300)JS"; 301``` 302 303**Expected output** 304 305The ISO-8859-1 encoding does not support Chinese characters. If Chinese characters are passed in, garbled characters will be displayed. 306 307 308**NOTE**<br>The call fails if the input parameter **arg** of **getValueStringLatin1()** is not of the string type. 309 310### OH_JSVM_CreateStringLatin1 311 312Call **OH_JSVM_CreateStringLatin1** to create a JS string from an ISO-8859-1-encoded C string. 313 314CPP code: 315 316```cpp 317// hello.cpp 318#include "napi/native_api.h" 319#include "ark_runtime/jsvm.h" 320#include <hilog/log.h> 321#include <cstring> 322// Register the CreateStringLatin1 callback. 323// Set the maximum length of the string buffer. 324static const int MAX_BUFFER_SIZE = 128; 325// Define OH_JSVM_CreateStringLatin1. 326static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) 327{ 328 const char *str = "Hello, World! éçñ, successes to create Latin1 string!"; 329 size_t length = JSVM_AUTO_LENGTH; 330 JSVM_Value result = nullptr; 331 JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, length, &result); 332 if (status != JSVM_OK) { 333 OH_LOG_ERROR(LOG_APP, "JSVM CreateStringLatin1 fail"); 334 } else { 335 char buf[MAX_BUFFER_SIZE]; 336 size_t length; 337 OH_JSVM_GetValueStringLatin1(env, result, buf, MAX_BUFFER_SIZE, &length); 338 OH_LOG_INFO(LOG_APP, "JSVM CreateStringLatin1 success: %{public}s", buf); 339 } 340 return result; 341} 342static JSVM_CallbackStruct param[] = { 343 {.data = nullptr, .callback = CreateStringLatin1}, 344}; 345static JSVM_CallbackStruct *method = param; 346// Alias for the CreateStringLatin1 method to be called from JS. 347static JSVM_PropertyDescriptor descriptor[] = { 348 {"createStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 349}; 350 351// Call the C++ code from JS. 352const char *srcCallNative = R"JS( 353 let script = createStringLatin1(); 354)JS"; 355``` 356 357**Expected output** 358 359 360<!--no_check-->