diff --git a/libdeno/binding.cc b/libdeno/binding.cc index 4c90e69931..c1fd0df73b 100644 --- a/libdeno/binding.cc +++ b/libdeno/binding.cc @@ -20,34 +20,23 @@ Deno* FromIsolate(v8::Isolate* isolate) { return static_cast(isolate->GetData(0)); } -void LazilyCreateDataMap(Deno* d) { - if (d->async_data_map.IsEmpty()) { - v8::HandleScope handle_scope(d->isolate); - // It's important for security reasons that async_data_map is not exposed to - // the VM. - auto async_data_map = v8::Map::New(d->isolate); - d->async_data_map.Reset(d->isolate, async_data_map); - } - DCHECK(!d->async_data_map.IsEmpty()); -} - void AddDataRef(Deno* d, int32_t req_id, v8::Local data_v) { - LazilyCreateDataMap(d); - auto async_data_map = d->async_data_map.Get(d->isolate); - auto context = d->context.Get(d->isolate); - auto req_id_v = v8::Integer::New(d->isolate, req_id); - auto r = async_data_map->Set(context, req_id_v, data_v); - CHECK(!r.IsEmpty()); + // TODO Use std::unique_ptr + auto pair = + std::make_pair(req_id, new v8::Persistent(d->isolate, data_v)); + d->async_data_map.insert(pair); } void DeleteDataRef(Deno* d, int32_t req_id) { - LazilyCreateDataMap(d); - auto context = d->context.Get(d->isolate); // Delete persistent reference to data ArrayBuffer. - auto async_data_map = d->async_data_map.Get(d->isolate); - auto req_id_v = v8::Integer::New(d->isolate, req_id); - auto maybe_deleted = async_data_map->Delete(context, req_id_v); - CHECK(maybe_deleted.IsJust()); + auto it = d->async_data_map.find(req_id); + if (it != d->async_data_map.end()) { + auto pair = *it; + auto p = pair.second; + p->Reset(); + delete p; + d->async_data_map.erase(it); + } } // Extracts a C string from a v8::V8 Utf8Value. diff --git a/libdeno/internal.h b/libdeno/internal.h index 02945dd2fb..2093725b94 100644 --- a/libdeno/internal.h +++ b/libdeno/internal.h @@ -2,6 +2,7 @@ #ifndef INTERNAL_H_ #define INTERNAL_H_ +#include #include #include "deno.h" #include "third_party/v8/include/v8.h" @@ -22,7 +23,7 @@ struct deno_s { int32_t pending_promise_events; v8::Persistent context; - v8::Persistent async_data_map; + std::map*> async_data_map; deno_recv_cb cb; int32_t next_req_id; void* user_data;