| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "fxjse/include/cfxjse_class.h" | 7 #include "fxjs/include/cfxjse_class.h" |
| 8 | 8 |
| 9 #include "fxjse/context.h" | 9 #include "fxjs/include/cfxjse_context.h" |
| 10 #include "fxjse/include/cfxjse_arguments.h" | 10 #include "fxjs/include/cfxjse_value.h" |
| 11 #include "fxjse/include/cfxjse_value.h" | |
| 12 #include "fxjse/scope_inline.h" | |
| 13 | 11 |
| 14 static void FXJSE_V8ConstructorCallback_Wrapper( | 12 namespace { |
| 15 const v8::FunctionCallbackInfo<v8::Value>& info); | |
| 16 static void FXJSE_V8FunctionCallback_Wrapper( | |
| 17 const v8::FunctionCallbackInfo<v8::Value>& info); | |
| 18 static void FXJSE_V8GetterCallback_Wrapper( | |
| 19 v8::Local<v8::String> property, | |
| 20 const v8::PropertyCallbackInfo<v8::Value>& info); | |
| 21 static void FXJSE_V8SetterCallback_Wrapper( | |
| 22 v8::Local<v8::String> property, | |
| 23 v8::Local<v8::Value> value, | |
| 24 const v8::PropertyCallbackInfo<void>& info); | |
| 25 | 13 |
| 26 static void FXJSE_V8FunctionCallback_Wrapper( | 14 void V8FunctionCallback_Wrapper( |
| 27 const v8::FunctionCallbackInfo<v8::Value>& info) { | 15 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 28 const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo = | 16 const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo = |
| 29 static_cast<FXJSE_FUNCTION_DESCRIPTOR*>( | 17 static_cast<FXJSE_FUNCTION_DESCRIPTOR*>( |
| 30 info.Data().As<v8::External>()->Value()); | 18 info.Data().As<v8::External>()->Value()); |
| 31 if (!lpFunctionInfo) { | 19 if (!lpFunctionInfo) |
| 32 return; | 20 return; |
| 33 } | 21 |
| 34 CFX_ByteStringC szFunctionName(lpFunctionInfo->name); | 22 CFX_ByteStringC szFunctionName(lpFunctionInfo->name); |
| 35 std::unique_ptr<CFXJSE_Value> lpThisValue( | 23 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 36 new CFXJSE_Value(info.GetIsolate())); | 24 new CFXJSE_Value(info.GetIsolate())); |
| 37 lpThisValue->ForceSetValue(info.This()); | 25 lpThisValue->ForceSetValue(info.This()); |
| 38 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); | 26 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); |
| 39 CFXJSE_Arguments impl(&info, lpRetValue.get()); | 27 CFXJSE_Arguments impl(&info, lpRetValue.get()); |
| 40 lpFunctionInfo->callbackProc(lpThisValue.get(), szFunctionName, impl); | 28 lpFunctionInfo->callbackProc(lpThisValue.get(), szFunctionName, impl); |
| 41 if (!lpRetValue->DirectGetValue().IsEmpty()) { | 29 if (!lpRetValue->DirectGetValue().IsEmpty()) |
| 42 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | 30 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); |
| 43 } | |
| 44 } | 31 } |
| 45 | 32 |
| 46 static void FXJSE_V8ClassGlobalConstructorCallback_Wrapper( | 33 void V8ClassGlobalConstructorCallback_Wrapper( |
| 47 const v8::FunctionCallbackInfo<v8::Value>& info) { | 34 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 48 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = | 35 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = |
| 49 static_cast<FXJSE_CLASS_DESCRIPTOR*>( | 36 static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 50 info.Data().As<v8::External>()->Value()); | 37 info.Data().As<v8::External>()->Value()); |
| 51 if (!lpClassDefinition) { | 38 if (!lpClassDefinition) |
| 52 return; | 39 return; |
| 53 } | 40 |
| 54 CFX_ByteStringC szFunctionName(lpClassDefinition->name); | 41 CFX_ByteStringC szFunctionName(lpClassDefinition->name); |
| 55 std::unique_ptr<CFXJSE_Value> lpThisValue( | 42 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 56 new CFXJSE_Value(info.GetIsolate())); | 43 new CFXJSE_Value(info.GetIsolate())); |
| 57 lpThisValue->ForceSetValue(info.This()); | 44 lpThisValue->ForceSetValue(info.This()); |
| 58 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); | 45 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); |
| 59 CFXJSE_Arguments impl(&info, lpRetValue.get()); | 46 CFXJSE_Arguments impl(&info, lpRetValue.get()); |
| 60 lpClassDefinition->constructor(lpThisValue.get(), szFunctionName, impl); | 47 lpClassDefinition->constructor(lpThisValue.get(), szFunctionName, impl); |
| 61 if (!lpRetValue->DirectGetValue().IsEmpty()) { | 48 if (!lpRetValue->DirectGetValue().IsEmpty()) |
| 62 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); | 49 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); |
| 63 } | |
| 64 } | 50 } |
| 65 | 51 |
| 66 static void FXJSE_V8GetterCallback_Wrapper( | 52 void V8GetterCallback_Wrapper(v8::Local<v8::String> property, |
| 67 v8::Local<v8::String> property, | 53 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 68 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
| 69 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = | 54 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = |
| 70 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | 55 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( |
| 71 info.Data().As<v8::External>()->Value()); | 56 info.Data().As<v8::External>()->Value()); |
| 72 if (!lpPropertyInfo) { | 57 if (!lpPropertyInfo) |
| 73 return; | 58 return; |
| 74 } | 59 |
| 75 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | 60 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); |
| 76 std::unique_ptr<CFXJSE_Value> lpThisValue( | 61 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 77 new CFXJSE_Value(info.GetIsolate())); | 62 new CFXJSE_Value(info.GetIsolate())); |
| 78 std::unique_ptr<CFXJSE_Value> lpPropValue( | 63 std::unique_ptr<CFXJSE_Value> lpPropValue( |
| 79 new CFXJSE_Value(info.GetIsolate())); | 64 new CFXJSE_Value(info.GetIsolate())); |
| 80 lpThisValue->ForceSetValue(info.This()); | 65 lpThisValue->ForceSetValue(info.This()); |
| 81 lpPropertyInfo->getProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); | 66 lpPropertyInfo->getProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); |
| 82 info.GetReturnValue().Set(lpPropValue->DirectGetValue()); | 67 info.GetReturnValue().Set(lpPropValue->DirectGetValue()); |
| 83 } | 68 } |
| 84 | 69 |
| 85 static void FXJSE_V8SetterCallback_Wrapper( | 70 void V8SetterCallback_Wrapper(v8::Local<v8::String> property, |
| 86 v8::Local<v8::String> property, | 71 v8::Local<v8::Value> value, |
| 87 v8::Local<v8::Value> value, | 72 const v8::PropertyCallbackInfo<void>& info) { |
| 88 const v8::PropertyCallbackInfo<void>& info) { | |
| 89 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = | 73 const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = |
| 90 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | 74 static_cast<FXJSE_PROPERTY_DESCRIPTOR*>( |
| 91 info.Data().As<v8::External>()->Value()); | 75 info.Data().As<v8::External>()->Value()); |
| 92 if (!lpPropertyInfo) { | 76 if (!lpPropertyInfo) |
| 93 return; | 77 return; |
| 94 } | 78 |
| 95 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); | 79 CFX_ByteStringC szPropertyName(lpPropertyInfo->name); |
| 96 std::unique_ptr<CFXJSE_Value> lpThisValue( | 80 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 97 new CFXJSE_Value(info.GetIsolate())); | 81 new CFXJSE_Value(info.GetIsolate())); |
| 98 std::unique_ptr<CFXJSE_Value> lpPropValue( | 82 std::unique_ptr<CFXJSE_Value> lpPropValue( |
| 99 new CFXJSE_Value(info.GetIsolate())); | 83 new CFXJSE_Value(info.GetIsolate())); |
| 100 lpThisValue->ForceSetValue(info.This()); | 84 lpThisValue->ForceSetValue(info.This()); |
| 101 lpPropValue->ForceSetValue(value); | 85 lpPropValue->ForceSetValue(value); |
| 102 lpPropertyInfo->setProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); | 86 lpPropertyInfo->setProc(lpThisValue.get(), szPropertyName, lpPropValue.get()); |
| 103 } | 87 } |
| 104 | 88 |
| 105 static void FXJSE_V8ConstructorCallback_Wrapper( | 89 void V8ConstructorCallback_Wrapper( |
| 106 const v8::FunctionCallbackInfo<v8::Value>& info) { | 90 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 107 if (!info.IsConstructCall()) { | 91 if (!info.IsConstructCall()) |
| 108 return; | 92 return; |
| 109 } | 93 |
| 110 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = | 94 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = |
| 111 static_cast<FXJSE_CLASS_DESCRIPTOR*>( | 95 static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 112 info.Data().As<v8::External>()->Value()); | 96 info.Data().As<v8::External>()->Value()); |
| 113 if (!lpClassDefinition) { | 97 if (!lpClassDefinition) |
| 114 return; | 98 return; |
| 115 } | 99 |
| 116 ASSERT(info.This()->InternalFieldCount()); | 100 ASSERT(info.This()->InternalFieldCount()); |
| 117 info.This()->SetAlignedPointerInInternalField(0, NULL); | 101 info.This()->SetAlignedPointerInInternalField(0, nullptr); |
| 118 } | 102 } |
| 119 | 103 |
| 120 v8::Isolate* CFXJSE_Arguments::GetRuntime() const { | 104 void Context_GlobalObjToString( |
| 121 return m_pRetValue->GetIsolate(); | |
| 122 } | |
| 123 | |
| 124 int32_t CFXJSE_Arguments::GetLength() const { | |
| 125 return m_pInfo->Length(); | |
| 126 } | |
| 127 | |
| 128 std::unique_ptr<CFXJSE_Value> CFXJSE_Arguments::GetValue(int32_t index) const { | |
| 129 std::unique_ptr<CFXJSE_Value> lpArgValue( | |
| 130 new CFXJSE_Value(v8::Isolate::GetCurrent())); | |
| 131 lpArgValue->ForceSetValue((*m_pInfo)[index]); | |
| 132 return lpArgValue; | |
| 133 } | |
| 134 | |
| 135 FX_BOOL CFXJSE_Arguments::GetBoolean(int32_t index) const { | |
| 136 return (*m_pInfo)[index]->BooleanValue(); | |
| 137 } | |
| 138 | |
| 139 int32_t CFXJSE_Arguments::GetInt32(int32_t index) const { | |
| 140 return static_cast<int32_t>((*m_pInfo)[index]->NumberValue()); | |
| 141 } | |
| 142 | |
| 143 FX_FLOAT CFXJSE_Arguments::GetFloat(int32_t index) const { | |
| 144 return static_cast<FX_FLOAT>((*m_pInfo)[index]->NumberValue()); | |
| 145 } | |
| 146 | |
| 147 CFX_ByteString CFXJSE_Arguments::GetUTF8String(int32_t index) const { | |
| 148 v8::Local<v8::String> hString = (*m_pInfo)[index]->ToString(); | |
| 149 v8::String::Utf8Value szStringVal(hString); | |
| 150 return CFX_ByteString(*szStringVal); | |
| 151 } | |
| 152 | |
| 153 CFXJSE_HostObject* CFXJSE_Arguments::GetObject(int32_t index, | |
| 154 CFXJSE_Class* pClass) const { | |
| 155 v8::Local<v8::Value> hValue = (*m_pInfo)[index]; | |
| 156 ASSERT(!hValue.IsEmpty()); | |
| 157 if (!hValue->IsObject()) | |
| 158 return nullptr; | |
| 159 return FXJSE_RetrieveObjectBinding(hValue.As<v8::Object>(), pClass); | |
| 160 } | |
| 161 | |
| 162 CFXJSE_Value* CFXJSE_Arguments::GetReturnValue() { | |
| 163 return m_pRetValue; | |
| 164 } | |
| 165 | |
| 166 static void FXJSE_Context_GlobalObjToString( | |
| 167 const v8::FunctionCallbackInfo<v8::Value>& info) { | 105 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 168 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( | 106 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 169 info.Data().As<v8::External>()->Value()); | 107 info.Data().As<v8::External>()->Value()); |
| 170 if (!lpClass) { | 108 if (!lpClass) |
| 171 return; | 109 return; |
| 172 } | 110 |
| 173 if (info.This() == info.Holder() && lpClass->name) { | 111 if (info.This() == info.Holder() && lpClass->name) { |
| 174 CFX_ByteString szStringVal; | 112 CFX_ByteString szStringVal; |
| 175 szStringVal.Format("[object %s]", lpClass->name); | 113 szStringVal.Format("[object %s]", lpClass->name); |
| 176 info.GetReturnValue().Set(v8::String::NewFromUtf8( | 114 info.GetReturnValue().Set(v8::String::NewFromUtf8( |
| 177 info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString, | 115 info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString, |
| 178 szStringVal.GetLength())); | 116 szStringVal.GetLength())); |
| 179 } else { | 117 return; |
| 180 v8::Local<v8::String> local_str = | 118 } |
| 181 info.This() | 119 v8::Local<v8::String> local_str = |
| 182 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) | 120 info.This() |
| 183 .FromMaybe(v8::Local<v8::String>()); | 121 ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) |
| 184 info.GetReturnValue().Set(local_str); | 122 .FromMaybe(v8::Local<v8::String>()); |
| 185 } | 123 info.GetReturnValue().Set(local_str); |
| 186 } | 124 } |
| 187 | 125 |
| 126 void DynPropGetterAdapter_MethodCallback( |
| 127 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 128 v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>(); |
| 129 FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 130 hCallBackInfo->GetAlignedPointerFromInternalField(0)); |
| 131 v8::Local<v8::String> hPropName = |
| 132 hCallBackInfo->GetInternalField(1).As<v8::String>(); |
| 133 ASSERT(lpClass && !hPropName.IsEmpty()); |
| 134 v8::String::Utf8Value szPropName(hPropName); |
| 135 CFX_ByteStringC szFxPropName = *szPropName; |
| 136 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 137 new CFXJSE_Value(info.GetIsolate())); |
| 138 lpThisValue->ForceSetValue(info.This()); |
| 139 std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); |
| 140 CFXJSE_Arguments impl(&info, lpRetValue.get()); |
| 141 lpClass->dynMethodCall(lpThisValue.get(), szFxPropName, impl); |
| 142 if (!lpRetValue->DirectGetValue().IsEmpty()) |
| 143 info.GetReturnValue().Set(lpRetValue->DirectGetValue()); |
| 144 } |
| 145 |
| 146 void DynPropGetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |
| 147 CFXJSE_Value* pObject, |
| 148 const CFX_ByteStringC& szPropName, |
| 149 CFXJSE_Value* pValue) { |
| 150 ASSERT(lpClass); |
| 151 int32_t nPropType = |
| 152 lpClass->dynPropTypeGetter == nullptr |
| 153 ? FXJSE_ClassPropType_Property |
| 154 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |
| 155 if (nPropType == FXJSE_ClassPropType_Property) { |
| 156 if (lpClass->dynPropGetter) |
| 157 lpClass->dynPropGetter(pObject, szPropName, pValue); |
| 158 } else if (nPropType == FXJSE_ClassPropType_Method) { |
| 159 if (lpClass->dynMethodCall && pValue) { |
| 160 v8::Isolate* pIsolate = pValue->GetIsolate(); |
| 161 v8::HandleScope hscope(pIsolate); |
| 162 v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate = |
| 163 v8::ObjectTemplate::New(pIsolate); |
| 164 hCallBackInfoTemplate->SetInternalFieldCount(2); |
| 165 v8::Local<v8::Object> hCallBackInfo = |
| 166 hCallBackInfoTemplate->NewInstance(); |
| 167 hCallBackInfo->SetAlignedPointerInInternalField( |
| 168 0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClass)); |
| 169 hCallBackInfo->SetInternalField( |
| 170 1, v8::String::NewFromUtf8( |
| 171 pIsolate, reinterpret_cast<const char*>(szPropName.raw_str()), |
| 172 v8::String::kNormalString, szPropName.GetLength())); |
| 173 pValue->ForceSetValue( |
| 174 v8::Function::New(pValue->GetIsolate()->GetCurrentContext(), |
| 175 DynPropGetterAdapter_MethodCallback, hCallBackInfo, |
| 176 0, v8::ConstructorBehavior::kThrow) |
| 177 .ToLocalChecked()); |
| 178 } |
| 179 } |
| 180 } |
| 181 |
| 182 void DynPropSetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |
| 183 CFXJSE_Value* pObject, |
| 184 const CFX_ByteStringC& szPropName, |
| 185 CFXJSE_Value* pValue) { |
| 186 ASSERT(lpClass); |
| 187 int32_t nPropType = |
| 188 lpClass->dynPropTypeGetter == nullptr |
| 189 ? FXJSE_ClassPropType_Property |
| 190 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |
| 191 if (nPropType != FXJSE_ClassPropType_Method) { |
| 192 if (lpClass->dynPropSetter) |
| 193 lpClass->dynPropSetter(pObject, szPropName, pValue); |
| 194 } |
| 195 } |
| 196 |
| 197 FX_BOOL DynPropQueryAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |
| 198 CFXJSE_Value* pObject, |
| 199 const CFX_ByteStringC& szPropName) { |
| 200 ASSERT(lpClass); |
| 201 int32_t nPropType = |
| 202 lpClass->dynPropTypeGetter == nullptr |
| 203 ? FXJSE_ClassPropType_Property |
| 204 : lpClass->dynPropTypeGetter(pObject, szPropName, TRUE); |
| 205 return nPropType != FXJSE_ClassPropType_None; |
| 206 } |
| 207 |
| 208 FX_BOOL DynPropDeleterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |
| 209 CFXJSE_Value* pObject, |
| 210 const CFX_ByteStringC& szPropName) { |
| 211 ASSERT(lpClass); |
| 212 int32_t nPropType = |
| 213 lpClass->dynPropTypeGetter == nullptr |
| 214 ? FXJSE_ClassPropType_Property |
| 215 : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |
| 216 if (nPropType != FXJSE_ClassPropType_Method) { |
| 217 if (lpClass->dynPropDeleter) |
| 218 return lpClass->dynPropDeleter(pObject, szPropName); |
| 219 return nPropType == FXJSE_ClassPropType_Property ? FALSE : TRUE; |
| 220 } |
| 221 return FALSE; |
| 222 } |
| 223 |
| 224 void NamedPropertyQueryCallback( |
| 225 v8::Local<v8::Name> property, |
| 226 const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 227 v8::Local<v8::Object> thisObject = info.This(); |
| 228 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 229 info.Data().As<v8::External>()->Value()); |
| 230 v8::Isolate* pIsolate = info.GetIsolate(); |
| 231 v8::HandleScope scope(pIsolate); |
| 232 v8::String::Utf8Value szPropName(property); |
| 233 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |
| 234 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 235 new CFXJSE_Value(info.GetIsolate())); |
| 236 lpThisValue->ForceSetValue(thisObject); |
| 237 if (DynPropQueryAdapter(lpClass, lpThisValue.get(), szFxPropName)) { |
| 238 info.GetReturnValue().Set(v8::DontDelete); |
| 239 return; |
| 240 } |
| 241 const int32_t iV8Absent = 64; |
| 242 info.GetReturnValue().Set(iV8Absent); |
| 243 } |
| 244 |
| 245 void NamedPropertyDeleterCallback( |
| 246 v8::Local<v8::Name> property, |
| 247 const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
| 248 v8::Local<v8::Object> thisObject = info.This(); |
| 249 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 250 info.Data().As<v8::External>()->Value()); |
| 251 v8::Isolate* pIsolate = info.GetIsolate(); |
| 252 v8::HandleScope scope(pIsolate); |
| 253 v8::String::Utf8Value szPropName(property); |
| 254 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |
| 255 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 256 new CFXJSE_Value(info.GetIsolate())); |
| 257 lpThisValue->ForceSetValue(thisObject); |
| 258 info.GetReturnValue().Set( |
| 259 !!DynPropDeleterAdapter(lpClass, lpThisValue.get(), szFxPropName)); |
| 260 } |
| 261 |
| 262 void NamedPropertyGetterCallback( |
| 263 v8::Local<v8::Name> property, |
| 264 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 265 v8::Local<v8::Object> thisObject = info.This(); |
| 266 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 267 info.Data().As<v8::External>()->Value()); |
| 268 v8::String::Utf8Value szPropName(property); |
| 269 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |
| 270 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 271 new CFXJSE_Value(info.GetIsolate())); |
| 272 lpThisValue->ForceSetValue(thisObject); |
| 273 std::unique_ptr<CFXJSE_Value> lpNewValue(new CFXJSE_Value(info.GetIsolate())); |
| 274 DynPropGetterAdapter(lpClass, lpThisValue.get(), szFxPropName, |
| 275 lpNewValue.get()); |
| 276 info.GetReturnValue().Set(lpNewValue->DirectGetValue()); |
| 277 } |
| 278 |
| 279 void NamedPropertySetterCallback( |
| 280 v8::Local<v8::Name> property, |
| 281 v8::Local<v8::Value> value, |
| 282 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 283 v8::Local<v8::Object> thisObject = info.This(); |
| 284 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 285 info.Data().As<v8::External>()->Value()); |
| 286 v8::String::Utf8Value szPropName(property); |
| 287 CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |
| 288 std::unique_ptr<CFXJSE_Value> lpThisValue( |
| 289 new CFXJSE_Value(info.GetIsolate())); |
| 290 lpThisValue->ForceSetValue(thisObject); |
| 291 |
| 292 CFXJSE_Value* lpNewValue = new CFXJSE_Value(info.GetIsolate()); |
| 293 lpNewValue->ForceSetValue(value); |
| 294 DynPropSetterAdapter(lpClass, lpThisValue.get(), szFxPropName, lpNewValue); |
| 295 info.GetReturnValue().Set(value); |
| 296 } |
| 297 |
| 298 void NamedPropertyEnumeratorCallback( |
| 299 const v8::PropertyCallbackInfo<v8::Array>& info) { |
| 300 const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 301 info.Data().As<v8::External>()->Value()); |
| 302 v8::Isolate* pIsolate = info.GetIsolate(); |
| 303 v8::Local<v8::Array> newArray = v8::Array::New(pIsolate, lpClass->propNum); |
| 304 for (int i = 0; i < lpClass->propNum; i++) { |
| 305 newArray->Set( |
| 306 i, v8::String::NewFromUtf8(pIsolate, lpClass->properties[i].name)); |
| 307 } |
| 308 info.GetReturnValue().Set(newArray); |
| 309 } |
| 310 |
| 311 } // namespace |
| 312 |
| 313 // static |
| 188 CFXJSE_Class* CFXJSE_Class::Create( | 314 CFXJSE_Class* CFXJSE_Class::Create( |
| 189 CFXJSE_Context* lpContext, | 315 CFXJSE_Context* lpContext, |
| 190 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition, | 316 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition, |
| 191 FX_BOOL bIsJSGlobal) { | 317 FX_BOOL bIsJSGlobal) { |
| 192 if (!lpContext || !lpClassDefinition) { | 318 if (!lpContext || !lpClassDefinition) |
| 193 return NULL; | 319 return nullptr; |
| 194 } | 320 |
| 195 CFXJSE_Class* pClass = | 321 CFXJSE_Class* pClass = |
| 196 GetClassFromContext(lpContext, lpClassDefinition->name); | 322 GetClassFromContext(lpContext, lpClassDefinition->name); |
| 197 if (pClass) { | 323 if (pClass) |
| 198 return pClass; | 324 return pClass; |
| 199 } | 325 |
| 200 v8::Isolate* pIsolate = lpContext->m_pIsolate; | 326 v8::Isolate* pIsolate = lpContext->m_pIsolate; |
| 201 pClass = new CFXJSE_Class(lpContext); | 327 pClass = new CFXJSE_Class(lpContext); |
| 202 pClass->m_szClassName = lpClassDefinition->name; | 328 pClass->m_szClassName = lpClassDefinition->name; |
| 203 pClass->m_lpClassDefinition = lpClassDefinition; | 329 pClass->m_lpClassDefinition = lpClassDefinition; |
| 204 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); | 330 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate); |
| 205 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New( | 331 v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New( |
| 206 pIsolate, bIsJSGlobal ? 0 : FXJSE_V8ConstructorCallback_Wrapper, | 332 pIsolate, bIsJSGlobal ? 0 : V8ConstructorCallback_Wrapper, |
| 207 v8::External::New( | 333 v8::External::New( |
| 208 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); | 334 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); |
| 209 hFunctionTemplate->SetClassName( | 335 hFunctionTemplate->SetClassName( |
| 210 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); | 336 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); |
| 211 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); | 337 hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); |
| 212 v8::Local<v8::ObjectTemplate> hObjectTemplate = | 338 v8::Local<v8::ObjectTemplate> hObjectTemplate = |
| 213 hFunctionTemplate->InstanceTemplate(); | 339 hFunctionTemplate->InstanceTemplate(); |
| 214 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition); | 340 SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition); |
| 215 | 341 |
| 216 if (lpClassDefinition->propNum) { | 342 if (lpClassDefinition->propNum) { |
| 217 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) { | 343 for (int32_t i = 0; i < lpClassDefinition->propNum; i++) { |
| 218 hObjectTemplate->SetNativeDataProperty( | 344 hObjectTemplate->SetNativeDataProperty( |
| 219 v8::String::NewFromUtf8(pIsolate, | 345 v8::String::NewFromUtf8(pIsolate, |
| 220 lpClassDefinition->properties[i].name), | 346 lpClassDefinition->properties[i].name), |
| 221 lpClassDefinition->properties[i].getProc | 347 lpClassDefinition->properties[i].getProc ? V8GetterCallback_Wrapper |
| 222 ? FXJSE_V8GetterCallback_Wrapper | 348 : nullptr, |
| 223 : NULL, | 349 lpClassDefinition->properties[i].setProc ? V8SetterCallback_Wrapper |
| 224 lpClassDefinition->properties[i].setProc | 350 : nullptr, |
| 225 ? FXJSE_V8SetterCallback_Wrapper | |
| 226 : NULL, | |
| 227 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>( | 351 v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>( |
| 228 lpClassDefinition->properties + i)), | 352 lpClassDefinition->properties + i)), |
| 229 static_cast<v8::PropertyAttribute>(v8::DontDelete)); | 353 static_cast<v8::PropertyAttribute>(v8::DontDelete)); |
| 230 } | 354 } |
| 231 } | 355 } |
| 232 if (lpClassDefinition->methNum) { | 356 if (lpClassDefinition->methNum) { |
| 233 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { | 357 for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { |
| 234 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( | 358 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( |
| 235 pIsolate, FXJSE_V8FunctionCallback_Wrapper, | 359 pIsolate, V8FunctionCallback_Wrapper, |
| 236 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>( | 360 v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>( |
| 237 lpClassDefinition->methods + i))); | 361 lpClassDefinition->methods + i))); |
| 238 fun->RemovePrototype(); | 362 fun->RemovePrototype(); |
| 239 hObjectTemplate->Set( | 363 hObjectTemplate->Set( |
| 240 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), | 364 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), |
| 241 fun, | 365 fun, |
| 242 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | 366 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); |
| 243 } | 367 } |
| 244 } | 368 } |
| 245 if (lpClassDefinition->constructor) { | 369 if (lpClassDefinition->constructor) { |
| 246 if (bIsJSGlobal) { | 370 if (bIsJSGlobal) { |
| 247 hObjectTemplate->Set( | 371 hObjectTemplate->Set( |
| 248 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | 372 v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), |
| 249 v8::FunctionTemplate::New( | 373 v8::FunctionTemplate::New( |
| 250 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | 374 pIsolate, V8ClassGlobalConstructorCallback_Wrapper, |
| 251 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>( | 375 v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 252 lpClassDefinition))), | 376 lpClassDefinition))), |
| 253 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); | 377 static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); |
| 254 } else { | 378 } else { |
| 255 v8::Local<v8::Context> hLocalContext = | 379 v8::Local<v8::Context> hLocalContext = |
| 256 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext); | 380 v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext); |
| 257 FXJSE_GetGlobalObjectFromContext(hLocalContext) | 381 FXJSE_GetGlobalObjectFromContext(hLocalContext) |
| 258 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), | 382 ->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), |
| 259 v8::Function::New( | 383 v8::Function::New( |
| 260 pIsolate, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, | 384 pIsolate, V8ClassGlobalConstructorCallback_Wrapper, |
| 261 v8::External::New(pIsolate, | 385 v8::External::New(pIsolate, |
| 262 const_cast<FXJSE_CLASS_DESCRIPTOR*>( | 386 const_cast<FXJSE_CLASS_DESCRIPTOR*>( |
| 263 lpClassDefinition)))); | 387 lpClassDefinition)))); |
| 264 } | 388 } |
| 265 } | 389 } |
| 266 if (bIsJSGlobal) { | 390 if (bIsJSGlobal) { |
| 267 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( | 391 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( |
| 268 pIsolate, FXJSE_Context_GlobalObjToString, | 392 pIsolate, Context_GlobalObjToString, |
| 269 v8::External::New( | 393 v8::External::New( |
| 270 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); | 394 pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition))); |
| 271 fun->RemovePrototype(); | 395 fun->RemovePrototype(); |
| 272 hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun); | 396 hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun); |
| 273 } | 397 } |
| 274 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); | 398 pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); |
| 275 lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass)); | 399 lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass)); |
| 276 return pClass; | 400 return pClass; |
| 277 } | 401 } |
| 278 | 402 |
| 279 CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext) | 403 // static |
| 280 : m_lpClassDefinition(nullptr), m_pContext(lpContext) {} | |
| 281 | |
| 282 CFXJSE_Class::~CFXJSE_Class() {} | |
| 283 | |
| 284 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext, | 404 CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext, |
| 285 const CFX_ByteStringC& szName) { | 405 const CFX_ByteStringC& szName) { |
| 286 for (const auto& pClass : pContext->m_rgClasses) { | 406 for (const auto& pClass : pContext->m_rgClasses) { |
| 287 if (pClass->m_szClassName == szName) | 407 if (pClass->m_szClassName == szName) |
| 288 return pClass.get(); | 408 return pClass.get(); |
| 289 } | 409 } |
| 290 return nullptr; | 410 return nullptr; |
| 291 } | 411 } |
| 412 |
| 413 // static |
| 414 void CFXJSE_Class::SetUpNamedPropHandler( |
| 415 v8::Isolate* pIsolate, |
| 416 v8::Local<v8::ObjectTemplate>& hObjectTemplate, |
| 417 const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition) { |
| 418 v8::NamedPropertyHandlerConfiguration configuration( |
| 419 lpClassDefinition->dynPropGetter ? NamedPropertyGetterCallback : 0, |
| 420 lpClassDefinition->dynPropSetter ? NamedPropertySetterCallback : 0, |
| 421 lpClassDefinition->dynPropTypeGetter ? NamedPropertyQueryCallback : 0, |
| 422 lpClassDefinition->dynPropDeleter ? NamedPropertyDeleterCallback : 0, |
| 423 NamedPropertyEnumeratorCallback, |
| 424 v8::External::New(pIsolate, |
| 425 const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)), |
| 426 v8::PropertyHandlerFlags::kNonMasking); |
| 427 hObjectTemplate->SetHandler(configuration); |
| 428 } |
| 429 |
| 430 CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext) |
| 431 : m_lpClassDefinition(nullptr), m_pContext(lpContext) {} |
| 432 |
| 433 CFXJSE_Class::~CFXJSE_Class() {} |
| OLD | NEW |