kernels | process meta kernel | TODO: repl process kernel | Search

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.

Run example

npm run import -- "test process meta kernel"

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
    */

}

What the code could have been:

// 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));
}

Code Breakdown

Dependencies and Imports

The code imports dependencies from a Core module using the require function and assigns them to variables:

testProcessKernel Function

This function returns a Promise that resolves with a kernel object and performs the following operations:

  1. Creates a new kernel object using the processMetaKernel function, passing an options object with:
  2. Sets a timeout to execute two do_message calls on the kernel object:
  3. Resolves the Promise with the kernel object

do_shutdown Function

The do_shutdown function is called on the resolved kernel object and logs its result to the console.

Module Exports

The testProcessKernel function is exported as a module.

Integration with $

If the $ object is defined, the code:

  1. Calls the async method on the $ object
  2. Calls the testProcessKernel function and sends its result or error to the $ object using the sendResult or sendError methods, respectively