JavascriptProxy.cpp 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Copyright (c) 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, flags);
  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, flags);
  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::ThrowTypeErrorVar(
  718. requestContext,
  719. JSERR_ProxyHandlerReturnedFalse,
  720. _u("deleteProperty"),
  721. threadContext->GetPropertyName(propertyId)->GetBuffer()
  722. );
  723. }
  724. return trapResult;
  725. }
  726. //12. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  727. //13. ReturnIfAbrupt(targetDesc).
  728. //14. If targetDesc is undefined, then return true.
  729. //15. If targetDesc.[[Configurable]] is false, then throw a TypeError exception.
  730. //16. Return true.
  731. PropertyDescriptor targetPropertyDescriptor;
  732. if (!Js::JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetPropertyDescriptor))
  733. {
  734. return TRUE;
  735. }
  736. if (!targetPropertyDescriptor.IsConfigurable())
  737. {
  738. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("deleteProperty"));
  739. }
  740. return TRUE;
  741. }
  742. BOOL JavascriptProxy::DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags)
  743. {
  744. PropertyRecord const *propertyRecord = nullptr;
  745. if (JavascriptOperators::ShouldTryDeleteProperty(this, propertyNameString, &propertyRecord))
  746. {
  747. Assert(propertyRecord);
  748. return DeleteProperty(propertyRecord->GetPropertyId(), flags);
  749. }
  750. return TRUE;
  751. }
  752. #if ENABLE_FIXED_FIELDS
  753. BOOL JavascriptProxy::IsFixedProperty(PropertyId propertyId)
  754. {
  755. // TODO: can we add support for fixed property? don't see a clear way to invalidate...
  756. return false;
  757. }
  758. #endif
  759. PropertyQueryFlags JavascriptProxy::HasItemQuery(uint32 index)
  760. {
  761. const PropertyRecord* propertyRecord;
  762. auto fn = [&](RecyclableObject* object)-> BOOL {
  763. return JavascriptOperators::HasItem(object, index);
  764. };
  765. auto getPropertyId = [&]() ->PropertyId {
  766. PropertyIdFromInt(index, &propertyRecord);
  767. return propertyRecord->GetPropertyId();
  768. };
  769. return JavascriptConversion::BooleanToPropertyQueryFlags(HasPropertyTrap(fn, getPropertyId));
  770. }
  771. BOOL JavascriptProxy::HasOwnItem(uint32 index)
  772. {
  773. const PropertyRecord* propertyRecord;
  774. auto fn = [&](RecyclableObject* object)-> BOOL {
  775. return JavascriptOperators::HasOwnItem(object, index);
  776. };
  777. auto getPropertyId = [&]() ->PropertyId {
  778. PropertyIdFromInt(index, &propertyRecord);
  779. return propertyRecord->GetPropertyId();
  780. };
  781. return HasPropertyTrap(fn, getPropertyId);
  782. }
  783. PropertyQueryFlags JavascriptProxy::GetItemQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  784. {
  785. const PropertyRecord* propertyRecord;
  786. auto fn = [&](RecyclableObject* object)-> BOOL {
  787. return JavascriptOperators::GetItem(originalInstance, object, index, value, requestContext);
  788. };
  789. auto getPropertyId = [&]() ->PropertyId {
  790. PropertyIdFromInt(index, &propertyRecord);
  791. return propertyRecord->GetPropertyId();
  792. };
  793. PropertyDescriptor result;
  794. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  795. if (!foundProperty)
  796. {
  797. *value = requestContext->GetMissingItemResult();
  798. }
  799. else if (result.IsFromProxy())
  800. {
  801. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  802. }
  803. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  804. }
  805. PropertyQueryFlags JavascriptProxy::GetItemReferenceQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  806. {
  807. const PropertyRecord* propertyRecord;
  808. auto fn = [&](RecyclableObject* object)-> BOOL {
  809. return JavascriptOperators::GetItem(originalInstance, object, index, value, requestContext);
  810. };
  811. auto getPropertyId = [&]() ->PropertyId {
  812. PropertyIdFromInt(index, &propertyRecord);
  813. return propertyRecord->GetPropertyId();
  814. };
  815. PropertyDescriptor result;
  816. BOOL foundProperty = GetPropertyTrap(originalInstance, &result, fn, getPropertyId, requestContext);
  817. if (!foundProperty)
  818. {
  819. *value = requestContext->GetMissingItemResult();
  820. }
  821. else if (result.IsFromProxy())
  822. {
  823. *value = GetValueFromDescriptor(originalInstance, result, requestContext);
  824. }
  825. return JavascriptConversion::BooleanToPropertyQueryFlags(foundProperty);
  826. }
  827. DescriptorFlags JavascriptProxy::GetItemSetter(uint32 index, Var* setterValueOrProxy, ScriptContext* requestContext)
  828. {
  829. *setterValueOrProxy = this;
  830. return DescriptorFlags::Proxy;
  831. }
  832. BOOL JavascriptProxy::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
  833. {
  834. const PropertyRecord* propertyRecord;
  835. PropertyIdFromInt(index, &propertyRecord);
  836. return SetProperty(propertyRecord->GetPropertyId(), value, flags, nullptr);
  837. }
  838. BOOL JavascriptProxy::DeleteItem(uint32 index, PropertyOperationFlags flags)
  839. {
  840. const PropertyRecord* propertyRecord;
  841. PropertyIdFromInt(index, &propertyRecord);
  842. return DeleteProperty(propertyRecord->GetPropertyId(), flags);
  843. }
  844. // No change to foreign enumerator, just forward
  845. BOOL JavascriptProxy::GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, EnumeratorCache * enumeratorCache)
  846. {
  847. // Reject implicit call
  848. ThreadContext* threadContext = requestContext->GetThreadContext();
  849. if (threadContext->IsDisableImplicitCall())
  850. {
  851. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  852. return FALSE;
  853. }
  854. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  855. // 2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  856. // 3. If handler is null, then throw a TypeError exception.
  857. if (IsRevoked())
  858. {
  859. // the proxy has been revoked; TypeError.
  860. if (!threadContext->RecordImplicitException())
  861. return FALSE;
  862. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("ownKeys"));
  863. }
  864. struct ProxyOwnkeysEnumerator : public JavascriptEnumerator
  865. {
  866. typedef JsUtil::BaseHashSet<JsUtil::CharacterBuffer<WCHAR>, Recycler> VisitedNamesHashSet;
  867. Field(VisitedNamesHashSet*) visited;
  868. Field(JavascriptArray*) trapResult;
  869. Field(JavascriptProxy*) proxy;
  870. FieldNoBarrier(ScriptContext*) scriptContext;
  871. Field(uint32) index;
  872. DEFINE_VTABLE_CTOR_ABSTRACT(ProxyOwnkeysEnumerator, JavascriptEnumerator)
  873. ProxyOwnkeysEnumerator(ScriptContext* scriptContext, JavascriptProxy* proxy, JavascriptArray* trapResult)
  874. :JavascriptEnumerator(scriptContext), scriptContext(scriptContext), proxy(proxy), trapResult(trapResult)
  875. {
  876. visited = RecyclerNew(scriptContext->GetRecycler(), VisitedNamesHashSet, scriptContext->GetRecycler());
  877. }
  878. virtual void Reset() override
  879. {
  880. index = 0;
  881. visited->Reset();
  882. }
  883. virtual JavascriptString * MoveAndGetNext(PropertyId& propertyId, PropertyAttributes* attributes = nullptr) override
  884. {
  885. propertyId = Constants::NoProperty;
  886. if (attributes != nullptr)
  887. {
  888. *attributes = PropertyEnumerable;
  889. }
  890. // 13.7.5.15 EnumerateObjectProperties(O) (https://tc39.github.io/ecma262/#sec-enumerate-object-properties)
  891. // for (let key of Reflect.ownKeys(obj)) {
  892. uint32 len = trapResult->GetLength();
  893. while (index < len)
  894. {
  895. Var var = trapResult->DirectGetItem(index++) ;
  896. if (var)
  897. {
  898. // if (typeof key === "string") {
  899. if (VarIs<JavascriptString>(var))
  900. {
  901. JavascriptString* propertyName = VarTo<JavascriptString>(var);
  902. // let desc = Reflect.getOwnPropertyDescriptor(obj, key);
  903. Js::PropertyDescriptor desc;
  904. BOOL ret = JavascriptOperators::GetOwnPropertyDescriptor(proxy, propertyName, scriptContext, &desc);
  905. const JsUtil::CharacterBuffer<WCHAR> propertyString(propertyName->GetString(), propertyName->GetLength());
  906. // if (desc && !visited.has(key)) {
  907. if (ret && !visited->Contains(propertyString))
  908. {
  909. visited->Add(propertyString);
  910. // if (desc.enumerable) yield key;
  911. if (desc.IsEnumerable())
  912. {
  913. return VarTo<JavascriptString>(CrossSite::MarshalVar(
  914. scriptContext, propertyName, propertyName->GetScriptContext()));
  915. }
  916. }
  917. }
  918. }
  919. }
  920. return nullptr;
  921. }
  922. };
  923. JavascriptArray* trapResult = JavascriptOperators::GetOwnPropertyNames(this, requestContext);
  924. ProxyOwnkeysEnumerator* ownKeysEnum = RecyclerNew(requestContext->GetRecycler(), ProxyOwnkeysEnumerator, requestContext, this, trapResult);
  925. return enumerator->Initialize(ownKeysEnum, nullptr, nullptr, flags, requestContext, enumeratorCache);
  926. }
  927. BOOL JavascriptProxy::SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  928. {
  929. // should be for __definegetter style usage. need to wait for clear spec what it means.
  930. Assert(FALSE);
  931. return false;
  932. }
  933. BOOL JavascriptProxy::Equals(__in Var other, __out BOOL* value, ScriptContext* requestContext)
  934. {
  935. //RecyclableObject* targetObj;
  936. if (IsRevoked())
  937. {
  938. // the proxy has been revoked; TypeError.
  939. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("equal"));
  940. }
  941. // Reject implicit call
  942. ThreadContext* threadContext = requestContext->GetThreadContext();
  943. if (threadContext->IsDisableImplicitCall())
  944. {
  945. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  946. *value = FALSE;
  947. return FALSE;
  948. }
  949. *value = (other == this);
  950. return true;
  951. }
  952. BOOL JavascriptProxy::StrictEquals(__in Var other, __out BOOL* value, ScriptContext* requestContext)
  953. {
  954. *value = FALSE;
  955. //RecyclableObject* targetObj;
  956. if (IsRevoked())
  957. {
  958. // the proxy has been revoked; TypeError.
  959. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("strict equal"));
  960. }
  961. // Reject implicit call
  962. ThreadContext* threadContext = requestContext->GetThreadContext();
  963. if (threadContext->IsDisableImplicitCall())
  964. {
  965. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  966. return FALSE;
  967. }
  968. *value = (other == this);
  969. return true;
  970. }
  971. BOOL JavascriptProxy::IsWritable(PropertyId propertyId)
  972. {
  973. PropertyDescriptor propertyDescriptor;
  974. if (!GetOwnPropertyDescriptor(this, propertyId, GetScriptContext(), &propertyDescriptor))
  975. {
  976. return FALSE;
  977. }
  978. // If property descriptor has getter/setter we should check if writable is specified before checking IsWritable
  979. return propertyDescriptor.WritableSpecified() ? propertyDescriptor.IsWritable() : FALSE;
  980. }
  981. BOOL JavascriptProxy::IsConfigurable(PropertyId propertyId)
  982. {
  983. Assert(FALSE);
  984. return target->IsConfigurable(propertyId);
  985. }
  986. BOOL JavascriptProxy::IsEnumerable(PropertyId propertyId)
  987. {
  988. Assert(FALSE);
  989. return target->IsEnumerable(propertyId);
  990. }
  991. BOOL JavascriptProxy::IsExtensible()
  992. {
  993. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  994. // Reject implicit call
  995. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  996. if (threadContext->IsDisableImplicitCall())
  997. {
  998. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  999. return FALSE;
  1000. }
  1001. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  1002. ScriptContext* requestContext =
  1003. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  1004. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1005. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1006. //2. If handler is null, then throw a TypeError exception.
  1007. if (handlerObj == nullptr)
  1008. {
  1009. // the proxy has been revoked; TypeError.
  1010. if (!threadContext->RecordImplicitException())
  1011. return FALSE;
  1012. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("isExtensible"));
  1013. }
  1014. //3. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1015. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1016. //4. Let trap be the result of GetMethod(handler, "isExtensible").
  1017. //5. ReturnIfAbrupt(trap).
  1018. //6. If trap is undefined, then
  1019. //a.Return the result of calling the[[IsExtensible]] internal method of target.
  1020. //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.
  1021. //8. Let booleanTrapResult be ToBoolean(trapResult).
  1022. //9. ReturnIfAbrupt(booleanTrapResult).
  1023. //10. Let targetResult be the result of calling the[[IsExtensible]] internal method of target.
  1024. //11. ReturnIfAbrupt(targetResult).
  1025. //12. If SameValue(booleanTrapResult, targetResult) is false, then throw a TypeError exception.
  1026. //13. Return booleanTrapResult.
  1027. JavascriptFunction* isExtensibleMethod = GetMethodHelper(PropertyIds::isExtensible, requestContext);
  1028. Assert(!requestContext->IsHeapEnumInProgress());
  1029. if (nullptr == isExtensibleMethod)
  1030. {
  1031. return targetObj->IsExtensible();
  1032. }
  1033. Var isExtensibleResult = threadContext->ExecuteImplicitCall(isExtensibleMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1034. {
  1035. return CALL_FUNCTION(threadContext, isExtensibleMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1036. });
  1037. BOOL trapResult = JavascriptConversion::ToBoolean(isExtensibleResult, requestContext);
  1038. BOOL targetIsExtensible = targetObj->IsExtensible();
  1039. if (trapResult != targetIsExtensible)
  1040. {
  1041. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("isExtensible"));
  1042. }
  1043. return trapResult;
  1044. }
  1045. BOOL JavascriptProxy::PreventExtensions()
  1046. {
  1047. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1048. // Reject implicit call
  1049. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1050. if (threadContext->IsDisableImplicitCall())
  1051. {
  1052. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1053. return FALSE;
  1054. }
  1055. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  1056. ScriptContext* requestContext =
  1057. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  1058. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1059. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1060. //2. If handler is null, then throw a TypeError exception.
  1061. if (handlerObj == nullptr)
  1062. {
  1063. // the proxy has been revoked; TypeError.
  1064. if (!threadContext->RecordImplicitException())
  1065. return FALSE;
  1066. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("preventExtensions"));
  1067. }
  1068. //3. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1069. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1070. //4. Let trap be the result of GetMethod(handler, "preventExtensions").
  1071. //5. ReturnIfAbrupt(trap).
  1072. //6. If trap is undefined, then
  1073. //a.Return the result of calling the[[PreventExtensions]] internal method of target.
  1074. //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.
  1075. JavascriptFunction* preventExtensionsMethod = GetMethodHelper(PropertyIds::preventExtensions, requestContext);
  1076. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1077. if (nullptr == preventExtensionsMethod)
  1078. {
  1079. return targetObj->PreventExtensions();
  1080. }
  1081. //8. Let booleanTrapResult be ToBoolean(trapResult)
  1082. //9. ReturnIfAbrupt(booleanTrapResult).
  1083. //10. Let targetIsExtensible be the result of calling the[[IsExtensible]] internal method of target.
  1084. //11. ReturnIfAbrupt(targetIsExtensible).
  1085. //12. If booleanTrapResult is true and targetIsExtensible is true, then throw a TypeError exception.
  1086. //13. Return booleanTrapResult.
  1087. Var preventExtensionsResult = threadContext->ExecuteImplicitCall(preventExtensionsMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1088. {
  1089. return CALL_FUNCTION(threadContext, preventExtensionsMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1090. });
  1091. BOOL trapResult = JavascriptConversion::ToBoolean(preventExtensionsResult, requestContext);
  1092. if (trapResult)
  1093. {
  1094. BOOL targetIsExtensible = targetObj->IsExtensible();
  1095. if (targetIsExtensible)
  1096. {
  1097. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("preventExtensions"));
  1098. }
  1099. }
  1100. return trapResult;
  1101. }
  1102. // 7.3.12 in ES 2015. While this should have been no observable behavior change. Till there is obvious change warrant this
  1103. // to be moved to JavascriptOperators, let's keep it in proxy only first.
  1104. BOOL JavascriptProxy::TestIntegrityLevel(IntegrityLevel integrityLevel, RecyclableObject* obj, ScriptContext* scriptContext)
  1105. {
  1106. //1. Assert: Type(O) is Object.
  1107. //2. Assert: level is either "sealed" or "frozen".
  1108. //3. Let status be IsExtensible(O).
  1109. //4. ReturnIfAbrupt(status).
  1110. //5. If status is true, then return false
  1111. //6. NOTE If the object is extensible, none of its properties are examined.
  1112. BOOL isExtensible = obj->IsExtensible();
  1113. if (isExtensible)
  1114. {
  1115. return FALSE;
  1116. }
  1117. // at this time this is called from proxy only; when we extend this to other objects, we need to handle the other codepath.
  1118. //7. Let keys be O.[[OwnPropertyKeys]]().
  1119. //8. ReturnIfAbrupt(keys).
  1120. Assert(VarIs<JavascriptProxy>(obj));
  1121. JavascriptArray* resultArray = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
  1122. //9. Repeat for each element k of keys,
  1123. // a. Let currentDesc be O.[[GetOwnProperty]](k).
  1124. // b. ReturnIfAbrupt(currentDesc).
  1125. // c. If currentDesc is not undefined, then
  1126. // i. If currentDesc.[[Configurable]] is true, return false.
  1127. // ii. If level is "frozen" and IsDataDescriptor(currentDesc) is true, then
  1128. // 1. If currentDesc.[[Writable]] is true, return false.
  1129. Var itemVar;
  1130. bool writable = false;
  1131. bool configurable = false;
  1132. const PropertyRecord* propertyRecord;
  1133. PropertyDescriptor propertyDescriptor;
  1134. for (uint i = 0; i < resultArray->GetLength(); i++)
  1135. {
  1136. itemVar = resultArray->DirectGetItem(i);
  1137. 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)");
  1138. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1139. PropertyId propertyId = propertyRecord->GetPropertyId();
  1140. if (JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor))
  1141. {
  1142. configurable |= propertyDescriptor.IsConfigurable();
  1143. if (propertyDescriptor.IsDataDescriptor())
  1144. {
  1145. writable |= propertyDescriptor.IsWritable();
  1146. }
  1147. }
  1148. }
  1149. if (integrityLevel == IntegrityLevel::IntegrityLevel_frozen && writable)
  1150. {
  1151. return FALSE;
  1152. }
  1153. if (configurable)
  1154. {
  1155. return FALSE;
  1156. }
  1157. return TRUE;
  1158. }
  1159. BOOL JavascriptProxy::SetIntegrityLevel(IntegrityLevel integrityLevel, RecyclableObject* obj, ScriptContext* scriptContext)
  1160. {
  1161. //1. Assert: Type(O) is Object.
  1162. //2. Assert : level is either "sealed" or "frozen".
  1163. //3. Let status be O.[[PreventExtensions]]().
  1164. //4. ReturnIfAbrupt(status).
  1165. //5. If status is false, return false.
  1166. // at this time this is called from proxy only; when we extend this to other objects, we need to handle the other codepath.
  1167. Assert(VarIs<JavascriptProxy>(obj));
  1168. if (obj->PreventExtensions() == FALSE)
  1169. return FALSE;
  1170. //6. Let keys be O.[[OwnPropertyKeys]]().
  1171. //7. ReturnIfAbrupt(keys).
  1172. JavascriptArray* resultArray = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
  1173. const PropertyRecord* propertyRecord;
  1174. if (integrityLevel == IntegrityLevel::IntegrityLevel_sealed)
  1175. {
  1176. //8. If level is "sealed", then
  1177. //a. Repeat for each element k of keys,
  1178. //i. Let status be DefinePropertyOrThrow(O, k, PropertyDescriptor{ [[Configurable]]: false }).
  1179. //ii. ReturnIfAbrupt(status).
  1180. PropertyDescriptor propertyDescriptor;
  1181. propertyDescriptor.SetConfigurable(false);
  1182. Var itemVar;
  1183. for (uint i = 0; i < resultArray->GetLength(); i++)
  1184. {
  1185. itemVar = resultArray->DirectGetItem(i);
  1186. 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)");
  1187. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1188. PropertyId propertyId = propertyRecord->GetPropertyId();
  1189. JavascriptObject::DefineOwnPropertyHelper(obj, propertyId, propertyDescriptor, scriptContext);
  1190. }
  1191. }
  1192. else
  1193. {
  1194. //9.Else level is "frozen",
  1195. // a.Repeat for each element k of keys,
  1196. // i. Let currentDesc be O.[[GetOwnProperty]](k).
  1197. // ii. ReturnIfAbrupt(currentDesc).
  1198. // iii. If currentDesc is not undefined, then
  1199. // 1. If IsAccessorDescriptor(currentDesc) is true, then
  1200. // a. Let desc be the PropertyDescriptor{[[Configurable]]: false}.
  1201. // 2.Else,
  1202. // a. Let desc be the PropertyDescriptor { [[Configurable]]: false, [[Writable]]: false }.
  1203. // 3. Let status be DefinePropertyOrThrow(O, k, desc).
  1204. // 4. ReturnIfAbrupt(status).
  1205. Assert(integrityLevel == IntegrityLevel::IntegrityLevel_frozen);
  1206. PropertyDescriptor current, dataDescriptor, accessorDescriptor;
  1207. dataDescriptor.SetConfigurable(false);
  1208. dataDescriptor.SetWritable(false);
  1209. accessorDescriptor.SetConfigurable(false);
  1210. Var itemVar;
  1211. for (uint i = 0; i < resultArray->GetLength(); i++)
  1212. {
  1213. itemVar = resultArray->DirectGetItem(i);
  1214. 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)");
  1215. JavascriptConversion::ToPropertyKey(itemVar, scriptContext, &propertyRecord, nullptr);
  1216. PropertyId propertyId = propertyRecord->GetPropertyId();
  1217. PropertyDescriptor propertyDescriptor;
  1218. if (JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor))
  1219. {
  1220. if (propertyDescriptor.IsDataDescriptor())
  1221. {
  1222. JavascriptObject::DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), dataDescriptor, scriptContext);
  1223. }
  1224. else if (propertyDescriptor.IsAccessorDescriptor())
  1225. {
  1226. JavascriptObject::DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), accessorDescriptor, scriptContext);
  1227. }
  1228. }
  1229. }
  1230. }
  1231. // 10. Return true
  1232. return TRUE;
  1233. }
  1234. BOOL JavascriptProxy::Seal()
  1235. {
  1236. return SetIntegrityLevel(IntegrityLevel::IntegrityLevel_sealed, this, this->GetScriptContext());
  1237. }
  1238. BOOL JavascriptProxy::Freeze()
  1239. {
  1240. return SetIntegrityLevel(IntegrityLevel::IntegrityLevel_frozen, this, this->GetScriptContext());
  1241. }
  1242. BOOL JavascriptProxy::IsSealed()
  1243. {
  1244. return TestIntegrityLevel(IntegrityLevel::IntegrityLevel_sealed, this, this->GetScriptContext());
  1245. }
  1246. BOOL JavascriptProxy::IsFrozen()
  1247. {
  1248. return TestIntegrityLevel(IntegrityLevel::IntegrityLevel_frozen, this, this->GetScriptContext());
  1249. }
  1250. BOOL JavascriptProxy::SetWritable(PropertyId propertyId, BOOL value)
  1251. {
  1252. Assert(FALSE);
  1253. return FALSE;
  1254. }
  1255. BOOL JavascriptProxy::SetConfigurable(PropertyId propertyId, BOOL value)
  1256. {
  1257. Assert(FALSE);
  1258. return FALSE;
  1259. }
  1260. BOOL JavascriptProxy::SetEnumerable(PropertyId propertyId, BOOL value)
  1261. {
  1262. Assert(FALSE);
  1263. return FALSE;
  1264. }
  1265. BOOL JavascriptProxy::SetAttributes(PropertyId propertyId, PropertyAttributes attributes)
  1266. {
  1267. Assert(FALSE);
  1268. return FALSE;
  1269. }
  1270. BOOL JavascriptProxy::HasInstance(Var instance, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  1271. {
  1272. Var funcPrototype = JavascriptOperators::GetProperty(this, PropertyIds::prototype, scriptContext);
  1273. return JavascriptFunction::HasInstance(funcPrototype, instance, scriptContext, NULL, NULL);
  1274. }
  1275. JavascriptString* JavascriptProxy::GetClassName(ScriptContext * requestContext)
  1276. {
  1277. Assert(FALSE);
  1278. return nullptr;
  1279. }
  1280. RecyclableObject* JavascriptProxy::GetPrototypeSpecial()
  1281. {
  1282. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1283. // Reject implicit call
  1284. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1285. if (threadContext->IsDisableImplicitCall())
  1286. {
  1287. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1288. return nullptr;
  1289. }
  1290. // Caller does not pass requestContext. Retrieve from host scriptContext stack.
  1291. ScriptContext* requestContext =
  1292. threadContext->GetPreviousHostScriptContext()->GetScriptContext();
  1293. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1294. if (handlerObj == nullptr)
  1295. {
  1296. // the proxy has been revoked; TypeError.
  1297. if (!threadContext->RecordImplicitException())
  1298. return nullptr;
  1299. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("getPrototypeOf"));
  1300. }
  1301. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1302. JavascriptFunction* getPrototypeOfMethod = GetMethodHelper(PropertyIds::getPrototypeOf, requestContext);
  1303. if (nullptr == getPrototypeOfMethod || GetScriptContext()->IsHeapEnumInProgress())
  1304. {
  1305. return VarTo<RecyclableObject>(JavascriptObject::GetPrototypeOf(targetObj, requestContext));
  1306. }
  1307. Var getPrototypeOfResult = threadContext->ExecuteImplicitCall(getPrototypeOfMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1308. {
  1309. return CALL_FUNCTION(threadContext, getPrototypeOfMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  1310. });
  1311. TypeId prototypeTypeId = JavascriptOperators::GetTypeId(getPrototypeOfResult);
  1312. if (!JavascriptOperators::IsObjectType(prototypeTypeId) && prototypeTypeId != TypeIds_Null)
  1313. {
  1314. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getPrototypeOf"));
  1315. }
  1316. if (!targetObj->IsExtensible() && !JavascriptConversion::SameValue(getPrototypeOfResult, targetObj->GetPrototype()))
  1317. {
  1318. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("getPrototypeOf"));
  1319. }
  1320. return VarTo<RecyclableObject>(getPrototypeOfResult);
  1321. }
  1322. RecyclableObject* JavascriptProxy::GetConfigurablePrototype(ScriptContext * requestContext)
  1323. {
  1324. // We should be using GetPrototypeSpecial for proxy object; never should come over here.
  1325. Assert(FALSE);
  1326. return nullptr;
  1327. }
  1328. void JavascriptProxy::RemoveFromPrototype(ScriptContext * requestContext, bool * allProtoCachesInvalidated)
  1329. {
  1330. Assert(FALSE);
  1331. }
  1332. void JavascriptProxy::AddToPrototype(ScriptContext * requestContext, bool * allProtoCachesInvalidated)
  1333. {
  1334. Assert(FALSE);
  1335. }
  1336. void JavascriptProxy::SetPrototype(RecyclableObject* newPrototype)
  1337. {
  1338. Assert(FALSE);
  1339. }
  1340. BOOL JavascriptProxy::SetPrototypeTrap(RecyclableObject* newPrototype, bool shouldThrow,
  1341. ScriptContext * requestContext)
  1342. {
  1343. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1344. Assert(JavascriptOperators::IsObjectOrNull(newPrototype));
  1345. // Reject implicit call
  1346. ThreadContext* threadContext = requestContext->GetThreadContext();
  1347. if (threadContext->IsDisableImplicitCall())
  1348. {
  1349. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1350. return FALSE;
  1351. }
  1352. //1. Assert: Either Type(V) is Object or Type(V) is Null.
  1353. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1354. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1355. //3. If handler is null, then throw a TypeError exception.
  1356. if (handlerObj == nullptr)
  1357. {
  1358. // the proxy has been revoked; TypeError.
  1359. if (shouldThrow)
  1360. {
  1361. if (!threadContext->RecordImplicitException())
  1362. return FALSE;
  1363. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("setPrototypeOf"));
  1364. }
  1365. }
  1366. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1367. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1368. //5. Let trap be the result of GetMethod(handler, "setPrototypeOf").
  1369. //6. ReturnIfAbrupt(trap).
  1370. //7. If trap is undefined, then
  1371. //a.Return the result of calling the[[SetPrototypeOf]] internal method of target with argument V.
  1372. JavascriptFunction* setPrototypeOfMethod = GetMethodHelper(PropertyIds::setPrototypeOf, requestContext);
  1373. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1374. if (nullptr == setPrototypeOfMethod)
  1375. {
  1376. JavascriptObject::ChangePrototype(targetObj, newPrototype, shouldThrow, requestContext);
  1377. return TRUE;
  1378. }
  1379. //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.
  1380. Var setPrototypeResult = threadContext->ExecuteImplicitCall(setPrototypeOfMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1381. {
  1382. return CALL_FUNCTION(threadContext, setPrototypeOfMethod, CallInfo(CallFlags_Value, 3), handlerObj, targetObj, newPrototype);
  1383. });
  1384. //9. Let booleanTrapResult be ToBoolean(trapResult).
  1385. //10. ReturnIfAbrupt(booleanTrapResult).
  1386. //11. Let extensibleTarget be the result of IsExtensible(target).
  1387. //12. ReturnIfAbrupt(extensibleTarget).
  1388. //13. If extensibleTarget is true, then return booleanTrapResult.
  1389. //14. Let targetProto be the result of calling the[[GetPrototypeOf]] internal method of target.
  1390. //15. ReturnIfAbrupt(targetProto).
  1391. //16. If booleanTrapResult is true and SameValue(V, targetProto) is false, then throw a TypeError exception.
  1392. //17. Return booleanTrapResult.
  1393. BOOL prototypeSetted = JavascriptConversion::ToBoolean(setPrototypeResult, requestContext);
  1394. BOOL isExtensible = targetObj->IsExtensible();
  1395. if (isExtensible)
  1396. {
  1397. if (!prototypeSetted && shouldThrow)
  1398. {
  1399. JavascriptError::ThrowTypeError(requestContext, JSERR_ProxyTrapReturnedFalse, _u("setPrototypeOf"));
  1400. }
  1401. return prototypeSetted;
  1402. }
  1403. Var targetProto = targetObj->GetPrototype();
  1404. if (!JavascriptConversion::SameValue(targetProto, newPrototype))
  1405. {
  1406. if (shouldThrow)
  1407. {
  1408. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("setPrototypeOf"));
  1409. }
  1410. return FALSE;
  1411. }
  1412. return TRUE;
  1413. }
  1414. Var JavascriptProxy::ToString(ScriptContext* scriptContext)
  1415. {
  1416. //RecyclableObject* targetObj;
  1417. if (IsRevoked())
  1418. {
  1419. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1420. // the proxy has been revoked; TypeError.
  1421. if (!threadContext->RecordImplicitException())
  1422. return nullptr;
  1423. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("toString"));
  1424. }
  1425. return JavascriptObject::ToStringHelper(target, scriptContext);
  1426. }
  1427. // before recursively calling something on 'target' use this helper in case there is nesting of proxies.
  1428. // the proxies could be deep nested and cause SO when processed recursively.
  1429. const JavascriptProxy* JavascriptProxy::UnwrapNestedProxies(const JavascriptProxy* proxy)
  1430. {
  1431. // continue while we have a proxy that is not revoked
  1432. while (!proxy->IsRevoked())
  1433. {
  1434. JavascriptProxy* nestedProxy = JavascriptOperators::TryFromVar<JavascriptProxy>(proxy->target);
  1435. if (nestedProxy == nullptr)
  1436. {
  1437. break;
  1438. }
  1439. proxy = nestedProxy;
  1440. }
  1441. return proxy;
  1442. }
  1443. BOOL JavascriptProxy::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  1444. {
  1445. const JavascriptProxy* proxy = UnwrapNestedProxies(this);
  1446. //RecyclableObject* targetObj;
  1447. if (proxy->IsRevoked())
  1448. {
  1449. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1450. // the proxy has been revoked; TypeError.
  1451. if (!threadContext->RecordImplicitException())
  1452. return FALSE;
  1453. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("getTypeString"));
  1454. }
  1455. return proxy->target->GetDiagTypeString(stringBuilder, requestContext);
  1456. }
  1457. RecyclableObject* JavascriptProxy::ToObject(ScriptContext * requestContext)
  1458. {
  1459. //RecyclableObject* targetObj;
  1460. if (IsRevoked())
  1461. {
  1462. ThreadContext* threadContext = GetScriptContext()->GetThreadContext();
  1463. // the proxy has been revoked; TypeError.
  1464. if (!threadContext->RecordImplicitException())
  1465. return nullptr;
  1466. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("toObject"));
  1467. }
  1468. return __super::ToObject(requestContext);
  1469. }
  1470. Var JavascriptProxy::GetTypeOfString(ScriptContext* requestContext)
  1471. {
  1472. const JavascriptProxy* proxy = UnwrapNestedProxies(this);
  1473. if (proxy->handler == nullptr)
  1474. {
  1475. // even if handler is nullptr, return typeof as "object"
  1476. return requestContext->GetLibrary()->GetObjectTypeDisplayString();
  1477. }
  1478. // if exotic object has [[Call]] we should return "function", otherwise return "object"
  1479. if (VarIs<JavascriptFunction>(this->target))
  1480. {
  1481. return requestContext->GetLibrary()->GetFunctionTypeDisplayString();
  1482. }
  1483. else
  1484. {
  1485. // handle nested cases recursively
  1486. return proxy->target->GetTypeOfString(requestContext);
  1487. }
  1488. }
  1489. BOOL JavascriptProxy::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* requestContext, PropertyDescriptor* propertyDescriptor)
  1490. {
  1491. JavascriptProxy* proxy = VarTo<JavascriptProxy>(obj);
  1492. return proxy->GetPropertyDescriptorTrap(propertyId, propertyDescriptor, requestContext);
  1493. }
  1494. BOOL JavascriptProxy::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* requestContext, PropertyOperationFlags flags)
  1495. {
  1496. // #sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  1497. PROBE_STACK(requestContext, Js::Constants::MinStackDefault);
  1498. // Reject implicit call
  1499. ThreadContext* threadContext = requestContext->GetThreadContext();
  1500. if (threadContext->IsDisableImplicitCall())
  1501. {
  1502. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1503. return FALSE;
  1504. }
  1505. JavascriptProxy* proxy = VarTo<JavascriptProxy>(obj);
  1506. //1. Assert: IsPropertyKey(P) is true.
  1507. //2. Let handler be O.[[ProxyHandler]].
  1508. RecyclableObject *handlerObj = proxy->MarshalHandler(requestContext);
  1509. //3. If handler is null, then throw a TypeError exception.
  1510. //4. Assert: Type(handler) is Object.
  1511. if (handlerObj == nullptr)
  1512. {
  1513. // the proxy has been revoked; TypeError.
  1514. if (!threadContext->RecordImplicitException())
  1515. return FALSE;
  1516. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("definePropertyDescriptor"));
  1517. }
  1518. //5. Let target be O.[[ProxyTarget]].
  1519. RecyclableObject *targetObj = proxy->MarshalTarget(requestContext);
  1520. //6. Let trap be ? GetMethod(handler, "defineProperty").
  1521. //7. If trap is undefined, then
  1522. //a. Return ? target.[[DefineOwnProperty]](P, Desc).
  1523. JavascriptFunction* defineOwnPropertyMethod = proxy->GetMethodHelper(PropertyIds::defineProperty, requestContext);
  1524. Assert(!requestContext->IsHeapEnumInProgress());
  1525. if (nullptr == defineOwnPropertyMethod)
  1526. {
  1527. return JavascriptOperators::DefineOwnPropertyDescriptor(targetObj, propId, descriptor, throwOnError, requestContext, flags);
  1528. }
  1529. //8. Let descObj be FromPropertyDescriptor(Desc).
  1530. //9. Let booleanTrapResult be ToBoolean(? Call(trap, handler, << target, P, descObj >> )).
  1531. //10. If booleanTrapResult is false, then return false.
  1532. //11. Let targetDesc be ? target.[[GetOwnProperty]](P).
  1533. Var descVar = JavascriptOperators::FromPropertyDescriptor(descriptor, requestContext);
  1534. Var propertyName = GetName(requestContext, propId);
  1535. Var definePropertyResult = threadContext->ExecuteImplicitCall(defineOwnPropertyMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1536. {
  1537. return CALL_FUNCTION(threadContext, defineOwnPropertyMethod, CallInfo(CallFlags_Value, 4), handlerObj, targetObj, propertyName, descVar);
  1538. });
  1539. BOOL defineResult = JavascriptConversion::ToBoolean(definePropertyResult, requestContext);
  1540. if (!defineResult)
  1541. {
  1542. if (throwOnError && flags & PropertyOperation_StrictMode)
  1543. {
  1544. JavascriptError::ThrowTypeErrorVar(
  1545. requestContext,
  1546. JSERR_ProxyHandlerReturnedFalse,
  1547. _u("defineProperty"),
  1548. requestContext->GetPropertyName(propId)->GetBuffer()
  1549. );
  1550. }
  1551. return defineResult;
  1552. }
  1553. //12. Let extensibleTarget be ? IsExtensible(target).
  1554. //13. If Desc has a[[Configurable]] field and if Desc.[[Configurable]] is false, then
  1555. // a.Let settingConfigFalse be true.
  1556. //14. Else let settingConfigFalse be false.
  1557. //15. If targetDesc is undefined, then
  1558. // a.If extensibleTarget is false, then throw a TypeError exception.
  1559. // b.If settingConfigFalse is true, then throw a TypeError exception.
  1560. //16. Else targetDesc is not undefined,
  1561. // a.If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, targetDesc) is false, then throw a TypeError exception.
  1562. // b.If settingConfigFalse is true and targetDesc.[[Configurable]] is true, then throw a TypeError exception.
  1563. //17. Return true.
  1564. PropertyDescriptor targetDescriptor;
  1565. BOOL hasProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propId, requestContext, &targetDescriptor);
  1566. BOOL isExtensible = targetObj->IsExtensible();
  1567. BOOL settingConfigFalse = (descriptor.ConfigurableSpecified() && !descriptor.IsConfigurable());
  1568. if (!hasProperty)
  1569. {
  1570. if (!isExtensible || settingConfigFalse)
  1571. {
  1572. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1573. }
  1574. }
  1575. else
  1576. {
  1577. if (!JavascriptOperators::IsCompatiblePropertyDescriptor(descriptor, hasProperty? &targetDescriptor : nullptr, !!isExtensible, true, requestContext))
  1578. {
  1579. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1580. }
  1581. if (settingConfigFalse && targetDescriptor.IsConfigurable())
  1582. {
  1583. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("defineProperty"));
  1584. }
  1585. }
  1586. return TRUE;
  1587. }
  1588. BOOL JavascriptProxy::SetPropertyTrap(Var receiver, SetPropertyTrapKind setPropertyTrapKind, Js::JavascriptString * propertyNameString, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  1589. {
  1590. const PropertyRecord* propertyRecord;
  1591. requestContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  1592. return SetPropertyTrap(receiver, setPropertyTrapKind, propertyRecord->GetPropertyId(), newValue, requestContext, propertyOperationFlags);
  1593. }
  1594. BOOL JavascriptProxy::SetPropertyTrap(Var receiver, SetPropertyTrapKind setPropertyTrapKind, PropertyId propertyId, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags, BOOL skipPrototypeCheck)
  1595. {
  1596. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1597. // Reject implicit call
  1598. ThreadContext* threadContext = requestContext->GetThreadContext();
  1599. if (threadContext->IsDisableImplicitCall())
  1600. {
  1601. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1602. return FALSE;
  1603. }
  1604. //1. Assert: IsPropertyKey(P) is true.
  1605. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1606. Js::RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  1607. //3. If handler is null, then throw a TypeError exception.
  1608. if (handlerObj == nullptr)
  1609. {
  1610. // the proxy has been revoked; TypeError.
  1611. if (!threadContext->RecordImplicitException())
  1612. return FALSE;
  1613. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, _u("set"));
  1614. }
  1615. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  1616. Js::RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  1617. //5. Let trap be the result of GetMethod(handler, "set").
  1618. //6. ReturnIfAbrupt(trap).
  1619. //7. If trap is undefined, then
  1620. //a.Return the result of calling the[[Set]] internal method of target with arguments P, V, and Receiver.
  1621. JavascriptFunction* setMethod = GetMethodHelper(PropertyIds::set, requestContext);
  1622. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  1623. if (nullptr == setMethod)
  1624. {
  1625. PropertyValueInfo info;
  1626. switch (setPropertyTrapKind)
  1627. {
  1628. case SetPropertyTrapKind::SetItemOnTaggedNumberKind:
  1629. {
  1630. uint32 indexVal;
  1631. BOOL isNumericPropertyId = requestContext->IsNumericPropertyId(propertyId, &indexVal);
  1632. Assert(isNumericPropertyId);
  1633. return JavascriptOperators::SetItemOnTaggedNumber(receiver, targetObj, indexVal, newValue, requestContext, propertyOperationFlags);
  1634. }
  1635. case SetPropertyTrapKind::SetPropertyOnTaggedNumberKind:
  1636. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, targetObj, propertyId, newValue, requestContext, propertyOperationFlags);
  1637. case SetPropertyTrapKind::SetPropertyKind:
  1638. return JavascriptOperators::SetProperty(receiver, targetObj, propertyId, newValue, requestContext, propertyOperationFlags);
  1639. case SetPropertyTrapKind::SetItemKind:
  1640. {
  1641. uint32 indexVal;
  1642. BOOL isNumericPropertyId = requestContext->IsNumericPropertyId(propertyId, &indexVal);
  1643. Assert(isNumericPropertyId);
  1644. return JavascriptOperators::SetItem(receiver, targetObj, indexVal, newValue, requestContext, propertyOperationFlags, skipPrototypeCheck);
  1645. }
  1646. case SetPropertyTrapKind::SetPropertyWPCacheKind:
  1647. {
  1648. PropertyValueInfo propertyValueInfo;
  1649. return JavascriptOperators::SetPropertyWPCache(receiver, targetObj, propertyId, newValue, requestContext, propertyOperationFlags, &propertyValueInfo);
  1650. }
  1651. default:
  1652. AnalysisAssert(FALSE);
  1653. }
  1654. }
  1655. //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.
  1656. //9. Let booleanTrapResult be ToBoolean(trapResult).
  1657. //10. ReturnIfAbrupt(booleanTrapResult).
  1658. //11. If booleanTrapResult is false, then return false.
  1659. Var propertyName = GetName(requestContext, propertyId);
  1660. Var setPropertyResult = threadContext->ExecuteImplicitCall(setMethod, ImplicitCall_Accessor, [=]()->Js::Var
  1661. {
  1662. return CALL_FUNCTION(threadContext, setMethod, CallInfo(CallFlags_Value, 5), handlerObj, targetObj, propertyName, newValue, receiver);
  1663. });
  1664. BOOL setResult = JavascriptConversion::ToBoolean(setPropertyResult, requestContext);
  1665. if (!setResult)
  1666. {
  1667. if (propertyOperationFlags & PropertyOperation_StrictMode)
  1668. {
  1669. JavascriptError::ThrowTypeErrorVar(
  1670. requestContext,
  1671. JSERR_ProxyHandlerReturnedFalse,
  1672. _u("set"),
  1673. requestContext->GetPropertyName(propertyId)->GetBuffer()
  1674. );
  1675. }
  1676. return setResult;
  1677. }
  1678. //12. Let targetDesc be the result of calling the[[GetOwnProperty]] internal method of target with argument P.
  1679. //13. ReturnIfAbrupt(targetDesc).
  1680. //14. If targetDesc is not undefined, then
  1681. //a.If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Writable]] is false, then
  1682. //i.If SameValue(V, targetDesc.[[Value]]) is false, then throw a TypeError exception.
  1683. //b.If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] is false, then
  1684. //i.If targetDesc.[[Set]] is undefined, then throw a TypeError exception.
  1685. //15. Return true
  1686. PropertyDescriptor targetDescriptor;
  1687. BOOL hasProperty;
  1688. hasProperty = JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetDescriptor);
  1689. if (hasProperty)
  1690. {
  1691. if (targetDescriptor.ValueSpecified())
  1692. {
  1693. if (!targetDescriptor.IsConfigurable() && !targetDescriptor.IsWritable() &&
  1694. !JavascriptConversion::SameValue(newValue, targetDescriptor.GetValue()))
  1695. {
  1696. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("set"));
  1697. }
  1698. }
  1699. else
  1700. {
  1701. if (!targetDescriptor.IsConfigurable() && targetDescriptor.GetSetter() == requestContext->GetLibrary()->GetDefaultAccessorFunction())
  1702. {
  1703. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("set"));
  1704. }
  1705. }
  1706. }
  1707. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  1708. return TRUE;
  1709. }
  1710. JavascriptFunction* JavascriptProxy::GetMethodHelper(PropertyId methodId, ScriptContext* requestContext)
  1711. {
  1712. //2. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  1713. //3. If handler is null, then throw a TypeError exception.
  1714. if (IsRevoked())
  1715. {
  1716. // the proxy has been revoked; TypeError.
  1717. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnRevokedProxy, requestContext->GetPropertyName(methodId)->GetBuffer());
  1718. }
  1719. Var varMethod;
  1720. //5. Let trap be the result of GetMethod(handler, "getOwnPropertyDescriptor").
  1721. //6. ReturnIfAbrupt(trap).
  1722. //7.3.9 GetMethod(V, P)
  1723. // The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
  1724. // 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
  1725. // property key. This abstract operation performs the following steps:
  1726. // 1. Assert: IsPropertyKey(P) is true.
  1727. // 2. Let func be ? GetV(V, P).
  1728. // 3. If func is either undefined or null, return undefined.
  1729. // 4. If IsCallable(func) is false, throw a TypeError exception.
  1730. // 5. Return func.
  1731. BOOL result = JavascriptOperators::GetPropertyReference(handler, methodId, &varMethod, requestContext);
  1732. if (!result || JavascriptOperators::IsUndefinedOrNull(varMethod))
  1733. {
  1734. return nullptr;
  1735. }
  1736. if (!VarIs<JavascriptFunction>(varMethod))
  1737. {
  1738. JavascriptError::ThrowTypeError(requestContext, JSERR_NeedFunction, requestContext->GetPropertyName(methodId)->GetBuffer());
  1739. }
  1740. JavascriptFunction* function = VarTo<JavascriptFunction>(varMethod);
  1741. return VarTo<JavascriptFunction>(CrossSite::MarshalVar(requestContext,
  1742. function, function->GetScriptContext()));
  1743. }
  1744. Var JavascriptProxy::GetValueFromDescriptor(Var instance, PropertyDescriptor propertyDescriptor, ScriptContext* requestContext)
  1745. {
  1746. if (propertyDescriptor.ValueSpecified())
  1747. {
  1748. return CrossSite::MarshalVar(requestContext, propertyDescriptor.GetValue());
  1749. }
  1750. if (propertyDescriptor.GetterSpecified())
  1751. {
  1752. return JavascriptOperators::CallGetter(VarTo<RecyclableObject>(propertyDescriptor.GetGetter()), instance, requestContext);
  1753. }
  1754. Assert(FALSE);
  1755. return requestContext->GetLibrary()->GetUndefined();
  1756. }
  1757. void JavascriptProxy::PropertyIdFromInt(uint32 index, PropertyRecord const** propertyRecord)
  1758. {
  1759. char16 buffer[22];
  1760. int pos = TaggedInt::ToBuffer(index, buffer, _countof(buffer));
  1761. GetScriptContext()->GetOrAddPropertyRecord((LPCWSTR)buffer + pos, (_countof(buffer) - 1) - pos, propertyRecord);
  1762. }
  1763. Var JavascriptProxy::GetName(ScriptContext* requestContext, PropertyId propertyId)
  1764. {
  1765. const PropertyRecord* propertyRecord = requestContext->GetThreadContext()->GetPropertyName(propertyId);
  1766. Var name;
  1767. if (propertyRecord->IsSymbol())
  1768. {
  1769. name = requestContext->GetSymbol(propertyRecord);
  1770. }
  1771. else
  1772. {
  1773. name = requestContext->GetPropertyString(propertyRecord);
  1774. }
  1775. return name;
  1776. }
  1777. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1778. PropertyId JavascriptProxy::EnsureHandlerPropertyId(ScriptContext* scriptContext)
  1779. {
  1780. ThreadContext* threadContext = scriptContext->GetThreadContext();
  1781. if (threadContext->handlerPropertyId == Js::Constants::NoProperty)
  1782. {
  1783. LPCWSTR autoProxyName;
  1784. if (threadContext->GetAutoProxyName() != nullptr)
  1785. {
  1786. autoProxyName = threadContext->GetAutoProxyName();
  1787. }
  1788. else
  1789. {
  1790. autoProxyName = Js::Configuration::Global.flags.autoProxy;
  1791. }
  1792. threadContext->handlerPropertyId = threadContext->GetOrAddPropertyRecordBind(
  1793. JsUtil::CharacterBuffer<WCHAR>(autoProxyName, static_cast<charcount_t>(wcslen(autoProxyName))))->GetPropertyId();
  1794. }
  1795. return threadContext->handlerPropertyId;
  1796. }
  1797. RecyclableObject* JavascriptProxy::AutoProxyWrapper(Var obj)
  1798. {
  1799. RecyclableObject* object = VarTo<RecyclableObject>(obj);
  1800. if (!JavascriptOperators::IsObject(object) || VarIs<JavascriptProxy>(object))
  1801. {
  1802. return object;
  1803. }
  1804. ScriptContext* scriptContext = object->GetScriptContext();
  1805. if (!scriptContext->GetThreadContext()->IsScriptActive())
  1806. {
  1807. return object;
  1808. }
  1809. if (!scriptContext->GetConfig()->IsES6ProxyEnabled())
  1810. {
  1811. return object;
  1812. }
  1813. Assert(Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag));
  1814. PropertyId handlerId = EnsureHandlerPropertyId(scriptContext);
  1815. GlobalObject* globalObject = scriptContext->GetLibrary()->GetGlobalObject();
  1816. Var handler = nullptr;
  1817. if (!JavascriptOperators::GetProperty(globalObject, handlerId, &handler, scriptContext))
  1818. {
  1819. handler = scriptContext->GetLibrary()->CreateObject();
  1820. JavascriptOperators::SetProperty(globalObject, globalObject, handlerId, handler, scriptContext);
  1821. }
  1822. CallInfo callInfo(CallFlags_Value, 3);
  1823. Var varArgs[3];
  1824. Js::Arguments arguments(callInfo, varArgs);
  1825. varArgs[0] = scriptContext->GetLibrary()->GetProxyConstructor();
  1826. varArgs[1] = object;
  1827. varArgs[2] = handler;
  1828. return Create(scriptContext, arguments);
  1829. }
  1830. #endif
  1831. Var JavascriptProxy::ConstructorTrap(Arguments args, ScriptContext* scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  1832. {
  1833. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  1834. Var functionResult;
  1835. if (spreadIndices != nullptr)
  1836. {
  1837. functionResult = JavascriptFunction::CallSpreadFunction(this, args, spreadIndices);
  1838. }
  1839. else
  1840. {
  1841. functionResult = JavascriptFunction::CallFunction<true>(this, this->GetEntryPoint(), args);
  1842. }
  1843. return functionResult;
  1844. }
  1845. Var JavascriptProxy::FunctionCallTrap(RecyclableObject* function, CallInfo callInfo, ...)
  1846. {
  1847. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1848. ARGUMENTS(args, callInfo);
  1849. ScriptContext* scriptContext = function->GetScriptContext();
  1850. BOOL hasOverridingNewTarget = args.HasNewTarget();
  1851. bool isCtorSuperCall = JavascriptOperators::GetAndAssertIsConstructorSuperCall(args);
  1852. bool isNewCall = args.IsNewCall() || hasOverridingNewTarget;
  1853. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1854. if (!VarIs<JavascriptProxy>(function))
  1855. {
  1856. if (args.Info.Flags & CallFlags_New)
  1857. {
  1858. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("construct"));
  1859. }
  1860. else
  1861. {
  1862. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("call"));
  1863. }
  1864. }
  1865. Var newTarget = nullptr;
  1866. JavascriptProxy* proxy = VarTo<JavascriptProxy>(function);
  1867. Js::RecyclableObject *handlerObj = proxy->handler;
  1868. Js::RecyclableObject *targetObj = proxy->target;
  1869. JavascriptFunction* callMethod;
  1870. Assert(!scriptContext->IsHeapEnumInProgress());
  1871. // To conform with ES6 spec 7.3.13
  1872. if (hasOverridingNewTarget)
  1873. {
  1874. newTarget = args.Values[callInfo.Count];
  1875. }
  1876. else
  1877. {
  1878. newTarget = proxy;
  1879. }
  1880. if (args.Info.Flags & CallFlags_New)
  1881. {
  1882. callMethod = proxy->GetMethodHelper(PropertyIds::construct, scriptContext);
  1883. }
  1884. else
  1885. {
  1886. callMethod = proxy->GetMethodHelper(PropertyIds::apply, scriptContext);
  1887. }
  1888. if (!JavascriptConversion::IsCallable(targetObj))
  1889. {
  1890. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction, _u("call"));
  1891. }
  1892. if (nullptr == callMethod)
  1893. {
  1894. // newCount is ushort. If args count is greater than or equal to 65535, an integer
  1895. // too many arguments
  1896. if (args.Info.Count >= USHORT_MAX) //check against CallInfo::kMaxCountArgs if newCount is ever made int
  1897. {
  1898. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  1899. }
  1900. // in [[construct]] case, we don't need to check if the function is a constructor: the function should throw there.
  1901. Var newThisObject = nullptr;
  1902. if (args.Info.Flags & CallFlags_New)
  1903. {
  1904. if (!JavascriptOperators::IsConstructor(targetObj))
  1905. {
  1906. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, _u("construct"));
  1907. }
  1908. // args.Values[0] will be null in the case where NewTarget is initially provided by proxy.
  1909. if (!isCtorSuperCall || !args.Values[0])
  1910. {
  1911. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  1912. {
  1913. newThisObject = JavascriptOperators::NewScObjectNoCtorCommon(proxy, scriptContext, false);
  1914. }
  1915. END_SAFE_REENTRANT_CALL
  1916. args.Values[0] = newThisObject;
  1917. }
  1918. else
  1919. {
  1920. newThisObject = args.Values[0];
  1921. }
  1922. }
  1923. ushort newCount = (ushort)args.Info.Count;
  1924. if (isNewCall)
  1925. {
  1926. newCount++;
  1927. if (!newCount)
  1928. {
  1929. ::Math::DefaultOverflowPolicy();
  1930. }
  1931. }
  1932. AnalysisAssert(newCount >= (ushort)args.Info.Count);
  1933. Var* newValues;
  1934. const unsigned STACK_ARGS_ALLOCA_THRESHOLD = 8; // Number of stack args we allow before using _alloca
  1935. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  1936. if (newCount > STACK_ARGS_ALLOCA_THRESHOLD)
  1937. {
  1938. PROBE_STACK(scriptContext, newCount * sizeof(Var) + Js::Constants::MinStackDefault); // args + function call
  1939. newValues = (Var*)_alloca(newCount * sizeof(Var));
  1940. }
  1941. else
  1942. {
  1943. newValues = stackArgs;
  1944. }
  1945. CallInfo calleeInfo((CallFlags)(args.Info.Flags), args.Info.Count);
  1946. if (isNewCall)
  1947. {
  1948. calleeInfo.Flags = (CallFlags)(calleeInfo.Flags | CallFlags_ExtraArg | CallFlags_NewTarget);
  1949. }
  1950. for (ushort argCount = 0; argCount < (ushort)args.Info.Count; argCount++)
  1951. {
  1952. AnalysisAssert(newCount >= ((ushort)args.Info.Count));
  1953. AnalysisAssert(argCount < newCount);
  1954. AnalysisAssert(argCount < (ushort)args.Info.Count);
  1955. AnalysisAssert(sizeof(Var*) == sizeof(void*));
  1956. AnalysisAssert(sizeof(Var*) * argCount < sizeof(void*) * newCount);
  1957. #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.")
  1958. newValues[argCount] = args.Values[argCount];
  1959. }
  1960. if (isNewCall)
  1961. {
  1962. AnalysisAssert(newCount == ((ushort)args.Info.Count) + 1);
  1963. newValues[args.Info.Count] = newTarget;
  1964. // If the function we're calling is a class constructor, it expects newTarget
  1965. // as the first arg so it knows how to construct "this". (We can leave the
  1966. // extra copy of newTarget at the end of the arguments; it's harmless so
  1967. // there's no need to introduce additional complexity here.)
  1968. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(targetObj, scriptContext);
  1969. if (functionInfo && functionInfo->IsClassConstructor())
  1970. {
  1971. newValues[0] = newTarget;
  1972. }
  1973. }
  1974. Js::Arguments arguments(calleeInfo, newValues);
  1975. Var aReturnValue = nullptr;
  1976. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  1977. {
  1978. aReturnValue = JavascriptFunction::CallFunction<true>(targetObj, targetObj->GetEntryPoint(), arguments);
  1979. }
  1980. END_SAFE_REENTRANT_CALL
  1981. // If this is constructor call, return the actual object instead of function result
  1982. if ((callInfo.Flags & CallFlags_New) && !JavascriptOperators::IsObject(aReturnValue))
  1983. {
  1984. aReturnValue = newThisObject;
  1985. }
  1986. return aReturnValue;
  1987. }
  1988. JavascriptArray* argList = scriptContext->GetLibrary()->CreateArray(callInfo.Count - 1);
  1989. for (uint i = 1; i < callInfo.Count; i++)
  1990. {
  1991. argList->DirectSetItemAt(i - 1, args[i]);
  1992. }
  1993. Var varArgs[4];
  1994. CallInfo calleeInfo(CallFlags_Value, 4);
  1995. Js::Arguments arguments(calleeInfo, varArgs);
  1996. varArgs[0] = handlerObj;
  1997. varArgs[1] = targetObj;
  1998. if (args.Info.Flags & CallFlags_New)
  1999. {
  2000. if (!JavascriptOperators::IsConstructor(targetObj))
  2001. {
  2002. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  2003. }
  2004. varArgs[2] = argList;
  2005. // 1st preference - overridden newTarget
  2006. // 2nd preference - 'this' in case of super() call
  2007. // 3rd preference - newTarget ( which is same as F)
  2008. varArgs[3] = hasOverridingNewTarget ? newTarget :
  2009. isCtorSuperCall ? args[0] : newTarget;
  2010. }
  2011. else
  2012. {
  2013. varArgs[2] = args[0];
  2014. varArgs[3] = argList;
  2015. }
  2016. Var trapResult = nullptr;
  2017. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  2018. {
  2019. trapResult = callMethod->CallFunction(arguments);
  2020. }
  2021. END_SAFE_REENTRANT_CALL
  2022. if (args.Info.Flags & CallFlags_New)
  2023. {
  2024. if (!Js::JavascriptOperators::IsObject(trapResult))
  2025. {
  2026. JavascriptError::ThrowTypeError(scriptContext, JSERR_InconsistentTrapResult, _u("construct"));
  2027. }
  2028. }
  2029. return trapResult;
  2030. }
  2031. JavascriptArray* JavascriptProxy::PropertyKeysTrap(KeysTrapKind keysTrapKind, ScriptContext* requestContext)
  2032. {
  2033. PROBE_STACK(GetScriptContext(), Js::Constants::MinStackDefault);
  2034. // Reject implicit call
  2035. ThreadContext* threadContext = requestContext->GetThreadContext();
  2036. if (threadContext->IsDisableImplicitCall())
  2037. {
  2038. threadContext->AddImplicitCallFlags(Js::ImplicitCall_External);
  2039. return nullptr;
  2040. }
  2041. //1. Let handler be the value of the[[ProxyHandler]] internal slot of O.
  2042. RecyclableObject *handlerObj = this->MarshalHandler(requestContext);
  2043. //2. If handler is null, throw a TypeError exception.
  2044. //3. Assert: Type(handler) is Object.
  2045. if (handlerObj == nullptr)
  2046. {
  2047. // the proxy has been revoked; TypeError.
  2048. if (!threadContext->RecordImplicitException())
  2049. return nullptr;
  2050. JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_ErrorOnRevokedProxy, _u("ownKeys"));
  2051. }
  2052. AssertMsg(JavascriptOperators::IsObject(handlerObj), "Handler should be object.");
  2053. //4. Let target be the value of the[[ProxyTarget]] internal slot of O.
  2054. RecyclableObject *targetObj = this->MarshalTarget(requestContext);
  2055. //5. Let trap be GetMethod(handler, "ownKeys").
  2056. //6. ReturnIfAbrupt(trap).
  2057. //7. If trap is undefined, then
  2058. // a. Return target.[[OwnPropertyKeys]]().
  2059. JavascriptFunction* ownKeysMethod = GetMethodHelper(PropertyIds::ownKeys, requestContext);
  2060. Assert(!GetScriptContext()->IsHeapEnumInProgress());
  2061. JavascriptArray *targetKeys;
  2062. if (nullptr == ownKeysMethod)
  2063. {
  2064. switch (keysTrapKind)
  2065. {
  2066. case GetOwnPropertyNamesKind:
  2067. targetKeys = JavascriptOperators::GetOwnPropertyNames(targetObj, requestContext);
  2068. break;
  2069. case GetOwnPropertySymbolKind:
  2070. targetKeys = JavascriptOperators::GetOwnPropertySymbols(targetObj, requestContext);
  2071. break;
  2072. case KeysKind:
  2073. targetKeys = JavascriptOperators::GetOwnPropertyKeys(targetObj, requestContext);
  2074. break;
  2075. default:
  2076. AssertMsg(false, "Invalid KeysTrapKind.");
  2077. return requestContext->GetLibrary()->CreateArray(0);
  2078. }
  2079. return targetKeys;
  2080. }
  2081. //8. Let trapResultArray be Call(trap, handler, <<target>>).
  2082. //9. Let trapResult be CreateListFromArrayLike(trapResultArray, <<String, Symbol>>).
  2083. //10. ReturnIfAbrupt(trapResult).
  2084. //11. Let extensibleTarget be IsExtensible(target).
  2085. //12. ReturnIfAbrupt(extensibleTarget).
  2086. //13. Let targetKeys be target.[[OwnPropertyKeys]]().
  2087. //14. ReturnIfAbrupt(targetKeys).
  2088. Var ownKeysResult = threadContext->ExecuteImplicitCall(ownKeysMethod, ImplicitCall_Accessor, [=]()->Js::Var
  2089. {
  2090. return CALL_FUNCTION(threadContext, ownKeysMethod, CallInfo(CallFlags_Value, 2), handlerObj, targetObj);
  2091. });
  2092. if (!JavascriptOperators::IsObject(ownKeysResult))
  2093. {
  2094. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2095. }
  2096. RecyclableObject* trapResultArray = VarTo<RecyclableObject>(ownKeysResult);
  2097. //15. Assert: targetKeys is a List containing only String and Symbol values.
  2098. //16. Let targetConfigurableKeys be an empty List.
  2099. //17. Let targetNonconfigurableKeys be an empty List.
  2100. //18. Repeat, for each element key of targetKeys,
  2101. // a.Let desc be target.[[GetOwnProperty]](key).
  2102. // b.ReturnIfAbrupt(desc).
  2103. // c.If desc is not undefined and desc.[[Configurable]] is false, then
  2104. // i.Append key as an element of targetNonconfigurableKeys.
  2105. // d.Else,
  2106. // i.Append key as an element of targetConfigurableKeys.
  2107. //19. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
  2108. // a. Return trapResult.
  2109. //20. Let uncheckedResultKeys be a new List which is a copy of trapResult.
  2110. //21. Repeat, for each key that is an element of targetNonconfigurableKeys,
  2111. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
  2112. // b. Remove key from uncheckedResultKeys
  2113. //22. If extensibleTarget is true, return trapResult.
  2114. /*
  2115. To avoid creating targetConfigurableKeys, targetNonconfigurableKeys and uncheckedResultKeys list in above steps,
  2116. use below algorithm to accomplish same behavior
  2117. // Track if there are any properties that are present in target but not present in trap result
  2118. for(var i = 0; i < trapResult.length; i++)
  2119. {
  2120. PropertyId propId = GetPropertyId(trapResult[i]);
  2121. if(propId != NoProperty) { targetToTrapResultMap[propId] = 1; }
  2122. else { isTrapResultMissingFromTargetKeys = true; }
  2123. }
  2124. isConfigurableKeyMissingFromTrapResult = false;
  2125. isNonconfigurableKeyMissingFromTrapResult = false;
  2126. for(var i = 0; i < targetKeys.length; i++)
  2127. {
  2128. PropertyId propId = GetPropertyId(targetKeys[i]);
  2129. Var desc = GetPropertyDescriptor(propId);
  2130. if(targetToTrapResultMap[propId]) {
  2131. delete targetToTrapResultMap[propId];
  2132. isMissingFromTrapResult = false;
  2133. } else {
  2134. isMissingFromTrapResult = true;
  2135. }
  2136. if(desc->IsConfigurable()) {
  2137. if(isMissingFromTrapResult) {
  2138. isConfigurableKeyMissingFromTrapResult = true;
  2139. }
  2140. } else {
  2141. isAnyNonconfigurableKeyPresent = true
  2142. if(isMissingFromTrapResult) {
  2143. isNonconfigurableKeyMissingFromTrapResult = true;
  2144. }
  2145. }
  2146. }
  2147. // 19.
  2148. if(isExtensible && !isAnyNonconfigurableKeyPresent) { return trapResult; }
  2149. // 21.
  2150. if(isNonconfigurableKeyMissingFromTrapResult) { throw TypeError; }
  2151. // 22.
  2152. if(isExtensible) { return trapResult; }
  2153. // 23.
  2154. if(isConfigurableKeyMissingFromTrapResult) { throw TypeError; }
  2155. // 24.
  2156. if(!targetToTrapResultMap.Empty()) { throw TypeError; }
  2157. return trapResult;
  2158. */
  2159. JavascriptArray* trapResult = requestContext->GetLibrary()->CreateArray(0);
  2160. bool isConfigurableKeyMissingFromTrapResult = false;
  2161. bool isNonconfigurableKeyMissingFromTrapResult = false;
  2162. bool isKeyMissingFromTrapResult = false;
  2163. bool isKeyMissingFromTargetResult = false;
  2164. bool isAnyNonconfigurableKeyPresent = false;
  2165. Var element;
  2166. PropertyId propertyId;
  2167. const PropertyRecord* propertyRecord = nullptr;
  2168. BOOL isTargetExtensible = FALSE;
  2169. BEGIN_TEMP_ALLOCATOR(tempAllocator, requestContext, _u("Runtime"))
  2170. {
  2171. // Dictionary containing intersection of keys present in targetKeys and trapResult
  2172. Var lenValue = JavascriptOperators::OP_GetLength(trapResultArray, requestContext);
  2173. uint32 len = (uint32)JavascriptConversion::ToLength(lenValue, requestContext);
  2174. JsUtil::BaseDictionary<Js::PropertyId, bool, ArenaAllocator> targetToTrapResultMap(tempAllocator, len);
  2175. // Trap result to return.
  2176. // Note : This will not necessarily have all elements present in trapResultArray. E.g. If trap was called from GetOwnPropertySymbols()
  2177. // trapResult will only contain symbol elements from trapResultArray.
  2178. switch (keysTrapKind)
  2179. {
  2180. case GetOwnPropertyNamesKind:
  2181. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2182. [&](const PropertyRecord *propertyRecord)->bool
  2183. {
  2184. return !propertyRecord->IsSymbol();
  2185. });
  2186. break;
  2187. case GetOwnPropertySymbolKind:
  2188. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2189. [&](const PropertyRecord *propertyRecord)->bool
  2190. {
  2191. return propertyRecord->IsSymbol();
  2192. });
  2193. break;
  2194. case KeysKind:
  2195. GetOwnPropertyKeysHelper(requestContext, trapResultArray, len, trapResult, targetToTrapResultMap,
  2196. [&](const PropertyRecord *propertyRecord)->bool
  2197. {
  2198. return true;
  2199. });
  2200. break;
  2201. }
  2202. isTargetExtensible = targetObj->IsExtensible();
  2203. targetKeys = JavascriptOperators::GetOwnPropertyKeys(targetObj, requestContext);
  2204. for (uint32 i = 0; i < targetKeys->GetLength(); i++)
  2205. {
  2206. element = targetKeys->DirectGetItem(i);
  2207. 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)");
  2208. JavascriptConversion::ToPropertyKey(element, requestContext, &propertyRecord, nullptr);
  2209. propertyId = propertyRecord->GetPropertyId();
  2210. if (propertyId == Constants::NoProperty)
  2211. continue;
  2212. // If not present in intersection means either the property is not present in targetKeys or
  2213. // we have already visited the property in targetKeys
  2214. if (targetToTrapResultMap.ContainsKey(propertyId))
  2215. {
  2216. isKeyMissingFromTrapResult = false;
  2217. targetToTrapResultMap.Remove(propertyId);
  2218. }
  2219. else
  2220. {
  2221. isKeyMissingFromTrapResult = true;
  2222. }
  2223. PropertyDescriptor targetKeyPropertyDescriptor;
  2224. if (Js::JavascriptOperators::GetOwnPropertyDescriptor(targetObj, propertyId, requestContext, &targetKeyPropertyDescriptor) && !targetKeyPropertyDescriptor.IsConfigurable())
  2225. {
  2226. isAnyNonconfigurableKeyPresent = true;
  2227. if (isKeyMissingFromTrapResult)
  2228. {
  2229. isNonconfigurableKeyMissingFromTrapResult = true;
  2230. }
  2231. }
  2232. else
  2233. {
  2234. if (isKeyMissingFromTrapResult)
  2235. {
  2236. isConfigurableKeyMissingFromTrapResult = true;
  2237. }
  2238. }
  2239. }
  2240. // Keys that were not found in targetKeys will continue to remain in the map
  2241. isKeyMissingFromTargetResult = targetToTrapResultMap.Count() != 0;
  2242. }
  2243. END_TEMP_ALLOCATOR(tempAllocator, requestContext)
  2244. // 19.
  2245. if (isTargetExtensible && !isAnyNonconfigurableKeyPresent)
  2246. {
  2247. return trapResult;
  2248. }
  2249. // 21.
  2250. if (isNonconfigurableKeyMissingFromTrapResult)
  2251. {
  2252. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2253. }
  2254. // 22.
  2255. if (isTargetExtensible)
  2256. {
  2257. return trapResult;
  2258. }
  2259. // 23.
  2260. if (isConfigurableKeyMissingFromTrapResult)
  2261. {
  2262. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2263. }
  2264. // 24.
  2265. if (isKeyMissingFromTargetResult)
  2266. {
  2267. JavascriptError::ThrowTypeError(requestContext, JSERR_InconsistentTrapResult, _u("ownKeys"));
  2268. }
  2269. return trapResult;
  2270. }
  2271. #if ENABLE_TTD
  2272. void JavascriptProxy::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
  2273. {
  2274. if(this->handler != nullptr)
  2275. {
  2276. extractor->MarkVisitVar(this->handler);
  2277. }
  2278. if(this->target != nullptr)
  2279. {
  2280. extractor->MarkVisitVar(this->target);
  2281. }
  2282. }
  2283. TTD::NSSnapObjects::SnapObjectType JavascriptProxy::GetSnapTag_TTD() const
  2284. {
  2285. return TTD::NSSnapObjects::SnapObjectType::SnapProxyObject;
  2286. }
  2287. void JavascriptProxy::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  2288. {
  2289. TTD::NSSnapObjects::SnapProxyInfo* spi = alloc.SlabAllocateStruct<TTD::NSSnapObjects::SnapProxyInfo>();
  2290. const uint32 reserveSize = 2;
  2291. uint32 depOnCount = 0;
  2292. TTD_PTR_ID* depOnArray = alloc.SlabReserveArraySpace<TTD_PTR_ID>(reserveSize);
  2293. spi->HandlerId = TTD_INVALID_PTR_ID;
  2294. if(this->handler != nullptr)
  2295. {
  2296. spi->HandlerId = TTD_CONVERT_VAR_TO_PTR_ID(this->handler);
  2297. if(TTD::JsSupport::IsVarComplexKind(this->handler))
  2298. {
  2299. depOnArray[depOnCount] = TTD_CONVERT_VAR_TO_PTR_ID(this->handler);
  2300. depOnCount++;
  2301. }
  2302. }
  2303. spi->TargetId = TTD_INVALID_PTR_ID;
  2304. if(this->target != nullptr)
  2305. {
  2306. spi->TargetId = TTD_CONVERT_VAR_TO_PTR_ID(this->target);
  2307. if(TTD::JsSupport::IsVarComplexKind(this->handler))
  2308. {
  2309. depOnArray[depOnCount] = TTD_CONVERT_VAR_TO_PTR_ID(this->target);
  2310. depOnCount++;
  2311. }
  2312. }
  2313. if(depOnCount == 0)
  2314. {
  2315. alloc.SlabAbortArraySpace<TTD_PTR_ID>(reserveSize);
  2316. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapProxyInfo*, TTD::NSSnapObjects::SnapObjectType::SnapProxyObject>(objData, spi);
  2317. }
  2318. else
  2319. {
  2320. alloc.SlabCommitArraySpace<TTD_PTR_ID>(depOnCount, reserveSize);
  2321. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapProxyInfo*, TTD::NSSnapObjects::SnapObjectType::SnapProxyObject>(objData, spi, alloc, depOnCount, depOnArray);
  2322. }
  2323. }
  2324. #endif
  2325. }