From f4ee6102624aa531b4a658a4eed168ffcea78699 Mon Sep 17 00:00:00 2001 From: maosiping Date: Tue, 23 Nov 2021 20:30:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=88=86=E5=8F=91=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6,=20=E9=87=8A=E6=94=BE=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- frameworks/js/builtin/fetch_module.cpp | 6 +- frameworks/js/builtin/test_fetch_module.cpp | 170 -------------------- 2 files changed, 5 insertions(+), 171 deletions(-) delete mode 100644 frameworks/js/builtin/test_fetch_module.cpp diff --git a/frameworks/js/builtin/fetch_module.cpp b/frameworks/js/builtin/fetch_module.cpp index e3d18da6d..c12d0b529 100644 --- a/frameworks/js/builtin/fetch_module.cpp +++ b/frameworks/js/builtin/fetch_module.cpp @@ -43,7 +43,11 @@ JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_ asyncCallback->responseCallback[CB_SUCCESS] = JSI::GetNamedProperty(args[0], CB_SUCCESS); asyncCallback->responseCallback[CB_FAIL] = JSI::GetNamedProperty(args[0], CB_FAIL); asyncCallback->responseCallback[CB_COMPLETE] = JSI::GetNamedProperty(args[0], CB_COMPLETE); - JsAsyncWork::DispatchAsyncWork(HttpAsyncCallback::AsyncExecHttpRequest, static_cast(asyncCallback)); + if (JsAsyncWork::DispatchAsyncWork(HttpAsyncCallback::AsyncExecHttpRequest, + static_cast(asyncCallback))) { + delete asyncCallback; + return JSI::CreateUndefined(); + } } else { delete asyncCallback; } diff --git a/frameworks/js/builtin/test_fetch_module.cpp b/frameworks/js/builtin/test_fetch_module.cpp deleted file mode 100644 index 723d71904..000000000 --- a/frameworks/js/builtin/test_fetch_module.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "fetch_module.h" -#include "http_request/http_async_callback.h" -#include "http_request/http_request_utils.h" -#include "jerryscript-core.h" -#include "js_async_work.h" -#include "message_queue_utils.h" - -#define FUNC_BEGIN() \ - do { \ - HTTP_REQUEST_INFO("%s BEGIN ##########", __FUNCTION__); \ - } while (0) - -#define FUNC_END_NO_NEW_LINE() \ - do { \ - HTTP_REQUEST_INFO("%s END ##########", __FUNCTION__); \ - } while (0) - -#define FUNC_END() \ - do { \ - HTTP_REQUEST_INFO("%s END ##########\n\n\n", __FUNCTION__); \ - } while (0) - -namespace OHOS { -namespace ACELite { -void InitFetchModule(JSIValue exports); -class JerryInitializer { -private: - int *temp; - JSIValue exports; - -public: - JerryInitializer() - { - temp = new int; - jerry_init(JERRY_INIT_EMPTY); - JsAsyncWork::SetAppQueueHandler(temp); - exports = JSI::CreateObject(); - InitFetchModule(exports); - } - - ~JerryInitializer() - { - jerry_cleanup(); - delete temp; - JSI::ReleaseValue(exports); - } -}; - -void TestPutMessage(void *data) -{ - auto msg = static_cast(data); - auto asyncWork = static_cast(msg->data); - asyncWork->workHandler(asyncWork->data); -} - -JSIValue TestCallbackOnSuccess(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) -{ - FUNC_BEGIN(); - (void)thisVal; - (void)argsNum; - - JSIValue para = args[0]; - HTTP_REQUEST_INFO("code = %d", - static_cast(JSI::GetNumberProperty(para, HttpConstant::KEY_HTTP_RESPONSE_CODE))); - - size_t size = 0; - char *data = JSI::GetStringProperty(para, HttpConstant::KEY_HTTP_RESPONSE_DATA, size); - std::string body; - for (uint32_t index = 0; index < size; ++index) { - if (data[index] != 0) { - body += data[index]; - } else { - body += "0"; - } - } - HTTP_REQUEST_INFO("%s", body.c_str()); - - JSIValue head = JSI::GetNamedProperty(para, HttpConstant::KEY_HTTP_RESPONSE_HEADERS); - - JSIValue keys = JSI::GetObjectKeys(head); - uint32_t length = JSI::GetArrayLength(keys); - for (uint32_t i = 0; i < length; ++i) { - JSIValue k = JSI::GetPropertyByIndex(keys, i); - char *s = JSI::ValueToString(k); - char *v = JSI::GetStringProperty(head, s); - HTTP_REQUEST_INFO("%s ---------------- %s", s, v); - } - - FUNC_END_NO_NEW_LINE(); - return JSI::CreateUndefined(); -} - -JSIValue TestCallbackOnFail(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) -{ - FUNC_BEGIN(); - (void)thisVal; - (void)argsNum; - - HTTP_REQUEST_INFO("err = %s", JSI::ValueToString(args[0])); - HTTP_REQUEST_INFO("code = %d", static_cast(JSI::ValueToNumber(args[1]))); - - FUNC_END_NO_NEW_LINE(); - return JSI::CreateUndefined(); -} - -JSIValue TestCallbackOnComplete(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) -{ - FUNC_BEGIN(); - (void)thisVal; - (void)args; - (void)argsNum; - - HTTP_REQUEST_INFO("request complete"); - - FUNC_END_NO_NEW_LINE(); - return JSI::CreateUndefined(); -} - -void TestHttpModuleMethodAndHeaderByDefault() -{ - FUNC_BEGIN(); - - JSIValue object = JSI::CreateObject(); - if (object == nullptr) { - return; - } - - JSIValue header = JSI::CreateObject(); - JSI::SetStringProperty(header, "no-use", "test value"); - JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_HEADER, header); - - JSIValue url = JSI::CreateString("https://www.zhihu.com"); - JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_URL, url); - - JSI::SetNamedProperty(object, CB_SUCCESS, JSI::CreateFunction(TestCallbackOnSuccess)); - JSI::SetNamedProperty(object, CB_FAIL, JSI::CreateFunction(TestCallbackOnFail)); - JSI::SetNamedProperty(object, CB_COMPLETE, JSI::CreateFunction(TestCallbackOnComplete)); - - JSIValue arg[1] = {object}; - FetchModule::Fetch(nullptr, arg, 1); - - FUNC_END(); -} - -} // namespace ACELite -} // namespace OHOS - -int main() -{ - OHOS::ACELite::JerryInitializer jerryInitializer; - - OHOS::ACELite::TestHttpModuleMethodAndHeaderByDefault(); - - return 0; -} \ No newline at end of file -- Gitee