2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2013-04-16 13:47:10 -07:00
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
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.
|
|
|
|
*/
|
2013-07-23 17:34:18 -07:00
|
|
|
|
2013-06-24 21:32:05 -07:00
|
|
|
#include "jsatominlines.h"
|
2013-04-23 23:44:36 -07:00
|
|
|
|
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
|
|
|
|
2013-06-12 22:20:27 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsstr.h"
|
2013-04-23 23:44:36 -07:00
|
|
|
#include "jstypes.h"
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2012-05-01 14:30:18 -07:00
|
|
|
#include "gc/Marking.h"
|
2012-09-06 13:48:40 -07:00
|
|
|
#include "vm/Xdr.h"
|
2011-10-17 14:54:28 -07:00
|
|
|
|
2013-06-05 19:07:43 -07:00
|
|
|
#include "jscompartmentinlines.h"
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2011-06-20 11:44:20 -07:00
|
|
|
#include "vm/String-inl.h"
|
|
|
|
|
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
|
|
|
|
2012-11-06 17:35:18 -08:00
|
|
|
using mozilla::ArrayEnd;
|
|
|
|
using mozilla::ArrayLength;
|
|
|
|
using mozilla::RangedPtr;
|
|
|
|
|
2008-01-19 13:15:08 -08:00
|
|
|
const char *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomToPrintableString(ExclusiveContext *cx, JSAtom *atom, JSAutoByteString *bytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-15 07:12:40 -07:00
|
|
|
// The only uses for this method when running off the main thread are for
|
|
|
|
// parse errors/warnings, which will not actually be reported in such cases.
|
|
|
|
if (!cx->isJSContext())
|
|
|
|
return "";
|
2013-07-10 08:29:52 -07:00
|
|
|
return js_ValueToPrintable(cx->asJSContext(), StringValue(atom), bytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-17 18:31:47 -07:00
|
|
|
const char * const js::TypeStrings[] = {
|
2012-09-06 13:48:40 -07:00
|
|
|
js_undefined_str,
|
|
|
|
js_object_str,
|
|
|
|
js_function_str,
|
|
|
|
js_string_str,
|
|
|
|
js_number_str,
|
|
|
|
js_boolean_str,
|
|
|
|
js_null_str,
|
|
|
|
};
|
|
|
|
|
2012-09-07 14:42:33 -07:00
|
|
|
#define DEFINE_PROTO_STRING(name,code,init) const char js_##name##_str[] = #name;
|
|
|
|
JS_FOR_EACH_PROTOTYPE(DEFINE_PROTO_STRING)
|
|
|
|
#undef DEFINE_PROTO_STRING
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-10 13:27:19 -07:00
|
|
|
#define CONST_CHAR_STR(idpart, id, text) const char js_##idpart##_str[] = text;
|
2012-09-06 13:48:40 -07:00
|
|
|
FOR_EACH_COMMON_PROPERTYNAME(CONST_CHAR_STR)
|
|
|
|
#undef CONST_CHAR_STR
|
2012-03-16 16:15:31 -07:00
|
|
|
|
2012-09-06 13:48:40 -07:00
|
|
|
/* Constant strings that are not atomized. */
|
|
|
|
const char js_break_str[] = "break";
|
|
|
|
const char js_case_str[] = "case";
|
|
|
|
const char js_catch_str[] = "catch";
|
|
|
|
const char js_class_str[] = "class";
|
2013-06-26 03:34:47 -07:00
|
|
|
const char js_close_str[] = "close";
|
2012-09-06 13:48:40 -07:00
|
|
|
const char js_const_str[] = "const";
|
|
|
|
const char js_continue_str[] = "continue";
|
|
|
|
const char js_debugger_str[] = "debugger";
|
|
|
|
const char js_default_str[] = "default";
|
|
|
|
const char js_do_str[] = "do";
|
|
|
|
const char js_else_str[] = "else";
|
|
|
|
const char js_enum_str[] = "enum";
|
|
|
|
const char js_export_str[] = "export";
|
|
|
|
const char js_extends_str[] = "extends";
|
|
|
|
const char js_finally_str[] = "finally";
|
|
|
|
const char js_for_str[] = "for";
|
|
|
|
const char js_getter_str[] = "getter";
|
|
|
|
const char js_if_str[] = "if";
|
|
|
|
const char js_implements_str[] = "implements";
|
|
|
|
const char js_import_str[] = "import";
|
|
|
|
const char js_in_str[] = "in";
|
|
|
|
const char js_instanceof_str[] = "instanceof";
|
|
|
|
const char js_interface_str[] = "interface";
|
|
|
|
const char js_let_str[] = "let";
|
|
|
|
const char js_new_str[] = "new";
|
|
|
|
const char js_package_str[] = "package";
|
|
|
|
const char js_private_str[] = "private";
|
|
|
|
const char js_protected_str[] = "protected";
|
|
|
|
const char js_public_str[] = "public";
|
2013-06-26 03:34:47 -07:00
|
|
|
const char js_send_str[] = "send";
|
2012-09-06 13:48:40 -07:00
|
|
|
const char js_setter_str[] = "setter";
|
|
|
|
const char js_static_str[] = "static";
|
|
|
|
const char js_super_str[] = "super";
|
|
|
|
const char js_switch_str[] = "switch";
|
|
|
|
const char js_this_str[] = "this";
|
|
|
|
const char js_try_str[] = "try";
|
|
|
|
const char js_typeof_str[] = "typeof";
|
|
|
|
const char js_void_str[] = "void";
|
|
|
|
const char js_while_str[] = "while";
|
|
|
|
const char js_with_str[] = "with";
|
|
|
|
const char js_yield_str[] = "yield";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2012-09-06 13:48:40 -07:00
|
|
|
js::InitAtoms(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-06 13:48:40 -07:00
|
|
|
return rt->atoms.init(JS_STRING_HASH_COUNT);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-06 13:48:40 -07:00
|
|
|
js::FinishAtoms(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-06 13:48:40 -07:00
|
|
|
AtomSet &atoms = rt->atoms;
|
|
|
|
if (!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-08-21 01:58:30 -07:00
|
|
|
FreeOp fop(rt, false);
|
2012-09-06 13:48:40 -07:00
|
|
|
for (AtomSet::Range r = 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
|
|
|
}
|
|
|
|
|
2012-09-06 13:48:40 -07:00
|
|
|
struct CommonNameInfo
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
bool
|
2012-09-06 13:48:40 -07:00
|
|
|
js::InitCommonNames(JSContext *cx)
|
2007-08-07 00:29:32 -07:00
|
|
|
{
|
2012-09-06 13:48:40 -07:00
|
|
|
static const CommonNameInfo cachedNames[] = {
|
2012-09-10 13:27:19 -07:00
|
|
|
#define COMMON_NAME_INFO(idpart, id, text) { js_##idpart##_str, sizeof(text) - 1 },
|
2012-09-06 13:48:40 -07:00
|
|
|
FOR_EACH_COMMON_PROPERTYNAME(COMMON_NAME_INFO)
|
|
|
|
#undef COMMON_NAME_INFO
|
|
|
|
#define COMMON_NAME_INFO(name, code, init) { js_##name##_str, sizeof(#name) - 1 },
|
|
|
|
JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INFO)
|
|
|
|
#undef COMMON_NAME_INFO
|
|
|
|
};
|
|
|
|
|
2013-06-10 14:22:18 -07:00
|
|
|
FixedHeapPtr<PropertyName> *names = &cx->runtime()->firstCachedName;
|
2012-09-06 13:48:40 -07:00
|
|
|
for (size_t i = 0; i < ArrayLength(cachedNames); i++, names++) {
|
|
|
|
JSAtom *atom = Atomize(cx, cachedNames[i].str, cachedNames[i].length, InternAtom);
|
2011-09-15 11:44:10 -07:00
|
|
|
if (!atom)
|
2011-05-17 12:15:12 -07:00
|
|
|
return false;
|
2012-09-10 20:42:08 -07:00
|
|
|
names->init(atom->asPropertyName());
|
2007-08-07 00:29:32 -07:00
|
|
|
}
|
2013-06-10 14:22:18 -07:00
|
|
|
JS_ASSERT(uintptr_t(names) == uintptr_t(&cx->runtime()->atomState + 1));
|
2007-08-07 00:29:32 -07:00
|
|
|
|
2013-06-10 14:22:18 -07:00
|
|
|
cx->runtime()->emptyString = cx->names().empty;
|
2011-05-17 12:15:12 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-06 13:48:40 -07:00
|
|
|
js::FinishCommonNames(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-02 18:29:11 -07:00
|
|
|
rt->emptyString = NULL;
|
2012-09-06 13:48:40 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
memset(&rt->atomState, JS_FREE_PATTERN, sizeof(JSAtomState));
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-06 13:48:40 -07:00
|
|
|
js::MarkAtoms(JSTracer *trc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-25 16:07:42 -07:00
|
|
|
JSRuntime *rt = trc->runtime;
|
2012-09-06 13:48:40 -07:00
|
|
|
for (AtomSet::Range r = rt->atoms.all(); !r.empty(); r.popFront()) {
|
2012-08-14 03:10:45 -07:00
|
|
|
AtomStateEntry entry = r.front();
|
|
|
|
if (!entry.isTagged())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
JSAtom *tmp = entry.asPtr();
|
|
|
|
MarkStringRoot(trc, &tmp, "interned_atom");
|
|
|
|
JS_ASSERT(tmp == entry.asPtr());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-06 13:48:40 -07:00
|
|
|
js::SweepAtoms(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-06 13:48:40 -07:00
|
|
|
for (AtomSet::Enum e(rt->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();
|
2012-11-16 07:34:22 -08:00
|
|
|
bool isDying = IsStringAboutToBeFinalized(&atom);
|
2011-05-17 12:15:12 -07:00
|
|
|
|
2012-05-23 10:34:29 -07:00
|
|
|
/* Pinned or interned key cannot be finalized. */
|
2012-11-16 07:34:22 -08:00
|
|
|
JS_ASSERT_IF(entry.isTagged(), !isDying);
|
2011-09-20 14:47:14 -07:00
|
|
|
|
2012-11-16 07:34:22 -08:00
|
|
|
if (isDying)
|
2011-05-17 12:15:12 -07:00
|
|
|
e.removeFront();
|
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;
|
|
|
|
|
2013-06-10 14:22:18 -07:00
|
|
|
AtomSet::Ptr p = cx->runtime()->atoms.lookup(atom);
|
2011-05-17 12:15:12 -07:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-01-07 15:32:01 -08:00
|
|
|
* When the jschars reside in a freshly allocated buffer the memory can be used
|
|
|
|
* as a new JSAtom's storage without copying. The contract is that the caller no
|
|
|
|
* longer owns the memory and this method is responsible for freeing the memory.
|
2011-03-14 13:59:53 -07:00
|
|
|
*/
|
2011-06-03 15:36:07 -07:00
|
|
|
JS_ALWAYS_INLINE
|
2013-04-30 15:38:45 -07:00
|
|
|
static JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomizeAndTakeOwnership(ExclusiveContext *cx, jschar *tbchars, size_t length, InternBehavior ib)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-01-07 15:32:01 -08:00
|
|
|
JS_ASSERT(tbchars[length] == 0);
|
2010-12-06 10:26:58 -08:00
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
if (JSAtom *s = cx->staticStrings().lookup(tbchars, length)) {
|
2013-05-05 00:03:18 -07:00
|
|
|
js_free(tbchars);
|
2011-03-14 13:59:53 -07:00
|
|
|
return s;
|
2013-01-07 15:32:01 -08:00
|
|
|
}
|
2009-09-05 21:48:30 -07:00
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2013-01-07 15:32:01 -08:00
|
|
|
/*
|
|
|
|
* If a GC occurs at js_NewStringCopy then |p| will still have the correct
|
|
|
|
* hash, allowing us to avoid rehashing it. Even though the hash is
|
|
|
|
* unchanged, we need to re-lookup the table position because a last-ditch
|
|
|
|
* GC will potentially free some table entries.
|
|
|
|
*/
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomSet& atoms = cx->atoms();
|
2013-04-30 03:18:18 -07:00
|
|
|
AtomSet::AddPtr p = atoms.lookupForAdd(AtomHasher::Lookup(tbchars, length));
|
2013-01-07 15:32:01 -08:00
|
|
|
SkipRoot skipHash(cx, &p); /* Prevent the hash from being poisoned. */
|
2010-07-28 09:54:14 -07:00
|
|
|
if (p) {
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *atom = p->asPtr();
|
2011-07-19 16:37:11 -07:00
|
|
|
p->setTagged(bool(ib));
|
2013-05-05 00:03:18 -07:00
|
|
|
js_free(tbchars);
|
2011-05-17 12:15:12 -07:00
|
|
|
return atom;
|
|
|
|
}
|
2011-03-14 13:55:24 -07:00
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoCompartment ac(cx, cx->atomsCompartment());
|
2011-05-17 12:15:12 -07:00
|
|
|
|
2013-05-05 00:03:18 -07:00
|
|
|
JSFlatString *flat = js_NewString<CanGC>(cx, tbchars, length);
|
2013-01-07 15:32:01 -08:00
|
|
|
if (!flat) {
|
2013-05-05 00:03:18 -07:00
|
|
|
js_free(tbchars);
|
2013-03-05 10:32:12 -08:00
|
|
|
return NULL;
|
2013-01-07 15:32:01 -08:00
|
|
|
}
|
2012-04-30 17:10:30 -07:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *atom = flat->morphAtomizedStringIntoAtom();
|
2012-08-12 18:50:49 -07:00
|
|
|
|
2013-04-30 03:18:18 -07:00
|
|
|
if (!atoms.relookupOrAdd(p, AtomHasher::Lookup(tbchars, length),
|
|
|
|
AtomStateEntry(atom, bool(ib)))) {
|
2013-07-10 08:29:52 -07:00
|
|
|
js_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */
|
2013-03-05 10:32:12 -08:00
|
|
|
return NULL;
|
2011-05-17 12:15:12 -07:00
|
|
|
}
|
2013-01-07 15:32:01 -08:00
|
|
|
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* |tbchars| must not point into an inline or short string. */
|
2013-01-24 19:18:34 -08:00
|
|
|
template <AllowGC allowGC>
|
2013-01-07 15:32:01 -08:00
|
|
|
JS_ALWAYS_INLINE
|
2013-04-30 15:38:45 -07:00
|
|
|
static JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomizeAndCopyChars(ExclusiveContext *cx, const jschar *tbchars, size_t length, InternBehavior ib)
|
2013-01-07 15:32:01 -08:00
|
|
|
{
|
2013-07-10 08:29:52 -07:00
|
|
|
if (JSAtom *s = cx->staticStrings().lookup(tbchars, length))
|
2013-01-07 15:32:01 -08:00
|
|
|
return s;
|
2011-03-14 13:59:53 -07:00
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
/*
|
2013-01-07 15:32:01 -08:00
|
|
|
* If a GC occurs at js_NewStringCopy then |p| will still have the correct
|
|
|
|
* hash, allowing us to avoid rehashing it. Even though the hash is
|
|
|
|
* unchanged, we need to re-lookup the table position because a last-ditch
|
|
|
|
* GC will potentially free some table entries.
|
2011-05-17 12:15:12 -07:00
|
|
|
*/
|
2013-04-30 03:18:18 -07:00
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomSet& atoms = cx->atoms();
|
2013-04-30 03:18:18 -07:00
|
|
|
AtomSet::AddPtr p = atoms.lookupForAdd(AtomHasher::Lookup(tbchars, length));
|
2013-01-07 15:32:01 -08:00
|
|
|
SkipRoot skipHash(cx, &p); /* Prevent the hash from being poisoned. */
|
|
|
|
if (p) {
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *atom = p->asPtr();
|
2013-01-07 15:32:01 -08:00
|
|
|
p->setTagged(bool(ib));
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoCompartment ac(cx, cx->atomsCompartment());
|
2013-01-07 15:32:01 -08:00
|
|
|
|
2013-04-30 15:39:03 -07:00
|
|
|
JSFlatString *flat = js_NewStringCopyN<allowGC>(cx, tbchars, length);
|
2013-01-07 15:32:01 -08:00
|
|
|
if (!flat)
|
2013-03-05 10:32:12 -08:00
|
|
|
return NULL;
|
2013-01-07 15:32:01 -08:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *atom = flat->morphAtomizedStringIntoAtom();
|
2013-01-07 15:32:01 -08:00
|
|
|
|
2013-04-30 03:18:18 -07:00
|
|
|
if (!atoms.relookupOrAdd(p, AtomHasher::Lookup(tbchars, length),
|
|
|
|
AtomStateEntry(atom, bool(ib)))) {
|
2013-07-18 13:55:39 -07:00
|
|
|
if (allowGC)
|
|
|
|
js_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */
|
2013-03-05 10:32:12 -08:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:32:01 -08:00
|
|
|
return atom;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
template <AllowGC allowGC>
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeString(ExclusiveContext *cx, JSString *str,
|
|
|
|
js::InternBehavior ib /* = js::DoNotInternAtom */)
|
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;
|
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomSet::Ptr p = cx->atoms().lookup(AtomHasher::Lookup(&atom));
|
2011-05-17 12:15:12 -07:00
|
|
|
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
|
|
|
|
2013-07-15 07:12:40 -07:00
|
|
|
const jschar *chars;
|
|
|
|
if (str->isLinear()) {
|
|
|
|
chars = str->asLinear().chars();
|
|
|
|
} else {
|
|
|
|
if (!cx->shouldBeJSContext())
|
|
|
|
return NULL;
|
|
|
|
chars = str->getChars(cx->asJSContext());
|
|
|
|
if (!chars)
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-02-05 14:22:27 -08:00
|
|
|
if (JSAtom *atom = AtomizeAndCopyChars<NoGC>(cx, chars, str->length(), ib))
|
2013-01-24 19:18:34 -08:00
|
|
|
return atom;
|
|
|
|
|
2013-07-15 07:12:40 -07:00
|
|
|
if (!cx->isJSContext() || !allowGC)
|
2013-01-24 19:18:34 -08:00
|
|
|
return NULL;
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
JSLinearString *linear = str->ensureLinear(cx->asJSContext());
|
2013-02-05 14:22:27 -08:00
|
|
|
if (!linear)
|
2011-03-14 13:59:53 -07:00
|
|
|
return NULL;
|
|
|
|
|
2013-02-05 14:22:27 -08:00
|
|
|
JS_ASSERT(linear->length() <= JSString::MAX_LENGTH);
|
|
|
|
return AtomizeAndCopyChars<CanGC>(cx, linear->chars(), linear->length(), ib);
|
2011-03-14 13:59:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeString<CanGC>(ExclusiveContext *cx, JSString *str, InternBehavior ib);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeString<NoGC>(ExclusiveContext *cx, JSString *str, InternBehavior ib);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::Atomize(ExclusiveContext *cx, const char *bytes, size_t length, InternBehavior ib)
|
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;
|
|
|
|
|
|
|
|
static const unsigned ATOMIZE_BUF_MAX = 32;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (length < ATOMIZE_BUF_MAX) {
|
2013-01-07 15:32:01 -08:00
|
|
|
/*
|
|
|
|
* Avoiding the malloc in InflateString on shorter strings saves us
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
jschar inflated[ATOMIZE_BUF_MAX];
|
|
|
|
size_t inflatedLength = ATOMIZE_BUF_MAX - 1;
|
2013-07-10 08:29:52 -07:00
|
|
|
if (!InflateStringToBuffer(cx->maybeJSContext(),
|
|
|
|
bytes, length, inflated, &inflatedLength))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-05 14:22:27 -08:00
|
|
|
return AtomizeAndCopyChars<CanGC>(cx, inflated, inflatedLength, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-11 14:04:25 -08:00
|
|
|
jschar *tbcharsZ = InflateString(cx, bytes, &length);
|
|
|
|
if (!tbcharsZ)
|
2013-03-05 10:32:12 -08:00
|
|
|
return NULL;
|
2013-02-05 14:22:27 -08:00
|
|
|
return AtomizeAndTakeOwnership(cx, tbcharsZ, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
template <AllowGC allowGC>
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeChars(ExclusiveContext *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;
|
|
|
|
|
2013-02-05 14:22:27 -08:00
|
|
|
return AtomizeAndCopyChars<allowGC>(cx, chars, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeChars<CanGC>(ExclusiveContext *cx,
|
|
|
|
const jschar *chars, size_t length, InternBehavior ib);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-04-30 15:38:45 -07:00
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::AtomizeChars<NoGC>(ExclusiveContext *cx,
|
|
|
|
const jschar *chars, size_t length, InternBehavior ib);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
|
|
|
template <AllowGC allowGC>
|
2011-08-04 19:21:25 -07:00
|
|
|
bool
|
2013-07-10 08:29:52 -07:00
|
|
|
js::IndexToIdSlow(ExclusiveContext *cx, uint32_t index,
|
2013-01-24 19:18:34 -08:00
|
|
|
typename MaybeRooted<jsid, allowGC>::MutableHandleType 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);
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
JSAtom *atom = AtomizeChars<allowGC>(cx, start.get(), end - start);
|
2011-08-04 19:21:25 -07:00
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
|
2013-01-11 00:43:00 -08:00
|
|
|
idp.set(JSID_FROM_BITS((size_t)atom));
|
2011-08-04 19:21:25 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-24 19:18:34 -08:00
|
|
|
template bool
|
2013-07-10 08:29:52 -07:00
|
|
|
js::IndexToIdSlow<CanGC>(ExclusiveContext *cx, uint32_t index, MutableHandleId idp);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
|
|
|
template bool
|
2013-07-10 08:29:52 -07:00
|
|
|
js::IndexToIdSlow<NoGC>(ExclusiveContext *cx, uint32_t index, FakeMutableHandle<jsid> idp);
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-06-20 17:59:39 -07:00
|
|
|
template <AllowGC allowGC>
|
|
|
|
JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::ToAtom(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType v)
|
2013-06-20 17:59:39 -07:00
|
|
|
{
|
|
|
|
if (!v.isString()) {
|
|
|
|
JSString *str = js::ToStringSlow<allowGC>(cx, v);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
JS::Anchor<JSString *> anchor(str);
|
|
|
|
return AtomizeString<allowGC>(cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *str = v.toString();
|
|
|
|
if (str->isAtom())
|
|
|
|
return &str->asAtom();
|
|
|
|
|
|
|
|
JS::Anchor<JSString *> anchor(str);
|
|
|
|
return AtomizeString<allowGC>(cx, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::ToAtom<CanGC>(ExclusiveContext *cx, HandleValue v);
|
2013-06-20 17:59:39 -07:00
|
|
|
|
|
|
|
template JSAtom *
|
2013-07-10 08:29:52 -07:00
|
|
|
js::ToAtom<NoGC>(ExclusiveContext *cx, Value v);
|
2013-06-20 17:59:39 -07:00
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
template<XDRMode mode>
|
|
|
|
bool
|
2012-10-04 16:22:03 -07:00
|
|
|
js::XDRAtom(XDRState<mode> *xdr, MutableHandleAtom atomp)
|
2012-02-20 02:58:00 -08:00
|
|
|
{
|
|
|
|
if (mode == XDR_ENCODE) {
|
2012-10-04 16:22:03 -07:00
|
|
|
uint32_t nchars = atomp->length();
|
2012-05-01 14:01:06 -07:00
|
|
|
if (!xdr->codeUint32(&nchars))
|
|
|
|
return false;
|
|
|
|
|
2012-10-04 16:22:03 -07:00
|
|
|
jschar *chars = const_cast<jschar *>(atomp->getChars(xdr->cx()));
|
2012-05-01 14:01:06 -07:00
|
|
|
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)));
|
2013-01-24 19:18:34 -08:00
|
|
|
atom = AtomizeChars<CanGC>(cx, chars, nchars);
|
2012-02-20 02:58:00 -08:00
|
|
|
#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.
|
|
|
|
*/
|
2013-06-10 14:22:18 -07:00
|
|
|
chars = cx->runtime()->pod_malloc<jschar>(nchars);
|
2012-02-20 02:58:00 -08:00
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ALWAYS_TRUE(xdr->codeChars(chars, nchars));
|
2013-02-12 13:29:29 -08:00
|
|
|
atom = AtomizeChars<CanGC>(cx, chars, nchars);
|
2012-02-20 02:58:00 -08:00
|
|
|
if (chars != stackChars)
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(chars);
|
2012-02-20 02:58:00 -08:00
|
|
|
#endif /* !IS_LITTLE_ENDIAN */
|
|
|
|
|
|
|
|
if (!atom)
|
|
|
|
return false;
|
2012-10-04 16:22:03 -07:00
|
|
|
atomp.set(atom);
|
2012-02-20 02:58:00 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template bool
|
2012-10-04 16:22:03 -07:00
|
|
|
js::XDRAtom(XDRState<XDR_ENCODE> *xdr, MutableHandleAtom atomp);
|
2012-02-20 02:58:00 -08:00
|
|
|
|
|
|
|
template bool
|
2012-10-04 16:22:03 -07:00
|
|
|
js::XDRAtom(XDRState<XDR_DECODE> *xdr, MutableHandleAtom atomp);
|
2012-02-20 02:58:00 -08:00
|
|
|
|