Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: src/api.cc

Issue 12426015: create uniform string api (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/v8.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 5152 matching lines...) Expand 10 before | Expand all | Expand 10 after
5163 Local<String> v8::String::Empty() { 5163 Local<String> v8::String::Empty() {
5164 i::Isolate* isolate = i::Isolate::Current(); 5164 i::Isolate* isolate = i::Isolate::Current();
5165 if (!EnsureInitializedForIsolate(isolate, "v8::String::Empty()")) { 5165 if (!EnsureInitializedForIsolate(isolate, "v8::String::Empty()")) {
5166 return v8::Local<String>(); 5166 return v8::Local<String>();
5167 } 5167 }
5168 LOG_API(isolate, "String::Empty()"); 5168 LOG_API(isolate, "String::Empty()");
5169 return Utils::ToLocal(isolate->factory()->empty_string()); 5169 return Utils::ToLocal(isolate->factory()->empty_string());
5170 } 5170 }
5171 5171
5172 5172
5173 Local<String> v8::String::New(const char* data, int length) { 5173 // anonymous namespace for string creation helper functions
5174 i::Isolate* isolate = i::Isolate::Current(); 5174 namespace {
5175 EnsureInitializedForIsolate(isolate, "v8::String::New()"); 5175
5176 LOG_API(isolate, "String::New(char)"); 5176 inline int StringLength(const char* string) {
Yang 2013/03/22 12:46:05 use the INLINE macro if you *really* want to inlin
5177 if (length == 0) return Empty(); 5177 return i::StrLength(string);
5178 }
5179
5180
5181 inline int StringLength(const uint8_t* string) {
5182 return i::StrLength(reinterpret_cast<const char*>(string));
5183 }
5184
5185
5186 inline int StringLength(const uint16_t* string) {
5187 int length = 0;
5188 while (string[length] != '\0')
5189 length++;
Yang 2013/03/22 12:46:05 either brackets or no line break.
5190 return length;
5191 }
5192
5193
5194 inline i::Handle<i::String> NewString(i::Factory* factory,
5195 String::NewStringType type,
5196 i::Vector<const char> string) {
5197 if (type ==String::kInternalizedString) {
5198 return factory->InternalizeUtf8String(string);
5199 }
5200 return factory->NewStringFromUtf8(string);
5201 }
5202
5203
5204 inline i::Handle<i::String> NewString(i::Factory* factory,
5205 String::NewStringType type,
5206 i::Vector<const uint8_t> string) {
5207 if (type == String::kInternalizedString) {
5208 return factory->InternalizeOneByteString(string);
5209 }
5210 return factory->NewStringFromOneByte(string);
5211 }
5212
5213
5214 inline i::Handle<i::String> NewString(i::Factory* factory,
5215 String::NewStringType type,
5216 i::Vector<const uint16_t> string) {
5217 if (type == String::kInternalizedString) {
5218 return factory->InternalizeTwoByteString(string);
5219 }
5220 return factory->NewStringFromTwoByte(string);
5221 }
5222
5223
5224 template<typename Char>
5225 inline Local<String> NewString(Isolate* v8_isolate,
5226 const char* location,
5227 const char* env,
5228 const Char* data,
5229 String::NewStringType type,
5230 int length) {
5231 i::Isolate* isolate = reinterpret_cast<internal::Isolate*>(v8_isolate);
5232 EnsureInitializedForIsolate(isolate, location);
5233 LOG_API(isolate, env);
5234 if (length == 0 && type != String::kUndetectableString) {
5235 return String::Empty();
5236 }
5178 ENTER_V8(isolate); 5237 ENTER_V8(isolate);
5179 if (length == -1) length = i::StrLength(data); 5238 if (length == -1) length = StringLength(data);
Yang 2013/03/22 12:46:05 I don't have a strong opinion on this, but maybe w
5180 i::Handle<i::String> result = 5239 i::Handle<i::String> result = NewString(
5181 isolate->factory()->NewStringFromUtf8( 5240 isolate->factory(), type, i::Vector<const Char>(data, length));
5182 i::Vector<const char>(data, length)); 5241 if (type == String::kUndetectableString) {
5242 result->MarkAsUndetectable();
5243 }
5183 return Utils::ToLocal(result); 5244 return Utils::ToLocal(result);
5184 } 5245 }
5185 5246
5247 } // anonymous namespace
5248
5249
5250 Local<String> String::NewFromUtf8(Isolate* isolate,
5251 const char* data,
5252 NewStringType type,
5253 int length) {
5254 return NewString(isolate,
5255 "v8::String::NewFromUtf8()",
5256 "String::NewFromUtf8",
5257 data,
5258 type,
5259 length);
5260 }
5261
5262
5263 Local<String> String::NewFromOneByte(Isolate* isolate,
5264 const uint8_t* data,
5265 NewStringType type,
5266 int length) {
5267 return NewString(isolate,
5268 "v8::String::NewFromOneByte()",
5269 "String::NewFromOneByte",
5270 data,
5271 type,
5272 length);
5273 }
5274
5275
5276 Local<String> String::NewFromTwoByte(Isolate* isolate,
5277 const uint16_t* data,
5278 NewStringType type,
5279 int length) {
5280 return NewString(isolate,
5281 "v8::String::NewFromTwoByte()",
5282 "String::NewFromTwoByte",
5283 data,
5284 type,
5285 length);
5286 }
5287
5186 5288
5187 Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) { 5289 Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
5188 i::Handle<i::String> left_string = Utils::OpenHandle(*left); 5290 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
5189 i::Isolate* isolate = left_string->GetIsolate(); 5291 i::Isolate* isolate = left_string->GetIsolate();
5190 EnsureInitializedForIsolate(isolate, "v8::String::New()"); 5292 EnsureInitializedForIsolate(isolate, "v8::String::New()");
5191 LOG_API(isolate, "String::New(char)"); 5293 LOG_API(isolate, "String::New(char)");
5192 ENTER_V8(isolate); 5294 ENTER_V8(isolate);
5193 i::Handle<i::String> right_string = Utils::OpenHandle(*right); 5295 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
5194 i::Handle<i::String> result = isolate->factory()->NewConsString(left_string, 5296 i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
5195 right_string); 5297 right_string);
5196 return Utils::ToLocal(result); 5298 return Utils::ToLocal(result);
5197 } 5299 }
5198 5300
5199
5200 Local<String> v8::String::NewUndetectable(const char* data, int length) {
5201 i::Isolate* isolate = i::Isolate::Current();
5202 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
5203 LOG_API(isolate, "String::NewUndetectable(char)");
5204 ENTER_V8(isolate);
5205 if (length == -1) length = i::StrLength(data);
5206 i::Handle<i::String> result =
5207 isolate->factory()->NewStringFromUtf8(
5208 i::Vector<const char>(data, length));
5209 result->MarkAsUndetectable();
5210 return Utils::ToLocal(result);
5211 }
5212
5213
5214 static int TwoByteStringLength(const uint16_t* data) {
5215 int length = 0;
5216 while (data[length] != '\0') length++;
5217 return length;
5218 }
5219
5220
5221 Local<String> v8::String::New(const uint16_t* data, int length) {
5222 i::Isolate* isolate = i::Isolate::Current();
5223 EnsureInitializedForIsolate(isolate, "v8::String::New()");
5224 LOG_API(isolate, "String::New(uint16_)");
5225 if (length == 0) return Empty();
5226 ENTER_V8(isolate);
5227 if (length == -1) length = TwoByteStringLength(data);
5228 i::Handle<i::String> result =
5229 isolate->factory()->NewStringFromTwoByte(
5230 i::Vector<const uint16_t>(data, length));
5231 return Utils::ToLocal(result);
5232 }
5233
5234
5235 Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
5236 i::Isolate* isolate = i::Isolate::Current();
5237 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
5238 LOG_API(isolate, "String::NewUndetectable(uint16_)");
5239 ENTER_V8(isolate);
5240 if (length == -1) length = TwoByteStringLength(data);
5241 i::Handle<i::String> result =
5242 isolate->factory()->NewStringFromTwoByte(
5243 i::Vector<const uint16_t>(data, length));
5244 result->MarkAsUndetectable();
5245 return Utils::ToLocal(result);
5246 }
5247
5248 5301
5249 i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate, 5302 i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate,
5250 v8::String::ExternalStringResource* resource) { 5303 v8::String::ExternalStringResource* resource) {
5251 i::Handle<i::String> result = 5304 i::Handle<i::String> result =
5252 isolate->factory()->NewExternalStringFromTwoByte(resource); 5305 isolate->factory()->NewExternalStringFromTwoByte(resource);
5253 return result; 5306 return result;
5254 } 5307 }
5255 5308
5256 5309
5257 i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate, 5310 i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate,
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
5590 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon)); 5643 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
5591 EXCEPTION_PREAMBLE(isolate); 5644 EXCEPTION_PREAMBLE(isolate);
5592 ENTER_V8(isolate); 5645 ENTER_V8(isolate);
5593 i::Handle<i::JSObject> result = i::Copy(paragon_handle); 5646 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
5594 has_pending_exception = result.is_null(); 5647 has_pending_exception = result.is_null();
5595 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); 5648 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
5596 return Utils::ToLocal(result); 5649 return Utils::ToLocal(result);
5597 } 5650 }
5598 5651
5599 5652
5600 Local<String> v8::String::NewSymbol(const char* data, int length) {
5601 i::Isolate* isolate = i::Isolate::Current();
5602 EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()");
5603 LOG_API(isolate, "String::NewSymbol(char)");
5604 ENTER_V8(isolate);
5605 if (length == -1) length = i::StrLength(data);
5606 i::Handle<i::String> result = isolate->factory()->InternalizeUtf8String(
5607 i::Vector<const char>(data, length));
5608 return Utils::ToLocal(result);
5609 }
5610
5611
5612 Local<Number> v8::Number::New(double value) { 5653 Local<Number> v8::Number::New(double value) {
5613 i::Isolate* isolate = i::Isolate::Current(); 5654 i::Isolate* isolate = i::Isolate::Current();
5614 EnsureInitializedForIsolate(isolate, "v8::Number::New()"); 5655 EnsureInitializedForIsolate(isolate, "v8::Number::New()");
5615 if (isnan(value)) { 5656 if (isnan(value)) {
5616 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs. 5657 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
5617 value = i::OS::nan_value(); 5658 value = i::OS::nan_value();
5618 } 5659 }
5619 ENTER_V8(isolate); 5660 ENTER_V8(isolate);
5620 i::Handle<i::Object> result = isolate->factory()->NewNumber(value); 5661 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5621 return Utils::NumberToLocal(result); 5662 return Utils::NumberToLocal(result);
(...skipping 1446 matching lines...) Expand 10 before | Expand all | Expand 10 after
7068 7109
7069 v->VisitPointers(blocks_.first(), first_block_limit_); 7110 v->VisitPointers(blocks_.first(), first_block_limit_);
7070 7111
7071 for (int i = 1; i < blocks_.length(); i++) { 7112 for (int i = 1; i < blocks_.length(); i++) {
7072 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); 7113 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
7073 } 7114 }
7074 } 7115 }
7075 7116
7076 7117
7077 } } // namespace v8::internal 7118 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « include/v8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698