1# 使用JSVM-API接口创建和获取string值 2 3## 简介 4 5使用JSVM-API关于string的六个接口,可以让JSVM模块和JavaScript字符串进行交互。 6 7## 基本概念 8 9string是编程中常用的数据类型之一。它可以存储和操作文本数据,用于表示和处理字符序列。还可用于构建用户界面元素,如标签、按钮和文本框,处理用户输入,验证和格式化输入数据。不同的编码支持不同的字符集和语言,以下是一些主要的编码方案及其区别: 10 11- **ASCII**:ASCII是最早的字符编码方案之一,使用7位编码,只能表示英文字母、数字和一些基本符号。它是许多其他编码方案的基础。 12- **UTF-8**:UTF-8是一种变长编码方案,可以表示全球范围的字符集。它使用8位编码,根据字符的不同范围使用不同长度的字节序列。UTF-8是互联网上广泛使用的编码方案。 13- **UTF-16**:UTF-16是一种定长或变长编码方案,使用16位编码。它可以表示全球范围的字符集,并且适用于较大的字符集。 14- **ISO-8859-1(Latin-1)**:ISO-8859-1是一种单字节编码方案,使用8位编码。它主要用于表示拉丁字母字符集,包括欧洲大部分语言。 15 16## 接口说明 17 18| 接口 | 功能说明 | 19|----------------------------|--------------------------------| 20| OH_JSVM_GetValueStringUtf8 | 获取给定JavaScript string对象的Utf8编码字符串。| 21| OH_JSVM_CreateStringUtf8 | 根据Utf8编码的字符串创建一个JavaScript string对象。| 22| OH_JSVM_GetValueStringUtf16 | 获取给定JavaScript string对象的Utf16编码字符串| 23| OH_JSVM_CreateStringUtf16 | 通过UTF16编码的C字符串数据创建JS String。| 24| OH_JSVM_GetValueStringLatin1 | 获取给定JavaScript string对象的Latin1编码字符串| 25| OH_JSVM_CreateStringLatin1 | 根据Latin-1编码的字符串创建一个JavaScript string对象。| 26 27## 使用示例 28 29JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅对接口对应C++及ArkTS相关代码进行展示。 30 31### OH_JSVM_GetValueStringUtf8 32 33OH_JSVM_GetValueStringUtf8接口可以将JavaScript的字符类型的数据转换为utf8编码的字符。 34 35cpp部分代码 36 37```cpp 38// hello.cpp 39#include "napi/native_api.h" 40#include "ark_runtime/jsvm.h" 41#include <hilog/log.h> 42// GetValueStringUtf8注册回调 43static JSVM_CallbackStruct param[] = { 44 {.data = nullptr, .callback = GetValueStringUtf8}, 45}; 46static JSVM_CallbackStruct *method = param; 47// GetValueStringUtf8方法别名,供JS调用 48static JSVM_PropertyDescriptor descriptor[] = { 49 {"getValueStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 50}; 51// OH_JSVM_GetValueStringUtf8的样例方法 52static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) 53{ 54 size_t argc = 1; 55 JSVM_Value args[1] = {nullptr}; 56 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 57 size_t length = 0; 58 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length); 59 char *buf = (char *)malloc(length + 1); 60 status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length); 61 if (status != JSVM_OK) { 62 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf8 fail"); 63 return nullptr; 64 } else { 65 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf8 success: %{public}s", buf); 66 } 67 JSVM_Value result = nullptr; 68 OH_JSVM_CreateStringUtf8(env, buf, length, &result); 69 return result; 70} 71``` 72 73ArkTS侧示例代码 74 75```ts 76import hilog from "@ohos.hilog" 77// 通过import的方式,引入Native能力。 78import napitest from "libentry.so" 79try { 80 let data = `"aaBC+-$%^你好123"`; 81 let script: string = `getValueStringUtf8(${data})`; 82 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf8: %{public}s', napitest.runJsVm(script)); 83} catch (error) { 84 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf8 error: %{public}s', error.message); 85} 86try { 87 let script: string = `getValueStringUtf8(50)`; 88 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf8: %{public}s', napitest.runJsVm(script)); 89} catch (error) { 90 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf8 error: %{public}s', error.message); 91} 92``` 93 94### OH_JSVM_CreateStringUtf8 95 96用于创建一个UTF-8编码的JavaScript字符串。 97 98cpp部分代码 99 100```cpp 101// hello.cpp 102#include "napi/native_api.h" 103#include "ark_runtime/jsvm.h" 104#include <hilog/log.h> 105// CreateStringUtf8注册回调 106static JSVM_CallbackStruct param[] = { 107 {.data = nullptr, .callback = CreateStringUtf8}, 108}; 109static JSVM_CallbackStruct *method = param; 110// CreateStringUtf8方法别名,供JS调用 111static JSVM_PropertyDescriptor descriptor[] = { 112 {"createStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 113}; 114// OH_JSVM_CreateStringUtf8的样例方法 115static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) 116{ 117 const char *str = u8"你好, World!, successes to create UTF-8 string!"; 118 size_t length = strlen(str); 119 JSVM_Value result = nullptr; 120 JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, length, &result); 121 if (status != JSVM_OK) { 122 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string"); 123 return nullptr; 124 } else { 125 OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf8 success: %{public}s", str); 126 } 127 return result; 128} 129``` 130 131ArkTS侧示例代码 132 133```ts 134import hilog from "@ohos.hilog" 135// 通过import的方式,引入Native能力。 136import napitest from "libentry.so" 137try { 138 let script: string = `createStringUtf8()`; 139 hilog.info(0x0000, 'testJSVM', 'Test JSVM createStringUtf8: %{public}s', napitest.runJsVm(script)); 140} catch (error) { 141 hilog.error(0x0000, 'testJSVM', 'Test JSVM createStringUtf8 error: %{public}s', error.message); 142} 143``` 144 145### OH_JSVM_GetValueStringUtf16 146 147OH_JSVM_GetValueStringUtf16,将JavaScript的字符类型的数据转换为utf16编码的字符。 148 149cpp部分代码 150 151```cpp 152// hello.cpp 153#include "napi/native_api.h" 154#include "ark_runtime/jsvm.h" 155#include <hilog/log.h> 156#include <codecvt> 157#include <locale> 158 159// GetValueStringUtf16注册回调 160static JSVM_CallbackStruct param[] = { 161 {.data = nullptr, .callback = GetValueStringUtf16}, 162}; 163static JSVM_CallbackStruct *method = param; 164// GetValueStringUtf16方法别名,供JS调用 165static JSVM_PropertyDescriptor descriptor[] = { 166 {"getValueStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 167}; 168// OH_JSVM_GetValueStringUtf16的样例方法 169// 定义字符串缓冲区的最大长度 170static const int MAX_BUFFER_SIZE = 128; 171static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) { 172 size_t argc = 1; 173 JSVM_Value args[1] = {nullptr}; 174 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 175 JSVM_Value result = nullptr; 176 size_t length = 0; 177 char16_t buffer[MAX_BUFFER_SIZE] = {0}; 178 // 字符串的缓冲区大小 179 size_t bufferSize = MAX_BUFFER_SIZE; 180 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length); 181 // 将 char16_t 转换为 std::u16string 182 std::u16string u16str = {buffer}; 183 // 将 std::u16string 转换为 std::string 184 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter; 185 std::string str = converter.to_bytes(u16str); 186 // 获取字符串返回结果 187 if (status != JSVM_OK) { 188 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf16 fail"); 189 return nullptr; 190 } else { 191 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf16 success: %{public}s", str.c_str()); 192 } 193 return result; 194} 195``` 196 197ArkTS侧示例代码 198 199```ts 200import hilog from "@ohos.hilog" 201// 通过import的方式,引入Native能力。 202import napitest from "libentry.so" 203try { 204 let data = `"ahello。"`; 205 let script: string = `getValueStringUtf16(${data})`; 206 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf16: %{public}s', napitest.runJsVm(script)); 207} catch (error) { 208 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf16 error: %{public}s', error.message); 209} 210try { 211 let script: string = `getValueStringUtf16(50)`; 212 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf16: %{public}s', napitest.runJsVm(script)); 213} catch (error) { 214 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringUtf16 error: %{public}s', error.message); 215} 216``` 217 218### OH_JSVM_CreateStringUtf16 219 220用于创建一个UTF-16编码的JavaScript字符串。 221 222cpp部分代码 223 224```cpp 225// hello.cpp 226#include "napi/native_api.h" 227#include "ark_runtime/jsvm.h" 228#include <hilog/log.h> 229#include <codecvt> 230#include <locale> 231 232// CreateStringUtf16注册回调 233static JSVM_CallbackStruct param[] = { 234 {.data = nullptr, .callback = CreateStringUtf16}, 235}; 236static JSVM_CallbackStruct *method = param; 237// CreateStringUtf16方法别名,供JS调用 238static JSVM_PropertyDescriptor descriptor[] = { 239 {"createStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 240}; 241// OH_JSVM_CreateStringUtf16的样例方法 242static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) 243{ 244 const char16_t *str = u"你好, World!, successes to create UTF-16 string!"; 245 std::u16string ustr(str); 246 size_t length = ustr.length(); 247 JSVM_Value result = nullptr; 248 JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result); 249 std::u16string u16str = {str}; 250 // 将 std::u16string 转换为 std::string 251 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter; 252 std::string strResult = converter.to_bytes(u16str); 253 if (status != JSVM_OK) { 254 OH_LOG_ERROR(LOG_APP, "JSVM CreateStringUtf16 fail"); 255 }else { 256 OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf16 success: %{public}s", strResult.c_str()); 257 } 258 return result; 259} 260``` 261 262ArkTS侧示例代码 263 264```ts 265import hilog from "@ohos.hilog" 266// 通过import的方式,引入Native能力。 267import napitest from "libentry.so" 268try { 269 let script: string = ` 270 createStringUtf16() 271 `; 272 hilog.info(0x0000, 'testJSVM', 'Test JSVM createStringUtf16: %{public}s', napitest.runJsVm(script)); 273} catch (error) { 274 hilog.error(0x0000, 'testJSVM', 'Test JSVM createStringUtf16 error: %{public}s', error.message); 275} 276``` 277 278### OH_JSVM_GetValueStringLatin1 279 280OH_JSVM_GetValueStringLatin1接口可以将JavaScript的字符类型的数据转换为ISO-8859-1编码的字符。 281 282cpp部分代码 283 284```cpp 285// hello.cpp 286#include "napi/native_api.h" 287#include "ark_runtime/jsvm.h" 288#include <hilog/log.h> 289// GetValueStringLatin1注册回调 290static JSVM_CallbackStruct param[] = { 291 {.data = nullptr, .callback = GetValueStringLatin1}, 292}; 293static JSVM_CallbackStruct *method = param; 294// GetValueStringLatin1方法别名,供JS调用 295static JSVM_PropertyDescriptor descriptor[] = { 296 {"getValueStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 297}; 298// OH_JSVM_GetValueStringLatin1的样例方法 299// 定义字符串缓冲区的最大长度 300static const int MAX_BUFFER_SIZE = 128; 301static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) 302{ 303 size_t argc = 1; 304 JSVM_Value args[1] = {nullptr}; 305 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 306 char buf[MAX_BUFFER_SIZE]; 307 size_t length; 308 JSVM_Value jsvmRes = nullptr; 309 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length); 310 if (status != JSVM_OK) { 311 OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringLatin1 fail"); 312 } else { 313 OH_LOG_INFO(LOG_APP, "JSVM GetValueStringLatin1 success: %{public}s", buf); 314 } 315 OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes); 316 return jsvmRes; 317} 318``` 319 320ArkTS侧示例代码 321 322```ts 323import hilog from "@ohos.hilog" 324// 通过import的方式,引入Native能力。 325import napitest from "libentry.so" 326try { 327 // ISO-8859-1编码不支持中文,传入中文字符会乱码 328 let data = `"中文"`; 329 let script: string = `getValueStringLatin1(${data})`; 330 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1: %{public}s', napitest.runJsVm(script)); 331} catch (error) { 332 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1 error: %{public}s', error.message); 333} 334try { 335 // 传入非字符型数据,函数返回undefined 336 let script: string = `getValueStringLatin1(10)`; 337 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1: %{public}s', napitest.runJsVm(script)); 338} catch (error) { 339 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1 error: %{public}s', error.message); 340} 341try { 342 // 传入其他字符,不会乱码 343 let data = `"abo ABP=-&*/"`; 344 let script: string = `getValueStringLatin1(${data})`; 345 hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1: %{public}s', napitest.runJsVm(script)); 346} catch (error) { 347 hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueStringLatin1 error: %{public}s', error.message); 348} 349``` 350 351### OH_JSVM_CreateStringLatin1 352 353用于创建一个Latin1编码的JavaScript字符串。 354 355cpp部分代码 356 357```cpp 358// hello.cpp 359#include "napi/native_api.h" 360#include "ark_runtime/jsvm.h" 361#include <hilog/log.h> 362// CreateStringLatin1注册回调 363static JSVM_CallbackStruct param[] = { 364 {.data = nullptr, .callback = CreateStringLatin1}, 365}; 366static JSVM_CallbackStruct *method = param; 367// CreateStringLatin1方法别名,供JS调用 368static JSVM_PropertyDescriptor descriptor[] = { 369 {"createStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 370}; 371// 定义字符串缓冲区的最大长度 372static const int MAX_BUFFER_SIZE = 128; 373// OH_JSVM_CreateStringLatin1的样例方法 374static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) 375{ 376 const char *str = "Hello, World! éçñ, successes to create Latin1 string! 111"; 377 size_t length = JSVM_AUTO_LENGTH; 378 JSVM_Value result = nullptr; 379 JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, length, &result); 380 if (status != JSVM_OK) { 381 OH_LOG_ERROR(LOG_APP, "JSVM CreateStringLatin1 fail"); 382 } else { 383 char buf[MAX_BUFFER_SIZE]; 384 size_t length; 385 OH_JSVM_GetValueStringLatin1(env, result, buf, MAX_BUFFER_SIZE, &length); 386 OH_LOG_INFO(LOG_APP, "JSVM CreateStringLatin1 success: %{public}s", buf); 387 } 388 return result; 389} 390``` 391 392ArkTS侧示例代码 393 394```ts 395import hilog from "@ohos.hilog" 396// 通过import的方式,引入Native能力。 397import napitest from "libentry.so" 398try { 399 let script: string = `createStringLatin1()`; 400 hilog.info(0x0000, 'testJSVM', 'Test JSVM createStringLatin1: %{public}s', napitest.runJsVm(script)); 401} catch (error) { 402 hilog.error(0x0000, 'testJSVM', 'Test JSVM createStringLatin1 error: %{public}s', error.message); 403} 404``` 405