1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (c) 2018 Philip Chimento <philip.chimento@gmail.com>
4 * Marco Trevisan <marco.trevisan@canonical.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #define GJS_USE_ATOM_FOREACH
26
27 #include <config.h>
28
29 #include <js/Id.h>
30 #include <js/RootingAPI.h>
31 #include <js/Symbol.h>
32 #include <js/TracingAPI.h>
33 #include <js/TypeDecls.h>
34 #include <jsapi.h> // for JS_AtomizeAndPinString
35
36 #include "cjs/atoms.h"
37
38 // Avoid static_assert in MSVC builds
39 namespace JS {
40 template <typename T> struct GCPolicy;
41
42 template <>
43 struct GCPolicy<void*> : public IgnoreGCPolicy<void*> {};
44 }
45
init(JSContext * cx,const char * str)46 bool GjsAtom::init(JSContext* cx, const char* str) {
47 JSString* s = JS_AtomizeAndPinString(cx, str);
48 if (!s)
49 return false;
50 m_jsid = JS::Heap<jsid>{JS::PropertyKey::fromPinnedString(s)};
51 return true;
52 }
53
init(JSContext * cx,const char * str)54 bool GjsSymbolAtom::init(JSContext* cx, const char* str) {
55 JS::RootedString descr(cx, JS_AtomizeAndPinString(cx, str));
56 if (!descr)
57 return false;
58 JS::Symbol* symbol = JS::NewSymbol(cx, descr);
59 if (!symbol)
60 return false;
61 m_jsid = JS::Heap<jsid>{SYMBOL_TO_JSID(symbol)};
62 return true;
63 }
64
65 /* Requires a current realm. This can GC, so it needs to be done after the
66 * tracing has been set up. */
init_atoms(JSContext * cx)67 bool GjsAtoms::init_atoms(JSContext* cx) {
68 #define INITIALIZE_ATOM(identifier, str) \
69 if (!identifier.init(cx, str)) \
70 return false;
71 FOR_EACH_ATOM(INITIALIZE_ATOM)
72 FOR_EACH_SYMBOL_ATOM(INITIALIZE_ATOM)
73 return true;
74 }
75
trace(JSTracer * trc)76 void GjsAtoms::trace(JSTracer* trc) {
77 #define TRACE_ATOM(identifier, str) \
78 JS::TraceEdge<jsid>(trc, identifier.id(), "Atom " str);
79 FOR_EACH_ATOM(TRACE_ATOM)
80 FOR_EACH_SYMBOL_ATOM(TRACE_ATOM)
81 #undef TRACE_ATOM
82 }
83