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