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
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-08-04 19:21:25 -07:00
|
|
|
#include "mozilla/RangedPtr.h"
|
|
|
|
|
2013-06-12 22:20:27 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#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-08-08 16:07:22 -07:00
|
|
|
#include "jscntxtinlines.h"
|
2013-06-05 19:07:43 -07:00
|
|
|
#include "jscompartmentinlines.h"
|
2013-08-27 02:17:41 -07:00
|
|
|
#include "jsobjinlines.h"
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2011-06-20 11:44:20 -07:00
|
|
|
#include "vm/String-inl.h"
|
2014-06-23 08:55:51 -07:00
|
|
|
#include "vm/Symbol-inl.h"
|
2011-06-20 11:44:20 -07:00
|
|
|
|
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-09-11 16:42:09 -07:00
|
|
|
JSString *str = js_QuoteString(cx, atom, 0);
|
|
|
|
if (!str)
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-09-11 16:42:09 -07:00
|
|
|
return bytes->encodeLatin1(cx, str);
|
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,
|
|
|
|
};
|
|
|
|
|
2013-11-22 10:55:42 -08:00
|
|
|
#define DEFINE_PROTO_STRING(name,code,init,clasp) const char js_##name##_str[] = #name;
|
2012-09-07 14:42:33 -07:00
|
|
|
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_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";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
// Use a low initial capacity for atom hash tables to avoid penalizing runtimes
|
|
|
|
// which create a small number of atoms.
|
|
|
|
static const uint32_t JS_STRING_HASH_COUNT = 64;
|
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
|
2014-02-19 08:02:13 -08:00
|
|
|
JSRuntime::initializeAtoms(JSContext *cx)
|
2007-08-07 00:29:32 -07:00
|
|
|
{
|
2014-02-19 08:02:13 -08:00
|
|
|
atoms_ = cx->new_<AtomSet>();
|
|
|
|
if (!atoms_ || !atoms_->init(JS_STRING_HASH_COUNT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (parentRuntime) {
|
|
|
|
staticStrings = parentRuntime->staticStrings;
|
|
|
|
commonNames = parentRuntime->commonNames;
|
|
|
|
emptyString = parentRuntime->emptyString;
|
|
|
|
permanentAtoms = parentRuntime->permanentAtoms;
|
2014-06-23 08:56:49 -07:00
|
|
|
wellKnownSymbols = parentRuntime->wellKnownSymbols;
|
2014-02-19 08:02:13 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
permanentAtoms = cx->new_<AtomSet>();
|
|
|
|
if (!permanentAtoms || !permanentAtoms->init(JS_STRING_HASH_COUNT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
staticStrings = cx->new_<StaticStrings>();
|
|
|
|
if (!staticStrings || !staticStrings->init(cx))
|
|
|
|
return false;
|
|
|
|
|
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
|
2013-11-22 10:55:42 -08:00
|
|
|
#define COMMON_NAME_INFO(name, code, init, clasp) { js_##name##_str, sizeof(#name) - 1 },
|
2012-09-06 13:48:40 -07:00
|
|
|
JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INFO)
|
|
|
|
#undef COMMON_NAME_INFO
|
|
|
|
};
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
commonNames = cx->new_<JSAtomState>();
|
|
|
|
if (!commonNames)
|
|
|
|
return false;
|
|
|
|
|
2014-04-25 14:22:27 -07:00
|
|
|
ImmutablePropertyNamePtr *names = reinterpret_cast<ImmutablePropertyNamePtr *>(commonNames);
|
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
|
|
|
}
|
2014-02-19 08:02:13 -08:00
|
|
|
JS_ASSERT(uintptr_t(names) == uintptr_t(commonNames + 1));
|
2007-08-07 00:29:32 -07:00
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
emptyString = commonNames->empty;
|
2014-06-23 08:56:49 -07:00
|
|
|
|
|
|
|
// Create the well-known symbols.
|
|
|
|
wellKnownSymbols = cx->new_<WellKnownSymbols>();
|
|
|
|
if (!wellKnownSymbols)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ImmutablePropertyNamePtr *descriptions = &commonNames->Symbol_iterator;
|
|
|
|
ImmutableSymbolPtr *symbols = reinterpret_cast<ImmutableSymbolPtr *>(wellKnownSymbols);
|
|
|
|
for (size_t i = 0; i < JS::WellKnownSymbolLimit; i++) {
|
|
|
|
JS::Symbol *symbol = JS::Symbol::new_(cx, JS::SymbolCode(i), descriptions[i]);
|
|
|
|
if (!symbol) {
|
|
|
|
js_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
symbols[i].init(symbol);
|
|
|
|
}
|
|
|
|
|
2011-05-17 12:15:12 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-19 08:02:13 -08:00
|
|
|
JSRuntime::finishAtoms()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-23 08:56:49 -07:00
|
|
|
js_delete(atoms_);
|
2014-02-19 08:02:13 -08:00
|
|
|
|
|
|
|
if (!parentRuntime) {
|
2014-06-23 08:56:49 -07:00
|
|
|
js_delete(staticStrings);
|
|
|
|
js_delete(commonNames);
|
|
|
|
js_delete(permanentAtoms);
|
|
|
|
js_delete(wellKnownSymbols);
|
2014-02-19 08:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
atoms_ = nullptr;
|
|
|
|
staticStrings = nullptr;
|
|
|
|
commonNames = nullptr;
|
|
|
|
permanentAtoms = nullptr;
|
2014-06-23 08:56:49 -07:00
|
|
|
wellKnownSymbols = nullptr;
|
2014-02-19 08:02:13 -08:00
|
|
|
emptyString = nullptr;
|
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
|
|
|
{
|
2014-04-11 15:19:05 -07:00
|
|
|
JSRuntime *rt = trc->runtime();
|
2013-02-15 02:42:39 -08:00
|
|
|
for (AtomSet::Enum e(rt->atoms()); !e.empty(); e.popFront()) {
|
|
|
|
const AtomStateEntry &entry = e.front();
|
2012-08-14 03:10:45 -07:00
|
|
|
if (!entry.isTagged())
|
|
|
|
continue;
|
|
|
|
|
2013-02-15 02:42:39 -08:00
|
|
|
JSAtom *atom = entry.asPtr();
|
|
|
|
bool tagged = entry.isTagged();
|
|
|
|
MarkStringRoot(trc, &atom, "interned_atom");
|
|
|
|
if (entry.asPtr() != atom)
|
|
|
|
e.rekeyFront(AtomHasher::Lookup(atom), AtomStateEntry(atom, tagged));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-19 08:02:13 -08:00
|
|
|
js::MarkPermanentAtoms(JSTracer *trc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-11 15:19:05 -07:00
|
|
|
JSRuntime *rt = trc->runtime();
|
2014-02-19 08:02:13 -08:00
|
|
|
|
|
|
|
// Permanent atoms only need to be marked in the runtime which owns them.
|
|
|
|
if (rt->parentRuntime)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Static strings are not included in the permanent atoms table.
|
|
|
|
if (rt->staticStrings)
|
|
|
|
rt->staticStrings->trace(trc);
|
|
|
|
|
|
|
|
if (rt->permanentAtoms) {
|
|
|
|
for (AtomSet::Enum e(*rt->permanentAtoms); !e.empty(); e.popFront()) {
|
|
|
|
const AtomStateEntry &entry = e.front();
|
|
|
|
|
|
|
|
JSAtom *atom = entry.asPtr();
|
|
|
|
MarkPermanentAtom(trc, atom, "permanent_table");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 08:56:49 -07:00
|
|
|
void
|
|
|
|
js::MarkWellKnownSymbols(JSTracer *trc)
|
|
|
|
{
|
|
|
|
JSRuntime *rt = trc->runtime();
|
|
|
|
|
|
|
|
if (rt->parentRuntime)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (WellKnownSymbols *wks = rt->wellKnownSymbols) {
|
|
|
|
for (size_t i = 0; i < JS::WellKnownSymbolLimit; i++)
|
|
|
|
MarkWellKnownSymbol(trc, wks->get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
void
|
|
|
|
JSRuntime::sweepAtoms()
|
|
|
|
{
|
|
|
|
if (!atoms_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (AtomSet::Enum e(*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. */
|
2014-02-19 08:02:13 -08:00
|
|
|
JS_ASSERT_IF(hasContexts() && 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
|
|
|
}
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
bool
|
|
|
|
JSRuntime::transformToPermanentAtoms()
|
|
|
|
{
|
|
|
|
JS_ASSERT(!parentRuntime);
|
|
|
|
|
|
|
|
// All static strings were created as permanent atoms, now move the contents
|
|
|
|
// of the atoms table into permanentAtoms and mark each as permanent.
|
|
|
|
|
|
|
|
JS_ASSERT(permanentAtoms && permanentAtoms->empty());
|
|
|
|
|
|
|
|
AtomSet *temp = atoms_;
|
|
|
|
atoms_ = permanentAtoms;
|
|
|
|
permanentAtoms = temp;
|
|
|
|
|
|
|
|
for (AtomSet::Enum e(*permanentAtoms); !e.empty(); e.popFront()) {
|
|
|
|
AtomStateEntry entry = e.front();
|
|
|
|
JSAtom *atom = entry.asPtr();
|
|
|
|
atom->morphIntoPermanentAtom();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomHasher::Lookup lookup(atom);
|
|
|
|
|
|
|
|
/* Likewise, permanent strings are considered to be interned. */
|
|
|
|
AtomSet::Ptr p = cx->permanentAtoms().readonlyThreadsafeLookup(lookup);
|
|
|
|
if (p)
|
|
|
|
return true;
|
|
|
|
|
2013-08-13 08:13:46 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
p = cx->runtime()->atoms().lookup(lookup);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE
|
2013-04-30 15:38:45 -07:00
|
|
|
static JSAtom *
|
2014-03-15 12:00:15 -07:00
|
|
|
AtomizeAndtake(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
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomHasher::Lookup lookup(tbchars, length);
|
|
|
|
|
|
|
|
AtomSet::Ptr pp = cx->permanentAtoms().readonlyThreadsafeLookup(lookup);
|
|
|
|
if (pp) {
|
|
|
|
js_free(tbchars);
|
|
|
|
return pp->asPtr();
|
|
|
|
}
|
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2013-01-07 15:32:01 -08:00
|
|
|
/*
|
2014-06-24 08:33:27 -07:00
|
|
|
* If a GC occurs at NewStringCopy then |p| will still have the correct
|
2013-01-07 15:32:01 -08:00
|
|
|
* 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();
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomSet::AddPtr p = atoms.lookupForAdd(lookup);
|
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
|
|
|
|
2014-06-24 08:33:27 -07:00
|
|
|
JSFlatString *flat = NewString<NoGC>(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-11-09 18:53:53 -08:00
|
|
|
js_ReportOutOfMemory(cx);
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
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
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
if (!atoms.relookupOrAdd(p, lookup, AtomStateEntry(atom, bool(ib)))) {
|
2013-07-10 08:29:52 -07:00
|
|
|
js_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
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. */
|
2014-06-13 11:51:05 -07:00
|
|
|
template <typename CharT>
|
2014-01-24 20:14:56 -08:00
|
|
|
MOZ_ALWAYS_INLINE
|
2013-04-30 15:38:45 -07:00
|
|
|
static JSAtom *
|
2014-06-13 11:51:05 -07:00
|
|
|
AtomizeAndCopyChars(ExclusiveContext *cx, const CharT *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
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomHasher::Lookup lookup(tbchars, length);
|
|
|
|
|
|
|
|
AtomSet::Ptr pp = cx->permanentAtoms().readonlyThreadsafeLookup(lookup);
|
|
|
|
if (pp)
|
|
|
|
return pp->asPtr();
|
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2013-07-10 08:29:52 -07:00
|
|
|
AtomSet& atoms = cx->atoms();
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomSet::AddPtr p = atoms.lookupForAdd(lookup);
|
2013-01-07 15:32:01 -08:00
|
|
|
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
|
|
|
|
2014-06-24 08:33:27 -07:00
|
|
|
JSFlatString *flat = NewStringCopyN<NoGC>(cx, tbchars, length);
|
2013-11-09 18:53:53 -08:00
|
|
|
if (!flat) {
|
2014-06-23 08:55:51 -07:00
|
|
|
// Grudgingly forgo last-ditch GC. The alternative would be to release
|
|
|
|
// the lock, manually GC here, and retry from the top. If you fix this,
|
|
|
|
// please also fix or comment the similar case in Symbol::new_.
|
2013-11-09 18:53:53 -08:00
|
|
|
js_ReportOutOfMemory(cx);
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-11-09 18:53:53 -08:00
|
|
|
}
|
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
|
|
|
|
2014-06-05 10:23:40 -07:00
|
|
|
// We have held the lock since looking up p, and the operations we've done
|
|
|
|
// since then can't GC; therefore the atoms table has not been modified and
|
|
|
|
// p is still valid.
|
|
|
|
if (!atoms.add(p, AtomStateEntry(atom, bool(ib)))) {
|
2013-11-09 18:53:53 -08:00
|
|
|
js_ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
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
|
|
|
}
|
|
|
|
|
2014-06-13 11:51:05 -07:00
|
|
|
template JSAtom *
|
|
|
|
AtomizeAndCopyChars(ExclusiveContext *cx, const jschar *tbchars, size_t length, InternBehavior ib);
|
|
|
|
|
|
|
|
template JSAtom *
|
|
|
|
AtomizeAndCopyChars(ExclusiveContext *cx, const Latin1Char *tbchars, size_t length, InternBehavior ib);
|
|
|
|
|
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;
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
AtomHasher::Lookup lookup(&atom);
|
|
|
|
|
|
|
|
/* Likewise, permanent atoms are always interned. */
|
|
|
|
AtomSet::Ptr p = cx->permanentAtoms().readonlyThreadsafeLookup(lookup);
|
|
|
|
if (p)
|
|
|
|
return &atom;
|
|
|
|
|
2013-07-19 07:06:02 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
p = cx->atoms().lookup(lookup);
|
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
|
|
|
|
2014-06-20 03:39:42 -07:00
|
|
|
JSLinearString *linear = str->ensureLinear(cx);
|
|
|
|
if (!linear)
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2014-06-20 03:39:42 -07:00
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
return linear->hasLatin1Chars()
|
|
|
|
? AtomizeAndCopyChars(cx, linear->latin1Chars(nogc), linear->length(), ib)
|
|
|
|
: AtomizeAndCopyChars(cx, linear->twoByteChars(nogc), 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
|
|
|
JSAtom *
|
2013-11-09 18:53:53 -08: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))
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2011-03-14 13:59:53 -07:00
|
|
|
|
2014-06-25 01:12:17 -07:00
|
|
|
if (EnableLatin1Strings) {
|
|
|
|
const Latin1Char *chars = reinterpret_cast<const Latin1Char*>(bytes);
|
|
|
|
return AtomizeAndCopyChars(cx, chars, length, ib);
|
|
|
|
}
|
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
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];
|
2014-05-31 01:44:32 -07:00
|
|
|
CopyAndInflateChars(inflated, bytes, length);
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeAndCopyChars(cx, inflated, length, 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-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2014-03-15 12:00:15 -07:00
|
|
|
return AtomizeAndtake(cx, tbcharsZ, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 11:51:05 -07:00
|
|
|
template <typename CharT>
|
2013-04-30 15:38:45 -07:00
|
|
|
JSAtom *
|
2014-06-13 11:51:05 -07:00
|
|
|
js::AtomizeChars(ExclusiveContext *cx, const CharT *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))
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2011-01-12 15:28:58 -08:00
|
|
|
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeAndCopyChars(cx, chars, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 11:51:05 -07:00
|
|
|
template JSAtom *
|
|
|
|
js::AtomizeChars(ExclusiveContext *cx, const Latin1Char *chars, size_t length, InternBehavior ib);
|
|
|
|
|
|
|
|
template JSAtom *
|
|
|
|
js::AtomizeChars(ExclusiveContext *cx, const jschar *chars, size_t length, InternBehavior ib);
|
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
bool
|
2013-12-18 04:30:49 -08:00
|
|
|
js::IndexToIdSlow(ExclusiveContext *cx, uint32_t index, MutableHandleId 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-11-09 18:53:53 -08:00
|
|
|
JSAtom *atom = AtomizeChars(cx, start.get(), end - start);
|
2011-08-04 19:21:25 -07:00
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
|
2013-12-18 04:30:49 -08:00
|
|
|
idp.set(JSID_FROM_BITS((size_t)atom));
|
2011-08-04 19:21:25 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 17:59:39 -07:00
|
|
|
template <AllowGC allowGC>
|
2013-08-27 02:17:41 -07:00
|
|
|
static JSAtom *
|
|
|
|
ToAtomSlow(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType arg)
|
2013-06-20 17:59:39 -07:00
|
|
|
{
|
2013-08-27 02:17:41 -07:00
|
|
|
JS_ASSERT(!arg.isString());
|
|
|
|
|
|
|
|
Value v = arg;
|
|
|
|
if (!v.isPrimitive()) {
|
|
|
|
if (!cx->shouldBeJSContext() || !allowGC)
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-08-27 02:17:41 -07:00
|
|
|
RootedValue v2(cx, v);
|
|
|
|
if (!ToPrimitive(cx->asJSContext(), JSTYPE_STRING, &v2))
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-08-27 02:17:41 -07:00
|
|
|
v = v2;
|
2013-06-20 17:59:39 -07:00
|
|
|
}
|
|
|
|
|
2013-08-27 02:17:41 -07:00
|
|
|
if (v.isString())
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeString(cx, v.toString());
|
2013-08-27 02:17:41 -07:00
|
|
|
if (v.isInt32())
|
2013-11-09 18:53:53 -08:00
|
|
|
return Int32ToAtom(cx, v.toInt32());
|
2013-08-27 02:17:41 -07:00
|
|
|
if (v.isDouble())
|
2013-11-09 18:53:53 -08:00
|
|
|
return NumberToAtom(cx, v.toDouble());
|
2013-08-27 02:17:41 -07:00
|
|
|
if (v.isBoolean())
|
|
|
|
return v.toBoolean() ? cx->names().true_ : cx->names().false_;
|
|
|
|
if (v.isNull())
|
|
|
|
return cx->names().null;
|
|
|
|
return cx->names().undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <AllowGC allowGC>
|
|
|
|
JSAtom *
|
|
|
|
js::ToAtom(ExclusiveContext *cx, typename MaybeRooted<Value, allowGC>::HandleType v)
|
|
|
|
{
|
|
|
|
if (!v.isString())
|
|
|
|
return ToAtomSlow<allowGC>(cx, v);
|
|
|
|
|
2013-06-20 17:59:39 -07:00
|
|
|
JSString *str = v.toString();
|
|
|
|
if (str->isAtom())
|
|
|
|
return &str->asAtom();
|
|
|
|
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeString(cx, str);
|
2013-06-20 17:59:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-06-24 03:01:39 -07:00
|
|
|
static_assert(JSString::MAX_LENGTH <= INT32_MAX, "String length must fit in 31 bits");
|
|
|
|
uint32_t length = atomp->length();
|
|
|
|
uint32_t lengthAndEncoding = (length << 1) | uint32_t(atomp->hasLatin1Chars());
|
|
|
|
if (!xdr->codeUint32(&lengthAndEncoding))
|
2012-05-01 14:01:06 -07:00
|
|
|
return false;
|
|
|
|
|
2014-06-24 03:01:39 -07:00
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
return atomp->hasLatin1Chars()
|
|
|
|
? xdr->codeChars(atomp->latin1Chars(nogc), length)
|
|
|
|
: xdr->codeChars(const_cast<jschar*>(atomp->twoByteChars(nogc)), length);
|
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. */
|
2014-06-24 03:01:39 -07:00
|
|
|
uint32_t lengthAndEncoding;
|
|
|
|
if (!xdr->codeUint32(&lengthAndEncoding))
|
2012-02-20 02:58:00 -08:00
|
|
|
return false;
|
|
|
|
|
2014-06-24 03:01:39 -07:00
|
|
|
uint32_t length = lengthAndEncoding >> 1;
|
|
|
|
bool latin1 = lengthAndEncoding & 0x1;
|
|
|
|
|
2012-02-20 02:58:00 -08:00
|
|
|
JSContext *cx = xdr->cx();
|
|
|
|
JSAtom *atom;
|
2014-06-24 03:01:39 -07:00
|
|
|
if (latin1) {
|
|
|
|
const Latin1Char *chars = reinterpret_cast<const Latin1Char *>(xdr->buf.read(length));
|
|
|
|
atom = AtomizeChars(cx, chars, length);
|
|
|
|
} else {
|
2012-02-20 02:58:00 -08:00
|
|
|
#if IS_LITTLE_ENDIAN
|
2014-06-24 03:01:39 -07:00
|
|
|
/* Directly access the little endian chars in the XDR buffer. */
|
|
|
|
const jschar *chars = reinterpret_cast<const jschar *>(xdr->buf.read(length * sizeof(jschar)));
|
|
|
|
atom = AtomizeChars(cx, chars, length);
|
2012-02-20 02:58:00 -08:00
|
|
|
#else
|
|
|
|
/*
|
2014-06-24 03:01:39 -07:00
|
|
|
* We must copy chars to a temporary buffer to convert between little and
|
|
|
|
* big endian data.
|
2012-02-20 02:58:00 -08:00
|
|
|
*/
|
2014-06-24 03:01:39 -07:00
|
|
|
jschar *chars;
|
|
|
|
jschar stackChars[256];
|
|
|
|
if (length <= 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 = cx->runtime()->pod_malloc<jschar>(length);
|
|
|
|
if (!chars)
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-20 02:58:00 -08:00
|
|
|
|
2014-06-24 03:01:39 -07:00
|
|
|
JS_ALWAYS_TRUE(xdr->codeChars(chars, length));
|
|
|
|
atom = AtomizeChars(cx, chars, length);
|
|
|
|
if (chars != stackChars)
|
|
|
|
js_free(chars);
|
2012-02-20 02:58:00 -08:00
|
|
|
#endif /* !IS_LITTLE_ENDIAN */
|
2014-06-24 03:01:39 -07:00
|
|
|
}
|
2012-02-20 02:58:00 -08:00
|
|
|
|
|
|
|
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
|
|
|
|