2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
|
|
|
*
|
|
|
|
* ***** 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 script operations.
|
|
|
|
*/
|
|
|
|
#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 "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.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"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsopcode.h"
|
2007-10-02 16:10:09 -07:00
|
|
|
#include "jsparse.h"
|
2008-09-08 02:47:12 -07:00
|
|
|
#include "jsscope.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsscript.h"
|
2009-02-09 16:58:39 -08:00
|
|
|
#include "jstracer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_XDR
|
|
|
|
#include "jsxdrapi.h"
|
|
|
|
#endif
|
|
|
|
|
2009-09-10 09:50:43 -07:00
|
|
|
#include "jsscriptinlines.h"
|
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2009-12-13 11:06:04 -08:00
|
|
|
const uint32 JSSLOT_EXEC_DEPTH = JSSLOT_PRIVATE + 1;
|
|
|
|
const uint32 JSSCRIPT_RESERVED_SLOTS = 1;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_SCRIPT_OBJECT
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
static const char js_script_exec_str[] = "Script.prototype.exec";
|
|
|
|
static const char js_script_compile_str[] = "Script.prototype.compile";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static jsint
|
2009-12-13 11:06:04 -08:00
|
|
|
GetScriptExecDepth(JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-13 11:06:04 -08:00
|
|
|
jsval v = obj->fslots[JSSLOT_EXEC_DEPTH];
|
2009-11-05 15:41:56 -08:00
|
|
|
return JSVAL_IS_VOID(v) ? 0 : JSVAL_TO_INT(v);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-13 11:06:04 -08:00
|
|
|
AdjustScriptExecDepth(JSObject *obj, jsint delta)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-13 11:06:04 -08:00
|
|
|
jsint execDepth = GetScriptExecDepth(obj);
|
|
|
|
obj->fslots[JSSLOT_EXEC_DEPTH] = INT_TO_JSVAL(execDepth + delta);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_toSource(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
uint32 indent;
|
|
|
|
JSScript *script;
|
|
|
|
size_t i, j, k, n;
|
|
|
|
char buf[16];
|
|
|
|
jschar *s, *t;
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
indent = 0;
|
2008-03-06 13:40:43 -08:00
|
|
|
if (argc != 0) {
|
|
|
|
indent = js_ValueToECMAUint32(cx, &vp[2]);
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2008-03-06 13:40:43 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Let n count the source string length, j the "front porch" length. */
|
|
|
|
j = JS_snprintf(buf, sizeof buf, "(new %s(", js_ScriptClass.name);
|
|
|
|
n = j + 2;
|
|
|
|
if (!script) {
|
|
|
|
/* Let k count the constructor argument string length. */
|
|
|
|
k = 0;
|
|
|
|
s = NULL; /* quell GCC overwarning */
|
|
|
|
} else {
|
|
|
|
str = JS_DecompileScript(cx, script, "Script.prototype.toSource",
|
|
|
|
(uintN)indent);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
str = js_QuoteString(cx, str, '\'');
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2009-11-05 15:41:56 -08:00
|
|
|
const jschar *cs;
|
|
|
|
str->getCharsAndLength(cs, k);
|
|
|
|
s = const_cast<jschar *>(cs);
|
2007-03-22 10:30:00 -07:00
|
|
|
n += k;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the source string and copy into it. */
|
2009-07-27 21:10:12 -07:00
|
|
|
t = (jschar *) cx->malloc((n + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!t)
|
|
|
|
return JS_FALSE;
|
|
|
|
for (i = 0; i < j; i++)
|
|
|
|
t[i] = buf[i];
|
|
|
|
for (j = 0; j < k; i++, j++)
|
|
|
|
t[i] = s[j];
|
|
|
|
t[i++] = ')';
|
|
|
|
t[i++] = ')';
|
|
|
|
t[i] = 0;
|
|
|
|
|
|
|
|
/* Create and return a JS string for t. */
|
|
|
|
str = JS_NewUCString(cx, t, n);
|
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(t);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif /* JS_HAS_TOSOURCE */
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_toString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
uint32 indent;
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script;
|
|
|
|
JSString *str;
|
|
|
|
|
|
|
|
indent = 0;
|
2008-03-06 13:40:43 -08:00
|
|
|
if (argc != 0) {
|
|
|
|
indent = js_ValueToECMAUint32(cx, &vp[2]);
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2008-03-06 13:40:43 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-08-04 14:06:55 -07:00
|
|
|
script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script) {
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(cx->runtime->emptyString);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = JS_DecompileScript(cx, script, "Script.prototype.toString",
|
|
|
|
(uintN)indent);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_compile_sub(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
|
|
|
jsval *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
JSObject *scopeobj;
|
|
|
|
JSScript *script, *oldscript;
|
2008-09-08 02:47:12 -07:00
|
|
|
JSStackFrame *caller;
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *file;
|
|
|
|
uintN line;
|
|
|
|
JSPrincipals *principals;
|
|
|
|
jsint execDepth;
|
|
|
|
|
|
|
|
/* Make sure obj is a Script object. */
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* If no args, leave private undefined and return early. */
|
|
|
|
if (argc == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Otherwise, the first arg is the script source to compile. */
|
|
|
|
str = js_ValueToString(cx, argv[0]);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[0] = STRING_TO_JSVAL(str);
|
|
|
|
|
|
|
|
scopeobj = NULL;
|
|
|
|
if (argc >= 2) {
|
|
|
|
if (!js_ValueToObject(cx, argv[1], &scopeobj))
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[1] = OBJECT_TO_JSVAL(scopeobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile using the caller's scope chain, which js_Invoke passes to fp. */
|
2008-12-09 08:38:32 -08:00
|
|
|
caller = js_GetScriptedCaller(cx, NULL);
|
2008-09-08 02:47:12 -07:00
|
|
|
JS_ASSERT(!caller || cx->fp->scopeChain == caller->scopeChain);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (caller) {
|
|
|
|
if (!scopeobj) {
|
|
|
|
scopeobj = js_GetScopeChain(cx, caller);
|
|
|
|
if (!scopeobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-09-08 02:47:12 -07:00
|
|
|
principals = JS_EvalFramePrincipals(cx, cx->fp, caller);
|
2008-03-06 14:52:58 -08:00
|
|
|
file = js_ComputeFilename(cx, caller, principals, &line);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
file = NULL;
|
|
|
|
line = 0;
|
|
|
|
principals = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we compile this script with the right (inner) principals. */
|
2007-08-01 21:33:52 -07:00
|
|
|
scopeobj = js_CheckScopeChainValidity(cx, scopeobj, js_script_compile_str);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!scopeobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compile the new script using the caller's scope chain, a la eval().
|
2007-11-29 06:49:42 -08:00
|
|
|
* Unlike jsobj.c:obj_eval, however, we do not pass TCF_COMPILE_N_GO in
|
2008-09-08 02:47:12 -07:00
|
|
|
* tcflags and use NULL for the callerFrame argument, because compilation
|
|
|
|
* is here separated from execution, and the run-time scope chain may not
|
|
|
|
* match the compile-time. TCF_COMPILE_N_GO is tested in jsemit.c and
|
|
|
|
* jsparse.c to optimize based on identity of run- and compile-time scope.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2009-10-18 17:41:24 -07:00
|
|
|
script = JSCompiler::compileScript(cx, scopeobj, NULL, principals,
|
|
|
|
TCF_NEED_MUTABLE_SCRIPT,
|
2009-06-10 18:29:44 -07:00
|
|
|
str->chars(), str->length(),
|
2009-05-26 15:36:32 -07:00
|
|
|
NULL, file, line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-12-13 11:06:04 -08:00
|
|
|
execDepth = GetScriptExecDepth(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* execDepth must be 0 to allow compilation here, otherwise the JSScript
|
|
|
|
* struct can be released while running.
|
|
|
|
*/
|
|
|
|
if (execDepth > 0) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_COMPILE_EXECED_SCRIPT);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap script for obj's old script, if any. */
|
2009-08-04 14:06:55 -07:00
|
|
|
oldscript = (JSScript*) obj->getPrivate();
|
|
|
|
obj->setPrivate(script);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
|
|
|
|
if (oldscript)
|
|
|
|
js_DestroyScript(cx, oldscript);
|
|
|
|
|
2008-08-20 22:18:42 -07:00
|
|
|
script->u.object = obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
js_CallNewScriptHook(cx, script, NULL);
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* Return the object. */
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_compile(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
|
|
|
return script_compile_sub(cx, JS_THIS_OBJECT(cx, vp), argc, vp + 2, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
script_exec_sub(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
|
|
|
jsval *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-02-18 23:57:24 -08:00
|
|
|
JSObject *scopeobj;
|
2008-12-09 08:38:32 -08:00
|
|
|
JSStackFrame *caller;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPrincipals *principals;
|
|
|
|
JSScript *script;
|
|
|
|
JSBool ok;
|
|
|
|
|
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
scopeobj = NULL;
|
2007-08-01 21:33:52 -07:00
|
|
|
if (argc != 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!js_ValueToObject(cx, argv[0], &scopeobj))
|
|
|
|
return JS_FALSE;
|
|
|
|
argv[0] = OBJECT_TO_JSVAL(scopeobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emulate eval() by using caller's this, var object, sharp array, etc.,
|
|
|
|
* all propagated by js_Execute via a non-null fourth (down) argument to
|
|
|
|
* js_Execute. If there is no scripted caller, js_Execute uses its second
|
2009-09-11 19:07:00 -07:00
|
|
|
* (chain) argument to set the exec frame's varobj, thisv, and scopeChain.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Unlike eval, which the compiler detects, Script.prototype.exec may be
|
|
|
|
* called from a lightweight function, or even from native code (in which
|
|
|
|
* case fp->varobj and fp->scopeChain are null). If exec is called from
|
|
|
|
* a lightweight function, we will need to get a Call object representing
|
|
|
|
* its frame, to act as the var object and scope chain head.
|
|
|
|
*/
|
2008-12-09 08:38:32 -08:00
|
|
|
caller = js_GetScriptedCaller(cx, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (caller && !caller->varobj) {
|
|
|
|
/* Called from a lightweight function. */
|
|
|
|
JS_ASSERT(caller->fun && !JSFUN_HEAVYWEIGHT_TEST(caller->fun->flags));
|
|
|
|
|
2009-02-18 23:57:24 -08:00
|
|
|
/* Scope chain links from Call object to caller's scope chain. */
|
|
|
|
if (!js_GetCallObject(cx, caller))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!scopeobj) {
|
|
|
|
/* No scope object passed in: try to use the caller's scope chain. */
|
|
|
|
if (caller) {
|
|
|
|
/*
|
|
|
|
* Load caller->scopeChain after the conditional js_GetCallObject
|
|
|
|
* call above, which resets scopeChain as well as varobj.
|
|
|
|
*/
|
|
|
|
scopeobj = js_GetScopeChain(cx, caller);
|
|
|
|
if (!scopeobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Called from native code, so we don't know what scope object to
|
2009-02-18 23:57:24 -08:00
|
|
|
* use. We could use the caller's scope chain (see above), but Script.prototype.exec
|
2007-03-22 10:30:00 -07:00
|
|
|
* might be a shared/sealed "superglobal" method. A more general
|
|
|
|
* approach would use cx->globalObject, which will be the same as
|
|
|
|
* exec.__parent__ in the non-superglobal case. In the superglobal
|
|
|
|
* case it's the right object: the global, not the superglobal.
|
|
|
|
*/
|
|
|
|
scopeobj = cx->globalObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
scopeobj = js_CheckScopeChainValidity(cx, scopeobj, js_script_exec_str);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!scopeobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* Keep track of nesting depth for the script. */
|
2009-12-13 11:06:04 -08:00
|
|
|
AdjustScriptExecDepth(obj, 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Must get to out label after this */
|
2009-08-04 14:06:55 -07:00
|
|
|
script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Belt-and-braces: check that this script object has access to scopeobj. */
|
|
|
|
principals = script->principals;
|
|
|
|
ok = js_CheckPrincipalsAccess(cx, scopeobj, principals,
|
|
|
|
CLASS_ATOM(cx, Script));
|
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ok = js_Execute(cx, scopeobj, script, caller, JSFRAME_EVAL, rval);
|
2007-08-01 21:33:52 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
out:
|
2009-12-13 11:06:04 -08:00
|
|
|
AdjustScriptExecDepth(obj, -1);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
static JSBool
|
|
|
|
script_exec(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
|
|
|
return script_exec_sub(cx, JS_THIS_OBJECT(cx, vp), argc, vp + 2, vp);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif /* JS_HAS_SCRIPT_OBJECT */
|
|
|
|
|
2009-10-18 17:41:24 -07:00
|
|
|
static const jsbytecode emptyScriptCode[] = {JSOP_STOP, SRC_NULL};
|
|
|
|
|
|
|
|
/* static */ const JSScript JSScript::emptyScriptConst = {
|
|
|
|
const_cast<jsbytecode*>(emptyScriptCode),
|
2009-11-18 13:33:53 -08:00
|
|
|
1, JSVERSION_DEFAULT, 0, 0, 0, 0, 0, true, false, false, false,
|
2009-10-18 17:41:24 -07:00
|
|
|
const_cast<jsbytecode*>(emptyScriptCode),
|
|
|
|
{0, NULL}, NULL, 0, 0, 0, NULL
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
JSBool
|
2009-10-18 17:41:24 -07:00
|
|
|
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, bool needMutableScript,
|
|
|
|
JSBool *hasMagic)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSContext *cx;
|
2007-07-08 02:03:34 -07:00
|
|
|
JSScript *script, *oldscript;
|
2008-03-22 18:11:51 -07:00
|
|
|
JSBool ok;
|
|
|
|
jsbytecode *code;
|
2008-07-20 13:13:17 -07:00
|
|
|
uint32 length, lineno, nslots, magic;
|
2008-08-21 03:47:33 -07:00
|
|
|
uint32 natoms, nsrcnotes, ntrynotes, nobjects, nupvars, nregexps, i;
|
2007-03-22 10:30:00 -07:00
|
|
|
uint32 prologLength, version;
|
2008-01-30 18:19:49 -08:00
|
|
|
JSTempValueRooter tvr;
|
2007-07-08 02:03:34 -07:00
|
|
|
JSPrincipals *principals;
|
|
|
|
uint32 encodeable;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool filenameWasSaved;
|
|
|
|
jssrcnote *notes, *sn;
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
cx = xdr->cx;
|
|
|
|
script = *scriptp;
|
2008-08-21 03:47:33 -07:00
|
|
|
nsrcnotes = ntrynotes = natoms = nobjects = nupvars = nregexps = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
filenameWasSaved = JS_FALSE;
|
|
|
|
notes = NULL;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_ENCODE)
|
|
|
|
magic = JSXDR_MAGIC_SCRIPT_CURRENT;
|
|
|
|
if (!JS_XDRUint32(xdr, &magic))
|
|
|
|
return JS_FALSE;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (magic != JSXDR_MAGIC_SCRIPT_CURRENT) {
|
|
|
|
/* We do not provide binary compatibility with older scripts. */
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!hasMagic) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_SCRIPT_MAGIC);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*hasMagic = JS_FALSE;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
if (hasMagic)
|
|
|
|
*hasMagic = JS_TRUE;
|
|
|
|
|
2009-10-18 17:41:24 -07:00
|
|
|
/*
|
|
|
|
* Since the shortest possible script has JSOP_STOP as its only bytecode,
|
2009-11-05 15:41:56 -08:00
|
|
|
* encode only the length 0 for the emptyScript singleton, and return the
|
|
|
|
* emptyScript instead of a new script when decoding a script of length 0.
|
2009-10-18 17:41:24 -07:00
|
|
|
*/
|
|
|
|
if (xdr->mode == JSXDR_ENCODE)
|
2009-11-05 15:41:56 -08:00
|
|
|
length = (script == JSScript::emptyScript()) ? 0 : script->length;
|
2009-10-18 17:41:24 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &length))
|
|
|
|
return JS_FALSE;
|
2009-11-05 15:41:56 -08:00
|
|
|
if (length == 0) {
|
2009-10-18 17:41:24 -07:00
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
|
|
|
JS_ASSERT(*scriptp == JSScript::emptyScript());
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decoding: check whether we need a mutable empty script. */
|
|
|
|
if (cx->debugHooks->newScriptHook)
|
|
|
|
needMutableScript = true;
|
2009-11-05 15:41:56 -08:00
|
|
|
if (needMutableScript) {
|
|
|
|
/*
|
|
|
|
* We need a mutable empty script but the encoder serialized only
|
|
|
|
* the shorthand (0 length word) for us. Make a new mutable empty
|
|
|
|
* script here and return it immediately.
|
|
|
|
*/
|
2009-11-08 07:41:44 -08:00
|
|
|
script = js_NewScript(cx, 1, 1, 0, 0, 0, 0, 0);
|
2009-11-05 15:41:56 -08:00
|
|
|
if (!script)
|
|
|
|
return JS_FALSE;
|
2009-11-08 07:41:44 -08:00
|
|
|
|
|
|
|
script->version = JSVERSION_DEFAULT;
|
|
|
|
script->noScriptRval = true;
|
|
|
|
script->code[0] = JSOP_STOP;
|
|
|
|
script->code[1] = SRC_NULL;
|
2009-11-05 15:41:56 -08:00
|
|
|
*scriptp = script;
|
2009-10-18 17:41:24 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2009-11-08 07:41:44 -08:00
|
|
|
|
2009-11-05 15:41:56 -08:00
|
|
|
*scriptp = JSScript::emptyScript();
|
|
|
|
return JS_TRUE;
|
2009-10-18 17:41:24 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2009-02-18 09:14:51 -08:00
|
|
|
prologLength = script->main - script->code;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT((int16)script->version != JSVERSION_UNKNOWN);
|
2008-07-20 13:13:17 -07:00
|
|
|
version = (uint32)script->version | (script->nfixed << 16);
|
2007-03-22 10:30:00 -07:00
|
|
|
lineno = (uint32)script->lineno;
|
2008-07-20 13:13:17 -07:00
|
|
|
nslots = (uint32)script->nslots;
|
2009-04-05 21:17:22 -07:00
|
|
|
nslots = (uint32)((script->staticLevel << 16) | script->nslots);
|
2007-07-08 02:03:34 -07:00
|
|
|
natoms = (uint32)script->atomMap.length;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Count the srcnotes, keeping notes pointing at the first one. */
|
2009-09-09 16:52:55 -07:00
|
|
|
notes = script->notes();
|
2007-03-22 10:30:00 -07:00
|
|
|
for (sn = notes; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn))
|
|
|
|
continue;
|
2009-02-18 09:14:51 -08:00
|
|
|
nsrcnotes = sn - notes;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrcnotes++; /* room for the terminator */
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (script->objectsOffset != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
nobjects = script->objects()->length;
|
2008-08-21 03:47:33 -07:00
|
|
|
if (script->upvarsOffset != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
nupvars = script->upvars()->length;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (script->regexpsOffset != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
nregexps = script->regexps()->length;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (script->trynotesOffset != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
ntrynotes = script->trynotes()->length;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &prologLength))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!JS_XDRUint32(xdr, &version))
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
/*
|
2008-08-21 03:47:33 -07:00
|
|
|
* To fuse allocations, we need srcnote, atom, objects, upvar, regexp,
|
|
|
|
* and trynote counts early.
|
2007-07-08 02:03:34 -07:00
|
|
|
*/
|
|
|
|
if (!JS_XDRUint32(xdr, &natoms))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!JS_XDRUint32(xdr, &nsrcnotes))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!JS_XDRUint32(xdr, &ntrynotes))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!JS_XDRUint32(xdr, &nobjects))
|
|
|
|
return JS_FALSE;
|
2008-08-21 03:47:33 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &nupvars))
|
|
|
|
return JS_FALSE;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &nregexps))
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2008-08-21 03:47:33 -07:00
|
|
|
script = js_NewScript(cx, length, nsrcnotes, natoms, nobjects, nupvars,
|
|
|
|
nregexps, ntrynotes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script)
|
|
|
|
return JS_FALSE;
|
2007-07-08 02:03:34 -07:00
|
|
|
|
|
|
|
script->main += prologLength;
|
2009-02-26 00:49:39 -08:00
|
|
|
script->version = JSVersion(version & 0xffff);
|
|
|
|
script->nfixed = uint16(version >> 16);
|
2007-07-08 02:03:34 -07:00
|
|
|
|
|
|
|
/* If we know nsrcnotes, we allocated space for notes in script. */
|
2009-09-09 16:52:55 -07:00
|
|
|
notes = script->notes();
|
2007-03-22 10:30:00 -07:00
|
|
|
*scriptp = script;
|
2008-01-30 18:19:49 -08:00
|
|
|
JS_PUSH_TEMP_ROOT_SCRIPT(cx, script, &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-07-08 02:03:34 -07:00
|
|
|
* Control hereafter must goto error on failure, in order for the
|
|
|
|
* DECODE case to destroy script.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-03-24 01:06:39 -07:00
|
|
|
oldscript = xdr->script;
|
2008-03-22 18:11:51 -07:00
|
|
|
code = script->code;
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2008-03-24 01:06:39 -07:00
|
|
|
code = js_UntrapScriptCode(cx, script);
|
|
|
|
if (!code)
|
|
|
|
goto error;
|
2008-03-22 18:11:51 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
xdr->script = script;
|
2008-03-22 18:11:51 -07:00
|
|
|
ok = JS_XDRBytes(xdr, (char *) code, length * sizeof(jsbytecode));
|
|
|
|
|
|
|
|
if (code != script->code)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(code);
|
2008-03-22 18:11:51 -07:00
|
|
|
|
|
|
|
if (!ok)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!JS_XDRBytes(xdr, (char *)notes, nsrcnotes * sizeof(jssrcnote)) ||
|
|
|
|
!JS_XDRCStringOrNull(xdr, (char **)&script->filename) ||
|
|
|
|
!JS_XDRUint32(xdr, &lineno) ||
|
2008-07-20 13:13:17 -07:00
|
|
|
!JS_XDRUint32(xdr, &nslots)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-09-05 16:24:53 -07:00
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
2007-07-08 02:03:34 -07:00
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
|
|
|
principals = script->principals;
|
2008-09-05 16:24:53 -07:00
|
|
|
encodeable = callbacks && callbacks->principalsTranscoder;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &encodeable))
|
|
|
|
goto error;
|
|
|
|
if (encodeable &&
|
2008-09-05 16:24:53 -07:00
|
|
|
!callbacks->principalsTranscoder(xdr, &principals)) {
|
2007-07-08 02:03:34 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!JS_XDRUint32(xdr, &encodeable))
|
|
|
|
goto error;
|
|
|
|
if (encodeable) {
|
2008-09-05 16:24:53 -07:00
|
|
|
if (!(callbacks && callbacks->principalsTranscoder)) {
|
2007-07-08 02:03:34 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_CANT_DECODE_PRINCIPALS);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
}
|
2008-09-05 16:24:53 -07:00
|
|
|
if (!callbacks->principalsTranscoder(xdr, &principals))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
2007-07-08 02:03:34 -07:00
|
|
|
script->principals = principals;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
const char *filename = script->filename;
|
|
|
|
if (filename) {
|
|
|
|
filename = js_SaveScriptFilename(cx, filename);
|
|
|
|
if (!filename)
|
|
|
|
goto error;
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((void *) script->filename);
|
2007-03-22 10:30:00 -07:00
|
|
|
script->filename = filename;
|
|
|
|
filenameWasSaved = JS_TRUE;
|
|
|
|
}
|
|
|
|
script->lineno = (uintN)lineno;
|
2008-08-21 03:47:33 -07:00
|
|
|
script->nslots = (uint16)nslots;
|
2009-04-05 21:17:22 -07:00
|
|
|
script->staticLevel = (uint16)(nslots >> 16);
|
2007-07-08 02:03:34 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
for (i = 0; i != natoms; ++i) {
|
|
|
|
if (!js_XDRAtom(xdr, &script->atomMap.vector[i]))
|
|
|
|
goto error;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
/*
|
|
|
|
* Here looping from 0-to-length to xdr objects is essential. It ensures
|
2008-08-21 03:47:33 -07:00
|
|
|
* that block objects from the script->objects array will be written and
|
2009-05-06 16:03:10 -07:00
|
|
|
* restored in the outer-to-inner order. js_XDRBlockObject relies on this
|
|
|
|
* to restore the parent chain.
|
2007-07-08 02:03:34 -07:00
|
|
|
*/
|
|
|
|
for (i = 0; i != nobjects; ++i) {
|
2009-09-09 16:52:55 -07:00
|
|
|
JSObject **objp = &script->objects()->vector[i];
|
2009-05-06 16:03:10 -07:00
|
|
|
uint32 isBlock;
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
|
|
|
JSClass *clasp = STOBJ_GET_CLASS(*objp);
|
|
|
|
JS_ASSERT(clasp == &js_FunctionClass ||
|
|
|
|
clasp == &js_BlockClass);
|
|
|
|
isBlock = (clasp == &js_BlockClass) ? 1 : 0;
|
2009-10-18 17:41:24 -07:00
|
|
|
}
|
2009-05-06 16:03:10 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &isBlock))
|
2007-07-08 02:03:34 -07:00
|
|
|
goto error;
|
2009-05-06 16:03:10 -07:00
|
|
|
if (isBlock == 0) {
|
|
|
|
if (!js_XDRFunctionObject(xdr, objp))
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(isBlock == 1);
|
|
|
|
if (!js_XDRBlockObject(xdr, objp))
|
|
|
|
goto error;
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
for (i = 0; i != nupvars; ++i) {
|
2009-09-09 16:52:55 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &script->upvars()->vector[i]))
|
2008-08-21 03:47:33 -07:00
|
|
|
goto error;
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
for (i = 0; i != nregexps; ++i) {
|
2009-09-09 16:52:55 -07:00
|
|
|
if (!js_XDRRegExpObject(xdr, &script->regexps()->vector[i]))
|
2007-07-08 02:03:34 -07:00
|
|
|
goto error;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (ntrynotes != 0) {
|
2007-05-24 00:51:46 -07:00
|
|
|
/*
|
2007-07-08 02:03:34 -07:00
|
|
|
* We combine tn->kind and tn->stackDepth when serializing as XDR is not
|
2007-05-24 00:51:46 -07:00
|
|
|
* efficient when serializing small integer types.
|
|
|
|
*/
|
2007-07-08 02:03:34 -07:00
|
|
|
JSTryNote *tn, *tnfirst;
|
|
|
|
uint32 kindAndDepth;
|
2007-05-24 00:51:46 -07:00
|
|
|
JS_STATIC_ASSERT(sizeof(tn->kind) == sizeof(uint8));
|
|
|
|
JS_STATIC_ASSERT(sizeof(tn->stackDepth) == sizeof(uint16));
|
|
|
|
|
2009-09-09 16:52:55 -07:00
|
|
|
tnfirst = script->trynotes()->vector;
|
|
|
|
JS_ASSERT(script->trynotes()->length == ntrynotes);
|
2007-07-08 02:03:34 -07:00
|
|
|
tn = tnfirst + ntrynotes;
|
|
|
|
do {
|
|
|
|
--tn;
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
|
|
|
kindAndDepth = ((uint32)tn->kind << 16)
|
|
|
|
| (uint32)tn->stackDepth;
|
|
|
|
}
|
|
|
|
if (!JS_XDRUint32(xdr, &kindAndDepth) ||
|
|
|
|
!JS_XDRUint32(xdr, &tn->start) ||
|
|
|
|
!JS_XDRUint32(xdr, &tn->length)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
tn->kind = (uint8)(kindAndDepth >> 16);
|
|
|
|
tn->stackDepth = (uint16)kindAndDepth;
|
|
|
|
}
|
|
|
|
} while (tn != tnfirst);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
xdr->script = oldscript;
|
2008-01-30 18:19:49 -08:00
|
|
|
if (xdr->mode == JSXDR_DECODE)
|
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2008-01-30 18:19:49 -08:00
|
|
|
JS_POP_TEMP_ROOT(cx, &tvr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (script->filename && !filenameWasSaved) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free((void *) script->filename);
|
2007-03-22 10:30:00 -07:00
|
|
|
script->filename = NULL;
|
|
|
|
}
|
|
|
|
js_DestroyScript(cx, script);
|
|
|
|
*scriptp = NULL;
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
xdr->script = oldscript;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_SCRIPT_OBJECT && JS_HAS_XDR_FREEZE_THAW
|
|
|
|
/*
|
|
|
|
* These cannot be exposed to web content, and chrome does not need them, so
|
|
|
|
* we take them out of the Mozilla client altogether. Fortunately, there is
|
|
|
|
* no way to serialize a native function (see fun_xdrObject in jsfun.c).
|
|
|
|
*/
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_freeze(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSXDRState *xdr;
|
|
|
|
JSScript *script;
|
|
|
|
JSBool ok, hasMagic;
|
|
|
|
uint32 len;
|
|
|
|
void *buf;
|
|
|
|
JSString *str;
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-08-04 14:06:55 -07:00
|
|
|
script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
/* create new XDR */
|
|
|
|
xdr = JS_XDRNewMem(cx, JSXDR_ENCODE);
|
|
|
|
if (!xdr)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* write */
|
2009-10-18 17:41:24 -07:00
|
|
|
ok = js_XDRScript(xdr, &script, false, &hasMagic);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
if (!hasMagic) {
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = JSVAL_VOID;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = JS_XDRMemGetData(xdr, &len);
|
|
|
|
if (!buf) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ASSERT((jsword)buf % sizeof(jschar) == 0);
|
|
|
|
len /= sizeof(jschar);
|
|
|
|
#if IS_BIG_ENDIAN
|
|
|
|
{
|
|
|
|
jschar *chars;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
/* Swap bytes in Unichars to keep frozen strings machine-independent. */
|
|
|
|
chars = (jschar *)buf;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
chars[i] = JSXDR_SWAB16(chars[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
str = JS_NewUCStringCopyN(cx, (jschar *)buf, len);
|
|
|
|
if (!str) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
out:
|
|
|
|
JS_XDRDestroy(xdr);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_thaw(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSXDRState *xdr;
|
|
|
|
JSString *str;
|
|
|
|
void *buf;
|
2009-11-05 15:41:56 -08:00
|
|
|
size_t len;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script, *oldscript;
|
|
|
|
JSBool ok, hasMagic;
|
2007-05-23 14:50:43 -07:00
|
|
|
jsint execDepth;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return JS_TRUE;
|
2007-08-01 21:33:52 -07:00
|
|
|
str = js_ValueToString(cx, vp[2]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
vp[2] = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* create new XDR */
|
|
|
|
xdr = JS_XDRNewMem(cx, JSXDR_DECODE);
|
|
|
|
if (!xdr)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-11-05 15:41:56 -08:00
|
|
|
const jschar *cs;
|
|
|
|
str->getCharsAndLength(cs, len);
|
|
|
|
buf = const_cast<jschar *>(cs);
|
2007-03-22 10:30:00 -07:00
|
|
|
#if IS_BIG_ENDIAN
|
|
|
|
{
|
|
|
|
jschar *from, *to;
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
/* Swap bytes in Unichars to keep frozen strings machine-independent. */
|
|
|
|
from = (jschar *)buf;
|
2009-07-27 21:10:12 -07:00
|
|
|
to = (jschar *) cx->malloc(len * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!to) {
|
|
|
|
JS_XDRDestroy(xdr);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
to[i] = JSXDR_SWAB16(from[i]);
|
|
|
|
buf = (char *)to;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
len *= sizeof(jschar);
|
|
|
|
JS_XDRMemSetData(xdr, buf, len);
|
|
|
|
|
|
|
|
/* XXXbe should magic mismatch be error, or false return value? */
|
2009-10-18 17:41:24 -07:00
|
|
|
ok = js_XDRScript(xdr, &script, true, &hasMagic);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto out;
|
|
|
|
if (!hasMagic) {
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = JSVAL_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2009-12-13 11:06:04 -08:00
|
|
|
execDepth = GetScriptExecDepth(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* execDepth must be 0 to allow compilation here, otherwise the JSScript
|
|
|
|
* struct can be released while running.
|
|
|
|
*/
|
|
|
|
if (execDepth > 0) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_COMPILE_EXECED_SCRIPT);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap script for obj's old script, if any. */
|
2009-08-04 14:06:55 -07:00
|
|
|
oldscript = (JSScript *) obj->getPrivate();
|
|
|
|
obj->setPrivate(script);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
|
|
|
|
if (oldscript)
|
|
|
|
js_DestroyScript(cx, oldscript);
|
|
|
|
|
2008-08-20 22:18:42 -07:00
|
|
|
script->u.object = obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
js_CallNewScriptHook(cx, script, NULL);
|
|
|
|
|
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* We reset the buffer to be NULL so that it doesn't free the chars
|
2007-08-01 21:33:52 -07:00
|
|
|
* memory owned by str (vp[2]).
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
JS_XDRMemSetData(xdr, NULL, 0);
|
|
|
|
JS_XDRDestroy(xdr);
|
|
|
|
#if IS_BIG_ENDIAN
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = JSVAL_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char js_thaw_str[] = "thaw";
|
|
|
|
|
|
|
|
#endif /* JS_HAS_SCRIPT_OBJECT && JS_HAS_XDR_FREEZE_THAW */
|
|
|
|
#endif /* JS_HAS_XDR */
|
|
|
|
|
|
|
|
#if JS_HAS_SCRIPT_OBJECT
|
|
|
|
|
|
|
|
static JSFunctionSpec script_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-09-08 02:47:12 -07:00
|
|
|
JS_FN(js_toSource_str, script_toSource, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-09-08 02:47:12 -07:00
|
|
|
JS_FN(js_toString_str, script_toString, 0,0),
|
|
|
|
JS_FN("compile", script_compile, 2,0),
|
|
|
|
JS_FN("exec", script_exec, 1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_XDR_FREEZE_THAW
|
2008-09-08 02:47:12 -07:00
|
|
|
JS_FN("freeze", script_freeze, 0,0),
|
|
|
|
JS_FN(js_thaw_str, script_thaw, 1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif /* JS_HAS_XDR_FREEZE_THAW */
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* JS_HAS_SCRIPT_OBJECT */
|
|
|
|
|
|
|
|
static void
|
|
|
|
script_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSScript *script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (script)
|
|
|
|
js_DestroyScript(cx, script);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
script_call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
#if JS_HAS_SCRIPT_OBJECT
|
2007-08-01 21:33:52 -07:00
|
|
|
return script_exec_sub(cx, JSVAL_TO_OBJECT(argv[-2]), argc, argv, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
return JS_FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
script_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSScript *script = (JSScript *) obj->getPrivate();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (script)
|
2007-04-16 23:53:37 -07:00
|
|
|
js_TraceScript(trc, script);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !JS_HAS_SCRIPT_OBJECT
|
|
|
|
#define JSProto_Script JSProto_Object
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JS_FRIEND_DATA(JSClass) js_ScriptClass = {
|
|
|
|
js_Script_str,
|
2009-12-13 11:06:04 -08:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(JSSCRIPT_RESERVED_SLOTS) |
|
2007-04-16 23:53:37 -07:00
|
|
|
JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Script),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, script_finalize,
|
|
|
|
NULL, NULL, script_call, NULL,/*XXXbe xdr*/
|
2007-04-16 23:53:37 -07:00
|
|
|
NULL, NULL, JS_CLASS_TRACE(script_trace), NULL
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#if JS_HAS_SCRIPT_OBJECT
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Script(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
/* If not constructing, replace obj with a new Script object. */
|
2008-12-09 08:38:32 -08:00
|
|
|
if (!JS_IsConstructing(cx)) {
|
2009-07-14 17:06:09 -07:00
|
|
|
obj = js_NewObject(cx, &js_ScriptClass, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
2007-08-01 21:33:52 -07:00
|
|
|
* script_compile_sub does not use rval to root its temporaries so we
|
|
|
|
* can use it to root obj.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
*rval = OBJECT_TO_JSVAL(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetReservedSlot(cx, obj, 0, INT_TO_JSVAL(0)))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
return script_compile_sub(cx, obj, argc, argv, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_SCRIPT_OBJECT && JS_HAS_XDR_FREEZE_THAW
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
script_static_thaw(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
obj = js_NewObject(cx, &js_ScriptClass, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
vp[1] = OBJECT_TO_JSVAL(obj);
|
2008-09-08 02:47:12 -07:00
|
|
|
if (!script_thaw(cx, argc, vp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec script_static_methods[] = {
|
2008-09-08 02:47:12 -07:00
|
|
|
JS_FN(js_thaw_str, script_static_thaw, 1,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#else /* !JS_HAS_SCRIPT_OBJECT || !JS_HAS_XDR_FREEZE_THAW */
|
|
|
|
|
|
|
|
#define script_static_methods NULL
|
|
|
|
|
|
|
|
#endif /* !JS_HAS_SCRIPT_OBJECT || !JS_HAS_XDR_FREEZE_THAW */
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitScriptClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
return JS_InitClass(cx, obj, NULL, &js_ScriptClass, Script, 1,
|
|
|
|
NULL, script_methods, NULL, script_static_methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JS_HAS_SCRIPT_OBJECT */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shared script filename management.
|
|
|
|
*/
|
2008-09-06 15:21:43 -07:00
|
|
|
static int
|
2007-03-22 10:30:00 -07:00
|
|
|
js_compare_strings(const void *k1, const void *k2)
|
|
|
|
{
|
2007-07-05 13:37:47 -07:00
|
|
|
return strcmp((const char *) k1, (const char *) k2) == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NB: This struct overlays JSHashEntry -- see jshash.h, do not reorganize. */
|
|
|
|
typedef struct ScriptFilenameEntry {
|
|
|
|
JSHashEntry *next; /* hash chain linkage */
|
|
|
|
JSHashNumber keyHash; /* key hash function result */
|
|
|
|
const void *key; /* ptr to filename, below */
|
|
|
|
uint32 flags; /* user-defined filename prefix flags */
|
|
|
|
JSPackedBool mark; /* GC mark flag */
|
|
|
|
char filename[3]; /* two or more bytes, NUL-terminated */
|
|
|
|
} ScriptFilenameEntry;
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void *
|
2007-08-11 13:25:16 -07:00
|
|
|
js_alloc_table_space(void *priv, size_t size)
|
|
|
|
{
|
2009-07-27 21:10:12 -07:00
|
|
|
return js_malloc(size);
|
2007-08-11 13:25:16 -07:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2009-04-05 21:17:22 -07:00
|
|
|
js_free_table_space(void *priv, void *item, size_t size)
|
2007-08-11 13:25:16 -07:00
|
|
|
{
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(item);
|
2007-08-11 13:25:16 -07:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSHashEntry *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_alloc_sftbl_entry(void *priv, const void *key)
|
|
|
|
{
|
2007-07-05 13:37:47 -07:00
|
|
|
size_t nbytes = offsetof(ScriptFilenameEntry, filename) +
|
|
|
|
strlen((const char *) key) + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
return (JSHashEntry *) js_malloc(JS_MAX(nbytes, sizeof(JSHashEntry)));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
js_free_sftbl_entry(void *priv, JSHashEntry *he, uintN flag)
|
|
|
|
{
|
|
|
|
if (flag != HT_FREE_ENTRY)
|
|
|
|
return;
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(he);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSHashAllocOps sftbl_alloc_ops = {
|
|
|
|
js_alloc_table_space, js_free_table_space,
|
|
|
|
js_alloc_sftbl_entry, js_free_sftbl_entry
|
|
|
|
};
|
|
|
|
|
2009-09-26 06:44:11 -07:00
|
|
|
static void
|
|
|
|
FinishRuntimeScriptState(JSRuntime *rt)
|
|
|
|
{
|
|
|
|
if (rt->scriptFilenameTable) {
|
|
|
|
JS_HashTableDestroy(rt->scriptFilenameTable);
|
|
|
|
rt->scriptFilenameTable = NULL;
|
|
|
|
}
|
|
|
|
#ifdef JS_THREADSAFE
|
|
|
|
if (rt->scriptFilenameTableLock) {
|
|
|
|
JS_DESTROY_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
rt->scriptFilenameTableLock = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
|
|
|
js_InitRuntimeScriptState(JSRuntime *rt)
|
|
|
|
{
|
|
|
|
#ifdef JS_THREADSAFE
|
|
|
|
JS_ASSERT(!rt->scriptFilenameTableLock);
|
|
|
|
rt->scriptFilenameTableLock = JS_NEW_LOCK();
|
|
|
|
if (!rt->scriptFilenameTableLock)
|
|
|
|
return JS_FALSE;
|
|
|
|
#endif
|
|
|
|
JS_ASSERT(!rt->scriptFilenameTable);
|
|
|
|
rt->scriptFilenameTable =
|
|
|
|
JS_NewHashTable(16, JS_HashString, js_compare_strings, NULL,
|
|
|
|
&sftbl_alloc_ops, NULL);
|
|
|
|
if (!rt->scriptFilenameTable) {
|
2009-09-26 06:44:11 -07:00
|
|
|
FinishRuntimeScriptState(rt); /* free lock if threadsafe */
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
JS_INIT_CLIST(&rt->scriptFilenamePrefixes);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct ScriptFilenamePrefix {
|
|
|
|
JSCList links; /* circular list linkage for easy deletion */
|
|
|
|
const char *name; /* pointer to pinned ScriptFilenameEntry string */
|
|
|
|
size_t length; /* prefix string length, precomputed */
|
|
|
|
uint32 flags; /* user-defined flags to inherit from this prefix */
|
|
|
|
} ScriptFilenamePrefix;
|
|
|
|
|
|
|
|
void
|
|
|
|
js_FreeRuntimeScriptState(JSRuntime *rt)
|
|
|
|
{
|
|
|
|
if (!rt->scriptFilenameTable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (!JS_CLIST_IS_EMPTY(&rt->scriptFilenamePrefixes)) {
|
2009-09-26 06:44:11 -07:00
|
|
|
ScriptFilenamePrefix *sfp = (ScriptFilenamePrefix *)
|
|
|
|
rt->scriptFilenamePrefixes.next;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_REMOVE_LINK(&sfp->links);
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(sfp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-26 06:44:11 -07:00
|
|
|
FinishRuntimeScriptState(rt);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_brendan
|
|
|
|
#define DEBUG_SFTBL
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG_SFTBL
|
|
|
|
size_t sftbl_savings = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static ScriptFilenameEntry *
|
|
|
|
SaveScriptFilename(JSRuntime *rt, const char *filename, uint32 flags)
|
|
|
|
{
|
|
|
|
JSHashTable *table;
|
|
|
|
JSHashNumber hash;
|
|
|
|
JSHashEntry **hep;
|
|
|
|
ScriptFilenameEntry *sfe;
|
|
|
|
size_t length;
|
|
|
|
JSCList *head, *link;
|
|
|
|
ScriptFilenamePrefix *sfp;
|
|
|
|
|
|
|
|
table = rt->scriptFilenameTable;
|
|
|
|
hash = JS_HashString(filename);
|
|
|
|
hep = JS_HashTableRawLookup(table, hash, filename);
|
|
|
|
sfe = (ScriptFilenameEntry *) *hep;
|
|
|
|
#ifdef DEBUG_SFTBL
|
|
|
|
if (sfe)
|
|
|
|
sftbl_savings += strlen(sfe->filename);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!sfe) {
|
|
|
|
sfe = (ScriptFilenameEntry *)
|
|
|
|
JS_HashTableRawAdd(table, hep, hash, filename, NULL);
|
|
|
|
if (!sfe)
|
|
|
|
return NULL;
|
|
|
|
sfe->key = strcpy(sfe->filename, filename);
|
|
|
|
sfe->flags = 0;
|
|
|
|
sfe->mark = JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If saving a prefix, add it to the set in rt->scriptFilenamePrefixes. */
|
|
|
|
if (flags != 0) {
|
|
|
|
/* Search in case filename was saved already; we must be idempotent. */
|
|
|
|
sfp = NULL;
|
|
|
|
length = strlen(filename);
|
|
|
|
for (head = link = &rt->scriptFilenamePrefixes;
|
|
|
|
link->next != head;
|
|
|
|
link = link->next) {
|
|
|
|
/* Lag link behind sfp to insert in non-increasing length order. */
|
|
|
|
sfp = (ScriptFilenamePrefix *) link->next;
|
|
|
|
if (!strcmp(sfp->name, filename))
|
|
|
|
break;
|
|
|
|
if (sfp->length <= length) {
|
|
|
|
sfp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sfp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sfp) {
|
|
|
|
/* No such prefix: add one now. */
|
2009-07-27 21:10:12 -07:00
|
|
|
sfp = (ScriptFilenamePrefix *) js_malloc(sizeof(ScriptFilenamePrefix));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!sfp)
|
|
|
|
return NULL;
|
|
|
|
JS_INSERT_AFTER(&sfp->links, link);
|
|
|
|
sfp->name = sfe->filename;
|
|
|
|
sfp->length = length;
|
|
|
|
sfp->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accumulate flags in both sfe and sfp: sfe for later access from the
|
|
|
|
* JS_GetScriptedCallerFilenameFlags debug-API, and sfp so that longer
|
|
|
|
* filename entries can inherit by prefix.
|
|
|
|
*/
|
|
|
|
sfe->flags |= flags;
|
|
|
|
sfp->flags |= flags;
|
|
|
|
}
|
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
#ifdef JS_FUNCTION_METERING
|
|
|
|
size_t len = strlen(sfe->filename);
|
|
|
|
if (len >= sizeof rt->lastScriptFilename)
|
|
|
|
len = sizeof rt->lastScriptFilename - 1;
|
|
|
|
memcpy(rt->lastScriptFilename, sfe->filename, len);
|
|
|
|
rt->lastScriptFilename[len] = '\0';
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return sfe;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
js_SaveScriptFilename(JSContext *cx, const char *filename)
|
|
|
|
{
|
|
|
|
JSRuntime *rt;
|
|
|
|
ScriptFilenameEntry *sfe;
|
|
|
|
JSCList *head, *link;
|
|
|
|
ScriptFilenamePrefix *sfp;
|
|
|
|
|
|
|
|
rt = cx->runtime;
|
|
|
|
JS_ACQUIRE_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
sfe = SaveScriptFilename(rt, filename, 0);
|
|
|
|
if (!sfe) {
|
|
|
|
JS_RELEASE_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to inherit flags by prefix. We assume there won't be more than a
|
|
|
|
* few (dozen! ;-) prefixes, so linear search is tolerable.
|
|
|
|
* XXXbe every time I've assumed that in the JS engine, I've been wrong!
|
|
|
|
*/
|
|
|
|
for (head = &rt->scriptFilenamePrefixes, link = head->next;
|
|
|
|
link != head;
|
|
|
|
link = link->next) {
|
|
|
|
sfp = (ScriptFilenamePrefix *) link;
|
|
|
|
if (!strncmp(sfp->name, filename, sfp->length)) {
|
|
|
|
sfe->flags |= sfp->flags;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_RELEASE_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
return sfe->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags)
|
|
|
|
{
|
|
|
|
ScriptFilenameEntry *sfe;
|
|
|
|
|
|
|
|
/* This may be called very early, via the jsdbgapi.h entry point. */
|
|
|
|
if (!rt->scriptFilenameTable && !js_InitRuntimeScriptState(rt))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
JS_ACQUIRE_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
sfe = SaveScriptFilename(rt, filename, flags);
|
|
|
|
JS_RELEASE_LOCK(rt->scriptFilenameTableLock);
|
|
|
|
if (!sfe)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return sfe->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Back up from a saved filename by its offset within its hash table entry.
|
|
|
|
*/
|
|
|
|
#define FILENAME_TO_SFE(fn) \
|
|
|
|
((ScriptFilenameEntry *) ((fn) - offsetof(ScriptFilenameEntry, filename)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The sfe->key member, redundant given sfe->filename but required by the old
|
|
|
|
* jshash.c code, here gives us a useful sanity check. This assertion will
|
|
|
|
* very likely botch if someone tries to mark a string that wasn't allocated
|
|
|
|
* as an sfe->filename.
|
|
|
|
*/
|
|
|
|
#define ASSERT_VALID_SFE(sfe) JS_ASSERT((sfe)->key == (sfe)->filename)
|
|
|
|
|
|
|
|
uint32
|
|
|
|
js_GetScriptFilenameFlags(const char *filename)
|
|
|
|
{
|
|
|
|
ScriptFilenameEntry *sfe;
|
|
|
|
|
|
|
|
sfe = FILENAME_TO_SFE(filename);
|
|
|
|
ASSERT_VALID_SFE(sfe);
|
|
|
|
return sfe->flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_MarkScriptFilename(const char *filename)
|
|
|
|
{
|
|
|
|
ScriptFilenameEntry *sfe;
|
|
|
|
|
|
|
|
sfe = FILENAME_TO_SFE(filename);
|
|
|
|
ASSERT_VALID_SFE(sfe);
|
|
|
|
sfe->mark = JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static intN
|
2007-03-22 10:30:00 -07:00
|
|
|
js_script_filename_marker(JSHashEntry *he, intN i, void *arg)
|
|
|
|
{
|
|
|
|
ScriptFilenameEntry *sfe = (ScriptFilenameEntry *) he;
|
|
|
|
|
|
|
|
sfe->mark = JS_TRUE;
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms)
|
|
|
|
{
|
|
|
|
JSCList *head, *link;
|
|
|
|
ScriptFilenamePrefix *sfp;
|
|
|
|
|
|
|
|
if (!rt->scriptFilenameTable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (keepAtoms) {
|
|
|
|
JS_HashTableEnumerateEntries(rt->scriptFilenameTable,
|
|
|
|
js_script_filename_marker,
|
|
|
|
rt);
|
|
|
|
}
|
|
|
|
for (head = &rt->scriptFilenamePrefixes, link = head->next;
|
|
|
|
link != head;
|
|
|
|
link = link->next) {
|
|
|
|
sfp = (ScriptFilenamePrefix *) link;
|
|
|
|
js_MarkScriptFilename(sfp->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static intN
|
2007-03-22 10:30:00 -07:00
|
|
|
js_script_filename_sweeper(JSHashEntry *he, intN i, void *arg)
|
|
|
|
{
|
|
|
|
ScriptFilenameEntry *sfe = (ScriptFilenameEntry *) he;
|
|
|
|
|
|
|
|
if (!sfe->mark)
|
|
|
|
return HT_ENUMERATE_REMOVE;
|
|
|
|
sfe->mark = JS_FALSE;
|
|
|
|
return HT_ENUMERATE_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_SweepScriptFilenames(JSRuntime *rt)
|
|
|
|
{
|
|
|
|
if (!rt->scriptFilenameTable)
|
|
|
|
return;
|
|
|
|
|
2009-09-26 06:44:11 -07:00
|
|
|
/*
|
|
|
|
* JS_HashTableEnumerateEntries shrinks the table if many entries are
|
|
|
|
* removed preventing wasting memory on a too sparse table.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_HashTableEnumerateEntries(rt->scriptFilenameTable,
|
|
|
|
js_script_filename_sweeper,
|
|
|
|
rt);
|
|
|
|
#ifdef DEBUG_notme
|
|
|
|
#ifdef DEBUG_SFTBL
|
|
|
|
printf("script filename table savings so far: %u\n", sftbl_savings);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
/*
|
|
|
|
* JSScript data structures memory alignment:
|
|
|
|
*
|
|
|
|
* JSScript
|
|
|
|
* JSObjectArray script objects' descriptor if JSScript.objectsOffset != 0,
|
2009-09-09 16:52:55 -07:00
|
|
|
* use script->objects() to access it.
|
2007-07-08 02:03:34 -07:00
|
|
|
* JSObjectArray script regexps' descriptor if JSScript.regexpsOffset != 0,
|
2009-09-09 16:52:55 -07:00
|
|
|
* use script->regexps() to access it.
|
2007-07-08 02:03:34 -07:00
|
|
|
* JSTryNoteArray script try notes' descriptor if JSScript.tryNotesOffset
|
2009-09-09 16:52:55 -07:00
|
|
|
* != 0, use script->trynotes() to access it.
|
2007-07-08 02:03:34 -07:00
|
|
|
* JSAtom *a[] array of JSScript.atomMap.length atoms pointed by
|
|
|
|
* JSScript.atomMap.vector if any.
|
2009-09-09 16:52:55 -07:00
|
|
|
* JSObject *o[] array of script->objects()->length objects if any
|
|
|
|
* pointed by script->objects()->vector.
|
|
|
|
* JSObject *r[] array of script->regexps()->length regexps if any
|
|
|
|
* pointed by script->regexps()->vector.
|
|
|
|
* JSTryNote t[] array of script->trynotes()->length try notes if any
|
|
|
|
* pointed by script->trynotes()->vector.
|
2007-07-08 02:03:34 -07:00
|
|
|
* jsbytecode b[] script bytecode pointed by JSScript.code.
|
2009-09-09 16:52:55 -07:00
|
|
|
* jssrcnote s[] script source notes, use script->notes() to access it
|
2007-07-08 02:03:34 -07:00
|
|
|
*
|
|
|
|
* The alignment avoids gaps between entries as alignment requirement for each
|
|
|
|
* subsequent structure or array is the same or divides the alignment
|
|
|
|
* requirement for the previous one.
|
|
|
|
*
|
|
|
|
* The followings asserts checks that assuming that the alignment requirement
|
|
|
|
* for JSObjectArray and JSTryNoteArray are sizeof(void *) and for JSTryNote
|
|
|
|
* it is sizeof(uint32) as the structure consists of 3 uint32 fields.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSScript) % sizeof(void *) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSObjectArray) % sizeof(void *) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSTryNoteArray) == sizeof(JSObjectArray));
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSAtom *) == sizeof(JSObject *));
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSObject *) % sizeof(uint32) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSTryNote) == 3 * sizeof(uint32));
|
|
|
|
JS_STATIC_ASSERT(sizeof(uint32) % sizeof(jsbytecode) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(jsbytecode) % sizeof(jssrcnote) == 0);
|
|
|
|
|
|
|
|
/*
|
2008-08-21 03:47:33 -07:00
|
|
|
* Check that uint8 offset for object, upvar, regexp, and try note arrays is
|
|
|
|
* sufficient.
|
2007-07-08 02:03:34 -07:00
|
|
|
*/
|
2008-08-21 03:47:33 -07:00
|
|
|
JS_STATIC_ASSERT(sizeof(JSScript) + 2 * sizeof(JSObjectArray) +
|
|
|
|
sizeof(JSUpvarArray) < JS_BIT(8));
|
2007-07-08 02:03:34 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *
|
2008-08-21 03:47:33 -07:00
|
|
|
js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms,
|
|
|
|
uint32 nobjects, uint32 nupvars, uint32 nregexps,
|
|
|
|
uint32 ntrynotes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 02:03:34 -07:00
|
|
|
size_t size, vectorSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script;
|
2007-07-08 02:03:34 -07:00
|
|
|
uint8 *cursor;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
size = sizeof(JSScript) +
|
|
|
|
sizeof(JSAtom *) * natoms +
|
|
|
|
length * sizeof(jsbytecode) +
|
|
|
|
nsrcnotes * sizeof(jssrcnote);
|
|
|
|
if (nobjects != 0)
|
|
|
|
size += sizeof(JSObjectArray) + nobjects * sizeof(JSObject *);
|
2008-08-21 03:47:33 -07:00
|
|
|
if (nupvars != 0)
|
|
|
|
size += sizeof(JSUpvarArray) + nupvars * sizeof(uint32);
|
2007-07-08 02:03:34 -07:00
|
|
|
if (nregexps != 0)
|
|
|
|
size += sizeof(JSObjectArray) + nregexps * sizeof(JSObject *);
|
|
|
|
if (ntrynotes != 0)
|
|
|
|
size += sizeof(JSTryNoteArray) + ntrynotes * sizeof(JSTryNote);
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
script = (JSScript *) cx->malloc(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script)
|
|
|
|
return NULL;
|
|
|
|
memset(script, 0, sizeof(JSScript));
|
|
|
|
script->length = length;
|
|
|
|
script->version = cx->version;
|
2007-07-08 02:03:34 -07:00
|
|
|
|
|
|
|
cursor = (uint8 *)script + sizeof(JSScript);
|
|
|
|
if (nobjects != 0) {
|
|
|
|
script->objectsOffset = (uint8)(cursor - (uint8 *)script);
|
|
|
|
cursor += sizeof(JSObjectArray);
|
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
if (nupvars != 0) {
|
|
|
|
script->upvarsOffset = (uint8)(cursor - (uint8 *)script);
|
|
|
|
cursor += sizeof(JSUpvarArray);
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
if (nregexps != 0) {
|
|
|
|
script->regexpsOffset = (uint8)(cursor - (uint8 *)script);
|
|
|
|
cursor += sizeof(JSObjectArray);
|
|
|
|
}
|
|
|
|
if (ntrynotes != 0) {
|
|
|
|
script->trynotesOffset = (uint8)(cursor - (uint8 *)script);
|
|
|
|
cursor += sizeof(JSTryNoteArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (natoms != 0) {
|
|
|
|
script->atomMap.length = natoms;
|
|
|
|
script->atomMap.vector = (JSAtom **)cursor;
|
|
|
|
vectorSize = natoms * sizeof(script->atomMap.vector[0]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear object map's vector so the GC tracing can run when not yet
|
|
|
|
* all atoms are copied to the array.
|
|
|
|
*/
|
|
|
|
memset(cursor, 0, vectorSize);
|
|
|
|
cursor += vectorSize;
|
|
|
|
}
|
2008-06-03 23:52:28 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (nobjects != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
script->objects()->length = nobjects;
|
|
|
|
script->objects()->vector = (JSObject **)cursor;
|
|
|
|
vectorSize = nobjects * sizeof(script->objects()->vector[0]);
|
2007-07-08 02:03:34 -07:00
|
|
|
memset(cursor, 0, vectorSize);
|
|
|
|
cursor += vectorSize;
|
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (nregexps != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
script->regexps()->length = nregexps;
|
|
|
|
script->regexps()->vector = (JSObject **)cursor;
|
|
|
|
vectorSize = nregexps * sizeof(script->regexps()->vector[0]);
|
2007-07-08 02:03:34 -07:00
|
|
|
memset(cursor, 0, vectorSize);
|
|
|
|
cursor += vectorSize;
|
|
|
|
}
|
2008-08-21 03:47:33 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (ntrynotes != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
script->trynotes()->length = ntrynotes;
|
|
|
|
script->trynotes()->vector = (JSTryNote *)cursor;
|
|
|
|
vectorSize = ntrynotes * sizeof(script->trynotes()->vector[0]);
|
2007-07-08 02:03:34 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
memset(cursor, 0, vectorSize);
|
|
|
|
#endif
|
|
|
|
cursor += vectorSize;
|
|
|
|
}
|
2008-06-03 23:52:28 -07:00
|
|
|
|
2009-09-06 12:05:51 -07:00
|
|
|
/*
|
|
|
|
* NB: We allocate the vector of uint32 upvar cookies after all vectors of
|
|
|
|
* pointers, to avoid misalignment on 64-bit platforms. See bug 514645.
|
|
|
|
*/
|
|
|
|
if (nupvars != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
script->upvars()->length = nupvars;
|
|
|
|
script->upvars()->vector = (uint32 *)cursor;
|
|
|
|
vectorSize = nupvars * sizeof(script->upvars()->vector[0]);
|
2009-09-06 12:05:51 -07:00
|
|
|
memset(cursor, 0, vectorSize);
|
|
|
|
cursor += vectorSize;
|
|
|
|
}
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
script->code = script->main = (jsbytecode *)cursor;
|
|
|
|
JS_ASSERT(cursor +
|
|
|
|
length * sizeof(jsbytecode) +
|
|
|
|
nsrcnotes * sizeof(jssrcnote) ==
|
|
|
|
(uint8 *)script + size);
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
script->owner = cx->thread;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2007-11-29 06:49:42 -08:00
|
|
|
JSScript *
|
|
|
|
js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-07-20 13:13:17 -07:00
|
|
|
uint32 mainLength, prologLength, nsrcnotes, nfixed;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSScript *script;
|
|
|
|
const char *filename;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
/* The counts of indexed things must be checked during code generation. */
|
|
|
|
JS_ASSERT(cg->atomList.count <= INDEX_LIMIT);
|
|
|
|
JS_ASSERT(cg->objectList.length <= INDEX_LIMIT);
|
|
|
|
JS_ASSERT(cg->regexpList.length <= INDEX_LIMIT);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mainLength = CG_OFFSET(cg);
|
|
|
|
prologLength = CG_PROLOG_OFFSET(cg);
|
2009-10-18 17:41:24 -07:00
|
|
|
|
2009-11-05 15:41:56 -08:00
|
|
|
if (prologLength + mainLength <= 3) {
|
2009-10-18 17:41:24 -07:00
|
|
|
/*
|
|
|
|
* Check very short scripts to see whether they are "empty" and return
|
|
|
|
* the const empty-script singleton if so. We are deliberately flexible
|
|
|
|
* about whether JSOP_TRACE is in the prolog.
|
|
|
|
*/
|
|
|
|
jsbytecode *pc = prologLength ? CG_PROLOG_BASE(cg) : CG_BASE(cg);
|
|
|
|
|
|
|
|
if (JSOp(*pc) == JSOP_TRACE) {
|
|
|
|
++pc;
|
|
|
|
if (pc == CG_PROLOG_BASE(cg) + prologLength)
|
|
|
|
pc = CG_BASE(cg);
|
|
|
|
}
|
|
|
|
if ((cg->flags & TCF_NO_SCRIPT_RVAL) && JSOp(*pc) == JSOP_FALSE)
|
|
|
|
++pc;
|
|
|
|
|
2009-11-08 07:41:44 -08:00
|
|
|
if (JSOp(*pc) == JSOP_STOP &&
|
|
|
|
!cx->debugHooks->newScriptHook &&
|
|
|
|
!(cg->flags & TCF_NEED_MUTABLE_SCRIPT))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We can probably use the immutable empty script singleton, just
|
|
|
|
* one hard case (nupvars != 0) may stand in our way.
|
|
|
|
*/
|
2009-11-05 15:41:56 -08:00
|
|
|
JSScript *empty = JSScript::emptyScript();
|
2009-11-08 07:41:44 -08:00
|
|
|
|
2009-10-18 17:41:24 -07:00
|
|
|
if (cg->flags & TCF_IN_FUNCTION) {
|
|
|
|
fun = cg->fun;
|
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun) && !FUN_SCRIPT(fun));
|
2009-10-21 11:33:21 -07:00
|
|
|
if (fun->u.i.nupvars != 0) {
|
|
|
|
/*
|
|
|
|
* FIXME: upvar uses that were all optimized away may leave
|
|
|
|
* fun->u.i.nupvars non-zero, and since that count is added
|
|
|
|
* into fun->countLocalNames() in order to discriminate the
|
|
|
|
* fun->u.i.names union, we cannot force fun->u.i.nupvars
|
|
|
|
* to 0 to match JSScript::emptyScript()->upvars()->length.
|
|
|
|
* So we skip the empty script optimization.
|
|
|
|
*
|
|
|
|
* Fixing this requires the compiler to track upvar uses as
|
|
|
|
* it analyzes and optimizes closures, and subsequently as
|
|
|
|
* the emitter performs useless expression elimination.
|
|
|
|
*/
|
|
|
|
goto skip_empty;
|
|
|
|
}
|
2009-10-18 17:41:24 -07:00
|
|
|
js_FreezeLocalNames(cx, fun);
|
|
|
|
fun->u.i.script = empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_RUNTIME_METER(cx->runtime, liveEmptyScripts);
|
|
|
|
JS_RUNTIME_METER(cx->runtime, totalEmptyScripts);
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 11:33:21 -07:00
|
|
|
skip_empty:
|
2007-03-22 10:30:00 -07:00
|
|
|
CG_COUNT_FINAL_SRCNOTES(cg, nsrcnotes);
|
2008-08-21 03:47:33 -07:00
|
|
|
script = js_NewScript(cx, prologLength + mainLength, nsrcnotes,
|
|
|
|
cg->atomList.count, cg->objectList.length,
|
|
|
|
cg->upvarList.count, cg->regexpList.length,
|
|
|
|
cg->ntrynotes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!script)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Now that we have script, error control flow must go to label bad. */
|
|
|
|
script->main += prologLength;
|
|
|
|
memcpy(script->code, CG_PROLOG_BASE(cg), prologLength * sizeof(jsbytecode));
|
|
|
|
memcpy(script->main, CG_BASE(cg), mainLength * sizeof(jsbytecode));
|
2009-04-05 21:17:22 -07:00
|
|
|
nfixed = (cg->flags & TCF_IN_FUNCTION)
|
|
|
|
? cg->fun->u.i.nvars
|
2010-01-06 15:12:54 -08:00
|
|
|
: cg->ngvars + cg->sharpSlots();
|
2008-07-20 13:13:17 -07:00
|
|
|
JS_ASSERT(nfixed < SLOTNO_LIMIT);
|
|
|
|
script->nfixed = (uint16) nfixed;
|
2007-07-08 02:03:34 -07:00
|
|
|
js_InitAtomMap(cx, &script->atomMap, &cg->atomList);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-05 21:17:22 -07:00
|
|
|
filename = cg->compiler->tokenStream.filename;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (filename) {
|
|
|
|
script->filename = js_SaveScriptFilename(cx, filename);
|
|
|
|
if (!script->filename)
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
script->lineno = cg->firstLine;
|
2008-08-21 03:47:33 -07:00
|
|
|
if (script->nfixed + cg->maxStackDepth >= JS_BIT(16)) {
|
|
|
|
js_ReportCompileErrorNumber(cx, CG_TS(cg), NULL, JSREPORT_ERROR,
|
|
|
|
JSMSG_NEED_DIET, "script");
|
|
|
|
goto bad;
|
|
|
|
}
|
2008-07-20 13:13:17 -07:00
|
|
|
script->nslots = script->nfixed + cg->maxStackDepth;
|
2009-04-05 21:17:22 -07:00
|
|
|
script->staticLevel = cg->staticLevel;
|
|
|
|
script->principals = cg->compiler->principals;
|
2007-10-02 16:10:09 -07:00
|
|
|
if (script->principals)
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPRINCIPALS_HOLD(cx, script->principals);
|
|
|
|
|
2009-09-09 16:52:55 -07:00
|
|
|
if (!js_FinishTakingSrcNotes(cx, cg, script->notes()))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto bad;
|
2007-07-08 02:03:34 -07:00
|
|
|
if (cg->ntrynotes != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
js_FinishTakingTryNotes(cg, script->trynotes());
|
2007-07-08 02:03:34 -07:00
|
|
|
if (cg->objectList.length != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
cg->objectList.finish(script->objects());
|
2007-07-08 02:03:34 -07:00
|
|
|
if (cg->regexpList.length != 0)
|
2009-09-09 16:52:55 -07:00
|
|
|
cg->regexpList.finish(script->regexps());
|
2009-04-05 21:17:22 -07:00
|
|
|
if (cg->flags & TCF_NO_SCRIPT_RVAL)
|
2009-10-08 10:29:03 -07:00
|
|
|
script->noScriptRval = true;
|
2009-09-09 20:21:15 -07:00
|
|
|
if (cg->hasSharps())
|
2009-10-08 10:29:03 -07:00
|
|
|
script->hasSharps = true;
|
2009-11-18 13:33:53 -08:00
|
|
|
if (cg->flags & TCF_STRICT_MODE_CODE)
|
|
|
|
script->strictModeCode = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-21 03:47:33 -07:00
|
|
|
if (cg->upvarList.count != 0) {
|
|
|
|
JS_ASSERT(cg->upvarList.count <= cg->upvarMap.length);
|
2009-09-09 16:52:55 -07:00
|
|
|
memcpy(script->upvars()->vector, cg->upvarMap.vector,
|
2008-08-21 03:47:33 -07:00
|
|
|
cg->upvarList.count * sizeof(uint32));
|
2009-04-05 21:17:22 -07:00
|
|
|
cg->upvarList.clear();
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(cg->upvarMap.vector);
|
2008-08-21 03:47:33 -07:00
|
|
|
cg->upvarMap.vector = NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
/*
|
|
|
|
* We initialize fun->u.script to be the script constructed above
|
|
|
|
* so that the debugger has a valid FUN_SCRIPT(fun).
|
|
|
|
*/
|
2008-03-28 15:27:36 -07:00
|
|
|
fun = NULL;
|
2009-04-05 21:17:22 -07:00
|
|
|
if (cg->flags & TCF_IN_FUNCTION) {
|
|
|
|
fun = cg->fun;
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_ASSERT(FUN_INTERPRETED(fun) && !FUN_SCRIPT(fun));
|
2009-10-18 17:41:24 -07:00
|
|
|
if (script->upvarsOffset != 0)
|
|
|
|
JS_ASSERT(script->upvars()->length == fun->u.i.nupvars);
|
|
|
|
else
|
|
|
|
fun->u.i.nupvars = 0;
|
2008-08-21 03:47:33 -07:00
|
|
|
|
2008-03-28 15:27:36 -07:00
|
|
|
js_FreezeLocalNames(cx, fun);
|
2008-03-29 03:34:29 -07:00
|
|
|
fun->u.i.script = script;
|
2008-02-07 15:18:45 -08:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
script->owner = NULL;
|
|
|
|
#endif
|
2009-04-05 21:17:22 -07:00
|
|
|
if (cg->flags & TCF_FUN_HEAVYWEIGHT)
|
2008-03-28 15:27:36 -07:00
|
|
|
fun->flags |= JSFUN_HEAVYWEIGHT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the debugger about this compiled script. */
|
2008-03-28 15:27:36 -07:00
|
|
|
js_CallNewScriptHook(cx, script, fun);
|
2009-10-18 17:41:24 -07:00
|
|
|
JS_RUNTIME_METER(cx->runtime, liveScripts);
|
|
|
|
JS_RUNTIME_METER(cx->runtime, totalScripts);
|
2007-03-22 10:30:00 -07:00
|
|
|
return script;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
js_DestroyScript(cx, script);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-18 17:41:24 -07:00
|
|
|
JS_ASSERT(script != JSScript::emptyScript());
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSNewScriptHook hook;
|
|
|
|
|
2007-06-14 23:44:18 -07:00
|
|
|
hook = cx->debugHooks->newScriptHook;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (hook) {
|
2007-06-14 23:44:18 -07:00
|
|
|
JS_KEEP_ATOMS(cx->runtime);
|
2008-03-29 03:34:29 -07:00
|
|
|
hook(cx, script->filename, script->lineno, script, fun,
|
2007-06-14 23:44:18 -07:00
|
|
|
cx->debugHooks->newScriptHookData);
|
|
|
|
JS_UNKEEP_ATOMS(cx->runtime);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:34:29 -07:00
|
|
|
JS_FRIEND_API(void)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_CallDestroyScriptHook(JSContext *cx, JSScript *script)
|
|
|
|
{
|
2009-10-18 17:41:24 -07:00
|
|
|
JS_ASSERT(script != JSScript::emptyScript());
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSDestroyScriptHook hook;
|
|
|
|
|
2007-06-14 23:44:18 -07:00
|
|
|
hook = cx->debugHooks->destroyScriptHook;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (hook)
|
2007-06-14 23:44:18 -07:00
|
|
|
hook(cx, script, cx->debugHooks->destroyScriptHookData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_DestroyScript(JSContext *cx, JSScript *script)
|
|
|
|
{
|
2009-10-18 17:41:24 -07:00
|
|
|
if (script == JSScript::emptyScript()) {
|
|
|
|
JS_RUNTIME_UNMETER(cx->runtime, liveEmptyScripts);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
js_CallDestroyScriptHook(cx, script);
|
|
|
|
JS_ClearScriptTraps(cx, script);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (script->principals)
|
|
|
|
JSPRINCIPALS_DROP(cx, script->principals);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2008-06-25 13:50:26 -07:00
|
|
|
if (JS_GSN_CACHE(cx).code == script->code)
|
2009-03-24 05:07:35 -07:00
|
|
|
JS_PURGE_GSN_CACHE(cx);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
|
|
|
/*
|
2009-07-23 17:59:49 -07:00
|
|
|
* Worry about purging the property cache and any compiled traces related
|
|
|
|
* to its bytecode if this script is being destroyed from JS_DestroyScript
|
|
|
|
* or equivalent according to a mandatory "New/Destroy" protocol.
|
2008-02-07 15:18:45 -08:00
|
|
|
*
|
2009-07-24 06:55:28 -07:00
|
|
|
* The GC purges all property caches when regenerating shapes upon shape
|
|
|
|
* generator overflow, so no need in that event to purge just the entries
|
|
|
|
* for this script.
|
|
|
|
*
|
|
|
|
* The GC purges trace-JITted code on every GC activation, not just when
|
|
|
|
* regenerating shapes, so we don't have to purge fragments if the GC is
|
|
|
|
* currently running.
|
|
|
|
*
|
2009-09-18 06:55:01 -07:00
|
|
|
* JS_THREADSAFE note: js_PurgePropertyCacheForScript purges only the
|
2008-02-07 15:18:45 -08:00
|
|
|
* current thread's property cache, so a script not owned by a function
|
|
|
|
* or object, which hands off lifetime management for that script to the
|
|
|
|
* GC, must be used by only one thread over its lifetime.
|
|
|
|
*
|
|
|
|
* This should be an API-compatible change, since a script is never safe
|
|
|
|
* against premature GC if shared among threads without a rooted object
|
|
|
|
* wrapping it to protect the script's mapped atoms against GC. We use
|
|
|
|
* script->owner to enforce this requirement via assertions.
|
|
|
|
*/
|
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
JS_ASSERT_IF(cx->runtime->gcRunning, !script->owner);
|
|
|
|
#endif
|
|
|
|
|
2009-07-24 12:47:55 -07:00
|
|
|
/* FIXME: bug 506341; would like to do this only if regenerating shapes. */
|
|
|
|
if (!cx->runtime->gcRunning) {
|
2008-12-09 08:38:32 -08:00
|
|
|
JSStackFrame *fp = js_GetTopStackFrame(cx);
|
|
|
|
|
|
|
|
if (!(fp && (fp->flags & JSFRAME_EVAL))) {
|
2009-09-18 06:55:01 -07:00
|
|
|
js_PurgePropertyCacheForScript(cx, script);
|
2009-07-23 18:40:13 -07:00
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
2009-07-24 12:47:55 -07:00
|
|
|
JS_ASSERT(script->owner == cx->thread);
|
2009-02-09 16:58:39 -08:00
|
|
|
#endif
|
2008-06-03 23:52:28 -07:00
|
|
|
}
|
2008-06-02 18:06:33 -07:00
|
|
|
}
|
|
|
|
|
2009-08-17 14:50:57 -07:00
|
|
|
#ifdef JS_TRACER
|
2010-01-22 14:49:18 -08:00
|
|
|
PurgeScriptFragments(cx, script);
|
2009-08-17 14:50:57 -07:00
|
|
|
#endif
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(script);
|
2009-10-18 17:41:24 -07:00
|
|
|
|
|
|
|
JS_RUNTIME_UNMETER(cx->runtime, liveScripts);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-04-16 23:53:37 -07:00
|
|
|
js_TraceScript(JSTracer *trc, JSScript *script)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSAtomMap *map;
|
|
|
|
uintN i, length;
|
|
|
|
JSAtom **vector;
|
2007-08-11 13:25:16 -07:00
|
|
|
jsval v;
|
2007-07-08 02:03:34 -07:00
|
|
|
JSObjectArray *objarray;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
map = &script->atomMap;
|
|
|
|
length = map->length;
|
|
|
|
vector = map->vector;
|
2007-07-08 02:03:34 -07:00
|
|
|
for (i = 0; i < length; i++) {
|
2007-08-11 13:25:16 -07:00
|
|
|
v = ATOM_KEY(vector[i]);
|
|
|
|
if (JSVAL_IS_TRACEABLE(v)) {
|
|
|
|
JS_SET_TRACING_INDEX(trc, "atomMap", i);
|
|
|
|
JS_CallTracer(trc, JSVAL_TO_TRACEABLE(v), JSVAL_TRACE_KIND(v));
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (script->objectsOffset != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
objarray = script->objects();
|
2007-07-08 02:03:34 -07:00
|
|
|
i = objarray->length;
|
|
|
|
do {
|
|
|
|
--i;
|
2008-01-30 18:19:49 -08:00
|
|
|
if (objarray->vector[i]) {
|
|
|
|
JS_SET_TRACING_INDEX(trc, "objects", i);
|
|
|
|
JS_CallTracer(trc, objarray->vector[i], JSTRACE_OBJECT);
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
} while (i != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->regexpsOffset != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
objarray = script->regexps();
|
2007-07-08 02:03:34 -07:00
|
|
|
i = objarray->length;
|
|
|
|
do {
|
|
|
|
--i;
|
2008-01-30 18:19:49 -08:00
|
|
|
if (objarray->vector[i]) {
|
|
|
|
JS_SET_TRACING_INDEX(trc, "regexps", i);
|
|
|
|
JS_CallTracer(trc, objarray->vector[i], JSTRACE_OBJECT);
|
|
|
|
}
|
2007-07-08 02:03:34 -07:00
|
|
|
} while (i != 0);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-20 22:18:42 -07:00
|
|
|
if (script->u.object) {
|
2008-06-12 13:58:07 -07:00
|
|
|
JS_SET_TRACING_NAME(trc, "object");
|
2008-08-20 22:18:42 -07:00
|
|
|
JS_CallTracer(trc, script->u.object, JSTRACE_OBJECT);
|
2008-06-12 13:58:07 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
if (IS_GC_MARKING_TRACER(trc) && script->filename)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_MarkScriptFilename(script->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct GSNCacheEntry {
|
|
|
|
JSDHashEntryHdr hdr;
|
|
|
|
jsbytecode *pc;
|
|
|
|
jssrcnote *sn;
|
|
|
|
} GSNCacheEntry;
|
|
|
|
|
|
|
|
#define GSN_CACHE_THRESHOLD 100
|
|
|
|
|
2009-03-24 05:07:35 -07:00
|
|
|
void
|
|
|
|
js_PurgeGSNCache(JSGSNCache *cache)
|
|
|
|
{
|
|
|
|
cache->code = NULL;
|
|
|
|
if (cache->table.ops) {
|
|
|
|
JS_DHashTableFinish(&cache->table);
|
|
|
|
cache->table.ops = NULL;
|
|
|
|
}
|
|
|
|
GSN_CACHE_METER(cache, purges);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
jssrcnote *
|
|
|
|
js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc)
|
|
|
|
{
|
|
|
|
ptrdiff_t target, offset;
|
|
|
|
GSNCacheEntry *entry;
|
|
|
|
jssrcnote *sn, *result;
|
|
|
|
uintN nsrcnotes;
|
|
|
|
|
|
|
|
|
2009-02-18 09:14:51 -08:00
|
|
|
target = pc - script->code;
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((uint32)target >= script->length)
|
|
|
|
return NULL;
|
|
|
|
|
2008-06-25 13:50:26 -07:00
|
|
|
if (JS_GSN_CACHE(cx).code == script->code) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_METER_GSN_CACHE(cx, hits);
|
|
|
|
entry = (GSNCacheEntry *)
|
|
|
|
JS_DHashTableOperate(&JS_GSN_CACHE(cx).table, pc,
|
|
|
|
JS_DHASH_LOOKUP);
|
|
|
|
return entry->sn;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_METER_GSN_CACHE(cx, misses);
|
|
|
|
offset = 0;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); ; sn = SN_NEXT(sn)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (SN_IS_TERMINATOR(sn)) {
|
|
|
|
result = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += SN_DELTA(sn);
|
|
|
|
if (offset == target && SN_IS_GETTABLE(sn)) {
|
|
|
|
result = sn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-25 13:50:26 -07:00
|
|
|
if (JS_GSN_CACHE(cx).code != script->code &&
|
2007-03-22 10:30:00 -07:00
|
|
|
script->length >= GSN_CACHE_THRESHOLD) {
|
2009-03-24 05:07:35 -07:00
|
|
|
JS_PURGE_GSN_CACHE(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrcnotes = 0;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); !SN_IS_TERMINATOR(sn);
|
2007-03-22 10:30:00 -07:00
|
|
|
sn = SN_NEXT(sn)) {
|
|
|
|
if (SN_IS_GETTABLE(sn))
|
|
|
|
++nsrcnotes;
|
|
|
|
}
|
|
|
|
if (!JS_DHashTableInit(&JS_GSN_CACHE(cx).table, JS_DHashGetStubOps(),
|
|
|
|
NULL, sizeof(GSNCacheEntry),
|
|
|
|
JS_DHASH_DEFAULT_CAPACITY(nsrcnotes))) {
|
|
|
|
JS_GSN_CACHE(cx).table.ops = NULL;
|
|
|
|
} else {
|
|
|
|
pc = script->code;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); !SN_IS_TERMINATOR(sn);
|
2007-03-22 10:30:00 -07:00
|
|
|
sn = SN_NEXT(sn)) {
|
|
|
|
pc += SN_DELTA(sn);
|
|
|
|
if (SN_IS_GETTABLE(sn)) {
|
|
|
|
entry = (GSNCacheEntry *)
|
|
|
|
JS_DHashTableOperate(&JS_GSN_CACHE(cx).table, pc,
|
|
|
|
JS_DHASH_ADD);
|
|
|
|
entry->pc = pc;
|
|
|
|
entry->sn = sn;
|
|
|
|
}
|
|
|
|
}
|
2008-06-25 13:50:26 -07:00
|
|
|
JS_GSN_CACHE(cx).code = script->code;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_METER_GSN_CACHE(cx, fills);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-11-13 00:30:20 -08:00
|
|
|
uintN
|
|
|
|
js_FramePCToLineNumber(JSContext *cx, JSStackFrame *fp)
|
|
|
|
{
|
|
|
|
return js_PCToLineNumber(cx, fp->script, fp->imacpc ? fp->imacpc : fp->regs->pc);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN
|
|
|
|
js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc)
|
|
|
|
{
|
2009-02-18 16:31:09 -08:00
|
|
|
JSOp op;
|
2008-03-29 03:34:29 -07:00
|
|
|
JSFunction *fun;
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN lineno;
|
|
|
|
ptrdiff_t offset, target;
|
|
|
|
jssrcnote *sn;
|
|
|
|
JSSrcNoteType type;
|
|
|
|
|
|
|
|
/* Cope with JSStackFrame.pc value prior to entering js_Interpret. */
|
|
|
|
if (!pc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special case: function definition needs no line number note because
|
|
|
|
* the function's script contains its starting line number.
|
|
|
|
*/
|
2009-02-18 16:31:09 -08:00
|
|
|
op = js_GetOpcode(cx, script, pc);
|
|
|
|
if (js_CodeSpec[op].format & JOF_INDEXBASE)
|
|
|
|
pc += js_CodeSpec[op].length;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*pc == JSOP_DEFFUN) {
|
2008-04-02 00:46:12 -07:00
|
|
|
GET_FUNCTION_FROM_BYTECODE(script, pc, 0, fun);
|
2008-03-29 03:34:29 -07:00
|
|
|
return fun->u.i.script->lineno;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* General case: walk through source notes accumulating their deltas,
|
|
|
|
* keeping track of line-number notes, until we pass the note for pc's
|
|
|
|
* offset within script->code.
|
|
|
|
*/
|
|
|
|
lineno = script->lineno;
|
|
|
|
offset = 0;
|
2009-02-18 09:14:51 -08:00
|
|
|
target = pc - script->code;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
offset += SN_DELTA(sn);
|
|
|
|
type = (JSSrcNoteType) SN_TYPE(sn);
|
|
|
|
if (type == SRC_SETLINE) {
|
|
|
|
if (offset <= target)
|
|
|
|
lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
|
|
|
|
} else if (type == SRC_NEWLINE) {
|
|
|
|
if (offset <= target)
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
if (offset > target)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return lineno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The line number limit is the same as the jssrcnote offset limit. */
|
|
|
|
#define SN_LINE_LIMIT (SN_3BYTE_OFFSET_FLAG << 16)
|
|
|
|
|
|
|
|
jsbytecode *
|
|
|
|
js_LineNumberToPC(JSScript *script, uintN target)
|
|
|
|
{
|
|
|
|
ptrdiff_t offset, best;
|
|
|
|
uintN lineno, bestdiff, diff;
|
|
|
|
jssrcnote *sn;
|
|
|
|
JSSrcNoteType type;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
best = -1;
|
|
|
|
lineno = script->lineno;
|
|
|
|
bestdiff = SN_LINE_LIMIT;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
|
2007-04-02 11:13:22 -07:00
|
|
|
/*
|
|
|
|
* Exact-match only if offset is not in the prolog; otherwise use
|
|
|
|
* nearest greater-or-equal line number match.
|
|
|
|
*/
|
|
|
|
if (lineno == target && script->code + offset >= script->main)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto out;
|
2007-04-02 11:13:22 -07:00
|
|
|
if (lineno >= target) {
|
2007-03-22 10:30:00 -07:00
|
|
|
diff = lineno - target;
|
|
|
|
if (diff < bestdiff) {
|
|
|
|
bestdiff = diff;
|
|
|
|
best = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += SN_DELTA(sn);
|
|
|
|
type = (JSSrcNoteType) SN_TYPE(sn);
|
|
|
|
if (type == SRC_SETLINE) {
|
|
|
|
lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
|
|
|
|
} else if (type == SRC_NEWLINE) {
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best >= 0)
|
|
|
|
offset = best;
|
|
|
|
out:
|
|
|
|
return script->code + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(uintN)
|
|
|
|
js_GetScriptLineExtent(JSScript *script)
|
|
|
|
{
|
|
|
|
uintN lineno;
|
|
|
|
jssrcnote *sn;
|
|
|
|
JSSrcNoteType type;
|
|
|
|
|
|
|
|
lineno = script->lineno;
|
2009-09-09 16:52:55 -07:00
|
|
|
for (sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
type = (JSSrcNoteType) SN_TYPE(sn);
|
|
|
|
if (type == SRC_SETLINE) {
|
|
|
|
lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
|
|
|
|
} else if (type == SRC_NEWLINE) {
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1 + lineno - script->lineno;
|
|
|
|
}
|