JavascriptProxy.cpp 111 KB

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