文档管理中心

使用JSVM-API接口创建和获取string值

简介

使用JSVM-API的六个字符串接口,可以实现JSVM模块与JavaScript字符串的交互功能。

基本概念

string是编程中常用的数据类型。用于存储和操作文本数据,它们可用于构建用户界面元素,如标签、按钮和文本框,处理用户输入,验证和格式化数据。不同的编码方案支持不同的字符集和语言,以下是一些主要的编码方案及其区别:

  • ASCII:ASCII是最早的字符编码方案之一,使用7位编码,只能表示英文字母、数字和一些基本符号。它是许多其他编码方案的基础。
  • UTF-8:UTF-8是一种变长编码方案,可以表示全球范围的字符集。它使用8位编码,根据字符的不同范围使用不同长度的字节序列。UTF-8是互联网上广泛使用的编码方案。
  • UTF-16:UTF-16是一种定长或变长编码方案,使用16位编码。它可以表示全球范围的字符集,并且适用于较大的字符集。
  • ISO-8859-1(Latin-1):ISO-8859-1是一种单字节编码方案,使用8位编码。它主要用于表示拉丁字母字符集,包括欧洲大部分语言。

接口说明

展开
接口 功能说明
OH_JSVM_GetValueStringUtf8 获取给定JavaScript string对象的Utf8编码字符串。
OH_JSVM_CreateStringUtf8 根据Utf8编码的字符串创建一个JavaScript string对象。
OH_JSVM_GetValueStringUtf16 获取给定JavaScript string对象的Utf16编码字符串。
OH_JSVM_CreateStringUtf16 根据Utf16编码的字符串数据创建JavaScript string对象。
OH_JSVM_GetValueStringLatin1 获取给定JavaScript string对象的Latin-1编码字符串。
OH_JSVM_CreateStringLatin1 根据Latin-1编码的字符串创建一个JavaScript string对象。

使用示例

JSVM-API接口开发流程参考使用JSVM-API实现JS与C/C++语言交互开发流程。本文仅展示接口对应的C++相关代码。

OH_JSVM_GetValueStringUtf8

