JavascriptProxy.cpp 103 KB

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