finished pkg_bootstrap

This commit is contained in:
Igor Klopov 2016-09-09 18:22:09 +03:00
parent f43c2899d9
commit b3c2cfbf1e

View File

@ -348,7 +348,7 @@
--- node/src/node_javascript.cc
+++ node/src/node_javascript.cc
@@ -31,8 +31,16 @@
@@ -31,8 +31,44 @@
env->isolate(), reinterpret_cast<const char*>(native.source),
NewStringType::kNormal, native.source_len).ToLocalChecked();
target->Set(name, source);
@ -357,9 +357,37 @@
+
+ auto name = String::NewFromUtf8(env->isolate(), "_pkg_bootstrap");
+ auto source = String::NewFromUtf8(env->isolate(),
+ "var vm = require('vm');\n" \
+ "console.log(vm);\n" \
+ "// TODO result(process, require, console);\n"
+ "function readOverlay() {\n" \
+ " var fs = require('fs');\n" \
+ " var me = fs.readFileSync(process.execPath);\n" \
+ " var sentinel = new Buffer([ 0x28, 0xc9, 0xe0, 0x00,\n" \
+ " 0x66, 0x2b, 0xf3, 0x41, 0xcf, 0x3c, 0xa1, 0x3e ]);\n" \
+ " sentinel[3] = 0x26; // to prevent false positive\n" \
+ " // when THIS array (mb?) found as plain bytes\n" \
+ " var index = me.indexOf(sentinel);\n" \
+ " if (index < 0) {\n" \
+ " process.argv.splice(1, 1);\n" \
+ " return undefined;\n" \
+ " }\n" \
+ " var vm = require('vm');\n" \
+ " var s = new vm.Script(undefined, {\n" \
+ " cachedData: me.slice(index + 16),\n" \
+ " sourceless: true\n" \
+ " });\n" \
+ " if (s.cachedDataRejected) {\n" \
+ " console.log('Pkg: Cached data was rejected');\n" \
+ " process.exit(2);\n" \
+ " }\n" \
+ " var fn = s.runInThisContext();\n" \
+ " return fn(process, require, console);\n" \
+ "}\n" \
+ "\n" \
+ "var r = readOverlay();\n" \
+ "if (r && r.undoPatch) {\n" \
+ " // fallback to plain node. need to revert patch to node/lib/module.js\n" \
+ " require('fs').internalModuleStat = process.binding('fs').internalModuleStat;\n" \
+ " require('fs').internalModuleReadFile = process.binding('fs').internalModuleReadFile;\n" \
+ "}\n"
+ );
+ target->Set(name, source);
}