<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 "core/bridge/script/core_side_in_script.h"

#include <cstdlib>
#include "base/LogDefines.h"
#include "base/make_copyable.h"
#include "base/thread/waitable_event.h"
#include "core/manager/weex_core_manager.h"
#include "core/render/manager/render_manager.h"
#ifdef OS_ANDROID
#include "android/jsengine/multiprocess/ExtendJSApi.h"
#endif

namespace WeexCore {

CoreSideInScript::CoreSideInScript() {}

CoreSideInScript::~CoreSideInScript() {}

inline char *copyStr(const char *str, int length = 0) {
  char *ret = nullptr;
  if (str == nullptr) return ret;
  size_t strLen = length == 0 ? strlen(str) : length;
  ret = new char[strLen + 1];
  memcpy(ret, str, static_cast<size_t>(strLen));
  ret[strLen] = '\0';
  return ret;
}

void CoreSideInScript::CallNative(const char *page_id, const char *task,
                                  const char *callback) {
  if (page_id == nullptr || task == nullptr) return;

  //  WeexCoreManager::Instance()->script_thread()->message_loop()->PostTask(
  //      weex::base::MakeCopyable(
  //          [pageId = std::unique_ptr<char[]>(copyStr(page_id)),
  //           taskS = std::unique_ptr<char[]>(copyStr(task)),
  //           callbackS = std::unique_ptr<char[]>(copyStr(callback))] {
  //            if (strcmp(taskS.get(),
  //                       "[{\"module\":\"dom\",\"method\":\"createFinish\","
  //                       "\"args\":[]}]") == 0) {
  //              RenderManager::GetInstance()->CreateFinish(pageId.get()) ? 0 :
  //              -1;
  //            } else {
  //              WeexCoreManager::Instance()
  //                  ->getPlatformBridge()
  //                  ->platform_side()
  //                  ->CallNative(pageId.get(), taskS.get(), callbackS.get());
  //            }
  //          }));
  if (strcmp(task,
             "[{\"module\":\"dom\",\"method\":\"createFinish\","
             "\"args\":[]}]") == 0) {
    RenderManager::GetInstance()->CreateFinish(page_id);
  } else {
    WeexCoreManager::Instance()
        ->getPlatformBridge()
        ->platform_side()
        ->CallNative(page_id, task, callback);
  }
}

std::unique_ptr<ValueWithType> CoreSideInScript::CallNativeModule(
    const char *page_id, const char *module, const char *method,
    const char *arguments, int arguments_length, const char *options,
    int options_length) {
  std::unique_ptr<ValueWithType> ret(new ValueWithType);
  ret->type = ParamsType::INT32;
  ret->value.int32Value = -1;
  if (page_id != nullptr && module != nullptr && method != nullptr) {
    return WeexCoreManager::Instance()
        ->getPlatformBridge()
        ->platform_side()
        ->CallNativeModule(page_id, module, method, arguments, arguments_length,
                           options, options_length);
  }

  return ret;
}

void CoreSideInScript::CallNativeComponent(const char *page_id, const char *ref,
                                           const char *method,
                                           const char *arguments,
                                           int arguments_length,
                                           const char *options,
                                           int options_length) {
  if (page_id != nullptr && ref != nullptr && method != nullptr) {
    //    WeexCoreManager::Instance()->script_thread()->message_loop()->PostTask(
    //        weex::base::MakeCopyable(
    //            [pageId = std::unique_ptr<char[]>(copyStr(page_id)),
    //             refS = std::unique_ptr<char[]>(copyStr(ref)),
    //             methodS = std::unique_ptr<char[]>(copyStr(method)),
    //             argumentsS =
    //                 std::unique_ptr<char[]>(copyStr(arguments,
    //                 arguments_length)),
    //             argLen = arguments_length,
    //             optionsS =
    //                 std::unique_ptr<char[]>(copyStr(options,
    //                 options_length)),
    //             optLen = options_length] {<commit_msg>[core] fix setViewPort not valid problem (#1525)<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 "core/bridge/script/core_side_in_script.h"

#include <cstdlib>
#include "base/LogDefines.h"
#include "base/make_copyable.h"
#include "base/thread/waitable_event.h"
#include "core/manager/weex_core_manager.h"
#include "core/render/manager/render_manager.h"
#ifdef OS_ANDROID
#include "android/jsengine/multiprocess/ExtendJSApi.h"
#endif

namespace WeexCore {

CoreSideInScript::CoreSideInScript() {}

CoreSideInScript::~CoreSideInScript() {}

inline char *copyStr(const char *str, int length = 0) {
  char *ret = nullptr;
  if (str == nullptr) return ret;
  size_t strLen = length == 0 ? strlen(str) : length;
  ret = new char[strLen + 1];
  memcpy(ret, str, static_cast<size_t>(strLen));
  ret[strLen] = '\0';
  return ret;
}

void CoreSideInScript::CallNative(const char *page_id, const char *task,
                                  const char *callback) {
  if (page_id == nullptr || task == nullptr) return;

  //  WeexCoreManager::Instance()->script_thread()->message_loop()->PostTask(
  //      weex::base::MakeCopyable(
  //          [pageId = std::unique_ptr<char[]>(copyStr(page_id)),
  //           taskS = std::unique_ptr<char[]>(copyStr(task)),
  //           callbackS = std::unique_ptr<char[]>(copyStr(callback))] {
  //            if (strcmp(taskS.get(),
  //                       "[{\"module\":\"dom\",\"method\":\"createFinish\","
  //                       "\"args\":[]}]") == 0) {
  //              RenderManager::GetInstance()->CreateFinish(pageId.get()) ? 0 :
  //              -1;
  //            } else {
  //              WeexCoreManager::Instance()
  //                  ->getPlatformBridge()
  //                  ->platform_side()
  //                  ->CallNative(pageId.get(), taskS.get(), callbackS.get());
  //            }
  //          }));
  if (strcmp(task,
             "[{\"module\":\"dom\",\"method\":\"createFinish\","
             "\"args\":[]}]") == 0) {
    RenderManager::GetInstance()->CreateFinish(page_id);
  } else {
    WeexCoreManager::Instance()
        ->getPlatformBridge()
        ->platform_side()
        ->CallNative(page_id, task, callback);
  }
}

std::unique_ptr<ValueWithType> CoreSideInScript::CallNativeModule(
    const char *page_id, const char *module, const char *method,
    const char *arguments, int arguments_length, const char *options,
    int options_length) {
  std::unique_ptr<ValueWithType> ret(new ValueWithType);
  ret->type = ParamsType::INT32;
  ret->value.int32Value = -1;
  if (page_id != nullptr && module != nullptr && method != nullptr) {
    RenderManager::GetInstance()->CallNativeModule(page_id, module, method,
                                                   arguments, arguments_length,
                                                   options, options_length);
    return WeexCoreManager::Instance()
        ->getPlatformBridge()
        ->platform_side()
        ->CallNativeModule(page_id, module, method, arguments, arguments_length,
                           options, options_length);
  }

  return ret;
}

void CoreSideInScript::CallNativeComponent(const char *page_id, const char *ref,
                                           const char *method,
                                           const char *arguments,
                                           int arguments_length,
                                           const char *options,
                                           int options_length) {
  if (page_id != nullptr && ref != nullptr && method != nullptr) {
    //    WeexCoreManager::Instance()->script_thread()->message_loop()->PostTask(
    //        weex::base::MakeCopyable(
    //            [pageId = std::unique_ptr<char[]>(copyStr(page_id)),
    //             refS = std::unique_ptr<char[]>(copyStr(ref)),
    //             methodS = std::unique_ptr<char[]>(copyStr(method)),
    //             argumentsS =
    //                 std::unique_ptr<char[]>(copyStr(arguments,
    //                 arguments_length)),
    //             argLen = arguments_length,
    //             optionsS =
    //                 std::unique_ptr<char[]>(copyStr(options,
    //                 options_length)),
    //             optLen = options_length] {
{'fraction_non_alphanumeric': 0.13670526101970343, 'fraction_numerical': 0.00385943530367662, 'mean_word_length': 2.223240589198036, 'pattern_counts': {'":': 12, '<': 27, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5042534', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2695, 'n_words': 641}