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
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JS atom table.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-08-04 19:21:25 -07:00
|
|
|
|
|
|
|
#include "mozilla/RangedPtr.h"
|
2011-10-10 23:00:28 -07:00
|
|
|
#include "mozilla/Util.h"
|
2011-08-04 19:21:25 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jstypes.h"
|
2010-10-01 16:46:54 -07:00
|
|
|
#include "jsutil.h"
|
2012-07-18 19:33:41 -07:00
|
|
|
#include "jshash.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsstr.h"
|
2009-04-05 21:17:22 -07:00
|
|
|
#include "jsversion.h"
|
2010-07-14 23:19:36 -07:00
|
|
|
#include "jsxml.h"
|
|
|
|
|
2011-10-17 14:54:28 -07:00
|
|
|
#include "frontend/Parser.h"
|
2012-05-01 14:30:18 -07:00
|
|
|
#include "gc/Marking.h"
|
2011-10-17 14:54:28 -07:00
|
|
|
|
2009-08-28 16:30:41 -07:00
|
|
|
#include "jsstrinlines.h"
|
2010-07-14 23:19:36 -07:00
|
|
|
#include "jsatominlines.h"
|
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
2011-06-20 11:44:20 -07:00
|
|
|
#include "vm/String-inl.h"
|
2012-02-20 02:58:00 -08:00
|
|
|
#include "vm/Xdr.h"
|
2011-06-20 11:44:20 -07:00
|
|
|
|
2011-10-10 23:00:28 -07:00
|
|
|
using namespace mozilla;
|
2010-07-14 23:19:36 -07:00
|
|
|
using namespace js;
|
2010-09-24 10:54:39 -07:00
|
|
|
using namespace js::gc;
|
2010-04-10 21:15:35 -07:00
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
const size_t JSAtomState::commonAtomsOffset = offsetof(JSAtomState, emptyAtom);
|
|
|
|
|
2012-07-18 19:33:41 -07:00
|
|
|
/*
|
|
|
|
* ATOM_HASH assumes that JSHashNumber is 32-bit even on 64-bit systems.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSHashNumber) == 4);
|
|
|
|
JS_STATIC_ASSERT(sizeof(JSAtom *) == JS_BYTES_PER_WORD);
|
|
|
|
|
2008-01-19 13:15:08 -08:00
|
|
|
const char *
|
2010-11-11 12:40:29 -08:00
|
|
|
js_AtomToPrintableString(JSContext *cx, JSAtom *atom, JSAutoByteString *bytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-14 13:59:53 -07:00
|
|
|
return js_ValueToPrintable(cx, StringValue(atom), bytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define JS_PROTO(name,code,init) const char js_##name##_str[] = #name;
|
|
|
|
#include "jsproto.tbl"
|
|
|
|
#undef JS_PROTO
|
|
|
|
|
2007-08-07 00:29:32 -07:00
|
|
|
/*
|
2008-10-29 00:14:30 -07:00
|
|
|
* String constants for common atoms defined in JSAtomState starting from
|
2007-08-07 00:29:32 -07:00
|
|
|
* JSAtomState.emptyAtom until JSAtomState.lazy.
|
|
|
|
*
|
|
|
|
* The elements of the array after the first empty string define strings
|
2008-10-29 00:14:30 -07:00
|
|
|
* corresponding to the two boolean literals, false and true, followed by the
|
|
|
|
* JSType enumerators from jspubtd.h starting with "undefined" for JSTYPE_VOID
|
2009-08-12 16:39:23 -07:00
|
|
|
* (which is special-value 2) and continuing as initialized below. The static
|
2008-10-29 00:14:30 -07:00
|
|
|
* asserts check these relations.
|
2007-08-07 00:29:32 -07:00
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(JSTYPE_LIMIT == 8);
|
2008-10-29 00:14:30 -07:00
|
|
|
JS_STATIC_ASSERT(JSTYPE_VOID == 0);
|
|
|
|
|
2007-08-07 00:29:32 -07:00
|
|
|
const char *const js_common_atom_names[] = {
|
|
|
|
"", /* emptyAtom */
|
2008-10-29 00:14:30 -07:00
|
|
|
js_false_str, /* booleanAtoms[0] */
|
|
|
|
js_true_str, /* booleanAtoms[1] */
|
2007-08-07 00:29:32 -07:00
|
|
|
js_undefined_str, /* typeAtoms[JSTYPE_VOID] */
|
|
|
|
js_object_str, /* typeAtoms[JSTYPE_OBJECT] */
|
|
|
|
js_function_str, /* typeAtoms[JSTYPE_FUNCTION] */
|
|
|
|
"string", /* typeAtoms[JSTYPE_STRING] */
|
|
|
|
"number", /* typeAtoms[JSTYPE_NUMBER] */
|
|
|
|
"boolean", /* typeAtoms[JSTYPE_BOOLEAN] */
|
|
|
|
js_null_str, /* typeAtoms[JSTYPE_NULL] */
|
|
|
|
"xml", /* typeAtoms[JSTYPE_XML] */
|
2012-03-16 16:15:31 -07:00
|
|
|
js_null_str /* nullAtom */
|
2007-08-07 00:29:32 -07:00
|
|
|
|
2012-03-16 16:15:31 -07:00
|
|
|
#define JS_PROTO(name,code,init) ,js_##name##_str
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsproto.tbl"
|
|
|
|
#undef JS_PROTO
|
2007-08-07 00:29:32 -07:00
|
|
|
|
2012-03-16 16:15:31 -07:00
|
|
|
#define DEFINE_ATOM(id, text) ,js_##id##_str
|
|
|
|
#define DEFINE_PROTOTYPE_ATOM(id) ,js_##id##_str
|
|
|
|
#define DEFINE_KEYWORD_ATOM(id) ,js_##id##_str
|
|
|
|
#include "jsatom.tbl"
|
|
|
|
#undef DEFINE_ATOM
|
|
|
|
#undef DEFINE_PROTOTYPE_ATOM
|
|
|
|
#undef DEFINE_KEYWORD_ATOM
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
2008-11-13 00:30:20 -08:00
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
void
|
|
|
|
JSAtomState::checkStaticInvariants()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Start and limit offsets for atom pointers in JSAtomState must be aligned
|
|
|
|
* on the word boundary.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(commonAtomsOffset % sizeof(JSAtom *) == 0);
|
|
|
|
JS_STATIC_ASSERT(sizeof(*this) % sizeof(JSAtom *) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS_BOOLEAN_STR and JS_TYPE_STR assume that boolean names starts from the
|
|
|
|
* index 1 and type name starts from the index 1+2 atoms in JSAtomState.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(1 * sizeof(JSAtom *) ==
|
|
|
|
offsetof(JSAtomState, booleanAtoms) - commonAtomsOffset);
|
|
|
|
JS_STATIC_ASSERT((1 + 2) * sizeof(JSAtom *) ==
|
|
|
|
offsetof(JSAtomState, typeAtoms) - commonAtomsOffset);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-13 00:30:20 -08:00
|
|
|
/*
|
|
|
|
* Interpreter macros called by the trace recorder assume common atom indexes
|
|
|
|
* fit in one byte of immediate operand.
|
|
|
|
*/
|
|
|
|
JS_STATIC_ASSERT(JS_ARRAY_LENGTH(js_common_atom_names) < 256);
|
|
|
|
|
|
|
|
const size_t js_common_atom_count = JS_ARRAY_LENGTH(js_common_atom_names);
|
|
|
|
|
2012-03-16 16:15:31 -07:00
|
|
|
const char js_undefined_str[] = "undefined";
|
|
|
|
const char js_object_str[] = "object";
|
|
|
|
|
2012-03-16 16:15:31 -07:00
|
|
|
#define DEFINE_ATOM(id, text) const char js_##id##_str[] = text;
|
|
|
|
#define DEFINE_PROTOTYPE_ATOM(id)
|
|
|
|
#define DEFINE_KEYWORD_ATOM(id)
|
|
|
|
#include "jsatom.tbl"
|
|
|
|
#undef DEFINE_ATOM
|
|
|
|
#undef DEFINE_PROTOTYPE_ATOM
|
|
|
|
#undef DEFINE_KEYWORD_ATOM
|
2012-03-16 16:15:31 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
const char js_close_str[] = "close";
|
|
|
|
const char js_send_str[] = "send";
|
|
|
|
#endif
|
|
|
|
|
2012-03-16 16:15:31 -07:00
|
|
|
/* Constant strings that are not atomized. */
|
|
|
|
const char js_getter_str[] = "getter";
|
|
|
|
const char js_setter_str[] = "setter";
|
|
|
|
|
2007-08-11 13:25:16 -07:00
|
|
|
/*
|
|
|
|
* For a browser build from 2007-08-09 after the browser starts up there are
|
|
|
|
* just 55 double atoms, but over 15000 string atoms. Not to penalize more
|
|
|
|
* economical embeddings allocating too much memory initially we initialize
|
|
|
|
* atomized strings with just 1K entries.
|
|
|
|
*/
|
|
|
|
#define JS_STRING_HASH_COUNT 1024
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JSBool
|
2007-08-07 00:29:32 -07:00
|
|
|
js_InitAtomState(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-07 00:29:32 -07:00
|
|
|
JSAtomState *state = &rt->atomState;
|
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
JS_ASSERT(!state->atoms.initialized());
|
|
|
|
if (!state->atoms.init(JS_STRING_HASH_COUNT))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
JS_ASSERT(state->atoms.initialized());
|
2011-07-18 14:54:48 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-07 00:29:32 -07:00
|
|
|
js_FinishAtomState(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-07 00:29:32 -07:00
|
|
|
JSAtomState *state = &rt->atomState;
|
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
if (!state->atoms.initialized()) {
|
2007-08-07 00:29:32 -07:00
|
|
|
/*
|
2007-08-11 13:25:16 -07:00
|
|
|
* We are called with uninitialized state when JS_NewRuntime fails and
|
|
|
|
* calls JS_DestroyRuntime on a partially initialized runtime.
|
2007-08-07 00:29:32 -07:00
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-20 03:22:05 -07:00
|
|
|
FreeOp fop(rt, false, false);
|
2011-03-31 16:05:31 -07:00
|
|
|
for (AtomSet::Range r = state->atoms.all(); !r.empty(); r.popFront())
|
2012-03-19 07:34:55 -07:00
|
|
|
r.front().asPtr()->finalize(&fop);
|
2007-08-07 00:29:32 -07:00
|
|
|
}
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
bool
|
2012-04-02 18:29:11 -07:00
|
|
|
js::InitCommonAtoms(JSContext *cx)
|
2007-08-07 00:29:32 -07:00
|
|
|
{
|
|
|
|
JSAtomState *state = &cx->runtime->atomState;
|
2011-05-17 12:15:12 -07:00
|
|
|
JSAtom **atoms = state->commonAtomsStart();
|
2011-10-10 23:00:28 -07:00
|
|
|
for (size_t i = 0; i < ArrayLength(js_common_atom_names); i++, atoms++) {
|
2011-09-15 11:44:10 -07:00
|
|
|
JSAtom *atom = js_Atomize(cx, js_common_atom_names[i], strlen(js_common_atom_names[i]),
|
|
|
|
InternAtom);
|
|
|
|
if (!atom)
|
2011-05-17 12:15:12 -07:00
|
|
|
return false;
|
2011-09-15 11:44:10 -07:00
|
|
|
*atoms = atom->asPropertyName();
|
2007-08-07 00:29:32 -07:00
|
|
|
}
|
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
cx->runtime->emptyString = state->emptyAtom;
|
2011-05-17 12:15:12 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-02 18:29:11 -07:00
|
|
|
js::FinishCommonAtoms(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-02 18:29:11 -07:00
|
|
|
rt->emptyString = NULL;
|
|
|
|
rt->atomState.junkAtoms();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-02 18:29:11 -07:00
|
|
|
js::MarkAtomState(JSTracer *trc, bool markAll)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-25 16:07:42 -07:00
|
|
|
JSRuntime *rt = trc->runtime;
|
2009-08-21 17:28:05 -07:00
|
|
|
JSAtomState *state = &rt->atomState;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-02 18:29:11 -07:00
|
|
|
if (markAll) {
|
2012-02-14 15:19:55 -08:00
|
|
|
for (AtomSet::Range r = state->atoms.all(); !r.empty(); r.popFront()) {
|
|
|
|
JSAtom *tmp = r.front().asPtr();
|
|
|
|
MarkStringRoot(trc, &tmp, "locked_atom");
|
|
|
|
JS_ASSERT(tmp == r.front().asPtr());
|
|
|
|
}
|
2010-07-28 09:54:14 -07:00
|
|
|
} else {
|
|
|
|
for (AtomSet::Range r = state->atoms.all(); !r.empty(); r.popFront()) {
|
2011-05-17 12:15:12 -07:00
|
|
|
AtomStateEntry entry = r.front();
|
2011-07-19 16:37:11 -07:00
|
|
|
if (!entry.isTagged())
|
2011-05-17 12:15:12 -07:00
|
|
|
continue;
|
|
|
|
|
2012-02-14 15:19:55 -08:00
|
|
|
JSAtom *tmp = entry.asPtr();
|
|
|
|
MarkStringRoot(trc, &tmp, "interned_atom");
|
|
|
|
JS_ASSERT(tmp == entry.asPtr());
|
2010-07-28 09:54:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-02 18:29:11 -07:00
|
|
|
js::SweepAtomState(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-07 16:51:32 -08:00
|
|
|
JSAtomState *state = &rt->atomState;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
for (AtomSet::Enum e(state->atoms); !e.empty(); e.popFront()) {
|
2011-05-17 12:15:12 -07:00
|
|
|
AtomStateEntry entry = e.front();
|
2012-05-23 10:34:29 -07:00
|
|
|
JSAtom *atom = entry.asPtr();
|
|
|
|
bool isMarked = IsStringMarked(&atom);
|
2011-05-17 12:15:12 -07:00
|
|
|
|
2012-05-23 10:34:29 -07:00
|
|
|
/* Pinned or interned key cannot be finalized. */
|
|
|
|
JS_ASSERT_IF(entry.isTagged(), isMarked);
|
2011-09-20 14:47:14 -07:00
|
|
|
|
2012-05-23 10:34:29 -07:00
|
|
|
if (!isMarked)
|
2011-05-17 12:15:12 -07:00
|
|
|
e.removeFront();
|
2012-05-23 10:34:29 -07:00
|
|
|
else
|
|
|
|
e.rekeyFront(AtomHasher::Lookup(atom), AtomStateEntry(atom, entry.isTagged()));
|
2010-07-28 09:54:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
bool
|
|
|
|
AtomIsInterned(JSContext *cx, JSAtom *atom)
|
|
|
|
{
|
2011-09-20 14:47:14 -07:00
|
|
|
/* We treat static strings as interned because they're never collected. */
|
|
|
|
if (StaticStrings::isStatic(atom))
|
2011-05-17 12:15:12 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
AtomSet::Ptr p = cx->runtime->atomState.atoms.lookup(atom);
|
|
|
|
if (!p)
|
|
|
|
return false;
|
|
|
|
|
2011-07-19 16:37:11 -07:00
|
|
|
return p->isTagged();
|
2011-05-17 12:15:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
enum OwnCharsBehavior
|
|
|
|
{
|
|
|
|
CopyChars, /* in other words, do not take ownership */
|
|
|
|
TakeCharOwnership
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callers passing OwnChars have freshly allocated *pchars and thus this
|
2011-05-12 13:15:36 -07:00
|
|
|
* memory can be used as a new JSAtom's buffer without copying. When this flag
|
|
|
|
* is set, the contract is that callers will free *pchars iff *pchars == NULL.
|
2011-03-14 13:59:53 -07:00
|
|
|
*/
|
2011-06-03 15:36:07 -07:00
|
|
|
JS_ALWAYS_INLINE
|
2011-03-14 13:59:53 -07:00
|
|
|
static JSAtom *
|
2011-06-03 15:36:07 -07:00
|
|
|
AtomizeInline(JSContext *cx, const jschar **pchars, size_t length,
|
|
|
|
InternBehavior ib, OwnCharsBehavior ocb = CopyChars)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-05-12 13:15:36 -07:00
|
|
|
const jschar *chars = *pchars;
|
2010-12-06 10:26:58 -08:00
|
|
|
|
2011-09-20 14:47:14 -07:00
|
|
|
if (JSAtom *s = cx->runtime->staticStrings.lookup(chars, length))
|
2011-03-14 13:59:53 -07:00
|
|
|
return s;
|
2009-09-05 21:48:30 -07:00
|
|
|
|
2011-03-14 13:55:24 -07:00
|
|
|
AtomSet &atoms = cx->runtime->atomState.atoms;
|
2011-03-14 13:59:53 -07:00
|
|
|
AtomSet::AddPtr p = atoms.lookupForAdd(AtomHasher::Lookup(chars, length));
|
2010-07-28 09:54:14 -07:00
|
|
|
|
|
|
|
if (p) {
|
2011-07-19 16:37:11 -07:00
|
|
|
JSAtom *atom = p->asPtr();
|
|
|
|
p->setTagged(bool(ib));
|
2011-05-17 12:15:12 -07:00
|
|
|
return atom;
|
|
|
|
}
|
2011-03-14 13:55:24 -07:00
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
SwitchToCompartment sc(cx, cx->runtime->atomsCompartment);
|
|
|
|
|
|
|
|
JSFixedString *key;
|
|
|
|
|
2012-04-30 17:10:30 -07:00
|
|
|
SkipRoot skip(cx, &chars);
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
if (ocb == TakeCharOwnership) {
|
|
|
|
key = js_NewString(cx, const_cast<jschar *>(chars), length);
|
|
|
|
if (!key)
|
2011-03-14 13:55:24 -07:00
|
|
|
return NULL;
|
2011-05-17 12:15:12 -07:00
|
|
|
*pchars = NULL; /* Called should not free *pchars. */
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(ocb == CopyChars);
|
|
|
|
key = js_NewStringCopyN(cx, chars, length);
|
|
|
|
if (!key)
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-14 13:59:53 -07:00
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
/*
|
|
|
|
* We have to relookup the key as the last ditch GC invoked from the
|
2011-12-05 12:28:07 -08:00
|
|
|
* string allocation or OOM handling unlocks the atomsCompartment.
|
2011-05-17 12:15:12 -07:00
|
|
|
*
|
|
|
|
* N.B. this avoids recomputing the hash but still has a potential
|
|
|
|
* (# collisions * # chars) comparison cost in the case of a hash
|
|
|
|
* collision!
|
|
|
|
*/
|
|
|
|
AtomHasher::Lookup lookup(chars, length);
|
2011-07-19 16:37:11 -07:00
|
|
|
if (!atoms.relookupOrAdd(p, lookup, AtomStateEntry((JSAtom *) key, bool(ib)))) {
|
2011-05-17 12:15:12 -07:00
|
|
|
JS_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report */
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
return key->morphAtomizedStringIntoAtom();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-03 15:36:07 -07:00
|
|
|
static JSAtom *
|
|
|
|
Atomize(JSContext *cx, const jschar **pchars, size_t length,
|
|
|
|
InternBehavior ib, OwnCharsBehavior ocb = CopyChars)
|
|
|
|
{
|
|
|
|
return AtomizeInline(cx, pchars, length, ib, ocb);
|
|
|
|
}
|
|
|
|
|
2008-01-19 13:15:08 -08:00
|
|
|
JSAtom *
|
2011-05-17 12:15:12 -07:00
|
|
|
js_AtomizeString(JSContext *cx, JSString *str, InternBehavior ib)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-05-17 12:15:12 -07:00
|
|
|
if (str->isAtom()) {
|
|
|
|
JSAtom &atom = str->asAtom();
|
|
|
|
/* N.B. static atoms are effectively always interned. */
|
2011-09-20 14:47:14 -07:00
|
|
|
if (ib != InternAtom || js::StaticStrings::isStatic(&atom))
|
2011-05-17 12:15:12 -07:00
|
|
|
return &atom;
|
|
|
|
|
|
|
|
AtomSet &atoms = cx->runtime->atomState.atoms;
|
|
|
|
AtomSet::Ptr p = atoms.lookup(AtomHasher::Lookup(&atom));
|
|
|
|
JS_ASSERT(p); /* Non-static atom must exist in atom state set. */
|
2011-07-19 16:37:11 -07:00
|
|
|
JS_ASSERT(p->asPtr() == &atom);
|
2011-05-17 12:15:12 -07:00
|
|
|
JS_ASSERT(ib == InternAtom);
|
2011-07-19 16:37:11 -07:00
|
|
|
p->setTagged(bool(ib));
|
2011-05-17 12:15:12 -07:00
|
|
|
return &atom;
|
|
|
|
}
|
2011-03-14 13:59:53 -07:00
|
|
|
|
|
|
|
size_t length = str->length();
|
|
|
|
const jschar *chars = str->getChars(cx);
|
|
|
|
if (!chars)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
JS_ASSERT(length <= JSString::MAX_LENGTH);
|
2011-05-17 12:15:12 -07:00
|
|
|
return Atomize(cx, &chars, length, ib);
|
2011-03-14 13:59:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
JSAtom *
|
2011-05-26 15:50:05 -07:00
|
|
|
js_Atomize(JSContext *cx, const char *bytes, size_t length, InternBehavior ib, FlationCoding fc)
|
2011-03-14 13:59:53 -07:00
|
|
|
{
|
2010-06-02 00:23:43 -07:00
|
|
|
CHECK_REQUEST(cx);
|
|
|
|
|
2011-10-07 16:34:28 -07:00
|
|
|
if (!JSString::validateLength(cx, length))
|
2011-03-14 13:59:53 -07:00
|
|
|
return NULL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2011-05-26 15:50:05 -07:00
|
|
|
* Avoiding the malloc in InflateString on shorter strings saves us
|
2007-03-22 10:30:00 -07:00
|
|
|
* over 20,000 malloc calls on mozilla browser startup. This compares to
|
|
|
|
* only 131 calls where the string is longer than a 31 char (net) buffer.
|
|
|
|
* The vast majority of atomized strings are already in the hashtable. So
|
|
|
|
* js_AtomizeString rarely has to copy the temp string we make.
|
|
|
|
*/
|
2011-03-14 13:59:53 -07:00
|
|
|
static const unsigned ATOMIZE_BUF_MAX = 32;
|
2007-03-22 10:30:00 -07:00
|
|
|
jschar inflated[ATOMIZE_BUF_MAX];
|
|
|
|
size_t inflatedLength = ATOMIZE_BUF_MAX - 1;
|
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
const jschar *chars;
|
2011-05-17 12:15:12 -07:00
|
|
|
OwnCharsBehavior ocb = CopyChars;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length < ATOMIZE_BUF_MAX) {
|
2011-05-26 15:50:05 -07:00
|
|
|
if (fc == CESU8Encoding)
|
|
|
|
InflateUTF8StringToBuffer(cx, bytes, length, inflated, &inflatedLength, fc);
|
2011-03-21 11:06:34 -07:00
|
|
|
else
|
2011-05-26 15:50:05 -07:00
|
|
|
InflateStringToBuffer(cx, bytes, length, inflated, &inflatedLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
inflated[inflatedLength] = 0;
|
|
|
|
chars = inflated;
|
|
|
|
} else {
|
|
|
|
inflatedLength = length;
|
2011-05-26 15:50:05 -07:00
|
|
|
chars = InflateString(cx, bytes, &inflatedLength, fc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars)
|
|
|
|
return NULL;
|
2011-05-17 12:15:12 -07:00
|
|
|
ocb = TakeCharOwnership;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
JSAtom *atom = Atomize(cx, &chars, inflatedLength, ib, ocb);
|
|
|
|
if (ocb == TakeCharOwnership && chars)
|
2011-05-12 13:15:36 -07:00
|
|
|
cx->free_((void *)chars);
|
|
|
|
return atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-19 13:15:08 -08:00
|
|
|
JSAtom *
|
2011-05-17 12:15:12 -07:00
|
|
|
js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, InternBehavior ib)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-02 00:23:43 -07:00
|
|
|
CHECK_REQUEST(cx);
|
2011-01-12 15:28:58 -08:00
|
|
|
|
2011-10-07 16:34:28 -07:00
|
|
|
if (!JSString::validateLength(cx, length))
|
2011-01-12 15:28:58 -08:00
|
|
|
return NULL;
|
|
|
|
|
2011-06-03 15:36:07 -07:00
|
|
|
return AtomizeInline(cx, &chars, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSAtom *
|
|
|
|
js_GetExistingStringAtom(JSContext *cx, const jschar *chars, size_t length)
|
|
|
|
{
|
2011-09-20 14:47:14 -07:00
|
|
|
if (JSAtom *atom = cx->runtime->staticStrings.lookup(chars, length))
|
2011-03-14 13:59:53 -07:00
|
|
|
return atom;
|
2011-07-18 14:54:48 -07:00
|
|
|
if (AtomSet::Ptr p = cx->runtime->atomState.atoms.lookup(AtomHasher::Lookup(chars, length)))
|
|
|
|
return p->asPtr();
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-07 00:29:32 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpAtoms(JSContext *cx, FILE *fp)
|
|
|
|
{
|
|
|
|
JSAtomState *state = &cx->runtime->atomState;
|
|
|
|
|
2010-07-28 09:54:14 -07:00
|
|
|
fprintf(fp, "atoms table contents:\n");
|
2010-08-05 05:16:56 -07:00
|
|
|
unsigned number = 0;
|
2010-07-28 09:54:14 -07:00
|
|
|
for (AtomSet::Range r = state->atoms.all(); !r.empty(); r.popFront()) {
|
2011-05-17 12:15:12 -07:00
|
|
|
AtomStateEntry entry = r.front();
|
2010-07-28 09:54:14 -07:00
|
|
|
fprintf(fp, "%3u ", number++);
|
2011-07-19 16:37:11 -07:00
|
|
|
JSAtom *key = entry.asPtr();
|
2011-05-17 12:15:12 -07:00
|
|
|
FileEscapedString(fp, key, '"');
|
2011-07-19 16:37:11 -07:00
|
|
|
if (entry.isTagged())
|
2011-05-17 12:15:12 -07:00
|
|
|
fputs(" interned", fp);
|
2010-07-28 09:54:14 -07:00
|
|
|
putc('\n', fp);
|
|
|
|
}
|
2007-08-11 13:25:16 -07:00
|
|
|
putc('\n', fp);
|
2007-08-07 00:29:32 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-05 15:54:57 -07:00
|
|
|
namespace js {
|
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
bool
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
IndexToIdSlow(JSContext *cx, uint32_t index, jsid *idp)
|
2011-08-04 19:21:25 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(index > JSID_INT_MAX);
|
|
|
|
|
|
|
|
jschar buf[UINT32_CHAR_BUFFER_LENGTH];
|
2011-10-10 23:00:28 -07:00
|
|
|
RangedPtr<jschar> end(ArrayEnd(buf), buf, ArrayEnd(buf));
|
2011-08-04 19:21:25 -07:00
|
|
|
RangedPtr<jschar> start = BackfillIndexInCharBuffer(index, end);
|
|
|
|
|
|
|
|
JSAtom *atom = js_AtomizeChars(cx, start.get(), end - start);
|
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
*idp = JSID_FROM_BITS((size_t)atom);
|
2011-08-04 19:21:25 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
bool
|
|
|
|
InternNonIntElementId(JSContext *cx, JSObject *obj, const Value &idval,
|
|
|
|
jsid *idp, Value *vp)
|
2011-07-27 15:44:29 -07:00
|
|
|
{
|
2012-05-06 13:45:19 -07:00
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
if (idval.isObject()) {
|
|
|
|
JSObject *idobj = &idval.toObject();
|
2011-07-27 15:44:29 -07:00
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
if (obj && obj->isXML()) {
|
|
|
|
*idp = OBJECT_TO_JSID(idobj);
|
|
|
|
*vp = idval;
|
|
|
|
return true;
|
2011-07-27 15:44:29 -07:00
|
|
|
}
|
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
if (js_GetLocalNameFromFunctionQName(idobj, idp, cx)) {
|
|
|
|
*vp = IdToValue(*idp);
|
|
|
|
return true;
|
2011-07-27 15:44:29 -07:00
|
|
|
}
|
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
if (!obj && idobj->isXMLId()) {
|
|
|
|
*idp = OBJECT_TO_JSID(idobj);
|
|
|
|
*vp = idval;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
2012-05-06 13:45:19 -07:00
|
|
|
#endif
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2012-06-12 16:03:10 -07:00
|
|
|
JSAtom *atom = ToAtom(cx, idval);
|
|
|
|
if (!atom)
|
2012-05-06 13:45:19 -07:00
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
*idp = AtomToId(atom);
|
|
|
|
vp->setString(atom);
|
|
|
|
return true;
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
|
|
|
|
2012-05-06 13:45:19 -07:00
|
|
|
} /* namespace js */
|
2012-02-20 02:58:00 -08:00
|
|
|
|
|
|
|
template<XDRMode mode>
|
|
|
|
bool
|
|
|
|
js::XDRAtom(XDRState<mode> *xdr, JSAtom **atomp)
|
|
|
|
{
|
|
|
|
if (mode == XDR_ENCODE) {
|
2012-05-01 14:01:06 -07:00
|
|
|
uint32_t nchars = (*atomp)->length();
|
|
|
|
if (!xdr->codeUint32(&nchars))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
jschar *chars = const_cast<jschar *>((*atomp)->getChars(xdr->cx()));
|
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return xdr->codeChars(chars, nchars);
|
2012-02-20 02:58:00 -08:00
|
|
|
}
|
|
|
|
|
2012-05-01 14:01:06 -07:00
|
|
|
/* Avoid JSString allocation for already existing atoms. See bug 321985. */
|
2012-02-20 02:58:00 -08:00
|
|
|
uint32_t nchars;
|
|
|
|
if (!xdr->codeUint32(&nchars))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JSContext *cx = xdr->cx();
|
|
|
|
JSAtom *atom;
|
|
|
|
#if IS_LITTLE_ENDIAN
|
|
|
|
/* Directly access the little endian chars in the XDR buffer. */
|
|
|
|
const jschar *chars = reinterpret_cast<const jschar *>(xdr->buf.read(nchars * sizeof(jschar)));
|
|
|
|
atom = js_AtomizeChars(cx, chars, nchars);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* We must copy chars to a temporary buffer to convert between little and
|
|
|
|
* big endian data.
|
2012-05-19 12:56:17 -07:00
|
|
|
*/
|
2012-02-20 02:58:00 -08:00
|
|
|
jschar *chars;
|
|
|
|
jschar stackChars[256];
|
|
|
|
if (nchars <= ArrayLength(stackChars)) {
|
|
|
|
chars = stackChars;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This is very uncommon. Don't use the tempLifoAlloc arena for this as
|
|
|
|
* most allocations here will be bigger than tempLifoAlloc's default
|
|
|
|
* chunk size.
|
|
|
|
*/
|
|
|
|
chars = static_cast<jschar *>(cx->runtime->malloc_(nchars * sizeof(jschar)));
|
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ALWAYS_TRUE(xdr->codeChars(chars, nchars));
|
|
|
|
atom = js_AtomizeChars(cx, chars, nchars);
|
|
|
|
if (chars != stackChars)
|
|
|
|
Foreground::free_(chars);
|
|
|
|
#endif /* !IS_LITTLE_ENDIAN */
|
|
|
|
|
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
*atomp = atom;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template bool
|
|
|
|
js::XDRAtom(XDRState<XDR_ENCODE> *xdr, JSAtom **atomp);
|
|
|
|
|
|
|
|
template bool
|
|
|
|
js::XDRAtom(XDRState<XDR_DECODE> *xdr, JSAtom **atomp);
|
|
|
|
|