2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2008-08-03 01:34:07 -07:00
|
|
|
* vim: set sw=4 ts=8 et tw=99:
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS bytecode descriptors, disassemblers, and decompilers.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_MEMORY_H
|
|
|
|
#include <memory.h>
|
|
|
|
#endif
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsarena.h" /* Added by JSIFY */
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsdtoa.h"
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "jsemit.h"
|
|
|
|
#include "jsfun.h"
|
2008-07-01 18:59:18 -07:00
|
|
|
#include "jsiter.h"
|
2008-10-15 14:02:33 -07:00
|
|
|
#include "jsnum.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
|
|
|
#include "jsregexp.h"
|
|
|
|
#include "jsscan.h"
|
|
|
|
#include "jsscope.h"
|
|
|
|
#include "jsscript.h"
|
|
|
|
#include "jsstr.h"
|
2008-09-08 10:51:10 -07:00
|
|
|
#include "jsstaticcheck.h"
|
2008-10-29 21:56:35 -07:00
|
|
|
#include "jstracer.h"
|
2009-08-18 15:43:17 -07:00
|
|
|
#include "jsvector.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-09 16:52:55 -07:00
|
|
|
#include "jsscriptinlines.h"
|
|
|
|
|
2008-05-23 23:44:08 -07:00
|
|
|
#include "jsautooplen.h"
|
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2009-03-05 03:12:50 -08:00
|
|
|
/*
|
|
|
|
* Index limit must stay within 32 bits.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(sizeof(uint32) * JS_BITS_PER_BYTE >= INDEX_LIMIT_LOG2 + 1);
|
|
|
|
|
2008-05-23 23:44:08 -07:00
|
|
|
/* Verify JSOP_XXX_LENGTH constant definitions. */
|
|
|
|
#define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
|
|
|
|
JS_STATIC_ASSERT(op##_LENGTH == length);
|
|
|
|
#include "jsopcode.tbl"
|
|
|
|
#undef OPDEF
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char js_incop_strs[][3] = {"++", "--"};
|
2008-10-22 12:47:51 -07:00
|
|
|
static const char js_for_each_str[] = "for each";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-06 03:50:07 -07:00
|
|
|
const JSCodeSpec js_CodeSpec[] = {
|
|
|
|
#define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
|
|
|
|
{length,nuses,ndefs,prec,format},
|
|
|
|
#include "jsopcode.tbl"
|
|
|
|
#undef OPDEF
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-06 03:50:07 -07:00
|
|
|
uintN js_NumCodeSpecs = JS_ARRAY_LENGTH(js_CodeSpec);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each element of the array is either a source literal associated with JS
|
|
|
|
* bytecode or null.
|
|
|
|
*/
|
|
|
|
static const char *CodeToken[] = {
|
2007-03-22 10:30:00 -07:00
|
|
|
#define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
|
2007-05-06 03:50:07 -07:00
|
|
|
token,
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsopcode.tbl"
|
|
|
|
#undef OPDEF
|
|
|
|
};
|
|
|
|
|
2008-10-29 21:56:35 -07:00
|
|
|
#if defined(DEBUG) || defined(JS_JIT_SPEW)
|
2007-05-06 03:50:07 -07:00
|
|
|
/*
|
2008-10-29 21:56:35 -07:00
|
|
|
* Array of JS bytecode names used by DEBUG-only js_Disassemble and by
|
|
|
|
* JIT debug spew.
|
2007-05-06 03:50:07 -07:00
|
|
|
*/
|
2008-02-28 16:29:59 -08:00
|
|
|
const char *js_CodeName[] = {
|
2007-05-06 03:50:07 -07:00
|
|
|
#define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
|
|
|
|
name,
|
|
|
|
#include "jsopcode.tbl"
|
|
|
|
#undef OPDEF
|
|
|
|
};
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static ptrdiff_t
|
|
|
|
GetJumpOffset(jsbytecode *pc, jsbytecode *pc2)
|
|
|
|
{
|
|
|
|
uint32 type;
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
type = JOF_OPTYPE(*pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (JOF_TYPE_IS_EXTENDED_JUMP(type))
|
|
|
|
return GET_JUMPX_OFFSET(pc2);
|
|
|
|
return GET_JUMP_OFFSET(pc2);
|
|
|
|
}
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
uintN
|
2008-02-17 17:32:12 -08:00
|
|
|
js_GetIndexFromBytecode(JSContext *cx, JSScript *script, jsbytecode *pc,
|
|
|
|
ptrdiff_t pcoff)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSOp op;
|
2007-07-08 02:03:34 -07:00
|
|
|
uintN span, base;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-18 16:31:09 -08:00
|
|
|
op = js_GetOpcode(cx, script, pc);
|
2007-07-08 02:03:34 -07:00
|
|
|
JS_ASSERT(js_CodeSpec[op].length >= 1 + pcoff + UINT16_LEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2007-07-08 02:03:34 -07:00
|
|
|
* We need to detect index base prefix. It presents when resetbase
|
2007-03-22 10:30:00 -07:00
|
|
|
* follows the bytecode.
|
|
|
|
*/
|
|
|
|
span = js_CodeSpec[op].length;
|
2007-07-08 02:03:34 -07:00
|
|
|
base = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pc - script->code + span < script->length) {
|
|
|
|
if (pc[span] == JSOP_RESETBASE) {
|
2007-07-08 02:03:34 -07:00
|
|
|
base = GET_INDEXBASE(pc - JSOP_INDEXBASE_LENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (pc[span] == JSOP_RESETBASE0) {
|
2007-07-08 02:03:34 -07:00
|
|
|
JS_ASSERT(JSOP_INDEXBASE1 <= pc[-1] || pc[-1] <= JSOP_INDEXBASE3);
|
|
|
|
base = (pc[-1] - JSOP_INDEXBASE1 + 1) << 16;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
return base + GET_UINT16(pc + pcoff);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-02 17:19:41 -07:00
|
|
|
uintN
|
2008-07-20 13:13:17 -07:00
|
|
|
js_GetVariableBytecodeLength(jsbytecode *pc)
|
2008-06-02 17:19:41 -07:00
|
|
|
{
|
2008-07-20 13:13:17 -07:00
|
|
|
JSOp op;
|
|
|
|
uintN jmplen, ncases;
|
|
|
|
jsint low, high;
|
2008-06-02 17:19:41 -07:00
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
op = (JSOp) *pc;
|
|
|
|
JS_ASSERT(js_CodeSpec[op].length == -1);
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_TABLESWITCHX:
|
|
|
|
jmplen = JUMPX_OFFSET_LEN;
|
|
|
|
goto do_table;
|
|
|
|
case JSOP_TABLESWITCH:
|
|
|
|
jmplen = JUMP_OFFSET_LEN;
|
|
|
|
do_table:
|
|
|
|
/* Structure: default-jump case-low case-high case1-jump ... */
|
|
|
|
pc += jmplen;
|
|
|
|
low = GET_JUMP_OFFSET(pc);
|
|
|
|
pc += JUMP_OFFSET_LEN;
|
|
|
|
high = GET_JUMP_OFFSET(pc);
|
|
|
|
ncases = (uintN)(high - low + 1);
|
|
|
|
return 1 + jmplen + INDEX_LEN + INDEX_LEN + ncases * jmplen;
|
|
|
|
|
|
|
|
case JSOP_LOOKUPSWITCHX:
|
|
|
|
jmplen = JUMPX_OFFSET_LEN;
|
|
|
|
goto do_lookup;
|
2008-06-02 17:19:41 -07:00
|
|
|
default:
|
2008-07-20 13:13:17 -07:00
|
|
|
JS_ASSERT(op == JSOP_LOOKUPSWITCH);
|
|
|
|
jmplen = JUMP_OFFSET_LEN;
|
|
|
|
do_lookup:
|
|
|
|
/* Structure: default-jump case-count (case1-value case1-jump) ... */
|
|
|
|
pc += jmplen;
|
|
|
|
ncases = GET_UINT16(pc);
|
|
|
|
return 1 + jmplen + INDEX_LEN + ncases * (INDEX_LEN + jmplen);
|
2008-06-02 17:19:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 23:25:15 -07:00
|
|
|
uintN
|
2009-03-30 07:19:50 -07:00
|
|
|
js_GetVariableStackUses(JSOp op, jsbytecode *pc)
|
2008-09-01 23:25:15 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(*pc == op || *pc == JSOP_TRAP);
|
|
|
|
JS_ASSERT(js_CodeSpec[op].nuses == -1);
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_POPN:
|
|
|
|
return GET_UINT16(pc);
|
2009-08-18 15:43:17 -07:00
|
|
|
case JSOP_CONCATN:
|
|
|
|
return GET_UINT16(pc);
|
2008-09-01 23:25:15 -07:00
|
|
|
case JSOP_LEAVEBLOCK:
|
|
|
|
return GET_UINT16(pc);
|
|
|
|
case JSOP_LEAVEBLOCKEXPR:
|
|
|
|
return GET_UINT16(pc) + 1;
|
|
|
|
case JSOP_NEWARRAY:
|
2009-04-10 17:41:13 -07:00
|
|
|
return GET_UINT16(pc);
|
2008-09-01 23:25:15 -07:00
|
|
|
default:
|
|
|
|
/* stack: fun, this, [argc arguments] */
|
|
|
|
JS_ASSERT(op == JSOP_NEW || op == JSOP_CALL ||
|
2008-10-29 13:30:41 -07:00
|
|
|
op == JSOP_EVAL || op == JSOP_SETCALL ||
|
|
|
|
op == JSOP_APPLY);
|
2008-09-01 23:25:15 -07:00
|
|
|
return 2 + GET_ARGC(pc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-30 07:19:50 -07:00
|
|
|
uintN
|
|
|
|
js_GetEnterBlockStackDefs(JSContext *cx, JSScript *script, jsbytecode *pc)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
JS_ASSERT(*pc == JSOP_ENTERBLOCK || *pc == JSOP_TRAP);
|
|
|
|
GET_OBJECT_FROM_BYTECODE(script, pc, 0, obj);
|
|
|
|
return OBJ_BLOCK_COUNT(cx, obj);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2010-05-18 14:34:25 -07:00
|
|
|
/* If pc != NULL, includes a prefix indicating whether the PC is at the current line. */
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_FRIEND_API(JSBool)
|
2010-05-18 14:34:25 -07:00
|
|
|
js_DisassembleAtPC(JSContext *cx, JSScript *script, JSBool lines, FILE *fp, jsbytecode *pc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-18 14:34:25 -07:00
|
|
|
jsbytecode *next, *end;
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN len;
|
|
|
|
|
2010-05-18 14:34:25 -07:00
|
|
|
next = script->code;
|
|
|
|
end = next + script->length;
|
|
|
|
while (next < end) {
|
|
|
|
if (next == script->main)
|
2007-03-22 10:30:00 -07:00
|
|
|
fputs("main:\n", fp);
|
2010-05-18 14:34:25 -07:00
|
|
|
if (pc != NULL) {
|
|
|
|
if (pc == next)
|
|
|
|
fputs("--> ", fp);
|
|
|
|
else
|
|
|
|
fputs(" ", fp);
|
|
|
|
}
|
|
|
|
len = js_Disassemble1(cx, script, next,
|
|
|
|
next - script->code,
|
2007-03-22 10:30:00 -07:00
|
|
|
lines, fp);
|
|
|
|
if (!len)
|
|
|
|
return JS_FALSE;
|
2010-05-18 14:34:25 -07:00
|
|
|
next += len;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-05-18 14:34:25 -07:00
|
|
|
JS_FRIEND_API(JSBool)
|
|
|
|
js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp)
|
|
|
|
{
|
|
|
|
return js_DisassembleAtPC(cx, script, lines, fp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(JSBool)
|
|
|
|
js_DumpPC(JSContext *cx)
|
|
|
|
{
|
|
|
|
return js_DisassembleAtPC(cx, cx->fp->script, true, stdout, cx->regs->pc);
|
|
|
|
}
|
|
|
|
|
2009-02-28 16:21:18 -08:00
|
|
|
JSBool
|
|
|
|
js_DumpScript(JSContext *cx, JSScript *script)
|
|
|
|
{
|
|
|
|
return js_Disassemble(cx, script, true, stdout);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *
|
|
|
|
ToDisassemblySource(JSContext *cx, jsval v)
|
|
|
|
{
|
2009-04-04 10:14:52 -07:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(v)) {
|
2009-04-05 21:17:22 -07:00
|
|
|
JSObject *obj = JSVAL_TO_OBJECT(v);
|
2010-04-07 16:09:49 -07:00
|
|
|
JSClass *clasp = obj->getClass();
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
if (clasp == &js_BlockClass) {
|
|
|
|
char *source = JS_sprintf_append(NULL, "depth %d {", OBJ_BLOCK_DEPTH(cx, obj));
|
2010-04-08 11:22:04 -07:00
|
|
|
for (JSScopeProperty *sprop = obj->scope()->lastProperty();
|
2009-04-05 21:17:22 -07:00
|
|
|
sprop;
|
2007-03-22 10:30:00 -07:00
|
|
|
sprop = sprop->parent) {
|
2009-04-05 21:17:22 -07:00
|
|
|
const char *bytes = js_AtomToPrintableString(cx, JSID_TO_ATOM(sprop->id));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!bytes)
|
|
|
|
return NULL;
|
|
|
|
source = JS_sprintf_append(source, "%s: %d%s",
|
|
|
|
bytes, sprop->shortid,
|
|
|
|
sprop->parent ? ", " : "");
|
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
source = JS_sprintf_append(source, "}");
|
|
|
|
if (!source)
|
|
|
|
return NULL;
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
JSString *str = JS_NewString(cx, source, strlen(source));
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
return js_GetStringBytes(cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clasp == &js_FunctionClass) {
|
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, obj);
|
|
|
|
JSString *str = JS_DecompileFunction(cx, fun, JS_DONT_PRETTY_PRINT);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
return js_GetStringBytes(cx, str);
|
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
|
|
|
if (clasp == &js_RegExpClass) {
|
2010-03-29 19:47:40 -07:00
|
|
|
AutoValueRooter tvr(cx);
|
2009-04-05 21:17:22 -07:00
|
|
|
if (!js_regexp_toString(cx, obj, tvr.addr()))
|
|
|
|
return NULL;
|
|
|
|
return js_GetStringBytes(cx, JSVAL_TO_STRING(tvr.value()));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return js_ValueToPrintableSource(cx, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(uintN)
|
|
|
|
js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc,
|
|
|
|
uintN loc, JSBool lines, FILE *fp)
|
|
|
|
{
|
|
|
|
JSOp op;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
ptrdiff_t len, off, jmplen;
|
|
|
|
uint32 type;
|
|
|
|
JSAtom *atom;
|
2007-07-08 02:03:34 -07:00
|
|
|
uintN index;
|
|
|
|
JSObject *obj;
|
|
|
|
jsval v;
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *bytes;
|
2007-07-11 02:25:45 -07:00
|
|
|
jsint i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
op = (JSOp)*pc;
|
|
|
|
if (op >= JSOP_LIMIT) {
|
|
|
|
char numBuf1[12], numBuf2[12];
|
|
|
|
JS_snprintf(numBuf1, sizeof numBuf1, "%d", op);
|
|
|
|
JS_snprintf(numBuf2, sizeof numBuf2, "%d", JSOP_LIMIT);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BYTECODE_TOO_BIG, numBuf1, numBuf2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
len = (ptrdiff_t) cs->length;
|
|
|
|
fprintf(fp, "%05u:", loc);
|
|
|
|
if (lines)
|
|
|
|
fprintf(fp, "%4u", JS_PCToLineNumber(cx, script, pc));
|
2008-02-28 16:29:59 -08:00
|
|
|
fprintf(fp, " %s", js_CodeName[op]);
|
2008-02-07 15:18:45 -08:00
|
|
|
type = JOF_TYPE(cs->format);
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (type) {
|
|
|
|
case JOF_BYTE:
|
|
|
|
if (op == JSOP_TRAP) {
|
|
|
|
op = JS_GetTrapOpcode(cx, script, pc);
|
|
|
|
len = (ptrdiff_t) js_CodeSpec[op].length;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOF_JUMP:
|
|
|
|
case JOF_JUMPX:
|
|
|
|
off = GetJumpOffset(pc, pc);
|
2008-07-29 07:11:36 -07:00
|
|
|
fprintf(fp, " %u (%d)", loc + (intN) off, (intN) off);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2007-08-08 06:35:05 -07:00
|
|
|
case JOF_ATOM:
|
2007-07-08 02:03:34 -07:00
|
|
|
case JOF_OBJECT:
|
|
|
|
case JOF_REGEXP:
|
2008-02-17 17:32:12 -08:00
|
|
|
index = js_GetIndexFromBytecode(cx, script, pc, 0);
|
2007-08-08 06:35:05 -07:00
|
|
|
if (type == JOF_ATOM) {
|
2009-09-04 16:27:17 -07:00
|
|
|
JS_GET_SCRIPT_ATOM(script, pc, index, atom);
|
2007-07-08 02:03:34 -07:00
|
|
|
v = ATOM_KEY(atom);
|
|
|
|
} else {
|
|
|
|
if (type == JOF_OBJECT)
|
2009-09-09 16:52:55 -07:00
|
|
|
obj = script->getObject(index);
|
2007-07-08 02:03:34 -07:00
|
|
|
else
|
2009-09-09 16:52:55 -07:00
|
|
|
obj = script->getRegExp(index);
|
2007-07-08 02:03:34 -07:00
|
|
|
v = OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
|
|
|
bytes = ToDisassemblySource(cx, v);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
fprintf(fp, " %s", bytes);
|
|
|
|
break;
|
|
|
|
|
2009-09-09 20:21:15 -07:00
|
|
|
case JOF_UINT16PAIR:
|
|
|
|
i = (jsint)GET_UINT16(pc);
|
|
|
|
fprintf(fp, " %d", i);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JOF_UINT16:
|
2007-07-11 02:25:45 -07:00
|
|
|
i = (jsint)GET_UINT16(pc);
|
|
|
|
goto print_int;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JOF_TABLESWITCH:
|
|
|
|
case JOF_TABLESWITCHX:
|
|
|
|
{
|
|
|
|
jsbytecode *pc2;
|
|
|
|
jsint i, low, high;
|
|
|
|
|
|
|
|
jmplen = (type == JOF_TABLESWITCH) ? JUMP_OFFSET_LEN
|
|
|
|
: JUMPX_OFFSET_LEN;
|
|
|
|
pc2 = pc;
|
|
|
|
off = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
low = GET_JUMP_OFFSET(pc2);
|
|
|
|
pc2 += JUMP_OFFSET_LEN;
|
|
|
|
high = GET_JUMP_OFFSET(pc2);
|
|
|
|
pc2 += JUMP_OFFSET_LEN;
|
2008-07-29 07:11:36 -07:00
|
|
|
fprintf(fp, " defaultOffset %d low %d high %d", (intN) off, low, high);
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = low; i <= high; i++) {
|
|
|
|
off = GetJumpOffset(pc, pc2);
|
2008-07-29 07:11:36 -07:00
|
|
|
fprintf(fp, "\n\t%d: %d", i, (intN) off);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc2 += jmplen;
|
|
|
|
}
|
|
|
|
len = 1 + pc2 - pc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JOF_LOOKUPSWITCH:
|
|
|
|
case JOF_LOOKUPSWITCHX:
|
|
|
|
{
|
|
|
|
jsbytecode *pc2;
|
|
|
|
jsatomid npairs;
|
|
|
|
|
|
|
|
jmplen = (type == JOF_LOOKUPSWITCH) ? JUMP_OFFSET_LEN
|
|
|
|
: JUMPX_OFFSET_LEN;
|
|
|
|
pc2 = pc;
|
|
|
|
off = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
npairs = GET_UINT16(pc2);
|
|
|
|
pc2 += UINT16_LEN;
|
2008-07-29 07:11:36 -07:00
|
|
|
fprintf(fp, " offset %d npairs %u", (intN) off, (uintN) npairs);
|
2007-03-22 10:30:00 -07:00
|
|
|
while (npairs) {
|
2009-09-04 16:27:17 -07:00
|
|
|
JS_GET_SCRIPT_ATOM(script, pc, GET_INDEX(pc2), atom);
|
2007-07-08 02:03:34 -07:00
|
|
|
pc2 += INDEX_LEN;
|
2007-03-22 10:30:00 -07:00
|
|
|
off = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
|
|
|
|
bytes = ToDisassemblySource(cx, ATOM_KEY(atom));
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
2008-07-29 07:11:36 -07:00
|
|
|
fprintf(fp, "\n\t%s: %d", bytes, (intN) off);
|
2007-03-22 10:30:00 -07:00
|
|
|
npairs--;
|
|
|
|
}
|
|
|
|
len = 1 + pc2 - pc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JOF_QARG:
|
|
|
|
fprintf(fp, " %u", GET_ARGNO(pc));
|
|
|
|
break;
|
|
|
|
|
2008-08-06 07:13:22 -07:00
|
|
|
case JOF_LOCAL:
|
2008-07-20 13:13:17 -07:00
|
|
|
fprintf(fp, " %u", GET_SLOTNO(pc));
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2007-08-08 06:35:05 -07:00
|
|
|
case JOF_SLOTATOM:
|
|
|
|
case JOF_SLOTOBJECT:
|
2008-07-20 13:13:17 -07:00
|
|
|
fprintf(fp, " %u", GET_SLOTNO(pc));
|
|
|
|
index = js_GetIndexFromBytecode(cx, script, pc, SLOTNO_LEN);
|
2007-09-16 11:23:39 -07:00
|
|
|
if (type == JOF_SLOTATOM) {
|
2009-09-04 16:27:17 -07:00
|
|
|
JS_GET_SCRIPT_ATOM(script, pc, index, atom);
|
2007-07-08 02:03:34 -07:00
|
|
|
v = ATOM_KEY(atom);
|
|
|
|
} else {
|
2009-09-09 16:52:55 -07:00
|
|
|
obj = script->getObject(index);
|
2007-07-08 02:03:34 -07:00
|
|
|
v = OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
|
|
|
bytes = ToDisassemblySource(cx, v);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
fprintf(fp, " %s", bytes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOF_UINT24:
|
2008-06-04 17:00:46 -07:00
|
|
|
JS_ASSERT(op == JSOP_UINT24 || op == JSOP_NEWARRAY);
|
2007-07-11 02:25:45 -07:00
|
|
|
i = (jsint)GET_UINT24(pc);
|
|
|
|
goto print_int;
|
|
|
|
|
2008-07-30 16:32:33 -07:00
|
|
|
case JOF_UINT8:
|
|
|
|
i = pc[1];
|
|
|
|
goto print_int;
|
|
|
|
|
2007-07-11 02:25:45 -07:00
|
|
|
case JOF_INT8:
|
|
|
|
i = GET_INT8(pc);
|
|
|
|
goto print_int;
|
|
|
|
|
|
|
|
case JOF_INT32:
|
|
|
|
JS_ASSERT(op == JSOP_INT32);
|
|
|
|
i = GET_INT32(pc);
|
|
|
|
print_int:
|
|
|
|
fprintf(fp, " %d", i);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
char numBuf[12];
|
|
|
|
JS_snprintf(numBuf, sizeof numBuf, "%lx", (unsigned long) cs->format);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_UNKNOWN_FORMAT, numBuf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputs("\n", fp);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sprintf, but with unlimited and automatically allocated buffering.
|
|
|
|
*/
|
|
|
|
typedef struct Sprinter {
|
|
|
|
JSContext *context; /* context executing the decompiler */
|
|
|
|
JSArenaPool *pool; /* string allocation pool */
|
|
|
|
char *base; /* base address of buffer in pool */
|
|
|
|
size_t size; /* size of buffer allocated at base */
|
|
|
|
ptrdiff_t offset; /* offset of next free char in buffer */
|
|
|
|
} Sprinter;
|
|
|
|
|
|
|
|
#define INIT_SPRINTER(cx, sp, ap, off) \
|
|
|
|
((sp)->context = cx, (sp)->pool = ap, (sp)->base = NULL, (sp)->size = 0, \
|
|
|
|
(sp)->offset = off)
|
|
|
|
|
|
|
|
#define OFF2STR(sp,off) ((sp)->base + (off))
|
|
|
|
#define STR2OFF(sp,str) ((str) - (sp)->base)
|
|
|
|
#define RETRACT(sp,str) ((sp)->offset = STR2OFF(sp, str))
|
|
|
|
|
|
|
|
static JSBool
|
2007-05-25 04:35:27 -07:00
|
|
|
SprintEnsureBuffer(Sprinter *sp, size_t len)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-25 04:35:27 -07:00
|
|
|
ptrdiff_t nb;
|
2007-08-25 00:18:35 -07:00
|
|
|
char *base;
|
2007-05-25 04:35:27 -07:00
|
|
|
|
|
|
|
nb = (sp->offset + len + 1) - sp->size;
|
|
|
|
if (nb < 0)
|
|
|
|
return JS_TRUE;
|
2007-08-25 00:18:35 -07:00
|
|
|
base = sp->base;
|
|
|
|
if (!base) {
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_ALLOCATE_CAST(base, char *, sp->pool, nb);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_GROW_CAST(base, char *, sp->pool, sp->size, nb);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-25 00:18:35 -07:00
|
|
|
if (!base) {
|
2007-11-22 14:21:18 -08:00
|
|
|
js_ReportOutOfScriptQuota(sp->context);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-25 00:18:35 -07:00
|
|
|
sp->base = base;
|
2007-03-22 10:30:00 -07:00
|
|
|
sp->size += nb;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ptrdiff_t
|
|
|
|
SprintPut(Sprinter *sp, const char *s, size_t len)
|
|
|
|
{
|
2010-03-14 14:12:47 -07:00
|
|
|
ptrdiff_t offset = sp->size; /* save old size */
|
|
|
|
char *bp = sp->base; /* save old base */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
/* Allocate space for s, including the '\0' at the end. */
|
2007-05-25 04:35:27 -07:00
|
|
|
if (!SprintEnsureBuffer(sp, len))
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
|
|
|
|
2010-03-14 14:12:47 -07:00
|
|
|
if (sp->base != bp && /* buffer was realloc'ed */
|
|
|
|
s >= bp && s < bp + offset) { /* s was within the buffer */
|
|
|
|
s = sp->base + (s - bp); /* this is where it lives now */
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Advance offset and copy s into sp's buffer. */
|
|
|
|
offset = sp->offset;
|
|
|
|
sp->offset += len;
|
|
|
|
bp = sp->base + offset;
|
2009-11-30 04:33:04 -08:00
|
|
|
memmove(bp, s, len);
|
2007-03-22 10:30:00 -07:00
|
|
|
bp[len] = 0;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ptrdiff_t
|
|
|
|
SprintCString(Sprinter *sp, const char *s)
|
|
|
|
{
|
|
|
|
return SprintPut(sp, s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ptrdiff_t
|
|
|
|
SprintString(Sprinter *sp, JSString *str)
|
|
|
|
{
|
2009-06-10 18:29:44 -07:00
|
|
|
const jschar *chars;
|
2007-03-22 10:30:00 -07:00
|
|
|
size_t length, size;
|
|
|
|
ptrdiff_t offset;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndLength(chars, length);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length == 0)
|
|
|
|
return sp->offset;
|
|
|
|
|
|
|
|
size = js_GetDeflatedStringLength(sp->context, chars, length);
|
2007-05-25 04:35:27 -07:00
|
|
|
if (size == (size_t)-1 || !SprintEnsureBuffer(sp, size))
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
offset = sp->offset;
|
|
|
|
sp->offset += size;
|
|
|
|
js_DeflateStringToBuffer(sp->context, chars, length, sp->base + offset,
|
|
|
|
&size);
|
|
|
|
sp->base[sp->offset] = 0;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ptrdiff_t
|
|
|
|
Sprint(Sprinter *sp, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *bp;
|
|
|
|
ptrdiff_t offset;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
bp = JS_vsmprintf(format, ap); /* XXX vsaprintf */
|
|
|
|
va_end(ap);
|
|
|
|
if (!bp) {
|
|
|
|
JS_ReportOutOfMemory(sp->context);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
offset = SprintCString(sp, bp);
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(bp);
|
2007-03-22 10:30:00 -07:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char js_EscapeMap[] = {
|
|
|
|
'\b', 'b',
|
|
|
|
'\f', 'f',
|
|
|
|
'\n', 'n',
|
|
|
|
'\r', 'r',
|
|
|
|
'\t', 't',
|
|
|
|
'\v', 'v',
|
|
|
|
'"', '"',
|
|
|
|
'\'', '\'',
|
|
|
|
'\\', '\\',
|
2007-09-17 12:11:52 -07:00
|
|
|
'\0', '0'
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DONT_ESCAPE 0x10000
|
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
static char *
|
|
|
|
QuoteString(Sprinter *sp, JSString *str, uint32 quote)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSBool dontEscape, ok;
|
|
|
|
jschar qc, c;
|
2007-05-25 04:35:27 -07:00
|
|
|
ptrdiff_t off, len;
|
2007-03-22 10:30:00 -07:00
|
|
|
const jschar *s, *t, *z;
|
|
|
|
const char *e;
|
|
|
|
char *bp;
|
|
|
|
|
|
|
|
/* Sample off first for later return value pointer computation. */
|
|
|
|
dontEscape = (quote & DONT_ESCAPE) != 0;
|
|
|
|
qc = (jschar) quote;
|
|
|
|
off = sp->offset;
|
|
|
|
if (qc && Sprint(sp, "%c", (char)qc) < 0)
|
2009-11-30 04:33:04 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Loop control variables: z points at end of string sentinel. */
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(s, z);
|
2007-03-22 10:30:00 -07:00
|
|
|
for (t = s; t < z; s = ++t) {
|
|
|
|
/* Move t forward from s past un-quote-worthy characters. */
|
|
|
|
c = *t;
|
2007-06-12 12:30:57 -07:00
|
|
|
while (JS_ISPRINT(c) && c != qc && c != '\\' && c != '\t' &&
|
|
|
|
!(c >> 8)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
c = *++t;
|
|
|
|
if (t == z)
|
|
|
|
break;
|
|
|
|
}
|
2009-02-18 09:14:51 -08:00
|
|
|
len = t - s;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Allocate space for s, including the '\0' at the end. */
|
2007-05-25 04:35:27 -07:00
|
|
|
if (!SprintEnsureBuffer(sp, len))
|
2009-11-30 04:33:04 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Advance sp->offset and copy s into sp's buffer. */
|
|
|
|
bp = sp->base + sp->offset;
|
|
|
|
sp->offset += len;
|
|
|
|
while (--len >= 0)
|
|
|
|
*bp++ = (char) *s++;
|
|
|
|
*bp = '\0';
|
|
|
|
|
|
|
|
if (t == z)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use js_EscapeMap, \u, or \x only if necessary. */
|
|
|
|
if (!(c >> 8) && (e = strchr(js_EscapeMap, (int)c)) != NULL) {
|
|
|
|
ok = dontEscape
|
|
|
|
? Sprint(sp, "%c", (char)c) >= 0
|
|
|
|
: Sprint(sp, "\\%c", e[1]) >= 0;
|
|
|
|
} else {
|
|
|
|
ok = Sprint(sp, (c >> 8) ? "\\u%04X" : "\\x%02X", c) >= 0;
|
|
|
|
}
|
|
|
|
if (!ok)
|
2009-11-30 04:33:04 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sprint the closing quote and return the quoted string. */
|
|
|
|
if (qc && Sprint(sp, "%c", (char)qc) < 0)
|
2009-11-30 04:33:04 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2009-11-30 04:33:04 -08:00
|
|
|
* If we haven't Sprint'd anything yet, Sprint an empty string so that
|
|
|
|
* the OFF2STR below gives a valid result.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
if (off == sp->offset && Sprint(sp, "") < 0)
|
|
|
|
return NULL;
|
|
|
|
return OFF2STR(sp, off);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *
|
|
|
|
js_QuoteString(JSContext *cx, JSString *str, jschar quote)
|
|
|
|
{
|
|
|
|
void *mark;
|
|
|
|
Sprinter sprinter;
|
|
|
|
char *bytes;
|
|
|
|
JSString *escstr;
|
|
|
|
|
2010-05-11 08:52:17 -07:00
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
2007-03-22 10:30:00 -07:00
|
|
|
INIT_SPRINTER(cx, &sprinter, &cx->tempPool, 0);
|
|
|
|
bytes = QuoteString(&sprinter, str, quote);
|
|
|
|
escstr = bytes ? JS_NewStringCopyZ(cx, bytes) : NULL;
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-03-22 10:30:00 -07:00
|
|
|
return escstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
struct JSPrinter {
|
|
|
|
Sprinter sprinter; /* base class state */
|
|
|
|
JSArenaPool pool; /* string allocation pool */
|
|
|
|
uintN indent; /* indentation in spaces */
|
2009-11-18 13:33:53 -08:00
|
|
|
bool pretty; /* pretty-print: indent, use newlines */
|
|
|
|
bool grouped; /* in parenthesized expression context */
|
|
|
|
bool strict; /* in code marked strict */
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script; /* script being printed */
|
2008-03-12 16:03:29 -07:00
|
|
|
jsbytecode *dvgfence; /* DecompileExpression fencepost */
|
2008-11-17 16:56:01 -08:00
|
|
|
jsbytecode **pcstack; /* DecompileExpression modeled stack */
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun; /* interpreted function */
|
2008-02-08 16:01:45 -08:00
|
|
|
jsuword *localNames; /* argument and variable names */
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSPrinter *
|
2009-11-18 13:33:53 -08:00
|
|
|
js_NewPrinter(JSContext *cx, const char *name, JSFunction *fun,
|
2009-11-18 13:33:53 -08:00
|
|
|
uintN indent, JSBool pretty, JSBool grouped, JSBool strict)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSPrinter *jp;
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
jp = (JSPrinter *) cx->malloc(sizeof(JSPrinter));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!jp)
|
|
|
|
return NULL;
|
|
|
|
INIT_SPRINTER(cx, &jp->sprinter, &jp->pool, 0);
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_InitArenaPool(&jp->pool, name, 256, 1, &cx->scriptStackQuota);
|
2009-11-05 14:34:39 -08:00
|
|
|
jp->indent = indent;
|
2010-04-01 13:54:57 -07:00
|
|
|
jp->pretty = !!pretty;
|
|
|
|
jp->grouped = !!grouped;
|
|
|
|
jp->strict = !!strict;
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->script = NULL;
|
|
|
|
jp->dvgfence = NULL;
|
2008-03-12 16:03:29 -07:00
|
|
|
jp->pcstack = NULL;
|
2008-03-29 03:34:29 -07:00
|
|
|
jp->fun = fun;
|
2008-02-08 16:01:45 -08:00
|
|
|
jp->localNames = NULL;
|
2009-05-29 05:23:58 -07:00
|
|
|
if (fun && FUN_INTERPRETED(fun) && fun->hasLocalNames()) {
|
2008-03-29 03:34:29 -07:00
|
|
|
jp->localNames = js_GetLocalNameArray(cx, fun, &jp->pool);
|
|
|
|
if (!jp->localNames) {
|
|
|
|
js_DestroyPrinter(jp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return jp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_DestroyPrinter(JSPrinter *jp)
|
|
|
|
{
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_FinishArenaPool(&jp->pool);
|
2009-07-27 21:10:12 -07:00
|
|
|
jp->sprinter.context->free(jp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSString *
|
|
|
|
js_GetPrinterOutput(JSPrinter *jp)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
cx = jp->sprinter.context;
|
|
|
|
if (!jp->sprinter.base)
|
|
|
|
return cx->runtime->emptyString;
|
|
|
|
str = JS_NewStringCopyZ(cx, jp->sprinter.base);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_FreeArenaPool(&jp->pool);
|
2007-03-22 10:30:00 -07:00
|
|
|
INIT_SPRINTER(cx, &jp->sprinter, &jp->pool, 0);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-05-30 15:39:21 -07:00
|
|
|
/*
|
|
|
|
* NB: Indexed by SRC_DECL_* defines from jsemit.h.
|
|
|
|
*/
|
|
|
|
static const char * const var_prefix[] = {"var ", "const ", "let "};
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
VarPrefix(jssrcnote *sn)
|
|
|
|
{
|
|
|
|
if (sn && (SN_TYPE(sn) == SRC_DECL || SN_TYPE(sn) == SRC_GROUPASSIGN)) {
|
|
|
|
ptrdiff_t type = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
if ((uintN)type <= SRC_DECL_LET)
|
|
|
|
return var_prefix[type];
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
int
|
|
|
|
js_printf(JSPrinter *jp, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *bp, *fp;
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
if (*format == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
/* If pretty-printing, expand magic tab into a run of jp->indent spaces. */
|
|
|
|
if (*format == '\t') {
|
|
|
|
format++;
|
2009-11-23 16:20:55 -08:00
|
|
|
if (jp->pretty && Sprint(&jp->sprinter, "%*s", jp->indent, "") < 0) {
|
|
|
|
va_end(ap);
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2009-11-23 16:20:55 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Suppress newlines (must be once per format, at the end) if not pretty. */
|
|
|
|
fp = NULL;
|
|
|
|
if (!jp->pretty && format[cc = strlen(format) - 1] == '\n') {
|
|
|
|
fp = JS_strdup(jp->sprinter.context, format);
|
2009-11-23 16:20:55 -08:00
|
|
|
if (!fp) {
|
|
|
|
va_end(ap);
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
2009-11-23 16:20:55 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
fp[cc] = '\0';
|
|
|
|
format = fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate temp space, convert format, and put. */
|
|
|
|
bp = JS_vsmprintf(format, ap); /* XXX vsaprintf */
|
|
|
|
if (fp) {
|
2009-07-27 21:10:12 -07:00
|
|
|
jp->sprinter.context->free(fp);
|
2007-03-22 10:30:00 -07:00
|
|
|
format = NULL;
|
|
|
|
}
|
|
|
|
if (!bp) {
|
|
|
|
JS_ReportOutOfMemory(jp->sprinter.context);
|
2009-11-23 16:20:55 -08:00
|
|
|
va_end(ap);
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc = strlen(bp);
|
|
|
|
if (SprintPut(&jp->sprinter, bp, (size_t)cc) < 0)
|
|
|
|
cc = -1;
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(bp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_puts(JSPrinter *jp, const char *s)
|
|
|
|
{
|
|
|
|
return SprintCString(&jp->sprinter, s) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
typedef struct SprintStack {
|
|
|
|
Sprinter sprinter; /* sprinter for postfix to infix buffering */
|
|
|
|
ptrdiff_t *offsets; /* stack of postfix string offsets */
|
|
|
|
jsbytecode *opcodes; /* parallel stack of JS opcodes */
|
|
|
|
uintN top; /* top of stack index */
|
|
|
|
uintN inArrayInit; /* array initialiser/comprehension level */
|
2007-05-17 18:41:17 -07:00
|
|
|
JSBool inGenExp; /* in generator expression */
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPrinter *printer; /* permanent output goes here */
|
|
|
|
} SprintStack;
|
|
|
|
|
2008-03-12 16:03:29 -07:00
|
|
|
/*
|
|
|
|
* Find the depth of the operand stack when the interpreter reaches the given
|
|
|
|
* pc in script. pcstack must have space for least script->depth elements. On
|
|
|
|
* return it will contain pointers to opcodes that populated the interpreter's
|
|
|
|
* current operand stack.
|
|
|
|
*
|
|
|
|
* This function cannot raise an exception or error. However, due to a risk of
|
|
|
|
* potential bugs when modeling the stack, the function returns -1 if it
|
|
|
|
* detects an inconsistency in the model. Such an inconsistency triggers an
|
|
|
|
* assert in a debug build.
|
|
|
|
*/
|
|
|
|
static intN
|
|
|
|
ReconstructPCStack(JSContext *cx, JSScript *script, jsbytecode *pc,
|
|
|
|
jsbytecode **pcstack);
|
|
|
|
|
|
|
|
#define FAILED_EXPRESSION_DECOMPILER ((char *) 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decompile a part of expression up to the given pc. The function returns
|
|
|
|
* NULL on out-of-memory, or the FAILED_EXPRESSION_DECOMPILER sentinel when
|
|
|
|
* the decompiler fails due to a bug and/or unimplemented feature, or the
|
|
|
|
* decompiled string on success.
|
|
|
|
*/
|
|
|
|
static char *
|
2008-03-29 03:34:29 -07:00
|
|
|
DecompileExpression(JSContext *cx, JSScript *script, JSFunction *fun,
|
2008-03-12 16:03:29 -07:00
|
|
|
jsbytecode *pc);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Get a stacked offset from ss->sprinter.base, or if the stacked value |off|
|
2008-03-12 16:03:29 -07:00
|
|
|
* is negative, fetch the generating pc from printer->pcstack[-2 - off] and
|
|
|
|
* decompile the code that generated the missing value. This is used when
|
2007-03-22 10:30:00 -07:00
|
|
|
* reporting errors, where the model stack will lack |pcdepth| non-negative
|
2008-03-12 16:03:29 -07:00
|
|
|
* offsets (see DecompileExpression and DecompileCode).
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* If the stacked offset is -1, return 0 to index the NUL padding at the start
|
|
|
|
* of ss->sprinter.base. If this happens, it means there is a decompiler bug
|
|
|
|
* to fix, but it won't violate memory safety.
|
|
|
|
*/
|
|
|
|
static ptrdiff_t
|
|
|
|
GetOff(SprintStack *ss, uintN i)
|
|
|
|
{
|
|
|
|
ptrdiff_t off;
|
2008-03-12 16:03:29 -07:00
|
|
|
jsbytecode *pc;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *bytes;
|
|
|
|
|
|
|
|
off = ss->offsets[i];
|
2008-03-12 16:03:29 -07:00
|
|
|
if (off >= 0)
|
|
|
|
return off;
|
|
|
|
|
|
|
|
JS_ASSERT(off <= -2);
|
|
|
|
JS_ASSERT(ss->printer->pcstack);
|
2008-12-29 23:22:23 -08:00
|
|
|
if (off <= -2 && ss->printer->pcstack) {
|
2008-03-12 16:03:29 -07:00
|
|
|
pc = ss->printer->pcstack[-2 - off];
|
|
|
|
bytes = DecompileExpression(ss->sprinter.context, ss->printer->script,
|
|
|
|
ss->printer->fun, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
2008-03-12 16:03:29 -07:00
|
|
|
if (bytes != FAILED_EXPRESSION_DECOMPILER) {
|
|
|
|
off = SprintCString(&ss->sprinter, bytes);
|
|
|
|
if (off < 0)
|
|
|
|
off = 0;
|
|
|
|
ss->offsets[i] = off;
|
2009-07-27 21:10:12 -07:00
|
|
|
ss->sprinter.context->free(bytes);
|
2008-03-12 16:03:29 -07:00
|
|
|
return off;
|
|
|
|
}
|
|
|
|
if (!ss->sprinter.base && SprintPut(&ss->sprinter, "", 0) >= 0) {
|
|
|
|
memset(ss->sprinter.base, 0, ss->sprinter.offset);
|
|
|
|
ss->offsets[i] = -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-12 16:03:29 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
GetStr(SprintStack *ss, uintN i)
|
|
|
|
{
|
|
|
|
ptrdiff_t off;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must call GetOff before using ss->sprinter.base, since it may be null
|
|
|
|
* until bootstrapped by GetOff.
|
|
|
|
*/
|
|
|
|
off = GetOff(ss, i);
|
|
|
|
return OFF2STR(&ss->sprinter, off);
|
|
|
|
}
|
|
|
|
|
2008-06-04 17:00:46 -07:00
|
|
|
/*
|
|
|
|
* Gap between stacked strings to allow for insertion of parens and commas
|
|
|
|
* when auto-parenthesizing expressions and decompiling array initialisers
|
|
|
|
* (see the JSOP_NEWARRAY case in Decompile).
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
#define PAREN_SLOP (2 + 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These pseudo-ops help js_DecompileValueGenerator decompile JSOP_SETNAME,
|
|
|
|
* JSOP_SETPROP, and JSOP_SETELEM, respectively. They are never stored in
|
|
|
|
* bytecode, so they don't preempt valid opcodes.
|
|
|
|
*/
|
2009-01-06 09:18:36 -08:00
|
|
|
#define JSOP_GETPROP2 JSOP_LIMIT
|
|
|
|
#define JSOP_GETELEM2 JSOP_LIMIT + 1
|
|
|
|
JS_STATIC_ASSERT(JSOP_GETELEM2 <= 255);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-17 18:41:17 -07:00
|
|
|
static void
|
|
|
|
AddParenSlop(SprintStack *ss)
|
|
|
|
{
|
|
|
|
memset(OFF2STR(&ss->sprinter, ss->sprinter.offset), 0, PAREN_SLOP);
|
|
|
|
ss->sprinter.offset += PAREN_SLOP;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
|
|
|
PushOff(SprintStack *ss, ptrdiff_t off, JSOp op)
|
|
|
|
{
|
|
|
|
uintN top;
|
|
|
|
|
2007-05-25 04:35:27 -07:00
|
|
|
if (!SprintEnsureBuffer(&ss->sprinter, PAREN_SLOP))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* ss->top points to the next free slot; be paranoid about overflow. */
|
|
|
|
top = ss->top;
|
2008-07-20 13:13:17 -07:00
|
|
|
JS_ASSERT(top < StackDepth(ss->printer->script));
|
|
|
|
if (top >= StackDepth(ss->printer->script)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportOutOfMemory(ss->sprinter.context);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The opcodes stack must contain real bytecodes that index js_CodeSpec. */
|
|
|
|
ss->offsets[top] = off;
|
2009-03-30 16:33:54 -07:00
|
|
|
ss->opcodes[top] = jsbytecode((op == JSOP_GETPROP2) ? JSOP_GETPROP
|
|
|
|
: (op == JSOP_GETELEM2) ? JSOP_GETELEM
|
|
|
|
: op);
|
2007-03-22 10:30:00 -07:00
|
|
|
ss->top = ++top;
|
2007-05-17 18:41:17 -07:00
|
|
|
AddParenSlop(ss);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ptrdiff_t
|
2008-10-21 11:35:22 -07:00
|
|
|
PopOffPrec(SprintStack *ss, uint8 prec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
uintN top;
|
2008-10-21 11:35:22 -07:00
|
|
|
const JSCodeSpec *topcs;
|
2007-03-22 10:30:00 -07:00
|
|
|
ptrdiff_t off;
|
|
|
|
|
|
|
|
/* ss->top points to the next free slot; be paranoid about underflow. */
|
|
|
|
top = ss->top;
|
|
|
|
JS_ASSERT(top != 0);
|
|
|
|
if (top == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ss->top = --top;
|
|
|
|
off = GetOff(ss, top);
|
|
|
|
topcs = &js_CodeSpec[ss->opcodes[top]];
|
2008-10-21 11:35:22 -07:00
|
|
|
if (topcs->prec != 0 && topcs->prec < prec) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ss->sprinter.offset = ss->offsets[top] = off - 2;
|
|
|
|
off = Sprint(&ss->sprinter, "(%s)", OFF2STR(&ss->sprinter, off));
|
|
|
|
} else {
|
|
|
|
ss->sprinter.offset = off;
|
|
|
|
}
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2008-10-21 11:35:22 -07:00
|
|
|
PopStrPrec(SprintStack *ss, uint8 prec)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ptrdiff_t off;
|
|
|
|
|
2008-10-21 11:35:22 -07:00
|
|
|
off = PopOffPrec(ss, prec);
|
2007-03-22 10:30:00 -07:00
|
|
|
return OFF2STR(&ss->sprinter, off);
|
|
|
|
}
|
|
|
|
|
2008-10-21 11:35:22 -07:00
|
|
|
static ptrdiff_t
|
|
|
|
PopOff(SprintStack *ss, JSOp op)
|
|
|
|
{
|
|
|
|
return PopOffPrec(ss, js_CodeSpec[op].prec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
PopStr(SprintStack *ss, JSOp op)
|
|
|
|
{
|
|
|
|
return PopStrPrec(ss, js_CodeSpec[op].prec);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
typedef struct TableEntry {
|
|
|
|
jsval key;
|
|
|
|
ptrdiff_t offset;
|
|
|
|
JSAtom *label;
|
|
|
|
jsint order; /* source order for stable tableswitch sort */
|
|
|
|
} TableEntry;
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CompareOffsets(void *arg, const void *v1, const void *v2, int *result)
|
|
|
|
{
|
|
|
|
ptrdiff_t offset_diff;
|
|
|
|
const TableEntry *te1 = (const TableEntry *) v1,
|
|
|
|
*te2 = (const TableEntry *) v2;
|
|
|
|
|
|
|
|
offset_diff = te1->offset - te2->offset;
|
|
|
|
*result = (offset_diff == 0 ? te1->order - te2->order
|
|
|
|
: offset_diff < 0 ? -1
|
|
|
|
: 1);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-04-01 19:01:50 -07:00
|
|
|
static ptrdiff_t
|
|
|
|
SprintDoubleValue(Sprinter *sp, jsval v, JSOp *opp)
|
|
|
|
{
|
|
|
|
jsdouble d;
|
|
|
|
ptrdiff_t todo;
|
|
|
|
char *s, buf[DTOSTR_STANDARD_BUFFER_SIZE];
|
|
|
|
|
|
|
|
JS_ASSERT(JSVAL_IS_DOUBLE(v));
|
|
|
|
d = *JSVAL_TO_DOUBLE(v);
|
|
|
|
if (JSDOUBLE_IS_NEGZERO(d)) {
|
|
|
|
todo = SprintCString(sp, "-0");
|
|
|
|
*opp = JSOP_NEG;
|
|
|
|
} else if (!JSDOUBLE_IS_FINITE(d)) {
|
|
|
|
/* Don't use Infinity and NaN, they're mutable. */
|
|
|
|
todo = SprintCString(sp,
|
|
|
|
JSDOUBLE_IS_NaN(d)
|
|
|
|
? "0 / 0"
|
|
|
|
: (d < 0)
|
|
|
|
? "1 / -0"
|
|
|
|
: "1 / 0");
|
|
|
|
*opp = JSOP_DIV;
|
|
|
|
} else {
|
2010-03-22 16:21:10 -07:00
|
|
|
s = js_dtostr(JS_THREAD_DATA(sp->context)->dtoaState, buf, sizeof buf,
|
|
|
|
DTOSTR_STANDARD, 0, d);
|
2007-04-01 19:01:50 -07:00
|
|
|
if (!s) {
|
|
|
|
JS_ReportOutOfMemory(sp->context);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
JS_ASSERT(strcmp(s, js_Infinity_str) &&
|
|
|
|
(*s != '-' ||
|
|
|
|
strcmp(s + 1, js_Infinity_str)) &&
|
|
|
|
strcmp(s, js_NaN_str));
|
|
|
|
todo = Sprint(sp, s);
|
|
|
|
}
|
|
|
|
return todo;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static jsbytecode *
|
2007-05-29 18:49:42 -07:00
|
|
|
Decompile(SprintStack *ss, jsbytecode *pc, intN nb, JSOp nextop);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
DecompileSwitch(SprintStack *ss, TableEntry *table, uintN tableLength,
|
|
|
|
jsbytecode *pc, ptrdiff_t switchLength,
|
|
|
|
ptrdiff_t defaultOffset, JSBool isCondSwitch)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
JSPrinter *jp;
|
2007-04-01 19:01:50 -07:00
|
|
|
ptrdiff_t off, off2, diff, caseExprOff, todo;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *lval, *rval;
|
|
|
|
uintN i;
|
|
|
|
jsval key;
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
cx = ss->sprinter.context;
|
|
|
|
jp = ss->printer;
|
|
|
|
|
|
|
|
/* JSOP_CONDSWITCH doesn't pop, unlike JSOP_{LOOKUP,TABLE}SWITCH. */
|
|
|
|
off = isCondSwitch ? GetOff(ss, ss->top-1) : PopOff(ss, JSOP_NOP);
|
|
|
|
lval = OFF2STR(&ss->sprinter, off);
|
|
|
|
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\tswitch (%s) {\n", lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (tableLength) {
|
|
|
|
diff = table[0].offset - defaultOffset;
|
|
|
|
if (diff > 0) {
|
|
|
|
jp->indent += 2;
|
|
|
|
js_printf(jp, "\t%s:\n", js_default_str);
|
|
|
|
jp->indent += 2;
|
2007-05-29 18:49:42 -07:00
|
|
|
if (!Decompile(ss, pc + defaultOffset, diff, JSOP_NOP))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
jp->indent -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
caseExprOff = isCondSwitch ? JSOP_CONDSWITCH_LENGTH : 0;
|
|
|
|
|
|
|
|
for (i = 0; i < tableLength; i++) {
|
|
|
|
off = table[i].offset;
|
|
|
|
off2 = (i + 1 < tableLength) ? table[i + 1].offset : switchLength;
|
|
|
|
|
|
|
|
key = table[i].key;
|
|
|
|
if (isCondSwitch) {
|
|
|
|
ptrdiff_t nextCaseExprOff;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* key encodes the JSOP_CASE bytecode's offset from switchtop.
|
|
|
|
* The next case expression follows immediately, unless we are
|
|
|
|
* at the last case.
|
|
|
|
*/
|
|
|
|
nextCaseExprOff = (ptrdiff_t)JSVAL_TO_INT(key);
|
|
|
|
nextCaseExprOff += js_CodeSpec[pc[nextCaseExprOff]].length;
|
|
|
|
jp->indent += 2;
|
|
|
|
if (!Decompile(ss, pc + caseExprOff,
|
2007-05-29 18:49:42 -07:00
|
|
|
nextCaseExprOff - caseExprOff, JSOP_NOP)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
caseExprOff = nextCaseExprOff;
|
|
|
|
|
|
|
|
/* Balance the stack as if this JSOP_CASE matched. */
|
|
|
|
--ss->top;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* key comes from an atom, not the decompiler, so we need to
|
|
|
|
* quote it if it's a string literal. But if table[i].label
|
|
|
|
* is non-null, key was constant-propagated and label is the
|
|
|
|
* name of the const we should show as the case label. We set
|
|
|
|
* key to undefined so this identifier is escaped, if required
|
|
|
|
* by non-ASCII characters, but not quoted, by QuoteString.
|
|
|
|
*/
|
2007-04-01 19:01:50 -07:00
|
|
|
todo = -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (table[i].label) {
|
|
|
|
str = ATOM_TO_STRING(table[i].label);
|
|
|
|
key = JSVAL_VOID;
|
2007-04-01 19:01:50 -07:00
|
|
|
} else if (JSVAL_IS_DOUBLE(key)) {
|
|
|
|
JSOp junk;
|
|
|
|
|
|
|
|
todo = SprintDoubleValue(&ss->sprinter, key, &junk);
|
|
|
|
str = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
str = js_ValueToString(cx, key);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-04-01 19:01:50 -07:00
|
|
|
if (todo >= 0) {
|
|
|
|
rval = OFF2STR(&ss->sprinter, todo);
|
|
|
|
} else {
|
|
|
|
rval = QuoteString(&ss->sprinter, str, (jschar)
|
|
|
|
(JSVAL_IS_STRING(key) ? '"' : 0));
|
|
|
|
if (!rval)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
RETRACT(&ss->sprinter, rval);
|
|
|
|
jp->indent += 2;
|
|
|
|
js_printf(jp, "\tcase %s:\n", rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
jp->indent += 2;
|
|
|
|
if (off <= defaultOffset && defaultOffset < off2) {
|
|
|
|
diff = defaultOffset - off;
|
|
|
|
if (diff != 0) {
|
2007-05-29 18:49:42 -07:00
|
|
|
if (!Decompile(ss, pc + off, diff, JSOP_NOP))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
off = defaultOffset;
|
|
|
|
}
|
|
|
|
jp->indent -= 2;
|
|
|
|
js_printf(jp, "\t%s:\n", js_default_str);
|
|
|
|
jp->indent += 2;
|
|
|
|
}
|
2007-05-29 18:49:42 -07:00
|
|
|
if (!Decompile(ss, pc + off, off2 - off, JSOP_NOP))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
jp->indent -= 4;
|
|
|
|
|
|
|
|
/* Re-balance as if last JSOP_CASE or JSOP_DEFAULT mismatched. */
|
|
|
|
if (isCondSwitch)
|
|
|
|
++ss->top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultOffset == switchLength) {
|
|
|
|
jp->indent += 2;
|
|
|
|
js_printf(jp, "\t%s:;\n", js_default_str);
|
|
|
|
jp->indent -= 2;
|
|
|
|
}
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
|
|
|
|
/* By the end of a JSOP_CONDSWITCH, the discriminant has been popped. */
|
|
|
|
if (isCondSwitch)
|
|
|
|
--ss->top;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-02 13:32:27 -07:00
|
|
|
#define LOCAL_ASSERT_CUSTOM(expr, BAD_EXIT) \
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
JS_ASSERT(expr); \
|
2008-09-02 13:32:27 -07:00
|
|
|
if (!(expr)) { BAD_EXIT; } \
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_END_MACRO
|
|
|
|
|
2008-09-02 13:32:27 -07:00
|
|
|
#define LOCAL_ASSERT_RV(expr, rv) \
|
|
|
|
LOCAL_ASSERT_CUSTOM(expr, return (rv))
|
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
static JSAtom *
|
2008-08-06 07:13:22 -07:00
|
|
|
GetArgOrVarAtom(JSPrinter *jp, uintN slot)
|
2007-11-19 09:15:45 -08:00
|
|
|
{
|
|
|
|
JSAtom *name;
|
|
|
|
|
|
|
|
LOCAL_ASSERT_RV(jp->fun, NULL);
|
2009-05-29 05:23:58 -07:00
|
|
|
LOCAL_ASSERT_RV(slot < jp->fun->countLocalNames(), NULL);
|
2008-02-08 16:01:45 -08:00
|
|
|
name = JS_LOCAL_NAME_TO_ATOM(jp->localNames[slot]);
|
|
|
|
#if !JS_HAS_DESTRUCTURING
|
|
|
|
LOCAL_ASSERT_RV(name, NULL);
|
|
|
|
#endif
|
2007-11-19 09:15:45 -08:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
const char *
|
2007-03-22 10:30:00 -07:00
|
|
|
GetLocal(SprintStack *ss, jsint i)
|
|
|
|
{
|
|
|
|
ptrdiff_t off;
|
|
|
|
JSContext *cx;
|
|
|
|
JSScript *script;
|
|
|
|
jsatomid j, n;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSObject *obj;
|
|
|
|
jsint depth, count;
|
|
|
|
JSScopeProperty *sprop;
|
2009-11-30 04:33:04 -08:00
|
|
|
const char *rval;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, "")
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
off = ss->offsets[i];
|
|
|
|
if (off >= 0)
|
2009-11-30 04:33:04 -08:00
|
|
|
return OFF2STR(&ss->sprinter, off);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We must be called from js_DecompileValueGenerator (via Decompile) when
|
2007-07-08 02:03:34 -07:00
|
|
|
* dereferencing a local that's undefined or null. Search script->objects
|
2007-03-22 10:30:00 -07:00
|
|
|
* for the block containing this local by its stack index, i.
|
2008-12-29 23:22:23 -08:00
|
|
|
*
|
|
|
|
* In case of destructuring's use of JSOP_GETLOCAL, however, there may be
|
|
|
|
* no such local. This could mean no blocks (no script objects at all, or
|
|
|
|
* none of the script's object literals are blocks), or the stack slot i is
|
|
|
|
* not in a block. In either case, return GetStr(ss, i).
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
cx = ss->sprinter.context;
|
|
|
|
script = ss->printer->script;
|
2008-12-29 23:22:23 -08:00
|
|
|
if (script->objectsOffset == 0)
|
2009-11-30 04:33:04 -08:00
|
|
|
return GetStr(ss, i);
|
2009-09-09 16:52:55 -07:00
|
|
|
for (j = 0, n = script->objects()->length; ; j++) {
|
2008-12-29 23:22:23 -08:00
|
|
|
if (j == n)
|
2009-11-30 04:33:04 -08:00
|
|
|
return GetStr(ss, i);
|
2009-09-09 16:52:55 -07:00
|
|
|
obj = script->getObject(j);
|
2010-04-07 16:09:49 -07:00
|
|
|
if (obj->getClass() == &js_BlockClass) {
|
2007-07-08 02:03:34 -07:00
|
|
|
depth = OBJ_BLOCK_DEPTH(cx, obj);
|
|
|
|
count = OBJ_BLOCK_COUNT(cx, obj);
|
|
|
|
if ((jsuint)(i - depth) < (jsuint)count)
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i -= depth;
|
2010-04-08 11:22:04 -07:00
|
|
|
for (sprop = obj->scope()->lastProperty(); sprop; sprop = sprop->parent) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sprop->shortid == i)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL_ASSERT(sprop && JSID_IS_ATOM(sprop->id));
|
|
|
|
atom = JSID_TO_ATOM(sprop->id);
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
|
|
|
return rval;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
}
|
|
|
|
|
2008-08-06 07:13:22 -07:00
|
|
|
static JSBool
|
|
|
|
IsVarSlot(JSPrinter *jp, jsbytecode *pc, jsint *indexp)
|
|
|
|
{
|
|
|
|
uintN slot;
|
|
|
|
|
|
|
|
slot = GET_SLOTNO(pc);
|
|
|
|
if (slot < jp->script->nfixed) {
|
|
|
|
/* The slot refers to a variable with name stored in jp->localNames. */
|
|
|
|
*indexp = jp->fun->nargs + slot;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a local which index is relative to the stack base. */
|
|
|
|
slot -= jp->script->nfixed;
|
|
|
|
JS_ASSERT(slot < StackDepth(jp->script));
|
|
|
|
*indexp = slot;
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-09-04 16:27:17 -07:00
|
|
|
#define LOAD_ATOM(PCOFF) \
|
|
|
|
GET_ATOM_FROM_BYTECODE(jp->script, pc, PCOFF, atom)
|
|
|
|
|
2008-10-15 14:02:33 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
|
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, NULL)
|
|
|
|
#define LOAD_OP_DATA(pc) (oplen = (cs = &js_CodeSpec[op=(JSOp)*pc])->length)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static jsbytecode *
|
|
|
|
DecompileDestructuring(SprintStack *ss, jsbytecode *pc, jsbytecode *endpc);
|
|
|
|
|
|
|
|
static jsbytecode *
|
|
|
|
DecompileDestructuringLHS(SprintStack *ss, jsbytecode *pc, jsbytecode *endpc,
|
|
|
|
JSBool *hole)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
JSPrinter *jp;
|
|
|
|
JSOp op;
|
|
|
|
const JSCodeSpec *cs;
|
2008-08-06 07:13:22 -07:00
|
|
|
uintN oplen;
|
|
|
|
jsint i;
|
2009-11-30 04:33:04 -08:00
|
|
|
const char *lval, *xval;
|
|
|
|
ptrdiff_t todo;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSAtom *atom;
|
|
|
|
|
|
|
|
*hole = JS_FALSE;
|
|
|
|
cx = ss->sprinter.context;
|
|
|
|
jp = ss->printer;
|
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_POP:
|
|
|
|
*hole = JS_TRUE;
|
|
|
|
todo = SprintPut(&ss->sprinter, ", ", 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DUP:
|
|
|
|
pc = DecompileDestructuring(ss, pc, endpc);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
LOAD_OP_DATA(pc);
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = PopStr(ss, JSOP_NOP);
|
|
|
|
todo = SprintCString(&ss->sprinter, lval);
|
2007-05-08 12:04:13 -07:00
|
|
|
if (op == JSOP_POPN)
|
2007-03-22 10:30:00 -07:00
|
|
|
return pc;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_POP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SETARG:
|
|
|
|
case JSOP_SETGVAR:
|
|
|
|
case JSOP_SETLOCAL:
|
2007-05-08 12:04:13 -07:00
|
|
|
LOCAL_ASSERT(pc[oplen] == JSOP_POP || pc[oplen] == JSOP_POPN);
|
2007-03-22 10:30:00 -07:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_SETLOCALPOP:
|
|
|
|
atom = NULL;
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = NULL;
|
2008-08-06 07:13:22 -07:00
|
|
|
if (op == JSOP_SETARG) {
|
|
|
|
atom = GetArgOrVarAtom(jp, GET_SLOTNO(pc));
|
2007-11-19 09:15:45 -08:00
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
} else if (op == JSOP_SETGVAR) {
|
2009-09-04 16:27:17 -07:00
|
|
|
LOAD_ATOM(0);
|
2008-08-06 07:13:22 -07:00
|
|
|
} else if (IsVarSlot(jp, pc, &i)) {
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
2007-11-19 09:15:45 -08:00
|
|
|
} else {
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = GetLocal(ss, i);
|
2007-11-19 09:15:45 -08:00
|
|
|
}
|
2009-11-30 04:33:04 -08:00
|
|
|
if (atom)
|
|
|
|
lval = js_AtomToPrintableString(cx, atom);
|
|
|
|
LOCAL_ASSERT(lval);
|
|
|
|
todo = SprintCString(&ss->sprinter, lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (op != JSOP_SETLOCALPOP) {
|
|
|
|
pc += oplen;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
LOAD_OP_DATA(pc);
|
2007-05-08 12:04:13 -07:00
|
|
|
if (op == JSOP_POPN)
|
2007-03-22 10:30:00 -07:00
|
|
|
return pc;
|
|
|
|
LOCAL_ASSERT(op == JSOP_POP);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* We may need to auto-parenthesize the left-most value decompiled
|
|
|
|
* here, so add back PAREN_SLOP temporarily. Then decompile until the
|
|
|
|
* opcode that would reduce the stack depth to (ss->top-1), which we
|
|
|
|
* pass to Decompile encoded as -(ss->top-1) - 1 or just -ss->top for
|
|
|
|
* the nb parameter.
|
|
|
|
*/
|
|
|
|
todo = ss->sprinter.offset;
|
|
|
|
ss->sprinter.offset = todo + PAREN_SLOP;
|
2007-06-18 12:33:21 -07:00
|
|
|
pc = Decompile(ss, pc, -((intN)ss->top), JSOP_NOP);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
LOCAL_ASSERT(op == JSOP_ENUMELEM || op == JSOP_ENUMCONSTELEM);
|
2009-11-30 04:33:04 -08:00
|
|
|
xval = PopStr(ss, JSOP_NOP);
|
|
|
|
lval = PopStr(ss, JSOP_GETPROP);
|
2007-03-22 10:30:00 -07:00
|
|
|
ss->sprinter.offset = todo;
|
2009-11-30 04:33:04 -08:00
|
|
|
if (*lval == '\0') {
|
|
|
|
/* lval is from JSOP_BINDNAME, so just print xval. */
|
|
|
|
todo = SprintCString(&ss->sprinter, xval);
|
|
|
|
} else if (*xval == '\0') {
|
|
|
|
/* xval is from JSOP_SETCALL or JSOP_BINDXMLNAME, print lval. */
|
|
|
|
todo = SprintCString(&ss->sprinter, lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_OPMODE(ss->opcodes[ss->top+1]) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? "%s.%s"
|
|
|
|
: "%s[%s]",
|
2009-11-30 04:33:04 -08:00
|
|
|
lval, xval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (todo < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
LOCAL_ASSERT(pc < endpc);
|
|
|
|
pc += oplen;
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Starting with a SRC_DESTRUCT-annotated JSOP_DUP, decompile a destructuring
|
|
|
|
* left-hand side object or array initialiser, including nested destructuring
|
|
|
|
* initialisers. On successful return, the decompilation will be pushed on ss
|
|
|
|
* and the return value will point to the POP or GROUP bytecode following the
|
|
|
|
* destructuring expression.
|
|
|
|
*
|
|
|
|
* At any point, if pc is equal to endpc and would otherwise advance, we stop
|
|
|
|
* immediately and return endpc.
|
|
|
|
*/
|
|
|
|
static jsbytecode *
|
|
|
|
DecompileDestructuring(SprintStack *ss, jsbytecode *pc, jsbytecode *endpc)
|
|
|
|
{
|
2008-01-29 22:27:13 -08:00
|
|
|
ptrdiff_t head;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext *cx;
|
|
|
|
JSPrinter *jp;
|
|
|
|
JSOp op, saveop;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
uintN oplen;
|
|
|
|
jsint i, lasti;
|
|
|
|
jsdouble d;
|
|
|
|
const char *lval;
|
|
|
|
JSAtom *atom;
|
|
|
|
jssrcnote *sn;
|
|
|
|
JSString *str;
|
|
|
|
JSBool hole;
|
|
|
|
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_DUP);
|
|
|
|
pc += JSOP_DUP_LENGTH;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set head so we can rewrite '[' to '{' as needed. Back up PAREN_SLOP
|
|
|
|
* chars so the destructuring decompilation accumulates contiguously in
|
|
|
|
* ss->sprinter starting with "[".
|
|
|
|
*/
|
|
|
|
head = SprintPut(&ss->sprinter, "[", 1);
|
|
|
|
if (head < 0 || !PushOff(ss, head, JSOP_NOP))
|
|
|
|
return NULL;
|
|
|
|
ss->sprinter.offset -= PAREN_SLOP;
|
|
|
|
LOCAL_ASSERT(head == ss->sprinter.offset - 1);
|
|
|
|
LOCAL_ASSERT(*OFF2STR(&ss->sprinter, head) == '[');
|
|
|
|
|
|
|
|
cx = ss->sprinter.context;
|
|
|
|
jp = ss->printer;
|
|
|
|
lasti = -1;
|
|
|
|
|
|
|
|
while (pc < endpc) {
|
2008-01-29 22:27:13 -08:00
|
|
|
#if JS_HAS_DESTRUCTURING_SHORTHAND
|
|
|
|
ptrdiff_t nameoff = -1;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
saveop = op;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_POP:
|
|
|
|
pc += oplen;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Handle the optimized number-pushing opcodes. */
|
|
|
|
case JSOP_ZERO: d = i = 0; goto do_getelem;
|
|
|
|
case JSOP_ONE: d = i = 1; goto do_getelem;
|
|
|
|
case JSOP_UINT16: d = i = GET_UINT16(pc); goto do_getelem;
|
|
|
|
case JSOP_UINT24: d = i = GET_UINT24(pc); goto do_getelem;
|
2007-07-11 02:25:45 -07:00
|
|
|
case JSOP_INT8: d = i = GET_INT8(pc); goto do_getelem;
|
|
|
|
case JSOP_INT32: d = i = GET_INT32(pc); goto do_getelem;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-11 02:25:45 -07:00
|
|
|
case JSOP_DOUBLE:
|
2009-09-04 16:27:17 -07:00
|
|
|
GET_DOUBLE_FROM_BYTECODE(jp->script, pc, 0, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
d = *ATOM_TO_DOUBLE(atom);
|
|
|
|
LOCAL_ASSERT(JSDOUBLE_IS_FINITE(d) && !JSDOUBLE_IS_NEGZERO(d));
|
|
|
|
i = (jsint)d;
|
|
|
|
|
|
|
|
do_getelem:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
pc += oplen;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
LOCAL_ASSERT(op == JSOP_GETELEM);
|
|
|
|
|
|
|
|
/* Distinguish object from array by opcode or source note. */
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_INITPROP) {
|
|
|
|
*OFF2STR(&ss->sprinter, head) = '{';
|
|
|
|
if (Sprint(&ss->sprinter, "%g: ", d) < 0)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Sanity check for the gnarly control flow above. */
|
|
|
|
LOCAL_ASSERT(i == d);
|
|
|
|
|
|
|
|
/* Fill in any holes (holes at the end don't matter). */
|
|
|
|
while (++lasti < i) {
|
|
|
|
if (SprintPut(&ss->sprinter, ", ", 2) < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
case JSOP_LENGTH:
|
|
|
|
atom = cx->runtime->atomState.lengthAtom;
|
|
|
|
goto do_destructure_atom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_CALLPROP:
|
|
|
|
case JSOP_GETPROP:
|
2009-09-04 16:27:17 -07:00
|
|
|
LOAD_ATOM(0);
|
2008-02-07 15:18:45 -08:00
|
|
|
do_destructure_atom:
|
|
|
|
*OFF2STR(&ss->sprinter, head) = '{';
|
2007-03-22 10:30:00 -07:00
|
|
|
str = ATOM_TO_STRING(atom);
|
2008-01-29 22:27:13 -08:00
|
|
|
#if JS_HAS_DESTRUCTURING_SHORTHAND
|
|
|
|
nameoff = ss->sprinter.offset;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!QuoteString(&ss->sprinter, str,
|
|
|
|
js_IsIdentifier(str) ? 0 : (jschar)'\'')) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (SprintPut(&ss->sprinter, ": ", 2) < 0)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOCAL_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pc += oplen;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decompile the left-hand side expression whose bytecode starts at pc
|
|
|
|
* and continues for a bounded number of bytecodes or stack operations
|
|
|
|
* (and which in any event stops before endpc).
|
|
|
|
*/
|
|
|
|
pc = DecompileDestructuringLHS(ss, pc, endpc, &hole);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
2008-01-29 22:27:13 -08:00
|
|
|
|
|
|
|
#if JS_HAS_DESTRUCTURING_SHORTHAND
|
|
|
|
if (nameoff >= 0) {
|
|
|
|
ptrdiff_t offset, initlen;
|
|
|
|
|
|
|
|
offset = ss->sprinter.offset;
|
|
|
|
LOCAL_ASSERT(*OFF2STR(&ss->sprinter, offset) == '\0');
|
|
|
|
initlen = offset - nameoff;
|
|
|
|
LOCAL_ASSERT(initlen >= 4);
|
|
|
|
|
|
|
|
/* Early check to rule out odd "name: lval" length. */
|
|
|
|
if (((size_t)initlen & 1) == 0) {
|
|
|
|
size_t namelen;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Even "name: lval" string length: check for "x: x" and the
|
|
|
|
* like, and apply the shorthand if we can.
|
|
|
|
*/
|
|
|
|
namelen = (size_t)(initlen - 2) >> 1;
|
|
|
|
name = OFF2STR(&ss->sprinter, nameoff);
|
|
|
|
if (!strncmp(name + namelen, ": ", 2) &&
|
|
|
|
!strncmp(name, name + namelen + 2, namelen)) {
|
|
|
|
offset -= namelen + 2;
|
|
|
|
*OFF2STR(&ss->sprinter, offset) = '\0';
|
|
|
|
ss->sprinter.offset = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pc == endpc || *pc != JSOP_DUP)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
2009-04-02 07:31:54 -07:00
|
|
|
* We should stop if JSOP_DUP is either without notes or its note is
|
|
|
|
* not SRC_CONTINUE. The former happens when JSOP_DUP duplicates the
|
|
|
|
* last destructuring reference implementing an op= assignment like in
|
|
|
|
* '([t] = z).y += x'. In the latter case the note is SRC_DESTRUCT and
|
|
|
|
* means another destructuring initialiser abuts this one like in
|
|
|
|
* '[a] = [b] = c'.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
2009-04-02 07:31:54 -07:00
|
|
|
if (!sn)
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2009-04-02 07:31:54 -07:00
|
|
|
if (SN_TYPE(sn) != SRC_CONTINUE) {
|
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_DESTRUCT);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!hole && SprintPut(&ss->sprinter, ", ", 2) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pc += JSOP_DUP_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
lval = OFF2STR(&ss->sprinter, head);
|
2008-01-29 22:27:13 -08:00
|
|
|
if (SprintPut(&ss->sprinter, (*lval == '[') ? "]" : "}", 1) < 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jsbytecode *
|
|
|
|
DecompileGroupAssignment(SprintStack *ss, jsbytecode *pc, jsbytecode *endpc,
|
|
|
|
jssrcnote *sn, ptrdiff_t *todop)
|
|
|
|
{
|
|
|
|
JSOp op;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
uintN oplen, start, end, i;
|
|
|
|
ptrdiff_t todo;
|
|
|
|
JSBool hole;
|
|
|
|
const char *rval;
|
|
|
|
|
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
LOCAL_ASSERT(op == JSOP_PUSH || op == JSOP_GETLOCAL);
|
|
|
|
|
|
|
|
todo = Sprint(&ss->sprinter, "%s[", VarPrefix(sn));
|
|
|
|
if (todo < 0 || !PushOff(ss, todo, JSOP_NOP))
|
|
|
|
return NULL;
|
|
|
|
ss->sprinter.offset -= PAREN_SLOP;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pc += oplen;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
pc = DecompileDestructuringLHS(ss, pc, endpc, &hole);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
|
|
|
if (pc == endpc)
|
|
|
|
return pc;
|
|
|
|
LOAD_OP_DATA(pc);
|
|
|
|
if (op != JSOP_PUSH && op != JSOP_GETLOCAL)
|
|
|
|
break;
|
|
|
|
if (!hole && SprintPut(&ss->sprinter, ", ", 2) < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-06 23:40:10 -07:00
|
|
|
LOCAL_ASSERT(op == JSOP_POPN);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (SprintPut(&ss->sprinter, "] = [", 5) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
end = ss->top - 1;
|
2007-05-08 12:04:13 -07:00
|
|
|
start = end - GET_UINT16(pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
rval = GetStr(ss, i);
|
2007-05-08 12:04:13 -07:00
|
|
|
if (Sprint(&ss->sprinter,
|
|
|
|
(i == start) ? "%s" : ", %s",
|
2007-03-22 10:30:00 -07:00
|
|
|
(i == end - 1 && *rval == '\0') ? ", " : rval) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SprintPut(&ss->sprinter, "]", 1) < 0)
|
|
|
|
return NULL;
|
|
|
|
ss->sprinter.offset = ss->offsets[i];
|
|
|
|
ss->top = start;
|
|
|
|
*todop = todo;
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
#undef LOAD_OP_DATA
|
|
|
|
|
|
|
|
#endif /* JS_HAS_DESTRUCTURING */
|
|
|
|
|
2007-05-17 18:41:17 -07:00
|
|
|
static JSBool
|
|
|
|
InitSprintStack(JSContext *cx, SprintStack *ss, JSPrinter *jp, uintN depth)
|
|
|
|
{
|
|
|
|
size_t offsetsz, opcodesz;
|
|
|
|
void *space;
|
|
|
|
|
|
|
|
INIT_SPRINTER(cx, &ss->sprinter, &cx->tempPool, PAREN_SLOP);
|
|
|
|
|
|
|
|
/* Allocate the parallel (to avoid padding) offset and opcode stacks. */
|
|
|
|
offsetsz = depth * sizeof(ptrdiff_t);
|
|
|
|
opcodesz = depth * sizeof(jsbytecode);
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_ALLOCATE(space, &cx->tempPool, offsetsz + opcodesz);
|
2007-11-22 14:21:18 -08:00
|
|
|
if (!space) {
|
|
|
|
js_ReportOutOfScriptQuota(cx);
|
2007-05-17 18:41:17 -07:00
|
|
|
return JS_FALSE;
|
2007-11-22 14:21:18 -08:00
|
|
|
}
|
2007-05-17 18:41:17 -07:00
|
|
|
ss->offsets = (ptrdiff_t *) space;
|
|
|
|
ss->opcodes = (jsbytecode *) ((char *)space + offsetsz);
|
|
|
|
|
|
|
|
ss->top = ss->inArrayInit = 0;
|
|
|
|
ss->inGenExp = JS_FALSE;
|
|
|
|
ss->printer = jp;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-04-29 10:26:31 -07:00
|
|
|
/*
|
|
|
|
* If nb is non-negative, decompile nb bytecodes starting at pc. Otherwise
|
|
|
|
* the decompiler starts at pc and continues until it reaches an opcode for
|
|
|
|
* which decompiling would result in the stack depth equaling -(nb + 1).
|
|
|
|
*
|
|
|
|
* The nextop parameter is either JSOP_NOP or the "next" opcode in order of
|
|
|
|
* abstract interpretation (not necessarily physically next in a bytecode
|
|
|
|
* vector). So nextop is JSOP_POP for the last operand in a comma expression,
|
|
|
|
* or JSOP_AND for the right operand of &&.
|
|
|
|
*/
|
|
|
|
static jsbytecode *
|
|
|
|
Decompile(SprintStack *ss, jsbytecode *pc, intN nb, JSOp nextop)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
JSPrinter *jp, *jp2;
|
2008-10-22 12:47:51 -07:00
|
|
|
jsbytecode *startpc, *endpc, *pc2, *done;
|
2007-03-22 10:30:00 -07:00
|
|
|
ptrdiff_t tail, todo, len, oplen, cond, next;
|
|
|
|
JSOp op, lastop, saveop;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
jssrcnote *sn, *sn2;
|
2007-05-06 03:50:07 -07:00
|
|
|
const char *lval, *rval, *xval, *fmt, *token;
|
2009-03-30 07:19:50 -07:00
|
|
|
uintN nuses;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsint i, argc;
|
|
|
|
char **argv;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSObject *obj;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str;
|
|
|
|
JSBool ok;
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
JSBool foreach, inXML, quoteAttr;
|
|
|
|
#else
|
|
|
|
#define inXML JS_FALSE
|
|
|
|
#endif
|
|
|
|
jsval val;
|
|
|
|
|
|
|
|
static const char exception_cookie[] = "/*EXCEPTION*/";
|
|
|
|
static const char retsub_pc_cookie[] = "/*RETSUB_PC*/";
|
|
|
|
static const char forelem_cookie[] = "/*FORELEM*/";
|
|
|
|
static const char with_cookie[] = "/*WITH*/";
|
|
|
|
static const char dot_format[] = "%s.%s";
|
|
|
|
static const char index_format[] = "%s[%s]";
|
|
|
|
static const char predot_format[] = "%s%s.%s";
|
|
|
|
static const char postdot_format[] = "%s.%s%s";
|
|
|
|
static const char preindex_format[] = "%s%s[%s]";
|
|
|
|
static const char postindex_format[] = "%s[%s]%s";
|
|
|
|
static const char ss_format[] = "%s%s";
|
2008-10-22 12:47:51 -07:00
|
|
|
static const char sss_format[] = "%s%s%s";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
/* Argument and variables decompilation uses the following to share code. */
|
2008-07-20 13:13:17 -07:00
|
|
|
JS_STATIC_ASSERT(ARGNO_LEN == SLOTNO_LEN);
|
2007-11-22 00:06:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Local macros
|
|
|
|
*/
|
2008-10-21 11:35:22 -07:00
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, NULL)
|
2007-05-29 18:49:42 -07:00
|
|
|
#define DECOMPILE_CODE(pc,nb) if (!Decompile(ss, pc, nb, JSOP_NOP)) return NULL
|
|
|
|
#define NEXT_OP(pc) (((pc) + (len) == endpc) ? nextop : pc[len])
|
2008-10-28 14:04:29 -07:00
|
|
|
#define TOP_STR() GetStr(ss, ss->top - 1)
|
2007-05-29 18:49:42 -07:00
|
|
|
#define POP_STR() PopStr(ss, op)
|
2008-10-21 11:35:22 -07:00
|
|
|
#define POP_STR_PREC(prec) PopStrPrec(ss, prec)
|
|
|
|
|
|
|
|
/*
|
2009-02-25 09:36:39 -08:00
|
|
|
* Pop a condition expression for if/while. JSOP_IFEQ's precedence forces
|
2008-10-21 11:35:22 -07:00
|
|
|
* extra parens around assignment, which avoids a strict-mode warning.
|
|
|
|
*/
|
2008-10-24 12:34:08 -07:00
|
|
|
#define POP_COND_STR() \
|
|
|
|
PopStr(ss, (js_CodeSpec[ss->opcodes[ss->top - 1]].format & JOF_SET) \
|
|
|
|
? JSOP_IFEQ \
|
2008-10-24 10:52:52 -07:00
|
|
|
: JSOP_NOP)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Callers know that ATOM_IS_STRING(atom), and we leave it to the optimizer to
|
|
|
|
* common ATOM_TO_STRING(atom) here and near the call sites.
|
|
|
|
*/
|
|
|
|
#define ATOM_IS_IDENTIFIER(atom) js_IsIdentifier(ATOM_TO_STRING(atom))
|
2007-05-01 11:07:44 -07:00
|
|
|
#define ATOM_IS_KEYWORD(atom) \
|
2009-06-10 18:29:44 -07:00
|
|
|
(js_CheckKeyword(ATOM_TO_STRING(atom)->chars(), \
|
|
|
|
ATOM_TO_STRING(atom)->length()) != TOK_EOF)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Given an atom already fetched from jp->script's atom map, quote/escape its
|
|
|
|
* string appropriately into rval, and select fmt from the quoted and unquoted
|
|
|
|
* alternatives.
|
|
|
|
*/
|
|
|
|
#define GET_QUOTE_AND_FMT(qfmt, ufmt, rval) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
jschar quote_; \
|
|
|
|
if (!ATOM_IS_IDENTIFIER(atom)) { \
|
|
|
|
quote_ = '\''; \
|
|
|
|
fmt = qfmt; \
|
|
|
|
} else { \
|
|
|
|
quote_ = 0; \
|
|
|
|
fmt = ufmt; \
|
|
|
|
} \
|
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), quote_); \
|
|
|
|
if (!rval) \
|
|
|
|
return NULL; \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
#define LOAD_OBJECT(PCOFF) \
|
|
|
|
GET_OBJECT_FROM_BYTECODE(jp->script, pc, PCOFF, obj)
|
|
|
|
|
|
|
|
#define LOAD_FUNCTION(PCOFF) \
|
2008-04-02 00:46:12 -07:00
|
|
|
GET_FUNCTION_FROM_BYTECODE(jp->script, pc, PCOFF, fun)
|
2007-07-08 02:03:34 -07:00
|
|
|
|
|
|
|
#define LOAD_REGEXP(PCOFF) \
|
|
|
|
GET_REGEXP_FROM_BYTECODE(jp->script, pc, PCOFF, obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-07 00:29:32 -07:00
|
|
|
#define GET_SOURCE_NOTE_ATOM(sn, atom) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
jsatomid atomIndex_ = (jsatomid) js_GetSrcNoteOffset((sn), 0); \
|
|
|
|
\
|
|
|
|
LOCAL_ASSERT(atomIndex_ < jp->script->atomMap.length); \
|
|
|
|
(atom) = jp->script->atomMap.vector[atomIndex_]; \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Get atom from jp->script's atom map, quote/escape its string appropriately
|
|
|
|
* into rval, and select fmt from the quoted and unquoted alternatives.
|
|
|
|
*/
|
|
|
|
#define GET_ATOM_QUOTE_AND_FMT(qfmt, ufmt, rval) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
LOAD_ATOM(0); \
|
|
|
|
GET_QUOTE_AND_FMT(qfmt, ufmt, rval); \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
2008-10-21 11:35:22 -07:00
|
|
|
/*
|
|
|
|
* Per spec, new x(y).z means (new x(y))).z. For example new (x(y).z) must
|
|
|
|
* decompile with the constructor parenthesized, but new x.z should not. The
|
|
|
|
* normal rules give x(y).z and x.z identical precedence: both are produced by
|
|
|
|
* JSOP_GETPROP.
|
|
|
|
*
|
|
|
|
* Therefore, we need to know in case JSOP_NEW whether the constructor
|
|
|
|
* expression contains any unparenthesized function calls. So when building a
|
|
|
|
* MemberExpression or CallExpression, we set ss->opcodes[n] to JSOP_CALL if
|
|
|
|
* this is true. x(y).z gets JSOP_CALL, not JSOP_GETPROP.
|
|
|
|
*/
|
|
|
|
#define PROPAGATE_CALLNESS() \
|
|
|
|
JS_BEGIN_MACRO \
|
2010-01-08 15:25:09 -08:00
|
|
|
if (ss->opcodes[ss->top - 1] == JSOP_CALL || \
|
|
|
|
ss->opcodes[ss->top - 1] == JSOP_EVAL || \
|
|
|
|
ss->opcodes[ss->top - 1] == JSOP_APPLY) { \
|
2008-10-21 11:35:22 -07:00
|
|
|
saveop = JSOP_CALL; \
|
2010-01-08 15:25:09 -08:00
|
|
|
} \
|
2008-10-21 11:35:22 -07:00
|
|
|
JS_END_MACRO
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cx = ss->sprinter.context;
|
2008-04-29 10:26:31 -07:00
|
|
|
JS_CHECK_RECURSION(cx, return NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
jp = ss->printer;
|
|
|
|
startpc = pc;
|
|
|
|
endpc = (nb < 0) ? jp->script->code + jp->script->length : pc + nb;
|
|
|
|
tail = -1;
|
|
|
|
todo = -2; /* NB: different from Sprint() error return. */
|
|
|
|
saveop = JSOP_NOP;
|
|
|
|
sn = NULL;
|
|
|
|
rval = NULL;
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
foreach = inXML = quoteAttr = JS_FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (nb < 0 || pc < endpc) {
|
|
|
|
/*
|
|
|
|
* Move saveop to lastop so prefixed bytecodes can take special action
|
|
|
|
* while sharing maximal code. Set op and saveop to the new bytecode,
|
|
|
|
* use op in POP_STR to trigger automatic parenthesization, but push
|
|
|
|
* saveop at the bottom of the loop if this op pushes. Thus op may be
|
|
|
|
* set to nop or otherwise mutated to suppress auto-parens.
|
|
|
|
*/
|
|
|
|
lastop = saveop;
|
|
|
|
op = (JSOp) *pc;
|
|
|
|
cs = &js_CodeSpec[op];
|
2007-07-08 02:03:34 -07:00
|
|
|
if (cs->format & JOF_INDEXBASE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2007-07-08 02:03:34 -07:00
|
|
|
* The decompiler uses js_GetIndexFromBytecode to get atoms and
|
|
|
|
* objects and ignores these suffix/prefix bytecodes, thus
|
|
|
|
* simplifying code that must process JSOP_GETTER/JSOP_SETTER
|
|
|
|
* prefixes.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
pc += cs->length;
|
|
|
|
if (pc >= endpc)
|
|
|
|
break;
|
|
|
|
op = (JSOp) *pc;
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
}
|
|
|
|
saveop = op;
|
|
|
|
len = oplen = cs->length;
|
2009-03-30 07:19:50 -07:00
|
|
|
nuses = js_GetStackUses(cs, op, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-30 07:19:50 -07:00
|
|
|
/*
|
|
|
|
* Here it is possible that nuses > ss->top when the op has a hidden
|
|
|
|
* source note. But when nb < 0 we assume that the caller knows that
|
|
|
|
* Decompile would never meet such opcodes.
|
|
|
|
*/
|
|
|
|
if (nb < 0) {
|
|
|
|
LOCAL_ASSERT(ss->top >= nuses);
|
|
|
|
uintN ndefs = js_GetStackDefs(cx, cs, op, jp->script, pc);
|
|
|
|
if ((uintN) -(nb + 1) == ss->top - nuses + ndefs)
|
|
|
|
return pc;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-09 14:40:36 -07:00
|
|
|
/*
|
|
|
|
* Save source literal associated with JS now before the following
|
|
|
|
* rewrite changes op. See bug 380197.
|
|
|
|
*/
|
|
|
|
token = CodeToken[op];
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pc + oplen == jp->dvgfence) {
|
|
|
|
JSStackFrame *fp;
|
|
|
|
uint32 format, mode, type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrite non-get ops to their "get" format if the error is in
|
|
|
|
* the bytecode at pc, so we don't decompile more than the error
|
|
|
|
* expression.
|
|
|
|
*/
|
2008-12-09 08:38:32 -08:00
|
|
|
fp = js_GetScriptedCaller(cx, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
format = cs->format;
|
2010-03-03 18:10:13 -08:00
|
|
|
if (((fp && pc == fp->pc(cx)) ||
|
2009-03-30 07:19:50 -07:00
|
|
|
(pc == startpc && nuses != 0)) &&
|
2008-08-06 17:34:58 -07:00
|
|
|
format & (JOF_SET|JOF_DEL|JOF_INCDEC|JOF_FOR|JOF_VARPROP)) {
|
2007-05-22 23:04:18 -07:00
|
|
|
mode = JOF_MODE(format);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mode == JOF_NAME) {
|
|
|
|
/*
|
2007-08-08 06:35:05 -07:00
|
|
|
* JOF_NAME does not imply JOF_ATOM, so we must check for
|
2007-03-22 10:30:00 -07:00
|
|
|
* the QARG and QVAR format types, and translate those to
|
2008-08-06 07:13:22 -07:00
|
|
|
* JSOP_GETARG or JSOP_GETLOCAL appropriately, instead of
|
|
|
|
* to JSOP_NAME.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-02-07 15:18:45 -08:00
|
|
|
type = JOF_TYPE(format);
|
2007-03-22 10:30:00 -07:00
|
|
|
op = (type == JOF_QARG)
|
|
|
|
? JSOP_GETARG
|
|
|
|
: (type == JOF_LOCAL)
|
|
|
|
? JSOP_GETLOCAL
|
|
|
|
: JSOP_NAME;
|
|
|
|
|
2009-03-30 07:19:50 -07:00
|
|
|
JS_ASSERT(js_CodeSpec[op].nuses >= 0);
|
|
|
|
i = nuses - js_CodeSpec[op].nuses;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (--i >= 0)
|
|
|
|
PopOff(ss, JSOP_NOP);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We must replace the faulting pc's bytecode with a
|
|
|
|
* corresponding JSOP_GET* code. For JSOP_SET{PROP,ELEM},
|
|
|
|
* we must use the "2nd" form of JSOP_GET{PROP,ELEM}, to
|
|
|
|
* throw away the assignment op's right-hand operand and
|
|
|
|
* decompile it as if it were a GET of its left-hand
|
|
|
|
* operand.
|
|
|
|
*/
|
|
|
|
if (mode == JOF_PROP) {
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (JSOp) ((format & JOF_SET)
|
|
|
|
? JSOP_GETPROP2
|
|
|
|
: JSOP_GETPROP);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (mode == JOF_ELEM) {
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (JSOp) ((format & JOF_SET)
|
|
|
|
? JSOP_GETELEM2
|
|
|
|
: JSOP_GETELEM);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
/*
|
2008-04-24 16:48:32 -07:00
|
|
|
* Unknown mode (including mode 0) means that op is
|
|
|
|
* uncategorized for our purposes, so we must write
|
|
|
|
* per-op special case code here.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_ENUMELEM:
|
|
|
|
case JSOP_ENUMCONSTELEM:
|
|
|
|
op = JSOP_GETELEM;
|
|
|
|
break;
|
|
|
|
case JSOP_SETCALL:
|
|
|
|
op = JSOP_CALL;
|
|
|
|
break;
|
2008-04-30 11:33:51 -07:00
|
|
|
case JSOP_GETTHISPROP:
|
|
|
|
/*
|
|
|
|
* NB: JSOP_GETTHISPROP can't fail due to |this|
|
|
|
|
* being null or undefined at runtime (beware that
|
|
|
|
* this may change for ES4). Therefore any error
|
|
|
|
* resulting from this op must be due to the value
|
|
|
|
* of the property accessed via |this|, so do not
|
|
|
|
* rewrite op to JSOP_THIS.
|
|
|
|
*
|
2008-08-06 07:13:22 -07:00
|
|
|
* The next two cases should not change op if
|
2008-04-30 11:33:51 -07:00
|
|
|
* js_DecompileValueGenerator was called from the
|
|
|
|
* the property getter. They should rewrite only
|
|
|
|
* if the base object in the arg/var/local is null
|
|
|
|
* or undefined. FIXME: bug 431569.
|
|
|
|
*/
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_GETARGPROP:
|
|
|
|
op = JSOP_GETARG;
|
|
|
|
break;
|
|
|
|
case JSOP_GETLOCALPROP:
|
|
|
|
op = JSOP_GETLOCAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOCAL_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveop = op;
|
|
|
|
if (op >= JSOP_LIMIT) {
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_GETPROP2:
|
|
|
|
saveop = JSOP_GETPROP;
|
|
|
|
break;
|
|
|
|
case JSOP_GETELEM2:
|
|
|
|
saveop = JSOP_GETELEM;
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOCAL_ASSERT(js_CodeSpec[saveop].length == oplen ||
|
2008-02-07 15:18:45 -08:00
|
|
|
JOF_TYPE(format) == JOF_SLOTATOM);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
jp->dvgfence = NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-06 03:50:07 -07:00
|
|
|
if (token) {
|
2009-03-30 07:19:50 -07:00
|
|
|
switch (nuses) {
|
2007-03-22 10:30:00 -07:00
|
|
|
case 2:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_ASSIGNOP) {
|
|
|
|
/*
|
|
|
|
* Avoid over-parenthesizing y in x op= y based on its
|
|
|
|
* expansion: x = x op y (replace y by z = w to see the
|
|
|
|
* problem).
|
|
|
|
*/
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (JSOp) pc[oplen];
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Print only the right operand of the assignment-op. */
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
op = saveop;
|
|
|
|
} else if (!inXML) {
|
2008-10-22 12:26:49 -07:00
|
|
|
rval = POP_STR_PREC(cs->prec + !!(cs->format & JOF_LEFTASSOC));
|
|
|
|
lval = POP_STR_PREC(cs->prec + !(cs->format & JOF_LEFTASSOC));
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = Sprint(&ss->sprinter, "%s %s %s",
|
2007-05-06 03:50:07 -07:00
|
|
|
lval, token, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
/* In XML, just concatenate the two operands. */
|
|
|
|
LOCAL_ASSERT(op == JSOP_ADD);
|
2008-10-22 12:26:49 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = Sprint(&ss->sprinter, ss_format, lval, rval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
rval = POP_STR();
|
2007-05-06 03:50:07 -07:00
|
|
|
todo = Sprint(&ss->sprinter, ss_format, token, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
2007-05-06 03:50:07 -07:00
|
|
|
todo = SprintCString(&ss->sprinter, token);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_NOP:
|
|
|
|
/*
|
|
|
|
* Check for a do-while loop, a for-loop with an empty
|
|
|
|
* initializer part, a labeled statement, a function
|
|
|
|
* definition, or try/finally.
|
|
|
|
*/
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = -2;
|
|
|
|
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
|
|
|
|
case SRC_WHILE:
|
|
|
|
++pc;
|
|
|
|
tail = js_GetSrcNoteOffset(sn, 0) - 1;
|
|
|
|
LOCAL_ASSERT(pc[tail] == JSOP_IFNE ||
|
|
|
|
pc[tail] == JSOP_IFNEX);
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\tdo {\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
DECOMPILE_CODE(pc, tail);
|
|
|
|
jp->indent -= 4;
|
2008-10-21 11:35:22 -07:00
|
|
|
js_printf(jp, "\t} while (%s);\n", POP_COND_STR());
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += tail;
|
|
|
|
len = js_CodeSpec[*pc].length;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_FOR:
|
|
|
|
rval = "";
|
|
|
|
|
|
|
|
do_forloop:
|
2008-11-07 14:01:11 -08:00
|
|
|
JS_ASSERT(SN_TYPE(sn) == SRC_FOR);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Skip the JSOP_NOP or JSOP_POP bytecode. */
|
2008-11-07 14:01:11 -08:00
|
|
|
pc += JSOP_NOP_LENGTH;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Get the cond, next, and loop-closing tail offsets. */
|
|
|
|
cond = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
next = js_GetSrcNoteOffset(sn, 1);
|
|
|
|
tail = js_GetSrcNoteOffset(sn, 2);
|
2008-06-26 17:49:01 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this loop has a condition, then pc points at a goto
|
|
|
|
* targeting the condition.
|
|
|
|
*/
|
2008-11-07 14:01:11 -08:00
|
|
|
pc2 = pc;
|
2008-06-26 17:49:01 -07:00
|
|
|
if (cond != tail) {
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_GOTO || *pc == JSOP_GOTOX);
|
2008-11-07 14:01:11 -08:00
|
|
|
pc2 += (*pc == JSOP_GOTO) ? JSOP_GOTO_LENGTH : JSOP_GOTOX_LENGTH;
|
2008-06-26 17:49:01 -07:00
|
|
|
}
|
2008-11-07 14:01:11 -08:00
|
|
|
LOCAL_ASSERT(tail + GetJumpOffset(pc+tail, pc+tail) == pc2 - pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Print the keyword and the possibly empty init-part. */
|
|
|
|
js_printf(jp, "\tfor (%s;", rval);
|
|
|
|
|
2008-06-26 17:49:01 -07:00
|
|
|
if (cond != tail) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Decompile the loop condition. */
|
2008-06-26 17:49:01 -07:00
|
|
|
DECOMPILE_CODE(pc + cond, tail - cond);
|
2008-10-22 12:06:16 -07:00
|
|
|
js_printf(jp, " %s", POP_STR());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Need a semicolon whether or not there was a cond. */
|
|
|
|
js_puts(jp, ";");
|
|
|
|
|
2008-06-26 17:49:01 -07:00
|
|
|
if (next != cond) {
|
2008-11-07 14:01:11 -08:00
|
|
|
/*
|
|
|
|
* Decompile the loop updater. It may end in a JSOP_POP
|
|
|
|
* that we skip; or in a JSOP_POPN that we do not skip,
|
|
|
|
* followed by a JSOP_NOP (skipped as if it's a POP).
|
|
|
|
* We cope with the difference between these two cases
|
|
|
|
* by checking for stack imbalance and popping if there
|
|
|
|
* is an rval.
|
|
|
|
*/
|
|
|
|
uintN saveTop = ss->top;
|
|
|
|
|
|
|
|
DECOMPILE_CODE(pc + next, cond - next - JSOP_POP_LENGTH);
|
|
|
|
LOCAL_ASSERT(ss->top - saveTop <= 1U);
|
|
|
|
rval = (ss->top == saveTop)
|
|
|
|
? ss->sprinter.base + ss->sprinter.offset
|
|
|
|
: POP_STR();
|
|
|
|
js_printf(jp, " %s", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the loop body. */
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, ") {\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
2008-11-07 14:01:11 -08:00
|
|
|
next -= pc2 - pc;
|
|
|
|
DECOMPILE_CODE(pc2, next);
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
|
|
|
|
/* Set len so pc skips over the entire loop. */
|
|
|
|
len = tail + js_CodeSpec[pc[tail]].length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_LABEL:
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent -= 4;
|
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t%s:\n", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_LABELBRACE:
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t%s: {\n", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_ENDBRACE:
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_FUNCDEF:
|
2009-09-09 16:52:55 -07:00
|
|
|
fun = jp->script->getFunction(js_GetSrcNoteOffset(sn, 0));
|
2007-03-22 10:30:00 -07:00
|
|
|
do_function:
|
2007-11-19 09:15:45 -08:00
|
|
|
js_puts(jp, "\n");
|
2009-11-18 13:33:53 -08:00
|
|
|
jp2 = js_NewPrinter(cx, "nested_function", fun,
|
2009-11-18 13:33:53 -08:00
|
|
|
jp->indent, jp->pretty, jp->grouped,
|
|
|
|
jp->strict);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!jp2)
|
|
|
|
return NULL;
|
2008-03-29 03:34:29 -07:00
|
|
|
ok = js_DecompileFunction(jp2);
|
2007-11-19 09:15:45 -08:00
|
|
|
if (ok && jp2->sprinter.base)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_puts(jp, jp2->sprinter.base);
|
|
|
|
js_DestroyPrinter(jp2);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
2007-11-19 09:15:45 -08:00
|
|
|
js_puts(jp, "\n\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_BRACE:
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t{\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
DECOMPILE_CODE(pc + oplen, len - oplen);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_PUSH:
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_GROUPASSIGN) {
|
|
|
|
pc = DecompileGroupAssignment(ss, pc, endpc, sn, &todo);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
2007-05-06 23:40:10 -07:00
|
|
|
LOCAL_ASSERT(*pc == JSOP_POPN);
|
|
|
|
len = oplen = JSOP_POPN_LENGTH;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto end_groupassignment;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_BINDNAME:
|
|
|
|
todo = Sprint(&ss->sprinter, "");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_TRY:
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\ttry {\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_FINALLY:
|
|
|
|
jp->indent -= 4;
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t} finally {\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
|
|
|
|
/*
|
2007-05-24 00:51:46 -07:00
|
|
|
* We push push the pair of exception/restsub cookies to
|
|
|
|
* simulate the effects [gosub] or control transfer during
|
|
|
|
* exception capturing on the stack.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
todo = Sprint(&ss->sprinter, exception_cookie);
|
|
|
|
if (todo < 0 || !PushOff(ss, todo, op))
|
|
|
|
return NULL;
|
|
|
|
todo = Sprint(&ss->sprinter, retsub_pc_cookie);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_RETSUB:
|
|
|
|
rval = POP_STR();
|
|
|
|
LOCAL_ASSERT(strcmp(rval, retsub_pc_cookie) == 0);
|
|
|
|
lval = POP_STR();
|
|
|
|
LOCAL_ASSERT(strcmp(lval, exception_cookie) == 0);
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_GOSUB:
|
|
|
|
case JSOP_GOSUBX:
|
|
|
|
/*
|
|
|
|
* JSOP_GOSUB and GOSUBX have no effect on the decompiler's
|
|
|
|
* string stack because the next op in bytecode order finds
|
|
|
|
* the stack balanced by a JSOP_RETSUB executed elsewhere.
|
|
|
|
*/
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
2007-05-06 23:40:10 -07:00
|
|
|
case JSOP_POPN:
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-10-15 14:02:33 -07:00
|
|
|
uintN newtop, oldtop;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The compiler models operand stack depth and fixes the stack
|
|
|
|
* pointer on entry to a catch clause based on its depth model.
|
|
|
|
* The decompiler must match the code generator's model, which
|
|
|
|
* is why JSOP_FINALLY pushes a cookie that JSOP_RETSUB pops.
|
|
|
|
*/
|
|
|
|
oldtop = ss->top;
|
2007-05-24 00:51:46 -07:00
|
|
|
newtop = oldtop - GET_UINT16(pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(newtop <= oldtop);
|
|
|
|
todo = -2;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
2007-05-06 23:40:10 -07:00
|
|
|
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
|
|
|
|
break;
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sn && SN_TYPE(sn) == SRC_GROUPASSIGN) {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s[] = [",
|
|
|
|
VarPrefix(sn));
|
|
|
|
if (todo < 0)
|
|
|
|
return NULL;
|
2008-10-15 14:02:33 -07:00
|
|
|
for (uintN i = newtop; i < oldtop; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = OFF2STR(&ss->sprinter, ss->offsets[i]);
|
|
|
|
if (Sprint(&ss->sprinter, ss_format,
|
|
|
|
(i == newtop) ? "" : ", ",
|
|
|
|
(i == oldtop - 1 && *rval == '\0')
|
|
|
|
? ", " : rval) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (SprintPut(&ss->sprinter, "]", 1) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
2008-11-07 14:01:11 -08:00
|
|
|
* If this is an empty group assignment, we have no stack
|
|
|
|
* budget into which we can push our result string. Adjust
|
|
|
|
* ss->sprinter.offset so that our consumer can find the
|
|
|
|
* empty group assignment decompilation.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-11-07 14:01:11 -08:00
|
|
|
if (newtop == oldtop) {
|
|
|
|
ss->sprinter.offset = todo;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Kill newtop before the end_groupassignment: label by
|
|
|
|
* retracting/popping early. Control will either jump
|
|
|
|
* to do_forloop: or do_letheadbody: or else break from
|
|
|
|
* our case JSOP_POPN: after the switch (*pc2) below.
|
|
|
|
*/
|
|
|
|
LOCAL_ASSERT(newtop < oldtop);
|
2007-03-22 10:30:00 -07:00
|
|
|
ss->sprinter.offset = GetOff(ss, newtop);
|
|
|
|
ss->top = newtop;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_groupassignment:
|
2008-11-07 14:01:11 -08:00
|
|
|
LOCAL_ASSERT(*pc == JSOP_POPN);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Thread directly to the next opcode if we can, to handle
|
|
|
|
* the special cases of a group assignment in the first or
|
|
|
|
* last part of a for(;;) loop head, or in a let block or
|
|
|
|
* expression head.
|
|
|
|
*
|
|
|
|
* NB: todo at this point indexes space in ss->sprinter
|
|
|
|
* that is liable to be overwritten. The code below knows
|
|
|
|
* exactly how long rval lives, or else copies it down via
|
2009-11-30 04:33:04 -08:00
|
|
|
* SprintCString.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = OFF2STR(&ss->sprinter, todo);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = -2;
|
|
|
|
pc2 = pc + oplen;
|
2008-11-07 14:01:11 -08:00
|
|
|
if (*pc2 == JSOP_NOP) {
|
2007-03-22 10:30:00 -07:00
|
|
|
sn = js_GetSrcNote(jp->script, pc2);
|
|
|
|
if (sn) {
|
|
|
|
if (SN_TYPE(sn) == SRC_FOR) {
|
2008-11-07 14:01:11 -08:00
|
|
|
op = JSOP_NOP;
|
2007-03-22 10:30:00 -07:00
|
|
|
pc = pc2;
|
|
|
|
goto do_forloop;
|
|
|
|
}
|
2008-11-07 14:01:11 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (SN_TYPE(sn) == SRC_DECL) {
|
2008-07-20 13:13:17 -07:00
|
|
|
if (ss->top == StackDepth(jp->script)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* This must be an empty destructuring
|
|
|
|
* in the head of a let whose body block
|
|
|
|
* is also empty.
|
|
|
|
*/
|
2008-11-07 14:01:11 -08:00
|
|
|
pc = pc2 + JSOP_NOP_LENGTH;
|
2007-03-22 10:30:00 -07:00
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
LOCAL_ASSERT(pc[len] == JSOP_LEAVEBLOCK);
|
2009-11-30 04:33:04 -08:00
|
|
|
js_printf(jp, "\tlet (%s) {\n", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
js_printf(jp, "\t}\n");
|
2008-11-07 14:01:11 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (todo < 0 || !PushOff(ss, todo, JSOP_NOP))
|
|
|
|
return NULL;
|
|
|
|
op = JSOP_POP;
|
2008-11-07 14:01:11 -08:00
|
|
|
pc = pc2 + JSOP_NOP_LENGTH;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_letheadbody;
|
|
|
|
}
|
2008-11-07 14:01:11 -08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* An unnannotated NOP following a POPN must be the
|
|
|
|
* third part of for(;;) loop head. If the POPN's
|
|
|
|
* immediate operand is 0, then we may have no slot
|
|
|
|
* on the sprint-stack in which to push our result
|
|
|
|
* string. In this case the result can be recovered
|
|
|
|
* at ss->sprinter.base + ss->sprinter.offset.
|
|
|
|
*/
|
|
|
|
if (GET_UINT16(pc) == 0)
|
|
|
|
break;
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
saveop = JSOP_NOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If control flow reaches this point with todo still -2,
|
2009-11-30 04:33:04 -08:00
|
|
|
* just print rval as an expression statement.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-01-04 17:34:11 -08:00
|
|
|
if (todo == -2)
|
2009-11-30 04:33:04 -08:00
|
|
|
js_printf(jp, "\t%s;\n", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (newtop < oldtop) {
|
|
|
|
ss->sprinter.offset = GetOff(ss, newtop);
|
|
|
|
ss->top = newtop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_EXCEPTION:
|
|
|
|
/* The catch decompiler handles this op itself. */
|
|
|
|
LOCAL_ASSERT(JS_FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_POP:
|
|
|
|
/*
|
|
|
|
* By default, do not automatically parenthesize when popping
|
|
|
|
* a stacked expression decompilation. We auto-parenthesize
|
|
|
|
* only when JSOP_POP is annotated with SRC_PCDELTA, meaning
|
|
|
|
* comma operator.
|
|
|
|
*/
|
|
|
|
op = JSOP_POPV;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_POPV:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
|
|
|
|
case SRC_FOR:
|
|
|
|
/* Force parens around 'in' expression at 'for' front. */
|
|
|
|
if (ss->opcodes[ss->top-1] == JSOP_IN)
|
|
|
|
op = JSOP_LSH;
|
|
|
|
rval = POP_STR();
|
|
|
|
todo = -2;
|
|
|
|
goto do_forloop;
|
|
|
|
|
|
|
|
case SRC_PCDELTA:
|
|
|
|
/* Comma operator: use JSOP_POP for correct precedence. */
|
|
|
|
op = JSOP_POP;
|
|
|
|
|
|
|
|
/* Pop and save to avoid blowing stack depth budget. */
|
|
|
|
lval = JS_strdup(cx, POP_STR());
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The offset tells distance to the end of the right-hand
|
|
|
|
* operand of the comma operator.
|
|
|
|
*/
|
|
|
|
done = pc + len;
|
|
|
|
pc += js_GetSrcNoteOffset(sn, 0);
|
|
|
|
len = 0;
|
|
|
|
|
2007-05-29 18:49:42 -07:00
|
|
|
if (!Decompile(ss, done, pc - done, JSOP_POP)) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop Decompile result and print comma expression. */
|
|
|
|
rval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s, %s", lval, rval);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_HIDDEN:
|
2010-02-10 15:17:52 -08:00
|
|
|
/* Hide this pop, it's from a goto in a with or for/in. */
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_DECL:
|
|
|
|
/* This pop is at the end of the let block/expr head. */
|
|
|
|
pc += JSOP_POP_LENGTH;
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
do_letheadbody:
|
|
|
|
#endif
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
if (pc[len] == JSOP_LEAVEBLOCK) {
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\tlet (%s) {\n", POP_STR());
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
DECOMPILE_CODE(pc, len);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
todo = -2;
|
|
|
|
} else {
|
|
|
|
LOCAL_ASSERT(pc[len] == JSOP_LEAVEBLOCKEXPR);
|
|
|
|
|
2008-10-29 11:36:08 -07:00
|
|
|
lval = JS_strdup(cx, PopStr(ss, JSOP_NOP));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
|
2007-05-29 18:49:42 -07:00
|
|
|
/* Set saveop to reflect what we will push. */
|
|
|
|
saveop = JSOP_LEAVEBLOCKEXPR;
|
|
|
|
if (!Decompile(ss, pc, len, saveop)) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-29 11:36:08 -07:00
|
|
|
rval = PopStr(ss, JSOP_SETNAME);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = Sprint(&ss->sprinter,
|
|
|
|
(*rval == '{')
|
|
|
|
? "let (%s) (%s)"
|
|
|
|
: "let (%s) %s",
|
|
|
|
lval, rval);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Turn off parens around a yield statement. */
|
|
|
|
if (ss->opcodes[ss->top-1] == JSOP_YIELD)
|
|
|
|
op = JSOP_NOP;
|
|
|
|
|
|
|
|
rval = POP_STR();
|
2007-05-17 16:56:17 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't emit decompiler-pushed strings that are not
|
|
|
|
* handled by other opcodes. They are pushed onto the
|
|
|
|
* stack to help model the interpreter stack and should
|
|
|
|
* not appear in the decompiler's output.
|
|
|
|
*/
|
|
|
|
if (*rval != '\0' && (rval[0] != '/' || rval[1] != '*')) {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_printf(jp,
|
|
|
|
(*rval == '{' ||
|
|
|
|
(strncmp(rval, js_function_str, 8) == 0 &&
|
|
|
|
rval[8] == ' '))
|
|
|
|
? "\t(%s);\n"
|
|
|
|
: "\t%s;\n",
|
|
|
|
rval);
|
2007-05-17 16:56:17 -07:00
|
|
|
} else {
|
|
|
|
LOCAL_ASSERT(*rval == '\0' ||
|
|
|
|
strcmp(rval, exception_cookie) == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
2007-04-04 16:35:30 -07:00
|
|
|
sn = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ENTERWITH:
|
|
|
|
LOCAL_ASSERT(!js_GetSrcNote(jp->script, pc));
|
|
|
|
rval = POP_STR();
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\twith (%s) {\n", rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
todo = Sprint(&ss->sprinter, with_cookie);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_LEAVEWITH:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = -2;
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
|
|
|
|
break;
|
|
|
|
rval = POP_STR();
|
|
|
|
LOCAL_ASSERT(strcmp(rval, with_cookie) == 0);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
break;
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
case JSOP_ENTERBLOCK:
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSAtom **atomv, *smallv[5];
|
|
|
|
JSScopeProperty *sprop;
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
LOAD_OBJECT(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
argc = OBJ_BLOCK_COUNT(cx, obj);
|
2007-11-26 07:18:43 -08:00
|
|
|
if ((size_t)argc <= JS_ARRAY_LENGTH(smallv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
atomv = smallv;
|
|
|
|
} else {
|
2009-07-27 21:10:12 -07:00
|
|
|
atomv = (JSAtom **) cx->malloc(argc * sizeof(JSAtom *));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!atomv)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("enterblock_out");
|
2008-09-02 13:32:27 -07:00
|
|
|
#define LOCAL_ASSERT_OUT(expr) LOCAL_ASSERT_CUSTOM(expr, ok = JS_FALSE; \
|
|
|
|
goto enterblock_out)
|
2010-04-08 11:22:04 -07:00
|
|
|
for (sprop = obj->scope()->lastProperty(); sprop;
|
2007-03-22 10:30:00 -07:00
|
|
|
sprop = sprop->parent) {
|
2010-02-05 16:11:13 -08:00
|
|
|
if (!sprop->hasShortID())
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(sprop->shortid < argc);
|
2007-03-22 10:30:00 -07:00
|
|
|
atomv[sprop->shortid] = JSID_TO_ATOM(sprop->id);
|
|
|
|
}
|
|
|
|
ok = JS_TRUE;
|
|
|
|
for (i = 0; i < argc; i++) {
|
2009-11-30 04:33:04 -08:00
|
|
|
atom = atomv[i];
|
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval ||
|
|
|
|
!PushOff(ss, STR2OFF(&ss->sprinter, rval), op)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
goto enterblock_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
|
|
|
|
#if JS_HAS_BLOCK_SCOPE
|
|
|
|
case SRC_BRACE:
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t{\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
2007-05-29 18:49:42 -07:00
|
|
|
ok = Decompile(ss, pc + oplen, len - oplen, JSOP_NOP)
|
|
|
|
!= NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto enterblock_out;
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SRC_CATCH:
|
|
|
|
jp->indent -= 4;
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, "\t} catch (");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
pc2 = pc;
|
|
|
|
pc += oplen;
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(*pc == JSOP_EXCEPTION);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += JSOP_EXCEPTION_LENGTH;
|
2007-05-02 00:09:27 -07:00
|
|
|
todo = Sprint(&ss->sprinter, exception_cookie);
|
|
|
|
if (todo < 0 || !PushOff(ss, todo, JSOP_EXCEPTION)) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto enterblock_out;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*pc == JSOP_DUP) {
|
|
|
|
sn2 = js_GetSrcNote(jp->script, pc);
|
2007-05-02 00:09:27 -07:00
|
|
|
if (!sn2 || SN_TYPE(sn2) != SRC_DESTRUCT) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2007-05-02 00:09:27 -07:00
|
|
|
* This is a dup to save the exception for later.
|
|
|
|
* It is emitted only when the catch head contains
|
2007-03-27 23:49:49 -07:00
|
|
|
* an exception guard.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(js_GetSrcNoteOffset(sn, 0) != 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += JSOP_DUP_LENGTH;
|
2007-05-02 00:09:27 -07:00
|
|
|
todo = Sprint(&ss->sprinter, exception_cookie);
|
|
|
|
if (todo < 0 ||
|
|
|
|
!PushOff(ss, todo, JSOP_EXCEPTION)) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto enterblock_out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-05-02 00:09:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
if (*pc == JSOP_DUP) {
|
|
|
|
pc = DecompileDestructuring(ss, pc, endpc);
|
|
|
|
if (!pc) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto enterblock_out;
|
|
|
|
}
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(*pc == JSOP_POP);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += JSOP_POP_LENGTH;
|
|
|
|
lval = PopStr(ss, JSOP_NOP);
|
|
|
|
js_puts(jp, lval);
|
|
|
|
} else {
|
|
|
|
#endif
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(*pc == JSOP_SETLOCALPOP);
|
2008-07-20 13:13:17 -07:00
|
|
|
i = GET_SLOTNO(pc) - jp->script->nfixed;
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += JSOP_SETLOCALPOP_LENGTH;
|
|
|
|
atom = atomv[i - OBJ_BLOCK_DEPTH(cx, obj)];
|
|
|
|
str = ATOM_TO_STRING(atom);
|
|
|
|
if (!QuoteString(&jp->sprinter, str, 0)) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto enterblock_out;
|
|
|
|
}
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-02 23:39:44 -07:00
|
|
|
/*
|
|
|
|
* Pop the exception_cookie (or its dup in the case of a
|
|
|
|
* guarded catch head) off the stack now.
|
|
|
|
*/
|
|
|
|
rval = PopStr(ss, JSOP_NOP);
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(strcmp(rval, exception_cookie) == 0);
|
2007-05-02 23:39:44 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
if (len) {
|
2009-02-18 09:14:51 -08:00
|
|
|
len -= pc - pc2;
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(len > 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
js_printf(jp, " if ");
|
2007-05-29 18:49:42 -07:00
|
|
|
ok = Decompile(ss, pc, len, JSOP_NOP) != NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto enterblock_out;
|
|
|
|
js_printf(jp, "%s", POP_STR());
|
|
|
|
pc += len;
|
2008-09-02 13:32:27 -07:00
|
|
|
LOCAL_ASSERT_OUT(*pc == JSOP_IFEQ || *pc == JSOP_IFEQX);
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += js_CodeSpec[*pc].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
js_printf(jp, ") {\n");
|
|
|
|
jp->indent += 4;
|
|
|
|
len = 0;
|
|
|
|
break;
|
2007-07-05 13:37:47 -07:00
|
|
|
default:
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
todo = -2;
|
|
|
|
|
2008-09-02 13:32:27 -07:00
|
|
|
#undef LOCAL_ASSERT_OUT
|
2007-03-22 10:30:00 -07:00
|
|
|
enterblock_out:
|
|
|
|
if (atomv != smallv)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(atomv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_LEAVEBLOCK:
|
|
|
|
case JSOP_LEAVEBLOCKEXPR:
|
|
|
|
{
|
|
|
|
uintN top, depth;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = -2;
|
|
|
|
if (op == JSOP_LEAVEBLOCKEXPR) {
|
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_PCBASE);
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = POP_STR();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (sn) {
|
|
|
|
LOCAL_ASSERT(op == JSOP_LEAVEBLOCK);
|
|
|
|
if (SN_TYPE(sn) == SRC_HIDDEN)
|
|
|
|
break;
|
2007-05-02 00:09:27 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This JSOP_LEAVEBLOCK must be for a catch block. If sn's
|
|
|
|
* offset does not equal the model stack depth, there must
|
|
|
|
* be a copy of the exception value on the stack due to a
|
|
|
|
* catch guard (see above, the JSOP_ENTERBLOCK + SRC_CATCH
|
|
|
|
* case code).
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_CATCH);
|
2007-05-02 00:09:27 -07:00
|
|
|
if ((uintN)js_GetSrcNoteOffset(sn, 0) != ss->top) {
|
|
|
|
LOCAL_ASSERT((uintN)js_GetSrcNoteOffset(sn, 0)
|
|
|
|
== ss->top - 1);
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = POP_STR();
|
|
|
|
LOCAL_ASSERT(strcmp(rval, exception_cookie) == 0);
|
2007-05-02 00:09:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
top = ss->top;
|
|
|
|
depth = GET_UINT16(pc);
|
|
|
|
LOCAL_ASSERT(top >= depth);
|
|
|
|
top -= depth;
|
|
|
|
ss->top = top;
|
|
|
|
ss->sprinter.offset = GetOff(ss, top);
|
|
|
|
if (op == JSOP_LEAVEBLOCKEXPR)
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-04 10:14:52 -07:00
|
|
|
case JSOP_GETUPVAR:
|
2009-04-05 21:17:22 -07:00
|
|
|
case JSOP_CALLUPVAR:
|
2009-06-04 18:58:47 -07:00
|
|
|
case JSOP_GETUPVAR_DBG:
|
|
|
|
case JSOP_CALLUPVAR_DBG:
|
2009-04-05 21:17:22 -07:00
|
|
|
case JSOP_GETDSLOT:
|
|
|
|
case JSOP_CALLDSLOT:
|
2009-05-29 05:23:58 -07:00
|
|
|
{
|
2009-04-05 21:17:22 -07:00
|
|
|
if (!jp->fun) {
|
2009-10-08 10:29:03 -07:00
|
|
|
JS_ASSERT(jp->script->savedCallerFun);
|
2009-09-09 16:52:55 -07:00
|
|
|
jp->fun = jp->script->getFunction(0);
|
2009-04-05 21:17:22 -07:00
|
|
|
}
|
2008-10-29 11:25:36 -07:00
|
|
|
|
|
|
|
if (!jp->localNames)
|
|
|
|
jp->localNames = js_GetLocalNameArray(cx, jp->fun, &jp->pool);
|
|
|
|
|
2009-05-29 05:23:58 -07:00
|
|
|
uintN index = GET_UINT16(pc);
|
|
|
|
if (index < jp->fun->u.i.nupvars) {
|
|
|
|
index += jp->fun->countArgsAndVars();
|
|
|
|
} else {
|
2008-08-27 14:35:41 -07:00
|
|
|
JSUpvarArray *uva;
|
2008-10-29 11:25:36 -07:00
|
|
|
#ifdef DEBUG
|
2008-08-27 14:35:41 -07:00
|
|
|
/*
|
|
|
|
* We must be in an eval called from jp->fun, where
|
|
|
|
* jp->script is the eval-compiled script.
|
2008-09-26 15:37:07 -07:00
|
|
|
*
|
|
|
|
* However, it's possible that a js_Invoke already
|
|
|
|
* pushed a frame trying to call js_Construct on an
|
|
|
|
* object that's not a constructor, causing us to be
|
|
|
|
* called with an intervening frame on the stack.
|
2008-08-27 14:35:41 -07:00
|
|
|
*/
|
2008-12-09 08:38:32 -08:00
|
|
|
JSStackFrame *fp = js_GetTopStackFrame(cx);
|
2008-10-29 11:25:36 -07:00
|
|
|
if (fp) {
|
|
|
|
while (!(fp->flags & JSFRAME_EVAL))
|
|
|
|
fp = fp->down;
|
|
|
|
JS_ASSERT(fp->script == jp->script);
|
|
|
|
JS_ASSERT(fp->down->fun == jp->fun);
|
|
|
|
JS_ASSERT(FUN_INTERPRETED(jp->fun));
|
|
|
|
JS_ASSERT(jp->script != jp->fun->u.i.script);
|
|
|
|
JS_ASSERT(jp->script->upvarsOffset != 0);
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-09 16:52:55 -07:00
|
|
|
uva = jp->script->upvars();
|
2009-05-29 05:23:58 -07:00
|
|
|
index = UPVAR_FRAME_SLOT(uva->vector[index]);
|
2008-08-27 14:35:41 -07:00
|
|
|
}
|
2009-05-29 05:23:58 -07:00
|
|
|
atom = GetArgOrVarAtom(jp, index);
|
2008-08-21 03:47:33 -07:00
|
|
|
goto do_name;
|
2009-05-29 05:23:58 -07:00
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_CALLLOCAL:
|
|
|
|
case JSOP_GETLOCAL:
|
2008-08-06 07:13:22 -07:00
|
|
|
if (IsVarSlot(jp, pc, &i)) {
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_name;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT((uintN)i < ss->top);
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
2008-12-31 12:32:35 -08:00
|
|
|
if (sn && SN_TYPE(sn) == SRC_GROUPASSIGN) {
|
|
|
|
/*
|
|
|
|
* Distinguish a js_DecompileValueGenerator call that
|
|
|
|
* targets op alone, from decompilation of a full group
|
|
|
|
* assignment sequence, triggered by SRC_GROUPASSIGN
|
|
|
|
* annotating the first JSOP_GETLOCAL in the sequence.
|
|
|
|
*/
|
|
|
|
if (endpc - pc > JSOP_GETLOCAL_LENGTH || pc > startpc) {
|
|
|
|
pc = DecompileGroupAssignment(ss, pc, endpc, sn, &todo);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_POPN);
|
|
|
|
len = oplen = JSOP_POPN_LENGTH;
|
|
|
|
goto end_groupassignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Null sn to prevent bogus VarPrefix'ing below. */
|
|
|
|
sn = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = GetLocal(ss, i);
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format, VarPrefix(sn), rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SETLOCAL:
|
|
|
|
case JSOP_SETLOCALPOP:
|
2008-08-06 07:13:22 -07:00
|
|
|
if (IsVarSlot(jp, pc, &i)) {
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_setname;
|
|
|
|
}
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = GetLocal(ss, i);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
goto do_setlval;
|
|
|
|
|
|
|
|
case JSOP_INCLOCAL:
|
|
|
|
case JSOP_DECLOCAL:
|
2008-08-06 07:13:22 -07:00
|
|
|
if (IsVarSlot(jp, pc, &i)) {
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_incatom;
|
|
|
|
}
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = GetLocal(ss, i);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_inclval;
|
|
|
|
|
|
|
|
case JSOP_LOCALINC:
|
|
|
|
case JSOP_LOCALDEC:
|
2008-08-06 07:13:22 -07:00
|
|
|
if (IsVarSlot(jp, pc, &i)) {
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_atominc;
|
|
|
|
}
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = GetLocal(ss, i);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_lvalinc;
|
|
|
|
|
|
|
|
case JSOP_RETRVAL:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_RETURN:
|
2007-11-19 09:15:45 -08:00
|
|
|
LOCAL_ASSERT(jp->fun);
|
|
|
|
fun = jp->fun;
|
2007-05-29 18:49:42 -07:00
|
|
|
if (fun->flags & JSFUN_EXPR_CLOSURE) {
|
2008-10-21 11:35:22 -07:00
|
|
|
/* Turn on parens around comma-expression here. */
|
|
|
|
op = JSOP_SETNAME;
|
2007-05-29 18:49:42 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
js_printf(jp, (*rval == '{') ? "(%s)%s" : ss_format,
|
|
|
|
rval,
|
|
|
|
((fun->flags & JSFUN_LAMBDA) || !fun->atom)
|
|
|
|
? ""
|
|
|
|
: ";");
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_SETRVAL:
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
if (*rval != '\0')
|
|
|
|
js_printf(jp, "\t%s %s;\n", js_return_str, rval);
|
|
|
|
else
|
|
|
|
js_printf(jp, "\t%s;\n", js_return_str);
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
case JSOP_YIELD:
|
2007-05-22 23:00:40 -07:00
|
|
|
#if JS_HAS_GENERATOR_EXPRS
|
|
|
|
if (!ss->inGenExp || !(sn = js_GetSrcNote(jp->script, pc)))
|
|
|
|
#endif
|
|
|
|
{
|
2007-05-29 18:49:42 -07:00
|
|
|
/* Turn off most parens. */
|
|
|
|
op = JSOP_SETNAME;
|
2007-05-17 18:41:17 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
todo = (*rval != '\0')
|
|
|
|
? Sprint(&ss->sprinter,
|
|
|
|
(strncmp(rval, js_yield_str, 5) == 0 &&
|
|
|
|
(rval[5] == ' ' || rval[5] == '\0'))
|
|
|
|
? "%s (%s)"
|
|
|
|
: "%s %s",
|
|
|
|
js_yield_str, rval)
|
|
|
|
: SprintCString(&ss->sprinter, js_yield_str);
|
|
|
|
break;
|
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2007-05-22 23:00:40 -07:00
|
|
|
#if JS_HAS_GENERATOR_EXPRS
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_HIDDEN);
|
|
|
|
/* FALL THROUGH */
|
2007-05-22 23:00:40 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_ARRAYPUSH:
|
|
|
|
{
|
2007-05-17 18:41:17 -07:00
|
|
|
uintN pos, forpos;
|
2007-03-22 10:30:00 -07:00
|
|
|
ptrdiff_t start;
|
|
|
|
|
2007-05-29 18:49:42 -07:00
|
|
|
/* Turn off most parens. */
|
|
|
|
op = JSOP_SETNAME;
|
|
|
|
|
2007-05-17 18:41:17 -07:00
|
|
|
/* Pop the expression being pushed or yielded. */
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
2007-05-17 18:41:17 -07:00
|
|
|
|
|
|
|
/*
|
2008-10-22 12:47:51 -07:00
|
|
|
* Skip the for loop head stacked by JSOP_FORLOCAL until we hit
|
|
|
|
* a block local slot (note empty destructuring patterns result
|
|
|
|
* in unit-count blocks).
|
2007-05-17 18:41:17 -07:00
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
pos = ss->top;
|
2008-10-22 12:47:51 -07:00
|
|
|
while (pos != 0) {
|
|
|
|
op = (JSOp) ss->opcodes[--pos];
|
2008-10-28 14:04:29 -07:00
|
|
|
if (op == JSOP_ENTERBLOCK)
|
2007-05-17 18:41:17 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-10-22 12:47:51 -07:00
|
|
|
JS_ASSERT(op == JSOP_ENTERBLOCK);
|
2007-05-17 18:41:17 -07:00
|
|
|
|
|
|
|
/*
|
2008-10-22 12:47:51 -07:00
|
|
|
* Here, forpos must index the space before the left-most |for|
|
|
|
|
* in the single string of accumulated |for| heads and optional
|
2007-05-17 18:41:17 -07:00
|
|
|
* final |if (condition)|.
|
|
|
|
*/
|
2010-05-07 17:52:52 -07:00
|
|
|
forpos = pos + 1;
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(forpos < ss->top);
|
|
|
|
|
|
|
|
/*
|
2008-10-22 12:47:51 -07:00
|
|
|
* Now move pos downward over the block's local slots. Even an
|
|
|
|
* empty destructuring pattern has one (dummy) local.
|
2007-05-17 18:41:17 -07:00
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
while (ss->opcodes[pos] == JSOP_ENTERBLOCK) {
|
|
|
|
if (pos == 0)
|
|
|
|
break;
|
|
|
|
--pos;
|
|
|
|
}
|
2007-05-17 18:41:17 -07:00
|
|
|
|
2007-05-22 23:00:40 -07:00
|
|
|
#if JS_HAS_GENERATOR_EXPRS
|
2007-05-17 18:41:17 -07:00
|
|
|
if (saveop == JSOP_YIELD) {
|
|
|
|
/*
|
|
|
|
* Generator expression: decompile just rval followed by
|
|
|
|
* the string starting at forpos. Leave the result string
|
|
|
|
* in ss->offsets[0] so it can be recovered by our caller
|
|
|
|
* (the JSOP_ANONFUNOBJ with SRC_GENEXP case). Bump the
|
|
|
|
* top of stack to balance yield, which is an expression
|
|
|
|
* (so has neutral stack balance).
|
|
|
|
*/
|
|
|
|
LOCAL_ASSERT(pos == 0);
|
|
|
|
xval = OFF2STR(&ss->sprinter, ss->offsets[forpos]);
|
|
|
|
ss->sprinter.offset = PAREN_SLOP;
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format, rval, xval);
|
|
|
|
if (todo < 0)
|
|
|
|
return NULL;
|
|
|
|
ss->offsets[0] = todo;
|
|
|
|
++ss->top;
|
|
|
|
return pc;
|
|
|
|
}
|
2007-05-22 23:00:40 -07:00
|
|
|
#endif /* JS_HAS_GENERATOR_EXPRS */
|
2007-05-17 18:41:17 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Array comprehension: retract the sprinter to the beginning
|
|
|
|
* of the array initialiser and decompile "[<rval> for ...]".
|
|
|
|
*/
|
2009-04-05 21:17:22 -07:00
|
|
|
JS_ASSERT(jp->script->nfixed + pos == GET_UINT16(pc));
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(ss->opcodes[pos] == JSOP_NEWINIT);
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
start = ss->offsets[pos];
|
|
|
|
LOCAL_ASSERT(ss->sprinter.base[start] == '[' ||
|
|
|
|
ss->sprinter.base[start] == '#');
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(forpos < ss->top);
|
|
|
|
xval = OFF2STR(&ss->sprinter, ss->offsets[forpos]);
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = OFF2STR(&ss->sprinter, start);
|
|
|
|
RETRACT(&ss->sprinter, lval);
|
2007-05-17 18:41:17 -07:00
|
|
|
|
2008-10-22 12:47:51 -07:00
|
|
|
todo = Sprint(&ss->sprinter, sss_format, lval, rval, xval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (todo < 0)
|
|
|
|
return NULL;
|
2007-05-17 18:41:17 -07:00
|
|
|
ss->offsets[pos] = todo;
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
#endif /* JS_HAS_GENERATORS */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_THROWING:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_THROW:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = -2;
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
|
|
|
|
break;
|
|
|
|
rval = POP_STR();
|
2007-05-06 03:50:07 -07:00
|
|
|
js_printf(jp, "\t%s %s;\n", js_throw_str, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2008-10-28 14:04:29 -07:00
|
|
|
case JSOP_ITER:
|
|
|
|
foreach = (pc[1] & (JSITER_FOREACH | JSITER_KEYVALUE)) ==
|
|
|
|
JSITER_FOREACH;
|
2010-05-07 17:52:52 -07:00
|
|
|
todo = -2;
|
2008-10-28 14:04:29 -07:00
|
|
|
break;
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
case JSOP_MOREITER:
|
|
|
|
JS_NOT_REACHED("JSOP_MOREITER");
|
2008-10-28 14:04:29 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ENDITER:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = -2;
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
|
|
|
|
break;
|
|
|
|
(void) PopOff(ss, op);
|
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_GOTO:
|
|
|
|
case JSOP_GOTOX:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
|
2008-10-22 12:47:51 -07:00
|
|
|
case SRC_FOR_IN:
|
|
|
|
/*
|
|
|
|
* The loop back-edge carries +1 stack balance, for the
|
|
|
|
* flag processed by JSOP_IFNE. We do not decompile the
|
2008-10-28 14:04:29 -07:00
|
|
|
* JSOP_IFNE, and instead push the left-hand side of 'in'
|
|
|
|
* after the loop edge in this stack slot (the JSOP_FOR*
|
|
|
|
* opcodes' decompilers do this pushing).
|
2008-10-22 12:47:51 -07:00
|
|
|
*/
|
|
|
|
cond = GetJumpOffset(pc, pc);
|
|
|
|
next = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
tail = js_GetSrcNoteOffset(sn, 1);
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT(pc[cond] == JSOP_MOREITER);
|
2008-10-22 12:47:51 -07:00
|
|
|
DECOMPILE_CODE(pc + oplen, next - oplen);
|
2008-10-28 14:04:29 -07:00
|
|
|
lval = POP_STR();
|
2010-05-07 17:52:52 -07:00
|
|
|
LOCAL_ASSERT(ss->top >= 1);
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2008-10-22 12:47:51 -07:00
|
|
|
if (ss->inArrayInit || ss->inGenExp) {
|
2010-05-07 17:52:52 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
if (ss->top >= 1 && ss->opcodes[ss->top - 1] == JSOP_FORLOCAL) {
|
|
|
|
ss->sprinter.offset = ss->offsets[ss->top] - PAREN_SLOP;
|
2008-10-22 12:47:51 -07:00
|
|
|
if (Sprint(&ss->sprinter, " %s (%s in %s)",
|
|
|
|
foreach ? js_for_each_str : js_for_str,
|
|
|
|
lval, rval) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-30 07:19:50 -07:00
|
|
|
|
2008-10-22 12:47:51 -07:00
|
|
|
/*
|
|
|
|
* Do not AddParentSlop here, as we will push the
|
|
|
|
* top-most offset again, which will add paren slop
|
|
|
|
* for us. We must push to balance the stack budget
|
|
|
|
* when nesting for heads in a comprehension.
|
|
|
|
*/
|
|
|
|
todo = ss->offsets[ss->top - 1];
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, " %s (%s in %s)",
|
|
|
|
foreach ? js_for_each_str : js_for_str,
|
|
|
|
lval, rval);
|
|
|
|
}
|
|
|
|
if (todo < 0 || !PushOff(ss, todo, JSOP_FORLOCAL))
|
|
|
|
return NULL;
|
|
|
|
DECOMPILE_CODE(pc + next, cond - next);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* As above, rval or an extension of it must remain
|
|
|
|
* stacked during loop body decompilation.
|
|
|
|
*/
|
2010-05-07 17:52:52 -07:00
|
|
|
rval = GetStr(ss, ss->top - 1);
|
2008-10-22 12:47:51 -07:00
|
|
|
js_printf(jp, "\t%s (%s in %s) {\n",
|
|
|
|
foreach ? js_for_each_str : js_for_str,
|
|
|
|
lval, rval);
|
|
|
|
jp->indent += 4;
|
|
|
|
DECOMPILE_CODE(pc + next, cond - next);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
}
|
2009-04-05 21:17:22 -07:00
|
|
|
|
2008-10-22 12:47:51 -07:00
|
|
|
pc += tail;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_IFNE || *pc == JSOP_IFNEX);
|
|
|
|
len = js_CodeSpec[*pc].length;
|
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case SRC_WHILE:
|
|
|
|
cond = GetJumpOffset(pc, pc);
|
|
|
|
tail = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
DECOMPILE_CODE(pc + cond, tail - cond);
|
2008-10-21 11:35:22 -07:00
|
|
|
js_printf(jp, "\twhile (%s) {\n", POP_COND_STR());
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
DECOMPILE_CODE(pc + oplen, cond - oplen);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
pc += tail;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_IFNE || *pc == JSOP_IFNEX);
|
|
|
|
len = js_CodeSpec[*pc].length;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_CONT2LABEL:
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
|
|
|
js_printf(jp, "\tcontinue %s;\n", rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_CONTINUE:
|
|
|
|
js_printf(jp, "\tcontinue;\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_BREAK2LABEL:
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
|
|
|
js_printf(jp, "\tbreak %s;\n", rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_HIDDEN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
js_printf(jp, "\tbreak;\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_IFEQ:
|
|
|
|
case JSOP_IFEQX:
|
|
|
|
{
|
|
|
|
JSBool elseif = JS_FALSE;
|
|
|
|
|
|
|
|
if_again:
|
|
|
|
len = GetJumpOffset(pc, pc);
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
|
|
|
|
switch (sn ? SN_TYPE(sn) : SRC_NULL) {
|
|
|
|
case SRC_IF:
|
|
|
|
case SRC_IF_ELSE:
|
2008-10-21 11:35:22 -07:00
|
|
|
rval = POP_COND_STR();
|
2007-05-17 18:41:17 -07:00
|
|
|
if (ss->inArrayInit || ss->inGenExp) {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_IF);
|
2007-05-17 18:41:17 -07:00
|
|
|
ss->sprinter.offset -= PAREN_SLOP;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (Sprint(&ss->sprinter, " if (%s)", rval) < 0)
|
|
|
|
return NULL;
|
2007-05-17 18:41:17 -07:00
|
|
|
AddParenSlop(ss);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp,
|
2007-03-22 10:30:00 -07:00
|
|
|
elseif ? " if (%s) {\n" : "\tif (%s) {\n",
|
|
|
|
rval);
|
|
|
|
jp->indent += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SN_TYPE(sn) == SRC_IF) {
|
|
|
|
DECOMPILE_CODE(pc + oplen, len - oplen);
|
|
|
|
} else {
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(!ss->inArrayInit && !ss->inGenExp);
|
2007-03-22 10:30:00 -07:00
|
|
|
tail = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
DECOMPILE_CODE(pc + oplen, tail - oplen);
|
|
|
|
jp->indent -= 4;
|
|
|
|
pc += tail;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_GOTO || *pc == JSOP_GOTOX);
|
|
|
|
oplen = js_CodeSpec[*pc].length;
|
|
|
|
len = GetJumpOffset(pc, pc);
|
|
|
|
js_printf(jp, "\t} else");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the second offset for sn is non-zero, it tells
|
|
|
|
* the distance from the goto around the else, to the
|
|
|
|
* ifeq for the if inside the else that forms an "if
|
|
|
|
* else if" chain. Thus cond spans the condition of
|
|
|
|
* the second if, so we simply decompile it and start
|
|
|
|
* over at label if_again.
|
|
|
|
*/
|
2010-04-27 20:22:57 -07:00
|
|
|
cond = js_GetSrcNoteOffset(sn, 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cond != 0) {
|
2010-04-27 21:00:38 -07:00
|
|
|
cond -= tail;
|
2007-03-22 10:30:00 -07:00
|
|
|
DECOMPILE_CODE(pc + oplen, cond - oplen);
|
|
|
|
pc += cond;
|
2010-05-04 14:38:47 -07:00
|
|
|
oplen = js_CodeSpec[*pc].length;
|
2007-03-22 10:30:00 -07:00
|
|
|
elseif = JS_TRUE;
|
|
|
|
goto if_again;
|
|
|
|
}
|
|
|
|
|
2008-01-04 17:34:11 -08:00
|
|
|
js_printf(jp, " {\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent += 4;
|
|
|
|
DECOMPILE_CODE(pc + oplen, len - oplen);
|
|
|
|
}
|
|
|
|
|
2007-05-17 18:41:17 -07:00
|
|
|
if (!ss->inArrayInit && !ss->inGenExp) {
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}\n");
|
|
|
|
}
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRC_COND:
|
|
|
|
xval = JS_strdup(cx, POP_STR());
|
|
|
|
if (!xval)
|
|
|
|
return NULL;
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
DECOMPILE_CODE(pc + oplen, len - oplen);
|
|
|
|
lval = JS_strdup(cx, POP_STR());
|
|
|
|
if (!lval) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((void *)xval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pc += len;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_GOTO || *pc == JSOP_GOTOX);
|
|
|
|
oplen = js_CodeSpec[*pc].length;
|
|
|
|
len = GetJumpOffset(pc, pc);
|
|
|
|
DECOMPILE_CODE(pc + oplen, len - oplen);
|
|
|
|
rval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s ? %s : %s",
|
|
|
|
xval, lval, rval);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((void *)xval);
|
|
|
|
cx->free((void *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_IFNE:
|
|
|
|
case JSOP_IFNEX:
|
|
|
|
LOCAL_ASSERT(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_OR:
|
|
|
|
case JSOP_ORX:
|
|
|
|
xval = "||";
|
|
|
|
|
|
|
|
do_logical_connective:
|
|
|
|
/* Top of stack is the first clause in a disjunction (||). */
|
|
|
|
lval = JS_strdup(cx, POP_STR());
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
done = pc + GetJumpOffset(pc, pc);
|
|
|
|
pc += len;
|
2009-02-18 09:14:51 -08:00
|
|
|
len = done - pc;
|
2007-05-29 18:49:42 -07:00
|
|
|
if (!Decompile(ss, pc, len, op)) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-05-29 18:49:42 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
if (jp->pretty &&
|
|
|
|
jp->indent + 4 + strlen(lval) + 4 + strlen(rval) > 75) {
|
|
|
|
rval = JS_strdup(cx, rval);
|
|
|
|
if (!rval) {
|
|
|
|
tail = -1;
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s\n", lval, xval);
|
|
|
|
tail = Sprint(&ss->sprinter, "%*s%s",
|
|
|
|
jp->indent + 4, "", rval);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (tail < 0)
|
|
|
|
todo = -1;
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s %s", lval, xval, rval);
|
|
|
|
}
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_AND:
|
|
|
|
case JSOP_ANDX:
|
|
|
|
xval = "&&";
|
|
|
|
goto do_logical_connective;
|
|
|
|
|
|
|
|
case JSOP_FORARG:
|
2008-10-22 12:47:51 -07:00
|
|
|
sn = NULL;
|
|
|
|
i = GET_ARGNO(pc);
|
|
|
|
goto do_forvarslot;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-06 07:13:22 -07:00
|
|
|
case JSOP_FORLOCAL:
|
2008-10-22 12:47:51 -07:00
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (!IsVarSlot(jp, pc, &i)) {
|
|
|
|
JS_ASSERT(op == JSOP_FORLOCAL);
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format, VarPrefix(sn), GetStr(ss, i));
|
|
|
|
break;
|
2008-08-06 07:13:22 -07:00
|
|
|
}
|
2008-10-22 12:47:51 -07:00
|
|
|
|
|
|
|
do_forvarslot:
|
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
todo = SprintCString(&ss->sprinter, VarPrefix(sn));
|
|
|
|
if (todo < 0 || !QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0))
|
|
|
|
return NULL;
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_FORNAME:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
2008-10-22 12:47:51 -07:00
|
|
|
todo = SprintCString(&ss->sprinter, VarPrefix(sn));
|
|
|
|
if (todo < 0 || !QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0))
|
|
|
|
return NULL;
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_FORPROP:
|
|
|
|
xval = NULL;
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
if (!ATOM_IS_IDENTIFIER(atom)) {
|
|
|
|
xval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
|
|
|
(jschar)'\'');
|
|
|
|
if (!xval)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
lval = POP_STR();
|
2008-10-23 11:37:07 -07:00
|
|
|
if (xval) {
|
|
|
|
JS_ASSERT(*lval);
|
|
|
|
todo = Sprint(&ss->sprinter, index_format, lval, xval);
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format, lval, *lval ? "." : "");
|
|
|
|
if (todo < 0)
|
|
|
|
return NULL;
|
|
|
|
if (!QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0))
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_FORELEM:
|
|
|
|
todo = SprintCString(&ss->sprinter, forelem_cookie);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ENUMELEM:
|
|
|
|
case JSOP_ENUMCONSTELEM:
|
|
|
|
/*
|
|
|
|
* The stack has the object under the (top) index expression.
|
|
|
|
* The "rval" property id is underneath those two on the stack.
|
|
|
|
* The for loop body net and gross lengths can now be adjusted
|
|
|
|
* to account for the length of the indexing expression that
|
|
|
|
* came after JSOP_FORELEM and before JSOP_ENUMELEM.
|
|
|
|
*/
|
|
|
|
atom = NULL;
|
2007-04-04 16:35:30 -07:00
|
|
|
op = JSOP_NOP; /* turn off parens around xval */
|
2009-11-30 04:33:04 -08:00
|
|
|
xval = POP_STR();
|
2007-03-22 10:30:00 -07:00
|
|
|
op = JSOP_GETELEM; /* lval must have high precedence */
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = POP_STR();
|
2007-03-22 10:30:00 -07:00
|
|
|
op = saveop;
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = POP_STR();
|
|
|
|
LOCAL_ASSERT(strcmp(rval, forelem_cookie) == 0);
|
|
|
|
if (*xval == '\0') {
|
|
|
|
todo = SprintCString(&ss->sprinter, lval);
|
2008-10-22 12:47:51 -07:00
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter,
|
|
|
|
(JOF_OPMODE(lastop) == JOF_XMLNAME)
|
|
|
|
? dot_format
|
|
|
|
: index_format,
|
2009-11-30 04:33:04 -08:00
|
|
|
lval, xval);
|
2008-10-22 12:47:51 -07:00
|
|
|
}
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_GETTER:
|
|
|
|
case JSOP_SETTER:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DUP2:
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = GetStr(ss, ss->top-2);
|
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-07-05 13:37:47 -07:00
|
|
|
if (todo < 0 || !PushOff(ss, todo,
|
|
|
|
(JSOp) ss->opcodes[ss->top-2])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2007-07-05 13:37:47 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_DUP:
|
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (sn) {
|
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_DESTRUCT);
|
|
|
|
pc = DecompileDestructuring(ss, pc, endpc);
|
|
|
|
if (!pc)
|
|
|
|
return NULL;
|
|
|
|
len = 0;
|
|
|
|
lval = POP_STR();
|
|
|
|
op = saveop = JSOP_ENUMELEM;
|
|
|
|
rval = POP_STR();
|
|
|
|
|
|
|
|
if (strcmp(rval, forelem_cookie) == 0) {
|
2008-10-22 12:47:51 -07:00
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
VarPrefix(sn), lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-22 12:47:51 -07:00
|
|
|
// Skip POP so the SRC_FOR_IN code can pop for itself.
|
|
|
|
if (*pc == JSOP_POP)
|
|
|
|
len = JSOP_POP_LENGTH;
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s%s = %s",
|
|
|
|
VarPrefix(sn), lval, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = GetStr(ss, ss->top-1);
|
2007-07-05 13:37:47 -07:00
|
|
|
saveop = (JSOp) ss->opcodes[ss->top-1];
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SETARG:
|
2008-08-06 07:13:22 -07:00
|
|
|
atom = GetArgOrVarAtom(jp, GET_ARGNO(pc));
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_setname;
|
|
|
|
|
|
|
|
case JSOP_SETCONST:
|
|
|
|
case JSOP_SETNAME:
|
|
|
|
case JSOP_SETGVAR:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
|
|
|
|
do_setname:
|
|
|
|
lval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
rval = POP_STR();
|
|
|
|
if (op == JSOP_SETNAME)
|
|
|
|
(void) PopOff(ss, op);
|
|
|
|
|
|
|
|
do_setlval:
|
|
|
|
sn = js_GetSrcNote(jp->script, pc - 1);
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_ASSIGNOP) {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s= %s",
|
|
|
|
lval,
|
|
|
|
(lastop == JSOP_GETTER)
|
|
|
|
? js_getter_str
|
|
|
|
: (lastop == JSOP_SETTER)
|
|
|
|
? js_setter_str
|
2007-05-06 03:50:07 -07:00
|
|
|
: CodeToken[lastop],
|
2007-03-22 10:30:00 -07:00
|
|
|
rval);
|
|
|
|
} else {
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
todo = Sprint(&ss->sprinter, "%s%s = %s",
|
|
|
|
VarPrefix(sn), lval, rval);
|
|
|
|
}
|
|
|
|
if (op == JSOP_SETLOCALPOP) {
|
|
|
|
if (!PushOff(ss, todo, saveop))
|
|
|
|
return NULL;
|
|
|
|
rval = POP_STR();
|
|
|
|
LOCAL_ASSERT(*rval != '\0');
|
|
|
|
js_printf(jp, "\t%s;\n", rval);
|
|
|
|
todo = -2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-08-18 15:43:17 -07:00
|
|
|
case JSOP_CONCATN:
|
|
|
|
{
|
|
|
|
argc = GET_UINT16(pc);
|
|
|
|
JS_ASSERT(argc > 0);
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
js::Vector<char *> argv(cx);
|
2009-08-18 15:43:17 -07:00
|
|
|
if (!argv.resize(argc))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
MUST_FLOW_THROUGH("out");
|
|
|
|
ok = JS_FALSE;
|
|
|
|
|
|
|
|
for (i = argc - 1; i >= 0; i--) {
|
2010-04-14 15:26:44 -07:00
|
|
|
argv[i] = JS_strdup(cx, POP_STR_PREC(cs->prec + 1));
|
2009-08-18 15:43:17 -07:00
|
|
|
if (!argv[i])
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
todo = Sprint(&ss->sprinter, "%s", argv[0]);
|
|
|
|
if (todo < 0)
|
|
|
|
goto out;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (Sprint(&ss->sprinter, " + %s", argv[i]) < 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-11-23 15:08:05 -08:00
|
|
|
/*
|
|
|
|
* The only way that our next op could be a JSOP_ADD is
|
|
|
|
* if we are about to concatenate at least one non-string
|
|
|
|
* literal. Deal with that here in order to avoid extra
|
|
|
|
* parentheses (because JSOP_ADD is left-associative).
|
|
|
|
*/
|
|
|
|
if (pc[len] == JSOP_ADD)
|
|
|
|
saveop = JSOP_NOP;
|
|
|
|
|
2009-08-18 15:43:17 -07:00
|
|
|
ok = JS_TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
JS_free(cx, argv[i]);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_NEW:
|
|
|
|
case JSOP_CALL:
|
|
|
|
case JSOP_EVAL:
|
2008-10-29 13:30:41 -07:00
|
|
|
case JSOP_APPLY:
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_SETCALL:
|
|
|
|
argc = GET_ARGC(pc);
|
|
|
|
argv = (char **)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc((size_t)(argc + 1) * sizeof *argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!argv)
|
|
|
|
return NULL;
|
|
|
|
|
2009-01-10 12:15:08 -08:00
|
|
|
op = JSOP_SETNAME;
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = JS_TRUE;
|
2009-01-10 12:15:08 -08:00
|
|
|
for (i = argc; i > 0; i--)
|
2007-03-22 10:30:00 -07:00
|
|
|
argv[i] = JS_strdup(cx, POP_STR());
|
|
|
|
|
|
|
|
/* Skip the JSOP_PUSHOBJ-created empty string. */
|
|
|
|
LOCAL_ASSERT(ss->top >= 2);
|
|
|
|
(void) PopOff(ss, op);
|
|
|
|
|
2007-04-04 16:32:33 -07:00
|
|
|
/*
|
|
|
|
* Special case: new (x(y)(z)) must be parenthesized like so.
|
|
|
|
* Same for new (x(y).z) -- contrast with new x(y).z.
|
2008-10-21 11:35:22 -07:00
|
|
|
* See PROPAGATE_CALLNESS.
|
2007-04-04 16:32:33 -07:00
|
|
|
*/
|
2009-01-10 12:15:08 -08:00
|
|
|
op = (JSOp) ss->opcodes[ss->top - 1];
|
2007-04-04 16:32:33 -07:00
|
|
|
lval = PopStr(ss,
|
|
|
|
(saveop == JSOP_NEW &&
|
2009-03-30 07:19:50 -07:00
|
|
|
(op == JSOP_CALL ||
|
2008-10-29 13:30:41 -07:00
|
|
|
op == JSOP_EVAL ||
|
|
|
|
op == JSOP_APPLY ||
|
2007-04-04 16:32:33 -07:00
|
|
|
(js_CodeSpec[op].format & JOF_CALLOP)))
|
|
|
|
? JSOP_NAME
|
|
|
|
: saveop);
|
2007-03-22 10:30:00 -07:00
|
|
|
op = saveop;
|
2007-04-04 16:32:33 -07:00
|
|
|
|
|
|
|
argv[0] = JS_strdup(cx, lval);
|
2009-01-10 12:15:08 -08:00
|
|
|
if (!argv[0])
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
|
|
|
|
lval = "(", rval = ")";
|
|
|
|
if (op == JSOP_NEW) {
|
|
|
|
if (argc == 0)
|
|
|
|
lval = rval = "";
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s%s",
|
|
|
|
js_new_str, argv[0], lval);
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
argv[0], lval);
|
|
|
|
}
|
|
|
|
if (todo < 0)
|
|
|
|
ok = JS_FALSE;
|
|
|
|
|
|
|
|
for (i = 1; i <= argc; i++) {
|
|
|
|
if (!argv[i] ||
|
|
|
|
Sprint(&ss->sprinter, ss_format,
|
|
|
|
argv[i], (i < argc) ? ", " : "") < 0) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Sprint(&ss->sprinter, rval) < 0)
|
|
|
|
ok = JS_FALSE;
|
|
|
|
|
2009-01-10 12:15:08 -08:00
|
|
|
for (i = 0; i <= argc; i++)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(argv[i]);
|
|
|
|
cx->free(argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
if (op == JSOP_SETCALL) {
|
|
|
|
if (!PushOff(ss, todo, op))
|
|
|
|
return NULL;
|
|
|
|
todo = Sprint(&ss->sprinter, "");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DELNAME:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
lval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, lval);
|
|
|
|
do_delete_lval:
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s", js_delete_str, lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DELPROP:
|
|
|
|
GET_ATOM_QUOTE_AND_FMT("%s %s[%s]", "%s %s.%s", rval);
|
2008-10-21 11:35:22 -07:00
|
|
|
op = JSOP_GETPROP;
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, fmt, js_delete_str, lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DELELEM:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
xval = POP_STR();
|
2008-10-21 11:35:22 -07:00
|
|
|
op = JSOP_GETPROP;
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
if (*xval == '\0')
|
|
|
|
goto do_delete_lval;
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_OPMODE(lastop) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? "%s %s.%s"
|
|
|
|
: "%s %s[%s]",
|
|
|
|
js_delete_str, lval, xval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
case JSOP_DELDESC:
|
|
|
|
xval = POP_STR();
|
2008-10-21 11:35:22 -07:00
|
|
|
op = JSOP_GETPROP;
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s..%s",
|
|
|
|
js_delete_str, lval, xval);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case JSOP_TYPEOFEXPR:
|
|
|
|
case JSOP_TYPEOF:
|
|
|
|
case JSOP_VOID:
|
|
|
|
rval = POP_STR();
|
2007-05-06 03:50:07 -07:00
|
|
|
todo = Sprint(&ss->sprinter, "%s %s",
|
|
|
|
(op == JSOP_VOID) ? js_void_str : js_typeof_str,
|
|
|
|
rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_INCARG:
|
|
|
|
case JSOP_DECARG:
|
2008-08-06 07:13:22 -07:00
|
|
|
atom = GetArgOrVarAtom(jp, GET_ARGNO(pc));
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_incatom;
|
|
|
|
|
|
|
|
case JSOP_INCNAME:
|
|
|
|
case JSOP_DECNAME:
|
|
|
|
case JSOP_INCGVAR:
|
|
|
|
case JSOP_DECGVAR:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
do_incatom:
|
|
|
|
lval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, lval);
|
|
|
|
do_inclval:
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)], lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_INCPROP:
|
|
|
|
case JSOP_DECPROP:
|
|
|
|
GET_ATOM_QUOTE_AND_FMT(preindex_format, predot_format, rval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force precedence below the numeric literal opcodes, so that
|
|
|
|
* 42..foo or 10000..toString(16), e.g., decompile with parens
|
|
|
|
* around the left-hand side of dot.
|
|
|
|
*/
|
|
|
|
op = JSOP_GETPROP;
|
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, fmt,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)],
|
|
|
|
lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_INCELEM:
|
|
|
|
case JSOP_DECELEM:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
xval = POP_STR();
|
|
|
|
op = JSOP_GETELEM;
|
|
|
|
lval = POP_STR();
|
|
|
|
if (*xval != '\0') {
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_OPMODE(lastop) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? predot_format
|
|
|
|
: preindex_format,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)],
|
|
|
|
lval, xval);
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)], lval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ARGINC:
|
|
|
|
case JSOP_ARGDEC:
|
2008-08-06 07:13:22 -07:00
|
|
|
atom = GetArgOrVarAtom(jp, GET_ARGNO(pc));
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
goto do_atominc;
|
|
|
|
|
|
|
|
case JSOP_NAMEINC:
|
|
|
|
case JSOP_NAMEDEC:
|
|
|
|
case JSOP_GVARINC:
|
|
|
|
case JSOP_GVARDEC:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
do_atominc:
|
|
|
|
lval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, lval);
|
|
|
|
do_lvalinc:
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
lval, js_incop_strs[!(cs->format & JOF_INC)]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_PROPINC:
|
|
|
|
case JSOP_PROPDEC:
|
|
|
|
GET_ATOM_QUOTE_AND_FMT(postindex_format, postdot_format, rval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force precedence below the numeric literal opcodes, so that
|
|
|
|
* 42..foo or 10000..toString(16), e.g., decompile with parens
|
|
|
|
* around the left-hand side of dot.
|
|
|
|
*/
|
|
|
|
op = JSOP_GETPROP;
|
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, fmt, lval, rval,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ELEMINC:
|
|
|
|
case JSOP_ELEMDEC:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
xval = POP_STR();
|
|
|
|
op = JSOP_GETELEM;
|
|
|
|
lval = POP_STR();
|
|
|
|
if (*xval != '\0') {
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_OPMODE(lastop) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? postdot_format
|
|
|
|
: postindex_format,
|
|
|
|
lval, xval,
|
|
|
|
js_incop_strs[!(cs->format & JOF_INC)]);
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter, ss_format,
|
|
|
|
lval, js_incop_strs[!(cs->format & JOF_INC)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
case JSOP_LENGTH:
|
|
|
|
fmt = dot_format;
|
|
|
|
rval = js_length_str;
|
|
|
|
goto do_getprop_lval;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_GETPROP2:
|
|
|
|
op = JSOP_GETPROP;
|
|
|
|
(void) PopOff(ss, lastop);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_CALLPROP:
|
|
|
|
case JSOP_GETPROP:
|
|
|
|
case JSOP_GETXPROP:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
|
|
|
|
do_getprop:
|
|
|
|
GET_QUOTE_AND_FMT(index_format, dot_format, rval);
|
2008-02-07 15:18:45 -08:00
|
|
|
do_getprop_lval:
|
2008-10-21 11:35:22 -07:00
|
|
|
PROPAGATE_CALLNESS();
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, fmt, lval, rval);
|
|
|
|
break;
|
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_GETTHISPROP:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
GET_QUOTE_AND_FMT(index_format, dot_format, rval);
|
|
|
|
todo = Sprint(&ss->sprinter, fmt, js_this_str, rval);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_GETARGPROP:
|
|
|
|
/* Get the name of the argument or variable. */
|
2008-08-06 07:13:22 -07:00
|
|
|
i = GET_ARGNO(pc);
|
|
|
|
|
|
|
|
do_getarg_prop:
|
|
|
|
atom = GetArgOrVarAtom(ss->printer, i);
|
2007-11-19 09:15:45 -08:00
|
|
|
LOCAL_ASSERT(atom);
|
2008-01-29 22:27:13 -08:00
|
|
|
LOCAL_ASSERT(ATOM_IS_STRING(atom));
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!lval || !PushOff(ss, STR2OFF(&ss->sprinter, lval), op))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2007-11-22 00:06:00 -08:00
|
|
|
|
|
|
|
/* Get the name of the property. */
|
|
|
|
LOAD_ATOM(ARGNO_LEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_getprop;
|
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_GETLOCALPROP:
|
2008-08-06 07:13:22 -07:00
|
|
|
if (IsVarSlot(jp, pc, &i))
|
|
|
|
goto do_getarg_prop;
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCAL_ASSERT((uintN)i < ss->top);
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = GetLocal(ss, i);
|
|
|
|
if (!lval)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, lval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (todo < 0 || !PushOff(ss, todo, op))
|
|
|
|
return NULL;
|
2008-08-06 07:13:22 -07:00
|
|
|
LOAD_ATOM(2);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_getprop;
|
|
|
|
|
|
|
|
case JSOP_SETPROP:
|
2009-09-03 14:41:19 -07:00
|
|
|
case JSOP_SETMETHOD:
|
2007-03-22 10:30:00 -07:00
|
|
|
LOAD_ATOM(0);
|
|
|
|
GET_QUOTE_AND_FMT("%s[%s] %s= %s", "%s.%s %s= %s", xval);
|
|
|
|
rval = POP_STR();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force precedence below the numeric literal opcodes, so that
|
|
|
|
* 42..foo or 10000..toString(16), e.g., decompile with parens
|
|
|
|
* around the left-hand side of dot.
|
|
|
|
*/
|
|
|
|
op = JSOP_GETPROP;
|
|
|
|
lval = POP_STR();
|
|
|
|
sn = js_GetSrcNote(jp->script, pc - 1);
|
|
|
|
todo = Sprint(&ss->sprinter, fmt, lval, xval,
|
|
|
|
(sn && SN_TYPE(sn) == SRC_ASSIGNOP)
|
|
|
|
? (lastop == JSOP_GETTER)
|
|
|
|
? js_getter_str
|
|
|
|
: (lastop == JSOP_SETTER)
|
|
|
|
? js_setter_str
|
2007-05-06 03:50:07 -07:00
|
|
|
: CodeToken[lastop]
|
2007-03-22 10:30:00 -07:00
|
|
|
: "",
|
|
|
|
rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_GETELEM2:
|
|
|
|
op = JSOP_GETELEM;
|
|
|
|
(void) PopOff(ss, lastop);
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
case JSOP_CALLELEM:
|
|
|
|
case JSOP_GETELEM:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
xval = POP_STR();
|
|
|
|
op = saveop;
|
2008-10-21 11:35:22 -07:00
|
|
|
PROPAGATE_CALLNESS();
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
if (*xval == '\0') {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s", lval);
|
|
|
|
} else {
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_OPMODE(lastop) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? dot_format
|
|
|
|
: index_format,
|
|
|
|
lval, xval);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SETELEM:
|
|
|
|
rval = POP_STR();
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
xval = POP_STR();
|
|
|
|
cs = &js_CodeSpec[ss->opcodes[ss->top]];
|
|
|
|
op = JSOP_GETELEM; /* lval must have high precedence */
|
|
|
|
lval = POP_STR();
|
|
|
|
op = saveop;
|
|
|
|
if (*xval == '\0')
|
|
|
|
goto do_setlval;
|
|
|
|
sn = js_GetSrcNote(jp->script, pc - 1);
|
|
|
|
todo = Sprint(&ss->sprinter,
|
2007-05-22 23:04:18 -07:00
|
|
|
(JOF_MODE(cs->format) == JOF_XMLNAME)
|
2007-03-22 10:30:00 -07:00
|
|
|
? "%s.%s %s= %s"
|
|
|
|
: "%s[%s] %s= %s",
|
|
|
|
lval, xval,
|
|
|
|
(sn && SN_TYPE(sn) == SRC_ASSIGNOP)
|
|
|
|
? (lastop == JSOP_GETTER)
|
|
|
|
? js_getter_str
|
|
|
|
: (lastop == JSOP_SETTER)
|
|
|
|
? js_setter_str
|
2007-05-06 03:50:07 -07:00
|
|
|
: CodeToken[lastop]
|
2007-03-22 10:30:00 -07:00
|
|
|
: "",
|
|
|
|
rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ARGSUB:
|
|
|
|
i = (jsint) GET_ARGNO(pc);
|
|
|
|
todo = Sprint(&ss->sprinter, "%s[%d]",
|
|
|
|
js_arguments_str, (int) i);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ARGCNT:
|
|
|
|
todo = Sprint(&ss->sprinter, dot_format,
|
|
|
|
js_arguments_str, js_length_str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_CALLARG:
|
|
|
|
case JSOP_GETARG:
|
|
|
|
i = GET_ARGNO(pc);
|
2008-08-06 07:13:22 -07:00
|
|
|
atom = GetArgOrVarAtom(jp, i);
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
if (!atom) {
|
|
|
|
todo = Sprint(&ss->sprinter, "%s[%d]", js_arguments_str, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
LOCAL_ASSERT(atom);
|
|
|
|
#endif
|
|
|
|
goto do_name;
|
|
|
|
|
|
|
|
case JSOP_CALLNAME:
|
|
|
|
case JSOP_NAME:
|
|
|
|
case JSOP_GETGVAR:
|
|
|
|
case JSOP_CALLGVAR:
|
|
|
|
LOAD_ATOM(0);
|
|
|
|
do_name:
|
|
|
|
lval = "";
|
2007-12-04 01:08:13 -08:00
|
|
|
#if JS_HAS_XML_SUPPORT
|
2007-03-22 10:30:00 -07:00
|
|
|
do_qname:
|
2007-12-04 01:08:13 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
|
|
|
inXML ? DONT_ESCAPE : 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
2008-10-22 12:47:51 -07:00
|
|
|
todo = Sprint(&ss->sprinter, sss_format,
|
2007-03-22 10:30:00 -07:00
|
|
|
VarPrefix(sn), lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_UINT16:
|
|
|
|
i = (jsint) GET_UINT16(pc);
|
|
|
|
goto do_sprint_int;
|
|
|
|
|
|
|
|
case JSOP_UINT24:
|
|
|
|
i = (jsint) GET_UINT24(pc);
|
2007-07-11 02:25:45 -07:00
|
|
|
goto do_sprint_int;
|
|
|
|
|
|
|
|
case JSOP_INT8:
|
|
|
|
i = GET_INT8(pc);
|
|
|
|
goto do_sprint_int;
|
|
|
|
|
|
|
|
case JSOP_INT32:
|
|
|
|
i = GET_INT32(pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
do_sprint_int:
|
2007-07-11 02:25:45 -07:00
|
|
|
todo = Sprint(&ss->sprinter, "%d", i);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_DOUBLE:
|
2009-09-04 16:27:17 -07:00
|
|
|
GET_DOUBLE_FROM_BYTECODE(jp->script, pc, 0, atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
val = ATOM_KEY(atom);
|
2008-01-29 22:27:13 -08:00
|
|
|
LOCAL_ASSERT(JSVAL_IS_DOUBLE(val));
|
2007-07-11 02:25:45 -07:00
|
|
|
todo = SprintDoubleValue(&ss->sprinter, val, &saveop);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_STRING:
|
|
|
|
LOAD_ATOM(0);
|
2009-11-30 04:33:04 -08:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
|
|
|
inXML ? DONT_ESCAPE : '"');
|
|
|
|
if (!rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-11-30 04:33:04 -08:00
|
|
|
todo = STR2OFF(&ss->sprinter, rval);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
case JSOP_LAMBDA:
|
|
|
|
case JSOP_LAMBDA_FC:
|
2009-06-04 18:58:47 -07:00
|
|
|
case JSOP_LAMBDA_DBGFC:
|
2007-05-22 23:00:40 -07:00
|
|
|
#if JS_HAS_GENERATOR_EXPRS
|
2007-05-17 18:41:17 -07:00
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_GENEXP) {
|
2009-04-04 10:14:52 -07:00
|
|
|
void *mark;
|
2009-04-05 21:17:22 -07:00
|
|
|
jsuword *innerLocalNames, *outerLocalNames;
|
|
|
|
JSScript *inner, *outer;
|
2007-05-17 18:41:17 -07:00
|
|
|
SprintStack ss2;
|
2009-04-05 21:17:22 -07:00
|
|
|
JSFunction *outerfun;
|
2007-05-17 18:41:17 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
LOAD_FUNCTION(0);
|
2007-05-17 18:41:17 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All allocation when decompiling is LIFO, using malloc
|
2009-01-10 15:25:03 -08:00
|
|
|
* or, more commonly, arena-allocating from cx->tempPool.
|
2009-04-05 21:17:22 -07:00
|
|
|
* Therefore after InitSprintStack succeeds, we must
|
|
|
|
* release to mark before returning.
|
2007-05-17 18:41:17 -07:00
|
|
|
*/
|
2010-05-11 08:52:17 -07:00
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
2009-05-29 05:23:58 -07:00
|
|
|
if (!fun->hasLocalNames()) {
|
2009-04-05 21:17:22 -07:00
|
|
|
innerLocalNames = NULL;
|
|
|
|
} else {
|
|
|
|
innerLocalNames = js_GetLocalNameArray(cx, fun, &cx->tempPool);
|
|
|
|
if (!innerLocalNames)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inner = fun->u.i.script;
|
|
|
|
if (!InitSprintStack(cx, &ss2, jp, StackDepth(inner))) {
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-05-17 18:41:17 -07:00
|
|
|
return NULL;
|
2009-04-05 21:17:22 -07:00
|
|
|
}
|
2007-05-17 18:41:17 -07:00
|
|
|
ss2.inGenExp = JS_TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively decompile this generator function as an
|
|
|
|
* un-parenthesized generator expression. The ss->inGenExp
|
|
|
|
* special case of JSOP_YIELD shares array comprehension
|
|
|
|
* decompilation code that leaves the result as the single
|
|
|
|
* string pushed on ss2.
|
|
|
|
*/
|
|
|
|
outer = jp->script;
|
2009-04-05 21:17:22 -07:00
|
|
|
outerfun = jp->fun;
|
|
|
|
outerLocalNames = jp->localNames;
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(JS_UPTRDIFF(pc, outer->code) <= outer->length);
|
|
|
|
jp->script = inner;
|
2009-04-05 21:17:22 -07:00
|
|
|
jp->fun = fun;
|
|
|
|
jp->localNames = innerLocalNames;
|
|
|
|
ok = Decompile(&ss2, inner->code, inner->length, JSOP_NOP) != NULL;
|
|
|
|
jp->script = outer;
|
|
|
|
jp->fun = outerfun;
|
|
|
|
jp->localNames = outerLocalNames;
|
|
|
|
if (!ok) {
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-05-17 18:41:17 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-08-01 21:33:52 -07:00
|
|
|
* Advance over this op and its global |this| push, and
|
2007-05-17 18:41:17 -07:00
|
|
|
* arrange to advance over the call to this lambda.
|
|
|
|
*/
|
|
|
|
pc += len;
|
2009-04-05 21:17:22 -07:00
|
|
|
LOCAL_ASSERT(*pc == JSOP_NULL);
|
2007-05-17 18:41:17 -07:00
|
|
|
pc += JSOP_NULL_LENGTH;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_CALL);
|
|
|
|
LOCAL_ASSERT(GET_ARGC(pc) == 0);
|
|
|
|
len = JSOP_CALL_LENGTH;
|
2008-10-08 19:46:12 -07:00
|
|
|
|
2007-05-17 18:41:17 -07:00
|
|
|
/*
|
|
|
|
* Arrange to parenthesize this genexp unless:
|
|
|
|
*
|
2007-05-22 23:00:40 -07:00
|
|
|
* 1. It is the complete expression consumed by a control
|
|
|
|
* flow bytecode such as JSOP_TABLESWITCH whose syntax
|
|
|
|
* always parenthesizes the controlling expression.
|
2009-02-25 09:36:39 -08:00
|
|
|
* 2. It is the sole argument to a function call.
|
2007-05-17 18:41:17 -07:00
|
|
|
*
|
2009-02-25 09:36:39 -08:00
|
|
|
* But if this genexp runs up against endpc, parenthesize
|
|
|
|
* regardless. (This can happen if we are called from
|
|
|
|
* DecompileExpression or recursively from case
|
|
|
|
* JSOP_{NOP,AND,OR}.)
|
|
|
|
*
|
|
|
|
* There's no special case for |if (genexp)| because the
|
|
|
|
* compiler optimizes that to |if (true)|.
|
2007-05-17 18:41:17 -07:00
|
|
|
*/
|
2008-06-26 17:49:01 -07:00
|
|
|
pc2 = pc + len;
|
2009-10-02 12:36:27 -07:00
|
|
|
op = JSOp(*pc2);
|
|
|
|
if (op == JSOP_TRACE || op == JSOP_NOP)
|
|
|
|
pc2 += JSOP_NOP_LENGTH;
|
2008-06-26 17:49:01 -07:00
|
|
|
LOCAL_ASSERT(pc2 < endpc ||
|
2007-05-22 23:00:40 -07:00
|
|
|
endpc < outer->code + outer->length);
|
2007-05-17 18:41:17 -07:00
|
|
|
LOCAL_ASSERT(ss2.top == 1);
|
|
|
|
ss2.opcodes[0] = JSOP_POP;
|
2009-02-25 09:36:39 -08:00
|
|
|
if (pc2 == endpc) {
|
2007-05-22 23:00:40 -07:00
|
|
|
op = JSOP_SETNAME;
|
|
|
|
} else {
|
2008-06-26 17:49:01 -07:00
|
|
|
op = (JSOp) *pc2;
|
2007-05-22 23:00:40 -07:00
|
|
|
op = ((js_CodeSpec[op].format & JOF_PARENHEAD) ||
|
2009-02-25 09:36:39 -08:00
|
|
|
((js_CodeSpec[op].format & JOF_INVOKE) && GET_ARGC(pc2) == 1))
|
2007-05-22 23:00:40 -07:00
|
|
|
? JSOP_POP
|
|
|
|
: JSOP_SETNAME;
|
2007-05-29 18:49:42 -07:00
|
|
|
|
2008-02-17 16:01:53 -08:00
|
|
|
/*
|
|
|
|
* Stack this result as if it's a name and not an
|
|
|
|
* anonymous function, so it doesn't get decompiled as
|
|
|
|
* a generator function in a getter or setter context.
|
|
|
|
* The precedence level is the same for JSOP_NAME and
|
2009-04-05 21:17:22 -07:00
|
|
|
* JSOP_LAMBDA.
|
2008-02-17 16:01:53 -08:00
|
|
|
*/
|
|
|
|
LOCAL_ASSERT(js_CodeSpec[JSOP_NAME].prec ==
|
|
|
|
js_CodeSpec[saveop].prec);
|
|
|
|
saveop = JSOP_NAME;
|
2007-05-22 23:00:40 -07:00
|
|
|
}
|
2007-05-17 18:41:17 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Alas, we have to malloc a copy of the result left on
|
|
|
|
* the top of ss2 because both ss and ss2 arena-allocate
|
|
|
|
* from cx's tempPool.
|
|
|
|
*/
|
|
|
|
rval = JS_strdup(cx, PopStr(&ss2, op));
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-05-17 18:41:17 -07:00
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
todo = SprintCString(&ss->sprinter, rval);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((void *)rval);
|
2007-05-17 18:41:17 -07:00
|
|
|
break;
|
|
|
|
}
|
2007-05-22 23:00:40 -07:00
|
|
|
#endif /* JS_HAS_GENERATOR_EXPRS */
|
2007-07-08 02:03:34 -07:00
|
|
|
/* FALL THROUGH */
|
2007-05-17 18:41:17 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
LOAD_FUNCTION(0);
|
|
|
|
{
|
2007-05-29 18:49:42 -07:00
|
|
|
/*
|
|
|
|
* 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
|
2009-04-05 21:17:22 -07:00
|
|
|
* that checks for JSOP_LAMBDA.
|
2007-05-29 18:49:42 -07:00
|
|
|
*/
|
2009-11-05 14:34:39 -08:00
|
|
|
bool grouped = !(fun->flags & JSFUN_EXPR_CLOSURE);
|
2009-11-18 13:33:53 -08:00
|
|
|
bool strict = jp->script->strictModeCode;
|
2009-11-05 14:34:39 -08:00
|
|
|
str = js_DecompileToString(cx, "lambda", fun, 0,
|
2009-11-18 13:33:53 -08:00
|
|
|
false, grouped, strict,
|
2009-11-05 14:34:39 -08:00
|
|
|
js_DecompileFunction);
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!str)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
sprint_string:
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = SprintString(&ss->sprinter, str);
|
|
|
|
break;
|
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
case JSOP_CALLEE:
|
|
|
|
JS_ASSERT(jp->fun && jp->fun->atom);
|
|
|
|
todo = SprintString(&ss->sprinter, ATOM_TO_STRING(jp->fun->atom));
|
|
|
|
break;
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
case JSOP_OBJECT:
|
|
|
|
LOAD_OBJECT(0);
|
2010-04-07 16:09:49 -07:00
|
|
|
LOCAL_ASSERT(obj->getClass() == &js_RegExpClass);
|
2007-07-08 02:03:34 -07:00
|
|
|
goto do_regexp;
|
|
|
|
|
|
|
|
case JSOP_REGEXP:
|
|
|
|
GET_REGEXP_FROM_BYTECODE(jp->script, pc, 0, obj);
|
|
|
|
do_regexp:
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!js_regexp_toString(cx, obj, &val))
|
2007-07-08 02:03:34 -07:00
|
|
|
return NULL;
|
2007-08-01 21:33:52 -07:00
|
|
|
str = JSVAL_TO_STRING(val);
|
|
|
|
goto sprint_string;
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_TABLESWITCH:
|
|
|
|
case JSOP_TABLESWITCHX:
|
|
|
|
{
|
|
|
|
ptrdiff_t jmplen, off, off2;
|
|
|
|
jsint j, n, low, high;
|
|
|
|
TableEntry *table, *tmp;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
LOCAL_ASSERT(sn && SN_TYPE(sn) == SRC_SWITCH);
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
jmplen = (op == JSOP_TABLESWITCH) ? JUMP_OFFSET_LEN
|
|
|
|
: JUMPX_OFFSET_LEN;
|
|
|
|
pc2 = pc;
|
|
|
|
off = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
low = GET_JUMP_OFFSET(pc2);
|
|
|
|
pc2 += JUMP_OFFSET_LEN;
|
|
|
|
high = GET_JUMP_OFFSET(pc2);
|
|
|
|
pc2 += JUMP_OFFSET_LEN;
|
|
|
|
|
|
|
|
n = high - low + 1;
|
|
|
|
if (n == 0) {
|
|
|
|
table = NULL;
|
|
|
|
j = 0;
|
|
|
|
ok = JS_TRUE;
|
|
|
|
} else {
|
|
|
|
table = (TableEntry *)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc((size_t)n * sizeof *table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
for (i = j = 0; i < n; i++) {
|
|
|
|
table[j].label = NULL;
|
|
|
|
off2 = GetJumpOffset(pc, pc2);
|
|
|
|
if (off2) {
|
|
|
|
sn = js_GetSrcNote(jp->script, pc2);
|
|
|
|
if (sn) {
|
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_LABEL);
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, table[j].label);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
table[j].key = INT_TO_JSVAL(low + i);
|
|
|
|
table[j].offset = off2;
|
|
|
|
table[j].order = j;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
pc2 += jmplen;
|
|
|
|
}
|
|
|
|
tmp = (TableEntry *)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc((size_t)j * sizeof *table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tmp) {
|
2009-01-30 15:40:05 -08:00
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = js_MergeSort(table, (size_t)j, sizeof(TableEntry),
|
|
|
|
CompareOffsets, NULL, tmp);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(tmp);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
ok = DecompileSwitch(ss, table, (uintN)j, pc, len, off,
|
|
|
|
JS_FALSE);
|
|
|
|
}
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_LOOKUPSWITCH:
|
|
|
|
case JSOP_LOOKUPSWITCHX:
|
|
|
|
{
|
|
|
|
ptrdiff_t jmplen, off, off2;
|
|
|
|
jsatomid npairs, k;
|
|
|
|
TableEntry *table;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
LOCAL_ASSERT(sn && SN_TYPE(sn) == SRC_SWITCH);
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
jmplen = (op == JSOP_LOOKUPSWITCH) ? JUMP_OFFSET_LEN
|
|
|
|
: JUMPX_OFFSET_LEN;
|
|
|
|
pc2 = pc;
|
|
|
|
off = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
npairs = GET_UINT16(pc2);
|
|
|
|
pc2 += UINT16_LEN;
|
|
|
|
|
|
|
|
table = (TableEntry *)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc((size_t)npairs * sizeof *table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
for (k = 0; k < npairs; k++) {
|
|
|
|
sn = js_GetSrcNote(jp->script, pc2);
|
|
|
|
if (sn) {
|
|
|
|
LOCAL_ASSERT(SN_TYPE(sn) == SRC_LABEL);
|
2007-08-07 00:29:32 -07:00
|
|
|
GET_SOURCE_NOTE_ATOM(sn, table[k].label);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
table[k].label = NULL;
|
|
|
|
}
|
2009-09-04 16:27:17 -07:00
|
|
|
JS_GET_SCRIPT_ATOM(jp->script, pc, GET_INDEX(pc2), atom);
|
2007-07-08 02:03:34 -07:00
|
|
|
pc2 += INDEX_LEN;
|
2007-03-22 10:30:00 -07:00
|
|
|
off2 = GetJumpOffset(pc, pc2);
|
|
|
|
pc2 += jmplen;
|
|
|
|
table[k].key = ATOM_KEY(atom);
|
|
|
|
table[k].offset = off2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = DecompileSwitch(ss, table, (uintN)npairs, pc, len, off,
|
|
|
|
JS_FALSE);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_CONDSWITCH:
|
|
|
|
{
|
|
|
|
ptrdiff_t off, off2, caseOff;
|
|
|
|
jsint ncases;
|
|
|
|
TableEntry *table;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
LOCAL_ASSERT(sn && SN_TYPE(sn) == SRC_SWITCH);
|
|
|
|
len = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
off = js_GetSrcNoteOffset(sn, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the cases using offsets from switch to first case,
|
|
|
|
* and case to case, stored in srcnote immediates.
|
|
|
|
*/
|
|
|
|
pc2 = pc;
|
|
|
|
off2 = off;
|
|
|
|
for (ncases = 0; off2 != 0; ncases++) {
|
|
|
|
pc2 += off2;
|
|
|
|
LOCAL_ASSERT(*pc2 == JSOP_CASE || *pc2 == JSOP_DEFAULT ||
|
|
|
|
*pc2 == JSOP_CASEX || *pc2 == JSOP_DEFAULTX);
|
|
|
|
if (*pc2 == JSOP_DEFAULT || *pc2 == JSOP_DEFAULTX) {
|
|
|
|
/* End of cases, but count default as a case. */
|
|
|
|
off2 = 0;
|
|
|
|
} else {
|
|
|
|
sn = js_GetSrcNote(jp->script, pc2);
|
|
|
|
LOCAL_ASSERT(sn && SN_TYPE(sn) == SRC_PCDELTA);
|
|
|
|
off2 = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate table and rescan the cases using their srcnotes,
|
|
|
|
* stashing each case's delta from switch top in table[i].key,
|
|
|
|
* and the distance to its statements in table[i].offset.
|
|
|
|
*/
|
|
|
|
table = (TableEntry *)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc((size_t)ncases * sizeof *table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!table)
|
|
|
|
return NULL;
|
|
|
|
pc2 = pc;
|
|
|
|
off2 = off;
|
|
|
|
for (i = 0; i < ncases; i++) {
|
|
|
|
pc2 += off2;
|
|
|
|
LOCAL_ASSERT(*pc2 == JSOP_CASE || *pc2 == JSOP_DEFAULT ||
|
|
|
|
*pc2 == JSOP_CASEX || *pc2 == JSOP_DEFAULTX);
|
|
|
|
caseOff = pc2 - pc;
|
|
|
|
table[i].key = INT_TO_JSVAL((jsint) caseOff);
|
|
|
|
table[i].offset = caseOff + GetJumpOffset(pc2, pc2);
|
|
|
|
if (*pc2 == JSOP_CASE || *pc2 == JSOP_CASEX) {
|
|
|
|
sn = js_GetSrcNote(jp->script, pc2);
|
|
|
|
LOCAL_ASSERT(sn && SN_TYPE(sn) == SRC_PCDELTA);
|
|
|
|
off2 = js_GetSrcNoteOffset(sn, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find offset of default code by fetching the default offset
|
|
|
|
* from the end of table. JSOP_CONDSWITCH always has a default
|
|
|
|
* case at the end.
|
|
|
|
*/
|
|
|
|
off = JSVAL_TO_INT(table[ncases-1].key);
|
|
|
|
pc2 = pc + off;
|
|
|
|
off += GetJumpOffset(pc2, pc2);
|
|
|
|
|
|
|
|
ok = DecompileSwitch(ss, table, (uintN)ncases, pc, len, off,
|
|
|
|
JS_TRUE);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(table);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_CASE:
|
|
|
|
case JSOP_CASEX:
|
|
|
|
{
|
|
|
|
lval = POP_STR();
|
|
|
|
if (!lval)
|
|
|
|
return NULL;
|
|
|
|
js_printf(jp, "\tcase %s:\n", lval);
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-08 04:58:17 -07:00
|
|
|
case JSOP_DEFFUN:
|
2009-04-05 21:17:22 -07:00
|
|
|
case JSOP_DEFFUN_FC:
|
2009-06-04 18:58:47 -07:00
|
|
|
case JSOP_DEFFUN_DBGFC:
|
2007-07-08 02:03:34 -07:00
|
|
|
LOAD_FUNCTION(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = -2;
|
|
|
|
goto do_function;
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_TRAP:
|
2008-02-17 17:32:12 -08:00
|
|
|
saveop = op = JS_GetTrapOpcode(cx, jp->script, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
*pc = op;
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
len = cs->length;
|
|
|
|
DECOMPILE_CODE(pc, len);
|
|
|
|
*pc = JSOP_TRAP;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
2008-06-04 17:00:46 -07:00
|
|
|
case JSOP_HOLE:
|
|
|
|
todo = SprintPut(&ss->sprinter, "", 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_NEWARRAY:
|
2009-04-10 17:41:13 -07:00
|
|
|
argc = GET_UINT16(pc);
|
2008-06-04 17:00:46 -07:00
|
|
|
LOCAL_ASSERT(ss->top >= (uintN) argc);
|
|
|
|
if (argc == 0) {
|
2009-01-10 12:15:08 -08:00
|
|
|
todo = SprintCString(&ss->sprinter, "[]");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
argv = (char **) cx->malloc(size_t(argc) * sizeof *argv);
|
2009-01-10 12:15:08 -08:00
|
|
|
if (!argv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
op = JSOP_SETNAME;
|
|
|
|
ok = JS_TRUE;
|
|
|
|
i = argc;
|
|
|
|
while (i > 0)
|
|
|
|
argv[--i] = JS_strdup(cx, POP_STR());
|
|
|
|
|
|
|
|
todo = SprintCString(&ss->sprinter, "[");
|
|
|
|
if (todo < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (!argv[i] ||
|
|
|
|
Sprint(&ss->sprinter, ss_format,
|
|
|
|
argv[i], (i < argc - 1) ? ", " : "") < 0) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
break;
|
2008-06-04 17:00:46 -07:00
|
|
|
}
|
|
|
|
}
|
2009-01-10 12:15:08 -08:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(argv[i]);
|
|
|
|
cx->free(argv);
|
2009-01-10 12:15:08 -08:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_CONTINUE && SprintCString(&ss->sprinter, ", ") < 0)
|
|
|
|
return NULL;
|
|
|
|
if (SprintCString(&ss->sprinter, "]") < 0)
|
|
|
|
return NULL;
|
2008-06-04 17:00:46 -07:00
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSOP_NEWINIT:
|
|
|
|
{
|
2007-12-03 19:05:24 -08:00
|
|
|
i = GET_INT8(pc);
|
2008-01-29 22:27:13 -08:00
|
|
|
LOCAL_ASSERT(i == JSProto_Array || i == JSProto_Object);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
todo = ss->sprinter.offset;
|
|
|
|
#if JS_HAS_SHARP_VARS
|
|
|
|
op = (JSOp)pc[len];
|
2009-09-09 20:21:15 -07:00
|
|
|
if (op == JSOP_SHARPINIT)
|
|
|
|
op = (JSOp)pc[len += JSOP_SHARPINIT_LENGTH];
|
2007-03-22 10:30:00 -07:00
|
|
|
if (op == JSOP_DEFSHARP) {
|
|
|
|
pc += len;
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
len = cs->length;
|
2007-12-03 19:05:24 -08:00
|
|
|
if (Sprint(&ss->sprinter, "#%u=",
|
2009-09-09 20:21:15 -07:00
|
|
|
(unsigned) (jsint) GET_UINT16(pc + UINT16_LEN))
|
|
|
|
< 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-09-09 20:21:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif /* JS_HAS_SHARP_VARS */
|
2007-12-03 19:05:24 -08:00
|
|
|
if (i == JSProto_Array) {
|
2007-03-22 10:30:00 -07:00
|
|
|
++ss->inArrayInit;
|
|
|
|
if (SprintCString(&ss->sprinter, "[") < 0)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
if (SprintCString(&ss->sprinter, "{") < 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case JSOP_ENDINIT:
|
2007-12-03 19:05:24 -08:00
|
|
|
{
|
|
|
|
JSBool inArray;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
rval = POP_STR();
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
|
|
|
|
|
|
|
/* Skip any #n= prefix to find the opening bracket. */
|
|
|
|
for (xval = rval; *xval != '[' && *xval != '{'; xval++)
|
|
|
|
continue;
|
2007-12-03 19:05:24 -08:00
|
|
|
inArray = (*xval == '[');
|
|
|
|
if (inArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
--ss->inArrayInit;
|
|
|
|
todo = Sprint(&ss->sprinter, "%s%s%c",
|
|
|
|
rval,
|
|
|
|
(sn && SN_TYPE(sn) == SRC_CONTINUE) ? ", " : "",
|
2007-12-03 19:05:24 -08:00
|
|
|
inArray ? ']' : '}');
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2007-12-03 19:05:24 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-03 19:05:24 -08:00
|
|
|
{
|
|
|
|
JSBool isFirst;
|
2008-02-09 19:30:07 -08:00
|
|
|
const char *maybeComma;
|
|
|
|
|
|
|
|
case JSOP_INITELEM:
|
|
|
|
isFirst = (ss->opcodes[ss->top - 3] == JSOP_NEWINIT);
|
2008-10-29 11:36:08 -07:00
|
|
|
|
|
|
|
/* Turn off most parens. */
|
|
|
|
op = JSOP_SETNAME;
|
2008-02-09 19:30:07 -08:00
|
|
|
rval = POP_STR();
|
|
|
|
|
|
|
|
/* Turn off all parens for xval and lval, which we control. */
|
|
|
|
op = JSOP_NOP;
|
|
|
|
xval = POP_STR();
|
|
|
|
lval = POP_STR();
|
|
|
|
sn = js_GetSrcNote(jp->script, pc);
|
2007-12-03 19:05:24 -08:00
|
|
|
|
2008-02-09 19:30:07 -08:00
|
|
|
if (sn && SN_TYPE(sn) == SRC_INITPROP) {
|
|
|
|
atom = NULL;
|
|
|
|
goto do_initprop;
|
|
|
|
}
|
|
|
|
maybeComma = isFirst ? "" : ", ";
|
2008-10-22 12:47:51 -07:00
|
|
|
todo = Sprint(&ss->sprinter, sss_format,
|
2008-02-09 19:30:07 -08:00
|
|
|
lval,
|
|
|
|
maybeComma,
|
|
|
|
rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_INITPROP:
|
2009-09-03 14:41:19 -07:00
|
|
|
case JSOP_INITMETHOD:
|
2007-03-22 10:30:00 -07:00
|
|
|
LOAD_ATOM(0);
|
|
|
|
xval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
2009-12-14 15:31:05 -08:00
|
|
|
jschar(ATOM_IS_IDENTIFIER(atom) ? 0 : '\''));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!xval)
|
|
|
|
return NULL;
|
2007-12-03 19:05:24 -08:00
|
|
|
isFirst = (ss->opcodes[ss->top - 2] == JSOP_NEWINIT);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
2008-02-09 19:30:07 -08:00
|
|
|
/* fall through */
|
2007-12-03 19:05:24 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
do_initprop:
|
2008-02-09 19:30:07 -08:00
|
|
|
maybeComma = isFirst ? "" : ", ";
|
2007-03-22 10:30:00 -07:00
|
|
|
if (lastop == JSOP_GETTER || lastop == JSOP_SETTER) {
|
2009-10-06 12:35:33 -07:00
|
|
|
const char *end = rval + strlen(rval);
|
|
|
|
|
|
|
|
if (*rval == '(')
|
|
|
|
++rval, --end;
|
|
|
|
LOCAL_ASSERT(strncmp(rval, js_function_str, 8) == 0);
|
|
|
|
LOCAL_ASSERT(rval[8] == ' ');
|
|
|
|
rval += 8 + 1;
|
|
|
|
LOCAL_ASSERT(*end ? *end == ')' : end[-1] == '}');
|
|
|
|
todo = Sprint(&ss->sprinter, "%s%s%s %s%s%.*s",
|
|
|
|
lval,
|
|
|
|
maybeComma,
|
|
|
|
(lastop == JSOP_GETTER)
|
|
|
|
? js_get_str : js_set_str,
|
|
|
|
xval,
|
|
|
|
(rval[0] != '(') ? " " : "",
|
|
|
|
end - rval, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-02-09 19:30:07 -08:00
|
|
|
todo = Sprint(&ss->sprinter, "%s%s%s: %s",
|
|
|
|
lval, maybeComma, xval, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
2007-12-03 19:05:24 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if JS_HAS_SHARP_VARS
|
|
|
|
case JSOP_DEFSHARP:
|
2009-09-09 20:21:15 -07:00
|
|
|
i = (jsint) GET_UINT16(pc + UINT16_LEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "#%u=%s", (unsigned) i, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_USESHARP:
|
2009-09-09 20:21:15 -07:00
|
|
|
i = (jsint) GET_UINT16(pc + UINT16_LEN);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = Sprint(&ss->sprinter, "#%u#", (unsigned) i);
|
|
|
|
break;
|
|
|
|
#endif /* JS_HAS_SHARP_VARS */
|
|
|
|
|
|
|
|
#if JS_HAS_DEBUGGER_KEYWORD
|
|
|
|
case JSOP_DEBUGGER:
|
|
|
|
js_printf(jp, "\tdebugger;\n");
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
#endif /* JS_HAS_DEBUGGER_KEYWORD */
|
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
case JSOP_STARTXML:
|
|
|
|
case JSOP_STARTXMLEXPR:
|
|
|
|
inXML = op == JSOP_STARTXML;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DEFXMLNS:
|
|
|
|
rval = POP_STR();
|
|
|
|
js_printf(jp, "\t%s %s %s = %s;\n",
|
|
|
|
js_default_str, js_xml_str, js_namespace_str, rval);
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ANYNAME:
|
|
|
|
if (pc[JSOP_ANYNAME_LENGTH] == JSOP_TOATTRNAME) {
|
|
|
|
len += JSOP_TOATTRNAME_LENGTH;
|
|
|
|
todo = SprintPut(&ss->sprinter, "@*", 2);
|
|
|
|
} else {
|
|
|
|
todo = SprintPut(&ss->sprinter, "*", 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_QNAMEPART:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pc[JSOP_QNAMEPART_LENGTH] == JSOP_TOATTRNAME) {
|
|
|
|
saveop = JSOP_TOATTRNAME;
|
|
|
|
len += JSOP_TOATTRNAME_LENGTH;
|
|
|
|
lval = "@";
|
|
|
|
goto do_qname;
|
|
|
|
}
|
|
|
|
goto do_name;
|
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_QNAMECONST:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0);
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
RETRACT(&ss->sprinter, rval);
|
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s::%s", lval, rval);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_QNAME:
|
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s::[%s]", lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_TOATTRNAME:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
rval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "@[%s]", rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_TOATTRVAL:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ADDATTRNAME:
|
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s %s", lval, rval);
|
|
|
|
/* This gets reset by all XML tag expressions. */
|
|
|
|
quoteAttr = JS_TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ADDATTRVAL:
|
|
|
|
rval = POP_STR();
|
|
|
|
lval = POP_STR();
|
|
|
|
if (quoteAttr)
|
|
|
|
todo = Sprint(&ss->sprinter, "%s=\"%s\"", lval, rval);
|
|
|
|
else
|
|
|
|
todo = Sprint(&ss->sprinter, "%s=%s", lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_BINDXMLNAME:
|
|
|
|
/* Leave the name stacked and push a dummy string. */
|
|
|
|
todo = Sprint(&ss->sprinter, "");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SETXMLNAME:
|
|
|
|
/* Pop the r.h.s., the dummy string, and the name. */
|
|
|
|
rval = POP_STR();
|
|
|
|
(void) PopOff(ss, op);
|
|
|
|
lval = POP_STR();
|
|
|
|
goto do_setlval;
|
|
|
|
|
|
|
|
case JSOP_XMLELTEXPR:
|
|
|
|
case JSOP_XMLTAGEXPR:
|
|
|
|
todo = Sprint(&ss->sprinter, "{%s}", POP_STR());
|
|
|
|
inXML = JS_TRUE;
|
|
|
|
/* If we're an attribute value, we shouldn't quote this. */
|
|
|
|
quoteAttr = JS_FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_TOXMLLIST:
|
|
|
|
op = JSOP_NOP; /* turn off parens */
|
|
|
|
todo = Sprint(&ss->sprinter, "<>%s</>", POP_STR());
|
|
|
|
inXML = JS_FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_TOXML:
|
|
|
|
case JSOP_CALLXMLNAME:
|
|
|
|
case JSOP_XMLNAME:
|
|
|
|
case JSOP_FILTER:
|
|
|
|
/* These ops indicate the end of XML expressions. */
|
|
|
|
inXML = JS_FALSE;
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_ENDFILTER:
|
|
|
|
rval = POP_STR();
|
2008-10-21 11:35:22 -07:00
|
|
|
PROPAGATE_CALLNESS();
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s.(%s)", lval, rval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DESCENDANTS:
|
|
|
|
rval = POP_STR();
|
2008-10-21 11:35:22 -07:00
|
|
|
PROPAGATE_CALLNESS();
|
2007-03-22 10:30:00 -07:00
|
|
|
lval = POP_STR();
|
|
|
|
todo = Sprint(&ss->sprinter, "%s..%s", lval, rval);
|
|
|
|
break;
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
case JSOP_XMLOBJECT:
|
|
|
|
LOAD_OBJECT(0);
|
|
|
|
todo = Sprint(&ss->sprinter, "<xml address='%p'>", obj);
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_XMLCDATA:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = SprintPut(&ss->sprinter, "<![CDATA[", 9);
|
|
|
|
if (!QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
|
|
|
DONT_ESCAPE))
|
|
|
|
return NULL;
|
|
|
|
SprintPut(&ss->sprinter, "]]>", 3);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_XMLCOMMENT:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
todo = SprintPut(&ss->sprinter, "<!--", 4);
|
|
|
|
if (!QuoteString(&ss->sprinter, ATOM_TO_STRING(atom),
|
|
|
|
DONT_ESCAPE))
|
|
|
|
return NULL;
|
|
|
|
SprintPut(&ss->sprinter, "-->", 3);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-22 00:06:00 -08:00
|
|
|
case JSOP_XMLPI:
|
|
|
|
LOAD_ATOM(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
rval = JS_strdup(cx, POP_STR());
|
|
|
|
if (!rval)
|
|
|
|
return NULL;
|
|
|
|
todo = SprintPut(&ss->sprinter, "<?", 2);
|
|
|
|
ok = QuoteString(&ss->sprinter, ATOM_TO_STRING(atom), 0) &&
|
|
|
|
(*rval == '\0' ||
|
|
|
|
(SprintPut(&ss->sprinter, " ", 1) >= 0 &&
|
|
|
|
SprintCString(&ss->sprinter, rval)));
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((char *)rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
SprintPut(&ss->sprinter, "?>", 2);
|
2007-11-22 00:06:00 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case JSOP_GETFUNNS:
|
|
|
|
todo = SprintPut(&ss->sprinter, js_function_str, 8);
|
|
|
|
break;
|
|
|
|
#endif /* JS_HAS_XML_SUPPORT */
|
|
|
|
|
|
|
|
default:
|
|
|
|
todo = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (todo < 0) {
|
|
|
|
/* -2 means "don't push", -1 means reported error. */
|
|
|
|
if (todo == -1)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
if (!PushOff(ss, todo, saveop))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cs->format & JOF_CALLOP) {
|
|
|
|
todo = Sprint(&ss->sprinter, "");
|
|
|
|
if (todo < 0 || !PushOff(ss, todo, saveop))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undefine local macros.
|
|
|
|
*/
|
|
|
|
#undef inXML
|
|
|
|
#undef DECOMPILE_CODE
|
2007-05-29 18:49:42 -07:00
|
|
|
#undef NEXT_OP
|
2008-10-28 14:04:29 -07:00
|
|
|
#undef TOP_STR
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef POP_STR
|
2008-10-28 14:04:29 -07:00
|
|
|
#undef POP_STR_PREC
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
#undef ATOM_IS_IDENTIFIER
|
|
|
|
#undef GET_QUOTE_AND_FMT
|
|
|
|
#undef GET_ATOM_QUOTE_AND_FMT
|
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2008-03-12 16:03:29 -07:00
|
|
|
static JSBool
|
|
|
|
DecompileCode(JSPrinter *jp, JSScript *script, jsbytecode *pc, uintN len,
|
|
|
|
uintN pcdepth)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
uintN depth, i;
|
|
|
|
SprintStack ss;
|
|
|
|
JSContext *cx;
|
|
|
|
void *mark;
|
|
|
|
JSBool ok;
|
|
|
|
JSScript *oldscript;
|
2008-04-29 10:26:31 -07:00
|
|
|
jsbytecode *oldcode, *oldmain, *code;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *last;
|
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
depth = StackDepth(script);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(pcdepth <= depth);
|
|
|
|
|
|
|
|
/* Initialize a sprinter for use with the offset stack. */
|
|
|
|
cx = jp->sprinter.context;
|
2010-05-11 08:52:17 -07:00
|
|
|
mark = JS_ARENA_MARK(&cx->tempPool);
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = InitSprintStack(cx, &ss, jp, depth);
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are called from js_DecompileValueGenerator with a portion of
|
|
|
|
* script's bytecode that starts with a non-zero model stack depth given
|
|
|
|
* by pcdepth, attempt to initialize the missing string offsets in ss to
|
|
|
|
* |spindex| negative indexes from fp->sp for the activation fp in which
|
|
|
|
* the error arose.
|
|
|
|
*
|
|
|
|
* See js_DecompileValueGenerator for how its |spindex| parameter is used,
|
|
|
|
* and see also GetOff, which makes use of the ss.offsets[i] < -1 that are
|
|
|
|
* potentially stored below.
|
|
|
|
*/
|
|
|
|
ss.top = pcdepth;
|
|
|
|
if (pcdepth != 0) {
|
|
|
|
for (i = 0; i < pcdepth; i++) {
|
2008-03-17 09:25:39 -07:00
|
|
|
ss.offsets[i] = -2 - (ptrdiff_t)i;
|
2008-03-12 16:03:29 -07:00
|
|
|
ss.opcodes[i] = *jp->pcstack[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call recursive subroutine to do the hard work. */
|
|
|
|
oldscript = jp->script;
|
|
|
|
jp->script = script;
|
2008-04-29 10:26:31 -07:00
|
|
|
oldcode = jp->script->code;
|
|
|
|
oldmain = jp->script->main;
|
|
|
|
code = js_UntrapScriptCode(cx, jp->script);
|
|
|
|
if (code != oldcode) {
|
|
|
|
jp->script->code = code;
|
2008-05-03 21:20:23 -07:00
|
|
|
jp->script->main = code + (oldmain - oldcode);
|
2008-04-29 10:26:31 -07:00
|
|
|
pc = code + (pc - oldcode);
|
|
|
|
}
|
|
|
|
|
2007-05-29 18:49:42 -07:00
|
|
|
ok = Decompile(&ss, pc, len, JSOP_NOP) != NULL;
|
2008-04-29 10:26:31 -07:00
|
|
|
if (code != oldcode) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(jp->script->code);
|
2008-04-29 10:26:31 -07:00
|
|
|
jp->script->code = oldcode;
|
|
|
|
jp->script->main = oldmain;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
jp->script = oldscript;
|
|
|
|
|
|
|
|
/* If the given code didn't empty the stack, do it now. */
|
2008-04-30 06:30:38 -07:00
|
|
|
if (ok && ss.top) {
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
|
|
|
last = OFF2STR(&ss.sprinter, PopOff(&ss, JSOP_POP));
|
|
|
|
} while (ss.top > pcdepth);
|
|
|
|
js_printf(jp, "%s", last);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* Free all temporary stuff allocated under this call. */
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&cx->tempPool, mark);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_DecompileScript(JSPrinter *jp, JSScript *script)
|
|
|
|
{
|
2008-03-12 16:03:29 -07:00
|
|
|
return DecompileCode(jp, script, script->code, (uintN)script->length, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-11-05 14:34:39 -08:00
|
|
|
JSString *
|
|
|
|
js_DecompileToString(JSContext *cx, const char *name, JSFunction *fun,
|
2009-11-18 13:33:53 -08:00
|
|
|
uintN indent, JSBool pretty, JSBool grouped, JSBool strict,
|
2009-11-26 17:22:24 -08:00
|
|
|
JSDecompilerPtr decompiler)
|
2009-11-05 14:34:39 -08:00
|
|
|
{
|
|
|
|
JSPrinter *jp;
|
|
|
|
JSString *str;
|
|
|
|
|
2009-11-18 13:33:53 -08:00
|
|
|
jp = js_NewPrinter(cx, name, fun, indent, pretty, grouped, strict);
|
2009-11-05 14:34:39 -08:00
|
|
|
if (!jp)
|
|
|
|
return NULL;
|
|
|
|
if (decompiler(jp))
|
|
|
|
str = js_GetPrinterOutput(jp);
|
|
|
|
else
|
|
|
|
str = NULL;
|
|
|
|
js_DestroyPrinter(jp);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
static const char native_code_str[] = "\t[native code]\n";
|
|
|
|
|
2009-10-27 17:46:09 -07:00
|
|
|
JSBool
|
2008-03-29 03:34:29 -07:00
|
|
|
js_DecompileFunctionBody(JSPrinter *jp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSScript *script;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(jp->fun);
|
2007-11-19 09:15:45 -08:00
|
|
|
JS_ASSERT(!jp->script);
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!FUN_INTERPRETED(jp->fun)) {
|
|
|
|
js_printf(jp, native_code_str);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
script = jp->fun->u.i.script;
|
2008-03-12 16:03:29 -07:00
|
|
|
return DecompileCode(jp, script, script->code, (uintN)script->length, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-27 17:46:09 -07:00
|
|
|
JSBool
|
2008-03-29 03:34:29 -07:00
|
|
|
js_DecompileFunction(JSPrinter *jp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-11-19 09:15:45 -08:00
|
|
|
uintN i;
|
|
|
|
JSAtom *param;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsbytecode *pc, *endpc;
|
|
|
|
ptrdiff_t len;
|
|
|
|
JSBool ok;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
fun = jp->fun;
|
|
|
|
JS_ASSERT(fun);
|
2007-11-19 09:15:45 -08:00
|
|
|
JS_ASSERT(!jp->script);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* If pretty, conform to ECMA-262 Edition 3, 15.3.4.2, by decompiling a
|
|
|
|
* FunctionDeclaration. Otherwise, check the JSFUN_LAMBDA flag and force
|
|
|
|
* an expression by parenthesizing.
|
|
|
|
*/
|
2008-03-29 03:34:29 -07:00
|
|
|
if (jp->pretty) {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_printf(jp, "\t");
|
2008-03-29 03:34:29 -07:00
|
|
|
} else {
|
|
|
|
if (!jp->grouped && (fun->flags & JSFUN_LAMBDA))
|
|
|
|
js_puts(jp, "(");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (JSFUN_GETTER_TEST(fun->flags))
|
|
|
|
js_printf(jp, "%s ", js_getter_str);
|
|
|
|
else if (JSFUN_SETTER_TEST(fun->flags))
|
|
|
|
js_printf(jp, "%s ", js_setter_str);
|
|
|
|
|
|
|
|
js_printf(jp, "%s ", js_function_str);
|
|
|
|
if (fun->atom && !QuoteString(&jp->sprinter, ATOM_TO_STRING(fun->atom), 0))
|
|
|
|
return JS_FALSE;
|
|
|
|
js_puts(jp, "(");
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!FUN_INTERPRETED(fun)) {
|
|
|
|
js_printf(jp, ") {\n");
|
|
|
|
jp->indent += 4;
|
|
|
|
js_printf(jp, native_code_str);
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}");
|
|
|
|
} else {
|
2009-10-30 12:56:10 -07:00
|
|
|
JSScript *script = fun->u.i.script;
|
2009-09-14 17:26:16 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
2008-03-29 03:34:29 -07:00
|
|
|
SprintStack ss;
|
|
|
|
void *mark;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Print the parameters. */
|
2009-10-30 12:56:10 -07:00
|
|
|
pc = script->main;
|
|
|
|
endpc = pc + script->length;
|
2008-03-29 03:34:29 -07:00
|
|
|
ok = JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-14 17:26:16 -07:00
|
|
|
/* Skip trace hint if it appears here. */
|
2009-10-30 12:56:10 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
if (js_GetOpcode(jp->sprinter.context, script, script->code) != JSOP_GENERATOR)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
JSOp op = js_GetOpcode(jp->sprinter.context, script, pc);
|
|
|
|
if (op == JSOP_TRACE || op == JSOP_NOP) {
|
|
|
|
JS_STATIC_ASSERT(JSOP_TRACE_LENGTH == JSOP_NOP_LENGTH);
|
|
|
|
pc += JSOP_TRACE_LENGTH;
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(op == JSOP_STOP); /* empty script singleton */
|
|
|
|
}
|
2009-09-14 17:26:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-30 12:56:10 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
2008-03-29 03:34:29 -07:00
|
|
|
ss.printer = NULL;
|
2009-10-30 12:56:10 -07:00
|
|
|
jp->script = script;
|
2010-05-11 08:52:17 -07:00
|
|
|
mark = JS_ARENA_MARK(&jp->sprinter.context->tempPool);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
for (i = 0; i < fun->nargs; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
js_puts(jp, ", ");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-06 07:13:22 -07:00
|
|
|
param = GetArgOrVarAtom(jp, i);
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, JS_FALSE)
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!param) {
|
|
|
|
ptrdiff_t todo;
|
2009-11-30 04:33:04 -08:00
|
|
|
const char *lval;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
LOCAL_ASSERT(*pc == JSOP_GETARG);
|
|
|
|
pc += JSOP_GETARG_LENGTH;
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_DUP);
|
|
|
|
if (!ss.printer) {
|
2009-10-30 12:56:10 -07:00
|
|
|
ok = InitSprintStack(jp->sprinter.context, &ss, jp, StackDepth(script));
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!ok)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pc = DecompileDestructuring(&ss, pc, endpc);
|
|
|
|
if (!pc) {
|
|
|
|
ok = JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
|
|
|
LOCAL_ASSERT(*pc == JSOP_POP);
|
|
|
|
pc += JSOP_POP_LENGTH;
|
2009-11-30 04:33:04 -08:00
|
|
|
lval = PopStr(&ss, JSOP_NOP);
|
|
|
|
todo = SprintCString(&jp->sprinter, lval);
|
2008-03-29 03:34:29 -07:00
|
|
|
if (todo < 0) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
#endif
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!QuoteString(&jp->sprinter, ATOM_TO_STRING(param), 0)) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-14 17:26:16 -07:00
|
|
|
#if JS_HAS_DESTRUCTURING
|
2008-03-29 03:34:29 -07:00
|
|
|
jp->script = NULL;
|
2010-05-11 08:52:17 -07:00
|
|
|
JS_ARENA_RELEASE(&jp->sprinter.context->tempPool, mark);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
|
|
|
if (fun->flags & JSFUN_EXPR_CLOSURE) {
|
|
|
|
js_printf(jp, ") ");
|
2009-11-18 13:33:53 -08:00
|
|
|
if (fun->u.i.script->strictModeCode && !jp->strict) {
|
|
|
|
/*
|
|
|
|
* We have no syntax for strict function expressions;
|
|
|
|
* at least give a hint.
|
|
|
|
*/
|
|
|
|
js_printf(jp, "\t/* use strict */ \n");
|
|
|
|
jp->strict = true;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
} else {
|
|
|
|
js_printf(jp, ") {\n");
|
|
|
|
jp->indent += 4;
|
2009-11-18 13:33:53 -08:00
|
|
|
if (fun->u.i.script->strictModeCode && !jp->strict) {
|
|
|
|
js_printf(jp, "\t'use strict';\n");
|
|
|
|
jp->strict = true;
|
|
|
|
}
|
2008-03-29 03:34:29 -07:00
|
|
|
}
|
2007-05-29 18:49:42 -07:00
|
|
|
|
2009-10-30 12:56:10 -07:00
|
|
|
len = script->code + script->length - pc;
|
|
|
|
ok = DecompileCode(jp, script, pc, (uintN)len, 0);
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!ok)
|
|
|
|
return JS_FALSE;
|
2007-11-19 09:15:45 -08:00
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
if (!(fun->flags & JSFUN_EXPR_CLOSURE)) {
|
|
|
|
jp->indent -= 4;
|
|
|
|
js_printf(jp, "\t}");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-29 18:49:42 -07:00
|
|
|
|
2007-11-19 09:15:45 -08:00
|
|
|
if (!jp->pretty && !jp->grouped && (fun->flags & JSFUN_LAMBDA))
|
|
|
|
js_puts(jp, ")");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v,
|
|
|
|
JSString *fallback)
|
|
|
|
{
|
2008-03-04 15:30:58 -08:00
|
|
|
JSStackFrame *fp;
|
2008-03-12 16:03:29 -07:00
|
|
|
jsbytecode *pc;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script;
|
|
|
|
|
2008-02-08 13:59:05 -08:00
|
|
|
JS_ASSERT(spindex < 0 ||
|
|
|
|
spindex == JSDVG_IGNORE_STACK ||
|
|
|
|
spindex == JSDVG_SEARCH_STACK);
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
LeaveTrace(cx);
|
|
|
|
|
|
|
|
/* Get scripted caller */
|
|
|
|
FrameRegsIter i(cx);
|
|
|
|
while (!i.done() && !i.fp()->script)
|
|
|
|
++i;
|
|
|
|
|
2010-05-18 10:11:49 -07:00
|
|
|
if (i.done() || !i.pc() || i.fp()->script->nslots == 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto do_fallback;
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
fp = i.fp();
|
2008-03-04 15:30:58 -08:00
|
|
|
script = fp->script;
|
2010-03-03 18:10:13 -08:00
|
|
|
pc = fp->imacpc ? fp->imacpc : i.pc();
|
|
|
|
JS_ASSERT(pc >= script->main && pc < script->code + script->length);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-04 15:30:58 -08:00
|
|
|
if (spindex != JSDVG_IGNORE_STACK) {
|
|
|
|
jsbytecode **pcstack;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2008-03-04 15:30:58 -08:00
|
|
|
* Prepare computing pcstack containing pointers to opcodes that
|
|
|
|
* populated interpreter's stack with its current content.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-03-04 15:30:58 -08:00
|
|
|
pcstack = (jsbytecode **)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc(StackDepth(script) * sizeof *pcstack);
|
2008-03-04 15:30:58 -08:00
|
|
|
if (!pcstack)
|
|
|
|
return NULL;
|
2010-03-03 18:10:13 -08:00
|
|
|
intN pcdepth = ReconstructPCStack(cx, script, pc, pcstack);
|
2008-03-04 15:30:58 -08:00
|
|
|
if (pcdepth < 0)
|
|
|
|
goto release_pcstack;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-04 15:30:58 -08:00
|
|
|
if (spindex != JSDVG_SEARCH_STACK) {
|
|
|
|
JS_ASSERT(spindex < 0);
|
|
|
|
pcdepth += spindex;
|
|
|
|
if (pcdepth < 0)
|
|
|
|
goto release_pcstack;
|
|
|
|
pc = pcstack[pcdepth];
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We search from fp->sp to base to find the most recently
|
|
|
|
* calculated value matching v under assumption that it is
|
|
|
|
* it that caused exception, see bug 328664.
|
|
|
|
*/
|
2010-03-03 18:10:13 -08:00
|
|
|
jsval *stackBase = StackBase(fp);
|
|
|
|
jsval *sp = i.sp();
|
2008-03-04 15:30:58 -08:00
|
|
|
do {
|
2008-07-20 13:13:17 -07:00
|
|
|
if (sp == stackBase) {
|
2008-03-04 15:30:58 -08:00
|
|
|
pcdepth = -1;
|
|
|
|
goto release_pcstack;
|
|
|
|
}
|
|
|
|
} while (*--sp != v);
|
|
|
|
|
2009-04-24 09:07:49 -07:00
|
|
|
/*
|
|
|
|
* The value may have come from beyond stackBase + pcdepth,
|
|
|
|
* meaning that it came from a temporary slot that the
|
|
|
|
* interpreter uses for GC roots or when JSOP_APPLY extended
|
|
|
|
* the stack to fit the argument array elements. Only update pc
|
|
|
|
* if beneath stackBase + pcdepth; otherwise blame existing
|
|
|
|
* (current) PC.
|
|
|
|
*/
|
|
|
|
if (sp < stackBase + pcdepth)
|
2008-07-20 13:13:17 -07:00
|
|
|
pc = pcstack[sp - stackBase];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-04 15:30:58 -08:00
|
|
|
release_pcstack:
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(pcstack);
|
2008-03-04 15:30:58 -08:00
|
|
|
if (pcdepth < 0)
|
|
|
|
goto do_fallback;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-11-17 16:56:01 -08:00
|
|
|
{
|
2010-03-03 18:10:13 -08:00
|
|
|
jsbytecode* savepc = i.pc();
|
2008-11-17 16:56:01 -08:00
|
|
|
jsbytecode* imacpc = fp->imacpc;
|
|
|
|
if (imacpc) {
|
2010-03-03 18:10:13 -08:00
|
|
|
if (fp == cx->fp)
|
|
|
|
cx->regs->pc = imacpc;
|
|
|
|
else
|
|
|
|
fp->savedPC = imacpc;
|
2008-11-17 16:56:01 -08:00
|
|
|
fp->imacpc = NULL;
|
|
|
|
}
|
2009-04-24 09:07:49 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: bug 489843. Stack reconstruction may have returned a pc
|
|
|
|
* value *inside* an imacro; this would confuse the decompiler.
|
|
|
|
*/
|
2010-03-03 18:10:13 -08:00
|
|
|
char *name;
|
2009-04-24 09:07:49 -07:00
|
|
|
if (imacpc && size_t(pc - script->code) >= script->length)
|
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
else
|
|
|
|
name = DecompileExpression(cx, script, fp->fun, pc);
|
|
|
|
|
2008-11-17 16:56:01 -08:00
|
|
|
if (imacpc) {
|
2010-03-03 18:10:13 -08:00
|
|
|
if (fp == cx->fp)
|
|
|
|
cx->regs->pc = imacpc;
|
|
|
|
else
|
|
|
|
fp->savedPC = savepc;
|
2008-11-17 16:56:01 -08:00
|
|
|
fp->imacpc = imacpc;
|
|
|
|
}
|
2010-03-03 18:10:13 -08:00
|
|
|
|
|
|
|
if (name != FAILED_EXPRESSION_DECOMPILER)
|
|
|
|
return name;
|
2008-11-17 16:56:01 -08:00
|
|
|
}
|
2008-03-12 16:03:29 -07:00
|
|
|
|
|
|
|
do_fallback:
|
|
|
|
if (!fallback) {
|
|
|
|
fallback = js_ValueToSource(cx, v);
|
|
|
|
if (!fallback)
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-06-10 18:29:44 -07:00
|
|
|
return js_DeflateString(cx, fallback->chars(), fallback->length());
|
2008-03-12 16:03:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2008-03-29 03:34:29 -07:00
|
|
|
DecompileExpression(JSContext *cx, JSScript *script, JSFunction *fun,
|
2008-03-12 16:03:29 -07:00
|
|
|
jsbytecode *pc)
|
|
|
|
{
|
2008-05-03 21:20:23 -07:00
|
|
|
jsbytecode *code, *oldcode, *oldmain;
|
2008-03-12 16:03:29 -07:00
|
|
|
JSOp op;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
jsbytecode *begin, *end;
|
|
|
|
jssrcnote *sn;
|
|
|
|
ptrdiff_t len;
|
|
|
|
jsbytecode **pcstack;
|
|
|
|
intN pcdepth;
|
|
|
|
JSPrinter *jp;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
JS_ASSERT(script->main <= pc && pc < script->code + script->length);
|
2008-05-03 21:20:23 -07:00
|
|
|
|
|
|
|
pcstack = NULL;
|
|
|
|
oldcode = script->code;
|
|
|
|
oldmain = script->main;
|
|
|
|
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("out");
|
2008-05-03 21:20:23 -07:00
|
|
|
code = js_UntrapScriptCode(cx, script);
|
|
|
|
if (code != oldcode) {
|
|
|
|
script->code = code;
|
|
|
|
script->main = code + (oldmain - oldcode);
|
|
|
|
pc = code + (pc - oldcode);
|
|
|
|
}
|
|
|
|
|
2008-05-01 14:36:19 -07:00
|
|
|
op = (JSOp) *pc;
|
2008-05-01 15:16:38 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* None of these stack-writing ops generates novel values. */
|
|
|
|
JS_ASSERT(op != JSOP_CASE && op != JSOP_CASEX &&
|
2008-05-23 23:44:08 -07:00
|
|
|
op != JSOP_DUP && op != JSOP_DUP2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-12-29 23:22:23 -08:00
|
|
|
/* JSOP_PUSH is used to generate undefined for group assignment holes. */
|
|
|
|
if (op == JSOP_PUSH) {
|
|
|
|
name = JS_strdup(cx, js_undefined_str);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* |this| could convert to a very long object initialiser, so cite it by
|
|
|
|
* its keyword name instead.
|
|
|
|
*/
|
2008-05-03 21:20:23 -07:00
|
|
|
if (op == JSOP_THIS) {
|
|
|
|
name = JS_strdup(cx, js_this_str);
|
|
|
|
goto out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JSOP_BINDNAME is special: it generates a value, the base object of a
|
|
|
|
* reference. But if it is the generating op for a diagnostic produced by
|
|
|
|
* js_DecompileValueGenerator, the name being bound is irrelevant. Just
|
|
|
|
* fall back to the base object.
|
|
|
|
*/
|
2008-05-03 21:20:23 -07:00
|
|
|
if (op == JSOP_BINDNAME) {
|
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* NAME ops are self-contained, others require left or right context. */
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
begin = pc;
|
|
|
|
end = pc + cs->length;
|
2007-05-22 23:04:18 -07:00
|
|
|
switch (JOF_MODE(cs->format)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
case JOF_PROP:
|
|
|
|
case JOF_ELEM:
|
|
|
|
case JOF_XMLNAME:
|
|
|
|
case 0:
|
|
|
|
sn = js_GetSrcNote(script, pc);
|
2008-05-03 21:20:23 -07:00
|
|
|
if (!sn) {
|
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (SN_TYPE(sn)) {
|
|
|
|
case SRC_PCBASE:
|
|
|
|
begin -= js_GetSrcNoteOffset(sn, 0);
|
|
|
|
break;
|
|
|
|
case SRC_PCDELTA:
|
|
|
|
end = begin + js_GetSrcNoteOffset(sn, 0);
|
|
|
|
begin += cs->length;
|
|
|
|
break;
|
|
|
|
default:
|
2008-05-03 21:20:23 -07:00
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
2009-02-18 09:14:51 -08:00
|
|
|
len = end - begin;
|
2008-05-03 21:20:23 -07:00
|
|
|
if (len <= 0) {
|
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
pcstack = (jsbytecode **)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->malloc(StackDepth(script) * sizeof *pcstack);
|
2008-05-03 21:20:23 -07:00
|
|
|
if (!pcstack) {
|
|
|
|
name = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-03-12 16:03:29 -07:00
|
|
|
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("out");
|
2008-03-12 16:03:29 -07:00
|
|
|
pcdepth = ReconstructPCStack(cx, script, begin, pcstack);
|
|
|
|
if (pcdepth < 0) {
|
|
|
|
name = FAILED_EXPRESSION_DECOMPILER;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-03-04 15:30:58 -08:00
|
|
|
|
2008-05-01 16:06:43 -07:00
|
|
|
name = NULL;
|
2009-11-18 13:33:53 -08:00
|
|
|
jp = js_NewPrinter(cx, "js_DecompileValueGenerator", fun, 0,
|
2009-11-18 13:33:53 -08:00
|
|
|
false, false, false);
|
2008-03-04 15:30:58 -08:00
|
|
|
if (jp) {
|
2008-03-29 03:34:29 -07:00
|
|
|
jp->dvgfence = end;
|
|
|
|
jp->pcstack = pcstack;
|
|
|
|
if (DecompileCode(jp, script, begin, (uintN) len, (uintN) pcdepth)) {
|
|
|
|
name = (jp->sprinter.base) ? jp->sprinter.base : (char *) "";
|
|
|
|
name = JS_strdup(cx, name);
|
2008-03-04 15:30:58 -08:00
|
|
|
}
|
|
|
|
js_DestroyPrinter(jp);
|
|
|
|
}
|
|
|
|
|
2008-03-12 16:03:29 -07:00
|
|
|
out:
|
2008-05-03 21:20:23 -07:00
|
|
|
if (code != oldcode) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(script->code);
|
2008-05-03 21:20:23 -07:00
|
|
|
script->code = oldcode;
|
|
|
|
script->main = oldmain;
|
|
|
|
}
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(pcstack);
|
2008-03-12 16:03:29 -07:00
|
|
|
return name;
|
2008-03-04 15:30:58 -08:00
|
|
|
}
|
|
|
|
|
2008-07-28 23:46:27 -07:00
|
|
|
uintN
|
|
|
|
js_ReconstructStackDepth(JSContext *cx, JSScript *script, jsbytecode *pc)
|
|
|
|
{
|
|
|
|
return ReconstructPCStack(cx, script, pc, NULL);
|
|
|
|
}
|
|
|
|
|
2009-04-24 09:07:49 -07:00
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, -1);
|
|
|
|
|
|
|
|
static intN
|
|
|
|
SimulateOp(JSContext *cx, JSScript *script, JSOp op, const JSCodeSpec *cs,
|
|
|
|
jsbytecode *pc, jsbytecode **pcstack, uintN &pcdepth)
|
|
|
|
{
|
|
|
|
uintN nuses = js_GetStackUses(cs, op, pc);
|
|
|
|
uintN ndefs = js_GetStackDefs(cx, cs, op, script, pc);
|
|
|
|
LOCAL_ASSERT(pcdepth >= nuses);
|
|
|
|
pcdepth -= nuses;
|
|
|
|
LOCAL_ASSERT(pcdepth + ndefs <= StackDepth(script));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill the slots that the opcode defines withs its pc unless it just
|
|
|
|
* reshuffles the stack. In the latter case we want to preserve the
|
|
|
|
* opcode that generated the original value.
|
|
|
|
*/
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
if (pcstack) {
|
|
|
|
for (uintN i = 0; i != ndefs; ++i)
|
|
|
|
pcstack[pcdepth + i] = pc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_CASE:
|
|
|
|
case JSOP_CASEX:
|
|
|
|
/* Keep the switch value. */
|
|
|
|
JS_ASSERT(ndefs == 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DUP:
|
|
|
|
JS_ASSERT(ndefs == 2);
|
|
|
|
if (pcstack)
|
|
|
|
pcstack[pcdepth + 1] = pcstack[pcdepth];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_DUP2:
|
|
|
|
JS_ASSERT(ndefs == 4);
|
|
|
|
if (pcstack) {
|
|
|
|
pcstack[pcdepth + 2] = pcstack[pcdepth];
|
|
|
|
pcstack[pcdepth + 3] = pcstack[pcdepth + 1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSOP_SWAP:
|
|
|
|
JS_ASSERT(ndefs == 2);
|
|
|
|
if (pcstack) {
|
|
|
|
jsbytecode *tmp = pcstack[pcdepth + 1];
|
|
|
|
pcstack[pcdepth + 1] = pcstack[pcdepth];
|
|
|
|
pcstack[pcdepth] = tmp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pcdepth += ndefs;
|
|
|
|
return pcdepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JS_TRACER
|
2009-08-12 02:49:52 -07:00
|
|
|
|
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_CUSTOM(expr, goto failure);
|
|
|
|
|
2009-04-24 09:07:49 -07:00
|
|
|
static intN
|
|
|
|
SimulateImacroCFG(JSContext *cx, JSScript *script,
|
|
|
|
uintN pcdepth, jsbytecode *pc, jsbytecode *target,
|
|
|
|
jsbytecode **pcstack)
|
|
|
|
{
|
|
|
|
size_t nbytes = StackDepth(script) * sizeof *pcstack;
|
2009-07-27 21:10:12 -07:00
|
|
|
jsbytecode** tmp_pcstack = (jsbytecode **) cx->malloc(nbytes);
|
2009-04-24 09:07:49 -07:00
|
|
|
if (!tmp_pcstack)
|
|
|
|
return -1;
|
|
|
|
memcpy(tmp_pcstack, pcstack, nbytes);
|
|
|
|
|
|
|
|
ptrdiff_t oplen;
|
|
|
|
for (; pc < target; pc += oplen) {
|
|
|
|
JSOp op = js_GetOpcode(cx, script, pc);
|
|
|
|
const JSCodeSpec *cs = &js_CodeSpec[op];
|
|
|
|
oplen = cs->length;
|
|
|
|
if (oplen < 0)
|
|
|
|
oplen = js_GetVariableBytecodeLength(pc);
|
|
|
|
|
|
|
|
if (SimulateOp(cx, script, op, cs, pc, tmp_pcstack, pcdepth) < 0)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
uint32 type = cs->format & JOF_TYPEMASK;
|
|
|
|
if (type == JOF_JUMP || type == JOF_JUMPX) {
|
|
|
|
ptrdiff_t jmpoff = (type == JOF_JUMP) ? GET_JUMP_OFFSET(pc)
|
|
|
|
: GET_JUMPX_OFFSET(pc);
|
|
|
|
LOCAL_ASSERT(jmpoff >= 0);
|
2009-07-03 17:03:24 -07:00
|
|
|
intN tmp_pcdepth = SimulateImacroCFG(cx, script, pcdepth, pc + jmpoff,
|
|
|
|
target, tmp_pcstack);
|
2009-04-24 09:07:49 -07:00
|
|
|
if (tmp_pcdepth >= 0) {
|
2009-07-03 17:03:24 -07:00
|
|
|
pcdepth = uintN(tmp_pcdepth);
|
2009-04-24 09:07:49 -07:00
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == JSOP_GOTO || op == JSOP_GOTOX)
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pc > target)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
LOCAL_ASSERT(pc == target);
|
|
|
|
|
|
|
|
success:
|
|
|
|
memcpy(pcstack, tmp_pcstack, nbytes);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(tmp_pcstack);
|
2009-04-24 09:07:49 -07:00
|
|
|
return pcdepth;
|
|
|
|
|
|
|
|
failure:
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(tmp_pcstack);
|
2009-04-24 09:07:49 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-12 02:49:52 -07:00
|
|
|
#undef LOCAL_ASSERT
|
|
|
|
#define LOCAL_ASSERT(expr) LOCAL_ASSERT_RV(expr, -1);
|
|
|
|
|
2008-03-04 15:30:58 -08:00
|
|
|
static intN
|
2008-08-03 01:34:07 -07:00
|
|
|
ReconstructPCStack(JSContext *cx, JSScript *script, jsbytecode *target,
|
2009-04-24 09:07:49 -07:00
|
|
|
jsbytecode **pcstack);
|
|
|
|
|
|
|
|
static intN
|
|
|
|
ReconstructImacroPCStack(JSContext *cx, JSScript *script,
|
|
|
|
jsbytecode *imacstart, jsbytecode *target,
|
|
|
|
jsbytecode **pcstack)
|
2008-03-04 15:30:58 -08:00
|
|
|
{
|
2009-04-24 09:07:49 -07:00
|
|
|
/*
|
|
|
|
* Begin with a recursive call back to ReconstructPCStack to pick up
|
|
|
|
* the state-of-the-world at the *start* of the imacro.
|
|
|
|
*/
|
|
|
|
JSStackFrame *fp = js_GetScriptedCaller(cx, NULL);
|
|
|
|
JS_ASSERT(fp->imacpc);
|
2009-07-03 17:03:24 -07:00
|
|
|
intN pcdepth = ReconstructPCStack(cx, script, fp->imacpc, pcstack);
|
2009-04-24 09:07:49 -07:00
|
|
|
if (pcdepth < 0)
|
2009-07-10 17:18:25 -07:00
|
|
|
return pcdepth;
|
2009-04-24 09:07:49 -07:00
|
|
|
return SimulateImacroCFG(cx, script, pcdepth, imacstart, target, pcstack);
|
|
|
|
}
|
2008-03-04 15:30:58 -08:00
|
|
|
|
2009-04-24 09:07:49 -07:00
|
|
|
extern jsbytecode* js_GetImacroStart(jsbytecode* pc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static intN
|
|
|
|
ReconstructPCStack(JSContext *cx, JSScript *script, jsbytecode *target,
|
|
|
|
jsbytecode **pcstack)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2008-03-04 15:30:58 -08:00
|
|
|
* Walk forward from script->main and compute the stack depth and stack of
|
|
|
|
* operand-generating opcode PCs in pcstack.
|
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* FIXME: Code to compute oplen copied from js_Disassemble1 and reduced.
|
|
|
|
* FIXME: Optimize to use last empty-stack sequence point.
|
|
|
|
*/
|
2009-04-24 09:07:49 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
jsbytecode *imacstart = js_GetImacroStart(target);
|
|
|
|
|
|
|
|
if (imacstart)
|
|
|
|
return ReconstructImacroPCStack(cx, script, imacstart, target, pcstack);
|
|
|
|
#endif
|
|
|
|
|
2008-08-03 01:34:07 -07:00
|
|
|
LOCAL_ASSERT(script->main <= target && target < script->code + script->length);
|
2009-03-30 07:19:50 -07:00
|
|
|
jsbytecode *pc = script->main;
|
2009-04-24 09:07:49 -07:00
|
|
|
uintN pcdepth = 0;
|
2009-03-30 07:19:50 -07:00
|
|
|
ptrdiff_t oplen;
|
|
|
|
for (; pc < target; pc += oplen) {
|
|
|
|
JSOp op = js_GetOpcode(cx, script, pc);
|
|
|
|
const JSCodeSpec *cs = &js_CodeSpec[op];
|
2007-03-22 10:30:00 -07:00
|
|
|
oplen = cs->length;
|
2008-07-20 13:13:17 -07:00
|
|
|
if (oplen < 0)
|
|
|
|
oplen = js_GetVariableBytecodeLength(pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2008-08-03 01:34:07 -07:00
|
|
|
* A (C ? T : E) expression requires skipping either T (if target is in
|
|
|
|
* E) or both T and E (if target is after the whole expression) before
|
2007-03-22 10:30:00 -07:00
|
|
|
* adjusting pcdepth based on the JSOP_IFEQ or JSOP_IFEQX at pc that
|
|
|
|
* tests condition C. We know that the stack depth can't change from
|
|
|
|
* what it was with C on top of stack.
|
|
|
|
*/
|
2009-03-30 07:19:50 -07:00
|
|
|
jssrcnote *sn = js_GetSrcNote(script, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sn && SN_TYPE(sn) == SRC_COND) {
|
2009-03-30 07:19:50 -07:00
|
|
|
ptrdiff_t jmpoff = js_GetSrcNoteOffset(sn, 0);
|
2008-08-03 01:34:07 -07:00
|
|
|
if (pc + jmpoff < target) {
|
2007-03-22 10:30:00 -07:00
|
|
|
pc += jmpoff;
|
2009-02-18 16:31:09 -08:00
|
|
|
op = js_GetOpcode(cx, script, pc);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(op == JSOP_GOTO || op == JSOP_GOTOX);
|
|
|
|
cs = &js_CodeSpec[op];
|
|
|
|
oplen = cs->length;
|
2008-06-02 17:19:41 -07:00
|
|
|
JS_ASSERT(oplen > 0);
|
2009-03-30 07:19:50 -07:00
|
|
|
ptrdiff_t jmplen = GetJumpOffset(pc, pc);
|
2008-08-03 01:34:07 -07:00
|
|
|
if (pc + jmplen < target) {
|
2007-03-22 10:30:00 -07:00
|
|
|
oplen = (uintN) jmplen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-08-03 01:34:07 -07:00
|
|
|
* Ok, target lies in E. Manually pop C off the model stack,
|
2007-03-22 10:30:00 -07:00
|
|
|
* since we have moved beyond the IFEQ now.
|
|
|
|
*/
|
2009-03-30 07:19:50 -07:00
|
|
|
LOCAL_ASSERT(pcdepth != 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
--pcdepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 15:17:52 -08:00
|
|
|
/* Ignore early-exit code, which is annotated SRC_HIDDEN. */
|
|
|
|
if (sn && SN_TYPE(sn) == SRC_HIDDEN)
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
2009-04-24 09:07:49 -07:00
|
|
|
if (SimulateOp(cx, script, op, cs, pc, pcstack, pcdepth) < 0)
|
|
|
|
return -1;
|
2008-03-04 15:30:58 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-08-03 01:34:07 -07:00
|
|
|
LOCAL_ASSERT(pc == target);
|
2008-03-04 15:30:58 -08:00
|
|
|
return pcdepth;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-04 15:30:58 -08:00
|
|
|
#undef LOCAL_ASSERT
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-04 15:30:58 -08:00
|
|
|
|
|
|
|
#undef LOCAL_ASSERT_RV
|