1 /* 2 * Copyright (c) 2022 Huawei Device 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 "native_module_convertxml.h" 17 #include "utils/log.h" 18 #include "js_convertxml.h" 19 20 extern const char _binary_js_convertxml_js_start[]; 21 extern const char _binary_js_convertxml_js_end[]; 22 extern const char _binary_convertxml_abc_start[]; 23 extern const char _binary_convertxml_abc_end[]; 24 25 namespace OHOS::Xml { ConvertXmlConstructor(napi_env env,napi_callback_info info)26 static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) 27 { 28 napi_value thisVar = nullptr; 29 void *data = nullptr; 30 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 31 auto objectInfo = new ConvertXml(); 32 napi_wrap( 33 env, thisVar, objectInfo, 34 [](napi_env environment, void *data, void *hint) { 35 auto obj = reinterpret_cast<ConvertXml*>(data); 36 if (obj != nullptr) { 37 delete obj; 38 } 39 }, 40 nullptr, nullptr); 41 return thisVar; 42 } 43 Convert(napi_env env,napi_callback_info info)44 static napi_value Convert(napi_env env, napi_callback_info info) 45 { 46 napi_value thisVar = nullptr; 47 size_t requireMaxArgc = 2; // 2:MaxArgc 48 size_t requireMinArgc = 1; 49 size_t argc = 2; // 2:The number of parameters is 2 50 napi_value args[2] = { nullptr }; 51 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); 52 NAPI_ASSERT(env, argc <= requireMaxArgc, "Wrong number of arguments(Over)"); 53 NAPI_ASSERT(env, argc >= requireMinArgc, "Wrong number of arguments(Less)"); 54 std::string strXml; 55 napi_valuetype valuetype; 56 ConvertXml *object = nullptr; 57 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object))); 58 59 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); 60 NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type: string expected."); 61 object->DealNapiStrValue(env, args[0], strXml); 62 63 if (argc > 1) { 64 object->DealOptions(env, args[1]); 65 } 66 napi_value result = object->Convert(env, strXml); 67 return result; 68 } 69 ConvertXmlInit(napi_env env,napi_value exports)70 napi_value ConvertXmlInit(napi_env env, napi_value exports) 71 { 72 const char *convertXmlClassName = "ConvertXml"; 73 napi_value convertXmlClass = nullptr; 74 napi_property_descriptor convertXmlDesc[] = { 75 DECLARE_NAPI_FUNCTION("convert", Convert) 76 }; 77 NAPI_CALL(env, napi_define_class(env, convertXmlClassName, strlen(convertXmlClassName), ConvertXmlConstructor, 78 nullptr, sizeof(convertXmlDesc) / sizeof(convertXmlDesc[0]), convertXmlDesc, 79 &convertXmlClass)); 80 napi_property_descriptor desc[] = { 81 DECLARE_NAPI_PROPERTY("ConvertXml", convertXmlClass) 82 }; 83 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 84 return exports; 85 } 86 87 extern "C" NAPI_convertxml_GetJSCode(const char ** buf,int * bufLen)88 __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen) 89 { 90 if (buf != nullptr) { 91 *buf = _binary_js_convertxml_js_start; 92 } 93 if (bufLen != nullptr) { 94 *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; 95 } 96 } 97 extern "C" NAPI_convertxml_GetABCCode(const char ** buf,int * buflen)98 __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen) 99 { 100 if (buf != nullptr) { 101 *buf = _binary_convertxml_abc_start; 102 } 103 if (buflen != nullptr) { 104 *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; 105 } 106 } 107 108 static napi_module_with_js convertXmlModule = { 109 .nm_version = 1, 110 .nm_flags = 0, 111 .nm_filename = nullptr, 112 .nm_register_func = ConvertXmlInit, 113 .nm_modname = "convertxml", 114 .nm_priv = reinterpret_cast<void*>(0), 115 .nm_get_abc_code = NAPI_convertxml_GetABCCode, 116 .nm_get_js_code = NAPI_convertxml_GetJSCode, 117 }; 118 ConvertXMLRegisterModule(void)119 extern "C" __attribute__((constructor)) void ConvertXMLRegisterModule(void) 120 { 121 napi_module_with_js_register(&convertXmlModule); 122 } 123 } // namespace OHOS::Xml 124