OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 "config.h" | 5 #include "config.h" |
6 #include "bindings/v8/PrivateScriptRunner.h" | 6 #include "bindings/v8/PrivateScriptRunner.h" |
7 | 7 |
8 #include "bindings/v8/V8Binding.h" | 8 #include "bindings/v8/V8Binding.h" |
9 #include "bindings/v8/V8PerContextData.h" | 9 #include "bindings/v8/V8PerContextData.h" |
10 #include "bindings/v8/V8ScriptRunner.h" | 10 #include "bindings/v8/V8ScriptRunner.h" |
11 #include "core/PrivateScriptSources.h" | 11 #include "core/PrivateScriptSources.h" |
| 12 #ifndef NDEBUG |
| 13 #include "core/PrivateScriptSourcesForTesting.h" |
| 14 #endif |
12 | 15 |
13 namespace WebCore { | 16 namespace WebCore { |
14 | 17 |
15 static v8::Handle<v8::Value> compilePrivateScript(v8::Isolate* isolate, String c
lassName) | 18 static v8::Handle<v8::Value> compilePrivateScript(v8::Isolate* isolate, String c
lassName) |
16 { | 19 { |
17 size_t index; | 20 size_t index; |
| 21 #ifndef NDEBUG |
| 22 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting); i
ndex++) { |
| 23 if (className == kPrivateScriptSourcesForTesting[index].name) |
| 24 break; |
| 25 } |
| 26 if (index != WTF_ARRAY_LENGTH(kPrivateScriptSourcesForTesting)) { |
| 27 String source(reinterpret_cast<const char*>(kPrivateScriptSourcesForTest
ing[index].source), kPrivateScriptSourcesForTesting[index].size); |
| 28 return V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, sou
rce), isolate); |
| 29 } |
| 30 #endif |
| 31 |
18 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is
auto-generated | 32 // |kPrivateScriptSources| is defined in V8PrivateScriptSources.h, which is
auto-generated |
19 // by make_private_script.py. | 33 // by make_private_script.py. |
20 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { | 34 for (index = 0; index < WTF_ARRAY_LENGTH(kPrivateScriptSources); index++) { |
21 if (className == kPrivateScriptSources[index].name) | 35 if (className == kPrivateScriptSources[index].name) |
22 break; | 36 break; |
23 } | 37 } |
24 RELEASE_ASSERT(index != WTF_ARRAY_LENGTH(kPrivateScriptSources)); | 38 RELEASE_ASSERT(index != WTF_ARRAY_LENGTH(kPrivateScriptSources)); |
25 String source(reinterpret_cast<const char*>(kPrivateScriptSources[index].sou
rce), kPrivateScriptSources[index].size); | 39 String source(reinterpret_cast<const char*>(kPrivateScriptSources[index].sou
rce), kPrivateScriptSources[index].size); |
26 return V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, source)
, isolate); | 40 return V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, source)
, isolate); |
27 } | 41 } |
28 | 42 |
29 v8::Handle<v8::Value> PrivateScriptRunner::run(ScriptState* scriptState, String
className, String functionName, v8::Handle<v8::Value> receiver, int argc, v8::Ha
ndle<v8::Value> argv[]) | 43 static v8::Handle<v8::Value> propertyValue(ScriptState* scriptState, String clas
sName, String propertyName) |
30 { | 44 { |
31 ASSERT(scriptState->perContextData()); | 45 ASSERT(scriptState->perContextData()); |
32 ASSERT(scriptState->executionContext()); | 46 ASSERT(scriptState->executionContext()); |
33 v8::Isolate* isolate = scriptState->isolate(); | 47 v8::Isolate* isolate = scriptState->isolate(); |
34 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile
dPrivateScript(className); | 48 v8::Handle<v8::Value> compiledClass = scriptState->perContextData()->compile
dPrivateScript(className); |
35 if (compiledClass.IsEmpty()) { | 49 if (compiledClass.IsEmpty()) { |
36 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()->
compiledPrivateScript("PrivateScriptRunner"); | 50 v8::Handle<v8::Value> installedClasses = scriptState->perContextData()->
compiledPrivateScript("PrivateScriptRunner"); |
37 if (installedClasses.IsEmpty()) { | 51 if (installedClasses.IsEmpty()) { |
38 installedClasses = compilePrivateScript(isolate, "PrivateScriptRunne
r"); | 52 installedClasses = compilePrivateScript(isolate, "PrivateScriptRunne
r"); |
39 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri
ptRunner", installedClasses); | 53 scriptState->perContextData()->setCompiledPrivateScript("PrivateScri
ptRunner", installedClasses); |
40 } | 54 } |
41 RELEASE_ASSERT(!installedClasses.IsEmpty()); | 55 RELEASE_ASSERT(!installedClasses.IsEmpty()); |
42 RELEASE_ASSERT(installedClasses->IsObject()); | 56 RELEASE_ASSERT(installedClasses->IsObject()); |
43 | 57 |
44 compilePrivateScript(isolate, className); | 58 compilePrivateScript(isolate, className); |
45 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St
ring(isolate, className)); | 59 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St
ring(isolate, className)); |
46 RELEASE_ASSERT(!compiledClass.IsEmpty()); | 60 RELEASE_ASSERT(!compiledClass.IsEmpty()); |
47 RELEASE_ASSERT(compiledClass->IsObject()); | 61 RELEASE_ASSERT(compiledClass->IsObject()); |
48 scriptState->perContextData()->setCompiledPrivateScript(className, compi
ledClass); | 62 scriptState->perContextData()->setCompiledPrivateScript(className, compi
ledClass); |
49 } | 63 } |
| 64 return v8::Handle<v8::Object>::Cast(compiledClass)->Get(v8String(isolate, pr
opertyName)); |
| 65 } |
50 | 66 |
51 v8::Handle<v8::Value> function = v8::Handle<v8::Object>::Cast(compiledClass)
->Get(v8String(isolate, functionName)); | 67 v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState
, String className, String operationName, v8::Handle<v8::Value> holder, int argc
, v8::Handle<v8::Value> argv[]) |
52 RELEASE_ASSERT(!function.IsEmpty()); | 68 { |
53 RELEASE_ASSERT(function->IsFunction()); | 69 v8::Handle<v8::Value> operation = propertyValue(scriptState, className, oper
ationName); |
54 | 70 RELEASE_ASSERT(!operation.IsEmpty()); |
55 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(function)
, scriptState->executionContext(), receiver, argc, argv, isolate); | 71 RELEASE_ASSERT(operation->IsFunction()); |
| 72 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(operation
), scriptState->executionContext(), holder, argc, argv, scriptState->isolate()); |
56 } | 73 } |
57 | 74 |
58 } // namespace WebCore | 75 } // namespace WebCore |
OLD | NEW |