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

Side by Side Diff: src/api.cc

Issue 886473005: Add WeakMap to v8.h (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project 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 "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 2221 matching lines...) Expand 10 before | Expand all | Expand 10 after
2232 return obj->IsTrue(); 2232 return obj->IsTrue();
2233 } 2233 }
2234 2234
2235 bool StackFrame::IsEval() const { return getBoolProperty(this, "isEval"); } 2235 bool StackFrame::IsEval() const { return getBoolProperty(this, "isEval"); }
2236 2236
2237 2237
2238 bool StackFrame::IsConstructor() const { 2238 bool StackFrame::IsConstructor() const {
2239 return getBoolProperty(this, "isConstructor"); 2239 return getBoolProperty(this, "isConstructor");
2240 } 2240 }
2241 2241
2242 2242
Michael Starzinger 2015/02/03 13:21:54 nit: // --- W e a k M a p ---
yurys 2015/02/03 13:39:07 Done.
2243 WeakMap* WeakMap::New(Isolate* v8_isolate) {
2244 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2245 ENTER_V8(isolate);
2246 i::HandleScope scope(isolate);
2247 i::Handle<i::JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
2248 i::Runtime::WeakCollectionInitialize(isolate, weakmap);
2249 Local<Object> v8_obj = Utils::ToLocal(i::Handle<i::JSObject>::cast(weakmap));
2250 return new WeakMap(v8_isolate, v8_obj);
2251 }
2252
2253
2254 void WeakMap::Set(Handle<Value> v8_key, Handle<Value> v8_value) {
2255 v8::HandleScope handleScope(isolate_);
2256 Local<Object> map_handle = Local<Object>::New(isolate_, map_);
2257 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_);
2258 ENTER_V8(isolate);
2259 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2260 i::Handle<i::Object> value = Utils::OpenHandle(*v8_value);
2261 i::Handle<i::JSWeakMap> weak_collection =
2262 i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle));
2263 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2264 DCHECK(false);
2265 return;
2266 }
2267 i::Handle<i::ObjectHashTable> table(
2268 i::ObjectHashTable::cast(weak_collection->table()));
2269 if (!table->IsKey(*key)) {
2270 DCHECK(false);
2271 return;
2272 }
2273 i::Runtime::WeakCollectionSet(weak_collection, key, value);
2274 }
2275
2276
2277 Local<Value> WeakMap::Get(Handle<Value> v8_key) {
2278 v8::EscapableHandleScope handleScope(isolate_);
2279 Local<Object> map_handle = Local<Object>::New(isolate_, map_);
2280 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_);
2281 ENTER_V8(isolate);
2282 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2283 i::Handle<i::JSWeakMap> weak_collection =
2284 i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle));
2285 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2286 DCHECK(false);
2287 return Undefined(isolate_);
2288 }
2289 i::Handle<i::ObjectHashTable> table(
2290 i::ObjectHashTable::cast(weak_collection->table()));
2291 if (!table->IsKey(*key)) {
2292 DCHECK(false);
2293 return Undefined(isolate_);
2294 }
2295 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2296 if (lookup->IsTheHole()) return Undefined(isolate_);
2297 Local<Value> result = Utils::ToLocal(lookup);
2298 return handleScope.Escape(result);
2299 }
2300
2301
2302 bool WeakMap::Has(Handle<Value> v8_key) {
2303 v8::HandleScope handleScope(isolate_);
2304 Local<Object> map_handle = Local<Object>::New(isolate_, map_);
2305 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_);
2306 ENTER_V8(isolate);
2307 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2308 i::Handle<i::JSWeakMap> weak_collection =
2309 i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle));
2310 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2311 DCHECK(false);
2312 return false;
2313 }
2314 i::Handle<i::ObjectHashTable> table(
2315 i::ObjectHashTable::cast(weak_collection->table()));
2316 if (!table->IsKey(*key)) {
2317 DCHECK(false);
2318 return false;
2319 }
2320 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2321 return !lookup->IsTheHole();
2322 }
2323
2324
2325 bool WeakMap::Delete(Handle<Value> v8_key) {
2326 v8::HandleScope handleScope(isolate_);
2327 Local<Object> map_handle = Local<Object>::New(isolate_, map_);
2328 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_);
2329 ENTER_V8(isolate);
2330 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2331 i::Handle<i::JSWeakMap> weak_collection =
2332 i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle));
2333 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2334 DCHECK(false);
2335 return false;
2336 }
2337 i::Handle<i::ObjectHashTable> table(
2338 i::ObjectHashTable::cast(weak_collection->table()));
2339 if (!table->IsKey(*key)) {
2340 DCHECK(false);
2341 return false;
2342 }
2343 return i::Runtime::WeakCollectionDelete(weak_collection, key);
2344 }
2345
2346
2243 // --- J S O N --- 2347 // --- J S O N ---
2244 2348
2245 Local<Value> JSON::Parse(Local<String> json_string) { 2349 Local<Value> JSON::Parse(Local<String> json_string) {
2246 i::Handle<i::String> string = Utils::OpenHandle(*json_string); 2350 i::Handle<i::String> string = Utils::OpenHandle(*json_string);
2247 i::Isolate* isolate = string->GetIsolate(); 2351 i::Isolate* isolate = string->GetIsolate();
2248 ENTER_V8(isolate); 2352 ENTER_V8(isolate);
2249 i::HandleScope scope(isolate); 2353 i::HandleScope scope(isolate);
2250 i::Handle<i::String> source = i::String::Flatten(string); 2354 i::Handle<i::String> source = i::String::Flatten(string);
2251 EXCEPTION_PREAMBLE(isolate); 2355 EXCEPTION_PREAMBLE(isolate);
2252 i::MaybeHandle<i::Object> maybe_result = 2356 i::MaybeHandle<i::Object> maybe_result =
(...skipping 5404 matching lines...) Expand 10 before | Expand all | Expand 10 after
7657 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 7761 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
7658 Address callback_address = 7762 Address callback_address =
7659 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 7763 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
7660 VMState<EXTERNAL> state(isolate); 7764 VMState<EXTERNAL> state(isolate);
7661 ExternalCallbackScope call_scope(isolate, callback_address); 7765 ExternalCallbackScope call_scope(isolate, callback_address);
7662 callback(info); 7766 callback(info);
7663 } 7767 }
7664 7768
7665 7769
7666 } } // namespace v8::internal 7770 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698