Chromium Code Reviews| Index: src/api.cc |
| diff --git a/src/api.cc b/src/api.cc |
| index 9b6be9ac556bee65c45f635b9dcba90028d5c9e3..a9e7df091247f9cd2843fab19e33c198f3087095 100644 |
| --- a/src/api.cc |
| +++ b/src/api.cc |
| @@ -2240,6 +2240,110 @@ bool StackFrame::IsConstructor() const { |
| } |
|
Michael Starzinger
2015/02/03 13:21:54
nit: // --- W e a k M a p ---
yurys
2015/02/03 13:39:07
Done.
|
| +WeakMap* WeakMap::New(Isolate* v8_isolate) { |
| + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| + ENTER_V8(isolate); |
| + i::HandleScope scope(isolate); |
| + i::Handle<i::JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap(); |
| + i::Runtime::WeakCollectionInitialize(isolate, weakmap); |
| + Local<Object> v8_obj = Utils::ToLocal(i::Handle<i::JSObject>::cast(weakmap)); |
| + return new WeakMap(v8_isolate, v8_obj); |
| +} |
| + |
| + |
| +void WeakMap::Set(Handle<Value> v8_key, Handle<Value> v8_value) { |
| + v8::HandleScope handleScope(isolate_); |
| + Local<Object> map_handle = Local<Object>::New(isolate_, map_); |
| + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_); |
| + ENTER_V8(isolate); |
| + i::Handle<i::Object> key = Utils::OpenHandle(*v8_key); |
| + i::Handle<i::Object> value = Utils::OpenHandle(*v8_value); |
| + i::Handle<i::JSWeakMap> weak_collection = |
| + i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle)); |
| + if (!key->IsJSReceiver() && !key->IsSymbol()) { |
| + DCHECK(false); |
| + return; |
| + } |
| + i::Handle<i::ObjectHashTable> table( |
| + i::ObjectHashTable::cast(weak_collection->table())); |
| + if (!table->IsKey(*key)) { |
| + DCHECK(false); |
| + return; |
| + } |
| + i::Runtime::WeakCollectionSet(weak_collection, key, value); |
| +} |
| + |
| + |
| +Local<Value> WeakMap::Get(Handle<Value> v8_key) { |
| + v8::EscapableHandleScope handleScope(isolate_); |
| + Local<Object> map_handle = Local<Object>::New(isolate_, map_); |
| + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_); |
| + ENTER_V8(isolate); |
| + i::Handle<i::Object> key = Utils::OpenHandle(*v8_key); |
| + i::Handle<i::JSWeakMap> weak_collection = |
| + i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle)); |
| + if (!key->IsJSReceiver() && !key->IsSymbol()) { |
| + DCHECK(false); |
| + return Undefined(isolate_); |
| + } |
| + i::Handle<i::ObjectHashTable> table( |
| + i::ObjectHashTable::cast(weak_collection->table())); |
| + if (!table->IsKey(*key)) { |
| + DCHECK(false); |
| + return Undefined(isolate_); |
| + } |
| + i::Handle<i::Object> lookup(table->Lookup(key), isolate); |
| + if (lookup->IsTheHole()) return Undefined(isolate_); |
| + Local<Value> result = Utils::ToLocal(lookup); |
| + return handleScope.Escape(result); |
| +} |
| + |
| + |
| +bool WeakMap::Has(Handle<Value> v8_key) { |
| + v8::HandleScope handleScope(isolate_); |
| + Local<Object> map_handle = Local<Object>::New(isolate_, map_); |
| + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_); |
| + ENTER_V8(isolate); |
| + i::Handle<i::Object> key = Utils::OpenHandle(*v8_key); |
| + i::Handle<i::JSWeakMap> weak_collection = |
| + i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle)); |
| + if (!key->IsJSReceiver() && !key->IsSymbol()) { |
| + DCHECK(false); |
| + return false; |
| + } |
| + i::Handle<i::ObjectHashTable> table( |
| + i::ObjectHashTable::cast(weak_collection->table())); |
| + if (!table->IsKey(*key)) { |
| + DCHECK(false); |
| + return false; |
| + } |
| + i::Handle<i::Object> lookup(table->Lookup(key), isolate); |
| + return !lookup->IsTheHole(); |
| +} |
| + |
| + |
| +bool WeakMap::Delete(Handle<Value> v8_key) { |
| + v8::HandleScope handleScope(isolate_); |
| + Local<Object> map_handle = Local<Object>::New(isolate_, map_); |
| + i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_); |
| + ENTER_V8(isolate); |
| + i::Handle<i::Object> key = Utils::OpenHandle(*v8_key); |
| + i::Handle<i::JSWeakMap> weak_collection = |
| + i::Handle<i::JSWeakMap>::cast(Utils::OpenHandle(*map_handle)); |
| + if (!key->IsJSReceiver() && !key->IsSymbol()) { |
| + DCHECK(false); |
| + return false; |
| + } |
| + i::Handle<i::ObjectHashTable> table( |
| + i::ObjectHashTable::cast(weak_collection->table())); |
| + if (!table->IsKey(*key)) { |
| + DCHECK(false); |
| + return false; |
| + } |
| + return i::Runtime::WeakCollectionDelete(weak_collection, key); |
| +} |
| + |
| + |
| // --- J S O N --- |
| Local<Value> JSON::Parse(Local<String> json_string) { |