Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js - eval'ing to a live process

Did anyone set up something like this for himself using the existing node.js REPL? I didn't think of a quick way to do it.

The way I do it today is using emacs and this: https://github.com/ivan4th/swank-js

This module is composed of:

  1. A SLIME-js addon to emacs which, in combination with js2-mode, lets you simply issue a C-M-x somewhere in the body of a function def - and off goes the function's string to the ..

  2. Swank-js server (yes, you could eval from your local-machine directly to a remote process) written in Node.js - It receives the string of the function you eval'ed and actually evals it

  3. A whole part that lets you connect to another port on that server with your BROWSER and then lets you manipulate the DOM on that browser (which is pretty amazing but not relevant)

My solution uses SLIME-js on the emacs side AND I require('swank- js') on my app.js file

Now.. I have several issues and questions regarding my solution or other possible ones:

Q1: Is this overdoing it? Does someone have a secret way to eval stuff from nano into his live process?

Q2: I had to change the way swank-js is EVALing.. it used some kind of black magic like this:


var Script = process.binding('evals').Script;
var evalcx = Script.runInContext;
....
this.context = Script.createContext();
for (var i in global) this.context[i] = global[i];
this.context.module = module;
this.context.require = require;
...
r = evalcx("CODECODE", this.context, "repl");

which, as far I understand, just copies the global variables to the new context, and upon eval, doesn't change the original function definitions - SOOO.. I am just using plain "eval" and IT WORKS.

Do you have any comments regarding this?

Q3: In order to re-eval a function, it needs to be a GLOBAL function - Is it bad practice to have all function definitions as global (clojure-like) ? Do you think there is another way to do this?

like image 683
ayal gelles Avatar asked Jan 31 '11 02:01

ayal gelles


2 Answers

Actually, swank.js is getting much better, and it is now much easier to set up swank js with your project using NPM. I'm in the process of writing the documentation right now, but the functionality is there!

like image 176
Jonathan Arkell Avatar answered Nov 15 '22 22:11

Jonathan Arkell


Check this out http://nodejs.org/api/vm.html

var util = require('util'),
vm = require('vm'),
sandbox = {
  animal: 'cat',
  count: 2
};

vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
console.log(util.inspect(sandbox));

// { animal: 'cat', count: 3, name: 'kitty' }

Should help you a lot, all of the sandbox things for node uses it :) but you can use it directly :)

like image 35
Jakub Oboza Avatar answered Nov 15 '22 22:11

Jakub Oboza