Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/object.h" | 5 #include "vm/object.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
| 10 #include "vm/bigint_operations.h" | 10 #include "vm/bigint_operations.h" |
| (...skipping 4765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4776 RawArray* TokenStream::TokenObjects() const { | 4776 RawArray* TokenStream::TokenObjects() const { |
| 4777 return raw_ptr()->token_objects_; | 4777 return raw_ptr()->token_objects_; |
| 4778 } | 4778 } |
| 4779 | 4779 |
| 4780 | 4780 |
| 4781 void TokenStream::SetTokenObjects(const Array& value) const { | 4781 void TokenStream::SetTokenObjects(const Array& value) const { |
| 4782 StorePointer(&raw_ptr()->token_objects_, value.raw()); | 4782 StorePointer(&raw_ptr()->token_objects_, value.raw()); |
| 4783 } | 4783 } |
| 4784 | 4784 |
| 4785 | 4785 |
| 4786 void TokenStream::SetLength(intptr_t value) const { | 4786 RawExternalUint8Array* TokenStream::GetStream() const { |
| 4787 raw_ptr()->length_ = Smi::New(value); | 4787 return raw_ptr()->stream_; |
| 4788 } | |
| 4789 | |
| 4790 | |
| 4791 void TokenStream::SetStream(const ExternalUint8Array& value) const { | |
| 4792 StorePointer(&raw_ptr()->stream_, value.raw()); | |
| 4793 } | |
| 4794 | |
| 4795 | |
| 4796 void TokenStream::DataFinalizer(void *peer) { | |
| 4797 ASSERT(peer != NULL); | |
| 4798 ::free(peer); | |
|
cshapiro
2012/09/05 20:33:50
It is always safe to free NULL. That is a no-op.
siva
2012/09/06 18:19:29
I added the ASSERT more to ensure that this finali
| |
| 4788 } | 4799 } |
| 4789 | 4800 |
| 4790 | 4801 |
| 4791 RawString* TokenStream::PrivateKey() const { | 4802 RawString* TokenStream::PrivateKey() const { |
| 4792 return raw_ptr()->private_key_; | 4803 return raw_ptr()->private_key_; |
| 4793 } | 4804 } |
| 4794 | 4805 |
| 4795 | 4806 |
| 4796 void TokenStream::SetPrivateKey(const String& value) const { | 4807 void TokenStream::SetPrivateKey(const String& value) const { |
| 4797 StorePointer(&raw_ptr()->private_key_, value.raw()); | 4808 StorePointer(&raw_ptr()->private_key_, value.raw()); |
| 4798 } | 4809 } |
| 4799 | 4810 |
| 4800 | 4811 |
| 4801 RawString* TokenStream::GenerateSource() const { | 4812 RawString* TokenStream::GenerateSource() const { |
| 4802 Iterator iterator(*this, 0); | 4813 Iterator iterator(*this, 0); |
| 4814 const ExternalUint8Array& data = ExternalUint8Array::Handle(GetStream()); | |
| 4803 const GrowableObjectArray& literals = | 4815 const GrowableObjectArray& literals = |
| 4804 GrowableObjectArray::Handle(GrowableObjectArray::New(Length())); | 4816 GrowableObjectArray::Handle(GrowableObjectArray::New(data.Length())); |
| 4805 const String& private_key = String::Handle(PrivateKey()); | 4817 const String& private_key = String::Handle(PrivateKey()); |
| 4806 intptr_t private_len = private_key.Length(); | 4818 intptr_t private_len = private_key.Length(); |
| 4807 | 4819 |
| 4808 String& blank = String::Handle(String::New(" ")); | 4820 String& blank = String::Handle(String::New(" ")); |
| 4809 String& newline = String::Handle(String::New("\n")); | 4821 String& newline = String::Handle(String::New("\n")); |
| 4810 String& two_newlines = String::Handle(String::New("\n\n")); | 4822 String& two_newlines = String::Handle(String::New("\n\n")); |
| 4811 String& double_quotes = String::Handle(String::New("\"")); | 4823 String& double_quotes = String::Handle(String::New("\"")); |
| 4812 String& dollar = String::Handle(String::New("$")); | 4824 String& dollar = String::Handle(String::New("$")); |
| 4813 String& two_spaces = String::Handle(String::New(" ")); | 4825 String& two_spaces = String::Handle(String::New(" ")); |
| 4814 | 4826 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4958 Token::Kind kind = iterator.CurrentTokenKind(); | 4970 Token::Kind kind = iterator.CurrentTokenKind(); |
| 4959 while (index < src_pos && kind != Token::kEOS) { | 4971 while (index < src_pos && kind != Token::kEOS) { |
| 4960 iterator.Advance(); | 4972 iterator.Advance(); |
| 4961 kind = iterator.CurrentTokenKind(); | 4973 kind = iterator.CurrentTokenKind(); |
| 4962 index += 1; | 4974 index += 1; |
| 4963 } | 4975 } |
| 4964 return iterator.CurrentPosition(); | 4976 return iterator.CurrentPosition(); |
| 4965 } | 4977 } |
| 4966 | 4978 |
| 4967 | 4979 |
| 4980 RawTokenStream* TokenStream::New() { | |
| 4981 ASSERT(Object::token_stream_class() != Class::null()); | |
| 4982 TokenStream& result = TokenStream::Handle(); | |
| 4983 { | |
| 4984 RawObject* raw = Object::Allocate(TokenStream::kClassId, | |
| 4985 TokenStream::InstanceSize(), | |
| 4986 Heap::kOld); | |
| 4987 NoGCScope no_gc; | |
| 4988 result ^= raw; | |
| 4989 } | |
| 4990 return result.raw(); | |
| 4991 } | |
| 4992 | |
| 4993 | |
| 4968 RawTokenStream* TokenStream::New(intptr_t len) { | 4994 RawTokenStream* TokenStream::New(intptr_t len) { |
| 4969 ASSERT(Object::token_stream_class() != Class::null()); | 4995 uint8_t* data = reinterpret_cast<uint8_t*>(::malloc(len)); |
| 4970 if (len < 0 || len > kMaxElements) { | 4996 ASSERT(data != NULL); |
| 4971 // This should be caught before we reach here. | 4997 const ExternalUint8Array& stream = ExternalUint8Array::Handle( |
| 4972 FATAL1("Fatal error in TokenStream::New: invalid len %ld\n", len); | 4998 ExternalUint8Array::New(data, len, data, DataFinalizer, Heap::kOld)); |
| 4973 } | 4999 const TokenStream& result = TokenStream::Handle(TokenStream::New()); |
| 4974 TokenStream& result = TokenStream::Handle(); | 5000 result.SetStream(stream); |
| 4975 { | |
| 4976 RawObject* raw = Object::Allocate(TokenStream::kClassId, | |
| 4977 TokenStream::InstanceSize(len), | |
| 4978 Heap::kOld); | |
| 4979 NoGCScope no_gc; | |
| 4980 result ^= raw; | |
| 4981 result.SetLength(len); | |
| 4982 } | |
| 4983 return result.raw(); | 5001 return result.raw(); |
| 4984 } | 5002 } |
| 4985 | 5003 |
| 4986 | 5004 |
| 4987 // Helper class for creation of compressed token stream data. | 5005 // Helper class for creation of compressed token stream data. |
| 4988 class CompressedTokenStreamData : public ValueObject { | 5006 class CompressedTokenStreamData : public ValueObject { |
| 4989 public: | 5007 public: |
| 4990 CompressedTokenStreamData() : | 5008 CompressedTokenStreamData() : |
| 4991 buffer_(NULL), | 5009 buffer_(NULL), |
| 4992 stream_(&buffer_, Reallocate), | 5010 stream_(&buffer_, Reallocate), |
| 4993 token_objects_(GrowableObjectArray::Handle( | 5011 token_objects_(GrowableObjectArray::Handle( |
| 4994 GrowableObjectArray::New(kInitialTokenCount, Heap::kOld))), | 5012 GrowableObjectArray::New(kInitialTokenCount, Heap::kOld))), |
| 4995 token_obj_(Object::Handle()), | 5013 token_obj_(Object::Handle()), |
| 4996 literal_token_(LiteralToken::Handle()), | 5014 literal_token_(LiteralToken::Handle()), |
| 4997 literal_str_(String::Handle()) { | 5015 literal_str_(String::Handle()) { |
| 4998 const String& empty_literal = String::Handle(); | 5016 const String& empty_literal = String::Handle(); |
| 4999 token_objects_.Add(empty_literal); | 5017 token_objects_.Add(empty_literal); |
| 5000 } | 5018 } |
| 5001 ~CompressedTokenStreamData() { | 5019 ~CompressedTokenStreamData() { |
| 5002 free(buffer_); | |
| 5003 } | 5020 } |
| 5004 | 5021 |
| 5005 // Add an IDENT token into the stream and the token objects array. | 5022 // Add an IDENT token into the stream and the token objects array. |
| 5006 void AddIdentToken(String* ident) { | 5023 void AddIdentToken(String* ident) { |
| 5007 if (ident != NULL) { | 5024 if (ident != NULL) { |
| 5008 // If the IDENT token is already in the tokens object array use the | 5025 // If the IDENT token is already in the tokens object array use the |
| 5009 // same index instead of duplicating it. | 5026 // same index instead of duplicating it. |
| 5010 intptr_t index = FindIdentIndex(ident); | 5027 intptr_t index = FindIdentIndex(ident); |
| 5011 if (index == -1) { | 5028 if (index == -1) { |
| 5012 WriteIndex(token_objects_.Length()); | 5029 WriteIndex(token_objects_.Length()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5142 ASSERT(token.kind < Token::kNumTokens); | 5159 ASSERT(token.kind < Token::kNumTokens); |
| 5143 data.AddSimpleToken(token.kind); | 5160 data.AddSimpleToken(token.kind); |
| 5144 } | 5161 } |
| 5145 } | 5162 } |
| 5146 if (FLAG_compiler_stats) { | 5163 if (FLAG_compiler_stats) { |
| 5147 CompilerStats::num_tokens_total += len; | 5164 CompilerStats::num_tokens_total += len; |
| 5148 } | 5165 } |
| 5149 data.AddSimpleToken(Token::kEOS); // End of stream. | 5166 data.AddSimpleToken(Token::kEOS); // End of stream. |
| 5150 | 5167 |
| 5151 // Create and setup the token stream object. | 5168 // Create and setup the token stream object. |
| 5152 const TokenStream& result = TokenStream::Handle(New(data.Length())); | 5169 const ExternalUint8Array& stream = ExternalUint8Array::Handle( |
| 5170 ExternalUint8Array::New(data.GetStream(), | |
| 5171 data.Length(), | |
| 5172 data.GetStream(), | |
| 5173 DataFinalizer, | |
| 5174 Heap::kOld)); | |
| 5175 const TokenStream& result = TokenStream::Handle(New()); | |
| 5153 result.SetPrivateKey(private_key); | 5176 result.SetPrivateKey(private_key); |
| 5154 { | 5177 { |
| 5155 NoGCScope no_gc; | 5178 NoGCScope no_gc; |
| 5156 memmove(result.EntryAddr(0), data.GetStream(), data.Length()); | 5179 result.SetStream(stream); |
| 5157 const Array& tokens = Array::Handle(Array::MakeArray(data.TokenObjects())); | 5180 const Array& tokens = Array::Handle(Array::MakeArray(data.TokenObjects())); |
| 5158 result.SetTokenObjects(tokens); | 5181 result.SetTokenObjects(tokens); |
| 5159 } | 5182 } |
| 5160 return result.raw(); | 5183 return result.raw(); |
| 5161 } | 5184 } |
| 5162 | 5185 |
| 5163 | 5186 |
| 5164 const char* TokenStream::ToCString() const { | 5187 const char* TokenStream::ToCString() const { |
| 5165 return "TokenStream"; | 5188 return "TokenStream"; |
| 5166 } | 5189 } |
| 5167 | 5190 |
| 5168 | 5191 |
| 5169 TokenStream::Iterator::Iterator(const TokenStream& tokens, intptr_t token_pos) | 5192 TokenStream::Iterator::Iterator(const TokenStream& tokens, intptr_t token_pos) |
| 5170 : tokens_(tokens), | 5193 : tokens_(tokens), |
| 5194 data_(ExternalUint8Array::Handle(tokens.GetStream())), | |
| 5195 stream_(reinterpret_cast<uint8_t*>(data_.GetPeer()), data_.Length()), | |
| 5171 token_objects_(Array::Handle(tokens.TokenObjects())), | 5196 token_objects_(Array::Handle(tokens.TokenObjects())), |
| 5172 obj_(Object::Handle()), | 5197 obj_(Object::Handle()), |
| 5173 cur_token_pos_(token_pos), | 5198 cur_token_pos_(token_pos), |
| 5174 stream_token_pos_(token_pos), | |
| 5175 cur_token_kind_(Token::kILLEGAL), | 5199 cur_token_kind_(Token::kILLEGAL), |
| 5176 cur_token_obj_index_(-1) { | 5200 cur_token_obj_index_(-1) { |
| 5177 SetCurrentPosition(token_pos); | 5201 SetCurrentPosition(token_pos); |
| 5178 } | 5202 } |
| 5179 | 5203 |
| 5180 | 5204 |
| 5181 bool TokenStream::Iterator::IsValid() const { | 5205 bool TokenStream::Iterator::IsValid() const { |
| 5182 return !tokens_.IsNull(); | 5206 return !tokens_.IsNull(); |
| 5183 } | 5207 } |
| 5184 | 5208 |
| 5185 | 5209 |
| 5186 Token::Kind TokenStream::Iterator::LookaheadTokenKind(intptr_t num_tokens) { | 5210 Token::Kind TokenStream::Iterator::LookaheadTokenKind(intptr_t num_tokens) { |
| 5187 intptr_t saved_position = stream_token_pos_; | 5211 intptr_t saved_position = stream_.Position(); |
| 5188 Token::Kind kind = Token::kILLEGAL; | 5212 Token::Kind kind = Token::kILLEGAL; |
| 5189 intptr_t value = -1; | 5213 intptr_t value = -1; |
| 5190 intptr_t count = 0; | 5214 intptr_t count = 0; |
| 5191 while (count < num_tokens && value != Token::kEOS) { | 5215 while (count < num_tokens && value != Token::kEOS) { |
| 5192 value = ReadToken(); | 5216 value = ReadToken(); |
| 5193 count += 1; | 5217 count += 1; |
| 5194 } | 5218 } |
| 5195 if (value < Token::kNumTokens) { | 5219 if (value < Token::kNumTokens) { |
| 5196 kind = static_cast<Token::Kind>(value); | 5220 kind = static_cast<Token::Kind>(value); |
| 5197 } else { | 5221 } else { |
| 5198 value = value - Token::kNumTokens; | 5222 value = value - Token::kNumTokens; |
| 5199 obj_ = token_objects_.At(value); | 5223 obj_ = token_objects_.At(value); |
| 5200 if (obj_.IsLiteralToken()) { | 5224 if (obj_.IsLiteralToken()) { |
| 5201 const LiteralToken& literal_token = LiteralToken::Cast(obj_); | 5225 const LiteralToken& literal_token = LiteralToken::Cast(obj_); |
| 5202 kind = literal_token.kind(); | 5226 kind = literal_token.kind(); |
| 5203 } else { | 5227 } else { |
| 5204 ASSERT(obj_.IsString()); // Must be an identifier. | 5228 ASSERT(obj_.IsString()); // Must be an identifier. |
| 5205 kind = Token::kIDENT; | 5229 kind = Token::kIDENT; |
| 5206 } | 5230 } |
| 5207 } | 5231 } |
| 5208 stream_token_pos_ = saved_position; | 5232 stream_.SetPosition(saved_position); |
| 5209 return kind; | 5233 return kind; |
| 5210 } | 5234 } |
| 5211 | 5235 |
| 5212 | 5236 |
| 5213 intptr_t TokenStream::Iterator::CurrentPosition() const { | 5237 intptr_t TokenStream::Iterator::CurrentPosition() const { |
| 5214 return cur_token_pos_; | 5238 return cur_token_pos_; |
| 5215 } | 5239 } |
| 5216 | 5240 |
| 5217 | 5241 |
| 5218 void TokenStream::Iterator::SetCurrentPosition(intptr_t value) { | 5242 void TokenStream::Iterator::SetCurrentPosition(intptr_t value) { |
| 5219 stream_token_pos_ = value; | 5243 stream_.SetPosition(value); |
| 5220 Advance(); | 5244 Advance(); |
| 5221 } | 5245 } |
| 5222 | 5246 |
| 5223 | 5247 |
| 5224 void TokenStream::Iterator::Advance() { | 5248 void TokenStream::Iterator::Advance() { |
| 5225 cur_token_pos_ = stream_token_pos_; | 5249 cur_token_pos_ = stream_.Position(); |
| 5226 intptr_t value = ReadToken(); | 5250 intptr_t value = ReadToken(); |
| 5227 if (value < Token::kNumTokens) { | 5251 if (value < Token::kNumTokens) { |
| 5228 cur_token_kind_ = static_cast<Token::Kind>(value); | 5252 cur_token_kind_ = static_cast<Token::Kind>(value); |
| 5229 cur_token_obj_index_ = -1; | 5253 cur_token_obj_index_ = -1; |
| 5230 return; | 5254 return; |
| 5231 } | 5255 } |
| 5232 cur_token_obj_index_ = value - Token::kNumTokens; | 5256 cur_token_obj_index_ = value - Token::kNumTokens; |
| 5233 obj_ = token_objects_.At(cur_token_obj_index_); | 5257 obj_ = token_objects_.At(cur_token_obj_index_); |
| 5234 if (obj_.IsLiteralToken()) { | 5258 if (obj_.IsLiteralToken()) { |
| 5235 const LiteralToken& literal_token = LiteralToken::Cast(obj_); | 5259 const LiteralToken& literal_token = LiteralToken::Cast(obj_); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5276 } | 5300 } |
| 5277 return Symbols::New(Token::Str(kind)); | 5301 return Symbols::New(Token::Str(kind)); |
| 5278 } else { | 5302 } else { |
| 5279 ASSERT(obj.IsLiteralToken()); // Must be a literal token. | 5303 ASSERT(obj.IsLiteralToken()); // Must be a literal token. |
| 5280 const LiteralToken& literal_token = LiteralToken::Cast(obj); | 5304 const LiteralToken& literal_token = LiteralToken::Cast(obj); |
| 5281 return literal_token.literal(); | 5305 return literal_token.literal(); |
| 5282 } | 5306 } |
| 5283 } | 5307 } |
| 5284 | 5308 |
| 5285 | 5309 |
| 5286 intptr_t TokenStream::Iterator::ReadToken() { | |
| 5287 uint8_t b = ReadByte(); | |
| 5288 if (b > kMaxUnsignedDataPerByte) { | |
| 5289 return static_cast<intptr_t>(b) - kEndUnsignedByteMarker; | |
| 5290 } | |
| 5291 intptr_t value = 0; | |
| 5292 uint8_t s = 0; | |
| 5293 do { | |
| 5294 value |= static_cast<intptr_t>(b) << s; | |
| 5295 s += kDataBitsPerByte; | |
| 5296 b = ReadByte(); | |
| 5297 } while (b <= kMaxUnsignedDataPerByte); | |
| 5298 value |= ((static_cast<intptr_t>(b) - kEndUnsignedByteMarker) << s); | |
| 5299 ASSERT((value >= 0) && (value <= kIntptrMax)); | |
| 5300 return value; | |
| 5301 } | |
| 5302 | |
| 5303 | |
| 5304 uint8_t TokenStream::Iterator::ReadByte() { | |
| 5305 ASSERT(stream_token_pos_ < tokens_.Length()); | |
| 5306 return *(tokens_.EntryAddr(stream_token_pos_++)); | |
| 5307 } | |
| 5308 | |
| 5309 | |
| 5310 bool Script::HasSource() const { | 5310 bool Script::HasSource() const { |
| 5311 return raw_ptr()->source_ != String::null(); | 5311 return raw_ptr()->source_ != String::null(); |
| 5312 } | 5312 } |
| 5313 | 5313 |
| 5314 | 5314 |
| 5315 RawString* Script::Source() const { | 5315 RawString* Script::Source() const { |
| 5316 String& source = String::Handle(raw_ptr()->source_); | 5316 String& source = String::Handle(raw_ptr()->source_); |
| 5317 if (source.IsNull()) { | 5317 if (source.IsNull()) { |
| 5318 const TokenStream& token_stream = TokenStream::Handle(tokens()); | 5318 const TokenStream& token_stream = TokenStream::Handle(tokens()); |
| 5319 return token_stream.GenerateSource(); | 5319 return token_stream.GenerateSource(); |
| (...skipping 5870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 11190 } | 11190 } |
| 11191 return result.raw(); | 11191 return result.raw(); |
| 11192 } | 11192 } |
| 11193 | 11193 |
| 11194 | 11194 |
| 11195 const char* WeakProperty::ToCString() const { | 11195 const char* WeakProperty::ToCString() const { |
| 11196 return "_WeakProperty"; | 11196 return "_WeakProperty"; |
| 11197 } | 11197 } |
| 11198 | 11198 |
| 11199 } // namespace dart | 11199 } // namespace dart |
| OLD | NEW |