开发者

node.js - eval'ing to a live process

开发者 https://www.devze.com 2023-02-07 09:08 出处:网络
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.

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?


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!


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 :)


You might take a look at jsapp.us, which runs JS in a sandbox, and then exposes that to the world as a quick little test server. Here's the jsapp.us github repo.

Also, stop into #node.js and ask questions for a quicker response :)

0

精彩评论

暂无评论...
验证码 换一张
取 消