| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright 2014 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  |  | 
|    7 #include "fxjse/include/cfxjse_arguments.h" |  | 
|    8 #include "fxjse/include/cfxjse_class.h" |  | 
|    9 #include "fxjse/include/cfxjse_value.h" |  | 
|   10  |  | 
|   11 static void FXJSE_DynPropGetterAdapter_MethodCallback( |  | 
|   12     const v8::FunctionCallbackInfo<v8::Value>& info) { |  | 
|   13   v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>(); |  | 
|   14   FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|   15       hCallBackInfo->GetAlignedPointerFromInternalField(0)); |  | 
|   16   v8::Local<v8::String> hPropName = |  | 
|   17       hCallBackInfo->GetInternalField(1).As<v8::String>(); |  | 
|   18   ASSERT(lpClass && !hPropName.IsEmpty()); |  | 
|   19   v8::String::Utf8Value szPropName(hPropName); |  | 
|   20   CFX_ByteStringC szFxPropName = *szPropName; |  | 
|   21   std::unique_ptr<CFXJSE_Value> lpThisValue( |  | 
|   22       new CFXJSE_Value(info.GetIsolate())); |  | 
|   23   lpThisValue->ForceSetValue(info.This()); |  | 
|   24   std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate())); |  | 
|   25   CFXJSE_Arguments impl(&info, lpRetValue.get()); |  | 
|   26   lpClass->dynMethodCall(lpThisValue.get(), szFxPropName, impl); |  | 
|   27   if (!lpRetValue->DirectGetValue().IsEmpty()) { |  | 
|   28     info.GetReturnValue().Set(lpRetValue->DirectGetValue()); |  | 
|   29   } |  | 
|   30 } |  | 
|   31  |  | 
|   32 static void FXJSE_DynPropGetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |  | 
|   33                                        CFXJSE_Value* pObject, |  | 
|   34                                        const CFX_ByteStringC& szPropName, |  | 
|   35                                        CFXJSE_Value* pValue) { |  | 
|   36   ASSERT(lpClass); |  | 
|   37   int32_t nPropType = |  | 
|   38       lpClass->dynPropTypeGetter == nullptr |  | 
|   39           ? FXJSE_ClassPropType_Property |  | 
|   40           : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |  | 
|   41   if (nPropType == FXJSE_ClassPropType_Property) { |  | 
|   42     if (lpClass->dynPropGetter) { |  | 
|   43       lpClass->dynPropGetter(pObject, szPropName, pValue); |  | 
|   44     } |  | 
|   45   } else if (nPropType == FXJSE_ClassPropType_Method) { |  | 
|   46     if (lpClass->dynMethodCall && pValue) { |  | 
|   47       v8::Isolate* pIsolate = pValue->GetIsolate(); |  | 
|   48       v8::HandleScope hscope(pIsolate); |  | 
|   49       v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate = |  | 
|   50           v8::ObjectTemplate::New(pIsolate); |  | 
|   51       hCallBackInfoTemplate->SetInternalFieldCount(2); |  | 
|   52       v8::Local<v8::Object> hCallBackInfo = |  | 
|   53           hCallBackInfoTemplate->NewInstance(); |  | 
|   54       hCallBackInfo->SetAlignedPointerInInternalField( |  | 
|   55           0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClass)); |  | 
|   56       hCallBackInfo->SetInternalField( |  | 
|   57           1, v8::String::NewFromUtf8( |  | 
|   58                  pIsolate, reinterpret_cast<const char*>(szPropName.raw_str()), |  | 
|   59                  v8::String::kNormalString, szPropName.GetLength())); |  | 
|   60       pValue->ForceSetValue( |  | 
|   61           v8::Function::New(pValue->GetIsolate()->GetCurrentContext(), |  | 
|   62                             FXJSE_DynPropGetterAdapter_MethodCallback, |  | 
|   63                             hCallBackInfo, 0, v8::ConstructorBehavior::kThrow) |  | 
|   64               .ToLocalChecked()); |  | 
|   65     } |  | 
|   66   } |  | 
|   67 } |  | 
|   68  |  | 
|   69 static void FXJSE_DynPropSetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |  | 
|   70                                        CFXJSE_Value* pObject, |  | 
|   71                                        const CFX_ByteStringC& szPropName, |  | 
|   72                                        CFXJSE_Value* pValue) { |  | 
|   73   ASSERT(lpClass); |  | 
|   74   int32_t nPropType = |  | 
|   75       lpClass->dynPropTypeGetter == nullptr |  | 
|   76           ? FXJSE_ClassPropType_Property |  | 
|   77           : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |  | 
|   78   if (nPropType != FXJSE_ClassPropType_Method) { |  | 
|   79     if (lpClass->dynPropSetter) { |  | 
|   80       lpClass->dynPropSetter(pObject, szPropName, pValue); |  | 
|   81     } |  | 
|   82   } |  | 
|   83 } |  | 
|   84  |  | 
|   85 static FX_BOOL FXJSE_DynPropQueryAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass, |  | 
|   86                                          CFXJSE_Value* pObject, |  | 
|   87                                          const CFX_ByteStringC& szPropName) { |  | 
|   88   ASSERT(lpClass); |  | 
|   89   int32_t nPropType = |  | 
|   90       lpClass->dynPropTypeGetter == nullptr |  | 
|   91           ? FXJSE_ClassPropType_Property |  | 
|   92           : lpClass->dynPropTypeGetter(pObject, szPropName, TRUE); |  | 
|   93   return nPropType != FXJSE_ClassPropType_None; |  | 
|   94 } |  | 
|   95  |  | 
|   96 static FX_BOOL FXJSE_DynPropDeleterAdapter( |  | 
|   97     const FXJSE_CLASS_DESCRIPTOR* lpClass, |  | 
|   98     CFXJSE_Value* pObject, |  | 
|   99     const CFX_ByteStringC& szPropName) { |  | 
|  100   ASSERT(lpClass); |  | 
|  101   int32_t nPropType = |  | 
|  102       lpClass->dynPropTypeGetter == nullptr |  | 
|  103           ? FXJSE_ClassPropType_Property |  | 
|  104           : lpClass->dynPropTypeGetter(pObject, szPropName, FALSE); |  | 
|  105   if (nPropType != FXJSE_ClassPropType_Method) { |  | 
|  106     if (lpClass->dynPropDeleter) { |  | 
|  107       return lpClass->dynPropDeleter(pObject, szPropName); |  | 
|  108     } else { |  | 
|  109       return nPropType == FXJSE_ClassPropType_Property ? FALSE : TRUE; |  | 
|  110     } |  | 
|  111   } |  | 
|  112   return FALSE; |  | 
|  113 } |  | 
|  114  |  | 
|  115 static void FXJSE_V8_GenericNamedPropertyQueryCallback( |  | 
|  116     v8::Local<v8::Name> property, |  | 
|  117     const v8::PropertyCallbackInfo<v8::Integer>& info) { |  | 
|  118   v8::Local<v8::Object> thisObject = info.This(); |  | 
|  119   const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|  120       info.Data().As<v8::External>()->Value()); |  | 
|  121   v8::Isolate* pIsolate = info.GetIsolate(); |  | 
|  122   v8::HandleScope scope(pIsolate); |  | 
|  123   v8::String::Utf8Value szPropName(property); |  | 
|  124   CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |  | 
|  125   std::unique_ptr<CFXJSE_Value> lpThisValue( |  | 
|  126       new CFXJSE_Value(info.GetIsolate())); |  | 
|  127   lpThisValue->ForceSetValue(thisObject); |  | 
|  128   if (FXJSE_DynPropQueryAdapter(lpClass, lpThisValue.get(), szFxPropName)) { |  | 
|  129     info.GetReturnValue().Set(v8::DontDelete); |  | 
|  130   } else { |  | 
|  131     const int32_t iV8Absent = 64; |  | 
|  132     info.GetReturnValue().Set(iV8Absent); |  | 
|  133   } |  | 
|  134 } |  | 
|  135  |  | 
|  136 static void FXJSE_V8_GenericNamedPropertyDeleterCallback( |  | 
|  137     v8::Local<v8::Name> property, |  | 
|  138     const v8::PropertyCallbackInfo<v8::Boolean>& info) { |  | 
|  139   v8::Local<v8::Object> thisObject = info.This(); |  | 
|  140   const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|  141       info.Data().As<v8::External>()->Value()); |  | 
|  142   v8::Isolate* pIsolate = info.GetIsolate(); |  | 
|  143   v8::HandleScope scope(pIsolate); |  | 
|  144   v8::String::Utf8Value szPropName(property); |  | 
|  145   CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |  | 
|  146   std::unique_ptr<CFXJSE_Value> lpThisValue( |  | 
|  147       new CFXJSE_Value(info.GetIsolate())); |  | 
|  148   lpThisValue->ForceSetValue(thisObject); |  | 
|  149   info.GetReturnValue().Set( |  | 
|  150       !!FXJSE_DynPropDeleterAdapter(lpClass, lpThisValue.get(), szFxPropName)); |  | 
|  151 } |  | 
|  152  |  | 
|  153 static void FXJSE_V8_GenericNamedPropertyGetterCallback( |  | 
|  154     v8::Local<v8::Name> property, |  | 
|  155     const v8::PropertyCallbackInfo<v8::Value>& info) { |  | 
|  156   v8::Local<v8::Object> thisObject = info.This(); |  | 
|  157   const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|  158       info.Data().As<v8::External>()->Value()); |  | 
|  159   v8::String::Utf8Value szPropName(property); |  | 
|  160   CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |  | 
|  161   std::unique_ptr<CFXJSE_Value> lpThisValue( |  | 
|  162       new CFXJSE_Value(info.GetIsolate())); |  | 
|  163   lpThisValue->ForceSetValue(thisObject); |  | 
|  164   std::unique_ptr<CFXJSE_Value> lpNewValue(new CFXJSE_Value(info.GetIsolate())); |  | 
|  165   FXJSE_DynPropGetterAdapter(lpClass, lpThisValue.get(), szFxPropName, |  | 
|  166                              lpNewValue.get()); |  | 
|  167   info.GetReturnValue().Set(lpNewValue->DirectGetValue()); |  | 
|  168 } |  | 
|  169  |  | 
|  170 static void FXJSE_V8_GenericNamedPropertySetterCallback( |  | 
|  171     v8::Local<v8::Name> property, |  | 
|  172     v8::Local<v8::Value> value, |  | 
|  173     const v8::PropertyCallbackInfo<v8::Value>& info) { |  | 
|  174   v8::Local<v8::Object> thisObject = info.This(); |  | 
|  175   const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|  176       info.Data().As<v8::External>()->Value()); |  | 
|  177   v8::String::Utf8Value szPropName(property); |  | 
|  178   CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); |  | 
|  179   std::unique_ptr<CFXJSE_Value> lpThisValue( |  | 
|  180       new CFXJSE_Value(info.GetIsolate())); |  | 
|  181   lpThisValue->ForceSetValue(thisObject); |  | 
|  182  |  | 
|  183   CFXJSE_Value* lpNewValue = new CFXJSE_Value(info.GetIsolate()); |  | 
|  184   lpNewValue->ForceSetValue(value); |  | 
|  185   FXJSE_DynPropSetterAdapter(lpClass, lpThisValue.get(), szFxPropName, |  | 
|  186                              lpNewValue); |  | 
|  187   info.GetReturnValue().Set(value); |  | 
|  188 } |  | 
|  189  |  | 
|  190 static void FXJSE_V8_GenericNamedPropertyEnumeratorCallback( |  | 
|  191     const v8::PropertyCallbackInfo<v8::Array>& info) { |  | 
|  192   const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>( |  | 
|  193       info.Data().As<v8::External>()->Value()); |  | 
|  194   v8::Isolate* pIsolate = info.GetIsolate(); |  | 
|  195   v8::Local<v8::Array> newArray = v8::Array::New(pIsolate, lpClass->propNum); |  | 
|  196   for (int i = 0; i < lpClass->propNum; i++) { |  | 
|  197     newArray->Set( |  | 
|  198         i, v8::String::NewFromUtf8(pIsolate, lpClass->properties[i].name)); |  | 
|  199   } |  | 
|  200   info.GetReturnValue().Set(newArray); |  | 
|  201 } |  | 
|  202  |  | 
|  203 void CFXJSE_Class::SetUpNamedPropHandler( |  | 
|  204     v8::Isolate* pIsolate, |  | 
|  205     v8::Local<v8::ObjectTemplate>& hObjectTemplate, |  | 
|  206     const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition) { |  | 
|  207   v8::NamedPropertyHandlerConfiguration configuration( |  | 
|  208       lpClassDefinition->dynPropGetter |  | 
|  209           ? FXJSE_V8_GenericNamedPropertyGetterCallback |  | 
|  210           : 0, |  | 
|  211       lpClassDefinition->dynPropSetter |  | 
|  212           ? FXJSE_V8_GenericNamedPropertySetterCallback |  | 
|  213           : 0, |  | 
|  214       lpClassDefinition->dynPropTypeGetter |  | 
|  215           ? FXJSE_V8_GenericNamedPropertyQueryCallback |  | 
|  216           : 0, |  | 
|  217       lpClassDefinition->dynPropDeleter |  | 
|  218           ? FXJSE_V8_GenericNamedPropertyDeleterCallback |  | 
|  219           : 0, |  | 
|  220       FXJSE_V8_GenericNamedPropertyEnumeratorCallback, |  | 
|  221       v8::External::New(pIsolate, |  | 
|  222                         const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)), |  | 
|  223       v8::PropertyHandlerFlags::kNonMasking); |  | 
|  224   hObjectTemplate->SetHandler(configuration); |  | 
|  225 } |  | 
| OLD | NEW |