Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScriptVal.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "ScriptVal.h"
21 
22 #include "js/jsapi.h"
23 
24 struct Unrooter
25 {
26  Unrooter(JSContext* cx) : cx(cx) { }
27  void operator()(jsval* p) { JS_RemoveValueRoot(cx, p); delete p; }
28  JSContext* cx;
29 };
30 
31 CScriptValRooted::CScriptValRooted(JSContext* cx, jsval val)
32 {
33  jsval* p = new jsval(val);
34  JS_AddNamedValueRoot(cx, p, "CScriptValRooted");
35  m_Val = boost::shared_ptr<jsval>(p, Unrooter(cx));
36 }
37 
39 {
40  jsval* p = new jsval(val.get());
41  JS_AddNamedValueRoot(cx, p, "CScriptValRooted");
42  m_Val = boost::shared_ptr<jsval>(p, Unrooter(cx));
43 }
44 
45 jsval CScriptValRooted::get() const
46 {
47  if (!m_Val)
48  return JSVAL_VOID;
49  return *m_Val;
50 }
51 
53 {
54  ENSURE(m_Val);
55  return *m_Val;
56 }
57 
59 {
60  return (!m_Val || JSVAL_IS_VOID(*m_Val));
61 }
62 
64 {
65  return !m_Val;
66 }
67 
68 AutoJSIdArray::AutoJSIdArray(JSContext* cx, JSIdArray* ida) :
69  m_Context(cx), m_IdArray(ida)
70 {
71 }
72 
74 {
75  if (m_IdArray)
76  JS_DestroyIdArray(m_Context, m_IdArray);
77 }
78 
79 JSIdArray* AutoJSIdArray::get() const
80 {
81  return m_IdArray;
82 }
83 
84 size_t AutoJSIdArray::length() const
85 {
86  if (!m_IdArray)
87  return 0;
88  return m_IdArray->length;
89 }
90 
91 jsid AutoJSIdArray::operator[](size_t i) const
92 {
93  if (!(m_IdArray && i < (size_t)m_IdArray->length))
94  return JSID_VOID;
95  return m_IdArray->vector[i];
96 }
bool uninitialised() const
Returns whether the value is uninitialised.
Definition: ScriptVal.cpp:63
boost::shared_ptr< jsval > m_Val
Definition: ScriptVal.h:78
JSContext * m_Context
Definition: ScriptVal.h:96
const jsval & get() const
Returns the current value.
Definition: ScriptVal.h:38
A trivial wrapper around a jsval.
Definition: ScriptVal.h:29
void operator()(jsval *p)
Definition: ScriptVal.cpp:27
JSContext * cx
Definition: ScriptVal.cpp:28
AutoJSIdArray(JSContext *cx, JSIdArray *ida)
Definition: ScriptVal.cpp:68
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
JSIdArray * m_IdArray
Definition: ScriptVal.h:97
bool undefined() const
Returns whether the value is uninitialised or is JSVAL_VOID.
Definition: ScriptVal.cpp:58
size_t length() const
Definition: ScriptVal.cpp:84
JSIdArray * get() const
Definition: ScriptVal.cpp:79
jsid operator[](size_t i) const
Definition: ScriptVal.cpp:91
jsval & getRef() const
Returns reference to the current value.
Definition: ScriptVal.cpp:52
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
Unrooter(JSContext *cx)
Definition: ScriptVal.cpp:26