JavascriptProxy.cpp 114 KB

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