You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by mo...@apache.org on 2020/09/17 08:12:22 UTC

[incubator-weex] branch master updated: [core] support sync call module on reactor

This is an automated email from the ASF dual-hosted git repository.

moshen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new ceb3fcf  [core] support sync call module on reactor
     new 6a17f5e  Merge pull request #3269 from jianhan-he/master
ceb3fcf is described below

commit ceb3fcfbf478e6bd1e2865deef755e91d61f14d9
Author: linghe.lh <li...@alibaba-inc.com>
AuthorDate: Wed Sep 16 10:53:25 2020 +0800

    [core] support sync call module on reactor
---
 weex_core/Source/core/render/page/reactor_page.cpp | 87 +++++++++++++++++++++-
 weex_core/Source/core/render/page/reactor_page.h   |  2 +-
 2 files changed, 87 insertions(+), 2 deletions(-)

diff --git a/weex_core/Source/core/render/page/reactor_page.cpp b/weex_core/Source/core/render/page/reactor_page.cpp
index ff2116c..081e9b33 100644
--- a/weex_core/Source/core/render/page/reactor_page.cpp
+++ b/weex_core/Source/core/render/page/reactor_page.cpp
@@ -19,6 +19,8 @@
 
 #include "core/render/page/reactor_page.h"
 
+#include "third_party/json11/json11.hpp"
+#include "base/string_util.h"
 #include "core/render/manager/render_manager.h"
 #include "core/render/page/render_page.h"
 #include "core/render/node/factory/render_creator.h"
@@ -34,6 +36,87 @@ void PostTaskOnComponentThread(const std::function<void()>& callback) {
 #endif
 }
 
+static std::string ResultToString(const std::unique_ptr<ValueWithType>& result) {
+  switch (result->type) {
+    case ParamsType::DOUBLE:
+      return json11::Json(result->value.doubleValue).dump();
+    case ParamsType::STRING: {
+      if (!result->value.string) {
+        return "";
+      } else {
+        std::string ret;
+        if (result->value.string->length > 0) {
+          const auto& basic_string = weex::base::to_utf8(result->value.string->content,
+                                                         result->value.string->length);
+          ret = json11::Json(basic_string).dump();
+        } else {
+          ret = json11::Json("").dump();
+        }
+        free(result->value.string);
+        return ret;
+      }
+    }
+    case ParamsType::BYTEARRAYSTRING: {
+      if (!result->value.byteArray) {
+        return "";//null
+      } else {
+        std::string ret;
+        if (result->value.byteArray->length > 0) {
+          ret = json11::Json(result->value.byteArray->content).dump();
+        } else {
+          ret = json11::Json("").dump();
+        }
+        free(result->value.byteArray);
+        return ret;
+      }
+    }
+    case ParamsType::JSONSTRING: {
+      if (!result->value.string) {
+        return "";
+      } else {
+        std::string ret;
+        std::string err;
+        if (result->value.string->length > 0) {
+          const auto& raw_str = weex::base::to_utf8(result->value.string->content,
+                                                    result->value.string->length);
+
+          const json11::Json& json = json11::Json::parse(raw_str, err);
+          if (err.empty()) {
+            //succ
+            ret = json.dump();
+          } else {
+            LOGE("VnodeManager CallNative return value to object err, %s", err.c_str());
+          }
+        }
+        free(result->value.string);
+        return ret;
+      }
+    }
+    case ParamsType::BYTEARRAYJSONSTRING: {
+      if (!result->value.byteArray) {
+        return "";
+      } else {
+        std::string ret;
+        std::string err;
+        if (result->value.string->length > 0) {
+          const json11::Json& json = json11::Json::parse(result->value.byteArray->content, err);
+          if (err.empty()) {
+            //succ
+            ret = json.dump();
+          } else {
+            LOGE("VnodeManager CallNative return value to object err, %s", err.c_str());
+          }
+        }
+        free(result->value.byteArray);
+        return ret;
+      }
+    }
+    case ParamsType::BYTEARRAY:
+    default:
+      return "";
+  }
+}
+
 void ReactorPage::CreateBody(const std::string& ref,
                              const std::string& type,
                              const std::map<std::string, std::string>& styles,
@@ -77,7 +160,7 @@ void ReactorPage::RemoveElement(const std::string& ref) {
     WeexCore::WeexCoreManager::Instance()->script_bridge()->core_side()->RemoveElement(page_id_.c_str(), ref.c_str());
 }
 
-void ReactorPage::CallNativeModule(const std::string& module,
+std::string ReactorPage::CallNativeModule(const std::string& module,
                                    const std::string& method,
                                    const std::string& arguments,
                                    size_t arguments_length,
@@ -87,6 +170,8 @@ void ReactorPage::CallNativeModule(const std::string& module,
       ->getPlatformBridge()
       ->platform_side()
       ->CallNativeModule(page_id_.c_str(), module.c_str(), method.c_str(), arguments.c_str(), static_cast<int>(arguments_length), options.c_str(), options_length);
+
+  return ResultToString(ptr);
 }
 
 void ReactorPage::CallNativeComponent(const std::string& ref,
diff --git a/weex_core/Source/core/render/page/reactor_page.h b/weex_core/Source/core/render/page/reactor_page.h
index 3386238..dd32e8a 100644
--- a/weex_core/Source/core/render/page/reactor_page.h
+++ b/weex_core/Source/core/render/page/reactor_page.h
@@ -59,7 +59,7 @@ public:
 
     const std::string& page_id() const {return page_id_;}
 
-    void CallNativeModule(const std::string& module,
+    std::string CallNativeModule(const std::string& module,
                           const std::string& method,
                           const std::string& arguments,
                           size_t arguments_length,