JavascriptProxy.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. BOOL JavascriptProxy::Is(_In_ RecyclableObject* obj)
  9. {
  10. return JavascriptOperators::GetTypeId(obj) == TypeIds_Proxy;
  11. }
  12. BOOL JavascriptProxy::Is(_In_ Var obj)
  13. {
  14. return JavascriptOperators::GetTypeId(obj) == TypeIds_Proxy;
  15. }
  16. bool JavascriptProxy::IsRevoked() const
  17. {
  18. return (target == nullptr);
  19. }
  20. RecyclableObject* JavascriptProxy::GetTarget()
  21. {
  22. if (target == nullptr)
  23. {
  24. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u(""));
  25. }
  26. return target;
  27. }
  28. RecyclableObject* JavascriptProxy::GetHandler()
  29. {
  30. if (handler == nullptr)
  31. {
  32. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u(""));
  33. }
  34. return handler;
  35. }
  36. Var JavascriptProxy::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  37. {
  38. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  39. ARGUMENTS(args, callInfo);
  40. ScriptContext* scriptContext = function->GetScriptContext();
  41. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  42. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Proxy, scriptContext);
  43. if (!(args.Info.Flags & CallFlags_New))
  44. {
  45. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew, _u("Proxy"));
  46. }
  47. JavascriptProxy* proxy = JavascriptProxy::Create(scriptContext, args);
  48. return proxy;
  49. }
  50. JavascriptProxy* JavascriptProxy::Create(ScriptContext* scriptContext, Arguments args)
  51. {
  52. // SkipDefaultNewObject function flag should have prevented the default object from
  53. // being created, except when call true a host dispatch.
  54. Var newTarget = args.GetNewTarget();
  55. bool isCtorSuperCall = JavascriptOperators::IsConstructorSuperCall(args);
  56. RecyclableObject* target, *handler;
  57. if (args.Info.Count < 3)
  58. {
  59. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedProxyArgument);
  60. }
  61. if (!JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(args[1])))
  62. {
  63. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u("target"));
  64. }
  65. target = DynamicObject::FromVar(args[1]);
  66. #if ENABLE_COPYONACCESS_ARRAY
  67. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(target);
  68. #endif
  69. if (JavascriptProxy::Is(target))
  70. {
  71. if (JavascriptProxy::FromVar(target)->target == nullptr)
  72. {
  73. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u("target"));
  74. }
  75. }
  76. if (!JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(args[2])))
  77. {
  78. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u("handler"));
  79. }
  80. handler = DynamicObject::FromVar(args[2]);
  81. if (JavascriptProxy::Is(handler))
  82. {
  83. if (JavascriptProxy::FromVar(handler)->handler == nullptr)
  84. {
  85. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u("handler"));
  86. }
  87. }
  88. JavascriptProxy* newProxy = RecyclerNew(scriptContext->GetRecycler(), JavascriptProxy, scriptContext->GetLibrary()->GetProxyType(), scriptContext, target, handler);
  89. if (JavascriptConversion::IsCallable(target))
  90. {
  91. newProxy->ChangeType();
  92. newProxy->GetDynamicType()->SetEntryPoint(JavascriptProxy::FunctionCallTrap);
  93. }
  94. return isCtorSuperCall ?
  95. JavascriptProxy::FromVar(JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), newProxy, nullptr, scriptContext)) :
  96. newProxy;
  97. }
  98. Var JavascriptProxy::EntryRevocable(RecyclableObject* function, CallInfo callInfo, ...)
  99. {
  100. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  101. ARGUMENTS(args, callInfo);
  102. ScriptContext* scriptContext = function->GetScriptContext();
  103. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, _u("Proxy.revocable"));
  104. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  105. if (args.Info.Flags & CallFlags_New)
  106. {
  107. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew, _u("Proxy.revocable"));
  108. }
  109. JavascriptProxy* proxy = JavascriptProxy::Create(scriptContext, args);
  110. JavascriptLibrary* library = scriptContext->GetLibrary();
  111. DynamicType* type = library->CreateFunctionWithConfigurableLengthType(&EntryInfo::Revoke);
  112. RuntimeFunction* revoker = RecyclerNewEnumClass(scriptContext->GetRecycler(),
  113. JavascriptLibrary::EnumFunctionClass, RuntimeFunction,
  114. type, &EntryInfo::Revoke);
  115. revoker->SetPropertyWithAttributes(Js::PropertyIds::length, Js::TaggedInt::ToVarUnchecked(2), PropertyConfigurable, NULL);
  116. revoker->SetInternalProperty(Js::InternalPropertyIds::RevocableProxy, proxy, PropertyOperationFlags::PropertyOperation_Force, nullptr);
  117. DynamicObject* obj = scriptContext->GetLibrary()->CreateObject(true, 2);
  118. JavascriptOperators::SetProperty(obj, obj, PropertyIds::proxy, proxy, scriptContext);
  119. JavascriptOperators::SetProperty(obj, obj, PropertyIds::revoke, revoker, scriptContext);
  120. return obj;
  121. }
  122. Var JavascriptProxy::EntryRevoke(RecyclableObject* function, CallInfo callInfo, ...)
  123. {
  124. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  125. ARGUMENTS(args, callInfo);
  126. ScriptContext* scriptContext = function->GetScriptContext();
  127. AUTO_TAG_NATIVE_LIBRARY_ENTRY(function, callInfo, _u("Proxy.revoke"));
  128. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  129. Var revokableProxy;
  130. if (!function->GetInternalProperty(function, Js::InternalPropertyIds::RevocableProxy, &revokableProxy, nullptr, scriptContext))
  131. {
  132. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u(""));
  133. }
  134. TypeId typeId = JavascriptOperators::GetTypeId(revokableProxy);
  135. if (typeId == TypeIds_Null)
  136. {
  137. return scriptContext->GetLibrary()->GetUndefined();
  138. }
  139. if (typeId != TypeIds_Proxy)
  140. {
  141. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidProxyArgument, _u(""));
  142. }
  143. function->SetInternalProperty(Js::InternalPropertyIds::RevocableProxy, scriptContext->GetLibrary()->GetNull(), PropertyOperationFlags::PropertyOperation_Force, nullptr);
  144. (JavascriptProxy::FromVar(revokableProxy))->RevokeObject();
  145. return scriptContext->GetLibrary()->GetUndefined();
  146. }
  147. JavascriptProxy::JavascriptProxy(DynamicType * type) :
  148. DynamicObject(type),
  149. handler(nullptr),
  150. target(nullptr)
  151. {
  152. type->SetHasSpecialPrototype(true);
  153. }
  154. JavascriptProxy::JavascriptProxy(DynamicType * type, ScriptContext * scriptContext, RecyclableObject* target, RecyclableObject* handler) :
  155. DynamicObject(type),
  156. handler(handler),
  157. target(target)
  158. {
  159. type->SetHasSpecialPrototype(true);
  160. }
  161. void JavascriptProxy::RevokeObject()
  162. {
  163. handler = nullptr;
  164. target = nullptr;
  165. }
  166. template <class Fn, class GetPropertyIdFunc>
  167. BOOL JavascriptProxy::GetPropertyDescriptorTrap(Var originalInstance, Fn fn, GetPropertyIdFunc getPropertyId, PropertyDescriptor* resultDescriptor, ScriptContext* requestContext)
  168. {
  169. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  170. // Reject implicit call
  171. ThreadContext* threadContext = requestContext->GetThreadContext();
  172. if (threadContext->IsDisableImplicitCall())
  173. {
  174. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  175. return FALSE;
  176. }
  177. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  178. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  179. //2. If handler is null, then throw a TypeError exception.
  180. if (handlerObj == nullptr)
  181. {
  182. if (!threadContext->RecordImplicitException())
  183. return FALSE;
  184. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("getOwnPropertyDescriptor"));
  185. }
  186. //3. Let target be the value of the[[ProxyTarget]] internal slot of O.
  187. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  188. Assert((static_cast<DynamicType*>(GetType()))->GetTypeHandler()->GetPropertyCount() == 0 ||
  189. (static_cast<DynamicType*>(GetType()))->GetTypeHandler()->GetPropertyId(GetScriptContext(), 0) == InternalPropertyIds::WeakMapKeyMap);
  190. JavascriptFunction* gOPDMethod = GetMethodHelper(PropertyIds::getOwnPropertyDescriptor, requestContext);
  191. //7. If trap is undefined, then
  192. // a.Return the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  193. if (nullptr == gOPDMethod || GetScriptContext()->IsHeapEnumInProgress())
  194. {
  195. resultDescriptor->SetFromProxy(false);
  196. return fn(targetObj);
  197. }
  198. PropertyId propertyId = getPropertyId();
  199. Var propertyName = GetName(requestContext, propertyId);
  200. Assert(JavascriptString::Is(propertyName) || JavascriptSymbol::Is(propertyName));
  201. //8. Let trapResultObj be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target and P.
  202. //9. ReturnIfAbrupt(trapResultObj).
  203. //10. If Type(trapResultObj) is neither Object nor Undefined, then throw a TypeError exception.
  204. Var getResult = threadContext->ExecuteImplicitCall(gOPDMethod, ImplicitCall_Accessor, [=]()->Js::Var
  205. {
  206. return CALL_FUNCTION(threadContext, gOPDMethod, CallInfo(CallFlags_Value, 3), handlerObj, targetObj, propertyName);
  207. });
  208. TypeId getResultTypeId = JavascriptOperators::GetTypeId(getResult);
  209. if (StaticType::Is(getResultTypeId) && getResultTypeId != TypeIds_Undefined)
  210. {
  211. JavascriptError::ThrowTypeError(requestContext, JSERR_NeedObject, _u("getOwnPropertyDescriptor"));
  212. }
  213. //11. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  214. //12. ReturnIfAbrupt(targetDesc).
  215. PropertyDescriptor targetDescriptor;
  216. BOOL hasProperty;
  217. hasProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, getPropertyId(), requestContext, &targetDescriptor);
  218. //13. If trapResultObj is undefined, then
  219. //a.If targetDesc is undefined, then return undefined.
  220. //b.If targetDesc.[[Configurable]] is false, then throw a TypeError exception.
  221. //c.Let extensibleTarget be the result of IsExtensible(target).
  222. //d.ReturnIfAbrupt(extensibleTarget).
  223. //e.If ToBoolean(extensibleTarget) is false, then throw a TypeError exception.
  224. //f.Return undefined.
  225. if (getResultTypeId == TypeIds_Undefined)
  226. {
  227. if (!hasProperty)
  228. {
  229. return FALSE;
  230. }
  231. if (!targetDescriptor.IsConfigurable())
  232. {
  233. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getOwnPropertyDescriptor"));
  234. }
  235. if (!target->IsExtensible())
  236. {
  237. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getOwnPropertyDescriptor"));
  238. }
  239. return FALSE;
  240. }
  241. //14. Let extensibleTarget be the result of IsExtensible(target).
  242. //15. ReturnIfAbrupt(extensibleTarget).
  243. //16. Let resultDesc be ToPropertyDescriptor(trapResultObj).
  244. //17. ReturnIfAbrupt(resultDesc).
  245. //18. Call CompletePropertyDescriptor(resultDesc, targetDesc).
  246. //19. Let valid be the result of IsCompatiblePropertyDescriptor(extensibleTarget, resultDesc, targetDesc).
  247. //20. If valid is false, then throw a TypeError exception.
  248. //21. If resultDesc.[[Configurable]] is false, then
  249. //a.If targetDesc is undefined or targetDesc.[[Configurable]] is true, then
  250. //i.Throw a TypeError exception.
  251. //22. Return resultDesc.
  252. BOOL isTargetExtensible = target->IsExtensible();
  253. BOOL toProperty = JavascriptOperators::ToPropertyDescriptor(getResult, resultDescriptor, requestContext);
  254. if (!toProperty && isTargetExtensible)
  255. {
  256. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getOwnPropertyDescriptor"));
  257. }
  258. JavascriptOperators::CompletePropertyDescriptor(resultDescriptor, nullptr, requestContext);
  259. if (!JavascriptOperators::IsCompatiblePropertyDescriptor(*resultDescriptor, hasProperty ? &targetDescriptor : nullptr, !!isTargetExtensible, true, requestContext))
  260. {
  261. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getOwnPropertyDescriptor"));
  262. }
  263. if (!resultDescriptor->IsConfigurable())
  264. {
  265. if (!hasProperty || targetDescriptor.IsConfigurable())
  266. {
  267. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getOwnPropertyDescriptor"));
  268. }
  269. }
  270. resultDescriptor->SetFromProxy(true);
  271. return toProperty;
  272. }
  273. template <class Fn, class GetPropertyIdFunc>
  274. BOOL JavascriptProxy::GetPropertyTrap(Var instance, PropertyDescriptor* propertyDescriptor, Fn fn, GetPropertyIdFunc getPropertyId, ScriptContext* requestContext)
  275. {
  276. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  277. // Reject implicit call
  278. ThreadContext* threadContext = requestContext->GetThreadContext();
  279. if (threadContext->IsDisableImplicitCall())
  280. {
  281. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  282. return FALSE;
  283. }
  284. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  285. if (handlerObj == nullptr)
  286. {
  287. // the proxy has been revoked; TypeError.
  288. if (!threadContext->RecordImplicitException())
  289. return FALSE;
  290. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("get"));
  291. }
  292. RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  293. JavascriptFunction* getGetMethod = GetMethodHelper(PropertyIds::get, requestContext);
  294. if (nullptr == getGetMethod || requestContext->IsHeapEnumInProgress())
  295. {
  296. propertyDescriptor->SetFromProxy(false);
  297. return fn(targetObj);
  298. }
  299. PropertyId propertyId = getPropertyId();
  300. propertyDescriptor->SetFromProxy(true);
  301. Var propertyName = GetName(requestContext, propertyId);
  302. Var getGetResult = threadContext->ExecuteImplicitCall(getGetMethod, ImplicitCall_Accessor, [=]()->Js::Var
  303. {
  304. return CALL_FUNCTION(threadContext, getGetMethod, CallInfo(CallFlags_Value, 4), handlerObj, targetObj, propertyName, instance);
  305. });
  306. // 9. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  307. // 10. ReturnIfAbrupt(targetDesc).
  308. // 11. If targetDesc is not undefined, then
  309. // a.If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Writable]] is false, then
  310. // i.If SameValue(trapResult, targetDesc.[[Value]]) is false, then throw a TypeError exception.
  311. // b.If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Get]] is undefined, then
  312. // i.If trapResult is not undefined, then throw a TypeError exception.
  313. // 12. Return trapResult.
  314. PropertyDescriptor targetDescriptor;
  315. Var defaultAccessor = requestContext->GetLibrary()->GetDefaultAccessorFunction();
  316. if (JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetDescriptor))
  317. {
  318. JavascriptOperators::CompletePropertyDescriptor(&targetDescriptor, nullptr, requestContext);
  319. if (targetDescriptor.ValueSpecified() && !targetDescriptor.IsConfigurable() && !targetDescriptor.IsWritable())
  320. {
  321. if (!JavascriptConversion::SameValue(getGetResult, targetDescriptor.GetValue()))
  322. {
  323. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("get"));
  324. }
  325. }
  326. else if (targetDescriptor.GetterSpecified() || targetDescriptor.SetterSpecified())
  327. {
  328. if (!targetDescriptor.IsConfigurable() &&
  329. targetDescriptor.GetGetter() == defaultAccessor &&
  330. JavascriptOperators::GetTypeId(getGetResult) != TypeIds_Undefined)
  331. {
  332. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("get"));
  333. }
  334. }
  335. }
  336. propertyDescriptor->SetValue(getGetResult);
  337. return TRUE;
  338. }
  339. template <class Fn, class GetPropertyIdFunc>
  340. BOOL JavascriptProxy::HasPropertyTrap(Fn fn, GetPropertyIdFunc getPropertyId)
  341. {
  342. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  343. // Reject implicit call
  344. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  345. if (threadContext->IsDisableImplicitCall())
  346. {
  347. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  348. return FALSE;
  349. }
  350. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  351. ScriptContext* requestContext =
  352. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  353. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  354. if (handlerObj == nullptr)
  355. {
  356. // the proxy has been revoked; TypeError.
  357. if (!threadContext->RecordImplicitException())
  358. return FALSE;
  359. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("has"));
  360. }
  361. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  362. JavascriptFunction* hasMethod = GetMethodHelper(PropertyIds::has, requestContext);
  363. if (nullptr == hasMethod || requestContext->IsHeapEnumInProgress())
  364. {
  365. return fn(targetObj);
  366. }
  367. PropertyId propertyId = getPropertyId();
  368. Var propertyName = GetName(requestContext, propertyId);
  369. Var getHasResult = threadContext->ExecuteImplicitCall(hasMethod, ImplicitCall_Accessor, [=]()->Js::Var
  370. {
  371. return CALL_FUNCTION(threadContext, hasMethod, CallInfo(CallFlags_Value, 3), handlerObj, targetObj, propertyName);
  372. });
  373. //9. Let booleanTrapResult be ToBoolean(trapResult).
  374. //10. ReturnIfAbrupt(booleanTrapResult).
  375. //11. If booleanTrapResult is false, then
  376. // a.Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  377. // b.ReturnIfAbrupt(targetDesc).
  378. // c.If targetDesc is not undefined, then
  379. // i.If targetDesc.[[Configurable]] is false, then throw a TypeError exception.
  380. // ii.Let extensibleTarget be the result of IsExtensible(target).
  381. // iii.ReturnIfAbrupt(extensibleTarget).
  382. // iv.If ToBoolean(extensibleTarget) is false, then throw a TypeError exception
  383. BOOL hasProperty = JavascriptConversion::ToBoolean(getHasResult, requestContext);
  384. if (!hasProperty)
  385. {
  386. PropertyDescriptor targetDescriptor;
  387. BOOL hasTargetProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetDescriptor);
  388. if (hasTargetProperty)
  389. {
  390. if (!targetDescriptor.IsConfigurable() || !targetObj->IsExtensible())
  391. {
  392. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("has"));
  393. }
  394. }
  395. }
  396. return hasProperty;
  397. }
  398. PropertyQueryFlags JavascriptProxy::HasPropertyQuery(PropertyId propertyId)
  399. {
  400. auto fn = [&](RecyclableObject* object)->BOOL {
  401. return JavascriptOperators::HasProperty(object, propertyId);
  402. };
  403. auto getPropertyId = [&]() ->PropertyId {
  404. return propertyId;
  405. };
  406. return JavascriptConversion::BooleanToPropertyQueryFlags(HasPropertyTrap(fn, getPropertyId));
  407. }
  408. BOOL JavascriptProxy::HasOwnProperty(PropertyId propertyId)
  409. {
  410. // should never come here and it will be redirected to GetOwnPropertyDescriptor
  411. Assert(FALSE);
  412. PropertyDescriptor propertyDesc;
  413. return GetOwnPropertyDescriptor(this, propertyId, GetScriptContext(), &propertyDesc);
  414. }
  415. BOOL JavascriptProxy::HasOwnPropertyNoHostObject(PropertyId propertyId)
  416. {
  417. // the virtual method is for checking if globalobject has local property before we start initializing
  418. // we shouldn't trap??
  419. Assert(FALSE);
  420. return HasProperty(propertyId);
  421. }
  422. BOOL JavascriptProxy::HasOwnPropertyCheckNoRedecl(PropertyId propertyId)
  423. {
  424. // root object and activation object verification only; not needed.
  425. Assert(FALSE);
  426. return false;
  427. }
  428. BOOL JavascriptProxy::UseDynamicObjectForNoHostObjectAccess()
  429. {
  430. // heapenum check for CEO etc., and we don't want to access external method during enumeration. not applicable here.
  431. Assert(FALSE);
  432. return false;
  433. }
  434. DescriptorFlags JavascriptProxy::GetSetter(PropertyId propertyId, Var* setterValueOrProxy, PropertyValueInfo* info, ScriptContext* requestContext)
  435. {
  436. // This is called when we walk prototype chain looking for setter. It is part of the [[set]] operation, but we don't need to restrict the
  437. // code to mimic the 'one step prototype chain lookup' spec letter. Current code structure is enough.
  438. *setterValueOrProxy = this;
  439. PropertyValueInfo::SetNoCache(info, this);
  440. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  441. return DescriptorFlags::Proxy;
  442. }
  443. // GetSetter is called for
  444. DescriptorFlags JavascriptProxy::GetSetter(JavascriptString* propertyNameString, Var* setterValueOrProxy, PropertyValueInfo* info, ScriptContext* requestContext)
  445. {
  446. *setterValueOrProxy = this;
  447. PropertyValueInfo::SetNoCache(info, this);
  448. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  449. return DescriptorFlags::Proxy;
  450. }
  451. PropertyQueryFlags JavascriptProxy::GetPropertyQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  452. {
  453. // We can't cache the property at this time. both target and handler can be changed outside of the proxy, so the inline cache needs to be
  454. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  455. PropertyValueInfo::SetNoCache(info, this);
  456. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  457. auto fn = [&](RecyclableObject* object)-> BOOL {
  458. return JavascriptOperators::GetProperty(originalInstance, object, propertyId, value, requestContext, nullptr);
  459. };
  460. auto getPropertyId = [&]()->PropertyId {return propertyId; };
  461. PropertyDescriptor result;
  462. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  463. if (!foundProperty)
  464. {
  465. *value = requestContext->GetMissingPropertyResult();
  466. }
  467. else if (result.IsFromProxy())
  468. {
  469. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  470. }
  471. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  472. }
  473. PropertyQueryFlags JavascriptProxy::GetPropertyQuery(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  474. {
  475. // We can't cache the property at this time. both target and handler can be changed outside of the proxy, so the inline cache needs to be
  476. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  477. PropertyValueInfo::SetNoCache(info, this);
  478. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  479. auto fn = [&](RecyclableObject* object)-> BOOL {
  480. return JavascriptOperators::GetPropertyWPCache(originalInstance, object, propertyNameString, value, requestContext, info);
  481. };
  482. auto getPropertyId = [&]()->PropertyId{
  483. const PropertyRecord* propertyRecord;
  484. requestContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  485. return propertyRecord->GetPropertyId();
  486. };
  487. PropertyDescriptor result;
  488. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  489. if (!foundProperty)
  490. {
  491. *value = requestContext->GetMissingPropertyResult();
  492. }
  493. else if (result.IsFromProxy())
  494. {
  495. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  496. }
  497. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  498. }
  499. BOOL JavascriptProxy::GetInternalProperty(Var instance, PropertyId internalPropertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  500. {
  501. if (internalPropertyId == InternalPropertyIds::WeakMapKeyMap)
  502. {
  503. return __super::GetInternalProperty(instance, internalPropertyId, value, info, requestContext);
  504. }
  505. return FALSE;
  506. }
  507. BOOL JavascriptProxy::GetAccessors(PropertyId propertyId, __out Var* getter, __out Var* setter, ScriptContext * requestContext)
  508. {
  509. PropertyDescriptor result;
  510. if (getter != nullptr)
  511. {
  512. *getter = nullptr;
  513. }
  514. if (setter != nullptr)
  515. {
  516. *setter = nullptr;
  517. }
  518. BOOL foundProperty = GetOwnPropertyDescriptor(this, propertyId, requestContext, &result);
  519. if (foundProperty && result.IsFromProxy())
  520. {
  521. if (result.GetterSpecified() && getter != nullptr)
  522. {
  523. *getter = result.GetGetter();
  524. }
  525. if (result.SetterSpecified() && setter != nullptr)
  526. {
  527. *setter = result.GetSetter();
  528. }
  529. foundProperty = result.GetterSpecified() || result.SetterSpecified();
  530. }
  531. return foundProperty;
  532. }
  533. PropertyQueryFlags JavascriptProxy::GetPropertyReferenceQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  534. {
  535. // We can't cache the property at this time. both target and handler can be changed outside of the proxy, so the inline cache needs to be
  536. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  537. PropertyValueInfo::SetNoCache(info, this);
  538. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  539. auto fn = [&](RecyclableObject* object)-> BOOL {
  540. return JavascriptOperators::GetPropertyReference(originalInstance, object, propertyId, value, requestContext, nullptr);
  541. };
  542. auto getPropertyId = [&]() -> PropertyId {return propertyId; };
  543. PropertyDescriptor result;
  544. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  545. if (!foundProperty)
  546. {
  547. *value = requestContext->GetMissingPropertyResult();
  548. }
  549. else if (result.IsFromProxy())
  550. {
  551. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  552. }
  553. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  554. }
  555. BOOL JavascriptProxy::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  556. {
  557. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  558. // This is the second half of [[set]] where when the handler does not specified [[set]] so we forward to [[set]] on target
  559. // with receiver as the proxy.
  560. //c.Let existingDescriptor be the result of calling the[[GetOwnProperty]] internal method of Receiver with argument P.
  561. //d.ReturnIfAbrupt(existingDescriptor).
  562. //e.If existingDescriptor is not undefined, then
  563. // i.Let valueDesc be the PropertyDescriptor{ [[Value]]: V }.
  564. // ii.Return the result of calling the[[DefineOwnProperty]] internal method of Receiver with arguments P and valueDesc.
  565. //f.Else Receiver does not currently have a property P,
  566. // i.Return the result of performing CreateDataProperty(Receiver, P, V).
  567. // We can't cache the property at this time. both target and handler can be changed outside of the proxy, so the inline cache needs to be
  568. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  569. PropertyValueInfo::SetNoCache(info, this);
  570. PropertyValueInfo::DisablePrototypeCache(info, this); // We can't cache prototype property either
  571. PropertyDescriptor proxyPropertyDescriptor;
  572. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  573. ScriptContext* requestContext =
  574. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  575. // Set implicit call flag so we bailout and not do copy-prop on field
  576. Js::ImplicitCallFlags saveImplicitCallFlags = threadContext->GetImplicitCallFlags();
  577. threadContext->SetImplicitCallFlags((Js::ImplicitCallFlags)(saveImplicitCallFlags | ImplicitCall_Accessor));
  578. if (!JavascriptOperators::GetOwnPropertyDescriptor(this, propertyId, requestContext, &proxyPropertyDescriptor))
  579. {
  580. PropertyDescriptor resultDescriptor;
  581. resultDescriptor.SetConfigurable(true);
  582. resultDescriptor.SetWritable(true);
  583. resultDescriptor.SetEnumerable(true);
  584. resultDescriptor.SetValue(value);
  585. return Js::JavascriptOperators::DefineOwnPropertyDescriptor(this, propertyId, resultDescriptor, true, requestContext);
  586. }
  587. else
  588. {
  589. // ES2017 Spec'd (9.1.9.1):
  590. // If existingDescriptor is not undefined, then
  591. // If IsAccessorDescriptor(existingDescriptor) is true, return false.
  592. // If existingDescriptor.[[Writable]] is false, return false.
  593. if (proxyPropertyDescriptor.IsAccessorDescriptor())
  594. {
  595. return FALSE;
  596. }
  597. if (proxyPropertyDescriptor.WritableSpecified() && !proxyPropertyDescriptor.IsWritable())
  598. {
  599. return FALSE;
  600. }
  601. proxyPropertyDescriptor.SetValue(value);
  602. proxyPropertyDescriptor.SetOriginal(nullptr);
  603. return Js::JavascriptOperators::DefineOwnPropertyDescriptor(this, propertyId, proxyPropertyDescriptor, true, requestContext);
  604. }
  605. }
  606. BOOL JavascriptProxy::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  607. {
  608. const PropertyRecord* propertyRecord;
  609. GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  610. return SetProperty(propertyRecord->GetPropertyId(), value, flags, info);
  611. }
  612. BOOL JavascriptProxy::SetInternalProperty(PropertyId internalPropertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  613. {
  614. if (internalPropertyId == InternalPropertyIds::WeakMapKeyMap)
  615. {
  616. return __super::SetInternalProperty(internalPropertyId, value, flags, info);
  617. }
  618. return FALSE;
  619. }
  620. BOOL JavascriptProxy::InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  621. {
  622. return SetProperty(propertyId, value, flags, info);
  623. }
  624. BOOL JavascriptProxy::EnsureProperty(PropertyId propertyId)
  625. {
  626. // proxy needs to be explicitly constructed. we don't have Ensure code path.
  627. Assert(FALSE);
  628. return false;
  629. }
  630. BOOL JavascriptProxy::EnsureNoRedeclProperty(PropertyId propertyId)
  631. {
  632. // proxy needs to be explicitly constructed. we don't have Ensure code path.
  633. Assert(FALSE);
  634. return false;
  635. }
  636. BOOL JavascriptProxy::SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags, SideEffects possibleSideEffects)
  637. {
  638. // called from untrapped DefineProperty and from DOM side. I don't see this being used when the object is a proxy.
  639. Assert(FALSE);
  640. return false;
  641. }
  642. BOOL JavascriptProxy::InitPropertyScoped(PropertyId propertyId, Var value)
  643. {
  644. // proxy needs to be explicitly constructed. we don't have Ensure code path.
  645. Assert(FALSE);
  646. return false;
  647. }
  648. BOOL JavascriptProxy::InitFuncScoped(PropertyId propertyId, Var value)
  649. {
  650. // proxy needs to be explicitly constructed. we don't have Ensure code path.
  651. Assert(FALSE);
  652. return false;
  653. }
  654. BOOL JavascriptProxy::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
  655. {
  656. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  657. // Reject implicit call
  658. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  659. if (threadContext->IsDisableImplicitCall())
  660. {
  661. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  662. return FALSE;
  663. }
  664. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  665. ScriptContext* requestContext =
  666. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  667. //1. Assert: IsPropertyKey(P) is true.
  668. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  669. RecyclableObject * handlerObj = this->MarshalHandler(requestContext);
  670. //3. If handler is null, then throw a TypeError exception.
  671. //6. ReturnIfAbrupt(trap).
  672. if (handlerObj == nullptr)
  673. {
  674. // the proxy has been revoked; TypeError.
  675. if (!threadContext->RecordImplicitException())
  676. return FALSE;
  677. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("deleteProperty"));
  678. }
  679. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  680. RecyclableObject * targetObj = this->MarshalTarget(requestContext);
  681. //5. Let trap be the result of GetMethod(handler, "deleteProperty").
  682. JavascriptFunction* deleteMethod = GetMethodHelper(PropertyIds::deleteProperty, requestContext);
  683. //7. If trap is undefined, then
  684. //a.Return the result of calling the[[Delete]] internal method of target with argument P.
  685. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  686. if (nullptr == deleteMethod)
  687. {
  688. uint32 indexVal;
  689. if (requestContext->IsNumericPropertyId(propertyId, &indexVal))
  690. {
  691. return targetObj->DeleteItem(indexVal, flags);
  692. }
  693. else
  694. {
  695. return targetObj->DeleteProperty(propertyId, flags);
  696. }
  697. }
  698. //8. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target and P.
  699. //9. Let booleanTrapResult be ToBoolean(trapResult).
  700. //10. ReturnIfAbrupt(booleanTrapResult).
  701. //11. If booleanTrapResult is false, then return false.
  702. Var propertyName = GetName(requestContext, propertyId);
  703. Var deletePropertyResult = threadContext->ExecuteImplicitCall(deleteMethod, ImplicitCall_Accessor, [=]()->Js::Var
  704. {
  705. return CALL_FUNCTION(threadContext, deleteMethod, CallInfo(CallFlags_Value, 3), handlerObj, targetObj, propertyName);
  706. });
  707. BOOL trapResult = JavascriptConversion::ToBoolean(deletePropertyResult, requestContext);
  708. if (!trapResult)
  709. {
  710. return trapResult;
  711. }
  712. //12. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  713. //13. ReturnIfAbrupt(targetDesc).
  714. //14. If targetDesc is undefined, then return true.
  715. //15. If targetDesc.[[Configurable]] is false, then throw a TypeError exception.
  716. //16. Return true.
  717. PropertyDescriptor targetPropertyDescriptor;
  718. if (!Js::JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetPropertyDescriptor))
  719. {
  720. return TRUE;
  721. }
  722. if (!targetPropertyDescriptor.IsConfigurable())
  723. {
  724. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("deleteProperty"));
  725. }
  726. return TRUE;
  727. }
  728. BOOL JavascriptProxy::DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags)
  729. {
  730. PropertyRecord const *propertyRecord = nullptr;
  731. if (JavascriptOperators::ShouldTryDeleteProperty(this, propertyNameString, &propertyRecord))
  732. {
  733. Assert(propertyRecord);
  734. return DeleteProperty(propertyRecord->GetPropertyId(), flags);
  735. }
  736. return TRUE;
  737. }
  738. #if ENABLE_FIXED_FIELDS
  739. BOOL JavascriptProxy::IsFixedProperty(PropertyId propertyId)
  740. {
  741. // TODO: can we add support for fixed property? don't see a clear way to invalidate...
  742. return false;
  743. }
  744. #endif
  745. PropertyQueryFlags JavascriptProxy::HasItemQuery(uint32 index)
  746. {
  747. const PropertyRecord* propertyRecord;
  748. auto fn = [&](RecyclableObject* object)-> BOOL {
  749. return JavascriptOperators::HasItem(object, index);
  750. };
  751. auto getPropertyId = [&]() ->PropertyId {
  752. PropertyIdFromInt(index, &propertyRecord);
  753. return propertyRecord->GetPropertyId();
  754. };
  755. return JavascriptConversion::BooleanToPropertyQueryFlags(HasPropertyTrap(fn, getPropertyId));
  756. }
  757. BOOL JavascriptProxy::HasOwnItem(uint32 index)
  758. {
  759. const PropertyRecord* propertyRecord;
  760. auto fn = [&](RecyclableObject* object)-> BOOL {
  761. return JavascriptOperators::HasOwnItem(object, index);
  762. };
  763. auto getPropertyId = [&]() ->PropertyId {
  764. PropertyIdFromInt(index, &propertyRecord);
  765. return propertyRecord->GetPropertyId();
  766. };
  767. return HasPropertyTrap(fn, getPropertyId);
  768. }
  769. PropertyQueryFlags JavascriptProxy::GetItemQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  770. {
  771. const PropertyRecord* propertyRecord;
  772. auto fn = [&](RecyclableObject* object)-> BOOL {
  773. return JavascriptOperators::GetItem(originalInstance, object, index, value, requestContext);
  774. };
  775. auto getPropertyId = [&]() ->PropertyId {
  776. PropertyIdFromInt(index, &propertyRecord);
  777. return propertyRecord->GetPropertyId();
  778. };
  779. PropertyDescriptor result;
  780. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  781. if (!foundProperty)
  782. {
  783. *value = requestContext->GetMissingItemResult();
  784. }
  785. else if (result.IsFromProxy())
  786. {
  787. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  788. }
  789. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  790. }
  791. PropertyQueryFlags JavascriptProxy::GetItemReferenceQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  792. {
  793. const PropertyRecord* propertyRecord;
  794. auto fn = [&](RecyclableObject* object)-> BOOL {
  795. return JavascriptOperators::GetItem(originalInstance, object, index, value, requestContext);
  796. };
  797. auto getPropertyId = [&]() ->PropertyId {
  798. PropertyIdFromInt(index, &propertyRecord);
  799. return propertyRecord->GetPropertyId();
  800. };
  801. PropertyDescriptor result;
  802. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  803. if (!foundProperty)
  804. {
  805. *value = requestContext->GetMissingItemResult();
  806. }
  807. else if (result.IsFromProxy())
  808. {
  809. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  810. }
  811. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  812. }
  813. DescriptorFlags JavascriptProxy::GetItemSetter(uint32 index, Var* setterValueOrProxy, ScriptContext* requestContext)
  814. {
  815. *setterValueOrProxy = this;
  816. return DescriptorFlags::Proxy;
  817. }
  818. BOOL JavascriptProxy::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
  819. {
  820. const PropertyRecord* propertyRecord;
  821. PropertyIdFromInt(index, &propertyRecord);
  822. return SetProperty(propertyRecord->GetPropertyId(), value, flags, nullptr);
  823. }
  824. BOOL JavascriptProxy::DeleteItem(uint32 index, PropertyOperationFlags flags)
  825. {
  826. const PropertyRecord* propertyRecord;
  827. PropertyIdFromInt(index, &propertyRecord);
  828. return DeleteProperty(propertyRecord->GetPropertyId(), flags);
  829. }
  830. // No change to foreign enumerator, just forward
  831. BOOL JavascriptProxy::GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, EnumeratorCache * enumeratorCache)
  832. {
  833. // Reject implicit call
  834. ThreadContext* threadContext = requestContext->GetThreadContext();
  835. if (threadContext->IsDisableImplicitCall())
  836. {
  837. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  838. return FALSE;
  839. }
  840. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  841. // 2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  842. // 3. If handler is null, then throw a TypeError exception.
  843. if (this->handler == nullptr)
  844. {
  845. // the proxy has been revoked; TypeError.
  846. if (!threadContext->RecordImplicitException())
  847. return FALSE;
  848. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("ownKeys"));
  849. }
  850. struct ProxyOwnkeysEnumerator : public JavascriptEnumerator
  851. {
  852. typedef JsUtil::BaseHashSet<const char16*, Recycler> VisitedNamesHashSet;
  853. Field(VisitedNamesHashSet*) visited;
  854. Field(JavascriptArray*) trapResult;
  855. Field(JavascriptProxy*) proxy;
  856. FieldNoBarrier(ScriptContext*) scriptContext;
  857. Field(uint32) index;
  858. DEFINE_VTABLE_CTOR_ABSTRACT(ProxyOwnkeysEnumerator, JavascriptEnumerator)
  859. ProxyOwnkeysEnumerator(ScriptContext* scriptContext, JavascriptProxy* proxy, JavascriptArray* trapResult)
  860. :JavascriptEnumerator(scriptContext), scriptContext(scriptContext), proxy(proxy), trapResult(trapResult)
  861. {
  862. visited = RecyclerNew(scriptContext->GetRecycler(), VisitedNamesHashSet, scriptContext->GetRecycler());
  863. }
  864. virtual void Reset() override
  865. {
  866. index = 0;
  867. visited->Reset();
  868. }
  869. virtual JavascriptString * MoveAndGetNext(PropertyId& propertyId, PropertyAttributes* attributes = nullptr) override
  870. {
  871. propertyId = Constants::NoProperty;
  872. if (attributes != nullptr)
  873. {
  874. *attributes = PropertyEnumerable;
  875. }
  876. // 13.7.5.15 EnumerateObjectProperties(O) (https://tc39.github.io/ecma262/#sec-enumerate-object-properties)
  877. // for (let key of Reflect.ownKeys(obj)) {
  878. uint32 len = trapResult->GetLength();
  879. while (index < len)
  880. {
  881. Var var = trapResult->DirectGetItem(index++) ;
  882. if (var)
  883. {
  884. // if (typeof key === "string") {
  885. if (JavascriptString::Is(var))
  886. {
  887. JavascriptString* propertyName = JavascriptString::FromVar(var);
  888. // let desc = Reflect.getOwnPropertyDescriptor(obj, key);
  889. Js::PropertyDescriptor desc;
  890. BOOL ret = JavascriptOperators::GetOwnPropertyDescriptor(proxy, propertyName, scriptContext, &desc);
  891. // if (desc && !visited.has(key)) {
  892. if (ret && !visited->Contains(propertyName->GetSz()))
  893. {
  894. visited->Add(propertyName->GetSz());
  895. // if (desc.enumerable) yield key;
  896. if (desc.IsEnumerable())
  897. {
  898. return JavascriptString::FromVar(CrossSite::MarshalVar(
  899. scriptContext, propertyName, propertyName->GetScriptContext()));
  900. }
  901. }
  902. }
  903. }
  904. }
  905. return nullptr;
  906. }
  907. };
  908. JavascriptArray* trapResult = JavascriptOperators::GetOwnPropertyNames(this, requestContext);
  909. ProxyOwnkeysEnumerator* ownKeysEnum = RecyclerNew(requestContext->GetRecycler(), ProxyOwnkeysEnumerator, requestContext, this, trapResult);
  910. return enumerator->Initialize(ownKeysEnum, nullptr, nullptr, flags, requestContext, enumeratorCache);
  911. }
  912. BOOL JavascriptProxy::SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  913. {
  914. // should be for __definegetter style usage. need to wait for clear spec what it means.
  915. Assert(FALSE);
  916. return false;
  917. }
  918. BOOL JavascriptProxy::Equals(__in Var other, __out BOOL* value, ScriptContext* requestContext)
  919. {
  920. //RecyclableObject* targetObj;
  921. if (this->target == nullptr)
  922. {
  923. // the proxy has been revoked; TypeError.
  924. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("equal"));
  925. }
  926. // Reject implicit call
  927. ThreadContext* threadContext = requestContext->GetThreadContext();
  928. if (threadContext->IsDisableImplicitCall())
  929. {
  930. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  931. *value = FALSE;
  932. return FALSE;
  933. }
  934. *value = (other == this);
  935. return true;
  936. }
  937. BOOL JavascriptProxy::StrictEquals(__in Var other, __out BOOL* value, ScriptContext* requestContext)
  938. {
  939. *value = FALSE;
  940. //RecyclableObject* targetObj;
  941. if (this->target == nullptr)
  942. {
  943. // the proxy has been revoked; TypeError.
  944. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("strict equal"));
  945. }
  946. // Reject implicit call
  947. ThreadContext* threadContext = requestContext->GetThreadContext();
  948. if (threadContext->IsDisableImplicitCall())
  949. {
  950. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  951. return FALSE;
  952. }
  953. *value = (other == this);
  954. return true;
  955. }
  956. BOOL JavascriptProxy::IsWritable(PropertyId propertyId)
  957. {
  958. PropertyDescriptor propertyDescriptor;
  959. if (!GetOwnPropertyDescriptor(this, propertyId, GetScriptContext(), &propertyDescriptor))
  960. {
  961. return FALSE;
  962. }
  963. // If property descriptor has getter/setter we should check if writable is specified before checking IsWritable
  964. return propertyDescriptor.WritableSpecified() ? propertyDescriptor.IsWritable() : FALSE;
  965. }
  966. BOOL JavascriptProxy::IsConfigurable(PropertyId propertyId)
  967. {
  968. Assert(FALSE);
  969. return target->IsConfigurable(propertyId);
  970. }
  971. BOOL JavascriptProxy::IsEnumerable(PropertyId propertyId)
  972. {
  973. Assert(FALSE);
  974. return target->IsEnumerable(propertyId);
  975. }
  976. BOOL JavascriptProxy::IsExtensible()
  977. {
  978. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  979. // Reject implicit call
  980. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  981. if (threadContext->IsDisableImplicitCall())
  982. {
  983. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  984. return FALSE;
  985. }
  986. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  987. ScriptContext* requestContext =
  988. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  989. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  990. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  991. //2. If handler is null, then throw a TypeError exception.
  992. if (handlerObj == nullptr)
  993. {
  994. // the proxy has been revoked; TypeError.
  995. if (!threadContext->RecordImplicitException())
  996. return FALSE;
  997. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("isExtensible"));
  998. }
  999. //3. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1000. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1001. //4. Let trap be the result of GetMethod(handler, "isExtensible").
  1002. //5. ReturnIfAbrupt(trap).
  1003. //6. If trap is undefined, then
  1004. //a.Return the result of calling the[[IsExtensible]] internal method of target.
  1005. //7. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target.
  1006. //8. Let booleanTrapResult be ToBoolean(trapResult).
  1007. //9. ReturnIfAbrupt(booleanTrapResult).
  1008. //10. Let targetResult be the result of calling the[[IsExtensible]] internal method of target.
  1009. //11. ReturnIfAbrupt(targetResult).
  1010. //12. If SameValue(booleanTrapResult, targetResult) is false, then throw a TypeError exception.
  1011. //13. Return booleanTrapResult.
  1012. JavascriptFunction* isExtensibleMethod = GetMethodHelper(PropertyIds::isExtensible, requestContext);
  1013. Assert(!requestContext->IsHeapEnumInProgress());
  1014. if (nullptr == isExtensibleMethod)
  1015. {
  1016. return targetObj->IsExtensible();
  1017. }
  1018. Var isExtensibleResult = threadContext->ExecuteImplicitCall(isExtensibleMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1019. {
  1020. return CALL_FUNCTION(threadContext, isExtensibleMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1021. });
  1022. BOOL trapResult = JavascriptConversion::ToBoolean(isExtensibleResult, requestContext);
  1023. BOOL targetIsExtensible = targetObj->IsExtensible();
  1024. if (trapResult != targetIsExtensible)
  1025. {
  1026. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("isExtensible"));
  1027. }
  1028. return trapResult;
  1029. }
  1030. BOOL JavascriptProxy::PreventExtensions()
  1031. {
  1032. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1033. // Reject implicit call
  1034. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1035. if (threadContext->IsDisableImplicitCall())
  1036. {
  1037. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1038. return FALSE;
  1039. }
  1040. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  1041. ScriptContext* requestContext =
  1042. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  1043. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1044. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1045. //2. If handler is null, then throw a TypeError exception.
  1046. if (handlerObj == nullptr)
  1047. {
  1048. // the proxy has been revoked; TypeError.
  1049. if (!threadContext->RecordImplicitException())
  1050. return FALSE;
  1051. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("preventExtensions"));
  1052. }
  1053. //3. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1054. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1055. //4. Let trap be the result of GetMethod(handler, "preventExtensions").
  1056. //5. ReturnIfAbrupt(trap).
  1057. //6. If trap is undefined, then
  1058. //a.Return the result of calling the[[PreventExtensions]] internal method of target.
  1059. //7. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target.
  1060. JavascriptFunction* preventExtensionsMethod = GetMethodHelper(PropertyIds::preventExtensions, requestContext);
  1061. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1062. if (nullptr == preventExtensionsMethod)
  1063. {
  1064. return targetObj->PreventExtensions();
  1065. }
  1066. //8. Let booleanTrapResult be ToBoolean(trapResult)
  1067. //9. ReturnIfAbrupt(booleanTrapResult).
  1068. //10. Let targetIsExtensible be the result of calling the[[IsExtensible]] internal method of target.
  1069. //11. ReturnIfAbrupt(targetIsExtensible).
  1070. //12. If booleanTrapResult is true and targetIsExtensible is true, then throw a TypeError exception.
  1071. //13. Return booleanTrapResult.
  1072. Var preventExtensionsResult = threadContext->ExecuteImplicitCall(preventExtensionsMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1073. {
  1074. return CALL_FUNCTION(threadContext, preventExtensionsMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1075. });
  1076. BOOL trapResult = JavascriptConversion::ToBoolean(preventExtensionsResult, requestContext);
  1077. if (trapResult)
  1078. {
  1079. BOOL targetIsExtensible = targetObj->IsExtensible();
  1080. if (targetIsExtensible)
  1081. {
  1082. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("preventExtensions"));
  1083. }
  1084. }
  1085. return trapResult;
  1086. }
  1087. BOOL JavascriptProxy::GetDefaultPropertyDescriptor(PropertyDescriptor& descriptor)
  1088. {
  1089. if (target == nullptr)
  1090. {
  1091. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u(""));
  1092. }
  1093. return target->GetDefaultPropertyDescriptor(descriptor);
  1094. }
  1095. // 7.3.12 in ES 2015. While this should have been no observable behavior change. Till there is obvious change warrant this
  1096. // to be moved to JavascriptOperators, let's keep it in proxy only first.
  1097. BOOL JavascriptProxy::TestIntegrityLevel(IntegrityLevel integrityLevel, RecyclableObject* obj, ScriptContext* scriptContext)
  1098. {
  1099. //1. Assert: Type(O) is Object.
  1100. //2. Assert: level is either "sealed" or "frozen".
  1101. //3. Let status be IsExtensible(O).
  1102. //4. ReturnIfAbrupt(status).
  1103. //5. If status is true, then return false
  1104. //6. NOTE If the object is extensible, none of its properties are examined.
  1105. BOOL isExtensible = obj->IsExtensible();
  1106. if (isExtensible)
  1107. {
  1108. return FALSE;
  1109. }
  1110. // at this time this is called from proxy only; when we extend this to other objects, we need to handle the other codepath.
  1111. //7. Let keys be O.[[OwnPropertyKeys]]().
  1112. //8. ReturnIfAbrupt(keys).
  1113. Assert(JavascriptProxy::Is(obj));
  1114. JavascriptArray* resultArray = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
  1115. //9. Repeat for each element k of keys,
  1116. // a. Let currentDesc be O.[[GetOwnProperty]](k).
  1117. // b. ReturnIfAbrupt(currentDesc).
  1118. // c. If currentDesc is not undefined, then
  1119. // i. If currentDesc.[[Configurable]] is true, return false.
  1120. // ii. If level is "frozen" and IsDataDescriptor(currentDesc) is true, then
  1121. // 1. If currentDesc.[[Writable]] is true, return false.
  1122. Var itemVar;
  1123. bool writable = false;
  1124. bool configurable = false;
  1125. const PropertyRecord* propertyRecord;
  1126. PropertyDescriptor propertyDescriptor;
  1127. for (uint i = 0; i < resultArray->GetLength(); i++)
  1128. {
  1129. itemVar = resultArray->DirectGetItem(i);
  1130. AssertMsg(JavascriptSymbol::Is(itemVar) || JavascriptString::Is(itemVar), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  1131. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1132. PropertyId propertyId = propertyRecord->GetPropertyId();
  1133. if (JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor))
  1134. {
  1135. configurable |= propertyDescriptor.IsConfigurable();
  1136. if (propertyDescriptor.IsDataDescriptor())
  1137. {
  1138. writable |= propertyDescriptor.IsWritable();
  1139. }
  1140. }
  1141. }
  1142. if (integrityLevel == IntegrityLevel::IntegrityLevel_frozen && writable)
  1143. {
  1144. return FALSE;
  1145. }
  1146. if (configurable)
  1147. {
  1148. return FALSE;
  1149. }
  1150. return TRUE;
  1151. }
  1152. BOOL JavascriptProxy::SetIntegrityLevel(IntegrityLevel integrityLevel, RecyclableObject* obj, ScriptContext* scriptContext)
  1153. {
  1154. //1. Assert: Type(O) is Object.
  1155. //2. Assert : level is either "sealed" or "frozen".
  1156. //3. Let status be O.[[PreventExtensions]]().
  1157. //4. ReturnIfAbrupt(status).
  1158. //5. If status is false, return false.
  1159. // at this time this is called from proxy only; when we extend this to other objects, we need to handle the other codepath.
  1160. Assert(JavascriptProxy::Is(obj));
  1161. if (obj->PreventExtensions() == FALSE)
  1162. return FALSE;
  1163. //6. Let keys be O.[[OwnPropertyKeys]]().
  1164. //7. ReturnIfAbrupt(keys).
  1165. JavascriptArray* resultArray = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
  1166. const PropertyRecord* propertyRecord;
  1167. if (integrityLevel == IntegrityLevel::IntegrityLevel_sealed)
  1168. {
  1169. //8. If level is "sealed", then
  1170. //a. Repeat for each element k of keys,
  1171. //i. Let status be DefinePropertyOrThrow(O, k, PropertyDescriptor{ [[Configurable]]: false }).
  1172. //ii. ReturnIfAbrupt(status).
  1173. PropertyDescriptor propertyDescriptor;
  1174. propertyDescriptor.SetConfigurable(false);
  1175. Var itemVar;
  1176. for (uint i = 0; i < resultArray->GetLength(); i++)
  1177. {
  1178. itemVar = resultArray->DirectGetItem(i);
  1179. AssertMsg(JavascriptSymbol::Is(itemVar) || JavascriptString::Is(itemVar), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  1180. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1181. PropertyId propertyId = propertyRecord->GetPropertyId();
  1182. JavascriptObject::DefineOwnPropertyHelper(obj, propertyId, propertyDescriptor, scriptContext);
  1183. }
  1184. }
  1185. else
  1186. {
  1187. //9.Else level is "frozen",
  1188. // a.Repeat for each element k of keys,
  1189. // i. Let currentDesc be O.[[GetOwnProperty]](k).
  1190. // ii. ReturnIfAbrupt(currentDesc).
  1191. // iii. If currentDesc is not undefined, then
  1192. // 1. If IsAccessorDescriptor(currentDesc) is true, then
  1193. // a. Let desc be the PropertyDescriptor{[[Configurable]]: false}.
  1194. // 2.Else,
  1195. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false, [[Writable]]: false }.
  1196. // 3. Let status be DefinePropertyOrThrow(O, k, desc).
  1197. // 4. ReturnIfAbrupt(status).
  1198. Assert(integrityLevel == IntegrityLevel::IntegrityLevel_frozen);
  1199. PropertyDescriptor current, dataDescriptor, accessorDescriptor;
  1200. dataDescriptor.SetConfigurable(false);
  1201. dataDescriptor.SetWritable(false);
  1202. accessorDescriptor.SetConfigurable(false);
  1203. Var itemVar;
  1204. for (uint i = 0; i < resultArray->GetLength(); i++)
  1205. {
  1206. itemVar = resultArray->DirectGetItem(i);
  1207. AssertMsg(JavascriptSymbol::Is(itemVar) || JavascriptString::Is(itemVar), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  1208. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1209. PropertyId propertyId = propertyRecord->GetPropertyId();
  1210. PropertyDescriptor propertyDescriptor;
  1211. if (JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor))
  1212. {
  1213. if (propertyDescriptor.IsDataDescriptor())
  1214. {
  1215. JavascriptObject::DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), dataDescriptor, scriptContext);
  1216. }
  1217. else if (propertyDescriptor.IsAccessorDescriptor())
  1218. {
  1219. JavascriptObject::DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), accessorDescriptor, scriptContext);
  1220. }
  1221. }
  1222. }
  1223. }
  1224. // 10. Return true
  1225. return TRUE;
  1226. }
  1227. BOOL JavascriptProxy::Seal()
  1228. {
  1229. return SetIntegrityLevel(IntegrityLevel::IntegrityLevel_sealed, this, this->GetScriptContext());
  1230. }
  1231. BOOL JavascriptProxy::Freeze()
  1232. {
  1233. return SetIntegrityLevel(IntegrityLevel::IntegrityLevel_frozen, this, this->GetScriptContext());
  1234. }
  1235. BOOL JavascriptProxy::IsSealed()
  1236. {
  1237. return TestIntegrityLevel(IntegrityLevel::IntegrityLevel_sealed, this, this->GetScriptContext());
  1238. }
  1239. BOOL JavascriptProxy::IsFrozen()
  1240. {
  1241. return TestIntegrityLevel(IntegrityLevel::IntegrityLevel_frozen, this, this->GetScriptContext());
  1242. }
  1243. BOOL JavascriptProxy::SetWritable(PropertyId propertyId, BOOL value)
  1244. {
  1245. Assert(FALSE);
  1246. return FALSE;
  1247. }
  1248. BOOL JavascriptProxy::SetConfigurable(PropertyId propertyId, BOOL value)
  1249. {
  1250. Assert(FALSE);
  1251. return FALSE;
  1252. }
  1253. BOOL JavascriptProxy::SetEnumerable(PropertyId propertyId, BOOL value)
  1254. {
  1255. Assert(FALSE);
  1256. return FALSE;
  1257. }
  1258. BOOL JavascriptProxy::SetAttributes(PropertyId propertyId, PropertyAttributes attributes)
  1259. {
  1260. Assert(FALSE);
  1261. return FALSE;
  1262. }
  1263. BOOL JavascriptProxy::HasInstance(Var instance, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  1264. {
  1265. Var funcPrototype = JavascriptOperators::GetProperty(this, PropertyIds::prototype, scriptContext);
  1266. return JavascriptFunction::HasInstance(funcPrototype, instance, scriptContext, NULL, NULL);
  1267. }
  1268. JavascriptString* JavascriptProxy::GetClassName(ScriptContext * requestContext)
  1269. {
  1270. Assert(FALSE);
  1271. return nullptr;
  1272. }
  1273. RecyclableObject* JavascriptProxy::GetPrototypeSpecial()
  1274. {
  1275. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1276. // Reject implicit call
  1277. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1278. if (threadContext->IsDisableImplicitCall())
  1279. {
  1280. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1281. return nullptr;
  1282. }
  1283. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  1284. ScriptContext* requestContext =
  1285. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  1286. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1287. if (handlerObj == nullptr)
  1288. {
  1289. // the proxy has been revoked; TypeError.
  1290. if (!threadContext->RecordImplicitException())
  1291. return nullptr;
  1292. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("getPrototypeOf"));
  1293. }
  1294. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1295. JavascriptFunction* getPrototypeOfMethod = GetMethodHelper(PropertyIds::getPrototypeOf, requestContext);
  1296. if (nullptr == getPrototypeOfMethod || GetScriptContext()->IsHeapEnumInProgress())
  1297. {
  1298. return RecyclableObject::FromVar(JavascriptObject::GetPrototypeOf(targetObj, requestContext));
  1299. }
  1300. Var getPrototypeOfResult = threadContext->ExecuteImplicitCall(getPrototypeOfMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1301. {
  1302. return CALL_FUNCTION(threadContext, getPrototypeOfMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1303. });
  1304. TypeId prototypeTypeId = JavascriptOperators::GetTypeId(getPrototypeOfResult);
  1305. if (!JavascriptOperators::IsObjectType(prototypeTypeId) && prototypeTypeId != TypeIds_Null)
  1306. {
  1307. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getPrototypeOf"));
  1308. }
  1309. if (!targetObj->IsExtensible() && !JavascriptConversion::SameValue(getPrototypeOfResult, targetObj->GetPrototype()))
  1310. {
  1311. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getPrototypeOf"));
  1312. }
  1313. return RecyclableObject::FromVar(getPrototypeOfResult);
  1314. }
  1315. RecyclableObject* JavascriptProxy::GetConfigurablePrototype(ScriptContext * requestContext)
  1316. {
  1317. // We should be using GetPrototypeSpecial for proxy object; never should come over here.
  1318. Assert(FALSE);
  1319. return nullptr;
  1320. }
  1321. void JavascriptProxy::RemoveFromPrototype(ScriptContext * requestContext)
  1322. {
  1323. Assert(FALSE);
  1324. }
  1325. void JavascriptProxy::AddToPrototype(ScriptContext * requestContext)
  1326. {
  1327. Assert(FALSE);
  1328. }
  1329. void JavascriptProxy::SetPrototype(RecyclableObject* newPrototype)
  1330. {
  1331. Assert(FALSE);
  1332. }
  1333. BOOL JavascriptProxy::SetPrototypeTrap(RecyclableObject* newPrototype, bool shouldThrow,
  1334. ScriptContext * requestContext)
  1335. {
  1336. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1337. Assert(JavascriptOperators::IsObjectOrNull(newPrototype));
  1338. // Reject implicit call
  1339. ThreadContext* threadContext = requestContext->GetThreadContext();
  1340. if (threadContext->IsDisableImplicitCall())
  1341. {
  1342. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1343. return FALSE;
  1344. }
  1345. //1. Assert: Either Type(V) is Object or Type(V) is Null.
  1346. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1347. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1348. //3. If handler is null, then throw a TypeError exception.
  1349. if (handlerObj == nullptr)
  1350. {
  1351. // the proxy has been revoked; TypeError.
  1352. if (shouldThrow)
  1353. {
  1354. if (!threadContext->RecordImplicitException())
  1355. return FALSE;
  1356. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("setPrototypeOf"));
  1357. }
  1358. }
  1359. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1360. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1361. //5. Let trap be the result of GetMethod(handler, "setPrototypeOf").
  1362. //6. ReturnIfAbrupt(trap).
  1363. //7. If trap is undefined, then
  1364. //a.Return the result of calling the[[SetPrototypeOf]] internal method of target with argument V.
  1365. JavascriptFunction* setPrototypeOfMethod = GetMethodHelper(PropertyIds::setPrototypeOf, requestContext);
  1366. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1367. if (nullptr == setPrototypeOfMethod)
  1368. {
  1369. JavascriptObject::ChangePrototype(targetObj, newPrototype, shouldThrow, requestContext);
  1370. return TRUE;
  1371. }
  1372. //8. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target and V.
  1373. Var setPrototypeResult = threadContext->ExecuteImplicitCall(setPrototypeOfMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1374. {
  1375. return CALL_FUNCTION(threadContext, setPrototypeOfMethod, CallInfo(CallFlags_Value, 3), handlerObj, targetObj, newPrototype);
  1376. });
  1377. //9. Let booleanTrapResult be ToBoolean(trapResult).
  1378. //10. ReturnIfAbrupt(booleanTrapResult).
  1379. //11. Let extensibleTarget be the result of IsExtensible(target).
  1380. //12. ReturnIfAbrupt(extensibleTarget).
  1381. //13. If extensibleTarget is true, then return booleanTrapResult.
  1382. //14. Let targetProto be the result of calling the[[GetPrototypeOf]] internal method of target.
  1383. //15. ReturnIfAbrupt(targetProto).
  1384. //16. If booleanTrapResult is true and SameValue(V, targetProto) is false, then throw a TypeError exception.
  1385. //17. Return booleanTrapResult.
  1386. BOOL prototypeSetted = JavascriptConversion::ToBoolean(setPrototypeResult, requestContext);
  1387. BOOL isExtensible = targetObj->IsExtensible();
  1388. if (isExtensible)
  1389. {
  1390. if (!prototypeSetted && shouldThrow)
  1391. {
  1392. JavascriptError::ThrowTypeError(requestContext, JSERR_ProxyTrapReturnedFalse, _u("setPrototypeOf"));
  1393. }
  1394. return prototypeSetted;
  1395. }
  1396. Var targetProto = targetObj->GetPrototype();
  1397. if (!JavascriptConversion::SameValue(targetProto, newPrototype))
  1398. {
  1399. if (shouldThrow)
  1400. {
  1401. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("setPrototypeOf"));
  1402. }
  1403. return FALSE;
  1404. }
  1405. return TRUE;
  1406. }
  1407. Var JavascriptProxy::ToString(ScriptContext* scriptContext)
  1408. {
  1409. //RecyclableObject* targetObj;
  1410. if (this->handler == nullptr)
  1411. {
  1412. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1413. // the proxy has been revoked; TypeError.
  1414. if (!threadContext->RecordImplicitException())
  1415. return nullptr;
  1416. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("toString"));
  1417. }
  1418. return JavascriptObject::ToStringHelper(target, scriptContext);
  1419. }
  1420. BOOL JavascriptProxy::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  1421. {
  1422. //RecyclableObject* targetObj;
  1423. if (this->handler == nullptr)
  1424. {
  1425. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1426. // the proxy has been revoked; TypeError.
  1427. if (!threadContext->RecordImplicitException())
  1428. return FALSE;
  1429. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("getTypeString"));
  1430. }
  1431. return target->GetDiagTypeString(stringBuilder, requestContext);
  1432. }
  1433. RecyclableObject* JavascriptProxy::ToObject(ScriptContext * requestContext)
  1434. {
  1435. //RecyclableObject* targetObj;
  1436. if (this->handler == nullptr)
  1437. {
  1438. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1439. // the proxy has been revoked; TypeError.
  1440. if (!threadContext->RecordImplicitException())
  1441. return nullptr;
  1442. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("toObject"));
  1443. }
  1444. return __super::ToObject(requestContext);
  1445. }
  1446. Var JavascriptProxy::GetTypeOfString(ScriptContext* requestContext)
  1447. {
  1448. if (this->handler == nullptr)
  1449. {
  1450. // even if handler is nullptr, return typeof as "object"
  1451. return requestContext->GetLibrary()->GetObjectTypeDisplayString();
  1452. }
  1453. // if exotic object has [[Call]] we should return "function", otherwise return "object"
  1454. if (JavascriptFunction::Is(this->target))
  1455. {
  1456. return requestContext->GetLibrary()->GetFunctionTypeDisplayString();
  1457. }
  1458. else
  1459. {
  1460. return requestContext->GetLibrary()->GetObjectTypeDisplayString();
  1461. }
  1462. }
  1463. BOOL JavascriptProxy::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* requestContext, PropertyDescriptor* propertyDescriptor)
  1464. {
  1465. JavascriptProxy* proxy = JavascriptProxy::FromVar(obj);
  1466. auto fn = [&](RecyclableObject *targetObj)-> BOOL {
  1467. return JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, propertyDescriptor);
  1468. };
  1469. auto getPropertyId = [&]() -> PropertyId {return propertyId; };
  1470. BOOL foundProperty = proxy->GetPropertyDescriptorTrap(obj, fn, getPropertyId, propertyDescriptor, requestContext);
  1471. return foundProperty;
  1472. }
  1473. BOOL JavascriptProxy::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* requestContext)
  1474. {
  1475. PROBE_STACK(requestContext, Js::Constants::MinStackDefault);
  1476. // Reject implicit call
  1477. ThreadContext* threadContext = requestContext->GetThreadContext();
  1478. if (threadContext->IsDisableImplicitCall())
  1479. {
  1480. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1481. return FALSE;
  1482. }
  1483. JavascriptProxy* proxy = JavascriptProxy::FromVar(obj);
  1484. //1. Assert: IsPropertyKey(P) is true.
  1485. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1486. RecyclableObject *handlerObj = proxy->MarshalHandler(requestContext);
  1487. //3. If handler is null, then throw a TypeError exception.
  1488. if (handlerObj == nullptr)
  1489. {
  1490. // the proxy has been revoked; TypeError.
  1491. if (!threadContext->RecordImplicitException())
  1492. return FALSE;
  1493. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("definePropertyDescriptor"));
  1494. }
  1495. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1496. RecyclableObject *targetObj = proxy->MarshalTarget(requestContext);
  1497. //5. Let trap be the result of GetMethod(handler, "defineProperty").
  1498. //6. ReturnIfAbrupt(trap).
  1499. //7. If trap is undefined, then
  1500. //a.Return the result of calling the[[DefineOwnProperty]] internal method of target with arguments P and Desc.
  1501. JavascriptFunction* defineOwnPropertyMethod = proxy->GetMethodHelper(PropertyIds::defineProperty, requestContext);
  1502. Assert(!requestContext->IsHeapEnumInProgress());
  1503. if (nullptr == defineOwnPropertyMethod)
  1504. {
  1505. return JavascriptOperators::DefineOwnPropertyDescriptor(targetObj, propId, descriptor, throwOnError, requestContext);
  1506. }
  1507. //8. Let descObj be FromPropertyDescriptor(Desc).
  1508. //9. NOTE If Desc was originally generated from an object using ToPropertyDescriptor, then descObj will be that original object.
  1509. //10. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target, P, and descObj.
  1510. //11. Let booleanTrapResult be ToBoolean(trapResult).
  1511. //12. ReturnIfAbrupt(booleanTrapResult).
  1512. //13. If booleanTrapResult is false, then return false.
  1513. //14. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  1514. //15. ReturnIfAbrupt(targetDesc).
  1515. Var descVar = descriptor.GetOriginal();
  1516. if (descVar == nullptr)
  1517. {
  1518. descVar = JavascriptOperators::FromPropertyDescriptor(descriptor, requestContext);
  1519. }
  1520. Var propertyName = GetName(requestContext, propId);
  1521. Var definePropertyResult = threadContext->ExecuteImplicitCall(defineOwnPropertyMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1522. {
  1523. return CALL_FUNCTION(threadContext, defineOwnPropertyMethod, CallInfo(CallFlags_Value, 4), handlerObj, targetObj, propertyName, descVar);
  1524. });
  1525. BOOL defineResult = JavascriptConversion::ToBoolean(definePropertyResult, requestContext);
  1526. if (!defineResult)
  1527. {
  1528. return defineResult;
  1529. }
  1530. //16. Let extensibleTarget be the result of IsExtensible(target).
  1531. //17. ReturnIfAbrupt(extensibleTarget).
  1532. //18. If Desc has a[[Configurable]] field and if Desc.[[Configurable]] is false, then
  1533. // a.Let settingConfigFalse be true.
  1534. //19. Else let settingConfigFalse be false.
  1535. //20. If targetDesc is undefined, then
  1536. // a.If extensibleTarget is false, then throw a TypeError exception.
  1537. // b.If settingConfigFalse is true, then throw a TypeError exception.
  1538. //21. Else targetDesc is not undefined,
  1539. // a.If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, targetDesc) is false, then throw a TypeError exception.
  1540. // b.If settingConfigFalse is true and targetDesc.[[Configurable]] is true, then throw a TypeError exception.
  1541. //22. Return true.
  1542. PropertyDescriptor targetDescriptor;
  1543. BOOL hasProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propId, requestContext, &targetDescriptor);
  1544. BOOL isExtensible = targetObj->IsExtensible();
  1545. BOOL settingConfigFalse = (descriptor.ConfigurableSpecified() && !descriptor.IsConfigurable());
  1546. if (!hasProperty)
  1547. {
  1548. if (!isExtensible || settingConfigFalse)
  1549. {
  1550. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1551. }
  1552. }
  1553. else
  1554. {
  1555. if (!JavascriptOperators::IsCompatiblePropertyDescriptor(descriptor, hasProperty? &targetDescriptor : nullptr, !!isExtensible, true, requestContext))
  1556. {
  1557. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1558. }
  1559. if (settingConfigFalse && targetDescriptor.IsConfigurable())
  1560. {
  1561. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1562. }
  1563. }
  1564. return TRUE;
  1565. }
  1566. BOOL JavascriptProxy::SetPropertyTrap(Var receiver, SetPropertyTrapKind setPropertyTrapKind, Js::JavascriptString * propertyNameString, Var newValue, ScriptContext* requestContext)
  1567. {
  1568. const PropertyRecord* propertyRecord;
  1569. requestContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  1570. return SetPropertyTrap(receiver, setPropertyTrapKind, propertyRecord->GetPropertyId(), newValue, requestContext);
  1571. }
  1572. BOOL JavascriptProxy::SetPropertyTrap(Var receiver, SetPropertyTrapKind setPropertyTrapKind, PropertyId propertyId, Var newValue, ScriptContext* requestContext, BOOL skipPrototypeCheck)
  1573. {
  1574. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1575. // Reject implicit call
  1576. ThreadContext* threadContext = requestContext->GetThreadContext();
  1577. if (threadContext->IsDisableImplicitCall())
  1578. {
  1579. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1580. return FALSE;
  1581. }
  1582. //1. Assert: IsPropertyKey(P) is true.
  1583. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1584. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1585. //3. If handler is null, then throw a TypeError exception.
  1586. if (handlerObj == nullptr)
  1587. {
  1588. // the proxy has been revoked; TypeError.
  1589. if (!threadContext->RecordImplicitException())
  1590. return FALSE;
  1591. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("set"));
  1592. }
  1593. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1594. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1595. //5. Let trap be the result of GetMethod(handler, "set").
  1596. //6. ReturnIfAbrupt(trap).
  1597. //7. If trap is undefined, then
  1598. //a.Return the result of calling the[[Set]] internal method of target with arguments P, V, and Receiver.
  1599. JavascriptFunction* setMethod = GetMethodHelper(PropertyIds::set, requestContext);
  1600. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1601. if (nullptr == setMethod)
  1602. {
  1603. PropertyValueInfo info;
  1604. switch (setPropertyTrapKind)
  1605. {
  1606. case SetPropertyTrapKind::SetItemOnTaggedNumberKind:
  1607. {
  1608. uint32 indexVal;
  1609. BOOL isNumericPropertyId = requestContext->IsNumericPropertyId(propertyId, &indexVal);
  1610. Assert(isNumericPropertyId);
  1611. return JavascriptOperators::SetItemOnTaggedNumber(receiver, targetObj, indexVal, newValue, requestContext, PropertyOperationFlags::PropertyOperation_None);
  1612. }
  1613. case SetPropertyTrapKind::SetPropertyOnTaggedNumberKind:
  1614. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, targetObj, propertyId, newValue, requestContext, PropertyOperation_None);
  1615. case SetPropertyTrapKind::SetPropertyKind:
  1616. return JavascriptOperators::SetProperty(receiver, targetObj, propertyId, newValue, requestContext);
  1617. case SetPropertyTrapKind::SetItemKind:
  1618. {
  1619. uint32 indexVal;
  1620. BOOL isNumericPropertyId = requestContext->IsNumericPropertyId(propertyId, &indexVal);
  1621. Assert(isNumericPropertyId);
  1622. return JavascriptOperators::SetItem(receiver, targetObj, indexVal, newValue, requestContext, PropertyOperationFlags::PropertyOperation_None, skipPrototypeCheck);
  1623. }
  1624. case SetPropertyTrapKind::SetPropertyWPCacheKind:
  1625. {
  1626. PropertyValueInfo propertyValueInfo;
  1627. return JavascriptOperators::SetPropertyWPCache(receiver, targetObj, propertyId, newValue, requestContext, PropertyOperationFlags::PropertyOperation_None, &propertyValueInfo);
  1628. }
  1629. default:
  1630. AnalysisAssert(FALSE);
  1631. }
  1632. }
  1633. //8. Let trapResult be the result of calling the[[Call]] internal method of trap with handler as the this value and a new List containing target, P, V, and Receiver.
  1634. //9. Let booleanTrapResult be ToBoolean(trapResult).
  1635. //10. ReturnIfAbrupt(booleanTrapResult).
  1636. //11. If booleanTrapResult is false, then return false.
  1637. Var propertyName = GetName(requestContext, propertyId);
  1638. Var setPropertyResult = threadContext->ExecuteImplicitCall(setMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1639. {
  1640. return CALL_FUNCTION(threadContext, setMethod, CallInfo(CallFlags_Value, 5), handlerObj, targetObj, propertyName, newValue, receiver);
  1641. });
  1642. BOOL setResult = JavascriptConversion::ToBoolean(setPropertyResult, requestContext);
  1643. if (!setResult)
  1644. {
  1645. return setResult;
  1646. }
  1647. //12. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  1648. //13. ReturnIfAbrupt(targetDesc).
  1649. //14. If targetDesc is not undefined, then
  1650. //a.If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Writable]] is false, then
  1651. //i.If SameValue(V, targetDesc.[[Value]]) is false, then throw a TypeError exception.
  1652. //b.If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] is false, then
  1653. //i.If targetDesc.[[Set]] is undefined, then throw a TypeError exception.
  1654. //15. Return true
  1655. PropertyDescriptor targetDescriptor;
  1656. BOOL hasProperty;
  1657. hasProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetDescriptor);
  1658. if (hasProperty)
  1659. {
  1660. if (targetDescriptor.ValueSpecified())
  1661. {
  1662. if (!targetDescriptor.IsConfigurable() && !targetDescriptor.IsWritable() &&
  1663. !JavascriptConversion::SameValue(newValue, targetDescriptor.GetValue()))
  1664. {
  1665. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("set"));
  1666. }
  1667. }
  1668. else
  1669. {
  1670. if (!targetDescriptor.IsConfigurable() && targetDescriptor.GetSetter() == requestContext->GetLibrary()->GetDefaultAccessorFunction())
  1671. {
  1672. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("set"));
  1673. }
  1674. }
  1675. }
  1676. return TRUE;
  1677. }
  1678. JavascriptFunction* JavascriptProxy::GetMethodHelper(PropertyId methodId, ScriptContext* requestContext)
  1679. {
  1680. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1681. //3. If handler is null, then throw a TypeError exception.
  1682. if (this->handler == nullptr)
  1683. {
  1684. // the proxy has been revoked; TypeError.
  1685. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, requestContext->GetPropertyName(methodId)->GetBuffer());
  1686. }
  1687. Var varMethod;
  1688. //5. Let trap be the result of GetMethod(handler, "getOwnPropertyDescriptor").
  1689. //6. ReturnIfAbrupt(trap).
  1690. //7.3.9 GetMethod(V, P)
  1691. // The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
  1692. // property is expected to be a function. The operation is called with arguments V and P where V is the ECMAScript language value, P is the
  1693. // property key. This abstract operation performs the following steps:
  1694. // 1. Assert: IsPropertyKey(P) is true.
  1695. // 2. Let func be ? GetV(V, P).
  1696. // 3. If func is either undefined or null, return undefined.
  1697. // 4. If IsCallable(func) is false, throw a TypeError exception.
  1698. // 5. Return func.
  1699. BOOL result = JavascriptOperators::GetPropertyReference(handler, methodId, &varMethod, requestContext);
  1700. if (!result || JavascriptOperators::IsUndefinedOrNull(varMethod))
  1701. {
  1702. return nullptr;
  1703. }
  1704. if (!JavascriptFunction::Is(varMethod))
  1705. {
  1706. JavascriptError::ThrowTypeError(requestContext, JSERR_NeedFunction, requestContext->GetPropertyName(methodId)->GetBuffer());
  1707. }
  1708. JavascriptFunction* function = JavascriptFunction::FromVar(varMethod);
  1709. return JavascriptFunction::FromVar(CrossSite::MarshalVar(requestContext,
  1710. function, function->GetScriptContext()));
  1711. }
  1712. Var JavascriptProxy::GetValueFromDescriptor(Var instance, PropertyDescriptor propertyDescriptor, ScriptContext* requestContext)
  1713. {
  1714. if (propertyDescriptor.ValueSpecified())
  1715. {
  1716. return CrossSite::MarshalVar(requestContext, propertyDescriptor.GetValue());
  1717. }
  1718. if (propertyDescriptor.GetterSpecified())
  1719. {
  1720. return JavascriptOperators::CallGetter(RecyclableObject::FromVar(propertyDescriptor.GetGetter()), instance, requestContext);
  1721. }
  1722. Assert(FALSE);
  1723. return requestContext->GetLibrary()->GetUndefined();
  1724. }
  1725. void JavascriptProxy::PropertyIdFromInt(uint32 index, PropertyRecord const** propertyRecord)
  1726. {
  1727. char16 buffer[22];
  1728. int pos = TaggedInt::ToBuffer(index, buffer, _countof(buffer));
  1729. GetScriptContext()->GetOrAddPropertyRecord((LPCWSTR)buffer + pos, (_countof(buffer) - 1) - pos, propertyRecord);
  1730. }
  1731. Var JavascriptProxy::GetName(ScriptContext* requestContext, PropertyId propertyId)
  1732. {
  1733. const PropertyRecord* propertyRecord = requestContext->GetThreadContext()->GetPropertyName(propertyId);
  1734. Var name;
  1735. if (propertyRecord->IsSymbol())
  1736. {
  1737. name = requestContext->GetSymbol(propertyRecord);
  1738. }
  1739. else
  1740. {
  1741. name = requestContext->GetPropertyString(propertyRecord);
  1742. }
  1743. return name;
  1744. }
  1745. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1746. PropertyId JavascriptProxy::EnsureHandlerPropertyId(ScriptContext* scriptContext)
  1747. {
  1748. ThreadContext* threadContext = scriptContext->GetThreadContext();
  1749. if (threadContext->handlerPropertyId == Js::Constants::NoProperty)
  1750. {
  1751. LPCWSTR autoProxyName;
  1752. if (threadContext->GetAutoProxyName() != nullptr)
  1753. {
  1754. autoProxyName = threadContext->GetAutoProxyName();
  1755. }
  1756. else
  1757. {
  1758. autoProxyName = Js::Configuration::Global.flags.autoProxy;
  1759. }
  1760. threadContext->handlerPropertyId = threadContext->GetOrAddPropertyRecordBind(
  1761. JsUtil::CharacterBuffer<WCHAR>(autoProxyName, static_cast<charcount_t>(wcslen(autoProxyName))))->GetPropertyId();
  1762. }
  1763. return threadContext->handlerPropertyId;
  1764. }
  1765. RecyclableObject* JavascriptProxy::AutoProxyWrapper(Var obj)
  1766. {
  1767. RecyclableObject* object = RecyclableObject::FromVar(obj);
  1768. if (!JavascriptOperators::IsObject(object) || JavascriptProxy::Is(object))
  1769. {
  1770. return object;
  1771. }
  1772. ScriptContext* scriptContext = object->GetScriptContext();
  1773. if (!scriptContext->GetThreadContext()->IsScriptActive())
  1774. {
  1775. return object;
  1776. }
  1777. if (!scriptContext->GetConfig()->IsES6ProxyEnabled())
  1778. {
  1779. return object;
  1780. }
  1781. Assert(Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag));
  1782. PropertyId handlerId = EnsureHandlerPropertyId(scriptContext);
  1783. GlobalObject* globalObject = scriptContext->GetLibrary()->GetGlobalObject();
  1784. Var handler = nullptr;
  1785. if (!JavascriptOperators::GetProperty(globalObject, handlerId, &handler, scriptContext))
  1786. {
  1787. handler = scriptContext->GetLibrary()->CreateObject();
  1788. JavascriptOperators::SetProperty(globalObject, globalObject, handlerId, handler, scriptContext);
  1789. }
  1790. CallInfo callInfo(CallFlags_Value, 3);
  1791. Var varArgs[3];
  1792. Js::Arguments arguments(callInfo, varArgs);
  1793. varArgs[0] = scriptContext->GetLibrary()->GetProxyConstructor();
  1794. varArgs[1] = object;
  1795. varArgs[2] = handler;
  1796. return Create(scriptContext, arguments);
  1797. }
  1798. #endif
  1799. Var JavascriptProxy::ConstructorTrap(Arguments args, ScriptContext* scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  1800. {
  1801. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1802. Var functionResult;
  1803. if (spreadIndices != nullptr)
  1804. {
  1805. functionResult = JavascriptFunction::CallSpreadFunction(this, args, spreadIndices);
  1806. }
  1807. else
  1808. {
  1809. functionResult = JavascriptFunction::CallFunction<true>(this, this->GetEntryPoint(), args);
  1810. }
  1811. return functionResult;
  1812. }
  1813. Var JavascriptProxy::FunctionCallTrap(RecyclableObject* function, CallInfo callInfo, ...)
  1814. {
  1815. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1816. ARGUMENTS(args, callInfo);
  1817. ScriptContext* scriptContext = function->GetScriptContext();
  1818. BOOL hasOverridingNewTarget = args.HasNewTarget();
  1819. bool isCtorSuperCall = JavascriptOperators::GetAndAssertIsConstructorSuperCall(args);
  1820. bool isNewCall = args.IsNewCall() || hasOverridingNewTarget;
  1821. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1822. if (!JavascriptProxy::Is(function))
  1823. {
  1824. if (args.Info.Flags & CallFlags_New)
  1825. {
  1826. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("construct"));
  1827. }
  1828. else
  1829. {
  1830. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("call"));
  1831. }
  1832. }
  1833. Var newTarget = nullptr;
  1834. JavascriptProxy* proxy = JavascriptProxy::FromVar(function);
  1835. Js::RecyclableObject *handlerObj = proxy->handler;
  1836. Js::RecyclableObject *targetObj = proxy->target;
  1837. JavascriptFunction* callMethod;
  1838. Assert(!scriptContext->IsHeapEnumInProgress());
  1839. // To conform with ES6 spec 7.3.13
  1840. if (hasOverridingNewTarget)
  1841. {
  1842. newTarget = args.Values[callInfo.Count];
  1843. }
  1844. else
  1845. {
  1846. newTarget = proxy;
  1847. }
  1848. if (args.Info.Flags & CallFlags_New)
  1849. {
  1850. callMethod = proxy->GetMethodHelper(PropertyIds::construct, scriptContext);
  1851. }
  1852. else
  1853. {
  1854. callMethod = proxy->GetMethodHelper(PropertyIds::apply, scriptContext);
  1855. }
  1856. if (!JavascriptConversion::IsCallable(targetObj))
  1857. {
  1858. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("call"));
  1859. }
  1860. if (nullptr == callMethod)
  1861. {
  1862. // newCount is ushort. If args count is greater than or equal to 65535, an integer
  1863. // too many arguments
  1864. if (args.Info.Count >= USHORT_MAX) //check against CallInfo::kMaxCountArgs if newCount is ever made int
  1865. {
  1866. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  1867. }
  1868. // in [[construct]] case, we don't need to check if the function is a constructor: the function should throw there.
  1869. Var newThisObject = nullptr;
  1870. if (args.Info.Flags & CallFlags_New)
  1871. {
  1872. if (!JavascriptOperators::IsConstructor(targetObj))
  1873. {
  1874. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, _u("construct"));
  1875. }
  1876. newThisObject = JavascriptOperators::NewScObjectNoCtor(targetObj, scriptContext);
  1877. args.Values[0] = newThisObject;
  1878. }
  1879. ushort newCount = (ushort)args.Info.Count;
  1880. if (isNewCall)
  1881. {
  1882. newCount++;
  1883. if (!newCount)
  1884. {
  1885. ::Math::DefaultOverflowPolicy();
  1886. }
  1887. }
  1888. Var* newValues;
  1889. const unsigned STACK_ARGS_ALLOCA_THRESHOLD = 8; // Number of stack args we allow before using _alloca
  1890. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  1891. if (newCount > STACK_ARGS_ALLOCA_THRESHOLD)
  1892. {
  1893. PROBE_STACK(scriptContext, newCount * sizeof(Var) + Js::Constants::MinStackDefault); // args + function call
  1894. newValues = (Var*)_alloca(newCount * sizeof(Var));
  1895. }
  1896. else
  1897. {
  1898. newValues = stackArgs;
  1899. }
  1900. CallInfo calleeInfo((CallFlags)(args.Info.Flags), args.Info.Count);
  1901. if (isNewCall)
  1902. {
  1903. calleeInfo.Flags = (CallFlags)(calleeInfo.Flags | CallFlags_ExtraArg | CallFlags_NewTarget);
  1904. }
  1905. for (uint argCount = 0; argCount < args.Info.Count; argCount++)
  1906. {
  1907. newValues[argCount] = args.Values[argCount];
  1908. }
  1909. #pragma prefast(suppress:6386)
  1910. if (isNewCall)
  1911. {
  1912. newValues[args.Info.Count] = newTarget;
  1913. }
  1914. Js::Arguments arguments(calleeInfo, newValues);
  1915. Var aReturnValue = JavascriptFunction::CallFunction<true>(targetObj, targetObj->GetEntryPoint(), arguments);
  1916. // If this is constructor call, return the actual object instead of function result
  1917. if ((callInfo.Flags & CallFlags_New) && !JavascriptOperators::IsObject(aReturnValue))
  1918. {
  1919. aReturnValue = newThisObject;
  1920. }
  1921. return aReturnValue;
  1922. }
  1923. JavascriptArray* argList = scriptContext->GetLibrary()->CreateArray(callInfo.Count - 1);
  1924. for (uint i = 1; i < callInfo.Count; i++)
  1925. {
  1926. argList->DirectSetItemAt(i - 1, args[i]);
  1927. }
  1928. Var varArgs[4];
  1929. CallInfo calleeInfo(CallFlags_Value, 4);
  1930. Js::Arguments arguments(calleeInfo, varArgs);
  1931. varArgs[0] = handlerObj;
  1932. varArgs[1] = targetObj;
  1933. if (args.Info.Flags & CallFlags_New)
  1934. {
  1935. if (!JavascriptOperators::IsConstructor(targetObj))
  1936. {
  1937. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  1938. }
  1939. varArgs[2] = argList;
  1940. // 1st preference - overridden newTarget
  1941. // 2nd preference - 'this' in case of super() call
  1942. // 3rd preference - newTarget ( which is same as F)
  1943. varArgs[3] = hasOverridingNewTarget ? newTarget :
  1944. isCtorSuperCall ? args[0] : newTarget;
  1945. }
  1946. else
  1947. {
  1948. varArgs[2] = args[0];
  1949. varArgs[3] = argList;
  1950. }
  1951. Var trapResult = callMethod->CallFunction(arguments);
  1952. if (args.Info.Flags & CallFlags_New)
  1953. {
  1954. if (!Js::JavascriptOperators::IsObject(trapResult))
  1955. {
  1956. JavascriptError::ThrowTypeError(scriptContext, JSERR_InconsistentTrapResult, _u("construct"));
  1957. }
  1958. }
  1959. return trapResult;
  1960. }
  1961. JavascriptArray* JavascriptProxy::PropertyKeysTrap(KeysTrapKind keysTrapKind, ScriptContext* requestContext)
  1962. {
  1963. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1964. // Reject implicit call
  1965. ThreadContext* threadContext = requestContext->GetThreadContext();
  1966. if (threadContext->IsDisableImplicitCall())
  1967. {
  1968. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1969. return nullptr;
  1970. }
  1971. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1972. RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1973. //2. If handler is null, throw a TypeError exception.
  1974. //3. Assert: Type(handler) is Object.
  1975. if (handlerObj == nullptr)
  1976. {
  1977. // the proxy has been revoked; TypeError.
  1978. if (!threadContext->RecordImplicitException())
  1979. return nullptr;
  1980. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("ownKeys"));
  1981. }
  1982. AssertMsg(JavascriptOperators::IsObject(handlerObj), "Handler should be object.");
  1983. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1984. RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1985. //5. Let trap be GetMethod(handler, "ownKeys").
  1986. //6. ReturnIfAbrupt(trap).
  1987. //7. If trap is undefined, then
  1988. // a. Return target.[[OwnPropertyKeys]]().
  1989. JavascriptFunction* ownKeysMethod = GetMethodHelper(PropertyIds::ownKeys, requestContext);
  1990. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1991. JavascriptArray *targetKeys;
  1992. if (nullptr == ownKeysMethod)
  1993. {
  1994. switch (keysTrapKind)
  1995. {
  1996. case GetOwnPropertyNamesKind:
  1997. targetKeys = JavascriptOperators::GetOwnPropertyNames(targetObj, requestContext);
  1998. break;
  1999. case GetOwnPropertySymbolKind:
  2000. targetKeys = JavascriptOperators::GetOwnPropertySymbols(targetObj, requestContext);
  2001. break;
  2002. case KeysKind:
  2003. targetKeys = JavascriptOperators::GetOwnPropertyKeys(targetObj, requestContext);
  2004. break;
  2005. default:
  2006. AssertMsg(false, "Invalid KeysTrapKind.");
  2007. return requestContext->GetLibrary()->CreateArray(0);
  2008. }
  2009. return targetKeys;
  2010. }
  2011. //8. Let trapResultArray be Call(trap, handler, <<target>>).
  2012. //9. Let trapResult be CreateListFromArrayLike(trapResultArray, <<String, Symbol>>).
  2013. //10. ReturnIfAbrupt(trapResult).
  2014. //11. Let extensibleTarget be IsExtensible(target).
  2015. //12. ReturnIfAbrupt(extensibleTarget).
  2016. //13. Let targetKeys be target.[[OwnPropertyKeys]]().
  2017. //14. ReturnIfAbrupt(targetKeys).
  2018. Var ownKeysResult = threadContext->ExecuteImplicitCall(ownKeysMethod, ImplicitCall_Accessor, [=]()->Js::Var
  2019. {
  2020. return CALL_FUNCTION(threadContext, ownKeysMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  2021. });
  2022. if (!JavascriptOperators::IsObject(ownKeysResult))
  2023. {
  2024. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2025. }
  2026. RecyclableObject* trapResultArray = RecyclableObject::FromVar(ownKeysResult);
  2027. BOOL isTargetExtensible = targetObj->IsExtensible();
  2028. targetKeys = JavascriptOperators::GetOwnPropertyKeys(targetObj, requestContext);
  2029. //15. Assert: targetKeys is a List containing only String and Symbol values.
  2030. //16. Let targetConfigurableKeys be an empty List.
  2031. //17. Let targetNonconfigurableKeys be an empty List.
  2032. //18. Repeat, for each element key of targetKeys,
  2033. // a.Let desc be target.[[GetOwnProperty]](key).
  2034. // b.ReturnIfAbrupt(desc).
  2035. // c.If desc is not undefined and desc.[[Configurable]] is false, then
  2036. // i.Append key as an element of targetNonconfigurableKeys.
  2037. // d.Else,
  2038. // i.Append key as an element of targetConfigurableKeys.
  2039. //19. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
  2040. // a. Return trapResult.
  2041. //20. Let uncheckedResultKeys be a new List which is a copy of trapResult.
  2042. //21. Repeat, for each key that is an element of targetNonconfigurableKeys,
  2043. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  2044. // b. Remove key from uncheckedResultKeys
  2045. //22. If extensibleTarget is true, return trapResult.
  2046. /*
  2047. To avoid creating targetConfigurableKeys, targetNonconfigurableKeys and uncheckedResultKeys list in above steps,
  2048. use below algorithm to accomplish same behavior
  2049. // Track if there are any properties that are present in target but not present in trap result
  2050. for(var i = 0; i < trapResult.length; i++)
  2051. {
  2052. PropertyId propId = GetPropertyId(trapResult[i]);
  2053. if(propId != NoProperty) { targetToTrapResultMap[propId] = 1; }
  2054. else { isTrapResultMissingFromTargetKeys = true; }
  2055. }
  2056. isConfigurableKeyMissingFromTrapResult = false;
  2057. isNonconfigurableKeyMissingFromTrapResult = false;
  2058. for(var i = 0; i < targetKeys.length; i++)
  2059. {
  2060. PropertyId propId = GetPropertyId(targetKeys[i]);
  2061. Var desc = GetPropertyDescriptor(propId);
  2062. if(targetToTrapResultMap[propId]) {
  2063. delete targetToTrapResultMap[propId];
  2064. isMissingFromTrapResult = false;
  2065. } else {
  2066. isMissingFromTrapResult = true;
  2067. }
  2068. if(desc->IsConfigurable()) {
  2069. if(isMissingFromTrapResult) {
  2070. isConfigurableKeyMissingFromTrapResult = true;
  2071. }
  2072. } else {
  2073. isAnyNonconfigurableKeyPresent = true
  2074. if(isMissingFromTrapResult) {
  2075. isNonconfigurableKeyMissingFromTrapResult = true;
  2076. }
  2077. }
  2078. }
  2079. // 19.
  2080. if(isExtensible && !isAnyNonconfigurableKeyPresent) { return trapResult; }
  2081. // 21.
  2082. if(isNonconfigurableKeyMissingFromTrapResult) { throw TypeError; }
  2083. // 22.
  2084. if(isExtensible) { return trapResult; }
  2085. // 23.
  2086. if(isConfigurableKeyMissingFromTrapResult) { throw TypeError; }
  2087. // 24.
  2088. if(!targetToTrapResultMap.Empty()) { throw TypeError; }
  2089. return trapResult;
  2090. */
  2091. JavascriptArray* trapResult = requestContext->GetLibrary()->CreateArray(0);
  2092. bool isConfigurableKeyMissingFromTrapResult = false;
  2093. bool isNonconfigurableKeyMissingFromTrapResult = false;
  2094. bool isKeyMissingFromTrapResult = false;
  2095. bool isKeyMissingFromTargetResult = false;
  2096. bool isAnyNonconfigurableKeyPresent = false;
  2097. Var element;
  2098. PropertyId propertyId;
  2099. const PropertyRecord* propertyRecord = nullptr;
  2100. BEGIN_TEMP_ALLOCATOR(tempAllocator, requestContext, _u("Runtime"))
  2101. {
  2102. // Dictionary containing intersection of keys present in targetKeys and trapResult
  2103. Var lenValue = JavascriptOperators::OP_GetLength(trapResultArray, requestContext);
  2104. uint32 len = (uint32)JavascriptConversion::ToLength(lenValue, requestContext);
  2105. JsUtil::BaseDictionary<Js::PropertyId, bool, ArenaAllocator> targetToTrapResultMap(tempAllocator, len);
  2106. // Trap result to return.
  2107. // Note : This will not necessarily have all elements present in trapResultArray. E.g. If trap was called from GetOwnPropertySymbols()
  2108. // trapResult will only contain symbol elements from trapResultArray.
  2109. switch (keysTrapKind)
  2110. {
  2111. case GetOwnPropertyNamesKind:
  2112. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2113. [&](const PropertyRecord *propertyRecord)->bool
  2114. {
  2115. return !propertyRecord->IsSymbol();
  2116. });
  2117. break;
  2118. case GetOwnPropertySymbolKind:
  2119. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2120. [&](const PropertyRecord *propertyRecord)->bool
  2121. {
  2122. return propertyRecord->IsSymbol();
  2123. });
  2124. break;
  2125. case KeysKind:
  2126. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2127. [&](const PropertyRecord *propertyRecord)->bool
  2128. {
  2129. return true;
  2130. });
  2131. break;
  2132. }
  2133. for (uint32 i = 0; i < targetKeys->GetLength(); i++)
  2134. {
  2135. element = targetKeys->DirectGetItem(i);
  2136. AssertMsg(JavascriptSymbol::Is(element) || JavascriptString::Is(element), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  2137. JavascriptConversion::ToPropertyKey(element, requestContext, &propertyRecord, nullptr);
  2138. propertyId = propertyRecord->GetPropertyId();
  2139. if (propertyId == Constants::NoProperty)
  2140. continue;
  2141. // If not present in intersection means either the property is not present in targetKeys or
  2142. // we have already visited the property in targetKeys
  2143. if (targetToTrapResultMap.ContainsKey(propertyId))
  2144. {
  2145. isKeyMissingFromTrapResult = false;
  2146. targetToTrapResultMap.Remove(propertyId);
  2147. }
  2148. else
  2149. {
  2150. isKeyMissingFromTrapResult = true;
  2151. }
  2152. PropertyDescriptor targetKeyPropertyDescriptor;
  2153. if (Js::JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetKeyPropertyDescriptor) && !targetKeyPropertyDescriptor.IsConfigurable())
  2154. {
  2155. isAnyNonconfigurableKeyPresent = true;
  2156. if (isKeyMissingFromTrapResult)
  2157. {
  2158. isNonconfigurableKeyMissingFromTrapResult = true;
  2159. }
  2160. }
  2161. else
  2162. {
  2163. if (isKeyMissingFromTrapResult)
  2164. {
  2165. isConfigurableKeyMissingFromTrapResult = true;
  2166. }
  2167. }
  2168. }
  2169. // Keys that were not found in targetKeys will continue to remain in the map
  2170. isKeyMissingFromTargetResult = targetToTrapResultMap.Count() != 0;
  2171. }
  2172. END_TEMP_ALLOCATOR(tempAllocator, requestContext)
  2173. // 19.
  2174. if (isTargetExtensible && !isAnyNonconfigurableKeyPresent)
  2175. {
  2176. return trapResult;
  2177. }
  2178. // 21.
  2179. if (isNonconfigurableKeyMissingFromTrapResult)
  2180. {
  2181. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2182. }
  2183. // 22.
  2184. if (isTargetExtensible)
  2185. {
  2186. return trapResult;
  2187. }
  2188. // 23.
  2189. if (isConfigurableKeyMissingFromTrapResult)
  2190. {
  2191. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2192. }
  2193. // 24.
  2194. if (isKeyMissingFromTargetResult)
  2195. {
  2196. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2197. }
  2198. return trapResult;
  2199. }
  2200. #if ENABLE_TTD
  2201. void JavascriptProxy::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
  2202. {
  2203. if(this->handler != nullptr)
  2204. {
  2205. extractor->MarkVisitVar(this->handler);
  2206. }
  2207. if(this->target != nullptr)
  2208. {
  2209. extractor->MarkVisitVar(this->target);
  2210. }
  2211. }
  2212. TTD::NSSnapObjects::SnapObjectType JavascriptProxy::GetSnapTag_TTD() const
  2213. {
  2214. return TTD::NSSnapObjects::SnapObjectType::SnapProxyObject;
  2215. }
  2216. void JavascriptProxy::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  2217. {
  2218. TTD::NSSnapObjects::SnapProxyInfo* spi = alloc.SlabAllocateStruct<TTD::NSSnapObjects::SnapProxyInfo>();
  2219. const uint32 reserveSize = 2;
  2220. uint32 depOnCount = 0;
  2221. TTD_PTR_ID* depOnArray = alloc.SlabReserveArraySpace<TTD_PTR_ID>(reserveSize);
  2222. spi->HandlerId = TTD_INVALID_PTR_ID;
  2223. if(this->handler != nullptr)
  2224. {
  2225. spi->HandlerId = TTD_CONVERT_VAR_TO_PTR_ID(this->handler);
  2226. if(TTD::JsSupport::IsVarComplexKind(this->handler))
  2227. {
  2228. depOnArray[depOnCount] = TTD_CONVERT_VAR_TO_PTR_ID(this->handler);
  2229. depOnCount++;
  2230. }
  2231. }
  2232. spi->TargetId = TTD_INVALID_PTR_ID;
  2233. if(this->target != nullptr)
  2234. {
  2235. spi->TargetId = TTD_CONVERT_VAR_TO_PTR_ID(this->target);
  2236. if(TTD::JsSupport::IsVarComplexKind(this->handler))
  2237. {
  2238. depOnArray[depOnCount] = TTD_CONVERT_VAR_TO_PTR_ID(this->target);
  2239. depOnCount++;
  2240. }
  2241. }
  2242. if(depOnCount == 0)
  2243. {
  2244. alloc.SlabAbortArraySpace<TTD_PTR_ID>(reserveSize);
  2245. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapProxyInfo*, TTD::NSSnapObjects::SnapObjectType::SnapProxyObject>(objData, spi);
  2246. }
  2247. else
  2248. {
  2249. alloc.SlabCommitArraySpace<TTD_PTR_ID>(depOnCount, reserveSize);
  2250. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapProxyInfo*, TTD::NSSnapObjects::SnapObjectType::SnapProxyObject>(objData, spi, alloc, depOnCount, depOnArray);
  2251. }
  2252. }
  2253. #endif
  2254. }