Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: verify performance.timerify() works w/ non-Node Contexts #23784

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env); // TODO(addaleax): Verify that this is correct.
CHECK_NOT_NULL(env);
Local<Context> context = env->context();
Local<Function> fn = args.Data().As<Function>();
size_t count = args.Length();
Expand Down
55 changes: 55 additions & 0 deletions test/addons/non-node-context/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <node.h>
#include <assert.h>

namespace {

using v8::Context;
using v8::Function;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::NewStringType;
using v8::Object;
using v8::Script;
using v8::String;
using v8::Value;

inline void RunInNewContext(
const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);

context->Global()->Set(
context,
String::NewFromUtf8(isolate, "data", NewStringType::kNormal)
.ToLocalChecked(),
args[1]).FromJust();

assert(args[0]->IsString()); // source code
Local<Script> script;
Local<Value> ret;
if (!Script::Compile(context, args[0].As<String>()).ToLocal(&script) ||
!script->Run(context).ToLocal(&ret)) {
return;
}

args.GetReturnValue().Set(ret);
}

inline void Initialize(Local<Object> exports,
Local<Value> module,
Local<Context> context) {
Isolate* isolate = context->GetIsolate();
Local<String> key = String::NewFromUtf8(
isolate, "runInNewContext", NewStringType::kNormal).ToLocalChecked();
Local<Function> value = FunctionTemplate::New(isolate, RunInNewContext)
->GetFunction(context)
.ToLocalChecked();
assert(exports->Set(context, key, value).IsJust());
}

} // anonymous namespace

NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
8 changes: 8 additions & 0 deletions test/addons/non-node-context/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
'targets': [
{
'target_name': 'binding',
'sources': ['binding.cc']
},
]
}
17 changes: 17 additions & 0 deletions test/addons/non-node-context/test-perf-hooks-timerify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const common = require('../../common');
const assert = require('assert');
const { runInNewContext } = require(`./build/${common.buildType}/binding`);
const { performance } = require('perf_hooks');

// Check that performance.timerify() works when called from another context,
// for a function created in another context.

const check = runInNewContext(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you assign the string to a variable, I find this is a bit tricky to read (probably because of GitHub viewer limitations)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure what you mean? If you are talking about the template string, why does assigning to a new variable make a difference here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
It looks awkward, especially the line that starts `, {

IMHO a simple variable makes it clearer:

const code = `
const { performance, assert } = data;
const timerified = performance.timerify(function() { return []; });
assert.strictEqual(timerified().constructor, Array);
'success';
`;
const check = runInNewContext(code, { performance, assert });
assert.strictEqual(check, 'success');

const { performance, assert } = data;
const timerified = performance.timerify(function() { return []; });
assert.strictEqual(timerified().constructor, Array);
'success';
`, { performance, assert });
assert.strictEqual(check, 'success');