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,


In reply to Diagnosing blocking io (or: finding the WHY of my Open2 woes). by Socrates

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.