Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NativeScript/runtime/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bool ToBool(const v8::Local<v8::Value>& value);
std::vector<uint16_t> ToVector(const std::string& value);

bool Exists(const char* fullPath);
v8::Local<v8::String> 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);
Expand Down
32 changes: 32 additions & 0 deletions NativeScript/runtime/Helpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@
return false;
}

Local<v8::String> 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<v8::String> 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) {
Expand Down
7 changes: 1 addition & 6 deletions NativeScript/runtime/ModuleInternal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,7 @@
}

Local<v8::String> 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) {
Expand Down