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

v8: add cachedDataVersionTag #11515

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const v8 = require('v8');

*Note*: The APIs and implementation are subject to change at any time.

## v8.cachedDataVersionTag()
<!-- YAML
added: REPLACEME
-->

Returns an integer representing a "version tag" derived from the V8 version,
command line flags and detected CPU features. This is useful for determining
whether a [`vm.Script`][] `cachedData` buffer is compatible with another V8.
Copy link
Member

Choose a reason for hiding this comment

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

I'd s/another V8/this instance of V8/ to emphasize that it applies to the running instance of V8.


## v8.getHeapSpaceStatistics()
<!-- YAML
added: v6.0.0
Expand Down Expand Up @@ -144,5 +153,6 @@ setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
```

[V8]: https://developers.google.com/v8/
[`vm.Script`]: vm.html#vm_new_vm_script_code_options
[here]: https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-5.0/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4
1 change: 1 addition & 0 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ exports.getHeapStatistics = function() {
};
};

exports.cachedDataVersionTag = v8binding.cachedDataVersionTag;
exports.setFlagsFromString = v8binding.setFlagsFromString;

exports.getHeapSpaceStatistics = function() {
Expand Down
13 changes: 13 additions & 0 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ using v8::Context;
using v8::FunctionCallbackInfo;
using v8::HeapSpaceStatistics;
using v8::HeapStatistics;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::ScriptCompiler;
using v8::String;
using v8::Uint32;
using v8::V8;
Expand Down Expand Up @@ -53,6 +55,15 @@ static const size_t kHeapSpaceStatisticsPropertiesCount =
static size_t number_of_heap_spaces = 0;


void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Integer> result =
Integer::NewFromUnsigned(env->isolate(),
ScriptCompiler::CachedDataVersionTag());
args.GetReturnValue().Set(result);
}


void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
HeapStatistics s;
Expand Down Expand Up @@ -99,6 +110,8 @@ void InitializeV8Bindings(Local<Object> target,
Local<Context> context) {
Environment* env = Environment::GetCurrent(context);

env->SetMethod(target, "cachedDataVersionTag", CachedDataVersionTag);

env->SetMethod(target,
"updateHeapStatisticsArrayBuffer",
UpdateHeapStatisticsArrayBuffer);
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-v8-version-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
require('../common');
const assert = require('assert');
const v8 = require('v8');

const versionTag1 = v8.cachedDataVersionTag();
assert.strictEqual(typeof versionTag1, 'number');
assert.strictEqual(v8.cachedDataVersionTag(), versionTag1);

// The value of cachedDataVersionTag is derived from the command line flags and
// detected CPU features. Test that the value does indeed update when flags
// are toggled.
v8.setFlagsFromString('--allow_natives_syntax');

const versionTag2 = v8.cachedDataVersionTag();
assert.strictEqual(typeof versionTag2, 'number');
assert.strictEqual(v8.cachedDataVersionTag(), versionTag2);

assert.notStrictEqual(versionTag1, versionTag2);