Skip to content

Commit

Permalink
v8: add cachedDataVersionTag
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
zertosh authored and addaleax committed Mar 5, 2017
1 parent 94d1c8d commit 70beef9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ 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 this instance
of V8.

## v8.getHeapSpaceStatistics()
<!-- YAML
added: v6.0.0
Expand Down Expand Up @@ -144,5 +154,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);

0 comments on commit 70beef9

Please sign in to comment.