OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 10 matching lines...) Expand all Loading... |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "bindings/v8/V8ScriptRunner.h" | 27 #include "bindings/v8/V8ScriptRunner.h" |
28 | 28 |
29 #include "bindings/v8/V8Binding.h" | 29 #include "bindings/v8/V8Binding.h" |
30 #include "bindings/v8/V8GCController.h" | 30 #include "bindings/v8/V8GCController.h" |
| 31 #include "bindings/v8/V8HiddenPropertyName.h" |
31 #include "bindings/v8/V8RecursionScope.h" | 32 #include "bindings/v8/V8RecursionScope.h" |
32 #include "core/dom/ScriptExecutionContext.h" | 33 #include "core/dom/ScriptExecutionContext.h" |
33 #include "core/loader/CachedMetadata.h" | 34 #include "core/loader/CachedMetadata.h" |
34 #include "core/loader/cache/CachedScript.h" | 35 #include "core/loader/cache/CachedScript.h" |
35 #include "core/platform/chromium/TraceEvent.h" | 36 #include "core/platform/chromium/TraceEvent.h" |
36 | 37 |
37 namespace WebCore { | 38 namespace WebCore { |
38 | 39 |
39 PassOwnPtr<v8::ScriptData> V8ScriptRunner::precompileScript(v8::Handle<v8::Strin
g> code, CachedScript* cachedScript) | 40 PassOwnPtr<v8::ScriptData> V8ScriptRunner::precompileScript(v8::Handle<v8::Strin
g> code, CachedScript* cachedScript) |
40 { | 41 { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa
me, scriptStartPosition, scriptData, isolate); | 109 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa
me, scriptStartPosition, scriptData, isolate); |
109 if (script.IsEmpty()) | 110 if (script.IsEmpty()) |
110 return v8::Local<v8::Value>(); | 111 return v8::Local<v8::Value>(); |
111 | 112 |
112 V8RecursionScope::MicrotaskSuppression recursionScope; | 113 V8RecursionScope::MicrotaskSuppression recursionScope; |
113 v8::Local<v8::Value> result = script->Run(); | 114 v8::Local<v8::Value> result = script->Run(); |
114 crashIfV8IsDead(); | 115 crashIfV8IsDead(); |
115 return result; | 116 return result; |
116 } | 117 } |
117 | 118 |
| 119 v8::Local<v8::Function> V8ScriptRunner::blinkJSConstructor(const char* name, Wra
pperTypeInfo* typeInfo, const String& source, int argc, v8::Handle<v8::Value> ar
gv[], v8::Handle<v8::Context> context, v8::Isolate* isolate) |
| 120 { |
| 121 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| 122 V8PerContextData* perContextData = V8PerContextData::from(context); |
| 123 if (!perContextData) { |
| 124 String message = String(name) + " constructor is not found"; |
| 125 throwError(v8GeneralError, message.utf8().data(), isolate); |
| 126 return v8::Local<v8::Function>(); |
| 127 } |
| 128 v8::Local<v8::Value> wrapperConstructor = perContextData->constructorForType
(typeInfo); |
| 129 if (wrapperConstructor.IsEmpty() || !wrapperConstructor->IsObject()) { |
| 130 String message = String(name) + " constructor is not found"; |
| 131 throwError(v8GeneralError, message.utf8().data(), isolate); |
| 132 return v8::Local<v8::Function>(); |
| 133 } |
| 134 v8::Local<v8::Value> value = wrapperConstructor.As<v8::Object>()->GetHiddenV
alue(V8HiddenPropertyName::constructor()); |
| 135 if (!value.IsEmpty() && value->IsFunction()) { |
| 136 // Return the stored constructor. |
| 137 return value.As<v8::Function>(); |
| 138 } |
| 139 |
| 140 value = V8ScriptRunner::compileAndRunInternalScript(v8String(source, isolate
), isolate); |
| 141 if (value.IsEmpty()) |
| 142 return v8::Local<v8::Function>(); |
| 143 if (!value->IsFunction()) { |
| 144 String message = String(name) + " constructor implementation must be a f
unction"; |
| 145 throwError(v8TypeError, message.utf8().data(), isolate); |
| 146 return v8::Local<v8::Function>(); |
| 147 } |
| 148 |
| 149 v8::Local<v8::Function> impl = value.As<v8::Function>(); |
| 150 value = impl->Call(v8::Object::New(), argc, argv); |
| 151 if (value.IsEmpty()) |
| 152 return v8::Local<v8::Function>(); |
| 153 if (!value->IsFunction()) { |
| 154 String message = String(name) + " constructor must be a function"; |
| 155 throwError(v8TypeError, message.utf8().data(), isolate); |
| 156 return v8::Local<v8::Function>(); |
| 157 } |
| 158 bool result = wrapperConstructor.As<v8::Object>()->SetHiddenValue(V8HiddenPr
opertyName::constructor(), value); |
| 159 if (!result) { |
| 160 String message = String(name) + " constructor is not found"; |
| 161 throwError(v8GeneralError, message.utf8().data(), isolate); |
| 162 return v8::Local<v8::Function>(); |
| 163 } |
| 164 return value.As<v8::Function>(); |
| 165 } |
| 166 |
118 static String functionInfo(const v8::Handle<v8::Function> function) | 167 static String functionInfo(const v8::Handle<v8::Function> function) |
119 { | 168 { |
120 String resourceName = "undefined"; | 169 String resourceName = "undefined"; |
121 int lineNumber = 1; | 170 int lineNumber = 1; |
122 v8::ScriptOrigin origin = function->GetScriptOrigin(); | 171 v8::ScriptOrigin origin = function->GetScriptOrigin(); |
123 if (!origin.ResourceName().IsEmpty()) { | 172 if (!origin.ResourceName().IsEmpty()) { |
124 resourceName = toWebCoreString(origin.ResourceName()); | 173 resourceName = toWebCoreString(origin.ResourceName()); |
125 lineNumber = function->GetScriptLineNumber() + 1; | 174 lineNumber = function->GetScriptLineNumber() + 1; |
126 } | 175 } |
127 | 176 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 240 |
192 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Handle<v8:
:Function> function, ScriptExecutionContext* context, int argc, v8::Handle<v8::V
alue> argv[]) | 241 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Handle<v8:
:Function> function, ScriptExecutionContext* context, int argc, v8::Handle<v8::V
alue> argv[]) |
193 { | 242 { |
194 TRACE_EVENT0("v8", "v8.newInstance"); | 243 TRACE_EVENT0("v8", "v8.newInstance"); |
195 V8RecursionScope scope(context); | 244 V8RecursionScope scope(context); |
196 v8::Local<v8::Object> result = function->NewInstance(argc, argv); | 245 v8::Local<v8::Object> result = function->NewInstance(argc, argv); |
197 crashIfV8IsDead(); | 246 crashIfV8IsDead(); |
198 return result; | 247 return result; |
199 } | 248 } |
200 | 249 |
| 250 v8::Local<v8::Value> V8ScriptRunner::callUnwrappedMethod(const char* name, const
v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype) |
| 251 { |
| 252 Vector<v8::Handle<v8::Value> > arguments; |
| 253 for (int i = 0; i < args.Length(); ++i) |
| 254 arguments.append(args[i]); |
| 255 return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.da
ta(), args.GetIsolate(), prototype); |
| 256 } |
| 257 |
| 258 v8::Local<v8::Value> V8ScriptRunner::callUnwrappedMethod(const char* name, v8::H
andle<v8::Object> thisObject, |
| 259 int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8:
:Value> prototype) |
| 260 { |
| 261 if (thisObject.IsEmpty()) { |
| 262 String message = String("Cannot call method '") + name + "' of undefined
"; |
| 263 throwError(v8TypeError, message.utf8().data(), isolate); |
| 264 return v8::Local<v8::Value>(); |
| 265 } |
| 266 v8::Handle<v8::Value> unwrapped = thisObject->GetInternalField(v8DOMWrapperO
bjectIndex); |
| 267 if (unwrapped.IsEmpty() || !unwrapped->IsObject()) { |
| 268 throwError(v8TypeError, "The wrapped this value is not an object", isola
te); |
| 269 return v8::Local<v8::Value>(); |
| 270 } |
| 271 |
| 272 if (prototype.IsEmpty() || !prototype->IsObject()) { |
| 273 throwError(v8TypeError, "The prototype is not an object", isolate); |
| 274 return v8::Local<v8::Value>(); |
| 275 } |
| 276 |
| 277 v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String::
NewSymbol(name)); |
| 278 if (property.IsEmpty() || !property->IsFunction()) { |
| 279 String message = String("Property '") + name + "' of the object is not a
function"; |
| 280 throwError(v8TypeError, message.utf8().data(), isolate); |
| 281 return v8::Local<v8::Value>(); |
| 282 } |
| 283 return property.As<v8::Function>()->Call(unwrapped.As<v8::Object>(), argc, a
rgv); |
| 284 } |
| 285 |
| 286 v8::Local<v8::Value> V8ScriptRunner::callStaticMethod(const char* name, const v8
::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor) |
| 287 { |
| 288 if (constructor.IsEmpty() || !constructor->IsObject()) { |
| 289 throwError(v8TypeError, "The constructor object is not found", args.GetI
solate()); |
| 290 return v8::Local<v8::Value>(); |
| 291 } |
| 292 v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String
::NewSymbol(name)); |
| 293 if (property.IsEmpty() || !property->IsFunction()) { |
| 294 String message = String("Property '") + name + "' of the object is not a
function"; |
| 295 throwError(v8TypeError, message.utf8().data(), args.GetIsolate()); |
| 296 return v8::Local<v8::Value>(); |
| 297 } |
| 298 Vector<v8::Handle<v8::Value> > arguments; |
| 299 for (int i = 0; i < args.Length(); ++i) |
| 300 arguments.append(args[i]); |
| 301 return property.As<v8::Function>()->Call(args.This(), arguments.size(), argu
ments.data()); |
| 302 } |
| 303 |
201 } // namespace WebCore | 304 } // namespace WebCore |
OLD | NEW |