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()));
104 Var::Var(const Var
& other
) {
107 if (NeedsRefcounting(var_
)) {
108 if (has_interface
<PPB_Var_1_0
>())
109 get_interface
<PPB_Var_1_0
>()->AddRef(var_
);
111 var_
.type
= PP_VARTYPE_NULL
;
116 if (NeedsRefcounting(var_
) &&
118 has_interface
<PPB_Var_1_0
>())
119 get_interface
<PPB_Var_1_0
>()->Release(var_
);
122 Var
& Var::operator=(const Var
& other
) {
123 // Early return for self-assignment. Note however, that two distinct vars
124 // can refer to the same object, so we still need to be careful about the
125 // refcounting below.
129 // Be careful to keep the ref alive for cases where we're assigning an
130 // object to itself by addrefing the new one before releasing the old one.
131 bool old_is_managed
= is_managed_
;
133 if (NeedsRefcounting(other
.var_
)) {
134 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else
135 // we couldn't have created them in the first place.
136 get_interface
<PPB_Var_1_0
>()->AddRef(other
.var_
);
138 if (NeedsRefcounting(var_
) && old_is_managed
)
139 get_interface
<PPB_Var_1_0
>()->Release(var_
);
145 bool Var::operator==(const Var
& other
) const {
146 if (var_
.type
!= other
.var_
.type
)
149 case PP_VARTYPE_UNDEFINED
:
150 case PP_VARTYPE_NULL
:
152 case PP_VARTYPE_BOOL
:
153 return AsBool() == other
.AsBool();
154 case PP_VARTYPE_INT32
:
155 return AsInt() == other
.AsInt();
156 case PP_VARTYPE_DOUBLE
:
157 return AsDouble() == other
.AsDouble();
158 case PP_VARTYPE_STRING
:
159 if (var_
.value
.as_id
== other
.var_
.value
.as_id
)
161 return AsString() == other
.AsString();
162 case PP_VARTYPE_OBJECT
:
163 case PP_VARTYPE_ARRAY
:
164 case PP_VARTYPE_ARRAY_BUFFER
:
165 case PP_VARTYPE_DICTIONARY
:
166 default: // Objects, arrays, dictionaries.
167 return var_
.value
.as_id
== other
.var_
.value
.as_id
;
171 bool Var::AsBool() const {
176 return PP_ToBool(var_
.value
.as_bool
);
179 int32_t Var::AsInt() const {
181 return var_
.value
.as_int
;
183 return static_cast<int>(var_
.value
.as_double
);
188 double Var::AsDouble() const {
190 return var_
.value
.as_double
;
192 return static_cast<double>(var_
.value
.as_int
);
197 std::string
Var::AsString() const {
200 return std::string();
203 if (!has_interface
<PPB_Var_1_0
>())
204 return std::string();
206 const char* str
= get_interface
<PPB_Var_1_0
>()->VarToUtf8(var_
, &len
);
207 return std::string(str
, len
);
210 std::string
Var::DebugString() const {
212 if (is_undefined()) {
213 snprintf(buf
, sizeof(buf
), "Var(UNDEFINED)");
214 } else if (is_null()) {
215 snprintf(buf
, sizeof(buf
), "Var(NULL)");
216 } else if (is_bool()) {
217 snprintf(buf
, sizeof(buf
), AsBool() ? "Var(true)" : "Var(false)");
218 } else if (is_int()) {
219 snprintf(buf
, sizeof(buf
), "Var(%d)", static_cast<int>(AsInt()));
220 } else if (is_double()) {
221 snprintf(buf
, sizeof(buf
), "Var(%f)", AsDouble());
222 } else if (is_string()) {
223 char format
[] = "Var<'%s'>";
224 size_t decoration
= sizeof(format
) - 2; // The %s is removed.
225 size_t available
= sizeof(buf
) - decoration
;
226 std::string str
= AsString();
227 if (str
.length() > available
) {
228 str
.resize(available
- 3); // Reserve space for ellipsis.
231 snprintf(buf
, sizeof(buf
), format
, str
.c_str());
232 } else if (is_array_buffer()) {
233 snprintf(buf
, sizeof(buf
), "Var(ARRAY_BUFFER)");
234 } else if (is_object()) {
235 snprintf(buf
, sizeof(buf
), "Var(OBJECT)");