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..f6556264 100644 --- a/NativeScript/runtime/Helpers.mm +++ b/NativeScript/runtime/Helpers.mm @@ -113,6 +113,38 @@ 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 += 61; + + // Add the closing "\n})" + newBuffer[length] = 10; + ++length; + newBuffer[length] = '}'; + ++length; + newBuffer[length] = ')'; + ++length; + newBuffer[length] = 0; + + 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) {