Skip to content

Commit 70beef9

Browse files
zertoshaddaleax
authored andcommitted
v8: add cachedDataVersionTag
Adds `v8.cachedDataVersionTag()`, which returns an integer representing the version tag for `cachedData` for the current V8 version & flags. PR-URL: #11515 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 94d1c8d commit 70beef9

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

doc/api/v8.md

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ const v8 = require('v8');
99

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

12+
## v8.cachedDataVersionTag()
13+
<!-- YAML
14+
added: REPLACEME
15+
-->
16+
17+
Returns an integer representing a "version tag" derived from the V8 version,
18+
command line flags and detected CPU features. This is useful for determining
19+
whether a [`vm.Script`][] `cachedData` buffer is compatible with this instance
20+
of V8.
21+
1222
## v8.getHeapSpaceStatistics()
1323
<!-- YAML
1424
added: v6.0.0
@@ -144,5 +154,6 @@ setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
144154
```
145155

146156
[V8]: https://developers.google.com/v8/
157+
[`vm.Script`]: vm.html#vm_new_vm_script_code_options
147158
[here]: https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
148159
[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-5.0/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4

lib/v8.js

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ exports.getHeapStatistics = function() {
5959
};
6060
};
6161

62+
exports.cachedDataVersionTag = v8binding.cachedDataVersionTag;
6263
exports.setFlagsFromString = v8binding.setFlagsFromString;
6364

6465
exports.getHeapSpaceStatistics = function() {

src/node_v8.cc

+13
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ using v8::Context;
1313
using v8::FunctionCallbackInfo;
1414
using v8::HeapSpaceStatistics;
1515
using v8::HeapStatistics;
16+
using v8::Integer;
1617
using v8::Isolate;
1718
using v8::Local;
1819
using v8::NewStringType;
1920
using v8::Object;
21+
using v8::ScriptCompiler;
2022
using v8::String;
2123
using v8::Uint32;
2224
using v8::V8;
@@ -53,6 +55,15 @@ static const size_t kHeapSpaceStatisticsPropertiesCount =
5355
static size_t number_of_heap_spaces = 0;
5456

5557

58+
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
59+
Environment* env = Environment::GetCurrent(args);
60+
Local<Integer> result =
61+
Integer::NewFromUnsigned(env->isolate(),
62+
ScriptCompiler::CachedDataVersionTag());
63+
args.GetReturnValue().Set(result);
64+
}
65+
66+
5667
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
5768
Environment* env = Environment::GetCurrent(args);
5869
HeapStatistics s;
@@ -99,6 +110,8 @@ void InitializeV8Bindings(Local<Object> target,
99110
Local<Context> context) {
100111
Environment* env = Environment::GetCurrent(context);
101112

113+
env->SetMethod(target, "cachedDataVersionTag", CachedDataVersionTag);
114+
102115
env->SetMethod(target,
103116
"updateHeapStatisticsArrayBuffer",
104117
UpdateHeapStatisticsArrayBuffer);

test/parallel/test-v8-version-tag.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const v8 = require('v8');
5+
6+
const versionTag1 = v8.cachedDataVersionTag();
7+
assert.strictEqual(typeof versionTag1, 'number');
8+
assert.strictEqual(v8.cachedDataVersionTag(), versionTag1);
9+
10+
// The value of cachedDataVersionTag is derived from the command line flags and
11+
// detected CPU features. Test that the value does indeed update when flags
12+
// are toggled.
13+
v8.setFlagsFromString('--allow_natives_syntax');
14+
15+
const versionTag2 = v8.cachedDataVersionTag();
16+
assert.strictEqual(typeof versionTag2, 'number');
17+
assert.strictEqual(v8.cachedDataVersionTag(), versionTag2);
18+
19+
assert.notStrictEqual(versionTag1, versionTag2);

0 commit comments

Comments
 (0)