JavascriptProxy.cpp 108 KB

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