exe: fixes #168. Implements package.json nexe.runtime.js-flags for v8 flags.

This commit is contained in:
Jared Allard
2016-01-28 14:25:31 -08:00
parent 635cd62196
commit 87660c3682
5 changed files with 141 additions and 8 deletions
+2 -1
View File
@@ -5,5 +5,6 @@ src
nexe.exe
nexe
npm-debug.log
# test/**/src
test/**/src
test/**/out.nex
test/**/test.nex
+2 -2
View File
@@ -1,4 +1,4 @@
node_modules
test/**/src
test/**/*.exe
test/**/*.nexe
test/**/*-test
test/**/*.nex
+109 -5
View File
@@ -186,6 +186,14 @@ exports.compile = function (options, complete) {
}
},
function monkeyPatchv8FlagsCc (next) {
if(options.jsFlags) {
return _monkeyPatchv8FlagsCc(nodeCompiler, options, next);
}
return next();
},
/**
* monkeypatch child_process.js so nexejs knows when it is a forked process
*/
@@ -434,9 +442,6 @@ function _getNodeCompiler (nodeFileDir, nodeConfigureArgs, nodeMakeArgs, complet
var conf = [cfg];
console.log(isPy);
console.log(dir);
if(isPy !== "python") {
conf = [conf].concat(nodeConfigureArgs);
}
@@ -673,6 +678,103 @@ function _monkeyPatchMainCc(compiler, complete) {
}
/**
* Patch flags.cc from deps/v8/src to use hard-coded flags.
* this function is very closely ready to accept custom injection code.
**/
function _monkeyPatchv8FlagsCc(compiler, options, complete) {
var mainPath = path.join(compiler.dir, "deps/v8/src", "flags.cc");
fs.readFile(mainPath, {encoding: 'utf8'}, function(err, contents) {
if(err) {
return _log('error', 'failed to read', mainPath);
}
// Super simple injection here. Perhaps make it an array at somepoint?
var injection = '\
const char* nexevargs = "{{args}}";\n\
int nexevargslen = strlen(nexevargs);\n\
SetFlagsFromString(nexevargs, nexevargslen);\n\
';
var injectionSplit = injection.split('\n');
var injectionLength = injectionSplit.length;
var contentsSplit = contents.split('\n');
var contentsLength = contentsSplit.length;
var lastInjectionLine = injectionSplit[injectionLength-2];
var lineToInjectAfter = contentsSplit.indexOf(' ComputeFlagListHash();');
var haveWeInjectedBefore = contentsSplit.indexOf(lastInjectionLine);
var lineInjectDifference = contentsLength - lineToInjectAfter;
// this is debug, comment out.
// _log('v8 injection is', injectionLength, 'newlines long');
// _log('v8 flags source is', contentsLength, 'newlines long');
// console.log(finalContents)
var finalContents,
dontCombine;
if(lineToInjectAfter !== -1 && haveWeInjectedBefore === -1) {
_log('injecting v8 args c++');
// super debug
// _log('v8 injection determined by', lastInjectionLine);
// _log('v8 inject after line', lineToInjectAfter);
// _log('v8 inject needs to shift', lineInjectDifference, 'amount of lines by', injectionLength);
// compute out the amount of space we'll need in this.
var startShiftLine = contentsLength-1; // minus one to make up for 0 arg line.
var endShiftLine = lineToInjectAfter;
var injectRoom = injectionLength-1;
injectionSplit[0] = injectionSplit[0].replace('{{args}}', options.jsFlags);
console.log(startShiftLine, endShiftLine);
for (var i = startShiftLine; i !== endShiftLine; i--) {
contentsSplit[i+injectRoom] = contentsSplit[i];
contentsSplit[i] = '';
}
var injectionPos = 0;
for(var i = 0; i !== injectionLength-1; i++) {
contentsSplit[(lineToInjectAfter+1)+injectionPos] = injectionSplit[injectionPos];
injectionPos++;
}
} else if(lineToInjectAfter !== -1 && haveWeInjectedBefore !== -1) {
_log('re-injecting v8 args');
dontCombine = true;
finalContents = contentsSplit.join('\n');
finalContents = finalContents.replace(/const char\* nexevargs = "[A-Z\-\_]*";/gi,
'const char* nexevargs = "'+options.jsFlags+'";');
} else {
_log('warn', 'failed to find a suitable injection point for v8 args.',
'File a bug report with the node version and log.');
_log('lineToInjectAfter='+lineToInjectAfter, 'haveWeInjectedBefore='+haveWeInjectedBefore);
return process.exit(1);
}
if(!dontCombine) {
finalContents = contentsSplit.join('\n');
}
fs.writeFile(mainPath, finalContents, {encoding: 'utf8'}, function(err) {
if(err) {
_log('error', 'failed to write to', mainPath);
return process.exit(1);
}
return complete();
})
});
}
/**
* Get the first directory of a string.
*/
function _getFirstDirectory (dir) {
@@ -687,6 +789,7 @@ function _getFirstDirectory (dir) {
}
/**
* Log the progress of a request object.
*/
function _logProgress (req) {
@@ -698,7 +801,7 @@ function _logProgress (req) {
complete: "=",
incomplete: " ",
total: len,
width: ((isWin) ? 100 : process.stdout.columns - 2) // windows doesn't output colums correctly
width: 100 // just use 100
});
req.on("data", function (chunk) {
@@ -747,12 +850,13 @@ exports.package = function(path, options) {
var obj = {
input: (_package.nexe.input || options.i),
output: (_package.nexe.output || options.o),
flags: (_package.nexe.runtime.ignoreFlags || options.f),
flags: (_package.nexe.runtime.ignoreFlags || (options.f || false)),
nodeMakeArgs: (_package.nexe.runtime.nodeMakeArgs || []),
resourceFiles: (_package.nexe.resourceFiles),
nodeVersion: (_package.nexe.runtime.version || options.r),
nodeConfigureArgs: (_package.nexe.runtime.nodeConfigureArgs || []),
nodeMakeArgs: (_package.nexe.runtime.nodeMakeArgs || []),
jsFlags: (_package.nexe.runtime['js-flags'] || false),
python: (_package.nexe.python || options.p),
nodeTempDir: (_package.nexe.temp || options.t),
framework: (_package.nexe.runtime.framework || options.f)
+13
View File
@@ -0,0 +1,13 @@
/**
* Test to verify if we support strict mode (flags)
*
* @author Jared Allard <jaredallard@outlook.com>
* @version 0.0.1
* @license MIT
**/
// "use strict"; w/o --use_strict
var isStrict = (function() { return !this; })();
console.log('test:strict-mode:status =', isStrict);
+15
View File
@@ -0,0 +1,15 @@
{
"name": "flags-test",
"nexe": {
"input": "./index.js",
"output": "test.nex^$",
"temp": "src",
"runtime": {
"framework": "node",
"version": "5.5.0",
"ignoreFlags": false,
"js-flags": "--use_strict",
"node-args": ""
}
}
}