diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 69439c2fead..6da3eea9cb1 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4902,7 +4902,8 @@ JS_DecompileScript(JSContext *cx, JSScript *script, const char *name, CHECK_REQUEST(cx); jp = JS_NEW_PRINTER(cx, name, NULL, indent & ~JS_DONT_PRETTY_PRINT, - !(indent & JS_DONT_PRETTY_PRINT)); + !(indent & JS_DONT_PRETTY_PRINT), + false); if (!jp) return NULL; if (js_DecompileScript(jp, script)) @@ -4916,41 +4917,21 @@ JS_DecompileScript(JSContext *cx, JSScript *script, const char *name, JS_PUBLIC_API(JSString *) JS_DecompileFunction(JSContext *cx, JSFunction *fun, uintN indent) { - JSPrinter *jp; - JSString *str; - CHECK_REQUEST(cx); - jp = JS_NEW_PRINTER(cx, "JS_DecompileFunction", fun, - indent & ~JS_DONT_PRETTY_PRINT, - !(indent & JS_DONT_PRETTY_PRINT)); - if (!jp) - return NULL; - if (js_DecompileFunction(jp)) - str = js_GetPrinterOutput(jp); - else - str = NULL; - js_DestroyPrinter(jp); - return str; + return js_DecompileToString(cx, "JS_DecompileFunction", fun, + indent & ~JS_DONT_PRETTY_PRINT, + !(indent & JS_DONT_PRETTY_PRINT), + false, js_DecompileFunction); } JS_PUBLIC_API(JSString *) JS_DecompileFunctionBody(JSContext *cx, JSFunction *fun, uintN indent) { - JSPrinter *jp; - JSString *str; - CHECK_REQUEST(cx); - jp = JS_NEW_PRINTER(cx, "JS_DecompileFunctionBody", fun, - indent & ~JS_DONT_PRETTY_PRINT, - !(indent & JS_DONT_PRETTY_PRINT)); - if (!jp) - return NULL; - if (js_DecompileFunctionBody(jp)) - str = js_GetPrinterOutput(jp); - else - str = NULL; - js_DestroyPrinter(jp); - return str; + return js_DecompileToString(cx, "JS_DecompileFunctionBody", fun, + indent & ~JS_DONT_PRETTY_PRINT, + !(indent & JS_DONT_PRETTY_PRINT), + false, js_DecompileFunctionBody); } JS_PUBLIC_API(JSBool) diff --git a/js/src/jsopcode.cpp b/js/src/jsopcode.cpp index 492f780ef0e..a0b818e7436 100644 --- a/js/src/jsopcode.cpp +++ b/js/src/jsopcode.cpp @@ -733,17 +733,9 @@ struct JSPrinter { jsuword *localNames; /* argument and variable names */ }; -/* - * Hack another flag, a la JS_DONT_PRETTY_PRINT, into uintN indent parameters - * to functions such as js_DecompileFunction and js_NewPrinter. This time, as - * opposed to JS_DONT_PRETTY_PRINT back in the dark ages, we can assume that a - * uintN is at least 32 bits. - */ -#define JS_IN_GROUP_CONTEXT 0x10000 - JSPrinter * JS_NEW_PRINTER(JSContext *cx, const char *name, JSFunction *fun, - uintN indent, JSBool pretty) + uintN indent, bool pretty, bool grouped) { JSPrinter *jp; @@ -752,9 +744,9 @@ JS_NEW_PRINTER(JSContext *cx, const char *name, JSFunction *fun, return NULL; INIT_SPRINTER(cx, &jp->sprinter, &jp->pool, 0); JS_INIT_ARENA_POOL(&jp->pool, name, 256, 1, &cx->scriptStackQuota); - jp->indent = indent & ~JS_IN_GROUP_CONTEXT; + jp->indent = indent; jp->pretty = pretty; - jp->grouped = (indent & JS_IN_GROUP_CONTEXT) != 0; + jp->grouped = grouped; jp->script = NULL; jp->dvgfence = NULL; jp->pcstack = NULL; @@ -2252,7 +2244,7 @@ Decompile(SprintStack *ss, jsbytecode *pc, intN nb, JSOp nextop) do_function: js_puts(jp, "\n"); jp2 = JS_NEW_PRINTER(cx, "nested_function", fun, - jp->indent, jp->pretty); + jp->indent, jp->pretty, jp->grouped); if (!jp2) return NULL; ok = js_DecompileFunction(jp2); @@ -4145,17 +4137,16 @@ Decompile(SprintStack *ss, jsbytecode *pc, intN nb, JSOp nextop) LOAD_FUNCTION(0); { - uintN indent = JS_DONT_PRETTY_PRINT; - /* * Always parenthesize expression closures. We can't force * saveop to a low-precedence op to arrange for auto-magic * parenthesization without confusing getter/setter code * that checks for JSOP_LAMBDA. */ - if (!(fun->flags & JSFUN_EXPR_CLOSURE)) - indent |= JS_IN_GROUP_CONTEXT; - str = JS_DecompileFunction(cx, fun, indent); + bool grouped = !(fun->flags & JSFUN_EXPR_CLOSURE); + str = js_DecompileToString(cx, "lambda", fun, 0, + false, grouped, + js_DecompileFunction); if (!str) return NULL; } @@ -4909,9 +4900,28 @@ js_DecompileScript(JSPrinter *jp, JSScript *script) return DecompileCode(jp, script, script->code, (uintN)script->length, 0); } +JSString * +js_DecompileToString(JSContext *cx, const char *name, JSFunction *fun, + uintN indent, bool pretty, bool grouped, + bool (*decompiler)(JSPrinter *jp)) +{ + JSPrinter *jp; + JSString *str; + + jp = JS_NEW_PRINTER(cx, name, fun, indent, pretty, grouped); + if (!jp) + return NULL; + if (decompiler(jp)) + str = js_GetPrinterOutput(jp); + else + str = NULL; + js_DestroyPrinter(jp); + return str; +} + static const char native_code_str[] = "\t[native code]\n"; -JSBool +bool js_DecompileFunctionBody(JSPrinter *jp) { JSScript *script; @@ -4927,7 +4937,7 @@ js_DecompileFunctionBody(JSPrinter *jp) return DecompileCode(jp, script, script->code, (uintN)script->length, 0); } -JSBool +bool js_DecompileFunction(JSPrinter *jp) { JSFunction *fun; @@ -5296,7 +5306,8 @@ DecompileExpression(JSContext *cx, JSScript *script, JSFunction *fun, } name = NULL; - jp = JS_NEW_PRINTER(cx, "js_DecompileValueGenerator", fun, 0, JS_FALSE); + jp = JS_NEW_PRINTER(cx, "js_DecompileValueGenerator", fun, 0, + false, false); if (jp) { jp->dvgfence = end; jp->pcstack = pcstack; diff --git a/js/src/jsopcode.h b/js/src/jsopcode.h index a00d066b65f..4bbeb07475d 100644 --- a/js/src/jsopcode.h +++ b/js/src/jsopcode.h @@ -276,16 +276,16 @@ js_QuoteString(JSContext *cx, JSString *str, jschar quote); */ #ifdef JS_ARENAMETER -# define JS_NEW_PRINTER(cx, name, fun, indent, pretty) \ - js_NewPrinter(cx, name, fun, indent, pretty) +# define JS_NEW_PRINTER(cx, name, fun, indent, pretty, grouped) \ + js_NewPrinter(cx, name, fun, indent, pretty, grouped) #else -# define JS_NEW_PRINTER(cx, name, fun, indent, pretty) \ - js_NewPrinter(cx, fun, indent, pretty) +# define JS_NEW_PRINTER(cx, name, fun, indent, pretty, grouped) \ + js_NewPrinter(cx, fun, indent, pretty, grouped) #endif extern JSPrinter * JS_NEW_PRINTER(JSContext *cx, const char *name, JSFunction *fun, - uintN indent, JSBool pretty); + uintN indent, bool pretty, bool grouped); extern void js_DestroyPrinter(JSPrinter *jp); @@ -421,12 +421,17 @@ js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc, extern JSBool js_DecompileScript(JSPrinter *jp, JSScript *script); -extern JSBool +extern bool js_DecompileFunctionBody(JSPrinter *jp); -extern JSBool +extern bool js_DecompileFunction(JSPrinter *jp); +extern JSString * +js_DecompileToString(JSContext *cx, const char *name, JSFunction *fun, + uintN indent, bool pretty, bool grouped, + bool (*decompiler)(JSPrinter *jp)); + /* * Find the source expression that resulted in v, and return a newly allocated * C-string containing it. Fall back on v's string conversion (fallback) if we