| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5bfa43d commit c8d2ca7
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,17 +5,35 @@ const fs = require('fs'); | |||
| 5 | 5 | ||
| 6 | 6 | const bench = common.createBenchmark(main, { | |
| 7 | 7 | n: [1e4], | |
| 8 | - kind: ['lstatSync', 'statSync'] | ||
| 8 | + kind: ['fstatSync', 'lstatSync', 'statSync'] | ||
| 9 | 9 | }); | |
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | 12 | function main(conf) { | |
| 13 | 13 | const n = conf.n >>> 0; | |
| 14 | - const fn = fs[conf.kind]; | ||
| 15 | - | ||
| 16 | - bench.start(); | ||
| 17 | - for (var i = 0; i < n; i++) { | ||
| 18 | - fn(__filename); | ||
| 14 | + var fn; | ||
| 15 | + var i; | ||
| 16 | + switch (conf.kind) { | ||
| 17 | + case 'statSync': | ||
| 18 | + case 'lstatSync': | ||
| 19 | + fn = fs[conf.kind]; | ||
| 20 | + bench.start(); | ||
| 21 | + for (i = 0; i < n; i++) { | ||
| 22 | + fn(__filename); | ||
| 23 | + } | ||
| 24 | + bench.end(n); | ||
| 25 | + break; | ||
| 26 | + case 'fstatSync': | ||
| 27 | + fn = fs.fstatSync; | ||
| 28 | + const fd = fs.openSync(__filename, 'r'); | ||
| 29 | + bench.start(); | ||
| 30 | + for (i = 0; i < n; i++) { | ||
| 31 | + fn(fd); | ||
| 32 | + } | ||
| 33 | + bench.end(n); | ||
| 34 | + fs.closeSync(fd); | ||
| 35 | + break; | ||
| 36 | + default: | ||
| 37 | + throw new Error('Invalid kind argument'); | ||
| 19 | 38 | } | |
| 20 | - bench.end(n); | ||
| 21 | 39 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -147,7 +147,7 @@ function isFd(path) { | |||
| 147 | 147 | } | |
| 148 | 148 | ||
| 149 | 149 | // Static method to set the stats properties on a Stats object. | |
| 150 | - fs.Stats = function( | ||
| 150 | + function Stats( | ||
| 151 | 151 | dev, | |
| 152 | 152 | mode, | |
| 153 | 153 | nlink, | |
@@ -176,7 +176,8 @@ fs.Stats = function( | |||
| 176 | 176 | this.mtime = new Date(mtim_msec); | |
| 177 | 177 | this.ctime = new Date(ctim_msec); | |
| 178 | 178 | this.birthtime = new Date(birthtim_msec); | |
| 179 | - }; | ||
| 179 | + } | ||
| 180 | + fs.Stats = Stats; | ||
| 180 | 181 | ||
| 181 | 182 | // Create a C++ binding to the function which creates a Stats object. | |
| 182 | 183 | binding.FSInitialize(fs.Stats); | |
@@ -261,7 +262,7 @@ fs.exists = function(path, callback) { | |||
| 261 | 262 | fs.existsSync = function(path) { | |
| 262 | 263 | try { | |
| 263 | 264 | nullCheck(path); | |
| 264 | - binding.stat(pathModule._makeLong(path)); | ||
| 265 | + binding.stat(pathModule._makeLong(path), statValues); | ||
| 265 | 266 | return true; | |
| 266 | 267 | } catch (e) { | |
| 267 | 268 | return false; | |
@@ -973,18 +974,31 @@ fs.stat = function(path, callback) { | |||
| 973 | 974 | binding.stat(pathModule._makeLong(path), req); | |
| 974 | 975 | }; | |
| 975 | 976 | ||
| 977 | + const statValues = new Float64Array(14); | ||
| 978 | + function statsFromValues() { | ||
| 979 | + return new Stats(statValues[0], statValues[1], statValues[2], statValues[3], | ||
| 980 | + statValues[4], statValues[5], | ||
| 981 | + statValues[6] < 0 ? undefined : statValues[6], statValues[7], | ||
| 982 | + statValues[8], statValues[9] < 0 ? undefined : statValues[9], | ||
| 983 | + statValues[10], statValues[11], statValues[12], | ||
| 984 | + statValues[13]); | ||
| 985 | + } | ||
| 986 | + | ||
| 976 | 987 | fs.fstatSync = function(fd) { | |
| 977 | - return binding.fstat(fd); | ||
| 988 | + binding.fstat(fd, statValues); | ||
| 989 | + return statsFromValues(); | ||
| 978 | 990 | }; | |
| 979 | 991 | ||
| 980 | 992 | fs.lstatSync = function(path) { | |
| 981 | 993 | nullCheck(path); | |
| 982 | - return binding.lstat(pathModule._makeLong(path)); | ||
| 994 | + binding.lstat(pathModule._makeLong(path), statValues); | ||
| 995 | + return statsFromValues(); | ||
| 983 | 996 | }; | |
| 984 | 997 | ||
| 985 | 998 | fs.statSync = function(path) { | |
| 986 | 999 | nullCheck(path); | |
| 987 | - return binding.stat(pathModule._makeLong(path)); | ||
| 1000 | + binding.stat(pathModule._makeLong(path), statValues); | ||
| 1001 | + return statsFromValues(); | ||
| 988 | 1002 | }; | |
| 989 | 1003 | ||
| 990 | 1004 | fs.readlink = function(path, options, callback) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,8 +27,10 @@ | |||
| 27 | 27 | namespace node { | |
| 28 | 28 | ||
| 29 | 29 | using v8::Array; | |
| 30 | + using v8::ArrayBuffer; | ||
| 30 | 31 | using v8::Context; | |
| 31 | 32 | using v8::EscapableHandleScope; | |
| 33 | + using v8::Float64Array; | ||
| 32 | 34 | using v8::Function; | |
| 33 | 35 | using v8::FunctionCallbackInfo; | |
| 34 | 36 | using v8::FunctionTemplate; | |
@@ -528,6 +530,37 @@ Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) { | |||
| 528 | 530 | return handle_scope.Escape(stats); | |
| 529 | 531 | } | |
| 530 | 532 | ||
| 533 | + void FillStatsArray(double* fields, const uv_stat_t* s) { | ||
| 534 | + fields[0] = s->st_dev; | ||
| 535 | + fields[1] = s->st_mode; | ||
| 536 | + fields[2] = s->st_nlink; | ||
| 537 | + fields[3] = s->st_uid; | ||
| 538 | + fields[4] = s->st_gid; | ||
| 539 | + fields[5] = s->st_rdev; | ||
| 540 | + #if defined(__POSIX__) | ||
| 541 | + fields[6] = s->st_blksize; | ||
| 542 | + #else | ||
| 543 | + fields[6] = -1; | ||
| 544 | + #endif | ||
| 545 | + fields[7] = s->st_ino; | ||
| 546 | + fields[8] = s->st_size; | ||
| 547 | + #if defined(__POSIX__) | ||
| 548 | + fields[9] = s->st_blocks; | ||
| 549 | + #else | ||
| 550 | + fields[9] = -1; | ||
| 551 | + #endif | ||
| 552 | + // Dates. | ||
| 553 | + #define X(idx, name) \ | ||
| 554 | + fields[idx] = (static_cast<double>(s->st_##name.tv_sec) * 1000) + \ | ||
| 555 | + (static_cast<double>(s->st_##name.tv_nsec / 1000000)); \ | ||
| 556 | + | ||
| 557 | + X(10, atim) | ||
| 558 | + X(11, mtim) | ||
| 559 | + X(12, ctim) | ||
| 560 | + X(13, birthtim) | ||
| 561 | + #undef X | ||
| 562 | + } | ||
| 563 | + | ||
| 531 | 564 | // Used to speed up module loading. Returns the contents of the file as | |
| 532 | 565 | // a string or undefined when the file cannot be opened. The speedup | |
| 533 | 566 | // comes from not creating Error objects on failure. | |
@@ -618,12 +651,15 @@ static void Stat(const FunctionCallbackInfo<Value>& args) { | |||
| 618 | 651 | BufferValue path(env->isolate(), args[0]); | |
| 619 | 652 | ASSERT_PATH(path) | |
| 620 | 653 | ||
| 621 | - if (args[1]->IsObject()) { | ||
| 622 | - ASYNC_CALL(stat, args[1], UTF8, *path) | ||
| 623 | - } else { | ||
| 654 | + if (args[1]->IsFloat64Array()) { | ||
| 655 | + Local<Float64Array> array = args[1].As<Float64Array>(); | ||
| 656 | + CHECK_EQ(array->Length(), 14); | ||
| 657 | + Local<ArrayBuffer> ab = array->Buffer(); | ||
| 658 | + double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 624 | 659 | SYNC_CALL(stat, *path, *path) | |
| 625 | - args.GetReturnValue().Set( | ||
| 626 | - BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); | ||
| 660 | + FillStatsArray(fields, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)); | ||
| 661 | + } else if (args[1]->IsObject()) { | ||
| 662 | + ASYNC_CALL(stat, args[1], UTF8, *path) | ||
| 627 | 663 | } | |
| 628 | 664 | } | |
| 629 | 665 | ||
@@ -636,12 +672,15 @@ static void LStat(const FunctionCallbackInfo<Value>& args) { | |||
| 636 | 672 | BufferValue path(env->isolate(), args[0]); | |
| 637 | 673 | ASSERT_PATH(path) | |
| 638 | 674 | ||
| 639 | - if (args[1]->IsObject()) { | ||
| 640 | - ASYNC_CALL(lstat, args[1], UTF8, *path) | ||
| 641 | - } else { | ||
| 675 | + if (args[1]->IsFloat64Array()) { | ||
| 676 | + Local<Float64Array> array = args[1].As<Float64Array>(); | ||
| 677 | + CHECK_EQ(array->Length(), 14); | ||
| 678 | + Local<ArrayBuffer> ab = array->Buffer(); | ||
| 679 | + double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 642 | 680 | SYNC_CALL(lstat, *path, *path) | |
| 643 | - args.GetReturnValue().Set( | ||
| 644 | - BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); | ||
| 681 | + FillStatsArray(fields, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)); | ||
| 682 | + } else if (args[1]->IsObject()) { | ||
| 683 | + ASYNC_CALL(lstat, args[1], UTF8, *path) | ||
| 645 | 684 | } | |
| 646 | 685 | } | |
| 647 | 686 | ||
@@ -655,12 +694,15 @@ static void FStat(const FunctionCallbackInfo<Value>& args) { | |||
| 655 | 694 | ||
| 656 | 695 | int fd = args[0]->Int32Value(); | |
| 657 | 696 | ||
| 658 | - if (args[1]->IsObject()) { | ||
| 659 | - ASYNC_CALL(fstat, args[1], UTF8, fd) | ||
| 660 | - } else { | ||
| 697 | + if (args[1]->IsFloat64Array()) { | ||
| 698 | + Local<Float64Array> array = args[1].As<Float64Array>(); | ||
| 699 | + CHECK_EQ(array->Length(), 14); | ||
| 700 | + Local<ArrayBuffer> ab = array->Buffer(); | ||
| 701 | + double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 661 | 702 | SYNC_CALL(fstat, 0, fd) | |
| 662 | - args.GetReturnValue().Set( | ||
| 663 | - BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); | ||
| 703 | + FillStatsArray(fields, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)); | ||
| 704 | + } else if (args[1]->IsObject()) { | ||
| 705 | + ASYNC_CALL(fstat, args[1], UTF8, fd) | ||
| 664 | 706 | } | |
| 665 | 707 | } | |
| 666 | 708 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments