The code imports dependencies from a Core
module and defines a testProcessKernel
function that creates a new kernel object, performs some operations on it, and resolves a Promise with the kernel object. The testProcessKernel
function is exported as a module and can be integrated with an external $
object, which is used to send its result or error to the outside.
npm run import -- "test process meta kernel"
var importer = require('../Core');
var {processMetaKernel} = importer.import("process meta kernel")
var metaKernelInterface = importer.import("meta kernel interface");
var interface = importer.import("enforcing an interface");
function testProcessKernel() {
return new Promise(resolve => {
var kernel = processMetaKernel({
do_respond: (message) => {
console.log(message);
resolve(kernel);
},
kernel_config: {
autoinit: true,
child_process: [
'node', '--eval', `
var importer = require('./Core');
var nodeMetaKernel = importer.import("node meta kernel")
nodeMetaKernel({
kernel_config: {autoinit: true}
})
process.stdin.resume();
`
]
},
});
setTimeout(() => {
//console.log(interface(kernel, metaKernelInterface, false))
console.log(kernel.do_message({do_execute: 'console.log(1 + 1)'}));
console.log(kernel.do_message({do_execute: '1 + 1'}));
}, 5000)
})
.then(kernel => console.log(kernel.do_shutdown()))
}
module.exports = testProcessKernel;
if(typeof $ !== 'undefined') {
$.async();
testProcessKernel()
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
/* expected output
undefined
undefined
2
2
*/
}
// Import necessary modules
const { processMetaKernel, importModule } = require('../Core');
// Define a function to test the process kernel
async function testProcessKernel() {
// Create a meta kernel instance with a response callback
const kernel = await processMetaKernel({
doRespond: (message) => {
console.log(message);
return kernel; // Return the kernel instance
},
kernelConfig: {
autoinit: true,
childProcess: [
'node', '--eval', `
const { importModule } = require('./Core');
const nodeMetaKernel = importModule('node meta kernel');
nodeMetaKernel({
kernelConfig: { autoinit: true }
});
process.stdin.resume();
`
]
}
});
// Wait for 5 seconds before sending messages to the kernel
await new Promise(resolve => setTimeout(resolve, 5000));
// Send messages to the kernel and log the responses
console.log(kernel.doMessage({ doExecute: 'console.log(1 + 1)' }));
console.log(kernel.doMessage({ doExecute: '1 + 1' }));
// Shut down the kernel and log the response
return kernel.doShutdown();
}
// Export the testProcessKernel function
module.exports = testProcessKernel;
// If the $ object is defined, run the test and send the result
if (typeof $!== 'undefined') {
$.async();
testProcessKernel()
.then((result) => $.sendResult(result))
.catch((error) => $.sendError(error));
}
The code imports dependencies from a Core
module using the require
function and assigns them to variables:
importer
: an object containing functions to import from the Core
moduleprocessMetaKernel
, metaKernelInterface
, interface
: imported functions from the Core
moduletestProcessKernel
FunctionThis function returns a Promise that resolves with a kernel
object and performs the following operations:
kernel
object using the processMetaKernel
function, passing an options object with:
do_respond
: a callback function that logs messages to the console and resolves the Promise with the kernel
objectkernel_config
: an object with autoinit
set to true
and a child_process
array containing a Node.js script to executedo_message
calls on the kernel
object:
do_message
with an object containing do_execute
set to 'console.log(1 + 1)'
do_message
with an object containing do_execute
set to '1 + 1'
kernel
objectdo_shutdown
FunctionThe do_shutdown
function is called on the resolved kernel
object and logs its result to the console.
The testProcessKernel
function is exported as a module.
If the $
object is defined, the code:
async
method on the $
objecttestProcessKernel
function and sends its result or error to the $
object using the sendResult
or sendError
methods, respectively