| Index: runtime/vm/object.cc
|
| diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc
|
| index 98d7b238584f2c48a85d7ce384475d7cf4e99e58..800d572b12f9e7c9427fda6afe98d36458737c94 100644
|
| --- a/runtime/vm/object.cc
|
| +++ b/runtime/vm/object.cc
|
| @@ -703,6 +703,10 @@ void Object::InitOnce(Isolate* isolate) {
|
| isolate->object_store()->set_bigint_class(cls);
|
| cls = Class::New<Double>();
|
| isolate->object_store()->set_double_class(cls);
|
| + cls = Class::New<Pixels>();
|
| + // TODO(regis): Remove once native getters are implemented for _Pixels.
|
| + cls.SetRefinalizeAfterPatch(); // Fields are defined both in C++ and in Dart.
|
| + isolate->object_store()->set_pixels_class(cls);
|
|
|
| // Ensure that class kExternalTypedDataUint8ArrayCid is registered as we
|
| // need it when reading in the token stream of bootstrap classes in the VM
|
| @@ -1378,6 +1382,13 @@ NOT_IN_PRODUCT(
|
| RegisterPrivateClass(cls, Symbols::_Double(), core_lib);
|
| pending_classes.Add(cls);
|
|
|
| + cls = Class::New<Pixels>();
|
| + object_store->set_pixels_class(cls);
|
| + // TODO(regis): Remove once native getters are implemented for _Pixels.
|
| + cls.SetRefinalizeAfterPatch(); // Fields are defined both in C++ and in Dart.
|
| + RegisterPrivateClass(cls, Symbols::_Pixels(), core_lib);
|
| + pending_classes.Add(cls);
|
| +
|
| // Class that represents the Dart class _Closure and C++ class Closure.
|
| cls = Class::New<Closure>();
|
| cls.set_type_arguments_field_offset(Closure::type_arguments_offset());
|
| @@ -1698,6 +1709,7 @@ NOT_IN_PRODUCT(
|
| #undef REGISTER_EXT_TYPED_DATA_CLASS
|
|
|
| cls = Class::New<Instance>(kByteBufferCid);
|
| + object_store->set_byte_buffer_class(cls);
|
|
|
| cls = Class::New<Integer>();
|
| object_store->set_integer_implementation_class(cls);
|
| @@ -1711,6 +1723,11 @@ NOT_IN_PRODUCT(
|
| cls = Class::New<Double>();
|
| object_store->set_double_class(cls);
|
|
|
| + cls = Class::New<Pixels>();
|
| + // TODO(regis): Remove once native getters are implemented for _Pixels.
|
| + cls.SetRefinalizeAfterPatch(); // Fields are defined both in C++ and in Dart.
|
| + object_store->set_pixels_class(cls);
|
| +
|
| cls = Class::New<Closure>();
|
| object_store->set_closure_class(cls);
|
|
|
| @@ -3572,7 +3589,7 @@ void Class::set_is_finalized() const {
|
|
|
|
|
| void Class::SetRefinalizeAfterPatch() const {
|
| - ASSERT(!IsTopLevel());
|
| + ASSERT((raw_ptr()->name_ == String::null()) || !IsTopLevel());
|
| set_state_bits(ClassFinalizedBits::update(RawClass::kRefinalizeAfterPatch,
|
| raw_ptr()->state_bits_));
|
| set_state_bits(TypeFinalizedBit::update(false, raw_ptr()->state_bits_));
|
| @@ -4338,6 +4355,30 @@ RawBigint* Class::LookupCanonicalBigint(Zone* zone,
|
| }
|
|
|
|
|
| +RawPixels* Class::LookupCanonicalPixels(Zone* zone,
|
| + intptr_t value,
|
| + intptr_t* index) const {
|
| + ASSERT(this->raw() == Isolate::Current()->object_store()->pixels_class());
|
| + const Array& constants = Array::Handle(zone, this->constants());
|
| + const intptr_t constants_len = constants.Length();
|
| + // Linear search to see whether this value is already present in the
|
| + // list of canonicalized constants.
|
| + Pixels& canonical_value = Pixels::Handle(zone);
|
| + while (*index < constants_len) {
|
| + canonical_value ^= constants.At(*index);
|
| + if (canonical_value.IsNull()) {
|
| + break;
|
| + }
|
| + if (canonical_value.Value() == value) {
|
| + ASSERT(canonical_value.IsCanonical());
|
| + return canonical_value.raw();
|
| + }
|
| + *index = *index + 1;
|
| + }
|
| + return Pixels::null();
|
| +}
|
| +
|
| +
|
| class CanonicalInstanceKey {
|
| public:
|
| explicit CanonicalInstanceKey(const Instance& key) : key_(key) {
|
| @@ -8017,6 +8058,9 @@ RawLiteralToken* LiteralToken::New(Token::Kind kind, const String& literal) {
|
| const Integer& value = Integer::Handle(Integer::NewCanonical(literal));
|
| ASSERT(value.IsSmi() || value.IsOld());
|
| result.set_value(value);
|
| + } else if (kind == Token::kPIXELS) {
|
| + const Pixels& value = Pixels::Handle(Pixels::NewCanonical(literal));
|
| + result.set_value(value);
|
| } else if (kind == Token::kDOUBLE) {
|
| const Double& value = Double::Handle(Double::NewCanonical(literal));
|
| result.set_value(value);
|
| @@ -18061,6 +18105,9 @@ RawInstance* Number::CheckAndCanonicalize(Thread* thread,
|
| return result.raw();
|
| }
|
| }
|
| + case kPixelsCid: {
|
| + return Pixels::NewCanonical(Pixels::Cast(*this).Value());
|
| + }
|
| default:
|
| UNREACHABLE();
|
| }
|
| @@ -19451,6 +19498,123 @@ const char* Bigint::ToCString() const {
|
| }
|
|
|
|
|
| +bool Pixels::CanonicalizeEquals(const Instance& other) const {
|
| + if (this->raw() == other.raw()) {
|
| + return true; // "===".
|
| + }
|
| + if (other.IsNull() || !other.IsPixels()) {
|
| + return false;
|
| + }
|
| + return Value() == Pixels::Cast(other).Value();
|
| +}
|
| +
|
| +
|
| +RawPixels* Pixels::New(intptr_t value, Heap::Space space) {
|
| + Thread* thread = Thread::Current();
|
| + Zone* zone = thread->zone();
|
| + Isolate* isolate = thread->isolate();
|
| + ASSERT(isolate->object_store()->pixels_class() != Class::null());
|
| + Pixels& result = Pixels::Handle(zone);
|
| + {
|
| + RawObject* raw = Object::Allocate(Pixels::kClassId,
|
| + Pixels::InstanceSize(),
|
| + space);
|
| + NoSafepointScope no_safepoint;
|
| + result ^= raw;
|
| + }
|
| + result.set_value(value);
|
| + return result.raw();
|
| +}
|
| +
|
| +
|
| +static bool CStringToPixels(const char* str,
|
| + intptr_t length,
|
| + intptr_t* result) {
|
| + ASSERT(str != NULL);
|
| + ASSERT(length > 0);
|
| + const uint64_t max_accum = (1LL << (63 - Pixels::kFractionalBits)) / 10;
|
| + uint64_t numerator = 0LL;
|
| + uint64_t denominator = 1LL;
|
| + bool seen_decimal_point = false;
|
| + for (intptr_t i = 0; i < length; i++) {
|
| + char c = str[i];
|
| + if (!seen_decimal_point && (c == '.')) {
|
| + seen_decimal_point = true;
|
| + } else if (('0' <= c) && (c <= '9')) {
|
| + // Ignore extra digits not contributing to the result.
|
| + if ((numerator < max_accum) && (denominator < max_accum)) {
|
| + numerator = numerator*10 + (c - '0');
|
| + if (seen_decimal_point) denominator *= 10;
|
| + }
|
| + } else {
|
| + if ((strcmp(str + i, "px") != 0) && (strcmp(str + i, "PX") != 0)) {
|
| + return false;
|
| + }
|
| + break;
|
| + }
|
| + }
|
| + uint64_t value = (numerator << Pixels::kFractionalBits) / denominator;
|
| + if (value > Pixels::kPlusInfinity) value = Pixels::kPlusInfinity;
|
| + *result = static_cast<intptr_t>(value);
|
| + return true;
|
| +}
|
| +
|
| +
|
| +RawPixels* Pixels::NewCanonical(const String& str) {
|
| + intptr_t value;
|
| + if (!CStringToPixels(str.ToCString(), str.Length(), &value)) {
|
| + return Pixels::null();
|
| + }
|
| + return Pixels::NewCanonical(value);
|
| +}
|
| +
|
| +RawPixels* Pixels::NewCanonical(intptr_t value) {
|
| + Thread* thread = Thread::Current();
|
| + Zone* zone = thread->zone();
|
| + Isolate* isolate = thread->isolate();
|
| + const Class& cls =
|
| + Class::Handle(zone, isolate->object_store()->pixels_class());
|
| + intptr_t index = 0;
|
| + Pixels& canonical_value = Pixels::Handle(zone);
|
| + canonical_value ^= cls.LookupCanonicalPixels(zone, value, &index);
|
| + if (!canonical_value.IsNull()) {
|
| + return canonical_value.raw();
|
| + }
|
| + {
|
| + SafepointMutexLocker ml(isolate->constant_canonicalization_mutex());
|
| + // Retry lookup.
|
| + {
|
| + canonical_value ^= cls.LookupCanonicalPixels(zone, value, &index);
|
| + if (!canonical_value.IsNull()) {
|
| + return canonical_value.raw();
|
| + }
|
| + }
|
| + canonical_value = Pixels::New(value, Heap::kOld);
|
| + canonical_value.SetCanonical();
|
| + // The value needs to be added to the constants list. Grow the list if
|
| + // it is full.
|
| + cls.InsertCanonicalNumber(zone, index, canonical_value);
|
| + return canonical_value.raw();
|
| + }
|
| +}
|
| +
|
| +
|
| +void Pixels::set_value(intptr_t value) const {
|
| + ASSERT((kMinusInfinity <= value) && (value <= kPlusInfinity));
|
| + StoreSmi(&raw_ptr()->value_, Smi::New(value));
|
| +}
|
| +
|
| +
|
| +const char* Pixels::ToCString() const {
|
| + if (IsNull()) {
|
| + return "_Pixels NULL";
|
| + }
|
| + // TODO(regis): Handle non finite values.
|
| + return OS::SCreate(Thread::Current()->zone(), "_Pixels %f",
|
| + static_cast<double>(Value()) / Pixels::kScaling);
|
| +}
|
| +
|
| +
|
| // Synchronize with implementation in compiler (intrinsifier).
|
| class StringHasher : ValueObject {
|
| public:
|
|
|