Bug 17531

Summary: Add interactive mode to testkjs
Product: WebKit Reporter: Sam Weinig <sam>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: mrowe
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Macintosh   
OS: OS X 10.5   
Attachments:
Description Flags
First stab at it
none
Proposed patch darin: review+

Description Sam Weinig 2008-02-25 00:28:36 PST
Adding an interactive shell to testkjs seems like the next step.  Let's do it!
Comment 1 Sam Weinig 2008-02-25 00:35:40 PST
Created attachment 19341 [details]
First stab at it

Here is a first stab at an interactive shell mode.  If the platform you are on has readline support (currently I only have it set for Darwin, since I know it does, but the automake platforms can be made to check for it too) I use it and make use of the history, otherwise we just use getchar.  The way interactive mode works with files is that all the files are run first, and if they succeed, we go into interactive mode.

There are some names I don't especially like in this patch (the Options struct for one, not really sure we even need it) and I think it has gotten to the point where we need a more fleshed out usage message.  All in all though, this is pretty close.
Comment 2 Sam Weinig 2008-02-25 00:40:13 PST
Another feature I would like to add to the interactive mode, though not necessarily in this first version, is good autocompletion support (js keywords, variables in scope).  This can be done rather easily using readline.

It also just occurred to me.  Does it make sense to enter interactive mode if -p has been supplied.
Comment 3 Mark Rowe (bdash) 2008-02-25 04:01:20 PST
+#if HAVE(asREADLINE)

I wonder if the default behaviour for running testkjs with no arguments should be to drop into the interactive shell?  With your patch it doesn't seem possible to get into interactive mode without executing code from some file first.  That may be a useful mode for inspecting the state of a completed script, but based on experiences using Python's interactive interpreter I feel it may be less commonly used than interactive mode without executing any code from a file.
Comment 4 Cameron Zwarich (cpst) 2008-06-09 01:20:57 PDT
Created attachment 21590 [details]
Proposed patch

Here is a cleaned up version of Sam's patch. I made the prompt string the same as SpiderMonkey's, and I made it enter interactive mode if no files are specified.
Comment 5 Darin Adler 2008-06-09 06:51:08 PDT
Comment on attachment 21590 [details]
Proposed patch

+static const char* interactivePrompt = "> ";

This should be static const char[] or static const char* const. The declaration as-is asks for a non-const global pointing to a const string.

+    fprintf(stderr, "  -p         Prints formatted source code\n");

I think this option is a little silly now that it does no formatting and just spits the code out as-is.

r=me
Comment 6 Mark Rowe (bdash) 2008-06-09 09:43:15 PDT
-p still seems to do a relatively good job.  Oliver explained to me why it may give worse results than before on some input, but in my quick test with some crazy-obfuscated gmail source code it gave reasonable results.
Comment 7 Cameron Zwarich (cpst) 2008-06-09 10:02:27 PDT
(In reply to comment #5)
> (From update of attachment 21590 [details] [edit])
> +static const char* interactivePrompt = "> ";
> 
> This should be static const char[] or static const char* const. The declaration
> as-is asks for a non-const global pointing to a const string.

Thanks. I meant to make it a static const char[].
Comment 8 Cameron Zwarich (cpst) 2008-06-09 10:12:01 PDT
Landed in r34470.