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

Side by Side Diff: Source/bindings/v8/V8PerContextData.cpp

Issue 180773003: Simplify the way to initialize context (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 30 matching lines...) Expand all
41 41
42 template<typename Map> 42 template<typename Map>
43 static void disposeMapWithUnsafePersistentValues(Map* map) 43 static void disposeMapWithUnsafePersistentValues(Map* map)
44 { 44 {
45 typename Map::iterator it = map->begin(); 45 typename Map::iterator it = map->begin();
46 for (; it != map->end(); ++it) 46 for (; it != map->end(); ++it)
47 it->value.dispose(); 47 it->value.dispose();
48 map->clear(); 48 map->clear();
49 } 49 }
50 50
51 V8PerContextData::V8PerContextData(v8::Handle<v8::Context> context, DOMWrapperWo rld* world)
52 : m_activityLogger(0)
53 , m_isolate(context->GetIsolate())
54 , m_contextHolder(adoptPtr(new gin::ContextHolder(context->GetIsolate())))
55 , m_world(world)
56 , m_context(m_isolate, context)
57 , m_customElementBindings(adoptPtr(new CustomElementBindingMap()))
58 {
59 m_contextHolder->SetContext(context);
60 context->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, this) ;
61
62 v8::Context::Scope contextScope(context);
63 ASSERT(m_errorPrototype.isEmpty());
64 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(context->Global ()->Get(v8AtomicString(m_isolate, "Error")));
65 ASSERT(!object.IsEmpty());
66 v8::Handle<v8::Value> prototypeValue = object->Get(v8AtomicString(m_isolate, "prototype"));
67 ASSERT(!prototypeValue.IsEmpty());
68 m_errorPrototype.set(m_isolate, prototypeValue);
69 }
70
51 V8PerContextData::~V8PerContextData() 71 V8PerContextData::~V8PerContextData()
52 { 72 {
53 v8::HandleScope handleScope(m_isolate); 73 v8::HandleScope handleScope(m_isolate);
54 V8PerContextDataHolder::from(m_context.newLocal(m_isolate))->setPerContextDa ta(0); 74 context()->SetAlignedPointerInEmbedderData(v8ContextPerContextDataIndex, 0);
55 75
56 disposeMapWithUnsafePersistentValues(&m_wrapperBoilerplates); 76 disposeMapWithUnsafePersistentValues(&m_wrapperBoilerplates);
57 disposeMapWithUnsafePersistentValues(&m_constructorMap); 77 disposeMapWithUnsafePersistentValues(&m_constructorMap);
58 m_customElementBindings.clear(); 78 m_customElementBindings.clear();
59 } 79 }
60 80
61 #define V8_STORE_PRIMORDIAL(name, Name) \
haraken 2014/02/28 11:14:05 This macro is used by only one place, so I inlined
62 { \
63 ASSERT(m_##name##Prototype.isEmpty()); \
64 v8::Handle<v8::String> symbol = v8AtomicString(m_isolate, #Name); \
65 if (symbol.IsEmpty()) \
66 return false; \
67 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(m_context.newLo cal(m_isolate)->Global()->Get(symbol)); \
68 if (object.IsEmpty()) \
69 return false; \
70 v8::Handle<v8::Value> prototypeValue = object->Get(prototypeString); \
71 if (prototypeValue.IsEmpty()) \
72 return false; \
73 m_##name##Prototype.set(m_isolate, prototypeValue); \
74 }
75
76 bool V8PerContextData::init()
haraken 2014/02/28 11:14:05 In reality, init() should not return false. Thus I
77 {
78 v8::Handle<v8::Context> context = m_context.newLocal(m_isolate);
79 V8PerContextDataHolder::from(context)->setPerContextData(this);
80
81 v8::Handle<v8::String> prototypeString = v8AtomicString(m_isolate, "prototyp e");
82 if (prototypeString.IsEmpty())
83 return false;
84
85 V8_STORE_PRIMORDIAL(error, Error);
86
87 return true;
88 }
89
90 #undef V8_STORE_PRIMORDIAL
91
92 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const Wra pperTypeInfo* type) 81 v8::Local<v8::Object> V8PerContextData::createWrapperFromCacheSlowCase(const Wra pperTypeInfo* type)
93 { 82 {
94 ASSERT(!m_errorPrototype.isEmpty()); 83 ASSERT(!m_errorPrototype.isEmpty());
95 84
96 v8::Context::Scope scope(m_context.newLocal(m_isolate)); 85 v8::Context::Scope scope(context());
97 v8::Local<v8::Function> function = constructorForType(type); 86 v8::Local<v8::Function> function = constructorForType(type);
98 v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(fu nction); 87 v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(fu nction);
99 if (!instanceTemplate.IsEmpty()) { 88 if (!instanceTemplate.IsEmpty()) {
100 m_wrapperBoilerplates.set(type, UnsafePersistent<v8::Object>(m_isolate, instanceTemplate)); 89 m_wrapperBoilerplates.set(type, UnsafePersistent<v8::Object>(m_isolate, instanceTemplate));
101 return instanceTemplate->Clone(); 90 return instanceTemplate->Clone();
102 } 91 }
103 return v8::Local<v8::Object>(); 92 return v8::Local<v8::Object>();
104 } 93 }
105 94
106 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const Wrapp erTypeInfo* type) 95 v8::Local<v8::Function> V8PerContextData::constructorForTypeSlowCase(const Wrapp erTypeInfo* type)
107 { 96 {
108 ASSERT(!m_errorPrototype.isEmpty()); 97 ASSERT(!m_errorPrototype.isEmpty());
109 98
110 v8::Context::Scope scope(m_context.newLocal(m_isolate)); 99 v8::Context::Scope scope(context());
111 v8::Handle<v8::FunctionTemplate> functionTemplate = type->domTemplate(m_isol ate, worldType(m_isolate)); 100 v8::Handle<v8::FunctionTemplate> functionTemplate = type->domTemplate(m_isol ate, worldType(m_isolate));
112 // Getting the function might fail if we're running out of stack or memory. 101 // Getting the function might fail if we're running out of stack or memory.
113 v8::TryCatch tryCatch; 102 v8::TryCatch tryCatch;
114 v8::Local<v8::Function> function = functionTemplate->GetFunction(); 103 v8::Local<v8::Function> function = functionTemplate->GetFunction();
115 if (function.IsEmpty()) 104 if (function.IsEmpty())
116 return v8::Local<v8::Function>(); 105 return v8::Local<v8::Function>();
117 106
118 if (type->parentClass) { 107 if (type->parentClass) {
119 v8::Local<v8::Object> prototypeTemplate = constructorForType(type->paren tClass); 108 v8::Local<v8::Object> prototypeTemplate = constructorForType(type->paren tClass);
120 if (prototypeTemplate.IsEmpty()) 109 if (prototypeTemplate.IsEmpty())
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 if (!data->IsString()) 201 if (!data->IsString())
213 return -1; 202 return -1;
214 v8::String::Utf8Value utf8(data); 203 v8::String::Utf8Value utf8(data);
215 char* comma = strnstr(*utf8, ",", utf8.length()); 204 char* comma = strnstr(*utf8, ",", utf8.length());
216 if (!comma) 205 if (!comma)
217 return -1; 206 return -1;
218 return atoi(comma + 1); 207 return atoi(comma + 1);
219 } 208 }
220 209
221 } // namespace WebCore 210 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698