YorkShen closed pull request #1827: [core] add script section in opcode file URL: https://github.com/apache/incubator-weex/pull/1827
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m index 4a04c1443b..3cd20f9b8a 100644 --- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m +++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m @@ -485,7 +485,7 @@ - (void)_renderWithRequest:(WXResourceRequest *)request options:(NSDictionary *) newOptions[bundleUrlOptionKey] = url.absoluteString; } - if ([url.absoluteString hasSuffix:WEEX_LITE_URL_SUFFIX]) { + if ([url.absoluteString hasSuffix:WEEX_LITE_URL_SUFFIX] || [url.absoluteString containsString:@"__eagle=true"]) { newOptions[@"WLASM_RENDER"] = @(YES); } @@ -549,7 +549,7 @@ - (void)_renderWithRequest:(WXResourceRequest *)request options:(NSDictionary *) return; } - if (([options[@"DATA_RENDER"] boolValue] && [options[@"RENDER_WITH_BINARY"] boolValue]) || [options[@"WLASM_RENDER"] boolValue]) { + if (([newOptions[@"DATA_RENDER"] boolValue] && [newOptions[@"RENDER_WITH_BINARY"] boolValue]) || [newOptions[@"WLASM_RENDER"] boolValue]) { [strongSelf _renderWithData:data]; return; } diff --git a/weex_core/Source/core/data_render/exec_state.cc b/weex_core/Source/core/data_render/exec_state.cc index 56aae72010..6ba2054c3c 100644 --- a/weex_core/Source/core/data_render/exec_state.cc +++ b/weex_core/Source/core/data_render/exec_state.cc @@ -92,6 +92,7 @@ void ExecState::Compile(std::string& err) { if (!context()->raw_json().is_null()) { VNodeExecEnv::ParseData(this); VNodeExecEnv::ParseStyle((this)); + VNodeExecEnv::ParseScript(this); ParseResult result = Parser::Parse(context()->raw_json(),err); generator.Visit(result.expr().get(), nullptr); } diff --git a/weex_core/Source/core/data_render/exec_state_binary.cc b/weex_core/Source/core/data_render/exec_state_binary.cc index 0b19a00818..6c57c81053 100644 --- a/weex_core/Source/core/data_render/exec_state_binary.cc +++ b/weex_core/Source/core/data_render/exec_state_binary.cc @@ -69,6 +69,11 @@ bool ExecStateEncoder::encoding(std::string &err) { err = "data section encoding error"; break; } + SectionScript script(this); + if (!script.encoding()) { + err = "script section encoding error"; + break; + } SectionFunction function(this, gs_op_code_bits); if (!function.encoding()) { err = "function section encoding error"; @@ -160,6 +165,14 @@ bool ExecStateDecoder::decoding(std::string &err) { } break; } + case ExecSection::EXEC_SECTION_SCRIPT: + { + SectionScript script(this, section_length); + if (!script.decoding()) { + throw EncoderError("script section decoding error"); + } + break; + } case ExecSection::EXEC_SECTION_FUNCTION: { SectionFunction function(this, gs_op_code_bits, section_length); diff --git a/weex_core/Source/core/data_render/exec_state_section.cc b/weex_core/Source/core/data_render/exec_state_section.cc index b68ea2f068..c40df1cce3 100644 --- a/weex_core/Source/core/data_render/exec_state_section.cc +++ b/weex_core/Source/core/data_render/exec_state_section.cc @@ -1274,6 +1274,154 @@ bool SectionData::decoding() { return finished; } + +uint32_t SectionScript::size() { + uint32_t size = 0; + do { + const json11::Json& scripts = encoder()->exec_state()->context()->script_json(); + if (!scripts.array_items().size()) { + break; + } + size += GetFTLVLength(kValueScriptSize, sizeof(uint32_t)); + for (auto script : scripts.array_items()) { + const json11::Json::object& items = script.object_items(); + size += GetFTLVLength(kValueScriptItemSize, sizeof(uint32_t)); + for (auto iter = items.begin(); iter != items.end(); iter++) { + uint32_t key_length = static_cast<uint32_t>(iter->first.length()); + size += GetFTLVLength(kValueScriptKey, key_length); + uint32_t val_length = static_cast<uint32_t>(iter->second.string_value().length()); + size += GetFTLVLength(kValueScriptValue, val_length); + } + } + } while (0); + + return size; +} + +bool SectionScript::encoding() { + bool finished = false; + do { + uint32_t size = this->size(); + if (!size) { + finished = true; + break; + } + if (!Section::encoding((uint16_t)ExecSection::EXEC_SECTION_SCRIPT, size)) { + break; + } + const json11::Json& scripts = encoder()->exec_state()->context()->script_json(); + if (!scripts.is_array()) { + break; + } + uint32_t scripts_size = (uint32_t)scripts.array_items().size(); + if (!Section::encoding(kValueScriptSize, sizeof(uint32_t), &scripts_size)) { + break; + } + for (auto it = scripts.array_items().begin(); it != scripts.array_items().end(); it++) { + uint32_t script_item_size = (uint32_t)it->object_items().size(); + if (!Section::encoding(kValueScriptItemSize, sizeof(uint32_t), &script_item_size)) { + throw DecoderError("decoding script item size error"); + break; + } + for (auto item : it->object_items()) { + uint32_t length = static_cast<uint32_t>(item.first.length()); + uint8_t *pstr = (uint8_t *)item.first.c_str(); + if (!Section::encoding(kValueScriptKey, length, (uint8_t *)pstr)) { + throw DecoderError("decoding script item key error"); + break; + } + length = static_cast<uint32_t>(item.second.string_value().length()); + const char *pstr_val = item.second.string_value().c_str(); + if (!Section::encoding(kValueScriptValue, length, (uint8_t *)pstr_val)) { + throw DecoderError("decoding script item value error"); + break; + } + } + } + finished = true; + } while (0); + + return finished; +} + +bool SectionScript::decoding() { + bool finished = false; + do { + fStream *stream = Section::stream(); + if (!stream) { + break; + } + if (stream->Tell() < 0) { + break; + } + std::vector<json11::Json> scripts; + uint16_t target = 0; + uint32_t script_size = 0; + uint32_t size = sizeof(uint32_t); + uint32_t readbytes = stream->ReadTarget(&target, (uint8_t *)&script_size, &size); + if (!readbytes || target != kValueScriptSize) { + break; + } + for (uint32_t i = 0; i < script_size; i++) { + size = sizeof(uint32_t); + uint32_t items_size = 0; + readbytes = stream->ReadTarget(&target, (uint8_t *)&items_size, &size); + if (!readbytes || target != kValueScriptItemSize) { + throw DecoderError("decoding script items size error"); + break; + } + std::unordered_map<std::string, json11::Json> items; + for (uint32_t j = 0; j < items_size; j++) { + if ((readbytes = stream->ReadTarget(&target, NULL, NULL)) == 0) { + throw DecoderError("decoding script target error"); + break; + } + if (target != kValueScriptKey) { + throw DecoderError("decoding script target error"); + break; + } + char *pstr_key = (char *)malloc(readbytes + 1); + if (!pstr_key) { + throw DecoderError("decoding script low memory error"); + break; + } + memset(pstr_key, 0, readbytes + 1); + if (stream->Read(pstr_key, 1, readbytes) != readbytes) { + throw DecoderError("decoding script key error"); + break; + } + if ((readbytes = stream->ReadTarget(&target, NULL, NULL)) == 0) { + throw DecoderError("decoding script target error"); + break; + } + if (target != kValueScriptValue) { + throw DecoderError("decoding script target error"); + break; + } + char *pstr_value = (char *)malloc(readbytes + 1); + if (!pstr_value) { + throw DecoderError("decoding script low memory error"); + break; + } + memset(pstr_value, 0, readbytes + 1); + if (stream->Read(pstr_value, 1, readbytes) != readbytes) { + throw DecoderError("decoding script value error"); + break; + } + json11::Json json(pstr_value); + items.insert(std::make_pair(pstr_key, json)); + free(pstr_key); + free(pstr_value); + } + scripts.push_back(std::move(items)); + } + decoder()->exec_state()->context()->set_script_json(scripts); + finished = true; + + } while (0); + + return finished; +} uint32_t SectionFunction::GetInstructionsBytes(std::vector<Instruction>& instructions) { uint32_t numBits = 0; @@ -2312,7 +2460,7 @@ bool SectionStyles::decoding() { size = sizeof(uint32_t); uint32_t items_size = 0; readbytes = stream->ReadTarget(&target, (uint8_t *)&items_size, &size); - if (!readbytes || target != kValueStyleItemSize || !items_size) { + if (!readbytes || target != kValueStyleItemSize) { throw DecoderError("decoding styles items size error"); break; } diff --git a/weex_core/Source/core/data_render/exec_state_section.h b/weex_core/Source/core/data_render/exec_state_section.h index 1e1e3ceb52..2989ba1f7c 100644 --- a/weex_core/Source/core/data_render/exec_state_section.h +++ b/weex_core/Source/core/data_render/exec_state_section.h @@ -44,7 +44,8 @@ enum ExecSection { EXEC_SECTION_GLOBAL_VARIABLES, EXEC_SECTION_STYLES, EXEC_SECTION_VALUEREF, - EXEC_SECTION_CLASS + EXEC_SECTION_CLASS, + EXEC_SECTION_SCRIPT }; class fStream; @@ -125,6 +126,22 @@ class SectionData : public Section { virtual bool decoding(); virtual uint32_t size(); }; + +class SectionScript : public Section { +public: + enum SectionKey { + kValueScriptSize, + kValueScriptKey, + kValueScriptValue, + kValueScriptItemSize, + }; + SectionScript(ExecStateEncoder *encoder) : Section(encoder) {} + SectionScript(ExecStateDecoder *decoder, uint32_t length) : Section(decoder, length) {} + virtual ~SectionScript() {}; + virtual bool encoding(); + virtual bool decoding(); + virtual uint32_t size(); +}; class SectionFunction : public Section { public: diff --git a/weex_core/Source/core/data_render/vnode/vnode_exec_env.cc b/weex_core/Source/core/data_render/vnode/vnode_exec_env.cc index 68a5a3d9a2..8eb42f0059 100644 --- a/weex_core/Source/core/data_render/vnode/vnode_exec_env.cc +++ b/weex_core/Source/core/data_render/vnode/vnode_exec_env.cc @@ -740,7 +740,13 @@ void VNodeExecEnv::ParseStyle(ExecState *state) { } } - + +void VNodeExecEnv::ParseScript(ExecState *state) { + json11::Json& json = state->context()->raw_json(); + const json11::Json& script_array = json["script"]; + state->context()->set_script_json(script_array); +} + Value StringToValue(ExecState *exec_state,const std::string &str) { Value ret; do { diff --git a/weex_core/Source/core/data_render/vnode/vnode_exec_env.h b/weex_core/Source/core/data_render/vnode/vnode_exec_env.h index aad432ece9..daa7c3fbae 100644 --- a/weex_core/Source/core/data_render/vnode/vnode_exec_env.h +++ b/weex_core/Source/core/data_render/vnode/vnode_exec_env.h @@ -32,6 +32,7 @@ class VNodeExecEnv { static void ImportExecData(ExecState *state, const std::string &init_data_str); static void ParseStyle(ExecState *state); static void ParseData(ExecState *state); + static void ParseScript(ExecState *state); }; Value StringToValue(ExecState *exec_state, const std::string &str); diff --git a/weex_core/Source/core/data_render/vnode/vnode_render_context.h b/weex_core/Source/core/data_render/vnode/vnode_render_context.h index 45769fbe88..b0a760a37a 100644 --- a/weex_core/Source/core/data_render/vnode/vnode_render_context.h +++ b/weex_core/Source/core/data_render/vnode/vnode_render_context.h @@ -46,6 +46,12 @@ class VNodeRenderContext { inline std::map<std::string, json11::Json>& style_json() { return style_json_; } + inline const json11::Json& script_json() { + return script_json_; + } + inline void set_script_json(const json11::Json& script_json) { + script_json_ = script_json; + } private: // node context @@ -56,6 +62,7 @@ class VNodeRenderContext { // script to execute std::string script_; std::map<std::string, json11::Json> style_json_; + json11::Json script_json_; }; } // namespace data_render } // namespace core diff --git a/weex_core/Source/core/data_render/vnode/vnode_render_manager.cc b/weex_core/Source/core/data_render/vnode/vnode_render_manager.cc index 2d57415156..3dad81b2af 100644 --- a/weex_core/Source/core/data_render/vnode/vnode_render_manager.cc +++ b/weex_core/Source/core/data_render/vnode/vnode_render_manager.cc @@ -189,6 +189,7 @@ std::string VNodeRenderManager::CreatePageWithContent(const std::string &input, exec_state->context()->raw_json() = json; VNodeExecEnv::ParseData(exec_state); VNodeExecEnv::ParseStyle(exec_state); + VNodeExecEnv::ParseScript(exec_state); } if (init_data.length() > 0) { VNodeExecEnv::ImportExecData(exec_state, init_data); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services