Nodejs modifying child process's context/global scope

830 views
Skip to first unread message

Vincent Hagen

unread,
Sep 28, 2013, 10:12:37 AM9/28/13
to nod...@googlegroups.com

Is there a way to modify the context or global scope of a spawned (or to be spawned) child process?

The idea is that I spawn a child process and executes another js file. But I want to add some functionalities for easy usage. So either I change the context in which the js file is ran to an object with the extra functionalities, or I change its global scope by adding functions and objects to it.

An important side note, I don’t want any of these features to be added in the separate js file. The js separate file(s) is/are from end user, and I want to make life easy for them by adding standard functions they can use to interact with a more complex system.


Thanks in advanced,
Vincent

Angel Java Lopez

unread,
Sep 28, 2013, 11:23:59 AM9/28/13
to nod...@googlegroups.com
First idea come to my mind:

see how jasmine-node do it


It builds a new context, read the file to run, and use
var vm = require('vm');
vm.runInNewContext(wrappedScript, newContext, s.path());

But you need a child process... ummm.
Maybe, there is a simpler solution

Angel "Java" Lopez
@ajlopez



--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Vincent Hagen

unread,
Sep 29, 2013, 4:18:44 AM9/29/13
to nod...@googlegroups.com
I was thinking about VM, but their are several problems with that solution.
 - First, its pretty slow because al the code is evaluated each time. (but you can make it faster by precompiling it the user code, than I need to do some bookkeeping)
 - Second, kinda overkill for what I am trying
 - Last, I know this is possible, because I have done it in C before. The only problem is, if you call upon fork in C, you get 2 processes (one parent and one child) from on that point in your code.

The nodeJs fork, does something completely different, you execute a file as a new child process. This is more like a Popen call.
If I could fork in the same file, as in with C. I could just check if it was the child, than set some global variables and run the user code. Else set some event handlers for the child process.

But as I said earlier, this is not how the nodeJs fork works unfortunately.

Thank you for your suggestion anyway

Op zaterdag 28 september 2013 17:23:59 UTC+2 schreef ajlopez:

greelgorke

unread,
Sep 30, 2013, 2:57:30 AM9/30/13
to nod...@googlegroups.com
you could implement a bootstrap script, which implements your standard functions. then you fork it, passing by the path to the user js file. your bootstrap script requires it then and executes. or do vm with context enhancement.
Reply all
Reply to author
Forward
0 new messages