| 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/core/v8/PrivateScriptRunner.h" | 6 #include "bindings/core/v8/PrivateScriptRunner.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/DOMWrapperWorld.h" | 8 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 compilePrivateScript(isolate, className); | 72 compilePrivateScript(isolate, className); |
| 73 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St
ring(isolate, className)); | 73 compiledClass = v8::Handle<v8::Object>::Cast(installedClasses)->Get(v8St
ring(isolate, className)); |
| 74 RELEASE_ASSERT(!compiledClass.IsEmpty()); | 74 RELEASE_ASSERT(!compiledClass.IsEmpty()); |
| 75 RELEASE_ASSERT(compiledClass->IsObject()); | 75 RELEASE_ASSERT(compiledClass->IsObject()); |
| 76 scriptState->perContextData()->setCompiledPrivateScript(className, compi
ledClass); | 76 scriptState->perContextData()->setCompiledPrivateScript(className, compi
ledClass); |
| 77 } | 77 } |
| 78 return v8::Handle<v8::Object>::Cast(compiledClass); | 78 return v8::Handle<v8::Object>::Cast(compiledClass); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // FIXME: Replace this method with a V8 API for getOwnPropertyDescriptor, once V
8 is rolled. | |
| 82 static v8::Handle<v8::Object> getOwnPropertyDescriptor(ScriptState* scriptState,
v8::Handle<v8::Object> classObject, String name) | |
| 83 { | |
| 84 ASSERT(!scriptState->contextIsEmpty()); | |
| 85 v8::Handle<v8::Value> object = scriptState->context()->Global()->Get(v8Strin
g(scriptState->isolate(), "Object")); | |
| 86 RELEASE_ASSERT(!object.IsEmpty()); | |
| 87 RELEASE_ASSERT(object->IsObject()); | |
| 88 v8::Handle<v8::Value> getOwnPropertyDescriptorFunction = v8::Handle<v8::Obje
ct>::Cast(object)->Get(v8String(scriptState->isolate(), "getOwnPropertyDescripto
r")); | |
| 89 RELEASE_ASSERT(!getOwnPropertyDescriptorFunction.IsEmpty()); | |
| 90 RELEASE_ASSERT(getOwnPropertyDescriptorFunction->IsFunction()); | |
| 91 v8::Handle<v8::Value> argv[] = { classObject, v8String(scriptState->isolate(
), name) }; | |
| 92 v8::Handle<v8::Value> descriptor = V8ScriptRunner::callInternalFunction(v8::
Handle<v8::Function>::Cast(getOwnPropertyDescriptorFunction), object, WTF_ARRAY_
LENGTH(argv), argv, scriptState->isolate()); | |
| 93 RELEASE_ASSERT(!descriptor.IsEmpty()); | |
| 94 RELEASE_ASSERT(descriptor->IsObject()); | |
| 95 return v8::Handle<v8::Object>::Cast(descriptor); | |
| 96 } | |
| 97 | |
| 98 static void initializeHolderIfNeeded(ScriptState* scriptState, v8::Handle<v8::Ob
ject> classObject, v8::Handle<v8::Value> holder) | 81 static void initializeHolderIfNeeded(ScriptState* scriptState, v8::Handle<v8::Ob
ject> classObject, v8::Handle<v8::Value> holder) |
| 99 { | 82 { |
| 100 RELEASE_ASSERT(!holder.IsEmpty()); | 83 RELEASE_ASSERT(!holder.IsEmpty()); |
| 101 RELEASE_ASSERT(holder->IsObject()); | 84 RELEASE_ASSERT(holder->IsObject()); |
| 102 v8::Handle<v8::Object> holderObject = v8::Handle<v8::Object>::Cast(holder); | 85 v8::Handle<v8::Object> holderObject = v8::Handle<v8::Object>::Cast(holder); |
| 103 v8::Isolate* isolate = scriptState->isolate(); | 86 v8::Isolate* isolate = scriptState->isolate(); |
| 104 v8::Handle<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(isolate,
holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate)); | 87 v8::Handle<v8::Value> isInitialized = V8HiddenValue::getHiddenValue(isolate,
holderObject, V8HiddenValue::privateScriptObjectIsInitialized(isolate)); |
| 105 if (isInitialized.IsEmpty()) { | 88 if (isInitialized.IsEmpty()) { |
| 106 v8::TryCatch block; | 89 v8::TryCatch block; |
| 107 v8::Handle<v8::Value> initializeFunction = classObject->Get(v8String(iso
late, "initialize")); | 90 v8::Handle<v8::Value> initializeFunction = classObject->Get(v8String(iso
late, "initialize")); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 131 if (!scriptState->executionContext()) | 114 if (!scriptState->executionContext()) |
| 132 return v8::Handle<v8::Value>(); | 115 return v8::Handle<v8::Value>(); |
| 133 | 116 |
| 134 ScriptState::Scope scope(scriptState); | 117 ScriptState::Scope scope(scriptState); |
| 135 return classObjectOfPrivateScript(scriptState, className); | 118 return classObjectOfPrivateScript(scriptState, className); |
| 136 } | 119 } |
| 137 | 120 |
| 138 v8::Handle<v8::Value> PrivateScriptRunner::runDOMAttributeGetter(ScriptState* sc
riptState, String className, String attributeName, v8::Handle<v8::Value> holder) | 121 v8::Handle<v8::Value> PrivateScriptRunner::runDOMAttributeGetter(ScriptState* sc
riptState, String className, String attributeName, v8::Handle<v8::Value> holder) |
| 139 { | 122 { |
| 140 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); | 123 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); |
| 141 v8::Handle<v8::Object> descriptor = getOwnPropertyDescriptor(scriptState, cl
assObject, attributeName); | 124 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S
tring(scriptState->isolate(), attributeName)); |
| 142 v8::Handle<v8::Value> getter = descriptor->Get(v8String(scriptState->isolate
(), "get")); | 125 if (descriptor.IsEmpty() || !descriptor->IsObject()) { |
| 126 FATAL("Private script error: Target DOM attribute getter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); |
| 127 RELEASE_ASSERT_NOT_REACHED(); |
| 128 } |
| 129 v8::Handle<v8::Value> getter = v8::Handle<v8::Object>::Cast(descriptor)->Get
(v8String(scriptState->isolate(), "get")); |
| 143 if (getter.IsEmpty() || !getter->IsFunction()) { | 130 if (getter.IsEmpty() || !getter->IsFunction()) { |
| 144 FATAL("Private script error: Target DOM attribute getter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); | 131 FATAL("Private script error: Target DOM attribute getter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); |
| 145 RELEASE_ASSERT_NOT_REACHED(); | 132 RELEASE_ASSERT_NOT_REACHED(); |
| 146 } | 133 } |
| 147 initializeHolderIfNeeded(scriptState, classObject, holder); | 134 initializeHolderIfNeeded(scriptState, classObject, holder); |
| 148 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(getter),
scriptState->executionContext(), holder, 0, 0, scriptState->isolate()); | 135 return V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(getter),
scriptState->executionContext(), holder, 0, 0, scriptState->isolate()); |
| 149 } | 136 } |
| 150 | 137 |
| 151 void PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, String
className, String attributeName, v8::Handle<v8::Value> holder, v8::Handle<v8::V
alue> v8Value) | 138 void PrivateScriptRunner::runDOMAttributeSetter(ScriptState* scriptState, String
className, String attributeName, v8::Handle<v8::Value> holder, v8::Handle<v8::V
alue> v8Value) |
| 152 { | 139 { |
| 153 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); | 140 v8::Handle<v8::Object> classObject = classObjectOfPrivateScript(scriptState,
className); |
| 154 v8::Handle<v8::Object> descriptor = getOwnPropertyDescriptor(scriptState, cl
assObject, attributeName); | 141 v8::Handle<v8::Value> descriptor = classObject->GetOwnPropertyDescriptor(v8S
tring(scriptState->isolate(), attributeName)); |
| 155 v8::Handle<v8::Value> setter = descriptor->Get(v8String(scriptState->isolate
(), "set")); | 142 if (descriptor.IsEmpty() || !descriptor->IsObject()) { |
| 143 FATAL("Private script error: Target DOM attribute setter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); |
| 144 RELEASE_ASSERT_NOT_REACHED(); |
| 145 } |
| 146 v8::Handle<v8::Value> setter = v8::Handle<v8::Object>::Cast(descriptor)->Get
(v8String(scriptState->isolate(), "set")); |
| 156 if (setter.IsEmpty() || !setter->IsFunction()) { | 147 if (setter.IsEmpty() || !setter->IsFunction()) { |
| 157 FATAL("Private script error: Target DOM attribute setter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); | 148 FATAL("Private script error: Target DOM attribute setter was not found.
(Class name = %s, Attribute name = %s)\n", className.utf8().data(), attributeNam
e.utf8().data()); |
| 158 RELEASE_ASSERT_NOT_REACHED(); | 149 RELEASE_ASSERT_NOT_REACHED(); |
| 159 } | 150 } |
| 160 initializeHolderIfNeeded(scriptState, classObject, holder); | 151 initializeHolderIfNeeded(scriptState, classObject, holder); |
| 161 v8::Handle<v8::Value> argv[] = { v8Value }; | 152 v8::Handle<v8::Value> argv[] = { v8Value }; |
| 162 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(setter), scriptS
tate->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, scriptState->iso
late()); | 153 V8ScriptRunner::callFunction(v8::Handle<v8::Function>::Cast(setter), scriptS
tate->executionContext(), holder, WTF_ARRAY_LENGTH(argv), argv, scriptState->iso
late()); |
| 163 } | 154 } |
| 164 | 155 |
| 165 v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState
, String className, String methodName, v8::Handle<v8::Value> holder, int argc, v
8::Handle<v8::Value> argv[]) | 156 v8::Handle<v8::Value> PrivateScriptRunner::runDOMMethod(ScriptState* scriptState
, String className, String methodName, v8::Handle<v8::Value> holder, int argc, v
8::Handle<v8::Value> argv[]) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 190 RELEASE_ASSERT(!message.IsEmpty() && message->IsString()); | 181 RELEASE_ASSERT(!message.IsEmpty() && message->IsString()); |
| 191 v8::Handle<v8::Value> code = exceptionObject->Get(v8String(isolate, "code"))
; | 182 v8::Handle<v8::Value> code = exceptionObject->Get(v8String(isolate, "code"))
; |
| 192 RELEASE_ASSERT(!code.IsEmpty() && code->IsInt32()); | 183 RELEASE_ASSERT(!code.IsEmpty() && code->IsInt32()); |
| 193 // FIXME: Support JavaScript errors such as TypeError, RangeError and Securi
tyError. | 184 // FIXME: Support JavaScript errors such as TypeError, RangeError and Securi
tyError. |
| 194 exceptionState.throwDOMException(toInt32(code), toCoreString(v8::Handle<v8::
String>::Cast(message))); | 185 exceptionState.throwDOMException(toInt32(code), toCoreString(v8::Handle<v8::
String>::Cast(message))); |
| 195 exceptionState.throwIfNeeded(); | 186 exceptionState.throwIfNeeded(); |
| 196 return true; | 187 return true; |
| 197 } | 188 } |
| 198 | 189 |
| 199 } // namespace WebCore | 190 } // namespace WebCore |
| OLD | NEW |