1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Atom support
44 /* #define TEST_ATOMS 1 */
48 _testAtoms(JSDContext
*jsdc
)
50 JSDAtom
* atom0
= jsd_AddAtom(jsdc
, "foo");
51 JSDAtom
* atom1
= jsd_AddAtom(jsdc
, "foo");
52 JSDAtom
* atom2
= jsd_AddAtom(jsdc
, "bar");
53 JSDAtom
* atom3
= jsd_CloneAtom(jsdc
, atom1
);
54 JSDAtom
* atom4
= jsd_CloneAtom(jsdc
, atom2
);
56 const char* c0
= JSD_ATOM_TO_STRING(atom0
);
57 const char* c1
= JSD_ATOM_TO_STRING(atom1
);
58 const char* c2
= JSD_ATOM_TO_STRING(atom2
);
59 const char* c3
= JSD_ATOM_TO_STRING(atom3
);
60 const char* c4
= JSD_ATOM_TO_STRING(atom4
);
62 jsd_DropAtom(jsdc
, atom0
);
63 jsd_DropAtom(jsdc
, atom1
);
64 jsd_DropAtom(jsdc
, atom2
);
65 jsd_DropAtom(jsdc
, atom3
);
66 jsd_DropAtom(jsdc
, atom4
);
71 _atom_smasher(JSHashEntry
*he
, intN i
, void *arg
)
75 JS_ASSERT(((JSDAtom
*)(he
->value
))->str
);
77 free(((JSDAtom
*)(he
->value
))->str
);
81 return HT_ENUMERATE_NEXT
;
85 _compareAtomKeys(const void *v1
, const void *v2
)
87 return 0 == strcmp((const char*)v1
, (const char*)v2
);
91 _compareAtoms(const void *v1
, const void *v2
)
93 return 0 == strcmp(((JSDAtom
*)v1
)->str
, ((JSDAtom
*)v2
)->str
);
98 jsd_CreateAtomTable(JSDContext
* jsdc
)
100 jsdc
->atoms
= JS_NewHashTable(256, JS_HashString
,
101 _compareAtomKeys
, _compareAtoms
,
106 return !!jsdc
->atoms
;
110 jsd_DestroyAtomTable(JSDContext
* jsdc
)
114 JS_HashTableEnumerateEntries(jsdc
->atoms
, _atom_smasher
, NULL
);
115 JS_HashTableDestroy(jsdc
->atoms
);
121 jsd_AddAtom(JSDContext
* jsdc
, const char* str
)
131 JSD_LOCK_ATOMS(jsdc
);
133 atom
= (JSDAtom
*) JS_HashTableLookup(jsdc
->atoms
, str
);
139 atom
= (JSDAtom
*) malloc(sizeof(JSDAtom
));
142 atom
->str
= strdup(str
);
144 if(!JS_HashTableAdd(jsdc
->atoms
, atom
->str
, atom
))
153 JSD_UNLOCK_ATOMS(jsdc
);
158 jsd_CloneAtom(JSDContext
* jsdc
, JSDAtom
* atom
)
160 JSD_LOCK_ATOMS(jsdc
);
162 JSD_UNLOCK_ATOMS(jsdc
);
167 jsd_DropAtom(JSDContext
* jsdc
, JSDAtom
* atom
)
169 JSD_LOCK_ATOMS(jsdc
);
170 if(! --atom
->refcount
)
172 JS_HashTableRemove(jsdc
->atoms
, atom
->str
);
176 JSD_UNLOCK_ATOMS(jsdc
);