From cac70652bfffc185b2bbcf6c3e40272847832fc6 Mon Sep 17 00:00:00 2001 From: Nathanael Anderson Date: Tue, 20 Oct 2020 01:22:56 -0500 Subject: [PATCH 1/2] feat: Increate speed of loading JS files --- NativeScript/runtime/Helpers.h | 1 + NativeScript/runtime/Helpers.mm | 30 ++++++++++++++++++++++++++ NativeScript/runtime/ModuleInternal.mm | 7 +----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/NativeScript/runtime/Helpers.h b/NativeScript/runtime/Helpers.h index ba55d2ce..e840943a 100644 --- a/NativeScript/runtime/Helpers.h +++ b/NativeScript/runtime/Helpers.h @@ -23,6 +23,7 @@ bool ToBool(const v8::Local& value); std::vector ToVector(const std::string& value); bool Exists(const char* fullPath); +v8::Local ReadModule(v8::Isolate* isolate, const std::string &filePath); const char* ReadText(const std::string& filePath, long& length, bool& isNew); std::string ReadText(const std::string& file); uint8_t* ReadBinary(const std::string path, long& length, bool& isNew); diff --git a/NativeScript/runtime/Helpers.mm b/NativeScript/runtime/Helpers.mm index 68f61a48..0901efb0 100644 --- a/NativeScript/runtime/Helpers.mm +++ b/NativeScript/runtime/Helpers.mm @@ -113,6 +113,36 @@ return false; } +Local tns::ReadModule(Isolate* isolate, const std::string &filePath) { + struct stat finfo; + + int file = open(filePath.c_str(), O_RDONLY); + if (file < 0) { + tns::Assert(false); + } + + fstat(file, &finfo); + long length = finfo.st_size; + + char* newBuffer = new char[length + 128]; + strcpy(newBuffer, "(function(module, exports, require, __filename, __dirname) { "); // 61 Characters + read(file, &newBuffer[61], length); + close(file); + length += 62; + + // Add the closing "\n})" + newBuffer[length] = 10; + ++length; + newBuffer[length] = '}'; + ++length; + newBuffer[length] = ')'; + + Local str = v8::String::NewFromUtf8(isolate, newBuffer, NewStringType::kNormal, (int)length).ToLocalChecked(); + delete[] newBuffer; + + return str; +} + const char* tns::ReadText(const std::string& filePath, long& length, bool& isNew) { FILE* file = fopen(filePath.c_str(), "rb"); if (file == nullptr) { diff --git a/NativeScript/runtime/ModuleInternal.mm b/NativeScript/runtime/ModuleInternal.mm index 67924fb2..7eb00d2c 100644 --- a/NativeScript/runtime/ModuleInternal.mm +++ b/NativeScript/runtime/ModuleInternal.mm @@ -283,12 +283,7 @@ } Local ModuleInternal::WrapModuleContent(Isolate* isolate, const std::string& path) { - std::string content = tns::ReadText(path); - std::string result("(function(module, exports, require, __filename, __dirname) { "); - result.reserve(content.length() + 1024); - result += content; - result += "\n})"; - return tns::ToV8String(isolate, result); + return tns::ReadModule(isolate, path); } std::string ModuleInternal::ResolvePath(Isolate* isolate, const std::string& baseDir, const std::string& moduleName) { From 7108c4ba1ebb13d90e1f967954185edc44d59f35 Mon Sep 17 00:00:00 2001 From: Nathanael Anderson Date: Tue, 20 Oct 2020 02:51:26 -0500 Subject: [PATCH 2/2] Fix off-by one error --- NativeScript/runtime/Helpers.mm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NativeScript/runtime/Helpers.mm b/NativeScript/runtime/Helpers.mm index 0901efb0..f6556264 100644 --- a/NativeScript/runtime/Helpers.mm +++ b/NativeScript/runtime/Helpers.mm @@ -128,7 +128,7 @@ strcpy(newBuffer, "(function(module, exports, require, __filename, __dirname) { "); // 61 Characters read(file, &newBuffer[61], length); close(file); - length += 62; + length += 61; // Add the closing "\n})" newBuffer[length] = 10; @@ -136,6 +136,8 @@ newBuffer[length] = '}'; ++length; newBuffer[length] = ')'; + ++length; + newBuffer[length] = 0; Local str = v8::String::NewFromUtf8(isolate, newBuffer, NewStringType::kNormal, (int)length).ToLocalChecked(); delete[] newBuffer;