http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/android/jsengine/multiprocess/ExtendJSApi.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/android/jsengine/multiprocess/ExtendJSApi.cpp b/weex_core/Source/android/jsengine/multiprocess/ExtendJSApi.cpp index 6cd9dce..36eeabd 100644 --- a/weex_core/Source/android/jsengine/multiprocess/ExtendJSApi.cpp +++ b/weex_core/Source/android/jsengine/multiprocess/ExtendJSApi.cpp @@ -265,10 +265,13 @@ std::unique_ptr<IPCResult> handleCallNativeModule(IPCArguments *arguments) { char* pageId = getArumentAsCStr(arguments, 0); char* module = getArumentAsCStr(arguments, 1); char* method = getArumentAsCStr(arguments, 2); - char* argString = getArumentAsCStr(arguments, 3); - char* optString = getArumentAsCStr(arguments, 4); + char* argumentsData = getArumentAsCStr(arguments, 3); + int argumentsDataLength = getArumentAsCStrLen(arguments, 3); + char* optionsData = getArumentAsCStr(arguments, 4); + int optionsDataLength = getArumentAsCStrLen(arguments, 4); - std::unique_ptr<IPCResult> ret = _callNativeModule(pageId,module,method,argString,optString); + std::unique_ptr<IPCResult> ret = _callNativeModule(pageId,module,method,argumentsData, + argumentsDataLength, optionsData, optionsDataLength); if (pageId != nullptr) { delete[]pageId; @@ -282,13 +285,13 @@ std::unique_ptr<IPCResult> handleCallNativeModule(IPCArguments *arguments) { delete[]method; method = nullptr; } - if (argString != nullptr) { - delete[]argString; - argString = nullptr; + if (argumentsData != nullptr) { + delete[]argumentsData; + argumentsData = nullptr; } - if (optString != nullptr) { - delete[]optString; - optString = nullptr; + if (optionsData != nullptr) { + delete[]optionsData; + optionsData = nullptr; } return ret; } @@ -297,10 +300,12 @@ std::unique_ptr<IPCResult> handleCallNativeComponent(IPCArguments *arguments) { char* pageId = getArumentAsCStr(arguments, 0); char* ref = getArumentAsCStr(arguments, 1); char* method = getArumentAsCStr(arguments, 2); - char* argString = getArumentAsCStr(arguments, 3); - char* optString = getArumentAsCStr(arguments, 4); + char* argumentsData = getArumentAsCStr(arguments, 3); + int argumentsDataLength = getArumentAsCStrLen(arguments, 3); + char* optionsData = getArumentAsCStr(arguments, 4); + int optionsDataLength = getArumentAsCStrLen(arguments, 4); - _callNativeComponent(pageId, ref, method, argString, optString); + _callNativeComponent(pageId, ref, method, argumentsData, argumentsDataLength, optionsData, optionsDataLength); if (pageId != nullptr) { delete[]pageId; @@ -314,13 +319,13 @@ std::unique_ptr<IPCResult> handleCallNativeComponent(IPCArguments *arguments) { delete[]method; method = nullptr; } - if (argString != nullptr) { - delete[]argString; - argString = nullptr; + if (argumentsData != nullptr) { + delete[]argumentsData; + argumentsData = nullptr; } - if (optString != nullptr) { - delete[]optString; - optString = nullptr; + if (optionsData != nullptr) { + delete[]optionsData; + optionsData = nullptr; } return createInt32Result(static_cast<int32_t>(true)); }
http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp b/weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp index 44a9aca..0c8f70c 100644 --- a/weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp +++ b/weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp @@ -110,6 +110,80 @@ namespace WeexCore { return param; } + inline void addParamsFromJArgs(std::vector<VALUE_WITH_TYPE *>& params, VALUE_WITH_TYPE *param, std::unique_ptr<IPCSerializer>& serializer, JNIEnv* env, jobject jArg){ + jfieldID jTypeId = env->GetFieldID(jWXJSObject, "type", "I"); + jint jTypeInt = env->GetIntField(jArg, jTypeId); + + jfieldID jDataId = env->GetFieldID(jWXJSObject, "data", "Ljava/lang/Object;"); + jobject jDataObj = env->GetObjectField(jArg, jDataId); + if (jTypeInt == 1) { + if (jDoubleValueMethodId == NULL) { + jclass jDoubleClazz = env->FindClass("java/lang/Double"); + jDoubleValueMethodId = env->GetMethodID(jDoubleClazz, "doubleValue", "()D"); + env->DeleteLocalRef(jDoubleClazz); + } + jdouble jDoubleObj = env->CallDoubleMethod(jDataObj, jDoubleValueMethodId); + if (js_server_api_functions != nullptr) { + param->type = ParamsType::DOUBLE; + param->value.doubleValue = jDoubleObj; + } else { + serializer->add(jDoubleObj); + } + + } else if (jTypeInt == 2) { + jstring jDataStr = (jstring) jDataObj; + if (js_server_api_functions != nullptr) { + param->type = ParamsType::STRING; + param->value.string = jstring2WeexString(env, jDataStr); + } else { + addString(env, serializer.get(), jDataStr); + } + } else if (jTypeInt == 3) { + jstring jDataStr = (jstring) jDataObj; + if (js_server_api_functions != nullptr) { + param->type = ParamsType::JSONSTRING; + param->value.string = jstring2WeexString(env, jDataStr); + } else { + addJSONString(env, serializer.get(), jDataStr); + } + } else if (jTypeInt == 4) { + jbyteArray dataArray = (jbyteArray) jDataObj; + if (js_server_api_functions != nullptr) { + param->type = ParamsType::BYTEARRAY; + jbyte* data = env->GetByteArrayElements(dataArray, 0); + size_t length = env->GetArrayLength(dataArray); + param->value.byteArray = genWeexByteArray((const char *) data, length); + } else { + addBinaryByteArray(env, serializer.get(), dataArray); + } + } else { + if (js_server_api_functions != nullptr) { + param->type = ParamsType::JSUNDEFINED; + } else { + serializer->addJSUndefined(); + } + } + + if (param != nullptr){ + params.push_back(param); + } + + env->DeleteLocalRef(jDataObj); + } + + inline void freeParams(std::vector<VALUE_WITH_TYPE *>& params){ + for (auto ¶m : params) { + if (param->type == ParamsType::STRING || + param->type == ParamsType::JSONSTRING) { + free(param->value.string); + } + if (param->type == ParamsType::BYTEARRAY) { + free(param->value.byteArray); + } + free(param); + } + } + jint WeexProxy::doInitFramework(JNIEnv *env, jobject object, jstring script, jobject params, jstring cacheDir, jboolean pieSupport) { @@ -295,55 +369,7 @@ namespace WeexCore { } jobject jArg = env->GetObjectArrayElement(jargs, i); - - jfieldID jTypeId = env->GetFieldID(jWXJSObject, "type", "I"); - jint jTypeInt = env->GetIntField(jArg, jTypeId); - - jfieldID jDataId = env->GetFieldID(jWXJSObject, - "data", "Ljava/lang/Object;"); - jobject jDataObj = env->GetObjectField(jArg, jDataId); - if (jTypeInt == 1) { - if (jDoubleValueMethodId == NULL) { - jclass jDoubleClazz = env->FindClass("java/lang/Double"); - jDoubleValueMethodId = env->GetMethodID(jDoubleClazz, - "doubleValue", "()D"); - env->DeleteLocalRef(jDoubleClazz); - } - jdouble jDoubleObj = env->CallDoubleMethod(jDataObj, - jDoubleValueMethodId); - if (js_server_api_functions != nullptr) { - param->type = ParamsType::DOUBLE; - param->value.doubleValue = jDoubleObj; - } else { - serializer->add(jDoubleObj); - } - - } else if (jTypeInt == 2) { - jstring jDataStr = (jstring) jDataObj; - if (js_server_api_functions != nullptr) { - param->type = ParamsType::STRING; - param->value.string = jstring2WeexString(env, jDataStr); - } else { - addString(env, serializer.get(), jDataStr); - } - } else if (jTypeInt == 3) { - jstring jDataStr = (jstring) jDataObj; - if (js_server_api_functions != nullptr) { - param->type = ParamsType::JSONSTRING; - param->value.string = jstring2WeexString(env, jDataStr); - } else { - addJSONString(env, serializer.get(), jDataStr); - } - } else { - if (js_server_api_functions != nullptr) { - param->type = ParamsType::JSUNDEFINED; - } else { - serializer->addJSUndefined(); - } - } - if (param != nullptr) - params.push_back(param); - env->DeleteLocalRef(jDataObj); + addParamsFromJArgs(params, param,serializer, env, jArg); env->DeleteLocalRef(jArg); } @@ -363,13 +389,7 @@ namespace WeexCore { name, funcChar.getChars(), params); - for (auto ¶m : params) { - if (param->type == ParamsType::STRING || - param->type == ParamsType::JSONSTRING) { - free(param->value.string); - } - free(param); - } + freeParams(params); return static_cast<bool>(i); } else { std::unique_ptr<IPCBuffer> buffer = serializer->finish(); @@ -929,73 +949,26 @@ namespace WeexCore { } jobject jArg = env->GetObjectArrayElement(jargs, i); - - jfieldID jTypeId = env->GetFieldID(jWXJSObject, "type", "I"); - jint jTypeInt = env->GetIntField(jArg, jTypeId); - - jfieldID jDataId = env->GetFieldID(jWXJSObject, "data", "Ljava/lang/Object;"); - jobject jDataObj = env->GetObjectField(jArg, jDataId); - if (jTypeInt == 1) { - if (jDoubleValueMethodId == NULL) { - jclass jDoubleClazz = env->FindClass("java/lang/Double"); - jDoubleValueMethodId = env->GetMethodID(jDoubleClazz, "doubleValue", - "()D"); - env->DeleteLocalRef(jDoubleClazz); - } - jdouble jDoubleObj = env->CallDoubleMethod(jDataObj, jDoubleValueMethodId); - if (js_server_api_functions != nullptr) { - param->type = ParamsType::DOUBLE; - param->value.doubleValue = jDoubleObj; - } else { - serializer->add(jDoubleObj); - } - - } else if (jTypeInt == 2) { - jstring jDataStr = (jstring) jDataObj; - if (js_server_api_functions != nullptr) { - param->type = ParamsType::STRING; - param->value.string = jstring2WeexString(env, jDataStr); - } else { - addString(env, serializer.get(), jDataStr); - } - } else if (jTypeInt == 3) { - jstring jDataStr = (jstring) jDataObj; - if (js_server_api_functions != nullptr) { - param->type = ParamsType::JSONSTRING; - param->value.string = jstring2WeexString(env, jDataStr); - } else { - addJSONString(env, serializer.get(), jDataStr); - } - } else { - if (js_server_api_functions != nullptr) { - param->type = ParamsType::JSUNDEFINED; - } else { - serializer->addJSUndefined(); - } - } - if (param != nullptr) - params.push_back(param); - env->DeleteLocalRef(jDataObj); + addParamsFromJArgs(params, param, serializer, env, jArg); env->DeleteLocalRef(jArg); } if (js_server_api_functions != nullptr) { - char *result = js_server_api_functions->funcExeJSWithResult( - env->GetStringUTFChars(jinstanceid, - nullptr), - env->GetStringUTFChars(jnamespace, - nullptr), - env->GetStringUTFChars(jfunction, - nullptr), - params); - for (auto ¶m : params) { - if (param->type == ParamsType::STRING || - param->type == ParamsType::JSONSTRING) { - free(param->value.string); - } - free(param); + ScopedJStringUTF8 instanceidChar(env, jinstanceid); + const char *namespaceChar; + if (jnamespace) { + ScopedJStringUTF8 nameSpaceChar(env, jnamespace); + namespaceChar = nameSpaceChar.getChars(); + } else { + namespaceChar = nullptr; } - return newJByteArray(env, result); + ScopedJStringUTF8 functionChar(env, jfunction); + WeexJSResult jsResultData = js_server_api_functions->funcExeJSWithResult( + instanceidChar.getChars(), namespaceChar, functionChar.getChars(), params); + freeParams(params); + jbyteArray array = newJByteArray(env, jsResultData.data, jsResultData.length); + WeexJSResultDataFree(jsResultData); + return array; } else { std::unique_ptr<IPCBuffer> buffer = serializer->finish(); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/api/WeexJSCoreApi.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/api/WeexJSCoreApi.cpp b/weex_core/Source/core/api/WeexJSCoreApi.cpp index 3ff7532..70ca722 100644 --- a/weex_core/Source/core/api/WeexJSCoreApi.cpp +++ b/weex_core/Source/core/api/WeexJSCoreApi.cpp @@ -25,6 +25,7 @@ #include <core/render/manager/render_manager.h> #include <android/jsengine/multiprocess/ExtendJSApi.h> #include <android/base/string/string_utils.h> +#include <wson/wson_parser.h> using namespace WeexCore; @@ -56,7 +57,7 @@ void _callNative(const char *pageId, const char *task, const char *callback) { std::unique_ptr<IPCResult> _callNativeModule(const char *pageId, const char *module, const char *method, - const char *argString, const char *optString) { + const char *arguments, int argumentsLength, const char *options, int optionsLength) { std::unique_ptr<IPCResult> ret = createInt32Result(-1); if (pageId != nullptr && module != nullptr && method != nullptr) { #if JSAPI_LOG @@ -67,10 +68,10 @@ _callNativeModule(const char *pageId, const char *module, const char *method, // add for android support jobject result; result = Bridge_Impl_Android::getInstance()->callNativeModule(pageId, module, method, - argString, optString); - - if (result == nullptr) + arguments, argumentsLength, options, optionsLength); + if (result == nullptr){ return ret; + } JNIEnv *env = getJNIEnv(); jfieldID jTypeId = env->GetFieldID(jWXJSObject, "type", "I"); @@ -92,16 +93,26 @@ _callNativeModule(const char *pageId, const char *module, const char *method, } else if (jTypeInt == 3) { jstring jDataStr = (jstring) jDataObj; ret = std::move(createJSONStringResult(env, jDataStr)); + } else if (jTypeInt == 4) { + jbyteArray array = (jbyteArray)jDataObj; + if(array != nullptr){ + int length = env->GetArrayLength(array); + void* data = env->GetByteArrayElements(array, 0); + ret = std::move(createByteArrayResult((const char*)data, length)); + env->ReleaseByteArrayElements(array, (jbyte*)data, 0); + } } env->DeleteLocalRef(jDataObj); + if(result != nullptr){ + env->DeleteLocalRef(result); + } } return ret; } -void _callNativeComponent(const char *pageId, const char *ref, - const char *method, const char *argString, - const char *optString) { +void _callNativeComponent(const char *pageId, const char *ref, const char *method, + const char *arguments, int argumentsLength, const char *options, int optionsLength) { if (pageId != nullptr && ref != nullptr && method != nullptr) { #if JSAPI_LOG @@ -109,8 +120,8 @@ void _callNativeComponent(const char *pageId, const char *ref, pageId, ref, method, argString, optString); #endif - Bridge_Impl_Android::getInstance()->callNativeComponent(pageId, ref, method, argString, - optString); + Bridge_Impl_Android::getInstance()->callNativeComponent(pageId, ref, method, + arguments, argumentsLength, options, optionsLength); } } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/api/WeexJSCoreApi.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/api/WeexJSCoreApi.h b/weex_core/Source/core/api/WeexJSCoreApi.h index 8ef33cc..cf0b81e 100644 --- a/weex_core/Source/core/api/WeexJSCoreApi.h +++ b/weex_core/Source/core/api/WeexJSCoreApi.h @@ -37,11 +37,11 @@ void _callNative(const char *pageId, const char *task, const char *callback); std::unique_ptr<IPCResult> _callNativeModule(const char *pageId, const char *module, const char *method, - const char *argString, const char *optString); + const char *arguments, int argumentsLength, const char *options, int optionsLength); void _callNativeComponent(const char *pageId, const char *ref, - const char *method, const char *argString, - const char *optString); + const char *method, + const char *arguments, int argumentsLength, const char *options, int optionsLength); void _callAddElement(const char *pageId, const char *parentRef, const char *domStr, const char *index_cstr); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/bridge/bridge.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/bridge/bridge.h b/weex_core/Source/core/bridge/bridge.h index 4494ac5..591bb89 100644 --- a/weex_core/Source/core/bridge/bridge.h +++ b/weex_core/Source/core/bridge/bridge.h @@ -42,10 +42,10 @@ namespace WeexCore { virtual int callNative(const char* pageId, const char *task, const char *callback) = 0; virtual jobject callNativeModule(const char* pageId, const char *module, const char *method, - const char *argString, const char *optString) = 0; + const char *arguments, int argumentsLength, const char *options, int optionsLength) = 0; virtual void callNativeComponent(const char* pageId, const char* ref, const char *method, - const char *argString, const char *optString) = 0; + const char *arguments, int argumentsLength, const char *options, int optionsLength) = 0; virtual void setTimeout(const char* callbackID, const char* time) = 0; http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/parser/dom_parser.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/parser/dom_parser.cpp b/weex_core/Source/core/parser/dom_parser.cpp deleted file mode 100644 index 216ad72..0000000 --- a/weex_core/Source/core/parser/dom_parser.cpp +++ /dev/null @@ -1,532 +0,0 @@ -/** - * 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 "dom_parser.h" -#include <core/render/node/render_object.h> -#include <core/render/page/render_page.h> -#include <core/render/node/factory/render_creator.h> -#include <base/ViewUtils.h> - -using namespace std; -using namespace rapidjson; - -namespace WeexCore { - - static inline int matchNum(std::string temp, std::string key, int startIndex, int endIndex) { - int num = 0; - while (startIndex < endIndex) { - int index = temp.find(key, startIndex); - if (index >= 0 && index < endIndex) { - num++; - startIndex = index + 1; - } else { - break; - } - } - return num; - } - - static inline int GetSplitIndex(std::string temp, const char * leftChar, const char * rightChar){ - int endIndex = temp.find(rightChar); - if(endIndex > 0) { - // has found! - int temp_length = temp.length(); - int startIndex = 0; - int leftMatchSize = matchNum(temp, leftChar, startIndex, endIndex) - 1; - while (leftMatchSize > 0 && startIndex <= endIndex && startIndex < temp_length) { - startIndex = endIndex + 1; - int markIndex = temp.find(rightChar, startIndex); - if(markIndex > 0) { - endIndex = markIndex; - leftMatchSize--; - } - leftMatchSize += matchNum(temp, leftChar, startIndex, endIndex); - } - } - return endIndex; - } - - bool JsonParserHandler::Null() { - st_ = kHasNull; - v_.SetNull(); - return true; - } - - bool JsonParserHandler::Bool(bool b) { - st_ = kHasBool; - v_.SetBool(b); - return true; - } - - bool JsonParserHandler::Int(int i) { - st_ = kHasNumber; - v_.SetInt(i); - return true; - } - - bool JsonParserHandler::Uint(unsigned u) { - st_ = kHasNumber; - v_.SetUint(u); - return true; - } - - bool JsonParserHandler::Int64(int64_t i) { - st_ = kHasNumber; - v_.SetInt64(i); - return true; - } - - bool JsonParserHandler::Uint64(uint64_t u) { - st_ = kHasNumber; - v_.SetUint64(u); - return true; - } - - bool JsonParserHandler::Double(double d) { - st_ = kHasNumber; - v_.SetDouble(d); - return true; - } - - bool JsonParserHandler::RawNumber(const char *, SizeType, bool) { return false; } - - bool JsonParserHandler::String(const char *str, SizeType length, bool) { - st_ = kHasString; - v_.SetString(str, length); - return true; - } - - bool JsonParserHandler::StartObject() { - st_ = kEnteringObject; - return true; - } - - bool JsonParserHandler::Key(const char *str, SizeType length, bool) { - st_ = kHasKey; - v_.SetString(str, length); - return true; - } - - bool JsonParserHandler::EndObject(SizeType) { - st_ = kExitingObject; - return true; - } - - bool JsonParserHandler::StartArray() { - st_ = kEnteringArray; - return true; - } - - bool JsonParserHandler::EndArray(SizeType) { - st_ = kExitingArray; - return true; - } - - JsonParserHandler::JsonParserHandler(char *str) : v_(), st_(kInit), r_(), ss_(str) { - r_.IterativeParseInit(); - ParseNext(); - } - - void JsonParserHandler::ParseNext() { - if (r_.HasParseError()) { - st_ = kError; - return; - } - - r_.IterativeParseNext<parseFlags>(ss_, *this); - } - - bool JsonParser::EnterObject() { - if (st_ != kEnteringObject) { - st_ = kError; - return false; - } - - ParseNext(); - return true; - } - - bool JsonParser::EnterArray() { - if (st_ != kEnteringArray) { - st_ = kError; - return false; - } - - ParseNext(); - return true; - } - - const char *JsonParser::NextObjectKey() { - if (st_ == kHasKey) { - const char *result = v_.GetString(); - ParseNext(); - return result; - } - - if (st_ != kExitingObject) { - st_ = kError; - return 0; - } - - ParseNext(); - return 0; - } - - bool JsonParser::NextArrayValue() { - if (st_ == kExitingArray) { - ParseNext(); - return false; - } - - if (st_ == kError || st_ == kExitingObject || st_ == kHasKey) { - st_ = kError; - return false; - } - - return true; - } - - int JsonParser::GetInt() { - if (st_ != kHasNumber || !v_.IsInt()) { - st_ = kError; - return 0; - } - - int result = v_.GetInt(); - ParseNext(); - return result; - } - - double JsonParser::GetDouble() { - if (st_ != kHasNumber) { - st_ = kError; - return 0.; - } - - double result = v_.GetDouble(); - ParseNext(); - return result; - } - - bool JsonParser::GetBool() { - if (st_ != kHasBool) { - st_ = kError; - return false; - } - - bool result = v_.GetBool(); - ParseNext(); - return result; - } - - void JsonParser::GetNull() { - if (st_ != kHasNull) { - st_ = kError; - return; - } - - ParseNext(); - } - - const char *JsonParser::GetString() { - if (st_ != kHasString) { - st_ = kError; - return 0; - } - - const char *temp = v_.GetString(); - int len = v_.GetStringLength(); - char *result = new char[len + 1]; - strcpy(result, temp); - result[len] = '\0'; - ParseNext(); - return result; - } - - const std::string JsonParser::GetObjectStr() { - std::string temp = "{"; - temp.append(Stringify()); - int endIndex = GetSplitIndex(temp, "{", "}"); - SkipValue(); - return temp.substr(0, endIndex + 1); - } - - const std::string JsonParser::GetArrayStr() { - std::string temp = "["; - temp.append(Stringify()); - int endIndex = GetSplitIndex(temp, "[", "]"); - SkipValue(); - return temp.substr(0, endIndex + 1); - } - - const char *JsonParser::Stringify() { - return ss_.src_; - } - - void JsonParser::SkipOut(int depth) { - do { - if (st_ == kEnteringArray || st_ == kEnteringObject) { - ++depth; - } else if (st_ == kExitingArray || st_ == kExitingObject) { - --depth; - } else if (st_ == kError) { - return; - } - - ParseNext(); - } while (depth > 0); - } - - void JsonParser::SkipValue() { - SkipOut(0); - } - - void JsonParser::SkipArray() { - SkipOut(1); - } - - void JsonParser::SkipObject() { - SkipOut(1); - } - - Value *JsonParser::PeekValue() { - if (st_ >= kHasNull && st_ <= kHasKey) { - return &v_; - } - - return 0; - } - - int JsonParser::PeekType() { - if (st_ >= kHasNull && st_ <= kHasKey) { - return v_.GetType(); - } - - if (st_ == kEnteringArray) { - return kArrayType; - } - - if (st_ == kEnteringObject) { - return kObjectType; - } - - return -1; - } - - RenderObject * - ParseJsonObject(JsonParser &r, RenderObject *parent, int index, const std::string &pageId) { - - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - r.EnterObject(); - - RenderObject *render; - std::string ref; - - while (const char *key = r.NextObjectKey()) { - - if (0 == strcmp(key, "ref")) { - RAPIDJSON_ASSERT(r.PeekType() == kStringType); - const char *temp_ref = r.GetString(); - ref = temp_ref; - if (temp_ref != nullptr) { - delete[]temp_ref; - temp_ref = nullptr; - } - } else if (0 == strcmp(key, "type")) { - RAPIDJSON_ASSERT(r.PeekType() == kStringType); - const char *temp_type = r.GetString(); - render = (RenderObject *) RenderCreator::GetInstance()->CreateRender(temp_type, ref); - render->SetPageId(pageId); - if (parent != nullptr) - parent->AddRenderObject(index, render); - if (temp_type != nullptr) { - delete[]temp_type; - temp_type = nullptr; - } - } else if (0 == strcmp(key, "attr") || 0 == strcmp(key, "style")) { - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - r.EnterObject(); - while (const char *key2 = r.NextObjectKey()) { - if (r.PeekType() == kNumberType) { - RAPIDJSON_ASSERT(r.PeekType() == kNumberType); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, to_string(r.GetDouble())); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, to_string(r.GetDouble())); - } - } else if (r.PeekType() == kStringType) { - RAPIDJSON_ASSERT(r.PeekType() == kStringType); - const char *temp_str = r.GetString(); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, temp_str); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, temp_str); - } - if (temp_str != nullptr) { - delete[]temp_str; - temp_str = nullptr; - } - } else if (r.PeekType() == kArrayType) { - RAPIDJSON_ASSERT(r.PeekType() == kArrayType); - std::string value = r.GetArrayStr(); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, value); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, value); - } - } else if (r.PeekType() == kTrueType) { - RAPIDJSON_ASSERT(r.PeekType() == kTrueType); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, "true"); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, "true"); - } - r.SkipValue(); - } else if (r.PeekType() == kFalseType) { - RAPIDJSON_ASSERT(r.PeekType() == kFalseType); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, "false"); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, "false"); - } - r.SkipValue(); - } else if (r.PeekType() == kObjectType) { - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - std::string value = r.GetObjectStr(); - if (0 == strcmp(key, "attr")) { - render->AddAttr(key2, value); - } else if (0 == strcmp(key, "style")) { - render->AddStyle(key2, value); - } - } else { - r.SkipValue(); - } - } - } else if (0 == strcmp(key, "event")) { - if (r.PeekType() == kArrayType) { - RAPIDJSON_ASSERT(r.PeekType() == kArrayType); - r.EnterArray(); - while (r.NextArrayValue()) { - std::string temp_event_str; - if(r.PeekType() == kStringType) { - RAPIDJSON_ASSERT(r.PeekType() == kStringType); - const char *temp_event = r.GetString(); - temp_event_str.append(temp_event); - delete[]temp_event; - } else if(r.PeekType() == kObjectType){ - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - temp_event_str = r.GetObjectStr(); - } else { - r.SkipValue(); - } - - if (temp_event_str.length() > 0) { - render->AddEvent(temp_event_str); - } - } - } - - } else if (0 == strcmp(key, "children")) { - RAPIDJSON_ASSERT(r.PeekType() == kArrayType); - r.EnterArray(); - - int index = 0; - - while (r.NextArrayValue()) { - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - ParseJsonObject(r, render, index, pageId); - index++; - } - } else { - r.SkipValue(); - } - } - - if (render != nullptr) { - render->ApplyDefaultStyle(); - render->ApplyDefaultAttr(); - } - - return render; - } - -/** - * Parse json data to RenderObject - * @param data : json data (include ref/style/attr/event/children) - * @param page : {@link RenderPage} - * @return {@link RenderObject*} - */ - RenderObject *Json2RenderObject(char *data, const std::string &pageId) { - JsonParser r(data); - return ParseJsonObject(r, nullptr, 0, pageId); - } - - std::vector<std::pair<std::string, std::string>> *Json2Pairs(char *data) { - std::vector<std::pair<std::string, std::string>> *pairs = nullptr; - JsonParser r(data); - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - r.EnterObject(); - pairs = new std::vector<std::pair<std::string, std::string>>(); - while (const char *key = r.NextObjectKey()) { - if (r.PeekType() == kNumberType) { - RAPIDJSON_ASSERT(r.PeekType() == kNumberType); - std::pair<std::string, std::string> myPair(key, to_string(r.GetDouble())); - pairs->insert(pairs->end(), myPair); - } else if (r.PeekType() == kStringType) { - RAPIDJSON_ASSERT(r.PeekType() == kStringType); - const char *value = r.GetString(); - std::pair<std::string, std::string> myPair(key, value); - pairs->insert(pairs->end(), myPair); - if (value != nullptr) { - delete value; - value = nullptr; - } - } else if (r.PeekType() == kArrayType) { - RAPIDJSON_ASSERT(r.PeekType() == kArrayType); - std::string value = r.GetArrayStr(); - std::pair<std::string, std::string> myPair(key, value); - pairs->insert(pairs->end(), myPair); - } else if (r.PeekType() == kTrueType) { - RAPIDJSON_ASSERT(r.PeekType() == kTrueType); - std::pair<std::string, std::string> myPair(key, "true"); - pairs->insert(pairs->end(), myPair); - r.SkipValue(); - } else if (r.PeekType() == kFalseType) { - RAPIDJSON_ASSERT(r.PeekType() == kFalseType); - std::pair<std::string, std::string> myPair(key, "false"); - pairs->insert(pairs->end(), myPair); - r.SkipValue(); - } else if (r.PeekType() == kNullType) { - RAPIDJSON_ASSERT(r.PeekType() == kNullType); - std::pair<std::string, std::string> myPair(key, ""); - pairs->insert(pairs->end(), myPair); - r.SkipValue(); - } else if (r.PeekType() == kObjectType) { - RAPIDJSON_ASSERT(r.PeekType() == kObjectType); - std::string value = r.GetObjectStr(); - std::pair<std::string, std::string> myPair(key, value); - pairs->insert(pairs->end(), myPair); - } else { - r.SkipValue(); - } - } - return pairs; - } -} - http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/parser/dom_parser.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/parser/dom_parser.h b/weex_core/Source/core/parser/dom_parser.h deleted file mode 100644 index e5ae4be..0000000 --- a/weex_core/Source/core/parser/dom_parser.h +++ /dev/null @@ -1,151 +0,0 @@ -/** - * 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. - */ -#ifndef WEEX_JSON_TOOLS -#define WEEX_JSON_TOOLS - -#include "rapidjson/pointer.h" -#include <vector> -#include <string> - -namespace WeexCore { - - class RenderObject; - - class RenderPage; - -/** - * Use to handler json parser result - */ - class JsonParserHandler { - public: - bool Null(); - - bool Bool(bool b); - - bool Int(int i); - - bool Uint(unsigned u); - - bool Int64(int64_t i); - - bool Uint64(uint64_t u); - - bool Double(double d); - - bool RawNumber(const char *, rapidjson::SizeType, bool); - - bool String(const char *str, rapidjson::SizeType length, bool); - - bool StartObject(); - - bool Key(const char *str, rapidjson::SizeType length, bool); - - bool EndObject(rapidjson::SizeType); - - bool StartArray(); - - bool EndArray(rapidjson::SizeType); - - protected: - JsonParserHandler(char *str); - - void ParseNext(); - - protected: - enum JsonParsingState { - kInit, - kError, - kHasNull, - kHasBool, - kHasNumber, - kHasString, - kHasKey, - kEnteringObject, - kExitingObject, - kEnteringArray, - kExitingArray - }; - - rapidjson::Value v_; - JsonParsingState st_; - rapidjson::Reader r_; - rapidjson::InsituStringStream ss_; - - static const int parseFlags = rapidjson::kParseDefaultFlags | rapidjson::kParseInsituFlag; - }; - -/** - * Use to parse json data - */ - class JsonParser : public JsonParserHandler { - public: - JsonParser(char *str) : JsonParserHandler(str) {} - - bool EnterObject(); - - bool EnterArray(); - - const char *NextObjectKey(); - - bool NextArrayValue(); - - int GetInt(); - - double GetDouble(); - - const char *GetString(); - - const std::string GetObjectStr(); - - const std::string GetArrayStr(); - - const char *Stringify(); - - bool GetBool(); - - void GetNull(); - - void SkipObject(); - - void SkipArray(); - - void SkipValue(); - - rapidjson::Value *PeekValue(); - - int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array) - - bool IsValid() { return st_ != kError; } - - protected: - void SkipOut(int depth); - }; - -/** - * Parse json data to RenderObject - * @param data : json data (include ref/style/attr/event/children) - * @param page : {@link RenderPage*} - * @return {@link RenderObject*} - */ - RenderObject *Json2RenderObject(char *data, const std::string &pageId); - - std::vector<std::pair<std::string, std::string>> *Json2Pairs(char *data); -} - -#endif //WEEX_JSON_TOOLS http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/parser/dom_wson.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/parser/dom_wson.cpp b/weex_core/Source/core/parser/dom_wson.cpp new file mode 100644 index 0000000..36ea3c3 --- /dev/null +++ b/weex_core/Source/core/parser/dom_wson.cpp @@ -0,0 +1,224 @@ +/** + * 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. + */ +// +// Created by furture on 2018/5/15. +// + +#include <jni.h> +#include <core/render/node/render_object.h> +#include <core/render/page/render_page.h> +#include <core/render/node/factory/render_creator.h> +#include "dom_wson.h" +#include "wson/wson.h" +#include "wson/wson_parser.h" + +namespace WeexCore { + + + + inline bool keys_order_as_expect(RenderObject *render, bool keyOrderRight){ + return (render != nullptr && keyOrderRight); + } + + + /** + * parser wson to render object + * */ + RenderObject *parserWson2RenderObject(wson_parser& parser, RenderObject *parent, int index, const std::string &pageId){ + int objectType = parser.nextType(); + if(!parser.isMap(objectType)){ + parser.skipValue(objectType); + return nullptr; + } + /** + * because strem key order specified, so will cann't dependecy it's keys order, + * if key orders right parse one time, if not first parse ref type create render object + * then parse others attrs + * */ + int size = parser.nextMapSize(); + std::string ref; + std::string renderType; + RenderObject *render = nullptr; + int state = parser.getState(); + bool keyOrderRight = true; + for(int i=0; i < size; i++){ + std::string objectKey = parser.nextMapKeyUTF8(); + if(0 == strcmp(objectKey.c_str(), "ref")){ + ref = parser.nextStringUTF8(parser.nextType()); + }else if (0 == strcmp(objectKey.c_str(), "type")) { + renderType = parser.nextStringUTF8(parser.nextType()); + render = (RenderObject *) RenderCreator::GetInstance()->CreateRender(renderType, ref); + render->SetPageId(pageId); + if (parent != nullptr){ + parent->AddRenderObject(index, render); + } + }else if (0 == strcmp(objectKey.c_str(), "attr")){ //attr is map object + uint8_t attrType = parser.nextType(); + if(parser.isMap(attrType) && keys_order_as_expect(render, keyOrderRight)){ + int attrMapSize = parser.nextMapSize(); + for(int attrIndex=0; attrIndex<attrMapSize; attrIndex++){ + std::string attrKeyString = parser.nextMapKeyUTF8(); + std::string attrValueString = parser.nextStringUTF8(parser.nextType()); + render->AddAttr(attrKeyString, attrValueString); + } + }else{ + keyOrderRight = keys_order_as_expect(render, keyOrderRight); + parser.skipValue(attrType); + } + }else if (0 == strcmp(objectKey.c_str(), "style")){ //style is map object + uint8_t styleType = parser.nextType(); + if(parser.isMap(styleType) && keys_order_as_expect(render, keyOrderRight)){ + int styleMapSize = parser.nextMapSize(); + for(int styleIndex=0; styleIndex<styleMapSize; styleIndex++){ + std::string styleKeyString = parser.nextMapKeyUTF8(); + std::string styleValueString = parser.nextStringUTF8(parser.nextType()); + render->AddStyle(styleKeyString, styleValueString); + } + }else{ + keyOrderRight = keys_order_as_expect(render, keyOrderRight); + parser.skipValue(styleType); + } + }else if (0 == strcmp(objectKey.c_str(), "event")) {//event is array + uint8_t eventType = parser.nextType(); + if(parser.isArray(eventType) && keys_order_as_expect(render, keyOrderRight)){ + int eventSize = parser.nextArraySize(); + for(int eventIndex=0; eventIndex < eventSize; eventIndex++){ + std::string eventValue = parser.nextStringUTF8(parser.nextType()); + if(eventValue.size() > 0){ + render->AddEvent(eventValue); + } + } + }else{ + keyOrderRight = keys_order_as_expect(render, keyOrderRight); + parser.skipValue(eventType); + } + }else if (0 == strcmp(objectKey.c_str(), "children")) { + uint8_t childType = parser.nextType(); + if(parser.isArray(childType) && keys_order_as_expect(render, keyOrderRight)){ + int childSize = parser.nextArraySize(); + for(int childIndex=0; childIndex < childSize; childIndex++){ + parserWson2RenderObject(parser, render, childIndex, pageId); + } + }else{ + keyOrderRight = keys_order_as_expect(render, keyOrderRight); + parser.skipValue(childType); + } + }else{ + parser.skipValue(parser.nextType()); + } + } + + /** + * if key order not right, parse attr&style events children again + * */ + if(!keyOrderRight && render != nullptr){ + parser.restoreToState(state); + for(int i=0; i < size; i++){ + std::string objectKey = parser.nextMapKeyUTF8(); + if (0 == strcmp(objectKey.c_str(), "attr")){ //attr is map object + uint8_t attrType = parser.nextType(); + if(parser.isMap(attrType)){ + int attrMapSize = parser.nextMapSize(); + for(int attrIndex=0; attrIndex<attrMapSize; attrIndex++){ + std::string attrKeyString = parser.nextMapKeyUTF8(); + std::string attrValueString = parser.nextStringUTF8(parser.nextType()); + render->AddAttr(attrKeyString, attrValueString); + } + }else{ + parser.skipValue(attrType); + } + }else if (0 == strcmp(objectKey.c_str(), "style")){ //style is map object + uint8_t styleType = parser.nextType(); + if(parser.isMap(styleType)){ + int styleMapSize = parser.nextMapSize(); + for(int styleIndex=0; styleIndex<styleMapSize; styleIndex++){ + std::string styleKeyString = parser.nextMapKeyUTF8(); + std::string styleValueString = parser.nextStringUTF8(parser.nextType()); + render->AddStyle(styleKeyString, styleValueString); + } + }else{ + parser.skipValue(styleType); + } + }else if (0 == strcmp(objectKey.c_str(), "event")) {//event is array + uint8_t eventType = parser.nextType(); + if(parser.isArray(eventType)){ + int eventSize = parser.nextArraySize(); + for(int eventIndex=0; eventIndex < eventSize; eventIndex++){ + std::string eventValue = parser.nextStringUTF8(parser.nextType()); + if(eventValue.size() > 0){ + render->AddEvent(eventValue); + } + } + }else{; + parser.skipValue(eventType); + } + }else if (0 == strcmp(objectKey.c_str(), "children")) { + uint8_t childType = parser.nextType(); + if(parser.isArray(childType)){ + int childSize = parser.nextArraySize(); + for(int childIndex=0; childIndex < childSize; childIndex++){ + parserWson2RenderObject(parser, render, childIndex, pageId); + } + }else{ + parser.skipValue(childType); + } + }else{ + parser.skipValue(parser.nextType()); + } + } + } + + + if (render != nullptr) { + render->ApplyDefaultStyle(); + render->ApplyDefaultAttr(); + } + return render; + } + + + RenderObject *Wson2RenderObject(const char *data, const std::string &pageId){ + if(!data){ + return nullptr; + } + wson_parser parser(data); + return parserWson2RenderObject(parser, nullptr, 0, pageId); + } + + std::vector<std::pair<std::string, std::string>> *Wson2Pairs(const char *data){ + if(!data){ + return nullptr; + } + wson_parser parser(data); + std::vector<std::pair<std::string, std::string>> *pairs = nullptr; + uint8_t type = parser.nextType(); + if(parser.isMap(type)){ + pairs = new std::vector<std::pair<std::string, std::string>>(); + int mapSize = parser.nextMapSize(); + for(int index=0; index < mapSize; index++){ + std::string mapKeyString = parser.nextMapKeyUTF8(); + std::string mapValueString = parser.nextStringUTF8(parser.nextType()); + std::pair<std::string, std::string> mapPair(mapKeyString, mapValueString); + pairs->insert(pairs->end(), mapPair); + } + } + return pairs; + }; + +} http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/parser/dom_wson.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/parser/dom_wson.h b/weex_core/Source/core/parser/dom_wson.h new file mode 100644 index 0000000..4574ad3 --- /dev/null +++ b/weex_core/Source/core/parser/dom_wson.h @@ -0,0 +1,42 @@ +/** + * 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. + */ +// +// Created by furture on 2018/5/15. +// + +#ifndef WEEX_PROJECT_WSON_PARSER_H +#define WEEX_PROJECT_WSON_PARSER_H + +#include <vector> +#include <string> + + +namespace WeexCore { + + class RenderObject; + class RenderPage; + + RenderObject *Wson2RenderObject(const char *data, const std::string &pageId); + std::vector<std::pair<std::string, std::string>> *Wson2Pairs(const char *data); +} + + + + +#endif //WEEX_PROJECT_WSON_PARSER_H http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/render/manager/render_manager.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/render/manager/render_manager.cpp b/weex_core/Source/core/render/manager/render_manager.cpp index 0429233..d271b14 100644 --- a/weex_core/Source/core/render/manager/render_manager.cpp +++ b/weex_core/Source/core/render/manager/render_manager.cpp @@ -16,28 +16,28 @@ * specific language governing permissions and limitations * under the License. */ -#include <core/parser/dom_parser.h> #include <core/render/manager/render_manager.h> #include <core/render/page/render_page.h> #include <core/render/node/render_object.h> #include <base/TimeUtils.h> +#include <core/parser/dom_wson.h> namespace WeexCore { RenderManager *RenderManager::m_pInstance = nullptr; - bool RenderManager::CreatePage(std::string pageId, const std::string &data) { + bool RenderManager::CreatePage(std::string pageId, const char* data) { #if RENDER_LOG - LOGD("[RenderManager] CreatePage >>>> pageId: %s, dom data: %s", pageId.c_str(), data.c_str()); + wson_parser parser(data); + LOGD("[RenderManager] CreatePage >>>> pageId: %s, dom data: %s", pageId.c_str(), parser.toStringUTF8().c_str()); #endif RenderPage *page = new RenderPage(pageId); mPages.insert(std::pair<std::string, RenderPage *>(pageId, page)); long long startTime = getCurrentTime(); - char *c_data = (char *) data.data(); - RenderObject *root = Json2RenderObject(c_data, pageId); + RenderObject *root = Wson2RenderObject(data, pageId); page->ParseJsonTime(getCurrentTime() - startTime); page->updateDirty(true); @@ -45,20 +45,20 @@ namespace WeexCore { } bool RenderManager::AddRenderObject(const std::string &pageId, const std::string &parentRef, - int index, const std::string &data) { + int index, const char* data) { RenderPage *page = GetPage(pageId); if (page == nullptr) return false; #if RENDER_LOG + wson_parser parser(data); LOGD("[RenderManager] AddRenderObject >>>> pageId: %s, parentRef: %s, index: %d, dom data: %s", - pageId.c_str(), parentRef.c_str(), index, data.c_str()); + pageId.c_str(), parentRef.c_str(), index, parser.toStringUTF8().c_str()); #endif long long startTime = getCurrentTime(); - char *c_data = (char *) data.data(); - RenderObject *child = Json2RenderObject(c_data, pageId); + RenderObject *child = Wson2RenderObject(data, pageId); page->ParseJsonTime(getCurrentTime() - startTime); if (child == nullptr) @@ -99,19 +99,19 @@ namespace WeexCore { } bool RenderManager::UpdateAttr(const std::string &pageId, const std::string &ref, - const std::string &data) { + const char* data) { RenderPage *page = this->GetPage(pageId); if (page == nullptr) return false; #if RENDER_LOG + wson_parser parser(data); LOGD("[RenderManager] UpdateAttr >>>> pageId: %s, ref: %s, data: %s", - pageId.c_str(), ref.c_str(), data.c_str()); + pageId.c_str(), ref.c_str(), parser.toStringUTF8().c_str()); #endif long long startTime = getCurrentTime(); - char *c_data = (char *) data.data(); - std::vector<std::pair<std::string, std::string>> *attrs = Json2Pairs(c_data); + std::vector<std::pair<std::string, std::string>> *attrs = Wson2Pairs(data); page->ParseJsonTime(getCurrentTime() - startTime); page->updateDirty(true); @@ -119,19 +119,19 @@ namespace WeexCore { } bool RenderManager::UpdateStyle(const std::string &pageId, const std::string &ref, - const std::string &data) { + const char* data) { RenderPage *page = this->GetPage(pageId); if (page == nullptr) return false; #if RENDER_LOG + wson_parser parser(data); LOGD("[RenderManager] UpdateStyle >>>> pageId: %s, ref: %s, data: %s", - pageId.c_str(), ref.c_str(), data.c_str()); + pageId.c_str(), ref.c_str(), parser.toStringUTF8().c_str()); #endif long long startTime = getCurrentTime(); - char *c_data = (char *) data.data(); - std::vector<std::pair<std::string, std::string>> *styles = Json2Pairs(c_data); + std::vector<std::pair<std::string, std::string>> *styles = Wson2Pairs(data); page->ParseJsonTime(getCurrentTime() - startTime); page->updateDirty(true); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/render/manager/render_manager.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/render/manager/render_manager.h b/weex_core/Source/core/render/manager/render_manager.h index fdcd943..fb91605 100644 --- a/weex_core/Source/core/render/manager/render_manager.h +++ b/weex_core/Source/core/render/manager/render_manager.h @@ -21,6 +21,7 @@ #include <map> #include <string> +#include <stdbool.h> namespace WeexCore { @@ -50,10 +51,11 @@ namespace WeexCore { void Batch(const std::string &pageId); // create root node - bool CreatePage(std::string pageId, const std::string &data); + bool CreatePage(std::string pageId, const char* data); + /** use auto constructor is bad idea, it cann't transfer binary, use char* is better */ bool AddRenderObject(const std::string &pageId, const std::string &parentRef, int index, - const std::string &data); + const char* data); bool RemoveRenderObject(const std::string &pageId, const std::string &ref); @@ -61,9 +63,9 @@ namespace WeexCore { MoveRenderObject(const std::string &pageId, const std::string &ref, const std::string &parentRef, int index); - bool UpdateAttr(const std::string &pageId, const std::string &ref, const std::string &data); + bool UpdateAttr(const std::string &pageId, const std::string &ref, const char* data); - bool UpdateStyle(const std::string &pageId, const std::string &ref, const std::string &data); + bool UpdateStyle(const std::string &pageId, const std::string &ref, const char* data); bool AddEvent(const std::string &pageId, const std::string &ref, const std::string &event); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/core/render/page/render_page.cpp ---------------------------------------------------------------------- diff --git a/weex_core/Source/core/render/page/render_page.cpp b/weex_core/Source/core/render/page/render_page.cpp index cb10636..5575692 100644 --- a/weex_core/Source/core/render/page/render_page.cpp +++ b/weex_core/Source/core/render/page/render_page.cpp @@ -16,7 +16,6 @@ * specific language governing permissions and limitations * under the License. */ -#include <core/parser/dom_parser.h> #include <core/render/action/render_action_add_element.h> #include <core/render/action/render_action_remove_element.h> #include <core/render/action/render_action_move_element.h> http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/include/WeexApiHeader.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/include/WeexApiHeader.h b/weex_core/Source/include/WeexApiHeader.h index dfb40ec..e624458 100644 --- a/weex_core/Source/include/WeexApiHeader.h +++ b/weex_core/Source/include/WeexApiHeader.h @@ -37,6 +37,27 @@ struct WeexByteArray { char content[1]; }; +struct WeexJSResult{ + char* data = nullptr; + int length = 0; + bool fromMalloc = false; + bool fromNew = false; +}; + +inline void WeexJSResultDataFree(WeexJSResult& result){ + if(result.fromMalloc){ + free(result.data); + return; + } + if(result.fromNew){ + delete[] result.data; + return; + } +} + + + + typedef void (*FuncSetJSVersion)(const char *jsVersion); typedef void (*FuncReportException)(const char *pageId, const char *func, @@ -46,12 +67,13 @@ typedef void (*FuncCallNative)(const char *pageId, const char *task, const char typedef std::unique_ptr<IPCResult> (*FuncCallNativeModule)(const char *pageId, const char *module, const char *method, - const char *argString, - const char *optString); + const char *arguments, + int argumentsLength, + const char *options, + int optionsLength); -typedef void (*FuncCallNativeComponent)(const char *pageId, const char *ref, - const char *method, const char *argString, - const char *optString); +typedef void (*FuncCallNativeComponent)(const char *pageId, const char *ref, const char *method, + const char *arguments, int argumentsLength, const char *options, int optionsLength); typedef void (*FuncCallAddElement)(const char *pageId, const char *parentRef, const char *domStr, const char *index_cstr); @@ -180,7 +202,7 @@ typedef int (*FuncExeCTimeCallback)(const char *source); typedef int (*FuncExeJS)(const char *instanceId, const char *nameSpace, const char *func, std::vector<VALUE_WITH_TYPE *> params); -typedef char *(*FuncExeJSWithResult)(const char *instanceId, const char *nameSpace, +typedef WeexJSResult(*FuncExeJSWithResult)(const char *instanceId, const char *nameSpace, const char *func, std::vector<VALUE_WITH_TYPE *> params); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/50ae70ce/weex_core/Source/rapidjson/allocators.h ---------------------------------------------------------------------- diff --git a/weex_core/Source/rapidjson/allocators.h b/weex_core/Source/rapidjson/allocators.h deleted file mode 100644 index 655f4a3..0000000 --- a/weex_core/Source/rapidjson/allocators.h +++ /dev/null @@ -1,271 +0,0 @@ -// Tencent is pleased to support the open source community by making RapidJSON available. -// -// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. -// -// Licensed under the MIT License (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://opensource.org/licenses/MIT -// -// 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. - -#ifndef RAPIDJSON_ALLOCATORS_H_ -#define RAPIDJSON_ALLOCATORS_H_ - -#include "rapidjson.h" - -RAPIDJSON_NAMESPACE_BEGIN - -/////////////////////////////////////////////////////////////////////////////// -// Allocator - -/*! \class rapidjson::Allocator - \brief Concept for allocating, resizing and freeing memory block. - - Note that Malloc() and Realloc() are non-static but Free() is static. - - So if an allocator need to support Free(), it needs to put its pointer in - the header of memory block. - -\code -concept Allocator { - static const bool kNeedFree; //!< Whether this allocator needs to call Free(). - - // Allocate a memory block. - // \param size of the memory block in bytes. - // \returns pointer to the memory block. - void* Malloc(size_t size); - - // Resize a memory block. - // \param originalPtr The pointer to current memory block. Null pointer is permitted. - // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) - // \param newSize the new size in bytes. - void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); - - // Free a memory block. - // \param pointer to the memory block. Null pointer is permitted. - static void Free(void *ptr); -}; -\endcode -*/ - -/////////////////////////////////////////////////////////////////////////////// -// CrtAllocator - -//! C-runtime library allocator. -/*! This class is just wrapper for standard C library memory routines. - \note implements Allocator concept -*/ -class CrtAllocator { -public: - static const bool kNeedFree = true; - void* Malloc(size_t size) { - if (size) // behavior of malloc(0) is implementation defined. - return std::malloc(size); - else - return NULL; // standardize to returning NULL. - } - void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { - (void)originalSize; - if (newSize == 0) { - std::free(originalPtr); - return NULL; - } - return std::realloc(originalPtr, newSize); - } - static void Free(void *ptr) { std::free(ptr); } -}; - -/////////////////////////////////////////////////////////////////////////////// -// MemoryPoolAllocator - -//! Default memory allocator used by the parser and DOM. -/*! This allocator allocate memory blocks from pre-allocated memory chunks. - - It does not free memory blocks. And Realloc() only allocate new memory. - - The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. - - User may also supply a buffer as the first chunk. - - If the user-buffer is full then additional chunks are allocated by BaseAllocator. - - The user-buffer is not deallocated by this allocator. - - \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. - \note implements Allocator concept -*/ -template <typename BaseAllocator = CrtAllocator> -class MemoryPoolAllocator { -public: - static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) - - //! Constructor with chunkSize. - /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. - \param baseAllocator The allocator for allocating memory chunks. - */ - MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : - chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) - { - } - - //! Constructor with user-supplied buffer. - /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. - - The user buffer will not be deallocated when this allocator is destructed. - - \param buffer User supplied buffer. - \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). - \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. - \param baseAllocator The allocator for allocating memory chunks. - */ - MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : - chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) - { - RAPIDJSON_ASSERT(buffer != 0); - RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); - chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer); - chunkHead_->capacity = size - sizeof(ChunkHeader); - chunkHead_->size = 0; - chunkHead_->next = 0; - } - - //! Destructor. - /*! This deallocates all memory chunks, excluding the user-supplied buffer. - */ - ~MemoryPoolAllocator() { - Clear(); - RAPIDJSON_DELETE(ownBaseAllocator_); - } - - //! Deallocates all memory chunks, excluding the user-supplied buffer. - void Clear() { - while (chunkHead_ && chunkHead_ != userBuffer_) { - ChunkHeader* next = chunkHead_->next; - baseAllocator_->Free(chunkHead_); - chunkHead_ = next; - } - if (chunkHead_ && chunkHead_ == userBuffer_) - chunkHead_->size = 0; // Clear user buffer - } - - //! Computes the total capacity of allocated memory chunks. - /*! \return total capacity in bytes. - */ - size_t Capacity() const { - size_t capacity = 0; - for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) - capacity += c->capacity; - return capacity; - } - - //! Computes the memory blocks allocated. - /*! \return total used bytes. - */ - size_t Size() const { - size_t size = 0; - for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) - size += c->size; - return size; - } - - //! Allocates a memory block. (concept Allocator) - void* Malloc(size_t size) { - if (!size) - return NULL; - - size = RAPIDJSON_ALIGN(size); - if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) - if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) - return NULL; - - void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; - chunkHead_->size += size; - return buffer; - } - - //! Resizes a memory block (concept Allocator) - void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { - if (originalPtr == 0) - return Malloc(newSize); - - if (newSize == 0) - return NULL; - - originalSize = RAPIDJSON_ALIGN(originalSize); - newSize = RAPIDJSON_ALIGN(newSize); - - // Do not shrink if new size is smaller than original - if (originalSize >= newSize) - return originalPtr; - - // Simply expand it if it is the last allocation and there is sufficient space - if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { - size_t increment = static_cast<size_t>(newSize - originalSize); - if (chunkHead_->size + increment <= chunkHead_->capacity) { - chunkHead_->size += increment; - return originalPtr; - } - } - - // Realloc process: allocate and copy memory, do not free original buffer. - if (void* newBuffer = Malloc(newSize)) { - if (originalSize) - std::memcpy(newBuffer, originalPtr, originalSize); - return newBuffer; - } - else - return NULL; - } - - //! Frees a memory block (concept Allocator) - static void Free(void *ptr) { (void)ptr; } // Do nothing - -private: - //! Copy constructor is not permitted. - MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; - //! Copy assignment operator is not permitted. - MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; - - //! Creates a new chunk. - /*! \param capacity Capacity of the chunk in bytes. - \return true if success. - */ - bool AddChunk(size_t capacity) { - if (!baseAllocator_) - ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)(); - if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { - chunk->capacity = capacity; - chunk->size = 0; - chunk->next = chunkHead_; - chunkHead_ = chunk; - return true; - } - else - return false; - } - - static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. - - //! Chunk header for perpending to each chunk. - /*! Chunks are stored as a singly linked list. - */ - struct ChunkHeader { - size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). - size_t size; //!< Current size of allocated memory in bytes. - ChunkHeader *next; //!< Next chunk in the linked list. - }; - - ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. - size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. - void *userBuffer_; //!< User supplied buffer. - BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. - BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. -}; - -RAPIDJSON_NAMESPACE_END - -#endif // RAPIDJSON_ENCODINGS_H_
