1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/cpp/var.h"
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/c/ppb_var.h"
14 #include "ppapi/cpp/instance.h"
15 #include "ppapi/cpp/logging.h"
16 #include "ppapi/cpp/module.h"
17 #include "ppapi/cpp/module_impl.h"
19 // Define equivalent to snprintf on Windows.
21 # define snprintf sprintf_s
28 template <> const char* interface_name
<PPB_Var_1_1
>() {
29 return PPB_VAR_INTERFACE_1_1
;
31 template <> const char* interface_name
<PPB_Var_1_0
>() {
32 return PPB_VAR_INTERFACE_1_0
;
35 // Technically you can call AddRef and Release on any Var, but it may involve
36 // cross-process calls depending on the plugin. This is an optimization so we
37 // only do refcounting on the necessary objects.
38 inline bool NeedsRefcounting(const PP_Var
& var
) {
39 return var
.type
> PP_VARTYPE_DOUBLE
;
42 // This helper function detects whether PPB_Var version 1.1 is available. If so,
43 // it uses it to create a PP_Var for the given string. Otherwise it falls back
44 // to PPB_Var version 1.0.
45 PP_Var
VarFromUtf8Helper(const char* utf8_str
, uint32_t len
) {
46 if (has_interface
<PPB_Var_1_1
>()) {
47 return get_interface
<PPB_Var_1_1
>()->VarFromUtf8(utf8_str
, len
);
48 } else if (has_interface
<PPB_Var_1_0
>()) {
49 return get_interface
<PPB_Var_1_0
>()->VarFromUtf8(Module::Get()->pp_module(),
60 memset(&var_
, 0, sizeof(var_
));
61 var_
.type
= PP_VARTYPE_UNDEFINED
;
66 memset(&var_
, 0, sizeof(var_
));
67 var_
.type
= PP_VARTYPE_NULL
;
72 var_
.type
= PP_VARTYPE_BOOL
;
74 var_
.value
.as_bool
= PP_FromBool(b
);
79 var_
.type
= PP_VARTYPE_INT32
;
81 var_
.value
.as_int
= i
;
86 var_
.type
= PP_VARTYPE_DOUBLE
;
88 var_
.value
.as_double
= d
;
92 Var::Var(const char* utf8_str
) {
93 uint32_t len
= utf8_str
? static_cast<uint32_t>(strlen(utf8_str
)) : 0;
94 var_
= VarFromUtf8Helper(utf8_str
, len
);
98 Var::Var(const std::string
& utf8_str
) {
99 var_
= VarFromUtf8Helper(utf8_str
.c_str(),
100 static_cast<uint32_t>(utf8_str
.size()));
105 Var::Var(const PP_Var
& var
) {
108 if (NeedsRefcounting(var_
)) {
109 if (has_interface
<PPB_Var_1_0
>())
110 get_interface
<PPB_Var_1_0
>()->AddRef(var_
);
112 var_
.type
= PP_VARTYPE_NULL
;
116 Var::Var(const Var
& other
) {
119 if (NeedsRefcounting(var_
)) {
120 if (has_interface
<PPB_Var_1_0
>())
121 get_interface
<PPB_Var_1_0
>()->AddRef(var_
);
123 var_
.type
= PP_VARTYPE_NULL
;
128 if (NeedsRefcounting(var_
) &&
130 has_interface
<PPB_Var_1_0
>())
131 get_interface
<PPB_Var_1_0
>()->Release(var_
);
134 Var
& Var::operator=(const Var
& other
) {
135 // Early return for self-assignment. Note however, that two distinct vars
136 // can refer to the same object, so we still need to be careful about the
137 // refcounting below.
141 // Be careful to keep the ref alive for cases where we're assigning an
142 // object to itself by addrefing the new one before releasing the old one.
143 bool old_is_managed
= is_managed_
;
145 if (NeedsRefcounting(other
.var_
)) {
146 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else
147 // we couldn't have created them in the first place.
148 get_interface
<PPB_Var_1_0
>()->AddRef(other
.var_
);
150 if (NeedsRefcounting(var_
) && old_is_managed
)
151 get_interface
<PPB_Var_1_0
>()->Release(var_
);
157 bool Var::operator==(const Var
& other
) const {
158 if (var_
.type
!= other
.var_
.type
)
161 case PP_VARTYPE_UNDEFINED
:
162 case PP_VARTYPE_NULL
:
164 case PP_VARTYPE_BOOL
:
165 return AsBool() == other
.AsBool();
166 case PP_VARTYPE_INT32
:
167 return AsInt() == other
.AsInt();
168 case PP_VARTYPE_DOUBLE
:
169 return AsDouble() == other
.AsDouble();
170 case PP_VARTYPE_STRING
:
171 if (var_
.value
.as_id
== other
.var_
.value
.as_id
)
173 return AsString() == other
.AsString();
174 case PP_VARTYPE_OBJECT
:
175 case PP_VARTYPE_ARRAY
:
176 case PP_VARTYPE_ARRAY_BUFFER
:
177 case PP_VARTYPE_DICTIONARY
:
178 default: // Objects, arrays, dictionaries.
179 return var_
.value
.as_id
== other
.var_
.value
.as_id
;
183 bool Var::AsBool() const {
188 return PP_ToBool(var_
.value
.as_bool
);
191 int32_t Var::AsInt() const {
193 return var_
.value
.as_int
;
195 return static_cast<int>(var_
.value
.as_double
);
200 double Var::AsDouble() const {
202 return var_
.value
.as_double
;
204 return static_cast<double>(var_
.value
.as_int
);
209 std::string
Var::AsString() const {
212 return std::string();
215 if (!has_interface
<PPB_Var_1_0
>())
216 return std::string();
218 const char* str
= get_interface
<PPB_Var_1_0
>()->VarToUtf8(var_
, &len
);
219 return std::string(str
, len
);
222 std::string
Var::DebugString() const {
224 if (is_undefined()) {
225 snprintf(buf
, sizeof(buf
), "Var(UNDEFINED)");
226 } else if (is_null()) {
227 snprintf(buf
, sizeof(buf
), "Var(NULL)");
228 } else if (is_bool()) {
229 snprintf(buf
, sizeof(buf
), AsBool() ? "Var(true)" : "Var(false)");
230 } else if (is_int()) {
231 snprintf(buf
, sizeof(buf
), "Var(%d)", static_cast<int>(AsInt()));
232 } else if (is_double()) {
233 snprintf(buf
, sizeof(buf
), "Var(%f)", AsDouble());
234 } else if (is_string()) {
235 char format
[] = "Var<'%s'>";
236 size_t decoration
= sizeof(format
) - 2; // The %s is removed.
237 size_t available
= sizeof(buf
) - decoration
;
238 std::string str
= AsString();
239 if (str
.length() > available
) {
240 str
.resize(available
- 3); // Reserve space for ellipsis.
243 snprintf(buf
, sizeof(buf
), format
, str
.c_str());
244 } else if (is_object()) {
245 snprintf(buf
, sizeof(buf
), "Var(OBJECT)");
246 } else if (is_array()) {
247 snprintf(buf
, sizeof(buf
), "Var(ARRAY)");
248 } else if (is_dictionary()) {
249 snprintf(buf
, sizeof(buf
), "Var(DICTIONARY)");
250 } else if (is_array_buffer()) {
251 snprintf(buf
, sizeof(buf
), "Var(ARRAY_BUFFER)");