Running in the current context of an app with a module.
See original GitHub issueI have been looking into the source for bytenode and was able to modify the runBytecode to support running a module. When I run in the global context I can not access the variables in the module. I would like to be able to call functions inside of the file even when it is compiled as a module.
Ex.
const hello = "https://www.google.com/";
function testfunc(paramater) {
paramater = paramater + 'hello';
return paramater;
}
function testfunc2(number, number2) {
return number * number2;
}
console.log(testfunc(hello))
That compiles using:
const bytenode = require('bytenode');
bytenode.compileFile({
filename: 'encode.js',
compileAsModule: true
});
I sorta hacked something together for the implementation, this is NOT going into production and simply for personal experiments ONLY, please understand this:
const runBytecode = function (bytecodeBuffer) {
if (!Buffer.isBuffer(bytecodeBuffer)) {
throw new Error(`bytecodeBuffer must be a buffer object.`);
}
fixBytecode(bytecodeBuffer);
let length = readSourceHash(bytecodeBuffer);
let dummyCode = "";
if (length > 1) {
dummyCode = '"' + "\u200b".repeat(length - 2) + '"'; // "\u200b" Zero width space
}
const c = new vm.Script(dummyCode, {
cachedData: bytecodeBuffer
})
c.runInNewContext(global)(module.exports, require, module, __filename, __dirname, process, global);
console.log(global) // Does not log the functions 'testfunc' and 'testfunc2'
// Successfully runs and logs the output, though.
/*if (script.cachedDataRejected) {
throw new Error('Invalid or incompatible cached data (cachedDataRejected)');
}*/
return //script.runInThisContext();
};
Any input would help!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6
Top Results From Across the Web
The Application Context — Flask Documentation (1.1.x)
The application context keeps track of the application-level data during a request, CLI command, or other activity. Rather than passing the application around ......
Read more >How to access application context from another module of ...
I have two modules of same application, having different Manifest files, Main App is in first Module, I want to access to Main...
Read more >Understanding the Application and Request Contexts in ...
First, let's look at how to work with the current_app object to access the Application context. Within the Python shell, if you try...
Read more >Configure the app module | Android Developers
This page describes useful app settings in the module-level build.gradle ... Safely adjust the namespace independent of the application ID.
Read more >Using current context | LoopBack Documentation
Running and debugging apps ... LoopBack 2.x introduced current-context APIs using the module continuation-local-storage to provide a context object ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not urgent, take your time, just curious.
I apologize for the late response. I will look into it and get back to you.