Skip to content

Commit 572ea60

Browse files
addaleaxtargos
authored andcommitted
test: verify performance.timerify() works w/ non-Node Contexts
PR-URL: #23784 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 167e99b commit 572ea60

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/node_perf.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
310310
Isolate* isolate = args.GetIsolate();
311311
HandleScope scope(isolate);
312312
Environment* env = Environment::GetCurrent(isolate);
313-
CHECK_NOT_NULL(env); // TODO(addaleax): Verify that this is correct.
313+
CHECK_NOT_NULL(env);
314314
Local<Context> context = env->context();
315315
Local<Function> fn = args.Data().As<Function>();
316316
size_t count = args.Length();
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <node.h>
2+
#include <assert.h>
3+
4+
namespace {
5+
6+
using v8::Context;
7+
using v8::Function;
8+
using v8::FunctionTemplate;
9+
using v8::Isolate;
10+
using v8::Local;
11+
using v8::MaybeLocal;
12+
using v8::NewStringType;
13+
using v8::Object;
14+
using v8::Script;
15+
using v8::String;
16+
using v8::Value;
17+
18+
inline void RunInNewContext(
19+
const v8::FunctionCallbackInfo<v8::Value>& args) {
20+
Isolate* isolate = args.GetIsolate();
21+
Local<Context> context = Context::New(isolate);
22+
Context::Scope context_scope(context);
23+
24+
context->Global()->Set(
25+
context,
26+
String::NewFromUtf8(isolate, "data", NewStringType::kNormal)
27+
.ToLocalChecked(),
28+
args[1]).FromJust();
29+
30+
assert(args[0]->IsString()); // source code
31+
Local<Script> script;
32+
Local<Value> ret;
33+
if (!Script::Compile(context, args[0].As<String>()).ToLocal(&script) ||
34+
!script->Run(context).ToLocal(&ret)) {
35+
return;
36+
}
37+
38+
args.GetReturnValue().Set(ret);
39+
}
40+
41+
inline void Initialize(Local<Object> exports,
42+
Local<Value> module,
43+
Local<Context> context) {
44+
Isolate* isolate = context->GetIsolate();
45+
Local<String> key = String::NewFromUtf8(
46+
isolate, "runInNewContext", NewStringType::kNormal).ToLocalChecked();
47+
Local<Function> value = FunctionTemplate::New(isolate, RunInNewContext)
48+
->GetFunction(context)
49+
.ToLocalChecked();
50+
assert(exports->Set(context, key, value).IsJust());
51+
}
52+
53+
} // anonymous namespace
54+
55+
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': ['binding.cc']
6+
},
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const { runInNewContext } = require(`./build/${common.buildType}/binding`);
6+
const { performance } = require('perf_hooks');
7+
8+
// Check that performance.timerify() works when called from another context,
9+
// for a function created in another context.
10+
11+
const check = runInNewContext(`
12+
const { performance, assert } = data;
13+
const timerified = performance.timerify(function() { return []; });
14+
assert.strictEqual(timerified().constructor, Array);
15+
'success';
16+
`, { performance, assert });
17+
assert.strictEqual(check, 'success');

0 commit comments

Comments
 (0)