FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test: add permission fast api test · nodejs/node@4c995fc · GitHub

/ node Public

Commit 4c995fc

Browse files
committed
test: add permission fast api test
Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 7dd8d04 commit 4c995fc

4 files changed

Lines changed: 101 additions & 6 deletions

File tree

‎src/permission/permission.cc‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ static void Has(const FunctionCallbackInfo<Value>& args) {
127127

128128
static bool FastHas(Local<Value> receiver,
129129
Local<Value> scope_arg,
130-
Local<Value> resource_arg,
131130
// NOLINTNEXTLINE(runtime/references) This is V8 api.
132131
FastApiCallbackOptions& options) {
133132
TRACK_V8_FAST_API_CALL("permission.has");
@@ -148,8 +147,30 @@ static bool FastHas(Local<Value> receiver,
148147
return false;
149148
}
150149

151-
if (resource_arg->IsUndefined()) {
152-
return env->permission()->is_granted(env, scope);
150+
return env->permission()->is_granted(env, scope);
151+
}
152+
153+
static bool FastHasResource(Local<Value> receiver,
154+
Local<Value> scope_arg,
155+
Local<Value> resource_arg,
156+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
157+
FastApiCallbackOptions& options) {
158+
TRACK_V8_FAST_API_CALL("permission.has");
159+
auto isolate = options.isolate;
160+
v8::HandleScope handle_scope(isolate);
161+
auto context = isolate->GetCurrentContext();
162+
163+
Environment* env = Environment::GetCurrent(context);
164+
165+
Local<String> str;
166+
if (!scope_arg->ToString(context).ToLocal(&str)) {
167+
return false;
168+
}
169+
Utf8Value utf8_scope(isolate, str);
170+
PermissionScope scope =
171+
Permission::StringToPermission(utf8_scope.ToStringView());
172+
if (scope == PermissionScope::kPermissionsRoot) {
173+
return false;
153174
}
154175

155176
Local<String> res_str;
@@ -164,7 +185,8 @@ static bool FastHas(Local<Value> receiver,
164185
return env->permission()->is_granted(env, scope, utf8_res.ToStringView());
165186
}
166187

167-
static CFunction fast_has_(CFunction::Make(FastHas));
188+
static CFunction fast_has_methods_[] = {CFunction::Make(FastHas),
189+
CFunction::Make(FastHasResource)};
168190

169191
} // namespace
170192

@@ -370,15 +392,18 @@ void Initialize(Local<Object> target,
370392
Local<Value> unused,
371393
Local<Context> context,
372394
void* priv) {
373-
SetFastMethodNoSideEffect(context, target, "has", Has, &fast_has_);
395+
SetFastMethodNoSideEffect(
396+
context, target, "has", Has, {fast_has_methods_, 2});
374397
SetMethod(context, target, "drop", Drop);
375398

376399
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
377400
}
378401

379402
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
380403
registry->Register(Has);
381-
registry->Register(fast_has_);
404+
for (const CFunction& method : fast_has_methods_) {
405+
registry->Register(method);
406+
}
382407
registry->Register(Drop);
383408
}
384409

‎src/util.cc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,32 @@ void SetFastMethodNoSideEffect(
468468
that->Set(name_string, t);
469469
}
470470

471+
void SetFastMethodNoSideEffect(
472+
Local<v8::Context> context,
473+
Local<v8::Object> that,
474+
const std::string_view name,
475+
v8::FunctionCallback slow_callback,
476+
const v8::MemorySpan<const v8::CFunction>& methods) {
477+
Isolate* isolate = Isolate::GetCurrent();
478+
Local<v8::Function> function =
479+
FunctionTemplate::NewWithCFunctionOverloads(
480+
isolate,
481+
slow_callback,
482+
Local<Value>(),
483+
Local<v8::Signature>(),
484+
0,
485+
v8::ConstructorBehavior::kThrow,
486+
v8::SideEffectType::kHasNoSideEffect,
487+
methods)
488+
->GetFunction(context)
489+
.ToLocalChecked();
490+
const v8::NewStringType type = v8::NewStringType::kInternalized;
491+
Local<v8::String> name_string =
492+
v8::String::NewFromUtf8(isolate, name.data(), type, name.size())
493+
.ToLocalChecked();
494+
that->Set(context, name_string, function).Check();
495+
}
496+
471497
void SetMethodNoSideEffect(Local<v8::Context> context,
472498
Local<v8::Object> that,
473499
const std::string_view name,

‎src/util.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,12 @@ void SetFastMethodNoSideEffect(
921921
const std::string_view name,
922922
v8::FunctionCallback slow_callback,
923923
const v8::MemorySpan<const v8::CFunction>& methods);
924+
void SetFastMethodNoSideEffect(
925+
v8::Local<v8::Context> context,
926+
v8::Local<v8::Object> that,
927+
const std::string_view name,
928+
v8::FunctionCallback slow_callback,
929+
const v8::MemorySpan<const v8::CFunction>& methods);
924930
void SetProtoMethod(v8::Isolate* isolate,
925931
v8::Local<v8::FunctionTemplate> that,
926932
const std::string_view name,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Flags: --permission --allow-fs-read=* --allow-fs-write=* --allow-natives-syntax --expose-internals --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const { internalBinding } = require('internal/test/binding');
7+
const path = require('path');
8+
9+
// Test that process.permission.has() uses the V8 fast API path.
10+
11+
// Test with scope only (no resource argument).
12+
function testHasScope() {
13+
assert.strictEqual(process.permission.has('fs.read', __filename), true);
14+
assert.strictEqual(process.permission.has('fs.write', __dirname), true);
15+
assert.strictEqual(
16+
process.permission.has('fs.read', path.resolve('/nonexistent')),
17+
true
18+
);
19+
assert.strictEqual(process.permission.has('fs.read'), true);
20+
assert.strictEqual(process.permission.has('fs.write'), true);
21+
assert.strictEqual(process.permission.has('child'), false);
22+
assert.strictEqual(process.permission.has('worker'), false);
23+
assert.strictEqual(process.permission.has('invalid-key'), false);
24+
}
25+
26+
// Warm up and optimize for the fast API path.
27+
eval('%PrepareFunctionForOptimization(testHasScope)');
28+
testHasScope();
29+
testHasScope();
30+
31+
eval('%OptimizeFunctionOnNextCall(testHasScope)');
32+
testHasScope();
33+
34+
if (common.isDebug) {
35+
const { getV8FastApiCallCount } = internalBinding('debug');
36+
// After optimization: testHasScope = 4, testHasResource = 3, testHasInvalid = 1
37+
assert.strictEqual(getV8FastApiCallCount('permission.has'), 8);
38+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL