| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 99161b0 commit ff8313c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -205,33 +205,17 @@ MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource(Isolate* isolate, | |||
| 205 | 205 | #ifdef NODE_BUILTIN_MODULES_PATH | |
| 206 | 206 | std::string filename = OnDiskFileName(id); | |
| 207 | 207 | ||
| 208 | - uv_fs_t req; | ||
| 209 | - uv_file file = | ||
| 210 | - uv_fs_open(nullptr, &req, filename.c_str(), O_RDONLY, 0, nullptr); | ||
| 211 | - CHECK_GE(req.result, 0); | ||
| 212 | - uv_fs_req_cleanup(&req); | ||
| 213 | - | ||
| 214 | - auto defer_close = OnScopeLeave([file]() { | ||
| 215 | - uv_fs_t close_req; | ||
| 216 | - CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); | ||
| 217 | - uv_fs_req_cleanup(&close_req); | ||
| 218 | - }); | ||
| 219 | - | ||
| 220 | 208 | std::string contents; | |
| 221 | - char buffer[4096]; | ||
| 222 | - uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer)); | ||
| 223 | - | ||
| 224 | - while (true) { | ||
| 225 | - const int r = | ||
| 226 | - uv_fs_read(nullptr, &req, file, &buf, 1, contents.length(), nullptr); | ||
| 227 | - CHECK_GE(req.result, 0); | ||
| 228 | - uv_fs_req_cleanup(&req); | ||
| 229 | - if (r <= 0) { | ||
| 230 | - break; | ||
| 231 | - } | ||
| 232 | - contents.append(buf.base, r); | ||
| 209 | + int r = ReadFileSync(&contents, filename.c_str()); | ||
| 210 | + if (r != 0) { | ||
| 211 | + const std::string buf = SPrintF("Cannot read local builtin. %s: %s \"%s\"", | ||
| 212 | + uv_err_name(r), | ||
| 213 | + uv_strerror(r), | ||
| 214 | + filename); | ||
| 215 | + Local<String> message = OneByteString(isolate, buf.c_str()); | ||
| 216 | + isolate->ThrowException(v8::Exception::Error(message)); | ||
| 217 | + return MaybeLocal<String>(); | ||
| 233 | 218 | } | |
| 234 | - | ||
| 235 | 219 | return String::NewFromUtf8( | |
| 236 | 220 | isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length()); | |
| 237 | 221 | #else | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -221,6 +221,40 @@ int WriteFileSync(v8::Isolate* isolate, | |||
| 221 | 221 | return WriteFileSync(path, buf); | |
| 222 | 222 | } | |
| 223 | 223 | ||
| 224 | + int ReadFileSync(std::string* result, const char* path) { | ||
| 225 | + uv_fs_t req; | ||
| 226 | + uv_file file = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr); | ||
| 227 | + if (req.result < 0) { | ||
| 228 | + return req.result; | ||
| 229 | + } | ||
| 230 | + uv_fs_req_cleanup(&req); | ||
| 231 | + | ||
| 232 | + auto defer_close = OnScopeLeave([file]() { | ||
| 233 | + uv_fs_t close_req; | ||
| 234 | + CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); | ||
| 235 | + uv_fs_req_cleanup(&close_req); | ||
| 236 | + }); | ||
| 237 | + | ||
| 238 | + *result = std::string(""); | ||
| 239 | + char buffer[4096]; | ||
| 240 | + uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer)); | ||
| 241 | + | ||
| 242 | + while (true) { | ||
| 243 | + const int r = | ||
| 244 | + uv_fs_read(nullptr, &req, file, &buf, 1, result->length(), nullptr); | ||
| 245 | + if (req.result < 0) { | ||
| 246 | + uv_fs_req_cleanup(&req); | ||
| 247 | + return req.result; | ||
| 248 | + } | ||
| 249 | + uv_fs_req_cleanup(&req); | ||
| 250 | + if (r <= 0) { | ||
| 251 | + break; | ||
| 252 | + } | ||
| 253 | + result->append(buf.base, r); | ||
| 254 | + } | ||
| 255 | + return 0; | ||
| 256 | + } | ||
| 257 | + | ||
| 224 | 258 | void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) { | |
| 225 | 259 | #ifdef _WIN32 | |
| 226 | 260 | GetLocalTime(tm_struct); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -813,6 +813,10 @@ std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr) { | |||
| 813 | 813 | } | |
| 814 | 814 | ||
| 815 | 815 | #define MAYBE_FIELD_PTR(ptr, field) ptr == nullptr ? nullptr : &(ptr->field) | |
| 816 | + | ||
| 817 | + // Returns a non-zero code if it fails to open or read the file, | ||
| 818 | + // aborts if it fails to close the file. | ||
| 819 | + int ReadFileSync(std::string* result, const char* path); | ||
| 816 | 820 | } // namespace node | |
| 817 | 821 | ||
| 818 | 822 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| Back | FazBrowse Home | New Git URL |
0 commit comments