diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-10-20 13:50:10 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-10-25 19:49:46 +0200 |
commit | a3cad3e2ff95eb7474b4baefafc969211657a896 (patch) | |
tree | 7f839e03a0e1422fa2574188a5bb745dd8b9d9f2 /test/addons/non-node-context | |
parent | 958d5b7f1d1fa47133de48625abb2fb3aa7b8ffd (diff) | |
download | android-node-v8-a3cad3e2ff95eb7474b4baefafc969211657a896.tar.gz android-node-v8-a3cad3e2ff95eb7474b4baefafc969211657a896.tar.bz2 android-node-v8-a3cad3e2ff95eb7474b4baefafc969211657a896.zip |
test: verify `performance.timerify()` works w/ non-Node Contexts
PR-URL: https://github.com/nodejs/node/pull/23784
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/addons/non-node-context')
-rw-r--r-- | test/addons/non-node-context/binding.cc | 55 | ||||
-rw-r--r-- | test/addons/non-node-context/binding.gyp | 8 | ||||
-rw-r--r-- | test/addons/non-node-context/test-perf-hooks-timerify.js | 17 |
3 files changed, 80 insertions, 0 deletions
diff --git a/test/addons/non-node-context/binding.cc b/test/addons/non-node-context/binding.cc new file mode 100644 index 0000000000..324f5c5a1e --- /dev/null +++ b/test/addons/non-node-context/binding.cc @@ -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) diff --git a/test/addons/non-node-context/binding.gyp b/test/addons/non-node-context/binding.gyp new file mode 100644 index 0000000000..b83bae3082 --- /dev/null +++ b/test/addons/non-node-context/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': ['binding.cc'] + }, + ] +} diff --git a/test/addons/non-node-context/test-perf-hooks-timerify.js b/test/addons/non-node-context/test-perf-hooks-timerify.js new file mode 100644 index 0000000000..10a9ab8064 --- /dev/null +++ b/test/addons/non-node-context/test-perf-hooks-timerify.js @@ -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(` +const { performance, assert } = data; +const timerified = performance.timerify(function() { return []; }); +assert.strictEqual(timerified().constructor, Array); +'success'; +`, { performance, assert }); +assert.strictEqual(check, 'success'); |