Skip to content

Commit

Permalink
Updates to support later v8's
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanaelA committed Nov 27, 2019
1 parent d8e5758 commit 42d5291
Show file tree
Hide file tree
Showing 10 changed files with 519 additions and 43 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ Please note the examples and helper commands can show you how to use a good chun
npm install v8-natives
```

### Breaking changes
#### Removed from recent versions of V8
- `setFlags`
- `getV8Version`

#### Renamed in v8
- `functionGetName` = `getFunctionName`



### Usage
#### Browser:

Expand Down Expand Up @@ -97,7 +107,6 @@ v8.collectGarbage();

#### ChangeLog
v8 Internal function list has changed the following functions have been removed:
- getV8Version
- getOptimizationCount


Expand Down
21 changes: 16 additions & 5 deletions examples/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
return result;
}

// Simple function, optimizable.
function sum0() {
sum(1,2,3);
}

// Using or Setting Arguments improperly (even if you don't use them afterwords) will cause it to be un-optimizable
function sum1() {
sum.call(this, 1, 2, 3);
sum.apply(this, [1,2,3]);
throw new Error("Hi");
}

// However a call with 'arguments' is considered leaking the arguments; and this is NOT optimizable.
Expand All @@ -32,32 +38,38 @@
// Using or Setting Arguments improperly (even if you don't use them afterwords) will cause it to be un-optimizable
function sum4() {
arguments[1] = 1;
sum(1,2);
sum(arguments[1],2);
}


function startTests() {
// Simple Hack to wait for the v8 namespace to be loaded
if (!v8.isNative()) {
notice('You need to start Chrome with the --js-flags="--allow-natives-syntax"', '#f00');
return;
}

log(0, "This is optimizable");
v8.helpers.testOptimization(sum0);
log(v8.getOptimizationStatus(sum0), 'sum0');

log(0, "This is optimizable; .calls & .apply do not mess up the optimizer");
v8.helpers.testOptimization(sum1);
log(v8.getOptimizationStatus(sum1), 'sum1');

log(0, "A call with 'arguments' is considered leaking the arguments; and this is NOT optimizable.");
log(0, "A call with 'arguments' is considered leaking the arguments; and this is NOT optimizable in some engines.");
v8.helpers.testOptimization(sum2);
log(v8.getOptimizationStatus(sum2), 'sum2');

log(0, "Copying the values out of args is safe and optimizable.");
v8.helpers.testOptimization(sum3);
log(v8.getOptimizationStatus(sum3), 'sum3');

log(0,"Using or Setting Arguments improperly (even if you don't use them afterwords) will cause it to be un-optimizable");
log(0, "Messing with Arguments.");
v8.helpers.testOptimization(sum4);
log(v8.getOptimizationStatus(sum4), 'sum4');


notice("Tests are now complete, you can now open the Javascript Command Console if you want...");
}

Expand Down Expand Up @@ -111,7 +123,7 @@
id.innerHTML = id.innerHTML + data + '<br>';

if (optStatus !== 0) {
id.innerHTML = id.innerHTML + "<p style='background-color: #EEE'>" + this[routine].toString() + "</p><br>";
id.innerHTML = id.innerHTML + "<p style='background-color: #EEE'>" + this[routine].toString().replace('<', "&lt;") + "</p><br>";
}


Expand All @@ -136,7 +148,6 @@ <h2>Chrome v8 Natives Test</h2><br>

<center><font color="red"><h3>Please note these tests and descriptions were done BEFORE TurboFan;<br>it appears all the rules as we knew them have changed. ;-)</h3></font></center>
<br><br>
<div>Please note the Chrome Dev Tools must be closed when you run this; otherwise the v8 engine optimizer will IGNORE all the optimization requests. </div>
<br><br><div id="log"></div>

</body>
Expand Down
7 changes: 6 additions & 1 deletion examples/node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
--------------------------------------
(c)2014, Nathanael Anderson.
(c)2014-2019, Nathanael Anderson.
Repository: https://github.com/Nathanaela/v8-Natives
--------------------------------------
v8-Natives is under The MIT License (MIT)
Expand Down Expand Up @@ -94,6 +94,11 @@ if (!v8.isNative()) {
console.log("\r\nSum5 should optimize, sum6 won't since it is writing back to arguments.");
testOptimization([sum5,sum6], ['sum5','sum6']);

console.log("");
console.log("----------------------------------");
console.log("Running benchmark on sum1");
console.log("----------------------------------");

var cnt = 1000000;
var time = benchmark(cnt, sum1);
//var time = benchmark(cnt, sum1);
Expand Down
38 changes: 29 additions & 9 deletions lib/v8-browser-all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
--------------------------------------
(c)2014-2016, Nathanael Anderson.
(c)2014-2019, Nathanael Anderson.
Repository: https://github.com/Nathanaela/v8-Natives
--------------------------------------
v8-Natives is under The MIT License (MIT)
Expand All @@ -24,12 +24,15 @@
THE SOFTWARE.
*/

// For a complete list of %Commands see -
// https://github.com/v8/v8/blob/master/src/runtime/runtime.h

(function (_global) {

function testIfAllowNativeSyntax() {
var native = false;
try {
eval('%NativeScriptsCount();');
eval('%GetHeapUsage();');
native = true;
}
catch (err) {
Expand Down Expand Up @@ -77,15 +80,22 @@
optStat.push("Marked for Concurrent Optimization");
}
if (checkBitmap(optStatus,512)) {
optStat.push("Conccurently Optimizating");
optStat.push("Concurrently Optimizing");
}
if (checkBitmap(optStatus,1024)) {
optStat.push("Is Executing");
}
if (checkBitmap(optStatus,2048)) {
optStat.push("Topmost frame is Turbo Fanned");
}
console.log("",fName, optStat.join(", "));
if (checkBitmap(optStatus, 4096)) {
optStat.push("Lite Mode")
}
if (checkBitmap(optStatus, 8192)) {
optStat.push("Marked for de-optimization")
}

console.log("",fName, optStat.join(", "));


return (optStatus);
Expand Down Expand Up @@ -181,7 +191,7 @@


if (testIfAllowNativeSyntax()) {
console.log("Useing native");
console.log("Using native");
var v8 = _global.v8 = new Function('"use strict";\
// v8 Functions are located in v8/lib/runtime.cc & runtime.h\
// SMI = SMall Integer\n\
Expand Down Expand Up @@ -246,10 +256,13 @@
return %HaveSameMap(data1, data2);\
},\
functionGetName: function(func) {\
return %FunctionGetName(func);\
return %GetFunctionName(func);\
},\
getFunctionName: function(func) {\
return %GetFunctionName(func);\
},\
isSmi: function(data) {\
return %_IsSmi(data);\
return %IsSmi(data);\
},\
isValidSmi: function(data) {\
return %IsValidSmi(data);\
Expand All @@ -265,7 +278,14 @@
}, \
traceExit: function(val) { \
return %TraceExit(val); \
} \
}, \
CompileOptimized(func, concurrent) {\
if (concurrent) {\
return %CompileOptimized_Concurrent(func);\
} else {\
return %CompileOptimized_NotConcurrent(func);\
}\
}\
};\
')();

Expand All @@ -285,7 +305,7 @@
v8.getHeapUsage = function() { return 0; }

// Needs to return a string
v8.functionGetName = function() { return "N/A"};
v8.getFunctionName = v8.functionGetName = function() { return "N/A"; };

// Returns nothing
v8.optimizeFunctionOnNextCall = v8.ClearFunctionFeedback = v8.deoptimizeNow = v8.debugTrace =
Expand Down
29 changes: 19 additions & 10 deletions lib/v8-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
--------------------------------------
(c)2014-2017, Nathanael Anderson.
(c)2014-2019, Nathanael Anderson.
Repository: https://github.com/Nathanaela/v8-Natives
--------------------------------------
v8-Natives is under The MIT License (MIT)
Expand Down Expand Up @@ -33,7 +33,7 @@
function testIfAllowNativeSyntax() {
var native = false;
try {
eval('%NativeScriptsCount();');
eval('%GetHeapUsage();');
native = true;
}
catch (err) {
Expand Down Expand Up @@ -93,15 +93,22 @@
optStat.push("Marked for Concurrent Optimization");
}
if (checkBitmap(optStatus,512)) {
optStat.push("Conccurently Optimizating");
optStat.push("Concurrently Optimizing");
}
if (checkBitmap(optStatus,1024)) {
optStat.push("Is Executing");
}
if (checkBitmap(optStatus,2048)) {
optStat.push("Topmost frame is Turbo Fanned");
}
console.log("",fName, optStat.join(", "));
if (checkBitmap(optStatus, 4096)) {
optStat.push("Lite Mode")
}
if (checkBitmap(optStatus, 8192)) {
optStat.push("Marked for de-optimization")
}

console.log("",fName, optStat.join(", "));


return (optStatus);
Expand Down Expand Up @@ -130,20 +137,22 @@
}

if (Array.isArray(f)) {
f[0]();
f[0](); // Have to Run this Twice now in v8 (See notes above)
for (var i = 0; i < f.length; i++) {
v8.optimizeFunctionOnNextCall(f[i]);
v8.CompileOptimized(f[i], true);
}
f[0]();
try {
f[0]();
} catch (e) {}
for (var j = 0; j < f.length; j++) {
printStatus(f[j], fname[j]);
}
} else {
f();
f(); // Have to Run this Twice now in v8 (See notes above)
v8.optimizeFunctionOnNextCall(f);
f();
v8.CompileOptimized(f, true);
try {
f();
} catch (e) {}
printStatus(f, fname);
}
}
Expand Down
23 changes: 17 additions & 6 deletions lib/v8-native-calls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
--------------------------------------
(c)2014-2017, Nathanael Anderson.
(c)2014-2019, Nathanael Anderson.
Repository: https://github.com/Nathanaela/v8-Natives
--------------------------------------
v8-Natives is under The MIT License (MIT)
Expand All @@ -24,6 +24,10 @@
THE SOFTWARE.
*/

// For a complete list of %Commands see -
// https://github.com/v8/v8/blob/master/src/runtime/runtime.h


(function (_global) {

"use strict";
Expand Down Expand Up @@ -87,26 +91,33 @@ _global.v8 = {
haveSameMap: function(data1, data2) {
return %HaveSameMap(data1, data2);
},
getFunctionName: function(func) {
return %GetFunctionName(func);
},
functionGetName: function(func) {
return %FunctionGetName(func);
return %GetFunctionName(func);
},
isSmi: function(data) {
return %_IsSmi(data);
return %IsSmi(data);
},
isValidSmi: function(data) {
return %IsValidSmi(data);
},
neverOptimizeFunction: function(func) {
return %NeverOptimizeFunction(func);
},
setFlags: function(flag) {
return %SetFlags(flag);
},
traceEnter: function() {
return %TraceEnter();
},
traceExit: function(val) {
return %TraceExit(val);
},
CompileOptimized(func, concurrent) {
if (concurrent) {
return %CompileOptimized_Concurrent(func);
} else {
return %CompileOptimized_NotConcurrent(func);
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/v8-native-dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var v8 = {
v8.getHeapUsage = function() { return 0; }

// Needs to return a string
v8.functionGetName = function() { return "N/A"};
v8.getFunctionName = v8.functionGetName = function() { return "N/A"};

// Returns nothing
v8.optimizeFunctionOnNextCall = v8.clearFunctionTypeFeedback = v8.deoptimizeNow = v8.debugTrace =
v8.debugPrint = v8.deoptimizeFunction = v8.neverOptimizeFunction = v8.collectGarbage = function() { return; }
v8.debugPrint = v8.deoptimizeFunction = v8.neverOptimizeFunction = v8.collectGarbage = function() { return; }

// Returns booleans, so we return false
v8.getOptimizationStatus = v8.hasFastProperties = v8.hasFastSmiElements = v8.hasFastObjectElements = v8.hasFastDoubleElements =
Expand Down
Loading

0 comments on commit 42d5291

Please sign in to comment.