-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async transpile #12
Comments
IJavascript code is ES5 and I will relax this requirement in the next major-version release (I'm waiting on BTW, I think that the need to run Wasn't your plan to define If you do that, your transpile function would simply transpile code like |
Yea, if you don’t mind I’ll take you up in that offer — I’m pretty rust when it comes to ES5. I looked into hanging |
What do you mean by server? I'll go ahead with the PR, so that you can experiment with your current approach. |
* To allow asynchronous transpilation, Server#transpile now accepts functions that return a Promise. Closes #12
Sorry, I'm not quite sure what nomenclature to use. I was trying to say that |
Thx for the neat diagram! Now that I'm clear what you mean by server, I'm still convinced #12 (comment) applies. I guess the best way to see what I meant is with a very "bare-bone" example implementing var Kernel = require("jp-kernel");
var config = {
cwd: process.cwd(),
hideUndefined: true,
protocolVersion: "5.1",
};
config.startupCallback = function startupCallback() {
// set `$$.magics = {}` in the session server to store the functions implementing magics
// and set the magics `%addcmd` and `%cd`
var code = "$$.magics = {};";
code += "$$.magics['%addcmd'] = function(label, cmd) { return $$.magics[label] = eval(cmd); };";
code += "$$.magics['%cd'] = function(path) { return process.chdir(path); };";
this.session.execute(code, {
onSuccess: function() {
log("startupCallback: '" + code + "' run successfuly");
},
onError: function() {
log("startupCallback: '" + code + "' failed to run");
},
});
};
config.transpile = function(code) {
// handle magic `%addcmd label cmd`
if (code.startsWith("%addcmd ")) {
var line = code.slice(8).trim(); // skip "%addcmd "
var pos = line.indexOf(' ');
if (pos === -1) {
throw new Error("%addmd: syntax error: missing label or cmd");
}
var label = JSON.stringify(line.slice(0, pos));
var cmd = JSON.stringify(line.slice(pos));
// transpile into code to set the magic handler in the session server
return "$$.magic['%addcmd'](" + label + ", " + cmd + ");";
}
// handle other magics starting with %
if (code.startsWith("%")) {
var pos = line.indexOf(' ');
if (pos === -1) {
// magic has no arguments
return "$$.magic[" + JSON.stringify(line) + "]()";
}
var label = JSON.stringify(line.slice(0, pos));
var arg = JSON.stringify(line.slice(pos).trim());
return ""$$.magic[" + label + "](" + arg + ");";
}
// not a magic, no transpilation needed
return code;
};
// any other options, e.g. `kernel.handlers.is_complete_request`
// ...
// launch the kernel
var kernel = new Kernel(config);
// Interpret a SIGINT signal as a request to interrupt the kernel
process.on("SIGINT", function() {
log("Interrupting kernel");
kernel.restart(); // TODO(NR) Implement kernel interruption
}); |
I like it. I'm going to try some variation of that -- thanks for the tip. :) |
In order to ensure functions exist when added as a magic as discussed in n-riesco/ijavascript#43, I need to resolve the function name using the
inspect
function of the NEL session. Inspect uses callbacks, but transpile is synchronous.Any objections to wrapping the transpile call in Promise.resolve() to make it indifferent as to whether the transpiler is synchronous or asynchronous? The
try / catch
could be replaced with.then / .catch
to the same effect.It doesn't look like
_runNow
expects to be synchronous, so I don't think this would break anything else.The text was updated successfully, but these errors were encountered: