Skip to content

Commit 25628ec

Browse files
author
Nathanael Anderson
authored
feat: Faster loading of JS files (#73)
* feat: Increase speed of loading JS files
1 parent 1237ad1 commit 25628ec

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

NativeScript/runtime/Helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ bool ToBool(const v8::Local<v8::Value>& value);
2323
std::vector<uint16_t> ToVector(const std::string& value);
2424

2525
bool Exists(const char* fullPath);
26+
v8::Local<v8::String> ReadModule(v8::Isolate* isolate, const std::string &filePath);
2627
const char* ReadText(const std::string& filePath, long& length, bool& isNew);
2728
std::string ReadText(const std::string& file);
2829
uint8_t* ReadBinary(const std::string path, long& length, bool& isNew);

NativeScript/runtime/Helpers.mm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@
113113
return false;
114114
}
115115

116+
Local<v8::String> tns::ReadModule(Isolate* isolate, const std::string &filePath) {
117+
struct stat finfo;
118+
119+
int file = open(filePath.c_str(), O_RDONLY);
120+
if (file < 0) {
121+
tns::Assert(false);
122+
}
123+
124+
fstat(file, &finfo);
125+
long length = finfo.st_size;
126+
127+
char* newBuffer = new char[length + 128];
128+
strcpy(newBuffer, "(function(module, exports, require, __filename, __dirname) { "); // 61 Characters
129+
read(file, &newBuffer[61], length);
130+
close(file);
131+
length += 61;
132+
133+
// Add the closing "\n})"
134+
newBuffer[length] = 10;
135+
++length;
136+
newBuffer[length] = '}';
137+
++length;
138+
newBuffer[length] = ')';
139+
++length;
140+
newBuffer[length] = 0;
141+
142+
Local<v8::String> str = v8::String::NewFromUtf8(isolate, newBuffer, NewStringType::kNormal, (int)length).ToLocalChecked();
143+
delete[] newBuffer;
144+
145+
return str;
146+
}
147+
116148
const char* tns::ReadText(const std::string& filePath, long& length, bool& isNew) {
117149
FILE* file = fopen(filePath.c_str(), "rb");
118150
if (file == nullptr) {

NativeScript/runtime/ModuleInternal.mm

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,7 @@
283283
}
284284

285285
Local<v8::String> ModuleInternal::WrapModuleContent(Isolate* isolate, const std::string& path) {
286-
std::string content = tns::ReadText(path);
287-
std::string result("(function(module, exports, require, __filename, __dirname) { ");
288-
result.reserve(content.length() + 1024);
289-
result += content;
290-
result += "\n})";
291-
return tns::ToV8String(isolate, result);
286+
return tns::ReadModule(isolate, path);
292287
}
293288

294289
std::string ModuleInternal::ResolvePath(Isolate* isolate, const std::string& baseDir, const std::string& moduleName) {

0 commit comments

Comments
 (0)