Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/var.h" | 5 #include "ppapi/cpp/var.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 | 97 |
| 98 Var::Var(const std::string& utf8_str) { | 98 Var::Var(const std::string& utf8_str) { |
| 99 var_ = VarFromUtf8Helper(utf8_str.c_str(), | 99 var_ = VarFromUtf8Helper(utf8_str.c_str(), |
| 100 static_cast<uint32_t>(utf8_str.size())); | 100 static_cast<uint32_t>(utf8_str.size())); |
| 101 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 101 needs_release_ = (var_.type == PP_VARTYPE_STRING); |
| 102 } | 102 } |
| 103 | 103 |
| 104 Var::Var(const Var& other) { | 104 Var::Var(const Var& other) { |
| 105 var_ = other.var_; | 105 var_ = other.var_; |
| 106 if (NeedsRefcounting(var_)) { | 106 if (NeedsRefcounting(var_)) { |
| 107 if (has_interface<PPB_Var>()) { | 107 if (has_interface<PPB_Var_1_0>()) { |
| 108 needs_release_ = true; | 108 needs_release_ = true; |
| 109 get_interface<PPB_Var_1_0>()->AddRef(var_); | 109 get_interface<PPB_Var_1_0>()->AddRef(var_); |
| 110 } else { | 110 } else { |
| 111 var_.type = PP_VARTYPE_NULL; | 111 var_.type = PP_VARTYPE_NULL; |
| 112 needs_release_ = false; | 112 needs_release_ = false; |
| 113 } | 113 } |
| 114 } else { | 114 } else { |
| 115 needs_release_ = false; | 115 needs_release_ = false; |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 Var::~Var() { | 119 Var::~Var() { |
| 120 if (needs_release_ && has_interface<PPB_Var>()) | 120 if (needs_release_ && has_interface<PPB_Var_1_0>()) |
| 121 get_interface<PPB_Var_1_0>()->Release(var_); | 121 get_interface<PPB_Var_1_0>()->Release(var_); |
| 122 } | 122 } |
| 123 | 123 |
| 124 Var& Var::operator=(const Var& other) { | 124 Var& Var::operator=(const Var& other) { |
| 125 // Early return for self-assignment. Note however, that two distinct vars | 125 // Early return for self-assignment. Note however, that two distinct vars |
| 126 // can refer to the same object, so we still need to be careful about the | 126 // can refer to the same object, so we still need to be careful about the |
| 127 // refcounting below. | 127 // refcounting below. |
| 128 if (this == &other) | 128 if (this == &other) |
| 129 return *this; | 129 return *this; |
| 130 | 130 |
| 131 // Be careful to keep the ref alive for cases where we're assigning an | 131 // Be careful to keep the ref alive for cases where we're assigning an |
| 132 // object to itself by addrefing the new one before releasing the old one. | 132 // object to itself by addrefing the new one before releasing the old one. |
| 133 bool old_needs_release = needs_release_; | 133 bool old_needs_release = needs_release_; |
| 134 if (NeedsRefcounting(other.var_)) { | 134 if (NeedsRefcounting(other.var_)) { |
| 135 // Assume we already has_interface<PPB_Var> for refcounted vars or else we | 135 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else |
| 136 // couldn't have created them in the first place. | 136 // we couldn't have created them in the first place. |
| 137 needs_release_ = true; | 137 needs_release_ = true; |
| 138 get_interface<PPB_Var_1_0>()->AddRef(other.var_); | 138 get_interface<PPB_Var_1_0>()->AddRef(other.var_); |
| 139 } else { | 139 } else { |
| 140 needs_release_ = false; | 140 needs_release_ = false; |
| 141 } | 141 } |
| 142 if (old_needs_release) | 142 if (old_needs_release) |
| 143 get_interface<PPB_Var_1_0>()->Release(var_); | 143 get_interface<PPB_Var_1_0>()->Release(var_); |
| 144 | 144 |
| 145 var_ = other.var_; | 145 var_ = other.var_; |
| 146 return *this; | 146 return *this; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 PP_NOTREACHED(); | 196 PP_NOTREACHED(); |
| 197 return 0.0; | 197 return 0.0; |
| 198 } | 198 } |
| 199 | 199 |
| 200 std::string Var::AsString() const { | 200 std::string Var::AsString() const { |
| 201 if (!is_string()) { | 201 if (!is_string()) { |
| 202 PP_NOTREACHED(); | 202 PP_NOTREACHED(); |
| 203 return std::string(); | 203 return std::string(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 if (!has_interface<PPB_Var>()) | 206 if (!has_interface<PPB_Var_1_0>()) |
| 207 return std::string(); | 207 return std::string(); |
| 208 uint32_t len; | 208 uint32_t len; |
| 209 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); | 209 const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len); |
| 210 return std::string(str, len); | 210 return std::string(str, len); |
| 211 } | 211 } |
| 212 | 212 |
| 213 std::string Var::DebugString() const { | 213 std::string Var::DebugString() const { |
| 214 char buf[256]; | 214 char buf[256]; |
| 215 if (is_undefined()) { | 215 if (is_undefined()) { |
| 216 snprintf(buf, sizeof(buf), "Var<UNDEFINED>"); | 216 snprintf(buf, sizeof(buf), "Var(UNDEFINED)"); |
|
dmichael (off chromium)
2011/12/20 23:52:27
The <> didn't play nicely when output as HTML. Thi
| |
| 217 } else if (is_null()) { | 217 } else if (is_null()) { |
| 218 snprintf(buf, sizeof(buf), "Var<NULL>"); | 218 snprintf(buf, sizeof(buf), "Var(NULL)"); |
| 219 } else if (is_bool()) { | 219 } else if (is_bool()) { |
| 220 snprintf(buf, sizeof(buf), AsBool() ? "Var<true>" : "Var<false>"); | 220 snprintf(buf, sizeof(buf), AsBool() ? "Var(true)" : "Var(false)"); |
| 221 } else if (is_int()) { | 221 } else if (is_int()) { |
| 222 // Note that the following static_cast is necessary because | 222 // Note that the following static_cast is necessary because |
| 223 // NativeClient's int32_t is actually "long". | 223 // NativeClient's int32_t is actually "long". |
| 224 // TODO(sehr,polina): remove this after newlib is changed. | 224 // TODO(sehr,polina): remove this after newlib is changed. |
| 225 snprintf(buf, sizeof(buf), "Var<%d>", static_cast<int>(AsInt())); | 225 snprintf(buf, sizeof(buf), "Var(%d)", static_cast<int>(AsInt())); |
| 226 } else if (is_double()) { | 226 } else if (is_double()) { |
| 227 snprintf(buf, sizeof(buf), "Var<%f>", AsDouble()); | 227 snprintf(buf, sizeof(buf), "Var(%f)", AsDouble()); |
| 228 } else if (is_string()) { | 228 } else if (is_string()) { |
| 229 char format[] = "Var<'%s'>"; | 229 char format[] = "Var<'%s'>"; |
| 230 size_t decoration = sizeof(format) - 2; // The %s is removed. | 230 size_t decoration = sizeof(format) - 2; // The %s is removed. |
| 231 size_t available = sizeof(buf) - decoration; | 231 size_t available = sizeof(buf) - decoration; |
| 232 std::string str = AsString(); | 232 std::string str = AsString(); |
| 233 if (str.length() > available) { | 233 if (str.length() > available) { |
| 234 str.resize(available - 3); // Reserve space for ellipsis. | 234 str.resize(available - 3); // Reserve space for ellipsis. |
| 235 str.append("..."); | 235 str.append("..."); |
| 236 } | 236 } |
| 237 snprintf(buf, sizeof(buf), format, str.c_str()); | 237 snprintf(buf, sizeof(buf), format, str.c_str()); |
| 238 } else if (is_array_buffer()) { | |
| 239 // TODO(dmichael): We could make this dump hex. Maybe DebugString should be | |
| 240 // virtual? | |
| 241 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); | |
| 238 } else if (is_object()) { | 242 } else if (is_object()) { |
| 239 snprintf(buf, sizeof(buf), "Var<OBJECT>"); | 243 snprintf(buf, sizeof(buf), "Var(OBJECT)"); |
| 240 } | 244 } |
| 241 return buf; | 245 return buf; |
| 242 } | 246 } |
| 243 | 247 |
| 244 } // namespace pp | 248 } // namespace pp |
| OLD | NEW |