I've been attempting to run jslint from the commandline via spidermonkey. I came up with a silly little hack by which I could simply cat a javascript file at it, by putting it into a readline(); loop, but I wanted to try my hand at writing a small script that could potentially pass multiple files into a single instance of spidermonkey (and also to allow for argument parsing and such).
For whatever reason, I wasn't able to build the CPAN Javascript module (and, by extension, Javascript::JSLint), so I thought I'd try my hand at a simple little script and, in the meantime, increase my understanding of Open2/3.
So, I wrote the following script to test it out:
#!/usr/bin/perl use IPC::Open2; use strict; my ($JSWRITE, $JSREAD); my $pid = open2($JSREAD, $JSWRITE, "js"); print $JSWRITE "load('jslint.js');\n"; #INPUT INTO JSLINT open FILE, '<', "test.js"; while (<FILE>) { print $JSWRITE "$_\n"; } close(FILE); #END INPUT print $JSWRITE "END\n"; close($JSWRITE); waitpid($pid, 0); my @output=<$JSREAD>; close($JSREAD); print join ("\n", @output);
And, in jslint.js, I put it into a readline(); loop like this:
var input=""; var line=""; while (true){ line=readline(); if (line=="END") break; if (line!="") { input += line; input += "\n"; } }
After which, it calls the function to scan the text in 'input' for errors.
And it doesn't work. And I'm pretty sure I know why, but I'd like to know for sure and also to increase my understanding of what's going on behind the scenes here and the particulars of the blocking.
So, I know that the problem lies in the readline(); loop. I know this because I can do something like the following in the perl script:
print $JSWRITE "var foo='bar';\n"; print $JSWRITE "print(foo);\n"; close($JSWRITE); waitpid($pid, 0); my @output=<$JSREAD>; close($JSREAD); print join ("\n", @output);
And I get 'bar'. So, my understanding is that the interpreter is properly signaling that it's ready for input, and then $JSWRITE is unlocked, and everything can continue.
So my guess is that, when my hacked version of jslint.js tells the interpreter to enter into that readline() loop, that signal stops getting sent, but I don't know the particulars of it, or what I can do to diagnose or prove that.
I'm sure there's a better way to do all of this. I could use an interpeter (like Rhino), that has support for reading entire files in by one command (or I could compile it into spidermonkey). I could also read the file in a line at a time by doing something like:
print $JSWRITE "input += '$escaped_line_of_file';\n";
But, at this point, I'm just trying to increase my understanding of IPC, and how to get more information about similar problems,
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |