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 "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"
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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";
|
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
|
|
|
{
|
2013-08-13 08:13:46 -07:00
|
|
|
AutoLockForExclusiveAccess lock(rt);
|
|
|
|
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
|
|
|
{
|
2013-08-13 08:13:46 -07:00
|
|
|
AtomSet &atoms = rt->atoms();
|
2012-09-06 13:48:40 -07:00
|
|
|
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
|
|
|
{
|
2013-10-07 09:43:08 -07:00
|
|
|
rt->emptyString = nullptr;
|
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;
|
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
|
2012-09-06 13:48:40 -07:00
|
|
|
js::SweepAtoms(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-13 08:13:46 -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. */
|
2013-08-14 14:48:40 -07:00
|
|
|
JS_ASSERT_IF(rt->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
|
|
|
}
|
|
|
|
|
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-08-13 08:13:46 -07:00
|
|
|
AutoLockForExclusiveAccess lock(cx);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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-11-09 18:53:53 -08:00
|
|
|
JSFlatString *flat = js_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
|
|
|
|
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-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. */
|
|
|
|
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-11-09 18:53:53 -08:00
|
|
|
JSFlatString *flat = js_NewStringCopyN<NoGC>(cx, tbchars, length);
|
|
|
|
if (!flat) {
|
|
|
|
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
|
|
|
|
2013-04-30 03:18:18 -07:00
|
|
|
if (!atoms.relookupOrAdd(p, AtomHasher::Lookup(tbchars, length),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-08-15 14:14:43 -07:00
|
|
|
const jschar *chars = str->getChars(cx);
|
|
|
|
if (!chars)
|
2013-10-07 09:43:08 -07:00
|
|
|
return nullptr;
|
2013-01-24 19:18:34 -08:00
|
|
|
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeAndCopyChars(cx, chars, str->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
|
|
|
|
|
|
|
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];
|
2013-09-21 02:36:33 -07:00
|
|
|
InflateStringToBuffer(bytes, length, inflated);
|
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;
|
2013-11-09 18:53:53 -08:00
|
|
|
return AtomizeAndTakeOwnership(cx, tbcharsZ, length, ib);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
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))
|
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
|
|
|
}
|
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
bool
|
2013-11-09 18:53:53 -08:00
|
|
|
js::IndexToIdSlow(ExclusiveContext *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);
|
|
|
|
|
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-11-09 18:53:53 -08:00
|
|
|
*idp = 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) {
|
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-11-09 18:53:53 -08:00
|
|
|
atom = AtomizeChars(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-11-17 23:30:25 -08:00
|
|
|
atom = AtomizeChars(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
|
|
|
|