2009-07-13 21:55:04 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-10-26 20:39:39 +00:00
|
|
|
#ifndef jsatominlines_h___
|
|
|
|
#define jsatominlines_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-01-30 17:17:38 +00:00
|
|
|
#include "mozilla/RangedPtr.h"
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:19:36 +00:00
|
|
|
inline bool
|
|
|
|
js_ValueToAtom(JSContext *cx, const js::Value &v, JSAtom **atomp)
|
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)
|
2010-07-15 06:19:36 +00:00
|
|
|
return false;
|
2011-03-14 20:59:53 +00:00
|
|
|
JS::Anchor<JSString *> anchor(str);
|
2011-05-17 19:15:12 +00:00
|
|
|
*atomp = js_AtomizeString(cx, str);
|
2011-03-14 20:59:53 +00:00
|
|
|
return !!*atomp;
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
2011-03-14 20:59:53 +00:00
|
|
|
|
|
|
|
JSString *str = v.toString();
|
|
|
|
if (str->isAtom()) {
|
|
|
|
*atomp = &str->asAtom();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-17 19:15:12 +00:00
|
|
|
*atomp = js_AtomizeString(cx, str);
|
2011-03-14 20:59:53 +00:00
|
|
|
return !!*atomp;
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:19:36 +00:00
|
|
|
inline bool
|
|
|
|
js_ValueToStringId(JSContext *cx, const js::Value &v, jsid *idp)
|
|
|
|
{
|
|
|
|
JSAtom *atom;
|
|
|
|
if (js_ValueToAtom(cx, v, &atom)) {
|
|
|
|
*idp = ATOM_TO_JSID(atom);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
js_InternNonIntElementId(JSContext *cx, JSObject *obj, const js::Value &idval,
|
|
|
|
jsid *idp)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!idval.isInt32() || !INT_FITS_IN_JSID(idval.toInt32()));
|
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
extern bool js_InternNonIntElementIdSlow(JSContext *, JSObject *,
|
|
|
|
const js::Value &, jsid *);
|
|
|
|
if (idval.isObject())
|
|
|
|
return js_InternNonIntElementIdSlow(cx, obj, idval, idp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return js_ValueToStringId(cx, idval, idp);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
js_InternNonIntElementId(JSContext *cx, JSObject *obj, const js::Value &idval,
|
|
|
|
jsid *idp, js::Value *vp)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!idval.isInt32() || !INT_FITS_IN_JSID(idval.toInt32()));
|
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
extern bool js_InternNonIntElementIdSlow(JSContext *, JSObject *,
|
|
|
|
const js::Value &,
|
|
|
|
jsid *, js::Value *);
|
|
|
|
if (idval.isObject())
|
|
|
|
return js_InternNonIntElementIdSlow(cx, obj, idval, idp, vp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JSAtom *atom;
|
|
|
|
if (js_ValueToAtom(cx, idval, &atom)) {
|
|
|
|
*idp = ATOM_TO_JSID(atom);
|
2011-03-14 20:59:53 +00:00
|
|
|
vp->setString(atom);
|
2010-07-15 06:19:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
js_Int32ToId(JSContext* cx, int32_t index, jsid* id)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2010-07-15 06:19:36 +00:00
|
|
|
if (INT_FITS_IN_JSID(index)) {
|
2009-07-13 21:55:04 +00:00
|
|
|
*id = INT_TO_JSID(index);
|
2010-07-15 06:19:36 +00:00
|
|
|
return true;
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
2010-07-15 06:19:36 +00:00
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
JSString* str = js_NumberToString(cx, index);
|
|
|
|
if (!str)
|
2010-07-15 06:19:36 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return js_ValueToStringId(cx, js::StringValue(str), id);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-30 07:19:26 +00:00
|
|
|
namespace js {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (index <= JSID_INT_MAX) {
|
|
|
|
*idp = INT_TO_JSID(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
extern bool IndexToIdSlow(JSContext *cx, uint32_t index, jsid *idp);
|
2011-08-05 02:21:25 +00:00
|
|
|
return IndexToIdSlow(cx, index, idp);
|
2011-04-30 07:19:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
JSString *str;
|
|
|
|
if (JS_LIKELY(JSID_IS_INT(id)))
|
|
|
|
str = js_IntToString(cx, JSID_TO_INT(id));
|
|
|
|
else
|
|
|
|
str = ToStringSlow(cx, IdToValue(id));
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-04-30 07:19:26 +00:00
|
|
|
} // namespace js
|
|
|
|
|
2009-10-26 20:39:39 +00:00
|
|
|
#endif /* jsatominlines_h___ */
|