OH_JSVM_GetValueStringUtf8接口可以将JavaScript的字符类型的数据转换为utf8编码的字符。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <cstdlib>
  5. // ...
  6. // OH_JSVM_GetValueStringUtf8的样例方法
  7. static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
  8. {
  9. size_t argc = 1;
  10. JSVM_Value args[1] = {nullptr};
  11. OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
  12. size_t length = 0;
  13. JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length);
  14. if (length == 0 || length > MAX_MALLOC_SIZE) {
  15. OH_LOG_ERROR(LOG_APP, "Invalid string length: %{public}zu", length);
  16. return nullptr;
  17. }
  18. char *buf = (char *)malloc(length + 1);
  19. status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length);
  20. if (status != JSVM_OK) {
  21. OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf8 fail");
  22. free(buf);
  23. return nullptr;
  24. } else {
  25. OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf8 success: %{public}s", buf);
  26. }
  27. JSVM_Value result = nullptr;
  28. OH_JSVM_CreateStringUtf8(env, buf, length, &result);
  29. free(buf);
  30. return result;
  31. }
  32. // GetValueStringUtf8注册回调
  33. static JSVM_CallbackStruct param[] = {
  34. {.data = nullptr, .callback = GetValueStringUtf8},
  35. };
  36. static JSVM_CallbackStruct *method = param;
  37. // GetValueStringUtf8方法别名,供JS调用
  38. static JSVM_PropertyDescriptor descriptor[] = {
  39. {"getValueStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  40. };
  41. // 样例测试js
  42. const char *SRC_CALL_NATIVE = R"JS(
  43. let data = "aaBC+-$%^你好123";
  44. let script = getValueStringUtf8(data);
  45. )JS";

预期输出结果:

收起
自动换行
深色代码主题
复制
  1. JSVM GetValueStringUtf8 success: aaBC+-$%^你好123

注意事项:getValueStringUtf8(arg)入参arg非字符串型数据时接口会调用失败。

OH_JSVM_CreateStringUtf8

用于创建一个UTF-8编码的JavaScript字符串。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <string>
  5. // ...
  6. // OH_JSVM_CreateStringUtf8的样例方法
  7. static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
  8. {
  9. const char *str = u8"你好, World!, succeed in creating UTF-8 string!";
  10. size_t length = strlen(str);
  11. JSVM_Value result = nullptr;
  12. JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, length, &result);
  13. if (status != JSVM_OK) {
  14. OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string");
  15. return nullptr;
  16. } else {
  17. OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf8 success: %{public}s", str);
  18. }
  19. return result;
  20. }
  21. // CreateStringUtf8注册回调
  22. static JSVM_CallbackStruct param[] = {
  23. {.data = nullptr, .callback = CreateStringUtf8},
  24. };
  25. static JSVM_CallbackStruct *method = param;
  26. // CreateStringUtf8方法别名,供JS调用
  27. static JSVM_PropertyDescriptor descriptor[] = {
  28. {"createStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  29. };
  30. // 样例测试js
  31. const char *SRC_CALL_NATIVE = R"JS(
  32. let script = createStringUtf8();
  33. )JS";

预期输出结果:

收起
自动换行
深色代码主题
复制
  1. JSVM CreateStringUtf8 success: 你好, World!, succeed in creating UTF-8 string!

OH_JSVM_GetValueStringUtf16

OH_JSVM_GetValueStringUtf16,将JavaScript的字符类型的数据转换为utf16编码的字符。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <codecvt>
  5. #include <locale>
  6. #include <cstdlib>
  7. // ...
  8. // OH_JSVM_GetValueStringUtf16的样例方法
  9. // 定义字符串缓冲区的最大长度
  10. static const int MAX_BUFFER_SIZE = 128;
  11. static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
  12. {
  13. size_t argc = 1;
  14. JSVM_Value args[1] = {nullptr};
  15. OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
  16. JSVM_Value result = nullptr;
  17. size_t length = 0;
  18. char16_t buffer[MAX_BUFFER_SIZE] = {0};
  19. // 字符串的缓冲区大小
  20. size_t bufferSize = MAX_BUFFER_SIZE;
  21. JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length);
  22. // 将 char16_t 转换为 std::u16string
  23. std::u16string u16str = {buffer};
  24. // 将 std::u16string 转换为 std::string
  25. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
  26. std::string str = converter.to_bytes(u16str);
  27. // 获取字符串返回结果
  28. if (status != JSVM_OK) {
  29. OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf16 fail");
  30. return nullptr;
  31. } else {
  32. OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf16 success: %{public}s", str.c_str());
  33. }
  34. return result;
  35. }
  36. // GetValueStringUtf16注册回调
  37. static JSVM_CallbackStruct param[] = {
  38. {.data = nullptr, .callback = GetValueStringUtf16},
  39. };
  40. static JSVM_CallbackStruct *method = param;
  41. // GetValueStringUtf16方法别名,供JS调用
  42. static JSVM_PropertyDescriptor descriptor[] = {
  43. {"getValueStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  44. };
  45. // 样例测试js
  46. const char *SRC_CALL_NATIVE = R"JS(
  47. let data = "ahello。";
  48. let script = getValueStringUtf16(data);
  49. )JS";

预期输出结果:

收起
自动换行
深色代码主题
复制
  1. JSVM GetValueStringUtf16 success: ahello。

注意事项:getValueStringUtf16(arg)的参数arg必须是字符串,否则接口会调用失败。

OH_JSVM_CreateStringUtf16

用于创建一个UTF-16编码的JavaScript字符串。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <codecvt>
  5. #include <locale>
  6. #include <cstring>
  7. // ...
  8. // OH_JSVM_CreateStringUtf16的样例方法
  9. static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
  10. {
  11. const char16_t *str = u"你好, World!, succeed in creating UTF-16 string!";
  12. std::u16string ustr(str);
  13. size_t length = ustr.length();
  14. JSVM_Value result = nullptr;
  15. JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result);
  16. std::u16string u16str = {str};
  17. // 将 std::u16string 转换为 std::string
  18. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
  19. std::string strResult = converter.to_bytes(u16str);
  20. if (status != JSVM_OK) {
  21. OH_LOG_ERROR(LOG_APP, "JSVM CreateStringUtf16 fail");
  22. } else {
  23. OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf16 success: %{public}s", strResult.c_str());
  24. }
  25. return result;
  26. }
  27. // CreateStringUtf16注册回调
  28. static JSVM_CallbackStruct param[] = {
  29. {.data = nullptr, .callback = CreateStringUtf16},
  30. };
  31. static JSVM_CallbackStruct *method = param;
  32. // CreateStringUtf16方法别名,供JS调用
  33. static JSVM_PropertyDescriptor descriptor[] = {
  34. {"createStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  35. };
  36. // 样例测试js
  37. const char *SRC_CALL_NATIVE = R"JS(
  38. let script = createStringUtf16();
  39. )JS";

预期输出结果:

收起
自动换行
深色代码主题
复制
  1. JSVM CreateStringUtf16 success: 你好, World!, succeed in creating UTF-16 string!

OH_JSVM_GetValueStringLatin1

OH_JSVM_GetValueStringLatin1接口可以将JavaScript的字符类型的数据转换为ISO-8859-1编码的字符。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <cstdlib>
  5. // ...
  6. // OH_JSVM_GetValueStringLatin1的样例方法
  7. // 定义字符串缓冲区的最大长度
  8. static const int MAX_BUFFER_SIZE = 128;
  9. static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
  10. {
  11. size_t argc = 1;
  12. JSVM_Value args[1] = {nullptr};
  13. OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
  14. char buf[MAX_BUFFER_SIZE];
  15. size_t length = 0;
  16. JSVM_Value jsvmRes = nullptr;
  17. JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length);
  18. if (status != JSVM_OK) {
  19. OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringLatin1 fail");
  20. } else {
  21. OH_LOG_INFO(LOG_APP, "JSVM GetValueStringLatin1 success: %{public}s", buf);
  22. }
  23. OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes);
  24. return jsvmRes;
  25. }
  26. // GetValueStringLatin1注册回调
  27. static JSVM_CallbackStruct param[] = {
  28. {.data = nullptr, .callback = GetValueStringLatin1},
  29. };
  30. static JSVM_CallbackStruct *method = param;
  31. // GetValueStringLatin1方法别名,供JS调用
  32. static JSVM_PropertyDescriptor descriptor[] = {
  33. {"getValueStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  34. };
  35. // 样例测试js
  36. const char *SRC_CALL_NATIVE = R"JS(
  37. let data = "中文";
  38. let script = getValueStringLatin1(data);
  39. )JS";

预期输出结果(ISO-8859-1编码不支持中文,传入中文字符会导致乱码):

注意事项:getValueStringLatin1(arg)入参arg必须为字符串类型,否则接口调用会失败。

OH_JSVM_CreateStringLatin1

用于创建一个Latin1编码的JavaScript字符串。

cpp部分代码:

收起
自动换行
深色代码主题
复制
  1. #include "napi/native_api.h"
  2. #include "ark_runtime/jsvm.h"
  3. #include "hilog/log.h"
  4. #include <cstring>
  5. // ...
  6. // CreateStringLatin1注册回调
  7. // 定义字符串缓冲区的最大长度
  8. static const int MAX_BUFFER_SIZE = 128;
  9. // OH_JSVM_CreateStringLatin1的样例方法
  10. static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
  11. {
  12. const char *str = "Hello, World! éçñ, succeed in creating Latin1 string!";
  13. size_t length = JSVM_AUTO_LENGTH;
  14. JSVM_Value result = nullptr;
  15. JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, length, &result);
  16. if (status != JSVM_OK) {
  17. OH_LOG_ERROR(LOG_APP, "JSVM CreateStringLatin1 fail");
  18. } else {
  19. char buf[MAX_BUFFER_SIZE];
  20. size_t lengthInner = 0;
  21. OH_JSVM_GetValueStringLatin1(env, result, buf, MAX_BUFFER_SIZE, &lengthInner);
  22. OH_LOG_INFO(LOG_APP, "JSVM CreateStringLatin1 success: %{public}s", buf);
  23. }
  24. return result;
  25. }
  26. static JSVM_CallbackStruct param[] = {
  27. {.data = nullptr, .callback = CreateStringLatin1},
  28. };
  29. static JSVM_CallbackStruct *method = param;
  30. // CreateStringLatin1方法别名,供JS调用
  31. static JSVM_PropertyDescriptor descriptor[] = {
  32. {"createStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
  33. };
  34. // 样例测试js
  35. const char *SRC_CALL_NATIVE = R"JS(
  36. let script = createStringLatin1();
  37. )JS";

预期输出结果:

收起
自动换行
深色代码主题
复制
  1. JSVM CreateStringLatin1 success: Hello, World! éçñ, succeed in creating Latin1 string!