2010-10-29 08:05:55 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 40; indent-tabs-mode: nil -*- */
|
|
|
|
/* vim: set ts=40 sw=4 et tw=99: */
|
|
|
|
/* ***** 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 the Mozilla SpiderMonkey bytecode type inference
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brian Hackett <bhackett@mozilla.com>
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2010-11-01 20:03:46 -07:00
|
|
|
/* Inline members for javascript type inference. */
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
#include "jsanalyze.h"
|
|
|
|
#include "jscompartment.h"
|
|
|
|
#include "jsinfer.h"
|
2010-10-29 11:44:30 -07:00
|
|
|
#include "jsprf.h"
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
#ifndef jsinferinlines_h___
|
|
|
|
#define jsinferinlines_h___
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Types
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
namespace types {
|
|
|
|
|
|
|
|
inline jstype
|
|
|
|
GetValueType(JSContext *cx, const Value &val)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(cx->typeInferenceEnabled());
|
2010-10-29 08:05:55 -07:00
|
|
|
if (val.isDouble())
|
|
|
|
return TYPE_DOUBLE;
|
|
|
|
switch (val.extractNonDoubleType()) {
|
|
|
|
case JSVAL_TYPE_INT32:
|
|
|
|
return TYPE_INT32;
|
|
|
|
case JSVAL_TYPE_UNDEFINED:
|
|
|
|
return TYPE_UNDEFINED;
|
|
|
|
case JSVAL_TYPE_BOOLEAN:
|
|
|
|
return TYPE_BOOLEAN;
|
|
|
|
case JSVAL_TYPE_STRING:
|
|
|
|
return TYPE_STRING;
|
|
|
|
case JSVAL_TYPE_NULL:
|
|
|
|
return TYPE_NULL;
|
|
|
|
case JSVAL_TYPE_OBJECT: {
|
|
|
|
JSObject *obj = &val.toObject();
|
2010-12-18 20:44:51 -08:00
|
|
|
JS_ASSERT(obj->type);
|
|
|
|
return (jstype) obj->type;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
JS_NOT_REACHED("Unknown value");
|
|
|
|
return (jstype) 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the canonical representation of an id to use when doing inference. This
|
|
|
|
* maintains the constraint that if two different jsids map to the same property
|
|
|
|
* in JS (e.g. 3 and "3"), they have the same type representation.
|
|
|
|
*/
|
|
|
|
inline jsid
|
2010-12-18 20:44:51 -08:00
|
|
|
MakeTypeId(JSContext *cx, jsid id)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* All integers must map to the aggregate property for index types, including
|
|
|
|
* negative integers.
|
|
|
|
*/
|
|
|
|
if (JSID_IS_INT(id))
|
|
|
|
return JSID_VOID;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for numeric strings, as in js_StringIsIndex, but allow negative
|
2010-12-18 20:44:51 -08:00
|
|
|
* and overflowing integers.
|
2010-10-29 08:05:55 -07:00
|
|
|
*/
|
|
|
|
if (JSID_IS_STRING(id)) {
|
2010-12-20 09:06:43 -08:00
|
|
|
JSFlatString *str = JSID_TO_FLAT_STRING(id);
|
|
|
|
const jschar *cp = str->getCharsZ(cx);
|
2010-10-29 08:05:55 -07:00
|
|
|
if (JS7_ISDEC(*cp) || *cp == '-') {
|
|
|
|
cp++;
|
|
|
|
while (JS7_ISDEC(*cp))
|
|
|
|
cp++;
|
|
|
|
if (unsigned(cp - str->chars()) == str->length())
|
|
|
|
return JSID_VOID;
|
|
|
|
}
|
2011-03-03 14:07:48 -08:00
|
|
|
return id;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return JSID_VOID;
|
|
|
|
}
|
|
|
|
|
2010-12-19 08:09:45 -08:00
|
|
|
const char * TypeIdStringImpl(jsid id);
|
|
|
|
|
2010-11-18 15:18:23 -08:00
|
|
|
/* Convert an id for printing during debug. */
|
|
|
|
static inline const char *
|
2010-11-20 15:45:52 -08:00
|
|
|
TypeIdString(jsid id)
|
2010-11-18 15:18:23 -08:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-12-07 17:11:37 -08:00
|
|
|
return TypeIdStringImpl(id);
|
2010-11-18 15:18:23 -08:00
|
|
|
#else
|
2010-11-24 17:41:52 -08:00
|
|
|
return "(missing)";
|
2010-11-18 15:18:23 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
/*
|
|
|
|
* Structure for type inference entry point functions. All functions which can
|
|
|
|
* change type information must use this, and functions which depend on
|
|
|
|
* intermediate types (i.e. JITs) can use this to ensure that intermediate
|
|
|
|
* information is not collected and does not change.
|
|
|
|
*
|
|
|
|
* Pins inference results so that intermediate type information, TypeObjects
|
|
|
|
* and JSScripts won't be collected during GC. Does additional sanity checking
|
|
|
|
* that inference is not reentrant and that recompilations occur properly.
|
|
|
|
*/
|
|
|
|
struct AutoEnterTypeInference
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
#ifdef DEBUG
|
|
|
|
unsigned depth;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AutoEnterTypeInference(JSContext *cx, bool compiling = false)
|
|
|
|
: cx(cx)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
depth = cx->compartment->types.inferenceDepth;
|
|
|
|
#endif
|
|
|
|
JS_ASSERT_IF(!compiling, cx->compartment->types.inferenceEnabled);
|
|
|
|
if (cx->compartment->types.inferenceDepth++ == 0)
|
|
|
|
cx->compartment->types.inferenceStartTime = cx->compartment->types.currentTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEnterTypeInference()
|
|
|
|
{
|
|
|
|
/* This should have been reset by checkPendingRecompiles. */
|
|
|
|
JS_ASSERT(cx->compartment->types.inferenceDepth == depth);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
TypeCompartment::checkPendingRecompiles(JSContext *cx)
|
|
|
|
{
|
|
|
|
JS_ASSERT(inferenceDepth);
|
|
|
|
if (--inferenceDepth != 0) {
|
|
|
|
/*
|
|
|
|
* There is still a type inference activation on the stack, wait for it to
|
|
|
|
* finish before handling any recompilations. Note that we should not be
|
|
|
|
* invoking any scripted code while the inference is running :TODO: assert this.
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (inferenceStartTime)
|
|
|
|
analysisTime += currentTime() - inferenceStartTime;
|
|
|
|
inferenceStartTime = 0;
|
|
|
|
if (pendingNukeTypes)
|
|
|
|
return nukeTypes(cx);
|
|
|
|
else if (pendingRecompiles && !processPendingRecompiles(cx))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
} } /* namespace js::types */
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// JSContext
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSContext::typeInferenceEnabled()
|
|
|
|
{
|
|
|
|
return compartment->types.inferenceEnabled;
|
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
inline js::types::TypeObject *
|
2010-12-18 20:44:51 -08:00
|
|
|
JSContext::getTypeNewObject(JSProtoKey key)
|
2010-11-24 17:41:52 -08:00
|
|
|
{
|
2010-12-18 20:44:51 -08:00
|
|
|
JSObject *proto;
|
|
|
|
if (!js_GetClassPrototype(this, NULL, key, &proto, NULL))
|
|
|
|
return NULL;
|
|
|
|
return proto->getNewType(this);
|
2010-11-24 17:41:52 -08:00
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
inline js::types::TypeObject *
|
|
|
|
JSContext::getTypeCallerInitObject(bool isArray)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (typeInferenceEnabled()) {
|
|
|
|
JSStackFrame *caller = js_GetScriptedCaller(this, NULL);
|
|
|
|
if (caller)
|
|
|
|
return caller->script()->getTypeInitObject(this, caller->pc(this), isArray);
|
|
|
|
}
|
2010-12-19 07:41:41 -08:00
|
|
|
return getTypeNewObject(isArray ? JSProto_Array : JSProto_Object);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::markTypeCallerUnexpected(js::types::jstype type)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!typeInferenceEnabled())
|
|
|
|
return true;
|
2011-03-13 21:59:03 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we are actually at a scripted callsite. This function is
|
|
|
|
* called from JS natives which can be called anywhere a script can be
|
|
|
|
* called, such as on property getters or setters. This filtering is not
|
|
|
|
* perfect, but we only need to make sure the type result is added wherever
|
|
|
|
* the native's type handler was used, i.e. at scripted callsites directly
|
|
|
|
* calling the native.
|
|
|
|
*/
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
JSStackFrame *caller = js_GetScriptedCaller(this, NULL);
|
2010-12-19 07:41:41 -08:00
|
|
|
if (!caller)
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
2011-03-13 21:59:03 -07:00
|
|
|
|
|
|
|
switch ((JSOp)*caller->pc(this)) {
|
|
|
|
case JSOP_CALL:
|
|
|
|
case JSOP_EVAL:
|
|
|
|
case JSOP_FUNCALL:
|
|
|
|
case JSOP_FUNAPPLY:
|
|
|
|
case JSOP_NEW:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return caller->script()->typeMonitorResult(this, caller->pc(this), type);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::markTypeCallerUnexpected(const js::Value &value)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
return markTypeCallerUnexpected(js::types::GetValueType(this, value));
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::markTypeCallerOverflow()
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
return markTypeCallerUnexpected(js::types::TYPE_DOUBLE);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::addTypeProperty(js::types::TypeObject *obj, const char *name, js::types::jstype type)
|
|
|
|
{
|
2011-03-05 17:13:40 -08:00
|
|
|
if (typeInferenceEnabled() && !obj->unknownProperties) {
|
2011-03-03 14:07:48 -08:00
|
|
|
jsid id = JSID_VOID;
|
|
|
|
if (name) {
|
|
|
|
JSAtom *atom = js_Atomize(this, name, strlen(name), 0);
|
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
id = ATOM_TO_JSID(atom);
|
|
|
|
}
|
|
|
|
return addTypePropertyId(obj, id, type);
|
|
|
|
}
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::addTypeProperty(js::types::TypeObject *obj, const char *name, const js::Value &value)
|
|
|
|
{
|
2011-03-05 17:13:40 -08:00
|
|
|
if (typeInferenceEnabled() && !obj->unknownProperties)
|
2011-03-03 14:07:48 -08:00
|
|
|
return addTypeProperty(obj, name, js::types::GetValueType(this, value));
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::addTypePropertyId(js::types::TypeObject *obj, jsid id, js::types::jstype type)
|
|
|
|
{
|
2011-03-05 17:13:40 -08:00
|
|
|
if (!typeInferenceEnabled() || obj->unknownProperties)
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
/* Convert string index properties into the common index property. */
|
2010-12-18 20:44:51 -08:00
|
|
|
id = js::types::MakeTypeId(this, id);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::AutoEnterTypeInference enter(this);
|
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
js::types::TypeSet *types = obj->getProperty(this, id, true);
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!types || types->hasType(type))
|
|
|
|
return compartment->types.checkPendingRecompiles(this);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::InferSpew(js::types::ISpewDynamic, "AddBuiltin: %s %s: %s",
|
|
|
|
obj->name(), js::types::TypeIdString(id),
|
|
|
|
js::types::TypeString(type));
|
|
|
|
types->addType(this, type);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return compartment->types.checkPendingRecompiles(this);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::addTypePropertyId(js::types::TypeObject *obj, jsid id, const js::Value &value)
|
|
|
|
{
|
2011-03-05 17:13:40 -08:00
|
|
|
if (typeInferenceEnabled() && !obj->unknownProperties)
|
2011-03-03 14:07:48 -08:00
|
|
|
return addTypePropertyId(obj, id, js::types::GetValueType(this, value));
|
|
|
|
return true;
|
2010-12-21 18:26:09 -08:00
|
|
|
}
|
|
|
|
|
2011-03-04 07:50:32 -08:00
|
|
|
inline js::types::TypeObject *
|
|
|
|
JSContext::getTypeEmpty()
|
|
|
|
{
|
2011-03-09 09:58:49 -08:00
|
|
|
return &compartment->types.typeEmpty;
|
2010-12-18 20:44:51 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
JSContext::aliasTypeProperties(js::types::TypeObject *obj, jsid first, jsid second)
|
|
|
|
{
|
2011-03-09 09:58:49 -08:00
|
|
|
if (!typeInferenceEnabled() || obj->unknownProperties)
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
js::types::AutoEnterTypeInference enter(this);
|
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
first = js::types::MakeTypeId(this, first);
|
|
|
|
second = js::types::MakeTypeId(this, second);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
js::types::TypeSet *firstTypes = obj->getProperty(this, first, true);
|
|
|
|
js::types::TypeSet *secondTypes = obj->getProperty(this, second, true);
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!firstTypes || !secondTypes)
|
|
|
|
return false;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
firstTypes->addBaseSubset(this, obj, secondTypes);
|
|
|
|
secondTypes->addBaseSubset(this, obj, firstTypes);
|
2011-03-03 14:07:48 -08:00
|
|
|
|
|
|
|
return compartment->types.checkPendingRecompiles(this);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSContext::markTypeArrayNotPacked(js::types::TypeObject *obj, bool notDense)
|
2010-11-05 07:37:09 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!typeInferenceEnabled() || (notDense ? !obj->isDenseArray : !obj->isPackedArray))
|
|
|
|
return true;
|
|
|
|
js::types::AutoEnterTypeInference enter(this);
|
2010-11-20 15:45:52 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
obj->markNotPacked(this, notDense);
|
2010-11-18 08:28:05 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return compartment->types.checkPendingRecompiles(this);
|
2010-11-05 07:37:09 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
bool
|
2010-11-20 15:45:52 -08:00
|
|
|
JSContext::markTypeObjectUnknownProperties(js::types::TypeObject *obj)
|
2010-11-09 14:40:10 -08:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!typeInferenceEnabled() || obj->unknownProperties)
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::AutoEnterTypeInference enter(this);
|
|
|
|
obj->markUnknown(this);
|
|
|
|
return compartment->types.checkPendingRecompiles(this);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSContext::typeMonitorAssign(JSObject *obj, jsid id, const js::Value &rval)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (typeInferenceEnabled())
|
|
|
|
return compartment->types.dynamicAssign(this, obj, id, rval);
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSContext::typeMonitorCall(const js::CallArgs &args, bool constructing)
|
2010-11-24 17:41:52 -08:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!typeInferenceEnabled() || !args.callee().isObject())
|
|
|
|
return true;
|
2010-11-24 17:41:52 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
JSObject *callee = &args.callee().toObject();
|
|
|
|
if (!callee->isFunction() || !callee->getFunctionPrivate()->isInterpreted())
|
|
|
|
return true;
|
2010-11-24 17:41:52 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return compartment->types.dynamicCall(this, callee, args, constructing);
|
2010-11-24 17:41:52 -08:00
|
|
|
}
|
|
|
|
|
2011-03-10 16:17:39 -08:00
|
|
|
inline bool
|
|
|
|
JSContext::fixArrayType(JSObject *obj)
|
|
|
|
{
|
|
|
|
return !typeInferenceEnabled() || compartment->types.fixArrayType(this, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
JSContext::fixObjectType(JSObject *obj)
|
|
|
|
{
|
|
|
|
return !typeInferenceEnabled() || compartment->types.fixObjectType(this, obj);
|
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// JSScript
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::ensureVarTypes(JSContext *cx)
|
|
|
|
{
|
|
|
|
if (varTypes)
|
|
|
|
return true;
|
|
|
|
return makeVarTypes(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeSet *
|
|
|
|
JSScript::returnTypes()
|
|
|
|
{
|
|
|
|
JS_ASSERT(varTypes);
|
|
|
|
return &varTypes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeSet *
|
|
|
|
JSScript::thisTypes()
|
|
|
|
{
|
|
|
|
JS_ASSERT(varTypes);
|
|
|
|
return &varTypes[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeSet *
|
|
|
|
JSScript::argTypes(unsigned i)
|
|
|
|
{
|
|
|
|
JS_ASSERT(varTypes && fun && i < fun->nargs);
|
|
|
|
return &varTypes[2 + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeSet *
|
|
|
|
JSScript::localTypes(unsigned i)
|
|
|
|
{
|
|
|
|
JS_ASSERT(varTypes && i < nfixed);
|
|
|
|
if (fun)
|
|
|
|
i += fun->nargs;
|
|
|
|
return &varTypes[2 + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeSet *
|
|
|
|
JSScript::upvarTypes(unsigned i)
|
|
|
|
{
|
|
|
|
JS_ASSERT(varTypes && i < bindings.countUpvars());
|
|
|
|
if (fun)
|
|
|
|
i += fun->nargs;
|
|
|
|
return &varTypes[2 + nfixed + i];
|
|
|
|
}
|
|
|
|
|
2010-12-28 11:53:50 -08:00
|
|
|
inline JSObject *
|
|
|
|
JSScript::getGlobal()
|
|
|
|
{
|
2011-03-01 13:10:05 -08:00
|
|
|
JS_ASSERT(compileAndGo && global);
|
|
|
|
return global;
|
2010-12-28 11:53:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeObject *
|
|
|
|
JSScript::getGlobalType()
|
|
|
|
{
|
|
|
|
return getGlobal()->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline js::types::TypeObject *
|
|
|
|
JSScript::getTypeNewObject(JSContext *cx, JSProtoKey key)
|
|
|
|
{
|
|
|
|
JSObject *proto;
|
|
|
|
if (!js_GetClassPrototype(cx, getGlobal(), key, &proto, NULL))
|
|
|
|
return NULL;
|
|
|
|
return proto->getNewType(cx);
|
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
inline js::types::TypeObject *
|
|
|
|
JSScript::getTypeInitObject(JSContext *cx, const jsbytecode *pc, bool isArray)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!cx->typeInferenceEnabled() || !compileAndGo)
|
2010-12-19 12:21:15 -08:00
|
|
|
return cx->getTypeNewObject(isArray ? JSProto_Array : JSProto_Object);
|
2010-12-28 11:53:50 -08:00
|
|
|
|
|
|
|
uint32 offset = pc - code;
|
2011-03-01 13:10:05 -08:00
|
|
|
js::types::TypeObject *prev = NULL, *obj = typeObjects;
|
2010-12-28 11:53:50 -08:00
|
|
|
while (obj) {
|
|
|
|
if (isArray ? obj->initializerArray : obj->initializerObject) {
|
|
|
|
if (obj->initializerOffset == offset) {
|
|
|
|
/* Move this to the head of the objects list, maintain LRU order. */
|
|
|
|
if (prev) {
|
|
|
|
prev->next = obj->next;
|
2011-03-01 13:10:05 -08:00
|
|
|
obj->next = typeObjects;
|
|
|
|
typeObjects = obj;
|
2010-12-28 11:53:50 -08:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = obj;
|
|
|
|
obj = obj->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cx->compartment->types.newInitializerTypeObject(cx, this, offset, isArray);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeMonitorResult(JSContext *cx, const jsbytecode *pc,
|
2010-11-15 18:13:05 -08:00
|
|
|
js::types::jstype type)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (cx->typeInferenceEnabled())
|
|
|
|
return cx->compartment->types.dynamicPush(cx, this, pc - code, type);
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeMonitorResult(JSContext *cx, const jsbytecode *pc, const js::Value &rval)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (cx->typeInferenceEnabled())
|
|
|
|
return typeMonitorResult(cx, pc, js::types::GetValueType(cx, rval));
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeMonitorOverflow(JSContext *cx, const jsbytecode *pc)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
return typeMonitorResult(cx, pc, js::types::TYPE_DOUBLE);
|
2011-03-01 13:10:05 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeMonitorUndefined(JSContext *cx, const jsbytecode *pc)
|
2010-11-15 18:13:05 -08:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
return typeMonitorResult(cx, pc, js::types::TYPE_UNDEFINED);
|
2010-11-15 18:13:05 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeMonitorUnknown(JSContext *cx, const jsbytecode *pc)
|
2010-11-15 18:13:05 -08:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
return typeMonitorResult(cx, pc, js::types::TYPE_UNKNOWN);
|
2010-11-15 18:13:05 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::typeSetThis(JSContext *cx, js::types::jstype type)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(cx->typeInferenceEnabled());
|
|
|
|
if (!ensureVarTypes(cx))
|
|
|
|
return false;
|
2011-03-04 11:28:52 -08:00
|
|
|
|
|
|
|
/* Analyze the script regardless if -a was used. */
|
|
|
|
bool analyze = !types && cx->hasRunOption(JSOPTION_METHODJIT_ALWAYS) && !isUncachedEval;
|
|
|
|
|
|
|
|
if (!thisTypes()->hasType(type) || analyze) {
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::AutoEnterTypeInference enter(cx);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::InferSpew(js::types::ISpewDynamic, "AddThis: #%u: %s",
|
|
|
|
id(), js::types::TypeString(type));
|
|
|
|
thisTypes()->addType(cx, type);
|
|
|
|
|
2011-03-05 17:34:09 -08:00
|
|
|
if (analyze && !types)
|
2011-03-04 11:28:52 -08:00
|
|
|
js::types::AnalyzeScriptTypes(cx, this);
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return cx->compartment->types.checkPendingRecompiles(cx);
|
|
|
|
}
|
2011-03-04 11:28:52 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-10 12:01:11 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::typeSetNewCalled(JSContext *cx)
|
|
|
|
{
|
|
|
|
if (!cx->typeInferenceEnabled() || calledWithNew)
|
|
|
|
return true;
|
|
|
|
calledWithNew = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determining the 'this' type used when the script is invoked with 'new'
|
|
|
|
* happens during the script's prologue, so we don't try to pick it up from
|
|
|
|
* dynamic calls. Instead, generate constraints modeling the construction
|
|
|
|
* of 'this' when the script is analyzed or reanalyzed after an invoke with 'new',
|
|
|
|
* and if 'new' is first invoked after the script has already been analyzed.
|
|
|
|
*/
|
|
|
|
if (analyzed) {
|
|
|
|
/* Regenerate types for the function. */
|
|
|
|
js::types::AutoEnterTypeInference enter(cx);
|
|
|
|
js::types::AnalyzeScriptNew(cx, this);
|
|
|
|
if (!cx->compartment->types.checkPendingRecompiles(cx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2011-03-11 16:29:38 -08:00
|
|
|
JSScript::typeSetLocal(JSContext *cx, unsigned local, js::types::jstype type)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!cx->typeInferenceEnabled())
|
|
|
|
return true;
|
2011-03-01 13:10:05 -08:00
|
|
|
if (!ensureVarTypes(cx))
|
2011-03-03 14:07:48 -08:00
|
|
|
return false;
|
|
|
|
if (!localTypes(local)->hasType(type)) {
|
|
|
|
js::types::AutoEnterTypeInference enter(cx);
|
|
|
|
|
|
|
|
js::types::InferSpew(js::types::ISpewDynamic, "SetLocal: #%u %u: %s",
|
|
|
|
id(), local, js::types::TypeString(type));
|
|
|
|
localTypes(local)->addType(cx, type);
|
|
|
|
|
|
|
|
return compartment->types.checkPendingRecompiles(cx);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-11 16:29:38 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::typeSetLocal(JSContext *cx, unsigned local, const js::Value &value)
|
|
|
|
{
|
|
|
|
if (cx->typeInferenceEnabled()) {
|
|
|
|
js::types::jstype type = js::types::GetValueType(cx, value);
|
|
|
|
return typeSetLocal(cx, local, type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::typeSetArgument(JSContext *cx, unsigned arg, js::types::jstype type)
|
|
|
|
{
|
|
|
|
if (!cx->typeInferenceEnabled())
|
|
|
|
return true;
|
|
|
|
if (!ensureVarTypes(cx))
|
|
|
|
return false;
|
2011-03-01 13:10:05 -08:00
|
|
|
if (!argTypes(arg)->hasType(type)) {
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::AutoEnterTypeInference enter(cx);
|
|
|
|
|
2010-12-28 11:53:50 -08:00
|
|
|
js::types::InferSpew(js::types::ISpewDynamic, "SetArgument: #%u %u: %s",
|
|
|
|
id(), arg, js::types::TypeString(type));
|
2011-03-03 14:07:48 -08:00
|
|
|
argTypes(arg)->addType(cx, type);
|
|
|
|
|
|
|
|
return cx->compartment->types.checkPendingRecompiles(cx);
|
2011-03-01 13:10:05 -08:00
|
|
|
}
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
2011-03-01 13:10:05 -08:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
|
|
|
JSScript::typeSetArgument(JSContext *cx, unsigned arg, const js::Value &value)
|
|
|
|
{
|
|
|
|
if (cx->typeInferenceEnabled()) {
|
|
|
|
js::types::jstype type = js::types::GetValueType(cx, value);
|
|
|
|
return typeSetArgument(cx, arg, type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2011-03-01 13:10:05 -08:00
|
|
|
JSScript::typeSetUpvar(JSContext *cx, unsigned upvar, const js::Value &value)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!cx->typeInferenceEnabled())
|
|
|
|
return true;
|
2011-03-01 13:10:05 -08:00
|
|
|
if (!ensureVarTypes(cx))
|
2011-03-03 14:07:48 -08:00
|
|
|
return false;
|
2011-03-01 13:10:05 -08:00
|
|
|
js::types::jstype type = js::types::GetValueType(cx, value);
|
|
|
|
if (!upvarTypes(upvar)->hasType(type)) {
|
2011-03-03 14:07:48 -08:00
|
|
|
js::types::AutoEnterTypeInference enter(cx);
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
js::types::InferSpew(js::types::ISpewDynamic, "SetUpvar: #%u %u: %s",
|
|
|
|
id(), upvar, js::types::TypeString(type));
|
2011-03-03 14:07:48 -08:00
|
|
|
upvarTypes(upvar)->addType(cx, type);
|
|
|
|
|
|
|
|
return cx->compartment->types.checkPendingRecompiles(cx);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace js {
|
2010-12-28 11:53:50 -08:00
|
|
|
namespace types {
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// TypeCompartment
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline void
|
|
|
|
TypeCompartment::addPending(JSContext *cx, TypeConstraint *constraint, TypeSet *source, jstype type)
|
|
|
|
{
|
|
|
|
JS_ASSERT(this == &cx->compartment->types);
|
|
|
|
JS_ASSERT(type);
|
2011-03-08 10:21:54 -08:00
|
|
|
JS_ASSERT(!cx->runtime->gcRunning);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
InferSpew(ISpewOps, "pending: C%p %s", constraint, TypeString(type));
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
if (pendingCount == pendingCapacity)
|
2011-03-01 13:10:05 -08:00
|
|
|
growPendingArray(cx);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
PendingWork &pending = pendingArray[pendingCount++];
|
|
|
|
pending.constraint = constraint;
|
|
|
|
pending.source = source;
|
|
|
|
pending.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
TypeCompartment::resolvePending(JSContext *cx)
|
|
|
|
{
|
|
|
|
JS_ASSERT(this == &cx->compartment->types);
|
|
|
|
|
|
|
|
if (resolving) {
|
|
|
|
/* There is an active call further up resolving the worklist. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolving = true;
|
|
|
|
|
|
|
|
/* Handle all pending type registrations. */
|
|
|
|
while (pendingCount) {
|
|
|
|
const PendingWork &pending = pendingArray[--pendingCount];
|
2011-03-01 13:10:05 -08:00
|
|
|
InferSpew(ISpewOps, "resolve: C%p %s",
|
|
|
|
pending.constraint, TypeString(pending.type));
|
2010-10-29 08:05:55 -07:00
|
|
|
pending.constraint->newType(cx, pending.source, pending.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// TypeSet
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The sets of objects and scripts in a type set grow monotonically, are usually
|
|
|
|
* empty, almost always small, and sometimes big. For empty or singleton sets,
|
|
|
|
* the pointer refers directly to the value. For sets fitting into SET_ARRAY_SIZE,
|
|
|
|
* an array of this length is used to store the elements. For larger sets, a hash
|
|
|
|
* table filled to 25%-50% of capacity is used, with collisions resolved by linear
|
|
|
|
* probing. TODO: replace these with jshashtables.
|
|
|
|
*/
|
|
|
|
const unsigned SET_ARRAY_SIZE = 8;
|
|
|
|
|
|
|
|
/* Get the capacity of a set with the given element count. */
|
|
|
|
static inline unsigned
|
|
|
|
HashSetCapacity(unsigned count)
|
|
|
|
{
|
|
|
|
JS_ASSERT(count >= 2);
|
|
|
|
|
|
|
|
if (count <= SET_ARRAY_SIZE)
|
|
|
|
return SET_ARRAY_SIZE;
|
|
|
|
|
|
|
|
unsigned log2;
|
|
|
|
JS_FLOOR_LOG2(log2, count);
|
|
|
|
return 1 << (log2 + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the FNV hash for the low 32 bits of v. */
|
2010-11-24 17:41:52 -08:00
|
|
|
template <class T, class KEY>
|
2010-10-29 08:05:55 -07:00
|
|
|
static inline uint32
|
2010-11-24 17:41:52 -08:00
|
|
|
HashKey(T v)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2010-11-24 17:41:52 -08:00
|
|
|
uint32 nv = KEY::keyBits(v);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
uint32 hash = 84696351 ^ (nv & 0xff);
|
|
|
|
hash = (hash * 16777619) ^ ((nv >> 8) & 0xff);
|
|
|
|
hash = (hash * 16777619) ^ ((nv >> 16) & 0xff);
|
|
|
|
return (hash * 16777619) ^ ((nv >> 24) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-24 17:41:52 -08:00
|
|
|
* Insert space for an element into the specified set and grow its capacity if needed.
|
|
|
|
* returned value is an existing or new entry (NULL if new).
|
2010-10-29 08:05:55 -07:00
|
|
|
*/
|
2010-11-24 17:41:52 -08:00
|
|
|
template <class T, class U, class KEY>
|
2011-03-03 14:07:48 -08:00
|
|
|
static U **
|
|
|
|
HashSetInsertTry(JSContext *cx, U **&values, unsigned &count, T key, bool pool)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
|
|
|
unsigned capacity = HashSetCapacity(count);
|
2010-11-24 17:41:52 -08:00
|
|
|
unsigned insertpos = HashKey<T,KEY>(key) & (capacity - 1);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
/* Whether we are converting from a fixed array to hashtable. */
|
|
|
|
bool converting = (count == SET_ARRAY_SIZE);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
if (!converting) {
|
|
|
|
while (values[insertpos] != NULL) {
|
|
|
|
if (KEY::getKey(values[insertpos]) == key)
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[insertpos];
|
2010-11-24 17:41:52 -08:00
|
|
|
insertpos = (insertpos + 1) & (capacity - 1);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
unsigned newCapacity = HashSetCapacity(count);
|
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
if (newCapacity == capacity) {
|
|
|
|
JS_ASSERT(!converting);
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[insertpos];
|
2010-11-24 17:41:52 -08:00
|
|
|
}
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
U **newValues = pool
|
|
|
|
? ArenaArray<U*>(cx->compartment->types.pool, newCapacity)
|
2011-03-08 10:21:54 -08:00
|
|
|
: (U **) ::js_malloc(newCapacity * sizeof(U*));
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!newValues) {
|
|
|
|
cx->compartment->types.setPendingNukeTypes(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PodZero(newValues, newCapacity);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
for (unsigned i = 0; i < capacity; i++) {
|
|
|
|
if (values[i]) {
|
|
|
|
unsigned pos = HashKey<T,KEY>(KEY::getKey(values[i])) & (newCapacity - 1);
|
|
|
|
while (newValues[pos] != NULL)
|
|
|
|
pos = (pos + 1) & (newCapacity - 1);
|
|
|
|
newValues[pos] = values[i];
|
|
|
|
}
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
if (values && !pool)
|
2011-03-08 10:21:54 -08:00
|
|
|
::js_free(values);
|
2010-11-24 17:41:52 -08:00
|
|
|
values = newValues;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
insertpos = HashKey<T,KEY>(key) & (newCapacity - 1);
|
|
|
|
while (values[insertpos] != NULL)
|
|
|
|
insertpos = (insertpos + 1) & (newCapacity - 1);
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[insertpos];
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-24 17:41:52 -08:00
|
|
|
* Insert an element into the specified set if it is not already there, returning
|
|
|
|
* an entry which is NULL if the element was not there.
|
2010-10-29 08:05:55 -07:00
|
|
|
*/
|
2010-11-24 17:41:52 -08:00
|
|
|
template <class T, class U, class KEY>
|
2011-03-03 14:07:48 -08:00
|
|
|
static inline U **
|
|
|
|
HashSetInsert(JSContext *cx, U **&values, unsigned &count, T key, bool pool)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
|
|
|
if (count == 0) {
|
2010-11-24 17:41:52 -08:00
|
|
|
JS_ASSERT(values == NULL);
|
2010-10-29 08:05:55 -07:00
|
|
|
count++;
|
2011-03-03 14:07:48 -08:00
|
|
|
return (U **) &values;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 1) {
|
2010-11-24 17:41:52 -08:00
|
|
|
U *oldData = (U*) values;
|
2011-03-03 14:07:48 -08:00
|
|
|
if (KEY::getKey(oldData) == key)
|
|
|
|
return (U **) &values;
|
|
|
|
|
|
|
|
values = pool
|
|
|
|
? ArenaArray<U*>(cx->compartment->types.pool, SET_ARRAY_SIZE)
|
2011-03-08 10:21:54 -08:00
|
|
|
: (U **) ::js_malloc(SET_ARRAY_SIZE * sizeof(U*));
|
2011-03-03 14:07:48 -08:00
|
|
|
if (!values) {
|
|
|
|
values = (U **) oldData;
|
|
|
|
cx->compartment->types.setPendingNukeTypes(cx);
|
|
|
|
return NULL;
|
2010-11-24 17:41:52 -08:00
|
|
|
}
|
2011-03-03 14:07:48 -08:00
|
|
|
PodZero(values, SET_ARRAY_SIZE);
|
2010-11-24 17:41:52 -08:00
|
|
|
count++;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
values[0] = oldData;
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[1];
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count <= SET_ARRAY_SIZE) {
|
|
|
|
for (unsigned i = 0; i < count; i++) {
|
2010-11-24 17:41:52 -08:00
|
|
|
if (KEY::getKey(values[i]) == key)
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[i];
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count < SET_ARRAY_SIZE) {
|
2010-11-24 17:41:52 -08:00
|
|
|
count++;
|
2011-03-03 14:07:48 -08:00
|
|
|
return &values[count - 1];
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return HashSetInsertTry<T,U,KEY>(cx, values, count, key, pool);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
/* Lookup an entry in a hash set, return NULL if it does not exist. */
|
|
|
|
template <class T, class U, class KEY>
|
|
|
|
static inline U *
|
|
|
|
HashSetLookup(U **values, unsigned count, T key)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
|
|
|
if (count == 0)
|
2010-11-24 17:41:52 -08:00
|
|
|
return NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
if (count == 1)
|
2010-11-24 17:41:52 -08:00
|
|
|
return (KEY::getKey((U *) values) == key) ? (U *) values : NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
if (count <= SET_ARRAY_SIZE) {
|
|
|
|
for (unsigned i = 0; i < count; i++) {
|
2010-11-24 17:41:52 -08:00
|
|
|
if (KEY::getKey(values[i]) == key)
|
|
|
|
return values[i];
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
2010-11-24 17:41:52 -08:00
|
|
|
return NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned capacity = HashSetCapacity(count);
|
2010-11-24 17:41:52 -08:00
|
|
|
unsigned pos = HashKey<T,KEY>(key) & (capacity - 1);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
while (values[pos] != NULL) {
|
2010-11-24 17:41:52 -08:00
|
|
|
if (KEY::getKey(values[pos]) == key)
|
|
|
|
return values[pos];
|
2010-10-29 08:05:55 -07:00
|
|
|
pos = (pos + 1) & (capacity - 1);
|
|
|
|
}
|
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
return NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2010-11-24 17:41:52 -08:00
|
|
|
struct TypeObjectKey {
|
2011-02-22 11:07:25 -08:00
|
|
|
static intptr_t keyBits(TypeObject *obj) { return (intptr_t) obj; }
|
2010-11-24 17:41:52 -08:00
|
|
|
static TypeObject *getKey(TypeObject *obj) { return obj; }
|
|
|
|
};
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline void
|
|
|
|
TypeSet::destroy(JSContext *cx)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!(typeFlags & TYPE_FLAG_INTERMEDIATE_SET));
|
|
|
|
if (objectCount >= 2)
|
2011-03-08 10:21:54 -08:00
|
|
|
::js_free(objectSet);
|
2011-03-06 08:15:44 -08:00
|
|
|
while (constraintList) {
|
|
|
|
TypeConstraint *next = constraintList->next;
|
|
|
|
if (constraintList->condensed() || constraintList->baseSubset())
|
2011-03-08 10:21:54 -08:00
|
|
|
::js_free(constraintList);
|
2011-03-06 08:15:44 -08:00
|
|
|
constraintList = next;
|
|
|
|
}
|
2011-03-03 14:07:48 -08:00
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
inline bool
|
|
|
|
TypeSet::hasType(jstype type)
|
|
|
|
{
|
2010-12-21 07:32:21 -08:00
|
|
|
if (unknown())
|
2010-10-29 08:05:55 -07:00
|
|
|
return true;
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
if (type == TYPE_UNKNOWN) {
|
|
|
|
return false;
|
|
|
|
} else if (TypeIsPrimitive(type)) {
|
2010-10-29 08:05:55 -07:00
|
|
|
return ((1 << type) & typeFlags) != 0;
|
2010-11-24 17:41:52 -08:00
|
|
|
} else {
|
|
|
|
return HashSetLookup<TypeObject*,TypeObject,TypeObjectKey>
|
|
|
|
(objectSet, objectCount, (TypeObject *) type) != NULL;
|
|
|
|
}
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 11:04:36 -08:00
|
|
|
inline void
|
|
|
|
TypeSet::markUnknown(JSContext *cx)
|
|
|
|
{
|
|
|
|
typeFlags = TYPE_FLAG_UNKNOWN | (typeFlags & TYPE_FLAG_INTERMEDIATE_SET);
|
|
|
|
if (objectCount >= 2 && !(typeFlags & TYPE_FLAG_INTERMEDIATE_SET))
|
|
|
|
cx->free(objectSet);
|
|
|
|
objectCount = 0;
|
|
|
|
objectSet = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
inline void
|
|
|
|
TypeSet::addType(JSContext *cx, jstype type)
|
|
|
|
{
|
|
|
|
JS_ASSERT(type);
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(cx->compartment->types.inferenceDepth);
|
2011-03-01 13:10:05 -08:00
|
|
|
InferSpew(ISpewOps, "addType: T%p %s", this, TypeString(type));
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-12-21 07:32:21 -08:00
|
|
|
if (unknown())
|
2010-10-29 08:05:55 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (type == TYPE_UNKNOWN) {
|
2011-03-09 11:04:36 -08:00
|
|
|
markUnknown(cx);
|
2010-10-29 08:05:55 -07:00
|
|
|
} else if (TypeIsPrimitive(type)) {
|
|
|
|
TypeFlags flag = 1 << type;
|
|
|
|
if (typeFlags & flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we add float to a type set it is also considered to contain int. */
|
|
|
|
if (flag == TYPE_FLAG_DOUBLE)
|
|
|
|
flag |= TYPE_FLAG_INT32;
|
|
|
|
|
|
|
|
typeFlags |= flag;
|
|
|
|
} else {
|
|
|
|
TypeObject *object = (TypeObject*) type;
|
2011-03-03 14:07:48 -08:00
|
|
|
TypeObject **pentry = HashSetInsert<TypeObject *,TypeObject,TypeObjectKey>
|
|
|
|
(cx, objectSet, objectCount, object,
|
|
|
|
typeFlags & TYPE_FLAG_INTERMEDIATE_SET);
|
|
|
|
if (!pentry || *pentry)
|
2010-10-29 08:05:55 -07:00
|
|
|
return;
|
2011-03-03 14:07:48 -08:00
|
|
|
*pentry = object;
|
2011-03-09 11:04:36 -08:00
|
|
|
|
|
|
|
object->contribution += objectCount;
|
|
|
|
if (object->contribution >= TypeObject::CONTRIBUTION_LIMIT) {
|
|
|
|
InferSpew(ISpewOps, "limitUnknown: T%p", this);
|
|
|
|
type = TYPE_UNKNOWN;
|
|
|
|
markUnknown(cx);
|
|
|
|
}
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Propagate the type to all constraints. */
|
|
|
|
TypeConstraint *constraint = constraintList;
|
|
|
|
while (constraint) {
|
|
|
|
cx->compartment->types.addPending(cx, constraint, this, type);
|
|
|
|
constraint = constraint->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
cx->compartment->types.resolvePending(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TypeSet *
|
2011-03-03 14:07:48 -08:00
|
|
|
TypeSet::make(JSContext *cx, const char *name)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(cx->compartment->types.inferenceDepth);
|
|
|
|
|
|
|
|
TypeSet *res = ArenaNew<TypeSet>(cx->compartment->types.pool);
|
|
|
|
if (!res) {
|
|
|
|
cx->compartment->types.setPendingNukeTypes(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
InferSpew(ISpewOps, "intermediate %s T%p", name, res);
|
2011-03-03 14:07:48 -08:00
|
|
|
res->setIntermediate();
|
2010-10-29 08:05:55 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// TypeCallsite
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline
|
2011-03-03 14:07:48 -08:00
|
|
|
TypeCallsite::TypeCallsite(JSContext *cx, JSScript *script, const jsbytecode *pc,
|
2010-12-28 11:53:50 -08:00
|
|
|
bool isNew, unsigned argumentCount)
|
|
|
|
: script(script), pc(pc), isNew(isNew), argumentCount(argumentCount),
|
2010-10-29 08:05:55 -07:00
|
|
|
thisTypes(NULL), thisType(0), returnTypes(NULL)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
/* Caller must check for failure. */
|
|
|
|
argumentTypes = ArenaArray<TypeSet*>(cx->compartment->types.pool, argumentCount);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
inline bool
|
2010-10-29 08:05:55 -07:00
|
|
|
TypeCallsite::forceThisTypes(JSContext *cx)
|
|
|
|
{
|
|
|
|
if (thisTypes)
|
2011-03-03 14:07:48 -08:00
|
|
|
return true;
|
|
|
|
thisTypes = TypeSet::make(cx, "site_this");
|
|
|
|
if (thisTypes)
|
|
|
|
thisTypes->addType(cx, thisType);
|
|
|
|
return thisTypes != NULL;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline TypeObject *
|
|
|
|
TypeCallsite::getInitObject(JSContext *cx, bool isArray)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
TypeObject *type = script->getTypeInitObject(cx, pc, isArray);
|
|
|
|
if (!type)
|
|
|
|
cx->compartment->types.setPendingNukeTypes(cx);
|
|
|
|
return type;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2010-12-20 09:06:43 -08:00
|
|
|
inline bool
|
|
|
|
TypeCallsite::compileAndGo()
|
|
|
|
{
|
2010-12-28 11:53:50 -08:00
|
|
|
return script->compileAndGo;
|
2010-12-20 09:06:43 -08:00
|
|
|
}
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
2010-11-24 17:41:52 -08:00
|
|
|
// TypeObject
|
2010-10-29 08:05:55 -07:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline TypeSet *
|
2010-11-24 17:41:52 -08:00
|
|
|
TypeObject::getProperty(JSContext *cx, jsid id, bool assign)
|
2010-10-29 08:05:55 -07:00
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(cx->compartment->types.inferenceDepth);
|
2010-12-18 20:44:51 -08:00
|
|
|
JS_ASSERT(JSID_IS_VOID(id) || JSID_IS_EMPTY(id) || JSID_IS_STRING(id));
|
2010-10-29 08:05:55 -07:00
|
|
|
JS_ASSERT_IF(JSID_IS_STRING(id), JSID_TO_STRING(id) != NULL);
|
2011-03-09 09:58:49 -08:00
|
|
|
JS_ASSERT(!unknownProperties);
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
Property **pprop = HashSetInsert<jsid,Property,Property>
|
|
|
|
(cx, propertySet, propertyCount, id, false);
|
|
|
|
if (!pprop || (!*pprop && !addProperty(cx, id, pprop)))
|
|
|
|
return NULL;
|
2010-11-20 15:45:52 -08:00
|
|
|
|
2011-03-03 14:07:48 -08:00
|
|
|
return assign ? &(*pprop)->ownTypes : &(*pprop)->types;
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2010-12-28 11:53:50 -08:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// TypeScript
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2010-12-28 11:53:50 -08:00
|
|
|
inline bool
|
|
|
|
TypeScript::monitored(uint32 offset)
|
|
|
|
{
|
|
|
|
JS_ASSERT(offset < script->length);
|
|
|
|
return 0x1 & (size_t) pushedArray[offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
TypeScript::setMonitored(uint32 offset)
|
|
|
|
{
|
2011-03-03 14:07:48 -08:00
|
|
|
JS_ASSERT(script->compartment->types.inferenceDepth);
|
2010-12-28 11:53:50 -08:00
|
|
|
JS_ASSERT(offset < script->length);
|
|
|
|
pushedArray[offset] = (TypeSet *) (0x1 | (size_t) pushedArray[offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TypeSet *
|
|
|
|
TypeScript::pushed(uint32 offset)
|
|
|
|
{
|
|
|
|
JS_ASSERT(offset < script->length);
|
|
|
|
return (TypeSet *) (~0x1 & (size_t) pushedArray[offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TypeSet *
|
|
|
|
TypeScript::pushed(uint32 offset, uint32 index)
|
|
|
|
{
|
|
|
|
JS_ASSERT(offset < script->length);
|
|
|
|
JS_ASSERT(index < js::analyze::GetDefCount(script, offset));
|
|
|
|
return pushed(offset) + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
TypeScript::addType(JSContext *cx, uint32 offset, uint32 index, jstype type)
|
|
|
|
{
|
|
|
|
TypeSet *types = pushed(offset, index);
|
|
|
|
types->addType(cx, type);
|
2010-10-29 08:05:55 -07:00
|
|
|
}
|
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
inline const char *
|
|
|
|
TypeObject::name()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
return TypeIdString(name_);
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
inline TypeObject::TypeObject(jsid name, JSObject *proto)
|
2010-12-18 20:44:51 -08:00
|
|
|
: proto(proto), emptyShapes(NULL), isFunction(false), marked(false),
|
2010-12-28 11:53:50 -08:00
|
|
|
initializerObject(false), initializerArray(false), initializerOffset(0),
|
2011-03-09 11:04:36 -08:00
|
|
|
contribution(0), propertySet(NULL), propertyCount(0),
|
2011-03-01 13:10:05 -08:00
|
|
|
instanceList(NULL), instanceNext(NULL), next(NULL), unknownProperties(false),
|
2010-12-22 17:02:24 -08:00
|
|
|
isDenseArray(false), isPackedArray(false)
|
2010-12-18 20:44:51 -08:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
this->name_ = name;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
InferSpew(ISpewOps, "newObject: %s", this->name());
|
|
|
|
|
|
|
|
if (proto) {
|
|
|
|
TypeObject *prototype = proto->getType();
|
2010-12-21 07:32:21 -08:00
|
|
|
if (prototype->unknownProperties) {
|
2010-12-18 20:44:51 -08:00
|
|
|
unknownProperties = true;
|
2010-12-21 07:32:21 -08:00
|
|
|
} else if (proto->isArray()) {
|
|
|
|
/*
|
|
|
|
* Note: this check is insufficient for determining whether new objects
|
|
|
|
* are dense arrays, as they may not themselves be arrays but simply
|
|
|
|
* have an array or Array.prototype as their prototype. We can't use
|
|
|
|
* a clasp here as type does not determine the clasp of an object, so we
|
|
|
|
* intercept at the places where a non-Array can have an Array as its
|
2010-12-21 18:26:09 -08:00
|
|
|
* prototype --- scripted 'new', reassignments to __proto__, particular
|
|
|
|
* natives and through the API.
|
2010-12-21 07:32:21 -08:00
|
|
|
*/
|
2010-12-18 20:44:51 -08:00
|
|
|
isDenseArray = isPackedArray = true;
|
2010-12-21 07:32:21 -08:00
|
|
|
}
|
2010-12-18 20:44:51 -08:00
|
|
|
instanceNext = prototype->instanceList;
|
|
|
|
prototype->instanceList = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 13:10:05 -08:00
|
|
|
inline TypeFunction::TypeFunction(jsid name, JSObject *proto)
|
|
|
|
: TypeObject(name, proto), handler(NULL), script(NULL), isGeneric(false)
|
2010-12-18 20:44:51 -08:00
|
|
|
{
|
|
|
|
isFunction = true;
|
|
|
|
}
|
|
|
|
|
2011-03-12 21:59:46 -08:00
|
|
|
inline void
|
|
|
|
SweepType(jstype *ptype)
|
|
|
|
{
|
|
|
|
if (TypeIsObject(*ptype) && !((TypeObject*)*ptype)->marked)
|
|
|
|
*ptype = TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2010-12-18 20:44:51 -08:00
|
|
|
} } /* namespace js::types */
|
|
|
|
|
2010-10-29 08:05:55 -07:00
|
|
|
#endif // jsinferinlines_h___
|