2009-07-13 21:55:04 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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/. */
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2009-10-26 20:39:39 +00:00
|
|
|
#ifndef jsatominlines_h___
|
|
|
|
#define jsatominlines_h___
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2012-09-11 03:42:08 +00:00
|
|
|
#include "mozilla/RangedPtr.h"
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsnum.h"
|
2011-07-27 22:44:43 +00:00
|
|
|
#include "jsobj.h"
|
2011-10-04 14:06:54 +00:00
|
|
|
#include "jsstr.h"
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2012-09-11 03:42:08 +00:00
|
|
|
#include "gc/Barrier.h"
|
2012-01-30 17:17:38 +00:00
|
|
|
#include "vm/String.h"
|
|
|
|
|
2012-01-25 02:32:56 +00:00
|
|
|
inline JSAtom *
|
|
|
|
js::AtomStateEntry::asPtr() const
|
|
|
|
{
|
|
|
|
JS_ASSERT(bits != 0);
|
|
|
|
JSAtom *atom = reinterpret_cast<JSAtom *>(bits & NO_TAG_MASK);
|
|
|
|
JSString::readBarrier(atom);
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2012-06-12 23:03:10 +00:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
inline JSAtom *
|
|
|
|
ToAtom(JSContext *cx, const js::Value &v)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2011-03-14 20:59:53 +00:00
|
|
|
if (!v.isString()) {
|
2011-12-02 03:35:44 +00:00
|
|
|
JSString *str = js::ToStringSlow(cx, v);
|
2009-07-13 21:55:04 +00:00
|
|
|
if (!str)
|
2012-06-12 23:03:10 +00:00
|
|
|
return NULL;
|
2011-03-14 20:59:53 +00:00
|
|
|
JS::Anchor<JSString *> anchor(str);
|
2012-08-08 21:02:30 +00:00
|
|
|
return AtomizeString(cx, str);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
2011-03-14 20:59:53 +00:00
|
|
|
|
|
|
|
JSString *str = v.toString();
|
2012-06-12 23:03:10 +00:00
|
|
|
if (str->isAtom())
|
|
|
|
return &str->asAtom();
|
2011-03-14 20:59:53 +00:00
|
|
|
|
2012-06-12 23:03:10 +00:00
|
|
|
JS::Anchor<JSString *> anchor(str);
|
2012-08-08 21:02:30 +00:00
|
|
|
return AtomizeString(cx, str);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:19:36 +00:00
|
|
|
inline bool
|
2012-05-06 20:45:19 +00:00
|
|
|
ValueToId(JSContext* cx, JSObject *obj, const Value &v, jsid *idp)
|
2010-07-15 06:19:36 +00:00
|
|
|
{
|
2012-05-06 20:45:19 +00:00
|
|
|
int32_t i;
|
|
|
|
if (ValueFitsInInt32(v, &i) && INT_FITS_IN_JSID(i)) {
|
|
|
|
*idp = INT_TO_JSID(i);
|
2010-07-15 06:19:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-06 20:45:19 +00:00
|
|
|
|
|
|
|
return InternNonIntElementId(cx, obj, v, idp);
|
2010-07-15 06:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2012-05-06 20:45:19 +00:00
|
|
|
ValueToId(JSContext* cx, const Value &v, jsid *idp)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2012-05-06 20:45:19 +00:00
|
|
|
return ValueToId(cx, NULL, v, idp);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-05 02:21:25 +00:00
|
|
|
/*
|
|
|
|
* Write out character representing |index| to the memory just before |end|.
|
|
|
|
* Thus |*end| is not touched, but |end[-1]| and earlier are modified as
|
2011-08-10 21:54:47 +00:00
|
|
|
* appropriate. There must be at least js::UINT32_CHAR_BUFFER_LENGTH elements
|
2011-08-05 02:21:25 +00:00
|
|
|
* before |end| to avoid buffer underflow. The start of the characters written
|
|
|
|
* is returned and is necessarily before |end|.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline mozilla::RangedPtr<T>
|
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-09 03:54:10 +00:00
|
|
|
BackfillIndexInCharBuffer(uint32_t index, mozilla::RangedPtr<T> end)
|
2011-08-05 02:21:25 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
* Assert that the buffer we're filling will hold as many characters as we
|
|
|
|
* could write out, by dereferencing the index that would hold the most
|
|
|
|
* significant digit.
|
|
|
|
*/
|
|
|
|
(void) *(end - UINT32_CHAR_BUFFER_LENGTH);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
do {
|
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-09 03:54:10 +00:00
|
|
|
uint32_t next = index / 10, digit = index % 10;
|
2011-08-05 02:21:25 +00:00
|
|
|
*--end = '0' + digit;
|
|
|
|
index = next;
|
|
|
|
} while (index > 0);
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2012-11-16 21:59:26 +00:00
|
|
|
bool
|
|
|
|
IndexToIdSlow(JSContext *cx, uint32_t index, jsid *idp);
|
|
|
|
|
2011-04-30 07:19:26 +00:00
|
|
|
inline 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-09 03:54:10 +00:00
|
|
|
IndexToId(JSContext *cx, uint32_t index, jsid *idp)
|
2011-04-30 07:19:26 +00:00
|
|
|
{
|
2012-05-01 00:10:30 +00:00
|
|
|
MaybeCheckStackRoots(cx);
|
|
|
|
|
2011-04-30 07:19:26 +00:00
|
|
|
if (index <= JSID_INT_MAX) {
|
|
|
|
*idp = INT_TO_JSID(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-05 02:21:25 +00:00
|
|
|
return IndexToIdSlow(cx, index, idp);
|
2011-04-30 07:19:26 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 20:45:19 +00:00
|
|
|
inline jsid
|
|
|
|
AtomToId(JSAtom *atom)
|
|
|
|
{
|
|
|
|
JS_STATIC_ASSERT(JSID_INT_MIN == 0);
|
|
|
|
|
|
|
|
uint32_t index;
|
|
|
|
if (atom->isIndex(&index) && index <= JSID_INT_MAX)
|
|
|
|
return INT_TO_JSID((int32_t) index);
|
|
|
|
|
|
|
|
return JSID_FROM_BITS((size_t)atom);
|
|
|
|
}
|
|
|
|
|
2012-01-19 00:56:22 +00:00
|
|
|
static JS_ALWAYS_INLINE JSFlatString *
|
2011-06-08 23:50:23 +00:00
|
|
|
IdToString(JSContext *cx, jsid id)
|
|
|
|
{
|
|
|
|
if (JSID_IS_STRING(id))
|
2012-01-19 00:56:22 +00:00
|
|
|
return JSID_TO_ATOM(id);
|
2012-02-05 10:32:12 +00:00
|
|
|
|
2012-06-18 16:37:25 +00:00
|
|
|
if (JS_LIKELY(JSID_IS_INT(id)))
|
|
|
|
return Int32ToString(cx, JSID_TO_INT(id));
|
2012-02-05 10:32:12 +00:00
|
|
|
|
2012-06-18 16:37:25 +00:00
|
|
|
JSString *str = ToStringSlow(cx, IdToValue(id));
|
2012-02-05 10:32:12 +00:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2012-06-18 16:37:25 +00:00
|
|
|
|
2012-02-05 10:32:12 +00:00
|
|
|
return str->ensureFlat(cx);
|
2011-06-08 23:50:23 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 17:17:38 +00:00
|
|
|
inline
|
|
|
|
AtomHasher::Lookup::Lookup(const JSAtom *atom)
|
|
|
|
: chars(atom->chars()), length(atom->length()), atom(atom)
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
AtomHasher::match(const AtomStateEntry &entry, const Lookup &lookup)
|
|
|
|
{
|
|
|
|
JSAtom *key = entry.asPtr();
|
|
|
|
if (lookup.atom)
|
|
|
|
return lookup.atom == key;
|
|
|
|
if (key->length() != lookup.length)
|
|
|
|
return false;
|
|
|
|
return PodEqual(key->chars(), lookup.chars, lookup.length);
|
|
|
|
}
|
|
|
|
|
2012-09-11 03:42:08 +00:00
|
|
|
inline Handle<PropertyName*>
|
2012-09-06 20:48:40 +00:00
|
|
|
TypeName(JSType type, JSRuntime *rt)
|
|
|
|
{
|
|
|
|
JS_ASSERT(type < JSTYPE_LIMIT);
|
2012-09-11 17:32:33 +00:00
|
|
|
JS_STATIC_ASSERT(offsetof(JSAtomState, undefined) +
|
2012-09-11 03:42:08 +00:00
|
|
|
JSTYPE_LIMIT * sizeof(FixedHeapPtr<PropertyName>) <=
|
2012-09-06 20:48:40 +00:00
|
|
|
sizeof(JSAtomState));
|
|
|
|
JS_STATIC_ASSERT(JSTYPE_VOID == 0);
|
2012-09-11 17:32:33 +00:00
|
|
|
return (&rt->atomState.undefined)[type];
|
2012-09-06 20:48:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-11 03:42:08 +00:00
|
|
|
inline Handle<PropertyName*>
|
2012-09-06 20:48:40 +00:00
|
|
|
TypeName(JSType type, JSContext *cx)
|
|
|
|
{
|
|
|
|
return TypeName(type, cx->runtime);
|
|
|
|
}
|
|
|
|
|
2012-09-11 03:42:08 +00:00
|
|
|
inline Handle<PropertyName*>
|
2012-09-06 20:48:40 +00:00
|
|
|
ClassName(JSProtoKey key, JSContext *cx)
|
|
|
|
{
|
|
|
|
JS_ASSERT(key < JSProto_LIMIT);
|
2012-09-11 17:32:33 +00:00
|
|
|
JS_STATIC_ASSERT(offsetof(JSAtomState, Null) +
|
2012-09-11 03:42:08 +00:00
|
|
|
JSProto_LIMIT * sizeof(FixedHeapPtr<PropertyName>) <=
|
2012-09-06 20:48:40 +00:00
|
|
|
sizeof(JSAtomState));
|
|
|
|
JS_STATIC_ASSERT(JSProto_Null == 0);
|
2012-09-11 17:32:33 +00:00
|
|
|
return (&cx->runtime->atomState.Null)[key];
|
2012-09-06 20:48:40 +00:00
|
|
|
}
|
|
|
|
|
2011-04-30 07:19:26 +00:00
|
|
|
} // namespace js
|
|
|
|
|
2009-10-26 20:39:39 +00:00
|
|
|
#endif /* jsatominlines_h___ */
|