Skip to content

Commit b563e9e

Browse files
committed
src: fix internalModuleStat v8 fast path
1 parent c1b15a4 commit b563e9e

File tree

8 files changed

+37
-25
lines changed

8 files changed

+37
-25
lines changed

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ function handleDirents({ result, currentPath, context }) {
14241424
const dirent = getDirent(currentPath, names[i], types[i]);
14251425
ArrayPrototypePush(context.readdirResults, dirent);
14261426

1427-
if (dirent.isDirectory() || binding.internalModuleStat(binding, fullPath) === 1) {
1427+
if (dirent.isDirectory() || binding.internalModuleStat(fullPath) === 1) {
14281428
ArrayPrototypePush(context.pathsQueue, fullPath);
14291429
}
14301430
}
@@ -1434,7 +1434,7 @@ function handleFilePaths({ result, currentPath, context }) {
14341434
for (let i = 0; i < result.length; i++) {
14351435
const resultPath = pathModule.join(currentPath, result[i]);
14361436
const relativeResultPath = pathModule.relative(context.basePath, resultPath);
1437-
const stat = binding.internalModuleStat(binding, resultPath);
1437+
const stat = binding.internalModuleStat(resultPath);
14381438
ArrayPrototypePush(context.readdirResults, relativeResultPath);
14391439

14401440
if (stat === 1) {

lib/internal/fs/promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ async function readdirRecursive(originalPath, options) {
910910
const { 0: path, 1: readdir } = ArrayPrototypePop(queue);
911911
for (const ent of readdir) {
912912
const direntPath = pathModule.join(path, ent);
913-
const stat = binding.internalModuleStat(binding, direntPath);
913+
const stat = binding.internalModuleStat(direntPath);
914914
ArrayPrototypePush(
915915
result,
916916
pathModule.relative(originalPath, direntPath),

lib/internal/modules/cjs/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ function stat(filename) {
255255
const result = statCache.get(filename);
256256
if (result !== undefined) { return result; }
257257
}
258-
const result = internalFsBinding.internalModuleStat(internalFsBinding, filename);
258+
const result = internalFsBinding.internalModuleStat(filename);
259259
if (statCache !== null && result >= 0) {
260-
// Only set cache when `internalModuleStat(internalFsBinding, filename)` succeeds.
260+
// Only set cache when `internalModuleStat(filename)` succeeds.
261261
statCache.set(filename, result);
262262
}
263263
return result;

lib/internal/modules/esm/resolve.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
248248
}
249249

250250
const stats = internalFsBinding.internalModuleStat(
251-
internalFsBinding,
252251
StringPrototypeEndsWith(internalFsBinding, path, '/') ? StringPrototypeSlice(path, -1) : path,
253252
);
254253

lib/internal/modules/package_json_reader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ function getPackageJSONURL(specifier, base) {
235235
let lastPath;
236236
do {
237237
const stat = internalFsBinding.internalModuleStat(
238-
internalFsBinding,
239238
StringPrototypeSlice(packageJSONPath, 0, packageJSONPath.length - 13),
240239
);
241240
// Check for !stat.isDirectory()

src/node_external_reference.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ using CFunctionCallback = void (*)(v8::Local<v8::Value> unused,
2727
using CFunctionCallbackReturnDouble =
2828
double (*)(v8::Local<v8::Object> unused, v8::Local<v8::Object> receiver);
2929
using CFunctionCallbackReturnInt32 =
30-
int32_t (*)(v8::Local<v8::Object> unused,
31-
v8::Local<v8::Object> receiver,
32-
const v8::FastOneByteString& input,
30+
int32_t (*)(v8::Local<v8::Value> receiver,
31+
v8::Local<v8::Value> input,
3332
// NOLINTNEXTLINE(runtime/references) This is V8 api.
3433
v8::FastApiCallbackOptions& options);
3534
using CFunctionCallbackValueReturnDouble =

src/node_file.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "aliased_buffer-inl.h"
2424
#include "memory_tracker-inl.h"
2525
#include "node_buffer.h"
26+
#include "node_debug.h"
2627
#include "node_errors.h"
2728
#include "node_external_reference.h"
2829
#include "node_file-inl.h"
@@ -1056,9 +1057,9 @@ static void ExistsSync(const FunctionCallbackInfo<Value>& args) {
10561057
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
10571058
Environment* env = Environment::GetCurrent(args);
10581059

1059-
CHECK_GE(args.Length(), 2);
1060-
CHECK(args[1]->IsString());
1061-
BufferValue path(env->isolate(), args[1]);
1060+
CHECK_EQ(args.Length(), 1);
1061+
CHECK(args[0]->IsString());
1062+
BufferValue path(env->isolate(), args[0]);
10621063
CHECK_NOT_NULL(*path);
10631064
ToNamespacedPath(env, &path);
10641065

@@ -1074,15 +1075,17 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
10741075
}
10751076

10761077
static int32_t FastInternalModuleStat(
1077-
Local<Object> unused,
1078-
Local<Object> recv,
1079-
const FastOneByteString& input,
1078+
Local<Value> recv,
1079+
Local<Value> input_,
10801080
// NOLINTNEXTLINE(runtime/references) This is V8 api.
10811081
FastApiCallbackOptions& options) {
1082-
Environment* env = Environment::GetCurrent(options.isolate);
1083-
HandleScope scope(env->isolate());
1082+
TRACK_V8_FAST_API_CALL("fs.internalModuleStat");
1083+
HandleScope scope(options.isolate);
1084+
1085+
CHECK(input_->IsString());
1086+
Utf8Value input(options.isolate, input_.As<String>());
10841087

1085-
auto path = std::filesystem::path(input.data, input.data + input.length);
1088+
auto path = std::filesystem::path(input.ToStringView());
10861089

10871090
switch (std::filesystem::status(path).type()) {
10881091
case std::filesystem::file_type::directory:

test/parallel/test-permission-fs-internal-module-stat.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// Flags: --expose-internals --permission --allow-fs-read=test/common* --allow-fs-read=tools* --allow-fs-read=test/parallel* --allow-child-process
1+
// Flags: --expose-internals --permission --allow-fs-read=test/common* --allow-fs-read=tools* --allow-fs-read=test/parallel* --allow-child-process --allow-natives-syntax
22
'use strict';
33

44
const common = require('../common');
55
const { isMainThread } = require('worker_threads');
6+
const { strictEqual } = require('assert');
67

78
if (!isMainThread) {
89
common.skip('This test only works on a main thread');
@@ -18,9 +19,20 @@ const fixtures = require('../common/fixtures');
1819
const blockedFile = fixtures.path('permission', 'deny', 'protected-file.md');
1920
const internalFsBinding = internalBinding('fs');
2021

21-
// Run this inside a for loop to trigger the fast API
22-
for (let i = 0; i < 10_000; i++) {
23-
// internalModuleStat does not use permission model.
24-
// doesNotThrow
25-
internalFsBinding.internalModuleStat(internalFsBinding, blockedFile);
22+
strictEqual(internalFsBinding.internalModuleStat(blockedFile), 0);
23+
24+
// Only javascript methods can be optimized through %OptimizeFunctionOnNextCall
25+
// This is why we surround the C++ method we want to optimize with a JS function.
26+
function testFastPaths(file) {
27+
return internalFsBinding.internalModuleStat(file);
28+
}
29+
30+
eval('%PrepareFunctionForOptimization(testFastPaths)');
31+
testFastPaths(blockedFile);
32+
eval('%OptimizeFunctionOnNextCall(testFastPaths)');
33+
strictEqual(testFastPaths(blockedFile), 0);
34+
35+
if (common.isDebug) {
36+
const { getV8FastApiCallCount } = internalBinding('debug');
37+
strictEqual(getV8FastApiCallCount('fs.internalModuleStat'), 1);
2638
}

0 commit comments

Comments
 (0)