Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 } else { | 52 } else { |
| 53 return PP_MakeNull(); | 53 return PP_MakeNull(); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 Var::Var() { | 59 Var::Var() { |
| 60 memset(&var_, 0, sizeof(var_)); | 60 memset(&var_, 0, sizeof(var_)); |
| 61 var_.type = PP_VARTYPE_UNDEFINED; | 61 var_.type = PP_VARTYPE_UNDEFINED; |
| 62 needs_release_ = false; | 62 needs_management_ = true; |
| 63 } | 63 } |
| 64 | 64 |
| 65 Var::Var(Null) { | 65 Var::Var(Null) { |
| 66 memset(&var_, 0, sizeof(var_)); | 66 memset(&var_, 0, sizeof(var_)); |
| 67 var_.type = PP_VARTYPE_NULL; | 67 var_.type = PP_VARTYPE_NULL; |
| 68 needs_release_ = false; | 68 needs_management_ = true; |
| 69 } | 69 } |
| 70 | 70 |
| 71 Var::Var(bool b) { | 71 Var::Var(bool b) { |
| 72 var_.type = PP_VARTYPE_BOOL; | 72 var_.type = PP_VARTYPE_BOOL; |
| 73 var_.padding = 0; | 73 var_.padding = 0; |
| 74 var_.value.as_bool = PP_FromBool(b); | 74 var_.value.as_bool = PP_FromBool(b); |
| 75 needs_release_ = false; | 75 needs_management_ = true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 Var::Var(int32_t i) { | 78 Var::Var(int32_t i) { |
| 79 var_.type = PP_VARTYPE_INT32; | 79 var_.type = PP_VARTYPE_INT32; |
| 80 var_.padding = 0; | 80 var_.padding = 0; |
| 81 var_.value.as_int = i; | 81 var_.value.as_int = i; |
| 82 needs_release_ = false; | 82 needs_management_ = true; |
| 83 } | 83 } |
| 84 | 84 |
| 85 Var::Var(double d) { | 85 Var::Var(double d) { |
| 86 var_.type = PP_VARTYPE_DOUBLE; | 86 var_.type = PP_VARTYPE_DOUBLE; |
| 87 var_.padding = 0; | 87 var_.padding = 0; |
| 88 var_.value.as_double = d; | 88 var_.value.as_double = d; |
| 89 needs_release_ = false; | 89 needs_management_ = true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 Var::Var(const char* utf8_str) { | 92 Var::Var(const char* utf8_str) { |
| 93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; | 93 uint32_t len = utf8_str ? static_cast<uint32_t>(strlen(utf8_str)) : 0; |
| 94 var_ = VarFromUtf8Helper(utf8_str, len); | 94 var_ = VarFromUtf8Helper(utf8_str, len); |
| 95 needs_release_ = (var_.type == PP_VARTYPE_STRING); | 95 needs_management_ = true; |
| 96 } | 96 } |
| 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_management_ = true; |
| 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 needs_management_ = other.needs_management_; | |
|
dmichael (off chromium)
2012/09/10 16:21:14
Shouldn't this always just be true, with the new m
Takashi Toyoshima
2012/09/11 08:38:15
Oops, this was wrong.
I revert this line to be alw
| |
| 106 if (NeedsRefcounting(var_)) { | 107 if (NeedsRefcounting(var_)) { |
| 107 if (has_interface<PPB_Var_1_0>()) { | 108 if (has_interface<PPB_Var_1_0>()) |
| 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; | |
| 113 } | |
| 114 } else { | |
| 115 needs_release_ = false; | |
| 116 } | 112 } |
| 117 } | 113 } |
| 118 | 114 |
| 119 Var::~Var() { | 115 Var::~Var() { |
| 120 if (needs_release_ && has_interface<PPB_Var_1_0>()) | 116 if (NeedsRefcounting(var_) && |
| 117 needs_management_ && | |
| 118 has_interface<PPB_Var_1_0>()) | |
| 121 get_interface<PPB_Var_1_0>()->Release(var_); | 119 get_interface<PPB_Var_1_0>()->Release(var_); |
| 122 } | 120 } |
| 123 | 121 |
| 124 Var& Var::operator=(const Var& other) { | 122 Var& Var::operator=(const Var& other) { |
| 125 // Early return for self-assignment. Note however, that two distinct vars | 123 // 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 | 124 // can refer to the same object, so we still need to be careful about the |
| 127 // refcounting below. | 125 // refcounting below. |
| 128 if (this == &other) | 126 if (this == &other) |
| 129 return *this; | 127 return *this; |
| 130 | 128 |
| 131 // Be careful to keep the ref alive for cases where we're assigning an | 129 // 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. | 130 // object to itself by addrefing the new one before releasing the old one. |
| 133 bool old_needs_release = needs_release_; | 131 bool old_needs_management_ = needs_management_; |
|
dmichael (off chromium)
2012/09/10 16:21:14
no trailing underscore for local variables
Takashi Toyoshima
2012/09/11 08:38:15
Done.
| |
| 132 needs_management_ = true; | |
| 134 if (NeedsRefcounting(other.var_)) { | 133 if (NeedsRefcounting(other.var_)) { |
| 135 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else | 134 // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else |
| 136 // we couldn't have created them in the first place. | 135 // we couldn't have created them in the first place. |
| 137 needs_release_ = true; | |
| 138 get_interface<PPB_Var_1_0>()->AddRef(other.var_); | 136 get_interface<PPB_Var_1_0>()->AddRef(other.var_); |
| 139 } else { | |
| 140 needs_release_ = false; | |
| 141 } | 137 } |
| 142 if (old_needs_release) | 138 if (NeedsRefcounting(var_) && old_needs_management_) |
| 143 get_interface<PPB_Var_1_0>()->Release(var_); | 139 get_interface<PPB_Var_1_0>()->Release(var_); |
| 144 | 140 |
| 145 var_ = other.var_; | 141 var_ = other.var_; |
| 146 return *this; | 142 return *this; |
| 147 } | 143 } |
| 148 | 144 |
| 149 bool Var::operator==(const Var& other) const { | 145 bool Var::operator==(const Var& other) const { |
| 150 if (var_.type != other.var_.type) | 146 if (var_.type != other.var_.type) |
| 151 return false; | 147 return false; |
| 152 switch (var_.type) { | 148 switch (var_.type) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 snprintf(buf, sizeof(buf), format, str.c_str()); | 231 snprintf(buf, sizeof(buf), format, str.c_str()); |
| 236 } else if (is_array_buffer()) { | 232 } else if (is_array_buffer()) { |
| 237 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); | 233 snprintf(buf, sizeof(buf), "Var(ARRAY_BUFFER)"); |
| 238 } else if (is_object()) { | 234 } else if (is_object()) { |
| 239 snprintf(buf, sizeof(buf), "Var(OBJECT)"); | 235 snprintf(buf, sizeof(buf), "Var(OBJECT)"); |
| 240 } | 236 } |
| 241 return buf; | 237 return buf; |
| 242 } | 238 } |
| 243 | 239 |
| 244 } // namespace pp | 240 } // namespace pp |
| OLD | NEW |