Subj : Re: Compacting JS source via ParseTree or Decompile To : Dan Libby From : Brendan Eich Date : Sat Jun 05 2004 05:39 pm Dan Libby wrote: > When I first saw the API docs I was delighted to see > JS_DecompileScript(). But on further reading it looks like that won't > print the functions, and I have to somehow already have a list of > JSFunction...? No, a script's function declarations will be properly decompiled. Here's a js shell example: js> void(s = Script("function f() {\n" + " return 42;\n" + "}\n" + "function g(x, y) {\n" + " return x * y;\n" + "}\n" + "print('hi!');\n")) js> s function f() { return 42; } function g(x, y) { return x * y; } print("hi!"); js> uneval(s) (new Script('\nfunction f() {\n return 42;\n}\n\n\nfunction g(x, y) {\n return x * y;\n}\n\nprint("hi!");\n')) Note that uneval on a Script object calls toSource, which compresses unprettily; whereas the shell calls toString, which pretty-prints. > Further, I'm not clear if comments/whitespace are > preserved in any fashion in the ByteCode. I would imagine not, which is > good. No comment or whitespace is preserved, but extra parentheses are. > I saw a post from Brendan with a sketch for using js_ParseTokenStream(). > [1] This seems like it is probably the best option as it gives me > more control over the output, but does this also do syntax checking? Yes, as the Parse in the function name implies. /be .