node.js - 评估实时进程

发布于 2024-10-14 22:31:58 字数 1200 浏览 5 评论 0原文

有没有人使用现有的东西为自己设置类似的东西 Node.js REPL?我没有想到快速的方法。

我今天的做法是使用 emacs 和这个: https://github.com/ivan4th/swank-js

该模块由以下部分组成:

  1. emacs 的 SLIME-js 插件,与 js2-mode 结合,让 您只需在函数 def 主体的某处发出 CMx - 并且 off 将函数的字符串转到 ..

  2. Swank-js 服务器(是的,您可以从本地计算机进行评估 直接到远程进程)用 Node.js 编写 - 它接收 您评估的函数的字符串并实际评估它

  3. 允许您连接到该服务器上另一个端口的整个部分 使用您的浏览器,然后让您在该浏览器上操作 DOM (这非常令人惊奇,但不相关)

我的解决方案在 emacs 端使用 SLIME-js 并且我需要('swank- 我的 app.js 文件中包含了 .js')

现在..我有几个关于我的解决方案的问题或疑问 其他可能的:

Q1:这样是否太过分了?有人有评估东西的秘密方法吗 从纳米到他的现场过程?

Q2:我必须改变 swank-js 的 EVALing 方式..它使用了一些 像这样的黑魔法:


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");

据我所知,它只是将全局变量复制到 新的上下文,并在评估后,不会改变原始函数 定义-SOOO..我只是使用简单的“eval”和IT 作品。

您对此有何评论?

Q3:为了重新评估一个函数,它需要是一个 GLOBAL 函数 - 将所有函数定义设为全局(类似 clojure)是一种不好的做法吗?您认为还有其他方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2024-10-21 22:31:59

看看这个 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' }

应该对你有很大帮助,节点的所有沙盒事物都使用它:)但您可以直接使用它:)

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

紫罗兰の梦幻 2024-10-21 22:31:59

您可以看一下 jsapp.us,它在沙箱中运行 JS,然后将其作为一个快速的小型测试服务器公开给世界。这是 jsapp.us github 存储库

另外,请进入 #node.js 并提出问题,以便更快地了解回复 :)

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

Saygoodbye 2024-10-21 22:31:58

实际上,swank.js 正在变得越来越好,现在使用 NPM 在您的项目中设置 swank js 变得更加容易。我现在正在编写文档,但功能已经存在了!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文