JavascriptProxy.cpp 103 KB

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