JavascriptFunction.cpp 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068
  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. #include "BackEndAPI.h"
  7. #include "Library\StackScriptFunction.h"
  8. #include "Types\SpreadArgument.h"
  9. #ifdef _M_X64
  10. #include "ByteCode\PropertyIdArray.h"
  11. #include "Language\AsmJsTypes.h"
  12. #include "Language\AsmJsModule.h"
  13. #endif
  14. extern "C" PVOID _ReturnAddress(VOID);
  15. #pragma intrinsic(_ReturnAddress)
  16. #ifdef _M_IX86
  17. #ifdef _CONTROL_FLOW_GUARD
  18. extern "C" PVOID __guard_check_icall_fptr;
  19. #endif
  20. extern "C" void __cdecl _alloca_probe_16();
  21. #endif
  22. namespace Js
  23. {
  24. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(JavascriptFunction);
  25. JavascriptFunction::JavascriptFunction(DynamicType * type)
  26. : DynamicObject(type), functionInfo(nullptr), constructorCache(&ConstructorCache::DefaultInstance)
  27. {
  28. Assert(this->constructorCache != nullptr);
  29. }
  30. JavascriptFunction::JavascriptFunction(DynamicType * type, FunctionInfo * functionInfo)
  31. : DynamicObject(type), functionInfo(functionInfo), constructorCache(&ConstructorCache::DefaultInstance)
  32. {
  33. Assert(this->constructorCache != nullptr);
  34. this->GetTypeHandler()->ClearHasOnlyWritableDataProperties(); // length is non-writable
  35. if (GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag)
  36. {
  37. // No need to invalidate store field caches for non-writable properties here. Since this type is just being created, it cannot represent
  38. // an object that is already a prototype. If it becomes a prototype and then we attempt to add a property to an object derived from this
  39. // object, then we will check if this property is writable, and only if it is will we do the fast path for add property.
  40. // GetScriptContext()->InvalidateStoreFieldCaches(PropertyIds::length);
  41. GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
  42. }
  43. }
  44. JavascriptFunction::JavascriptFunction(DynamicType * type, FunctionInfo * functionInfo, ConstructorCache* cache)
  45. : DynamicObject(type), functionInfo(functionInfo), constructorCache(cache)
  46. {
  47. Assert(this->constructorCache != nullptr);
  48. this->GetTypeHandler()->ClearHasOnlyWritableDataProperties(); // length is non-writable
  49. if (GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag)
  50. {
  51. // No need to invalidate store field caches for non-writable properties here. Since this type is just being created, it cannot represent
  52. // an object that is already a prototype. If it becomes a prototype and then we attempt to add a property to an object derived from this
  53. // object, then we will check if this property is writable, and only if it is will we do the fast path for add property.
  54. // GetScriptContext()->InvalidateStoreFieldCaches(PropertyIds::length);
  55. GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
  56. }
  57. }
  58. FunctionProxy *JavascriptFunction::GetFunctionProxy() const
  59. {
  60. Assert(functionInfo != nullptr);
  61. return functionInfo->GetFunctionProxy();
  62. }
  63. ParseableFunctionInfo *JavascriptFunction::GetParseableFunctionInfo() const
  64. {
  65. Assert(functionInfo != nullptr);
  66. return functionInfo->GetParseableFunctionInfo();
  67. }
  68. DeferDeserializeFunctionInfo *JavascriptFunction::GetDeferDeserializeFunctionInfo() const
  69. {
  70. Assert(functionInfo != nullptr);
  71. return functionInfo->GetDeferDeserializeFunctionInfo();
  72. }
  73. FunctionBody *JavascriptFunction::GetFunctionBody() const
  74. {
  75. Assert(functionInfo != nullptr);
  76. return functionInfo->GetFunctionBody();
  77. }
  78. BOOL JavascriptFunction::IsScriptFunction() const
  79. {
  80. Assert(functionInfo != nullptr);
  81. return functionInfo->HasBody();
  82. }
  83. bool JavascriptFunction::Is(Var aValue)
  84. {
  85. if (JavascriptOperators::GetTypeId(aValue) == TypeIds_Function)
  86. {
  87. return true;
  88. }
  89. return false;
  90. }
  91. JavascriptFunction* JavascriptFunction::FromVar(Var aValue)
  92. {
  93. AssertMsg(Is(aValue), "Ensure var is actually a 'JavascriptFunction'");
  94. return static_cast<JavascriptFunction *>(RecyclableObject::FromVar(aValue));
  95. }
  96. BOOL JavascriptFunction::IsStrictMode() const
  97. {
  98. FunctionProxy * proxy = this->GetFunctionProxy();
  99. return proxy && proxy->EnsureDeserialized()->GetIsStrictMode();
  100. }
  101. BOOL JavascriptFunction::IsLambda() const
  102. {
  103. return this->GetFunctionInfo()->IsLambda();
  104. }
  105. BOOL JavascriptFunction::IsConstructor() const
  106. {
  107. return this->GetFunctionInfo()->IsConstructor();
  108. }
  109. #if DBG
  110. /* static */
  111. bool JavascriptFunction::IsBuiltinProperty(Var objectWithProperty, PropertyIds propertyId)
  112. {
  113. return ScriptFunction::Is(objectWithProperty)
  114. && (propertyId == PropertyIds::length || (JavascriptFunction::FromVar(objectWithProperty)->HasRestrictedProperties() && (propertyId == PropertyIds::arguments || propertyId == PropertyIds::caller)));
  115. }
  116. #endif
  117. static wchar_t const funcName[] = L"function anonymous";
  118. static wchar_t const genFuncName[] = L"function* anonymous";
  119. static wchar_t const asyncFuncName[] = L"async function anonymous";
  120. static wchar_t const bracket[] = L" {\012";
  121. Var JavascriptFunction::NewInstanceHelper(ScriptContext *scriptContext, RecyclableObject* function, CallInfo callInfo, Js::ArgumentReader& args, FunctionKind functionKind /* = FunctionKind::Normal */)
  122. {
  123. JavascriptLibrary* library = function->GetLibrary();
  124. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  125. // SkipDefaultNewObject function flag should have prevented the default object from
  126. // being created, except when call true a host dispatch.
  127. Var newTarget = callInfo.Flags & CallFlags_NewTarget ? args.Values[args.Info.Count] : args[0];
  128. bool isCtorSuperCall = (callInfo.Flags & CallFlags_New) && newTarget != nullptr && RecyclableObject::Is(newTarget);
  129. Assert(isCtorSuperCall || !(callInfo.Flags & CallFlags_New) || args[0] == nullptr
  130. || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  131. JavascriptString* separator = library->GetCommaDisplayString();
  132. // Gather all the formals into a string like (fml1, fml2, fml3)
  133. JavascriptString *formals = library->CreateStringFromCppLiteral(L"(");
  134. for (uint i = 1; i < args.Info.Count - 1; ++i)
  135. {
  136. if (i != 1)
  137. {
  138. formals = JavascriptString::Concat(formals, separator);
  139. }
  140. formals = JavascriptString::Concat(formals, JavascriptConversion::ToString(args.Values[i], scriptContext));
  141. }
  142. formals = JavascriptString::Concat(formals, library->CreateStringFromCppLiteral(L")"));
  143. // Function body, last argument to Function(...)
  144. JavascriptString *fnBody = NULL;
  145. if (args.Info.Count > 1)
  146. {
  147. fnBody = JavascriptConversion::ToString(args.Values[args.Info.Count - 1], scriptContext);
  148. }
  149. // Create a string representing the anonymous function
  150. Assert(CountNewlines(funcName) + CountNewlines(bracket) == numberLinesPrependedToAnonymousFunction); // Be sure to add exactly one line to anonymous function
  151. JavascriptString *bs = functionKind == FunctionKind::Async ?
  152. library->CreateStringFromCppLiteral(asyncFuncName) :
  153. functionKind == FunctionKind::Generator ?
  154. library->CreateStringFromCppLiteral(genFuncName) :
  155. library->CreateStringFromCppLiteral(funcName);
  156. bs = JavascriptString::Concat(bs, formals);
  157. bs = JavascriptString::Concat(bs, library->CreateStringFromCppLiteral(bracket));
  158. if (fnBody != NULL)
  159. {
  160. bs = JavascriptString::Concat(bs, fnBody);
  161. }
  162. bs = JavascriptString::Concat(bs, library->CreateStringFromCppLiteral(L"\012}"));
  163. // Bug 1105479. Get the module id from the caller
  164. ModuleID moduleID = kmodGlobal;
  165. BOOL strictMode = FALSE;
  166. JavascriptFunction *pfuncScript;
  167. ParseableFunctionInfo *pfuncBodyCache = NULL;
  168. wchar_t const * sourceString = bs->GetSz();
  169. charcount_t sourceLen = bs->GetLength();
  170. EvalMapString key(sourceString, sourceLen, moduleID, strictMode, /* isLibraryCode = */ false);
  171. if (!scriptContext->IsInNewFunctionMap(key, &pfuncBodyCache))
  172. {
  173. // Validate formals here
  174. scriptContext->GetGlobalObject()->ValidateSyntax(
  175. scriptContext, formals->GetSz(), formals->GetLength(),
  176. functionKind == FunctionKind::Generator, functionKind == FunctionKind::Async,
  177. &Parser::ValidateFormals);
  178. if (fnBody != NULL)
  179. {
  180. // Validate function body
  181. scriptContext->GetGlobalObject()->ValidateSyntax(
  182. scriptContext, fnBody->GetSz(), fnBody->GetLength(),
  183. functionKind == FunctionKind::Generator, functionKind == FunctionKind::Async,
  184. &Parser::ValidateSourceElementList);
  185. }
  186. pfuncScript = scriptContext->GetGlobalObject()->EvalHelper(scriptContext, sourceString, sourceLen, moduleID, fscrNil, Constants::FunctionCode, TRUE, TRUE, strictMode);
  187. // Indicate that this is a top-level function. We don't pass the fscrGlobalCode flag to the eval helper,
  188. // or it will return the global function that wraps the declared function body, as though it were an eval.
  189. // But we want, for instance, to be able to verify that we did the right amount of deferred parsing.
  190. ParseableFunctionInfo *functionInfo = pfuncScript->GetParseableFunctionInfo();
  191. Assert(functionInfo);
  192. functionInfo->SetGrfscr(functionInfo->GetGrfscr() | fscrGlobalCode);
  193. scriptContext->AddToNewFunctionMap(key, functionInfo);
  194. }
  195. else
  196. {
  197. // Get the latest proxy
  198. FunctionProxy * proxy = pfuncBodyCache->GetFunctionProxy();
  199. if (proxy->IsGenerator())
  200. {
  201. pfuncScript = scriptContext->GetLibrary()->CreateGeneratorVirtualScriptFunction(proxy);
  202. }
  203. else
  204. {
  205. pfuncScript = scriptContext->GetLibrary()->CreateScriptFunction(proxy);
  206. }
  207. }
  208. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_FUNCTION(pfuncScript, EtwTrace::GetFunctionId(pfuncScript->GetFunctionProxy())));
  209. if (functionKind == FunctionKind::Generator)
  210. {
  211. Assert(pfuncScript->GetFunctionInfo()->IsGenerator());
  212. auto pfuncVirt = static_cast<GeneratorVirtualScriptFunction*>(pfuncScript);
  213. auto pfuncGen = scriptContext->GetLibrary()->CreateGeneratorFunction(JavascriptGeneratorFunction::EntryGeneratorFunctionImplementation, pfuncVirt);
  214. pfuncVirt->SetRealGeneratorFunction(pfuncGen);
  215. pfuncScript = pfuncGen;
  216. }
  217. return isCtorSuperCall ?
  218. JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), pfuncScript, nullptr, scriptContext) :
  219. pfuncScript;
  220. }
  221. Var JavascriptFunction::NewInstanceRestrictedMode(RecyclableObject* function, CallInfo callInfo, ...)
  222. {
  223. ScriptContext* scriptContext = function->GetScriptContext();
  224. scriptContext->CheckEvalRestriction();
  225. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  226. ARGUMENTS(args, callInfo);
  227. return NewInstanceHelper(scriptContext, function, callInfo, args);
  228. }
  229. Var JavascriptFunction::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  230. {
  231. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  232. ARGUMENTS(args, callInfo);
  233. ScriptContext* scriptContext = function->GetScriptContext();
  234. return NewInstanceHelper(scriptContext, function, callInfo, args);
  235. }
  236. Var JavascriptFunction::NewAsyncFunctionInstance(RecyclableObject* function, CallInfo callInfo, ...)
  237. {
  238. // Get called when creating a new async function through the constructor (e.g. af.__proto__.constructor)
  239. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  240. ARGUMENTS(args, callInfo);
  241. return JavascriptFunction::NewInstanceHelper(function->GetScriptContext(), function, callInfo, args, JavascriptFunction::FunctionKind::Async);
  242. }
  243. //
  244. // Dummy EntryPoint for Function.prototype
  245. //
  246. Var JavascriptFunction::PrototypeEntryPoint(RecyclableObject* function, CallInfo callInfo, ...)
  247. {
  248. ARGUMENTS(args, callInfo);
  249. ScriptContext* scriptContext = function->GetScriptContext();
  250. JavascriptLibrary* library = function->GetLibrary();
  251. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  252. if (callInfo.Flags & CallFlags_New)
  253. {
  254. JavascriptError::ThrowTypeError(scriptContext, VBSERR_ActionNotSupported);
  255. }
  256. return library->GetUndefined();
  257. }
  258. BOOL JavascriptFunction::IsThrowTypeErrorFunction()
  259. {
  260. ScriptContext* scriptContext = this->GetScriptContext();
  261. Assert(scriptContext);
  262. return
  263. this == scriptContext->GetLibrary()->GetThrowTypeErrorAccessorFunction() ||
  264. this == scriptContext->GetLibrary()->GetThrowTypeErrorCalleeAccessorFunction() ||
  265. this == scriptContext->GetLibrary()->GetThrowTypeErrorCallerAccessorFunction() ||
  266. this == scriptContext->GetLibrary()->GetThrowTypeErrorArgumentsAccessorFunction();
  267. }
  268. enum : unsigned { STACK_ARGS_ALLOCA_THRESHOLD = 8 }; // Number of stack args we allow before using _alloca
  269. // ES5 15.3.4.3
  270. //When the apply method is called on an object func with arguments thisArg and argArray the following steps are taken:
  271. // 1. If IsCallable(func) is false, then throw a TypeError exception.
  272. // 2. If argArray is null or undefined, then
  273. // a. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and an empty list of arguments.
  274. // 3. If Type(argArray) is not Object, then throw a TypeError exception.
  275. // 4. Let len be the result of calling the [[Get]] internal method of argArray with argument "length".
  276. //
  277. // Steps 5 and 7 deleted from July 19 Errata of ES5 spec
  278. //
  279. // 5. If len is null or undefined, then throw a TypeError exception.
  280. // 6. Len n be ToUint32(len).
  281. // 7. If n is not equal to ToNumber(len), then throw a TypeError exception.
  282. // 8. Let argList be an empty List.
  283. // 9. Let index be 0.
  284. // 10. Repeat while index < n
  285. // a. Let indexName be ToString(index).
  286. // b. Let nextArg be the result of calling the [[Get]] internal method of argArray with indexName as the argument.
  287. // c. Append nextArg as the last element of argList.
  288. // d. Set index to index + 1.
  289. // 11. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.
  290. // The length property of the apply method is 2.
  291. Var JavascriptFunction::EntryApply(RecyclableObject* function, CallInfo callInfo, ...)
  292. {
  293. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  294. // Ideally, we want to maintain CallFlags_Eval behavior and pass along the extra FrameDisplay parameter
  295. // but that we would be a bigger change than what we want to do in this ship cycle. See WIN8: 915315.
  296. // If eval is executed using apply it will not get the frame display and always execute in global scope.
  297. ARGUMENTS(args, callInfo);
  298. ScriptContext* scriptContext = function->GetScriptContext();
  299. Assert(!(callInfo.Flags & CallFlags_New));
  300. ///
  301. /// Check Argument[0] has internal [[Call]] property
  302. /// If not, throw TypeError
  303. ///
  304. if (args.Info.Count == 0 || !JavascriptConversion::IsCallable(args[0]))
  305. {
  306. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, L"Function.prototype.apply");
  307. }
  308. Var thisVar = NULL;
  309. Var argArray = NULL;
  310. RecyclableObject* pFunc = RecyclableObject::FromVar(args[0]);
  311. if (args.Info.Count == 1)
  312. {
  313. thisVar = scriptContext->GetLibrary()->GetUndefined();
  314. }
  315. else if (args.Info.Count == 2)
  316. {
  317. thisVar = args.Values[1];
  318. }
  319. else if (args.Info.Count > 2)
  320. {
  321. thisVar = args.Values[1];
  322. argArray = args.Values[2];
  323. }
  324. return CalloutHelper<false>(pFunc, thisVar, /* overridingNewTarget = */nullptr, argArray, scriptContext);
  325. }
  326. template <bool isConstruct>
  327. Var JavascriptFunction::CalloutHelper(RecyclableObject* pFunc, Var thisVar, Var overridingNewTarget, Var argArray, ScriptContext* scriptContext)
  328. {
  329. CallFlags callFlag;
  330. if (isConstruct)
  331. {
  332. callFlag = CallFlags_New;
  333. }
  334. else
  335. {
  336. callFlag = CallFlags_Value;
  337. }
  338. Arguments outArgs(CallInfo(callFlag, 0), nullptr);
  339. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  340. if (nullptr == argArray)
  341. {
  342. outArgs.Info.Count = 1;
  343. outArgs.Values = &thisVar;
  344. }
  345. else
  346. {
  347. bool isArray = JavascriptArray::Is(argArray);
  348. TypeId typeId = JavascriptOperators::GetTypeId(argArray);
  349. bool isNullOrUndefined = (typeId == TypeIds_Null || typeId == TypeIds_Undefined);
  350. if (!isNullOrUndefined && !JavascriptOperators::IsObject(argArray)) // ES5: throw if Type(argArray) is not Object
  351. {
  352. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, L"Function.prototype.apply");
  353. }
  354. int64 len;
  355. JavascriptArray* arr = NULL;
  356. RecyclableObject* dynamicObject = RecyclableObject::FromVar(argArray);
  357. if (isNullOrUndefined)
  358. {
  359. len = 0;
  360. }
  361. else if (isArray)
  362. {
  363. #if ENABLE_COPYONACCESS_ARRAY
  364. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(argArray);
  365. #endif
  366. arr = JavascriptArray::FromVar(argArray);
  367. len = arr->GetLength();
  368. }
  369. else
  370. {
  371. Var lenProp = JavascriptOperators::OP_GetLength(dynamicObject, scriptContext);
  372. len = JavascriptConversion::ToLength(lenProp, scriptContext);
  373. }
  374. if (len >= CallInfo::kMaxCountArgs)
  375. {
  376. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  377. }
  378. outArgs.Info.Count = (uint)len + 1;
  379. if (len == 0)
  380. {
  381. outArgs.Values = &thisVar;
  382. }
  383. else
  384. {
  385. if (outArgs.Info.Count > STACK_ARGS_ALLOCA_THRESHOLD)
  386. {
  387. PROBE_STACK(scriptContext, outArgs.Info.Count * sizeof(Var)+Js::Constants::MinStackDefault); // args + function call
  388. outArgs.Values = (Var*)_alloca(outArgs.Info.Count * sizeof(Var));
  389. }
  390. else
  391. {
  392. outArgs.Values = stackArgs;
  393. }
  394. outArgs.Values[0] = thisVar;
  395. Var undefined = pFunc->GetLibrary()->GetUndefined();
  396. if (isArray && arr->GetScriptContext() == scriptContext)
  397. {
  398. arr->ForEachItemInRange<false>(0, (uint)len, undefined, scriptContext,
  399. [&outArgs](uint index, Var element)
  400. {
  401. outArgs.Values[index + 1] = element;
  402. });
  403. }
  404. else
  405. {
  406. for (uint i = 0; i < len; i++)
  407. {
  408. Var element;
  409. if (!JavascriptOperators::GetItem(dynamicObject, i, &element, scriptContext))
  410. {
  411. element = undefined;
  412. }
  413. outArgs.Values[i + 1] = element;
  414. }
  415. }
  416. }
  417. }
  418. if (isConstruct)
  419. {
  420. return JavascriptFunction::CallAsConstructor(pFunc, overridingNewTarget, outArgs, scriptContext);
  421. }
  422. else
  423. {
  424. return JavascriptFunction::CallFunction<true>(pFunc, pFunc->GetEntryPoint(), outArgs);
  425. }
  426. }
  427. Var JavascriptFunction::ApplyHelper(RecyclableObject* function, Var thisArg, Var argArray, ScriptContext* scriptContext)
  428. {
  429. return CalloutHelper<false>(function, thisArg, /* overridingNewTarget = */nullptr, argArray, scriptContext);
  430. }
  431. Var JavascriptFunction::ConstructHelper(RecyclableObject* function, Var thisArg, Var overridingNewTarget, Var argArray, ScriptContext* scriptContext)
  432. {
  433. return CalloutHelper<true>(function, thisArg, overridingNewTarget, argArray, scriptContext);
  434. }
  435. Var JavascriptFunction::EntryBind(RecyclableObject* function, CallInfo callInfo, ...)
  436. {
  437. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  438. ARGUMENTS(args, callInfo);
  439. ScriptContext* scriptContext = function->GetScriptContext();
  440. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(FunctionBindCount);
  441. Assert(!(callInfo.Flags & CallFlags_New));
  442. ///
  443. /// Check Argument[0] has internal [[Call]] property
  444. /// If not, throw TypeError
  445. ///
  446. if (args.Info.Count == 0 || !JavascriptConversion::IsCallable(args[0]))
  447. {
  448. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, L"Function.prototype.bind");
  449. }
  450. BoundFunction* boundFunc = BoundFunction::New(scriptContext, args);
  451. return boundFunc;
  452. }
  453. // ES5 15.3.4.4
  454. // Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
  455. // When the call method is called on an object func with argument thisArg and optional arguments arg1, arg2 etc, the following steps are taken:
  456. // 1. If IsCallable(func) is false, then throw a TypeError exception.
  457. // 2. Let argList be an empty List.
  458. // 3. If this method was called with more than one argument then in left to right order starting with arg1 append each argument as the last element of argList
  459. // 4. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.
  460. // The length property of the call method is 1.
  461. Var JavascriptFunction::EntryCall(RecyclableObject* function, CallInfo callInfo, ...)
  462. {
  463. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  464. RUNTIME_ARGUMENTS(args, callInfo);
  465. ScriptContext* scriptContext = function->GetScriptContext();
  466. Assert(!(callInfo.Flags & CallFlags_New));
  467. ///
  468. /// Check Argument[0] has internal [[Call]] property
  469. /// If not, throw TypeError
  470. ///
  471. if (args.Info.Count == 0 || !JavascriptConversion::IsCallable(args[0]))
  472. {
  473. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, L"Function.prototype.call");
  474. }
  475. RecyclableObject *pFunc = RecyclableObject::FromVar(args[0]);
  476. if (args.Info.Count == 1)
  477. {
  478. args.Values[0] = scriptContext->GetLibrary()->GetUndefined();
  479. }
  480. else
  481. {
  482. ///
  483. /// Remove function object from the arguments and pass the rest
  484. ///
  485. for (uint i = 0; i < args.Info.Count - 1; ++i)
  486. {
  487. args.Values[i] = args.Values[i + 1];
  488. }
  489. args.Info.Count = args.Info.Count - 1;
  490. }
  491. ///
  492. /// Call the [[Call]] method on the function object
  493. ///
  494. return JavascriptFunction::CallFunction<true>(pFunc, pFunc->GetEntryPoint(), args);
  495. }
  496. Var JavascriptFunction::CallRootFunctionInScript(JavascriptFunction* func, Arguments args)
  497. {
  498. ScriptContext* scriptContext = func->GetScriptContext();
  499. if (scriptContext->GetThreadContext()->HasPreviousHostScriptContext())
  500. {
  501. ScriptContext* requestContext = scriptContext->GetThreadContext()->GetPreviousHostScriptContext()->GetScriptContext();
  502. func = JavascriptFunction::FromVar(CrossSite::MarshalVar(requestContext, func));
  503. }
  504. return func->CallRootFunction(args, scriptContext, true);
  505. }
  506. Var JavascriptFunction::CallRootFunction(Arguments args, ScriptContext * scriptContext, bool inScript)
  507. {
  508. #ifdef _M_X64
  509. Var ret = nullptr;
  510. #ifdef FAULT_INJECTION
  511. if (Js::Configuration::Global.flags.FaultInjection >= 0)
  512. {
  513. Js::FaultInjection::pfnHandleAV = JavascriptFunction::ResumeForOutOfBoundsArrayRefs;
  514. __try
  515. {
  516. ret = CallRootFunctionInternal(args, scriptContext, inScript);
  517. }
  518. __finally
  519. {
  520. Js::FaultInjection::pfnHandleAV = nullptr;
  521. }
  522. //ret should never be null here
  523. Assert(ret);
  524. return ret;
  525. }
  526. #endif
  527. __try
  528. {
  529. ret = CallRootFunctionInternal(args, scriptContext, inScript);
  530. }
  531. __except (ResumeForOutOfBoundsArrayRefs(GetExceptionCode(), GetExceptionInformation()))
  532. {
  533. // should never reach here
  534. Assert(false);
  535. }
  536. //ret should never be null here
  537. Assert(ret);
  538. return ret;
  539. #else
  540. return CallRootFunctionInternal(args, scriptContext, inScript);
  541. #endif
  542. }
  543. Var JavascriptFunction::CallRootFunctionInternal(Arguments args, ScriptContext * scriptContext, bool inScript)
  544. {
  545. #if DBG
  546. if (IsInAssert != 0)
  547. {
  548. // Just don't execute anything if we are in an assert
  549. // throw the exception directly to avoid additional assert in Js::Throw::InternalError
  550. throw Js::InternalErrorException();
  551. }
  552. #endif
  553. if (inScript)
  554. {
  555. Assert(!(args.Info.Flags & CallFlags_New));
  556. return JavascriptFunction::CallFunction<true>(this, GetEntryPoint(), args);
  557. }
  558. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  559. Js::Var varThis;
  560. if (PHASE_FORCE1(Js::EvalCompilePhase) && args.Info.Count == 0)
  561. {
  562. varThis = JavascriptOperators::OP_GetThis(scriptContext->GetLibrary()->GetUndefined(), kmodGlobal, scriptContext);
  563. args.Info.Flags = (Js::CallFlags)(args.Info.Flags | CallFlags_Eval);
  564. args.Info.Count = 1;
  565. args.Values = &varThis;
  566. }
  567. #endif
  568. Var varResult = nullptr;
  569. ThreadContext *threadContext;
  570. threadContext = scriptContext->GetThreadContext();
  571. JavascriptExceptionObject* pExceptionObject = NULL;
  572. bool hasCaller = scriptContext->GetHostScriptContext() ? !!scriptContext->GetHostScriptContext()->HasCaller() : false;
  573. Assert(scriptContext == GetScriptContext());
  574. BEGIN_JS_RUNTIME_CALLROOT_EX(scriptContext, hasCaller)
  575. {
  576. scriptContext->VerifyAlive(true);
  577. try
  578. {
  579. varResult =
  580. args.Info.Flags & CallFlags_New ?
  581. CallAsConstructor(this, /* overridingNewTarget = */nullptr, args, scriptContext) :
  582. CallFunction<true>(this, this->GetEntryPoint(), args);
  583. // A recent compiler bug 150148 can incorrectly eliminate catch block, temporary workaround
  584. if (threadContext == NULL)
  585. {
  586. throw (JavascriptExceptionObject*)NULL;
  587. }
  588. }
  589. catch (JavascriptExceptionObject* exceptionObject)
  590. {
  591. pExceptionObject = exceptionObject;
  592. }
  593. if (pExceptionObject)
  594. {
  595. pExceptionObject = pExceptionObject->CloneIfStaticExceptionObject(scriptContext);
  596. throw pExceptionObject;
  597. }
  598. }
  599. END_JS_RUNTIME_CALL(scriptContext);
  600. Assert(varResult != nullptr);
  601. return varResult;
  602. }
  603. #if DBG
  604. /*static*/
  605. void JavascriptFunction::CheckValidDebugThunk(ScriptContext* scriptContext, RecyclableObject *function)
  606. {
  607. Assert(scriptContext != nullptr);
  608. Assert(function != nullptr);
  609. if (scriptContext->IsInDebugMode()
  610. && !scriptContext->IsInterpreted() && !CONFIG_FLAG(ForceDiagnosticsMode) // Does not work nicely if we change the default settings.
  611. && function->GetEntryPoint() != scriptContext->CurrentThunk
  612. && function->GetEntryPoint() != scriptContext->CurrentCrossSiteThunk
  613. && JavascriptFunction::Is(function))
  614. {
  615. JavascriptFunction *jsFunction = JavascriptFunction::FromVar(function);
  616. if (!jsFunction->IsBoundFunction()
  617. && !jsFunction->GetFunctionInfo()->IsDeferred()
  618. && (jsFunction->GetFunctionInfo()->GetAttributes() & FunctionInfo::DoNotProfile) != FunctionInfo::DoNotProfile
  619. && jsFunction->GetFunctionInfo() != &JavascriptExternalFunction::EntryInfo::WrappedFunctionThunk)
  620. {
  621. Js::FunctionProxy *proxy = jsFunction->GetFunctionProxy();
  622. if (proxy)
  623. {
  624. AssertMsg(proxy->HasValidEntryPoint(), "Function does not have valid entrypoint");
  625. }
  626. }
  627. }
  628. }
  629. #endif
  630. Var JavascriptFunction::CallAsConstructor(Var v, Var overridingNewTarget, Arguments args, ScriptContext* scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  631. {
  632. Assert(v);
  633. Assert(args.Info.Flags & CallFlags_New);
  634. Assert(scriptContext);
  635. // newCount is ushort.
  636. if (args.Info.Count >= USHORT_MAX)
  637. {
  638. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  639. }
  640. AnalysisAssert(args.Info.Count < USHORT_MAX);
  641. // Create the empty object if necessary:
  642. // - Built-in constructor functions will return a new object of a specific type, so a new empty object does not need to
  643. // be created
  644. // - If the newTarget is specified and the function is base kind then the this object will be already created. So we can
  645. // just use it instead of creating a new one.
  646. // - For user-defined constructor functions, an empty object is created with the function's prototype
  647. Var resultObject = nullptr;
  648. if (overridingNewTarget != nullptr && args.Info.Count > 0)
  649. {
  650. resultObject = args.Values[0];
  651. }
  652. else
  653. {
  654. resultObject = JavascriptOperators::NewScObjectNoCtor(v, scriptContext);
  655. }
  656. // JavascriptOperators::NewScObject should have thrown if 'v' is not a constructor
  657. RecyclableObject* functionObj = RecyclableObject::FromVar(v);
  658. Var* newValues = args.Values;
  659. CallFlags newFlags = args.Info.Flags;
  660. ushort newCount = args.Info.Count;
  661. bool thisAlreadySpecified = false;
  662. if (overridingNewTarget != nullptr)
  663. {
  664. if (ScriptFunction::Is(functionObj) && ScriptFunction::FromVar(functionObj)->GetFunctionInfo()->IsClassConstructor())
  665. {
  666. thisAlreadySpecified = true;
  667. args.Values[0] = overridingNewTarget;
  668. }
  669. else
  670. {
  671. newCount++;
  672. newFlags = (CallFlags)(newFlags | CallFlags_NewTarget | CallFlags_ExtraArg);
  673. const unsigned STACK_ARGS_ALLOCA_THRESHOLD = 8; // Number of stack args we allow before using _alloca
  674. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  675. if (newCount > STACK_ARGS_ALLOCA_THRESHOLD)
  676. {
  677. PROBE_STACK(scriptContext, newCount * sizeof(Var) + Js::Constants::MinStackDefault); // args + function call
  678. newValues = (Var*)_alloca(newCount * sizeof(Var));
  679. }
  680. else
  681. {
  682. newValues = stackArgs;
  683. }
  684. for (unsigned int i = 0; i < args.Info.Count; i++)
  685. {
  686. newValues[i] = args.Values[i];
  687. }
  688. #pragma prefast(suppress:6386, "The index is within the bounds")
  689. newValues[args.Info.Count] = overridingNewTarget;
  690. }
  691. }
  692. // Call the constructor function:
  693. // - If this is not already specified as the overriding new target in Reflect.construct a class case, then
  694. // - Pass in the new empty object as the 'this' parameter. This can be null if an empty object was not created.
  695. if (!thisAlreadySpecified)
  696. {
  697. newValues[0] = resultObject;
  698. }
  699. CallInfo newCallInfo(newFlags, newCount);
  700. Arguments newArgs(newCallInfo, newValues);
  701. if (JavascriptProxy::Is(v))
  702. {
  703. JavascriptProxy* proxy = JavascriptProxy::FromVar(v);
  704. return proxy->ConstructorTrap(newArgs, scriptContext, spreadIndices);
  705. }
  706. #if DBG
  707. if (scriptContext->IsInDebugMode())
  708. {
  709. CheckValidDebugThunk(scriptContext, functionObj);
  710. }
  711. #endif
  712. Var functionResult;
  713. if (spreadIndices != nullptr)
  714. {
  715. functionResult = CallSpreadFunction(functionObj, functionObj->GetEntryPoint(), newArgs, spreadIndices);
  716. }
  717. else
  718. {
  719. functionResult = CallFunction<true>(functionObj, functionObj->GetEntryPoint(), newArgs);
  720. }
  721. return
  722. FinishConstructor(
  723. functionResult,
  724. resultObject,
  725. JavascriptFunction::Is(functionObj) && functionObj->GetScriptContext() == scriptContext ?
  726. JavascriptFunction::FromVar(functionObj) :
  727. nullptr);
  728. }
  729. Var JavascriptFunction::FinishConstructor(
  730. const Var constructorReturnValue,
  731. Var newObject,
  732. JavascriptFunction *const function)
  733. {
  734. Assert(constructorReturnValue);
  735. // CONSIDER: Using constructorCache->ctorHasNoExplicitReturnValue to speed up this interpreter code path.
  736. if (JavascriptOperators::IsObject(constructorReturnValue))
  737. {
  738. newObject = constructorReturnValue;
  739. }
  740. if (function && function->GetConstructorCache()->NeedsUpdateAfterCtor())
  741. {
  742. JavascriptOperators::UpdateNewScObjectCache(function, newObject, function->GetScriptContext());
  743. }
  744. return newObject;
  745. }
  746. Var JavascriptFunction::EntrySpreadCall(const Js::AuxArray<uint32> *spreadIndices, RecyclableObject* function, CallInfo callInfo, ...)
  747. {
  748. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  749. RUNTIME_ARGUMENTS(args, callInfo);
  750. return JavascriptFunction::CallSpreadFunction(function, function->GetEntryPoint(), args, spreadIndices);
  751. }
  752. uint32 JavascriptFunction::GetSpreadSize(const Arguments args, const Js::AuxArray<uint32> *spreadIndices, ScriptContext *scriptContext)
  753. {
  754. // Work out the expanded number of arguments.
  755. uint32 totalLength = args.Info.Count - spreadIndices->count;
  756. ::Math::RecordOverflowPolicy overflow;
  757. for (unsigned i = 0; i < spreadIndices->count; ++i)
  758. {
  759. uint32 elementLength = JavascriptArray::GetSpreadArgLen(args[spreadIndices->elements[i]], scriptContext);
  760. totalLength = UInt32Math::Add(totalLength, elementLength, overflow);
  761. }
  762. if (totalLength >= CallInfo::kMaxCountArgs || overflow.HasOverflowed())
  763. {
  764. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  765. }
  766. return totalLength;
  767. }
  768. void JavascriptFunction::SpreadArgs(const Arguments args, Arguments& destArgs, const Js::AuxArray<uint32> *spreadIndices, ScriptContext *scriptContext)
  769. {
  770. Assert(args.Values != nullptr);
  771. Assert(destArgs.Values != nullptr);
  772. CallInfo callInfo = args.Info;
  773. size_t destArgsByteSize = destArgs.Info.Count * sizeof(Var);
  774. destArgs.Values[0] = args[0];
  775. // Iterate over the arguments, spreading inline. We skip 'this'.
  776. Var undefined = scriptContext->GetLibrary()->GetUndefined();
  777. for (unsigned i = 1, argsIndex = 1, spreadArgIndex = 0; i < callInfo.Count; ++i)
  778. {
  779. uint32 spreadIndex = spreadIndices->elements[spreadArgIndex]; // Next index to be spread.
  780. if (i < spreadIndex)
  781. {
  782. // Copy everything until the next spread index.
  783. js_memcpy_s(destArgs.Values + argsIndex,
  784. destArgsByteSize - (argsIndex * sizeof(Var)),
  785. args.Values + i,
  786. (spreadIndex - i) * sizeof(Var));
  787. argsIndex += spreadIndex - i;
  788. i = spreadIndex - 1;
  789. continue;
  790. }
  791. else if (i > spreadIndex)
  792. {
  793. // Copy everything after the last spread index.
  794. js_memcpy_s(destArgs.Values + argsIndex,
  795. destArgsByteSize - (argsIndex * sizeof(Var)),
  796. args.Values + i,
  797. (args.Info.Count - i) * sizeof(Var));
  798. break;
  799. }
  800. else
  801. {
  802. // Expand the spread element.
  803. Var instance = args[spreadIndex];
  804. if (SpreadArgument::Is(instance))
  805. {
  806. SpreadArgument* spreadedArgs = SpreadArgument::FromVar(instance);
  807. uint size = spreadedArgs->GetArgumentSpreadCount();
  808. const Var * spreadBuffer = spreadedArgs->GetArgumentSpread();
  809. js_memcpy_s(destArgs.Values + argsIndex,
  810. size * sizeof(Var),
  811. spreadBuffer,
  812. size * sizeof(Var));
  813. argsIndex += size;
  814. }
  815. else
  816. {
  817. AssertMsg(JavascriptArray::Is(instance) || TypedArrayBase::Is(instance), "Only SpreadArgument, TypedArray, and JavascriptArray should be listed as spread arguments");
  818. // We first try to interpret the spread parameter as a JavascriptArray.
  819. JavascriptArray *arr = nullptr;
  820. if (JavascriptArray::Is(instance))
  821. {
  822. arr = JavascriptArray::FromVar(instance);
  823. }
  824. if (arr != nullptr && !arr->IsCrossSiteObject())
  825. {
  826. // CONSIDER: Optimize by creating a JavascriptArray routine which allows
  827. // memcpy-like semantics in optimal situations (no gaps, etc.)
  828. if (argsIndex + arr->GetLength() > destArgs.Info.Count)
  829. {
  830. AssertMsg(false, "The array length has changed since we allocated the destArgs buffer?");
  831. Throw::FatalInternalError();
  832. }
  833. for (uint32 j = 0; j < arr->GetLength(); j++)
  834. {
  835. Var element;
  836. if (!arr->DirectGetItemAtFull(j, &element))
  837. {
  838. element = undefined;
  839. }
  840. destArgs.Values[argsIndex++] = element;
  841. }
  842. }
  843. else
  844. {
  845. // Emulate %ArrayPrototype%.values() iterator; basically iterate from 0 to length
  846. RecyclableObject *propertyObject;
  847. if (!JavascriptOperators::GetPropertyObject(instance, scriptContext, &propertyObject))
  848. {
  849. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidSpreadArgument);
  850. }
  851. uint32 len = JavascriptArray::GetSpreadArgLen(instance, scriptContext);
  852. if (argsIndex + len > destArgs.Info.Count)
  853. {
  854. AssertMsg(false, "The array length has changed since we allocated the destArgs buffer?");
  855. Throw::FatalInternalError();
  856. }
  857. for (uint j = 0; j < len; j++)
  858. {
  859. Var element;
  860. if (!JavascriptOperators::GetItem(instance, propertyObject, j, &element, scriptContext))
  861. {
  862. element = undefined;
  863. }
  864. destArgs.Values[argsIndex++] = element;
  865. }
  866. }
  867. }
  868. if (spreadArgIndex < spreadIndices->count - 1)
  869. {
  870. spreadArgIndex++;
  871. }
  872. }
  873. }
  874. }
  875. Var JavascriptFunction::CallSpreadFunction(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args, const Js::AuxArray<uint32> *spreadIndices)
  876. {
  877. ScriptContext* scriptContext = function->GetScriptContext();
  878. // Work out the expanded number of arguments.
  879. uint32 actualLength = GetSpreadSize(args, spreadIndices, scriptContext);
  880. // Allocate (if needed) space for the expanded arguments.
  881. Arguments outArgs(CallInfo(args.Info.Flags, 0), nullptr);
  882. outArgs.Info.Count = actualLength;
  883. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  884. size_t outArgsSize = 0;
  885. if (outArgs.Info.Count > STACK_ARGS_ALLOCA_THRESHOLD)
  886. {
  887. PROBE_STACK(scriptContext, outArgs.Info.Count * sizeof(Var) + Js::Constants::MinStackDefault); // args + function call
  888. outArgsSize = outArgs.Info.Count * sizeof(Var);
  889. outArgs.Values = (Var*)_alloca(outArgsSize);
  890. }
  891. else
  892. {
  893. outArgs.Values = stackArgs;
  894. outArgsSize = STACK_ARGS_ALLOCA_THRESHOLD * sizeof(Var);
  895. ZeroMemory(outArgs.Values, outArgsSize); // We may not use all of the elements
  896. }
  897. SpreadArgs(args, outArgs, spreadIndices, scriptContext);
  898. return JavascriptFunction::CallFunction<true>(function, entryPoint, outArgs);
  899. }
  900. Var JavascriptFunction::CallFunction(Arguments args)
  901. {
  902. return JavascriptFunction::CallFunction<true>(this, this->GetEntryPoint(), args);
  903. }
  904. template Var JavascriptFunction::CallFunction<true>(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args);
  905. template Var JavascriptFunction::CallFunction<false>(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args);
  906. #ifdef _M_IX86
  907. template <bool doStackProbe>
  908. Var JavascriptFunction::CallFunction(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args)
  909. {
  910. Js::Var varResult;
  911. #if DBG && ENABLE_NATIVE_CODEGEN
  912. CheckIsExecutable(function, entryPoint);
  913. #endif
  914. // compute size of stack to reserve
  915. CallInfo callInfo = args.Info;
  916. uint argsSize = callInfo.Count * sizeof(Var);
  917. ScriptContext * scriptContext = function->GetScriptContext();
  918. if (doStackProbe)
  919. {
  920. PROBE_STACK_CALL(scriptContext, function, argsSize);
  921. }
  922. void *data;
  923. void *savedEsp;
  924. __asm
  925. {
  926. // Save ESP
  927. mov savedEsp, esp
  928. mov eax, argsSize
  929. // Make sure we don't go beyond guard page
  930. cmp eax, 0x1000
  931. jge alloca_probe
  932. sub esp, eax
  933. jmp dbl_align
  934. alloca_probe:
  935. // Use alloca to allocate more then a page size
  936. // Alloca assumes eax, contains size, and adjust ESP while
  937. // probing each page.
  938. call _alloca_probe_16
  939. dbl_align:
  940. // 8-byte align frame to improve floating point perf of our JIT'd code.
  941. and esp, -8
  942. mov data, esp
  943. }
  944. {
  945. Var* dest = (Var*)data;
  946. Var* src = args.Values;
  947. for(unsigned int i =0; i < callInfo.Count; i++)
  948. {
  949. dest[i] = src[i];
  950. }
  951. }
  952. // call variable argument function provided in entryPoint
  953. __asm
  954. {
  955. #ifdef _CONTROL_FLOW_GUARD
  956. // verify that the call target is valid
  957. mov ecx, entryPoint
  958. call [__guard_check_icall_fptr]
  959. ; no need to restore ecx ('call entryPoint' is a __cdecl call)
  960. #endif
  961. push callInfo
  962. push function
  963. call entryPoint
  964. // Restore ESP
  965. mov esp, savedEsp
  966. // save the return value from realsum.
  967. mov varResult, eax;
  968. }
  969. return varResult;
  970. }
  971. #elif _M_X64
  972. template <bool doStackProbe>
  973. Var JavascriptFunction::CallFunction(RecyclableObject *function, JavascriptMethod entryPoint, Arguments args)
  974. {
  975. // compute size of stack to reserve and make sure we have enough stack.
  976. CallInfo callInfo = args.Info;
  977. uint argsSize = callInfo.Count * sizeof(Var);
  978. if (doStackProbe == true)
  979. {
  980. PROBE_STACK_CALL(function->GetScriptContext(), function, argsSize);
  981. }
  982. #if DBG && ENABLE_NATIVE_CODEGEN
  983. CheckIsExecutable(function, entryPoint);
  984. #endif
  985. #ifdef _CONTROL_FLOW_GUARD
  986. _guard_check_icall((uintptr_t) entryPoint); /* check function pointer integrity */
  987. #endif
  988. return amd64_CallFunction(function, entryPoint, args.Info, args.Info.Count, &args.Values[0]);
  989. }
  990. #elif defined(_M_ARM)
  991. extern "C"
  992. {
  993. extern Var arm_CallFunction(JavascriptFunction* function, CallInfo info, Var* values, JavascriptMethod entryPoint);
  994. }
  995. template <bool doStackProbe>
  996. Var JavascriptFunction::CallFunction(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args)
  997. {
  998. // compute size of stack to reserve and make sure we have enough stack.
  999. CallInfo callInfo = args.Info;
  1000. uint argsSize = callInfo.Count * sizeof(Var);
  1001. if (doStackProbe)
  1002. {
  1003. PROBE_STACK_CALL(function->GetScriptContext(), function, argsSize);
  1004. }
  1005. #if DBG && ENABLE_NATIVE_CODEGEN
  1006. CheckIsExecutable(function, entryPoint);
  1007. #endif
  1008. Js::Var varResult;
  1009. //The ARM can pass 4 arguments via registers so handle the cases for 0 or 1 values without resorting to asm code
  1010. //(so that the asm code can assume 0 or more values will go on the stack: putting -1 values on the stack is unhealthy).
  1011. unsigned count = args.Info.Count;
  1012. if (count == 0)
  1013. {
  1014. varResult = entryPoint((JavascriptFunction*)function, args.Info);
  1015. }
  1016. else if (count == 1)
  1017. {
  1018. varResult = entryPoint((JavascriptFunction*)function, args.Info, args.Values[0]);
  1019. }
  1020. else
  1021. {
  1022. varResult = arm_CallFunction((JavascriptFunction*)function, args.Info, args.Values, entryPoint);
  1023. }
  1024. return varResult;
  1025. }
  1026. #elif defined(_M_ARM64)
  1027. extern "C"
  1028. {
  1029. extern Var arm64_CallFunction(JavascriptFunction* function, CallInfo info, Var* values, JavascriptMethod entryPoint);
  1030. }
  1031. template <bool doStackProbe>
  1032. Var JavascriptFunction::CallFunction(RecyclableObject* function, JavascriptMethod entryPoint, Arguments args)
  1033. {
  1034. // compute size of stack to reserve and make sure we have enough stack.
  1035. CallInfo callInfo = args.Info;
  1036. uint argsSize = callInfo.Count * sizeof(Var);
  1037. if (doStackProbe)
  1038. {
  1039. PROBE_STACK_CALL(function->GetScriptContext(), function, argsSize);
  1040. }
  1041. #if DBG && ENABLE_NATIVE_CODEGEN
  1042. CheckIsExecutable(function, entryPoint);
  1043. #endif
  1044. Js::Var varResult;
  1045. varResult = arm64_CallFunction((JavascriptFunction*)function, args.Info, args.Values, entryPoint);
  1046. return varResult;
  1047. }
  1048. #else
  1049. Var JavascriptFunction::CallFunction(RecyclableObject *function, JavascriptMethod entryPoint, Arguments args)
  1050. {
  1051. #if DBG && ENABLE_NATIVE_CODEGEN
  1052. CheckIsExecutable(function, entryPoint);
  1053. #endif
  1054. #if 1
  1055. Js::Throw::NotImplemented();
  1056. return nullptr;
  1057. #else
  1058. Var varResult;
  1059. switch (info.Count)
  1060. {
  1061. case 0:
  1062. {
  1063. varResult=entryPoint((JavascriptFunction*)function, args.Info);
  1064. break;
  1065. }
  1066. case 1: {
  1067. varResult=entryPoint(
  1068. (JavascriptFunction*)function,
  1069. args.Info,
  1070. args.Values[0]);
  1071. break;
  1072. }
  1073. case 2: {
  1074. varResult=entryPoint(
  1075. (JavascriptFunction*)function,
  1076. args.Info,
  1077. args.Values[0],
  1078. args.Values[1]);
  1079. break;
  1080. }
  1081. case 3: {
  1082. varResult=entryPoint(
  1083. (JavascriptFunction*)function,
  1084. args.Info,
  1085. args.Values[0],
  1086. args.Values[1],
  1087. args.Values[2]);
  1088. break;
  1089. }
  1090. case 4: {
  1091. varResult=entryPoint(
  1092. (JavascriptFunction*)function,
  1093. args.Info,
  1094. args.Values[0],
  1095. args.Values[1],
  1096. args.Values[2],
  1097. args.Values[3]);
  1098. break;
  1099. }
  1100. case 5: {
  1101. varResult=entryPoint(
  1102. (JavascriptFunction*)function,
  1103. args.Info,
  1104. args.Values[0],
  1105. args.Values[1],
  1106. args.Values[2],
  1107. args.Values[3],
  1108. args.Values[4]);
  1109. break;
  1110. }
  1111. case 6: {
  1112. varResult=entryPoint(
  1113. (JavascriptFunction*)function,
  1114. args.Info,
  1115. args.Values[0],
  1116. args.Values[1],
  1117. args.Values[2],
  1118. args.Values[3],
  1119. args.Values[4],
  1120. args.Values[5]);
  1121. break;
  1122. }
  1123. case 7: {
  1124. varResult=entryPoint(
  1125. (JavascriptFunction*)function,
  1126. args.Info,
  1127. args.Values[0],
  1128. args.Values[1],
  1129. args.Values[2],
  1130. args.Values[3],
  1131. args.Values[4],
  1132. args.Values[5],
  1133. args.Values[6]);
  1134. break;
  1135. }
  1136. case 8: {
  1137. varResult=entryPoint(
  1138. (JavascriptFunction*)function,
  1139. args.Info,
  1140. args.Values[0],
  1141. args.Values[1],
  1142. args.Values[2],
  1143. args.Values[3],
  1144. args.Values[4],
  1145. args.Values[5],
  1146. args.Values[6],
  1147. args.Values[7]);
  1148. break;
  1149. }
  1150. case 9: {
  1151. varResult=entryPoint(
  1152. (JavascriptFunction*)function,
  1153. args.Info,
  1154. args.Values[0],
  1155. args.Values[1],
  1156. args.Values[2],
  1157. args.Values[3],
  1158. args.Values[4],
  1159. args.Values[5],
  1160. args.Values[6],
  1161. args.Values[7],
  1162. args.Values[8]);
  1163. break;
  1164. }
  1165. default:
  1166. ScriptContext* scriptContext = function->type->GetScriptContext();
  1167. varResult = scriptContext->GetLibrary()->GetUndefined();
  1168. AssertMsg(false, "CallFunction call with unsupported number of arguments");
  1169. break;
  1170. }
  1171. #endif
  1172. return varResult;
  1173. }
  1174. #endif
  1175. Var JavascriptFunction::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
  1176. {
  1177. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1178. ARGUMENTS(args, callInfo);
  1179. ScriptContext* scriptContext = function->GetScriptContext();
  1180. Assert(!(callInfo.Flags & CallFlags_New));
  1181. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  1182. if (args.Info.Count == 0 || !JavascriptFunction::Is(args[0]))
  1183. {
  1184. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedFunction, L"Function.prototype.toString");
  1185. }
  1186. JavascriptFunction *pFunc = JavascriptFunction::FromVar(args[0]);
  1187. // pFunc can be from a different script context if Function.prototype.toString is invoked via .call/.apply.
  1188. // Marshal the resulting string to the current script context (that of the toString)
  1189. return CrossSite::MarshalVar(scriptContext, pFunc->EnsureSourceString());
  1190. }
  1191. JavascriptString* JavascriptFunction::GetNativeFunctionDisplayString(ScriptContext *scriptContext, JavascriptString *name)
  1192. {
  1193. return GetNativeFunctionDisplayStringCommon<JavascriptString>(scriptContext, name);
  1194. }
  1195. JavascriptString* JavascriptFunction::GetLibraryCodeDisplayString(ScriptContext *scriptContext, PCWSTR displayName)
  1196. {
  1197. return GetLibraryCodeDisplayStringCommon<JavascriptString, JavascriptString*>(scriptContext, displayName);
  1198. }
  1199. #ifdef _M_IX86
  1200. // This code is enabled by the -checkAlignment switch.
  1201. // It verifies that all of our JS frames are 8 byte aligned.
  1202. // Our alignments is based on aligning the return address of the function.
  1203. // Note that this test can fail when Javascript functions are called directly
  1204. // from helper functions. This could be fixed by making these calls through
  1205. // CallFunction(), or by having the helper 8 byte align the frame itself before
  1206. // the call. A lot of these though are not dealing with floats, so the cost
  1207. // of doing the 8 byte alignment would outweigh the benefit...
  1208. __declspec (naked)
  1209. void JavascriptFunction::CheckAlignment()
  1210. {
  1211. _asm
  1212. {
  1213. test esp, 0x4
  1214. je LABEL1
  1215. ret
  1216. LABEL1:
  1217. call Throw::InternalError
  1218. }
  1219. }
  1220. #else
  1221. void JavascriptFunction::CheckAlignment()
  1222. {
  1223. // Note: in order to enable this on ARM, uncomment/fix code in LowerMD.cpp (LowerEntryInstr).
  1224. }
  1225. #endif
  1226. BOOL JavascriptFunction::IsNativeAddress(ScriptContext * scriptContext, void * codeAddr)
  1227. {
  1228. #if ENABLE_NATIVE_CODEGEN
  1229. return scriptContext->IsNativeAddress(codeAddr);
  1230. #else
  1231. return false;
  1232. #endif
  1233. }
  1234. Js::JavascriptMethod JavascriptFunction::DeferredParse(ScriptFunction** functionRef)
  1235. {
  1236. BOOL fParsed;
  1237. return Js::ScriptFunction::DeferredParseCore(functionRef, fParsed);
  1238. }
  1239. Js::JavascriptMethod JavascriptFunction::DeferredParseCore(ScriptFunction** functionRef, BOOL &fParsed)
  1240. {
  1241. // Do the actual deferred parsing and byte code generation, passing the new entry point to the caller.
  1242. ParseableFunctionInfo* functionInfo = (*functionRef)->GetParseableFunctionInfo();
  1243. FunctionBody* funcBody = nullptr;
  1244. Assert(functionInfo);
  1245. if (functionInfo->IsDeferredParseFunction())
  1246. {
  1247. funcBody = functionInfo->Parse(functionRef);
  1248. fParsed = funcBody->IsFunctionParsed() ? TRUE : FALSE;
  1249. #if ENABLE_PROFILE_INFO
  1250. // This is the first call to the function, ensure dynamic profile info
  1251. funcBody->EnsureDynamicProfileInfo();
  1252. #endif
  1253. }
  1254. else
  1255. {
  1256. funcBody = functionInfo->GetFunctionBody();
  1257. Assert(funcBody != nullptr);
  1258. Assert(!funcBody->IsDeferredParseFunction());
  1259. }
  1260. DebugOnly(JavascriptMethod directEntryPoint = funcBody->GetDirectEntryPoint(funcBody->GetDefaultEntryPointInfo()));
  1261. Assert(directEntryPoint != DefaultDeferredParsingThunk && directEntryPoint != ProfileDeferredParsingThunk);
  1262. return (*functionRef)->UpdateUndeferredBody(funcBody);
  1263. }
  1264. void JavascriptFunction::ReparseAsmJsModule(ScriptFunction** functionRef)
  1265. {
  1266. ParseableFunctionInfo* functionInfo = (*functionRef)->GetParseableFunctionInfo();
  1267. Assert(functionInfo);
  1268. Assert(functionInfo->HasBody());
  1269. functionInfo->GetFunctionBody()->AddDeferParseAttribute();
  1270. functionInfo->GetFunctionBody()->ResetEntryPoint();
  1271. functionInfo->GetFunctionBody()->ResetInParams();
  1272. FunctionBody * funcBody = functionInfo->Parse(functionRef);
  1273. #if ENABLE_PROFILE_INFO
  1274. // This is the first call to the function, ensure dynamic profile info
  1275. funcBody->EnsureDynamicProfileInfo();
  1276. #endif
  1277. (*functionRef)->UpdateUndeferredBody(funcBody);
  1278. }
  1279. // Thunk for handling calls to functions that have not had byte code generated for them.
  1280. #if _M_IX86
  1281. __declspec(naked)
  1282. Var JavascriptFunction::DeferredParsingThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1283. {
  1284. __asm
  1285. {
  1286. push ebp
  1287. mov ebp, esp
  1288. lea eax, [esp+8] // load the address of the function os that if we need to box, we can patch it up
  1289. push eax
  1290. call JavascriptFunction::DeferredParse
  1291. #ifdef _CONTROL_FLOW_GUARD
  1292. // verify that the call target is valid
  1293. mov ecx, eax
  1294. call[__guard_check_icall_fptr]
  1295. mov eax, ecx
  1296. #endif
  1297. pop ebp
  1298. jmp eax
  1299. }
  1300. }
  1301. #elif defined(_M_X64) || defined(_M_ARM32_OR_ARM64)
  1302. //Do nothing: the implementation of JavascriptFunction::DeferredParsingThunk is declared (appropriately decorated) in
  1303. // Library\amd64\javascriptfunctiona.asm
  1304. // Library\arm\arm_DeferredParsingThunk.asm
  1305. // Library\arm64\arm64_DeferredParsingThunk.asm
  1306. #else
  1307. Var JavascriptFunction::DeferredParsingThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1308. {
  1309. Js::Throw::NotImplemented();
  1310. return nullptr;
  1311. }
  1312. #endif
  1313. ConstructorCache* JavascriptFunction::EnsureValidConstructorCache()
  1314. {
  1315. Assert(this->constructorCache != nullptr);
  1316. this->constructorCache = ConstructorCache::EnsureValidInstance(this->constructorCache, this->GetScriptContext());
  1317. return this->constructorCache;
  1318. }
  1319. void JavascriptFunction::ResetConstructorCacheToDefault()
  1320. {
  1321. Assert(this->constructorCache != nullptr);
  1322. if (!this->constructorCache->IsDefault())
  1323. {
  1324. this->constructorCache = &ConstructorCache::DefaultInstance;
  1325. }
  1326. }
  1327. // Thunk for handling calls to functions that have not had byte code generated for them.
  1328. #if _M_IX86
  1329. __declspec(naked)
  1330. Var JavascriptFunction::DeferredDeserializeThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1331. {
  1332. __asm
  1333. {
  1334. push ebp
  1335. mov ebp, esp
  1336. push [esp+8]
  1337. call JavascriptFunction::DeferredDeserialize
  1338. #ifdef _CONTROL_FLOW_GUARD
  1339. // verify that the call target is valid
  1340. mov ecx, eax
  1341. call[__guard_check_icall_fptr]
  1342. mov eax, ecx
  1343. #endif
  1344. pop ebp
  1345. jmp eax
  1346. }
  1347. }
  1348. #elif defined(_M_X64) || defined(_M_ARM32_OR_ARM64)
  1349. //Do nothing: the implementation of JavascriptFunction::DeferredParsingThunk is declared (appropriately decorated) in
  1350. // Library\amd64\javascriptfunctiona.asm
  1351. // Library\arm\arm_DeferredParsingThunk.asm
  1352. // Library\arm64\arm64_DeferredParsingThunk.asm
  1353. #else
  1354. Var JavascriptFunction::DeferredDeserializeThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1355. {
  1356. Js::Throw::NotImplemented();
  1357. return nullptr;
  1358. }
  1359. #endif
  1360. Js::JavascriptMethod JavascriptFunction::DeferredDeserialize(ScriptFunction* function)
  1361. {
  1362. FunctionInfo* funcInfo = function->GetFunctionInfo();
  1363. Assert(funcInfo);
  1364. FunctionBody* funcBody = nullptr;
  1365. // If we haven't already deserialized this function, do so now
  1366. // FunctionProxies could have gotten deserialized during the interpreter when
  1367. // we tried to record the callsite info for the function which meant that it was a
  1368. // target of a call. Or we could have deserialized the function info in another JavascriptFunctionInstance
  1369. // In any case, fix up the function info if it's already been deserialized so that
  1370. // we don't hold on to the proxy for too long, and rethunk it so that it directly
  1371. // calls the default entry point the next time around
  1372. if (funcInfo->IsDeferredDeserializeFunction())
  1373. {
  1374. DeferDeserializeFunctionInfo* deferDeserializeFunction = (DeferDeserializeFunctionInfo*) funcInfo;
  1375. // This is the first call to the function, ensure dynamic profile info
  1376. // Deserialize is a no-op if the function has already been deserialized
  1377. funcBody = deferDeserializeFunction->Deserialize();
  1378. #if ENABLE_PROFILE_INFO
  1379. funcBody->EnsureDynamicProfileInfo();
  1380. #endif
  1381. }
  1382. else
  1383. {
  1384. funcBody = funcInfo->GetFunctionBody();
  1385. Assert(funcBody != nullptr);
  1386. Assert(!funcBody->IsDeferredDeserializeFunction());
  1387. }
  1388. return function->UpdateUndeferredBody(funcBody);
  1389. }
  1390. void JavascriptFunction::SetEntryPoint(JavascriptMethod method)
  1391. {
  1392. this->GetDynamicType()->SetEntryPoint(method);
  1393. }
  1394. Var JavascriptFunction::EnsureSourceString()
  1395. {
  1396. return this->GetLibrary()->GetFunctionDisplayString();
  1397. }
  1398. /*
  1399. *****************************************************************************************************************
  1400. Conditions checked by instruction decoder (In sequential order)
  1401. ******************************************************************************************************************
  1402. 1) Exception Code is AV i.e STATUS_ACCESS_VIOLATION
  1403. 2) Check if Rip is Native address
  1404. 3) Get the function object from RBP (a fixed offset from RBP) and check for the following
  1405. a. Not Null
  1406. b. Ensure that the function object is heap allocated
  1407. c. Ensure that the entrypointInfo is heap allocated
  1408. d. Ensure that the functionbody is heap allocated
  1409. e. Is a function
  1410. f. Is AsmJs Function object for asmjs
  1411. 4) Check if Array BufferLength > 0x10000 (64K), power of 2 if length is less than 2^24 or multiple of 2^24 and multiple of 0x1000(4K) for asmjs
  1412. 5) Check If the instruction is valid
  1413. a. Is one of the move instructions , i.e. mov, movsx, movzx, movsxd, movss or movsd
  1414. b. Get the array buffer register and its value for asmjs
  1415. c. Get the dst register(in case of load)
  1416. d. Calculate the number of bytes read in order to get the length of the instruction , ensure that the length should never be greater than 15 bytes
  1417. 6) Check that the Array buffer value is same as the one we passed in EntryPointInfo in asmjs
  1418. 7) Set the dst reg if the instr type is load
  1419. 8) Add the bytes read to Rip and set it as new Rip
  1420. 9) Return EXCEPTION_CONTINUE_EXECUTION
  1421. */
  1422. #ifdef _M_X64
  1423. ArrayAccessDecoder::InstructionData ArrayAccessDecoder::CheckValidInstr(BYTE* &pc, PEXCEPTION_POINTERS exceptionInfo) // get the reg operand and isLoad and
  1424. {
  1425. InstructionData instrData;
  1426. uint prefixValue = 0;
  1427. ArrayAccessDecoder::RexByteValue rexByteValue;
  1428. bool isFloat = false;
  1429. uint immBytes = 0;
  1430. uint dispBytes = 0;
  1431. bool isImmediate = false;
  1432. bool isSIB = false;
  1433. // Read first byte - check for prefix
  1434. BYTE* beginPc = pc;
  1435. if (((*pc) == 0x0F2) || ((*pc) == 0x0F3))
  1436. {
  1437. //MOVSD or MOVSS
  1438. prefixValue = *pc;
  1439. isFloat = true;
  1440. pc++;
  1441. }
  1442. else if (*pc == 0x66)
  1443. {
  1444. prefixValue = *pc;
  1445. pc++;
  1446. }
  1447. // Check for Rex Byte - After prefix we should have a rexByte if there is one
  1448. if (*pc >= 0x40 && *pc <= 0x4F)
  1449. {
  1450. rexByteValue.rexValue = *pc;
  1451. uint rexByte = *pc - 0x40;
  1452. if (rexByte & 0x8)
  1453. {
  1454. rexByteValue.isW = true;
  1455. }
  1456. if (rexByte & 0x4)
  1457. {
  1458. rexByteValue.isR = true;
  1459. }
  1460. if (rexByte & 0x2)
  1461. {
  1462. rexByteValue.isX = true;
  1463. }
  1464. if (rexByte & 0x1)
  1465. {
  1466. rexByteValue.isB = true;
  1467. }
  1468. pc++;
  1469. }
  1470. // read opcode
  1471. // Is one of the move instructions , i.e. mov, movsx, movzx, movsxd, movss or movsd
  1472. switch (*pc)
  1473. {
  1474. //MOV - Store
  1475. case 0x89:
  1476. case 0x88:
  1477. {
  1478. pc++;
  1479. instrData.isLoad = false;
  1480. break;
  1481. }
  1482. //MOV - Load
  1483. case 0x8A:
  1484. case 0x8B:
  1485. {
  1486. pc++;
  1487. instrData.isLoad = true;
  1488. break;
  1489. }
  1490. case 0x0F:
  1491. {
  1492. // more than one byte opcode and hence we will read pc multiple times
  1493. pc++;
  1494. //MOVSX , MOVSXD
  1495. if (*pc == 0xBE || *pc == 0xBF)
  1496. {
  1497. instrData.isLoad = true;
  1498. }
  1499. //MOVZX
  1500. else if (*pc == 0xB6 || *pc == 0xB7)
  1501. {
  1502. instrData.isLoad = true;
  1503. }
  1504. //MOVSS - Load
  1505. else if (*pc == 0x10 && prefixValue == 0xF3)
  1506. {
  1507. Assert(isFloat);
  1508. instrData.isLoad = true;
  1509. instrData.isFloat32 = true;
  1510. }
  1511. //MOVSS - Store
  1512. else if (*pc == 0x11 && prefixValue == 0xF3)
  1513. {
  1514. Assert(isFloat);
  1515. instrData.isLoad = false;
  1516. instrData.isFloat32 = true;
  1517. }
  1518. //MOVSD - Load
  1519. else if (*pc == 0x10 && prefixValue == 0xF2)
  1520. {
  1521. Assert(isFloat);
  1522. instrData.isLoad = true;
  1523. instrData.isFloat64 = true;
  1524. }
  1525. //MOVSD - Store
  1526. else if (*pc == 0x11 && prefixValue == 0xF2)
  1527. {
  1528. Assert(isFloat);
  1529. instrData.isLoad = false;
  1530. instrData.isFloat64 = true;
  1531. }
  1532. //MOVUPS - Load
  1533. else if (*pc == 0x10 && prefixValue == 0)
  1534. {
  1535. instrData.isLoad = true;
  1536. instrData.isSimd = true;
  1537. }
  1538. //MOVUPS - Store
  1539. else if (*pc == 0x11 && prefixValue == 0)
  1540. {
  1541. instrData.isLoad = false;
  1542. instrData.isSimd = true;
  1543. }
  1544. else
  1545. {
  1546. instrData.isInvalidInstr = true;
  1547. }
  1548. pc++;
  1549. break;
  1550. }
  1551. // Support Mov Immediates
  1552. // MOV
  1553. case 0xC6:
  1554. case 0xC7:
  1555. {
  1556. instrData.isLoad = false;
  1557. instrData.isFloat64 = false;
  1558. isImmediate = true;
  1559. if (*pc == 0xC6)
  1560. {
  1561. immBytes = 1;
  1562. }
  1563. else if (rexByteValue.isW) // For MOV, REX.W set means we have a 32 bit immediate value, which gets extended to 64 bit.
  1564. {
  1565. immBytes = 4;
  1566. }
  1567. else
  1568. {
  1569. if (prefixValue == 0x66)
  1570. {
  1571. immBytes = 2;
  1572. }
  1573. else
  1574. {
  1575. immBytes = 4;
  1576. }
  1577. }
  1578. pc++;
  1579. break;
  1580. }
  1581. default:
  1582. instrData.isInvalidInstr = true;
  1583. break;
  1584. }
  1585. // if the opcode is not a move return
  1586. if (instrData.isInvalidInstr)
  1587. {
  1588. return instrData;
  1589. }
  1590. //Read ModR/M
  1591. // Read the Src Reg and also check for SIB
  1592. // Add the isR bit to SrcReg and get the actual SRCReg
  1593. // Get the number of bytes for displacement
  1594. //get mod bits
  1595. BYTE modVal = *pc & 0xC0; // first two bits(7th and 6th bits)
  1596. modVal >>= 6;
  1597. //get the R/M bits
  1598. BYTE rmVal = (*pc) & 0x07; // last 3 bits ( 0,1 and 2nd bits)
  1599. //get the reg value
  1600. BYTE dstReg = (*pc) & 0x38; // mask reg bits (3rd 4th and 5th bits)
  1601. dstReg >>= 3;
  1602. Assert(dstReg <= 0x07);
  1603. Assert(modVal <= 0x03);
  1604. Assert(rmVal <= 0x07);
  1605. switch (modVal)
  1606. {
  1607. case 0x00:
  1608. dispBytes = 0;
  1609. break;
  1610. case 0x01:
  1611. dispBytes = 1;
  1612. break;
  1613. case 0x02:
  1614. dispBytes = 4;
  1615. break;
  1616. default:
  1617. instrData.isInvalidInstr = true;
  1618. break;
  1619. }
  1620. if (instrData.isInvalidInstr)
  1621. {
  1622. return instrData;
  1623. }
  1624. // Get the R/M value and see if SIB is present , else get the buffer reg
  1625. if (rmVal == 0x04)
  1626. {
  1627. isSIB = true;
  1628. }
  1629. else
  1630. {
  1631. instrData.bufferReg = rmVal;
  1632. }
  1633. // Get the RegByes from ModRM
  1634. instrData.dstReg = dstReg;
  1635. // increment the modrm byte
  1636. pc++;
  1637. // Check if we have SIB and in that case bufferReg should not be set
  1638. if (isSIB)
  1639. {
  1640. Assert(!instrData.bufferReg);
  1641. // Get the Base and Index Reg from SIB and ensure that Scale is zero
  1642. // We don't care about the Index reg
  1643. // Add the isB value from Rex and get the actual Base Reg
  1644. // Get the base register
  1645. // 6f. Get the array buffer register and its value
  1646. instrData.bufferReg = (*pc % 8);
  1647. pc++;
  1648. }
  1649. // check for the Rex.B value and append it to the base register
  1650. if (rexByteValue.isB)
  1651. {
  1652. instrData.bufferReg |= 1 << 3;
  1653. }
  1654. // check for the Rex.R value and append it to the dst register
  1655. if (rexByteValue.isR)
  1656. {
  1657. instrData.dstReg |= 1 << 3;
  1658. }
  1659. // Get the buffer address - this is always 64 bit GPR
  1660. switch (instrData.bufferReg)
  1661. {
  1662. case 0x0:
  1663. instrData.bufferValue = exceptionInfo->ContextRecord->Rax;
  1664. break;
  1665. case 0x1:
  1666. instrData.bufferValue = exceptionInfo->ContextRecord->Rcx;
  1667. break;
  1668. case 0x2:
  1669. instrData.bufferValue = exceptionInfo->ContextRecord->Rdx;
  1670. break;
  1671. case 0x3:
  1672. instrData.bufferValue = exceptionInfo->ContextRecord->Rbx;
  1673. break;
  1674. case 0x4:
  1675. instrData.bufferValue = exceptionInfo->ContextRecord->Rsp;
  1676. break;
  1677. case 0x5:
  1678. // RBP wouldn't point to an array buffer
  1679. instrData.bufferValue = NULL;
  1680. break;
  1681. case 0x6:
  1682. instrData.bufferValue = exceptionInfo->ContextRecord->Rsi;
  1683. break;
  1684. case 0x7:
  1685. instrData.bufferValue = exceptionInfo->ContextRecord->Rdi;
  1686. break;
  1687. case 0x8:
  1688. instrData.bufferValue = exceptionInfo->ContextRecord->R8;
  1689. break;
  1690. case 0x9:
  1691. instrData.bufferValue = exceptionInfo->ContextRecord->R9;
  1692. break;
  1693. case 0xA:
  1694. instrData.bufferValue = exceptionInfo->ContextRecord->R10;
  1695. break;
  1696. case 0xB:
  1697. instrData.bufferValue = exceptionInfo->ContextRecord->R11;
  1698. break;
  1699. case 0xC:
  1700. instrData.bufferValue = exceptionInfo->ContextRecord->R12;
  1701. break;
  1702. case 0xD:
  1703. instrData.bufferValue = exceptionInfo->ContextRecord->R13;
  1704. break;
  1705. case 0xE:
  1706. instrData.bufferValue = exceptionInfo->ContextRecord->R14;
  1707. break;
  1708. case 0xF:
  1709. instrData.bufferValue = exceptionInfo->ContextRecord->R15;
  1710. break;
  1711. default:
  1712. instrData.isInvalidInstr = true;
  1713. Assert(false);// should never reach here as validation is done before itself
  1714. return instrData;
  1715. }
  1716. // add the pc for displacement , we don't need the displacement Byte value
  1717. if (dispBytes > 0)
  1718. {
  1719. pc = pc + dispBytes;
  1720. }
  1721. instrData.instrSizeInByte = (uint)(pc - beginPc);
  1722. if (isImmediate)
  1723. {
  1724. Assert(immBytes > 0);
  1725. instrData.instrSizeInByte += immBytes;
  1726. }
  1727. // Calculate the number of bytes read in order to get the length of the instruction , ensure that the length should never be greater than 15 bytes
  1728. if (instrData.instrSizeInByte > 15)
  1729. {
  1730. // no instr size can be greater than 15
  1731. instrData.isInvalidInstr = true;
  1732. }
  1733. return instrData;
  1734. }
  1735. int JavascriptFunction::ResumeForOutOfBoundsArrayRefs(int exceptionCode, PEXCEPTION_POINTERS exceptionInfo)
  1736. {
  1737. #if ENABLE_NATIVE_CODEGEN
  1738. if (exceptionCode != STATUS_ACCESS_VIOLATION)
  1739. {
  1740. return EXCEPTION_CONTINUE_SEARCH;
  1741. }
  1742. ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
  1743. // AV should come from JITed code, since we don't eliminate bound checks in interpreter
  1744. if (!threadContext->IsNativeAddress((Var)exceptionInfo->ContextRecord->Rip))
  1745. {
  1746. return EXCEPTION_CONTINUE_SEARCH;
  1747. }
  1748. Var* addressOfFuncObj = (Var*)(exceptionInfo->ContextRecord->Rbp + 2 * sizeof(Var));
  1749. if (!addressOfFuncObj)
  1750. {
  1751. return EXCEPTION_CONTINUE_SEARCH;
  1752. }
  1753. Js::ScriptFunction* func = (ScriptFunction::Is(*addressOfFuncObj))?(Js::ScriptFunction*)(*addressOfFuncObj):nullptr;
  1754. if (!func)
  1755. {
  1756. return EXCEPTION_CONTINUE_SEARCH;
  1757. }
  1758. RecyclerHeapObjectInfo heapObject;
  1759. Recycler* recycler = threadContext->GetRecycler();
  1760. bool isFuncObjHeapAllocated = recycler->FindHeapObject(func, FindHeapObjectFlags_NoFlags, heapObject); // recheck if this needs to be removed
  1761. bool isEntryPointHeapAllocated = recycler->FindHeapObject(func->GetEntryPointInfo(), FindHeapObjectFlags_NoFlags, heapObject);
  1762. bool isFunctionBodyHeapAllocated = recycler->FindHeapObject(func->GetFunctionBody(), FindHeapObjectFlags_NoFlags, heapObject);
  1763. // ensure that all our objects are heap allocated
  1764. if (!(isFuncObjHeapAllocated && isEntryPointHeapAllocated && isFunctionBodyHeapAllocated))
  1765. {
  1766. return EXCEPTION_CONTINUE_SEARCH;
  1767. }
  1768. bool isAsmJs = func->GetFunctionBody()->GetIsAsmJsFunction();
  1769. Js::FunctionBody* funcBody = func->GetFunctionBody();
  1770. BYTE* buffer = nullptr;
  1771. if (isAsmJs)
  1772. {
  1773. // some extra checks for asm.js because we have slightly more information that we can validate
  1774. Js::EntryPointInfo* entryPointInfo = (Js::EntryPointInfo*)funcBody->GetDefaultEntryPointInfo();
  1775. uintptr_t moduleMemory = entryPointInfo->GetModuleAddress();
  1776. if (!moduleMemory)
  1777. {
  1778. return EXCEPTION_CONTINUE_SEARCH;
  1779. }
  1780. ArrayBuffer* arrayBuffer = *(ArrayBuffer**)(moduleMemory + AsmJsModuleMemory::MemoryTableBeginOffset);
  1781. if (!arrayBuffer || !arrayBuffer->GetBuffer())
  1782. {
  1783. // don't have a heap buffer for asm.js... so this shouldn't be an asm.js heap access
  1784. return EXCEPTION_CONTINUE_SEARCH;
  1785. }
  1786. buffer = arrayBuffer->GetBuffer();
  1787. uint bufferLength = arrayBuffer->GetByteLength();
  1788. if (!arrayBuffer->IsValidAsmJsBufferLength(bufferLength))
  1789. {
  1790. return EXCEPTION_CONTINUE_SEARCH;
  1791. }
  1792. }
  1793. BYTE* pc = (BYTE*)exceptionInfo->ExceptionRecord->ExceptionAddress;
  1794. ArrayAccessDecoder::InstructionData instrData = ArrayAccessDecoder::CheckValidInstr(pc, exceptionInfo);
  1795. // Check If the instruction is valid
  1796. if (instrData.isInvalidInstr)
  1797. {
  1798. return EXCEPTION_CONTINUE_SEARCH;
  1799. }
  1800. // If we didn't find the array buffer, ignore
  1801. if (!instrData.bufferValue)
  1802. {
  1803. return EXCEPTION_CONTINUE_SEARCH;
  1804. }
  1805. // If asm.js, make sure the base address is that of the heap buffer
  1806. if (isAsmJs && (instrData.bufferValue != (uint64)buffer))
  1807. {
  1808. return EXCEPTION_CONTINUE_SEARCH;
  1809. }
  1810. // SIMD loads/stores do bounds checks.
  1811. if (instrData.isSimd)
  1812. {
  1813. return EXCEPTION_CONTINUE_SEARCH;
  1814. }
  1815. // Set the dst reg if the instr type is load
  1816. if (instrData.isLoad)
  1817. {
  1818. Var exceptionInfoReg = exceptionInfo->ContextRecord;
  1819. Var* exceptionInfoIntReg = (Var*)((uint64)exceptionInfoReg + offsetof(CONTEXT, CONTEXT::Rax)); // offset in the contextRecord for RAX , the assert below checks for any change in the exceptionInfo struct
  1820. Var* exceptionInfoFloatReg = (Var*)((uint64)exceptionInfoReg + offsetof(CONTEXT, CONTEXT::Xmm0));// offset in the contextRecord for XMM0 , the assert below checks for any change in the exceptionInfo struct
  1821. Assert((DWORD64)*exceptionInfoIntReg == exceptionInfo->ContextRecord->Rax);
  1822. Assert((uint64)*exceptionInfoFloatReg == exceptionInfo->ContextRecord->Xmm0.Low);
  1823. if (instrData.isLoad)
  1824. {
  1825. double nanVal = JavascriptNumber::NaN;
  1826. if (instrData.isFloat64)
  1827. {
  1828. double* destRegLocation = (double*)((uint64)exceptionInfoFloatReg + 16 * (instrData.dstReg));
  1829. *destRegLocation = nanVal;
  1830. }
  1831. else if (instrData.isFloat32)
  1832. {
  1833. float* destRegLocation = (float*)((uint64)exceptionInfoFloatReg + 16 * (instrData.dstReg));
  1834. *destRegLocation = (float)nanVal;
  1835. }
  1836. else
  1837. {
  1838. uint64* destRegLocation = (uint64*)((uint64)exceptionInfoIntReg + 8 * (instrData.dstReg));
  1839. *destRegLocation = 0;
  1840. }
  1841. }
  1842. }
  1843. // Add the bytes read to Rip and set it as new Rip
  1844. exceptionInfo->ContextRecord->Rip = exceptionInfo->ContextRecord->Rip + instrData.instrSizeInByte;
  1845. return EXCEPTION_CONTINUE_EXECUTION;
  1846. #else
  1847. return EXCEPTION_CONTINUE_SEARCH;
  1848. #endif
  1849. }
  1850. #endif
  1851. #if DBG
  1852. void JavascriptFunction::VerifyEntryPoint()
  1853. {
  1854. JavascriptMethod callEntryPoint = this->GetType()->GetEntryPoint();
  1855. if (this->IsCrossSiteObject())
  1856. {
  1857. Assert(CrossSite::IsThunk(callEntryPoint));
  1858. }
  1859. else if (ScriptFunction::Is(this))
  1860. {
  1861. }
  1862. else
  1863. {
  1864. JavascriptMethod originalEntryPoint = this->GetFunctionInfo()->GetOriginalEntryPoint();
  1865. Assert(callEntryPoint == originalEntryPoint || callEntryPoint == ProfileEntryThunk
  1866. || (this->GetScriptContext()->GetHostScriptContext()
  1867. && this->GetScriptContext()->GetHostScriptContext()->IsHostCrossSiteThunk(callEntryPoint))
  1868. );
  1869. }
  1870. }
  1871. #endif
  1872. /*static*/
  1873. PropertyId const JavascriptFunction::specialPropertyIds[] =
  1874. {
  1875. PropertyIds::caller,
  1876. PropertyIds::arguments
  1877. };
  1878. bool JavascriptFunction::HasRestrictedProperties() const
  1879. {
  1880. return !(
  1881. this->functionInfo->IsClassMethod() ||
  1882. this->functionInfo->IsClassConstructor() ||
  1883. this->functionInfo->IsLambda() ||
  1884. this->functionInfo->IsAsync() ||
  1885. this->IsGeneratorFunction() ||
  1886. this->IsBoundFunction() ||
  1887. this->IsStrictMode()
  1888. );
  1889. }
  1890. BOOL JavascriptFunction::HasProperty(PropertyId propertyId)
  1891. {
  1892. switch (propertyId)
  1893. {
  1894. case PropertyIds::caller:
  1895. case PropertyIds::arguments:
  1896. if (this->HasRestrictedProperties())
  1897. {
  1898. return true;
  1899. }
  1900. break;
  1901. case PropertyIds::length:
  1902. if (this->IsScriptFunction())
  1903. {
  1904. return true;
  1905. }
  1906. break;
  1907. }
  1908. return DynamicObject::HasProperty(propertyId);
  1909. }
  1910. BOOL JavascriptFunction::GetAccessors(PropertyId propertyId, Var *getter, Var *setter, ScriptContext * requestContext)
  1911. {
  1912. Assert(!this->IsBoundFunction());
  1913. Assert(propertyId != Constants::NoProperty);
  1914. Assert(getter);
  1915. Assert(setter);
  1916. Assert(requestContext);
  1917. if (this->HasRestrictedProperties())
  1918. {
  1919. switch (propertyId)
  1920. {
  1921. case PropertyIds::caller:
  1922. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  1923. {
  1924. *setter = *getter = requestContext->GetLibrary()->GetThrowTypeErrorCallerAccessorFunction();
  1925. return true;
  1926. }
  1927. break;
  1928. case PropertyIds::arguments:
  1929. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  1930. {
  1931. *setter = *getter = requestContext->GetLibrary()->GetThrowTypeErrorArgumentsAccessorFunction();
  1932. return true;
  1933. }
  1934. break;
  1935. }
  1936. }
  1937. return __super::GetAccessors(propertyId, getter, setter, requestContext);
  1938. }
  1939. DescriptorFlags JavascriptFunction::GetSetter(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  1940. {
  1941. DescriptorFlags flags;
  1942. if (GetSetterBuiltIns(propertyId, setterValue, info, requestContext, &flags))
  1943. {
  1944. return flags;
  1945. }
  1946. return __super::GetSetter(propertyId, setterValue, info, requestContext);
  1947. }
  1948. DescriptorFlags JavascriptFunction::GetSetter(JavascriptString* propertyNameString, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  1949. {
  1950. DescriptorFlags flags;
  1951. PropertyRecord const* propertyRecord;
  1952. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  1953. if (propertyRecord != nullptr && GetSetterBuiltIns(propertyRecord->GetPropertyId(), setterValue, info, requestContext, &flags))
  1954. {
  1955. return flags;
  1956. }
  1957. return __super::GetSetter(propertyNameString, setterValue, info, requestContext);
  1958. }
  1959. bool JavascriptFunction::GetSetterBuiltIns(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext, DescriptorFlags* descriptorFlags)
  1960. {
  1961. Assert(propertyId != Constants::NoProperty);
  1962. Assert(setterValue);
  1963. Assert(requestContext);
  1964. switch (propertyId)
  1965. {
  1966. case PropertyIds::caller:
  1967. if (this->HasRestrictedProperties()) {
  1968. PropertyValueInfo::SetNoCache(info, this);
  1969. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  1970. {
  1971. *setterValue = requestContext->GetLibrary()->GetThrowTypeErrorCallerAccessorFunction();
  1972. *descriptorFlags = Accessor;
  1973. }
  1974. else
  1975. {
  1976. *descriptorFlags = Data;
  1977. }
  1978. return true;
  1979. }
  1980. break;
  1981. case PropertyIds::arguments:
  1982. if (this->HasRestrictedProperties()) {
  1983. PropertyValueInfo::SetNoCache(info, this);
  1984. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  1985. {
  1986. *setterValue = requestContext->GetLibrary()->GetThrowTypeErrorArgumentsAccessorFunction();
  1987. *descriptorFlags = Accessor;
  1988. }
  1989. else
  1990. {
  1991. *descriptorFlags = Data;
  1992. }
  1993. return true;
  1994. }
  1995. break;
  1996. }
  1997. return false;
  1998. }
  1999. BOOL JavascriptFunction::IsConfigurable(PropertyId propertyId)
  2000. {
  2001. if (DynamicObject::GetPropertyIndex(propertyId) == Constants::NoSlot)
  2002. {
  2003. switch (propertyId)
  2004. {
  2005. case PropertyIds::caller:
  2006. case PropertyIds::arguments:
  2007. if (this->HasRestrictedProperties())
  2008. {
  2009. return false;
  2010. }
  2011. break;
  2012. case PropertyIds::length:
  2013. if (this->IsScriptFunction() || this->IsBoundFunction())
  2014. {
  2015. return true;
  2016. }
  2017. break;
  2018. }
  2019. }
  2020. return DynamicObject::IsConfigurable(propertyId);
  2021. }
  2022. BOOL JavascriptFunction::IsEnumerable(PropertyId propertyId)
  2023. {
  2024. if (DynamicObject::GetPropertyIndex(propertyId) == Constants::NoSlot)
  2025. {
  2026. switch (propertyId)
  2027. {
  2028. case PropertyIds::caller:
  2029. case PropertyIds::arguments:
  2030. if (this->HasRestrictedProperties())
  2031. {
  2032. return false;
  2033. }
  2034. break;
  2035. case PropertyIds::length:
  2036. if (this->IsScriptFunction())
  2037. {
  2038. return false;
  2039. }
  2040. break;
  2041. }
  2042. }
  2043. return DynamicObject::IsEnumerable(propertyId);
  2044. }
  2045. BOOL JavascriptFunction::IsWritable(PropertyId propertyId)
  2046. {
  2047. if (DynamicObject::GetPropertyIndex(propertyId) == Constants::NoSlot)
  2048. {
  2049. switch (propertyId)
  2050. {
  2051. case PropertyIds::caller:
  2052. case PropertyIds::arguments:
  2053. if (this->HasRestrictedProperties())
  2054. {
  2055. return false;
  2056. }
  2057. break;
  2058. case PropertyIds::length:
  2059. if (this->IsScriptFunction())
  2060. {
  2061. return false;
  2062. }
  2063. break;
  2064. }
  2065. }
  2066. return DynamicObject::IsWritable(propertyId);
  2067. }
  2068. BOOL JavascriptFunction::GetSpecialPropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext)
  2069. {
  2070. uint length = GetSpecialPropertyCount();
  2071. if (index < length)
  2072. {
  2073. Assert(DynamicObject::GetPropertyIndex(specialPropertyIds[index]) == Constants::NoSlot);
  2074. *propertyName = requestContext->GetPropertyString(specialPropertyIds[index]);
  2075. return true;
  2076. }
  2077. if (index == length)
  2078. {
  2079. if (this->IsScriptFunction() || this->IsBoundFunction())
  2080. {
  2081. if (DynamicObject::GetPropertyIndex(PropertyIds::length) == Constants::NoSlot)
  2082. {
  2083. //Only for user defined functions length is a special property.
  2084. *propertyName = requestContext->GetPropertyString(PropertyIds::length);
  2085. return true;
  2086. }
  2087. }
  2088. }
  2089. return false;
  2090. }
  2091. // Returns the number of special non-enumerable properties this type has.
  2092. uint JavascriptFunction::GetSpecialPropertyCount() const
  2093. {
  2094. return this->HasRestrictedProperties() ? _countof(specialPropertyIds) : 0;
  2095. }
  2096. // Returns the list of special non-enumerable properties for the type.
  2097. PropertyId const * JavascriptFunction::GetSpecialPropertyIds() const
  2098. {
  2099. return specialPropertyIds;
  2100. }
  2101. BOOL JavascriptFunction::GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2102. {
  2103. return JavascriptFunction::GetProperty(originalInstance, propertyId, value, info, requestContext);
  2104. }
  2105. JavascriptFunction* JavascriptFunction::FindCaller(BOOL* foundThis, JavascriptFunction* nullValue, ScriptContext* requestContext)
  2106. {
  2107. ScriptContext* scriptContext = this->GetScriptContext();
  2108. JavascriptFunction* funcCaller = nullValue;
  2109. JavascriptStackWalker walker(scriptContext);
  2110. if (walker.WalkToTarget(this))
  2111. {
  2112. *foundThis = TRUE;
  2113. while (walker.GetCaller(&funcCaller))
  2114. {
  2115. if (walker.IsCallerGlobalFunction())
  2116. {
  2117. // Caller is global/eval. If it's eval, keep looking.
  2118. // Otherwise, return null.
  2119. if (walker.IsEvalCaller())
  2120. {
  2121. continue;
  2122. }
  2123. funcCaller = nullValue;
  2124. }
  2125. break;
  2126. }
  2127. if (funcCaller->GetScriptContext() != requestContext && funcCaller->GetTypeId() == TypeIds_Null)
  2128. {
  2129. // There are cases where StackWalker might return null value from different scriptContext
  2130. // Caller of this function expects nullValue from the requestContext.
  2131. funcCaller = nullValue;
  2132. }
  2133. }
  2134. return StackScriptFunction::EnsureBoxed(BOX_PARAM(funcCaller, nullptr, L"caller"));
  2135. }
  2136. BOOL JavascriptFunction::GetCallerProperty(Var originalInstance, Var* value, ScriptContext* requestContext)
  2137. {
  2138. ScriptContext* scriptContext = this->GetScriptContext();
  2139. if (this->IsStrictMode())
  2140. {
  2141. return false;
  2142. }
  2143. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  2144. {
  2145. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2146. {
  2147. JavascriptFunction* accessor = requestContext->GetLibrary()->GetThrowTypeErrorCallerAccessorFunction();
  2148. *value = accessor->GetEntryPoint()(accessor, 1, originalInstance);
  2149. }
  2150. return true;
  2151. }
  2152. JavascriptFunction* nullValue = (JavascriptFunction*)requestContext->GetLibrary()->GetNull();
  2153. if (this->IsLibraryCode()) // Hide .caller for builtins
  2154. {
  2155. *value = nullValue;
  2156. return true;
  2157. }
  2158. // Use a stack walker to find this function's frame. If we find it, find its caller.
  2159. BOOL foundThis = FALSE;
  2160. JavascriptFunction* funcCaller = FindCaller(&foundThis, nullValue, requestContext);
  2161. // WOOB #1142373. We are trying to get the caller in window.onerror = function(){alert(arguments.callee.caller);} case
  2162. // window.onerror is called outside of JavascriptFunction::CallFunction loop, so the caller information is not available
  2163. // in the stack to be found by the stack walker.
  2164. // As we had already walked the stack at throw time retrieve the caller information stored in the exception object
  2165. // The down side is that we can only find the top level caller at thrown time, and won't be able to find caller.caller etc.
  2166. // We'll try to fetch the caller only if we can find the function on the stack, but we can't find the caller if and we are in
  2167. // window.onerror scenario.
  2168. *value = funcCaller;
  2169. if (foundThis && funcCaller == nullValue && scriptContext->GetThreadContext()->HasUnhandledException())
  2170. {
  2171. Js::JavascriptExceptionObject* unhandledExceptionObject = scriptContext->GetThreadContext()->GetUnhandledExceptionObject();
  2172. if (unhandledExceptionObject)
  2173. {
  2174. JavascriptFunction* exceptionFunction = unhandledExceptionObject->GetFunction();
  2175. // This is for getcaller in window.onError. The behavior is different in different browsers
  2176. if (exceptionFunction && scriptContext == exceptionFunction->GetScriptContext())
  2177. {
  2178. *value = exceptionFunction;
  2179. }
  2180. }
  2181. }
  2182. else if (foundThis && scriptContext != funcCaller->GetScriptContext())
  2183. {
  2184. HRESULT hr = scriptContext->GetHostScriptContext()->CheckCrossDomainScriptContext(funcCaller->GetScriptContext());
  2185. if (S_OK != hr)
  2186. {
  2187. *value = scriptContext->GetLibrary()->GetNull();
  2188. }
  2189. }
  2190. if (Js::JavascriptFunction::Is(*value) && Js::JavascriptFunction::FromVar(*value)->IsStrictMode())
  2191. {
  2192. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2193. {
  2194. // ES5.15.3.5.4 [[Get]] (P) -- access to the 'caller' property of strict mode function results in TypeError.
  2195. // Note that for caller coming from remote context (see the check right above) we can't call IsStrictMode()
  2196. // unless CheckCrossDomainScriptContext succeeds. If it fails we don't know whether caller is strict mode
  2197. // function or not and throw if it's not, so just return Null.
  2198. JavascriptError::ThrowTypeError(scriptContext, JSERR_AccessCallerRestricted);
  2199. }
  2200. }
  2201. return true;
  2202. }
  2203. BOOL JavascriptFunction::GetArgumentsProperty(Var originalInstance, Var* value, ScriptContext* requestContext)
  2204. {
  2205. ScriptContext* scriptContext = this->GetScriptContext();
  2206. if (this->IsStrictMode())
  2207. {
  2208. return false;
  2209. }
  2210. if (this->GetEntryPoint() == JavascriptFunction::PrototypeEntryPoint)
  2211. {
  2212. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2213. {
  2214. JavascriptFunction* accessor = requestContext->GetLibrary()->GetThrowTypeErrorArgumentsAccessorFunction();
  2215. *value = accessor->GetEntryPoint()(accessor, 1, originalInstance);
  2216. }
  2217. return true;
  2218. }
  2219. if (!this->IsScriptFunction())
  2220. {
  2221. // builtin function do not have an argument object - return null.
  2222. *value = scriptContext->GetLibrary()->GetNull();
  2223. return true;
  2224. }
  2225. // Use a stack walker to find this function's frame. If we find it, compute its arguments.
  2226. // Note that we are currently unable to guarantee that the binding between formal arguments
  2227. // and foo.arguments[n] will be maintained after this object is returned.
  2228. JavascriptStackWalker walker(scriptContext);
  2229. if (walker.WalkToTarget(this))
  2230. {
  2231. if (walker.IsCallerGlobalFunction())
  2232. {
  2233. *value = requestContext->GetLibrary()->GetNull();
  2234. }
  2235. else
  2236. {
  2237. Var args = walker.GetPermanentArguments();
  2238. if (args == NULL)
  2239. {
  2240. CallInfo const *callInfo = walker.GetCallInfo();
  2241. args = JavascriptOperators::LoadHeapArguments(
  2242. this, callInfo->Count - 1,
  2243. walker.GetJavascriptArgs(),
  2244. scriptContext->GetLibrary()->GetNull(),
  2245. scriptContext->GetLibrary()->GetNull(),
  2246. scriptContext,
  2247. /* formalsAreLetDecls */ false);
  2248. walker.SetPermanentArguments(args);
  2249. }
  2250. *value = args;
  2251. }
  2252. }
  2253. else
  2254. {
  2255. *value = scriptContext->GetLibrary()->GetNull();
  2256. }
  2257. return true;
  2258. }
  2259. BOOL JavascriptFunction::GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2260. {
  2261. BOOL result = DynamicObject::GetProperty(originalInstance, propertyId, value, info, requestContext);
  2262. if (result)
  2263. {
  2264. if (propertyId == PropertyIds::prototype)
  2265. {
  2266. PropertyValueInfo::DisableStoreFieldCache(info);
  2267. }
  2268. }
  2269. else
  2270. {
  2271. GetPropertyBuiltIns(originalInstance, propertyId, value, requestContext, &result);
  2272. }
  2273. return result;
  2274. }
  2275. BOOL JavascriptFunction::GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  2276. {
  2277. BOOL result;
  2278. PropertyRecord const* propertyRecord;
  2279. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2280. result = DynamicObject::GetProperty(originalInstance, propertyNameString, value, info, requestContext);
  2281. if (result)
  2282. {
  2283. if (propertyRecord != nullptr && propertyRecord->GetPropertyId() == PropertyIds::prototype)
  2284. {
  2285. PropertyValueInfo::DisableStoreFieldCache(info);
  2286. }
  2287. return result;
  2288. }
  2289. if (propertyRecord != nullptr)
  2290. {
  2291. GetPropertyBuiltIns(originalInstance, propertyRecord->GetPropertyId(), value, requestContext, &result);
  2292. }
  2293. return result;
  2294. }
  2295. bool JavascriptFunction::GetPropertyBuiltIns(Var originalInstance, PropertyId propertyId, Var* value, ScriptContext* requestContext, BOOL* result)
  2296. {
  2297. if (propertyId == PropertyIds::caller && this->HasRestrictedProperties())
  2298. {
  2299. *result = GetCallerProperty(originalInstance, value, requestContext);
  2300. return true;
  2301. }
  2302. if (propertyId == PropertyIds::arguments && this->HasRestrictedProperties())
  2303. {
  2304. *result = GetArgumentsProperty(originalInstance, value, requestContext);
  2305. return true;
  2306. }
  2307. if (propertyId == PropertyIds::length)
  2308. {
  2309. FunctionProxy *proxy = this->GetFunctionProxy();
  2310. if (proxy)
  2311. {
  2312. *value = TaggedInt::ToVarUnchecked(proxy->EnsureDeserialized()->GetReportedInParamsCount() - 1);
  2313. *result = true;
  2314. return true;
  2315. }
  2316. }
  2317. return false;
  2318. }
  2319. BOOL JavascriptFunction::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  2320. {
  2321. bool isReadOnly = false;
  2322. switch (propertyId)
  2323. {
  2324. case PropertyIds::caller:
  2325. if (this->HasRestrictedProperties())
  2326. {
  2327. isReadOnly = true;
  2328. }
  2329. break;
  2330. case PropertyIds::arguments:
  2331. if (this->HasRestrictedProperties())
  2332. {
  2333. isReadOnly = true;
  2334. }
  2335. break;
  2336. case PropertyIds::length:
  2337. if (this->IsScriptFunction())
  2338. {
  2339. isReadOnly = true;
  2340. }
  2341. break;
  2342. }
  2343. if (isReadOnly)
  2344. {
  2345. JavascriptError::ThrowCantAssignIfStrictMode(flags, this->GetScriptContext());
  2346. return false;
  2347. }
  2348. BOOL result = DynamicObject::SetProperty(propertyId, value, flags, info);
  2349. if (propertyId == PropertyIds::prototype)
  2350. {
  2351. PropertyValueInfo::SetNoCache(info, this);
  2352. InvalidateConstructorCacheOnPrototypeChange();
  2353. this->GetScriptContext()->GetThreadContext()->InvalidateIsInstInlineCachesForFunction(this);
  2354. }
  2355. return result;
  2356. }
  2357. BOOL JavascriptFunction::SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags, SideEffects possibleSideEffects)
  2358. {
  2359. BOOL result = __super::SetPropertyWithAttributes(propertyId, value, attributes, info, flags, possibleSideEffects);
  2360. if (propertyId == PropertyIds::prototype)
  2361. {
  2362. PropertyValueInfo::SetNoCache(info, this);
  2363. InvalidateConstructorCacheOnPrototypeChange();
  2364. this->GetScriptContext()->GetThreadContext()->InvalidateIsInstInlineCachesForFunction(this);
  2365. }
  2366. return result;
  2367. }
  2368. BOOL JavascriptFunction::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  2369. {
  2370. PropertyRecord const * propertyRecord;
  2371. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2372. if (propertyRecord != nullptr)
  2373. {
  2374. return JavascriptFunction::SetProperty(propertyRecord->GetPropertyId(), value, flags, info);
  2375. }
  2376. else
  2377. {
  2378. return DynamicObject::SetProperty(propertyNameString, value, flags, info);
  2379. }
  2380. }
  2381. BOOL JavascriptFunction::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
  2382. {
  2383. switch (propertyId)
  2384. {
  2385. case PropertyIds::caller:
  2386. case PropertyIds::arguments:
  2387. if (this->HasRestrictedProperties())
  2388. {
  2389. JavascriptError::ThrowCantDeleteIfStrictMode(flags, this->GetScriptContext(), this->GetScriptContext()->GetPropertyName(propertyId)->GetBuffer());
  2390. return false;
  2391. }
  2392. break;
  2393. case PropertyIds::length:
  2394. if (this->IsScriptFunction())
  2395. {
  2396. JavascriptError::ThrowCantDeleteIfStrictMode(flags, this->GetScriptContext(), this->GetScriptContext()->GetPropertyName(propertyId)->GetBuffer());
  2397. return false;
  2398. }
  2399. break;
  2400. }
  2401. BOOL result = DynamicObject::DeleteProperty(propertyId, flags);
  2402. if (result && propertyId == PropertyIds::prototype)
  2403. {
  2404. InvalidateConstructorCacheOnPrototypeChange();
  2405. this->GetScriptContext()->GetThreadContext()->InvalidateIsInstInlineCachesForFunction(this);
  2406. }
  2407. return result;
  2408. }
  2409. void JavascriptFunction::InvalidateConstructorCacheOnPrototypeChange()
  2410. {
  2411. Assert(this->constructorCache != nullptr);
  2412. #if DBG_DUMP
  2413. if (PHASE_TRACE1(Js::ConstructorCachePhase))
  2414. {
  2415. // This is under DBG_DUMP so we can allow a check
  2416. ParseableFunctionInfo* body = this->GetFunctionProxy() != nullptr ? this->GetFunctionProxy()->EnsureDeserialized() : nullptr;
  2417. const wchar_t* ctorName = body != nullptr ? body->GetDisplayName() : L"<unknown>";
  2418. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2419. Output::Print(L"CtorCache: before invalidating cache (0x%p) for ctor %s (%s): ", this->constructorCache, ctorName,
  2420. body ? body->GetDebugNumberSet(debugStringBuffer) : L"(null)");
  2421. this->constructorCache->Dump();
  2422. Output::Print(L"\n");
  2423. Output::Flush();
  2424. }
  2425. #endif
  2426. this->constructorCache->InvalidateOnPrototypeChange();
  2427. #if DBG_DUMP
  2428. if (PHASE_TRACE1(Js::ConstructorCachePhase))
  2429. {
  2430. // This is under DBG_DUMP so we can allow a check
  2431. ParseableFunctionInfo* body = this->GetFunctionProxy() != nullptr ? this->GetFunctionProxy()->EnsureDeserialized() : nullptr;
  2432. const wchar_t* ctorName = body != nullptr ? body->GetDisplayName() : L"<unknown>";
  2433. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2434. Output::Print(L"CtorCache: after invalidating cache (0x%p) for ctor %s (%s): ", this->constructorCache, ctorName,
  2435. body ? body->GetDebugNumberSet(debugStringBuffer) : L"(null)");
  2436. this->constructorCache->Dump();
  2437. Output::Print(L"\n");
  2438. Output::Flush();
  2439. }
  2440. #endif
  2441. }
  2442. BOOL JavascriptFunction::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  2443. {
  2444. JavascriptString * pString = NULL;
  2445. Var sourceString = this->GetSourceString();
  2446. if (sourceString == nullptr)
  2447. {
  2448. FunctionProxy* proxy = this->GetFunctionProxy();
  2449. if (proxy)
  2450. {
  2451. ParseableFunctionInfo * func = proxy->EnsureDeserialized();
  2452. Utf8SourceInfo* sourceInfo = func->GetUtf8SourceInfo();
  2453. if (sourceInfo->GetIsLibraryCode())
  2454. {
  2455. charcount_t displayNameLength = 0;
  2456. pString = JavascriptFunction::GetLibraryCodeDisplayString(this->GetScriptContext(), func->GetShortDisplayName(&displayNameLength));
  2457. }
  2458. else
  2459. {
  2460. charcount_t count = min(DIAG_MAX_FUNCTION_STRING, func->LengthInChars());
  2461. utf8::DecodeOptions options = sourceInfo->IsCesu8() ? utf8::doAllowThreeByteSurrogates : utf8::doDefault;
  2462. utf8::DecodeInto(stringBuilder->AllocBufferSpace(count), func->GetSource(L"JavascriptFunction::GetDiagValueString"), count, options);
  2463. stringBuilder->IncreaseCount(count);
  2464. return TRUE;
  2465. }
  2466. }
  2467. else
  2468. {
  2469. pString = GetLibrary()->GetFunctionDisplayString();
  2470. }
  2471. }
  2472. else
  2473. {
  2474. if (TaggedInt::Is(sourceString))
  2475. {
  2476. pString = GetNativeFunctionDisplayString(this->GetScriptContext(), this->GetScriptContext()->GetPropertyString(TaggedInt::ToInt32(sourceString)));
  2477. }
  2478. else
  2479. {
  2480. Assert(JavascriptString::Is(sourceString));
  2481. pString = JavascriptString::FromVar(sourceString);
  2482. }
  2483. }
  2484. Assert(pString);
  2485. stringBuilder->Append(pString->GetString(), pString->GetLength());
  2486. return TRUE;
  2487. }
  2488. BOOL JavascriptFunction::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  2489. {
  2490. stringBuilder->AppendCppLiteral(L"Object, (Function)");
  2491. return TRUE;
  2492. }
  2493. JavascriptString* JavascriptFunction::GetDisplayNameImpl() const
  2494. {
  2495. Assert(this->GetFunctionProxy() != nullptr); // The caller should guarantee a proxy exists
  2496. ParseableFunctionInfo * func = this->GetFunctionProxy()->EnsureDeserialized();
  2497. charcount_t length = 0;
  2498. const wchar_t* name = func->GetShortDisplayName(&length);
  2499. return DisplayNameHelper(name, length);
  2500. }
  2501. JavascriptString* JavascriptFunction::DisplayNameHelper(const wchar_t* name, charcount_t length) const
  2502. {
  2503. ScriptContext* scriptContext = this->GetScriptContext();
  2504. Assert(this->GetFunctionProxy() != nullptr); // The caller should guarantee a proxy exists
  2505. ParseableFunctionInfo * func = this->GetFunctionProxy()->EnsureDeserialized();
  2506. if (func->GetDisplayName() == Js::Constants::FunctionCode)
  2507. {
  2508. return LiteralString::NewCopyBuffer(Js::Constants::Anonymous, Js::Constants::AnonymousLength, scriptContext);
  2509. }
  2510. else if (func->GetIsAccessor())
  2511. {
  2512. const wchar_t* accessorName = func->GetDisplayName();
  2513. if (accessorName[0] == L'g')
  2514. {
  2515. return LiteralString::Concat(LiteralString::NewCopySz(L"get ", scriptContext), LiteralString::NewCopyBuffer(name, length, scriptContext));
  2516. }
  2517. AssertMsg(accessorName[0] == L's', "should be a set");
  2518. return LiteralString::Concat(LiteralString::NewCopySz(L"set ", scriptContext), LiteralString::NewCopyBuffer(name, length, scriptContext));
  2519. }
  2520. return LiteralString::NewCopyBuffer(name, length, scriptContext);
  2521. }
  2522. bool JavascriptFunction::GetFunctionName(JavascriptString** name) const
  2523. {
  2524. Assert(name != nullptr);
  2525. FunctionProxy* proxy = this->GetFunctionProxy();
  2526. JavascriptFunction* thisFunction = const_cast<JavascriptFunction*>(this);
  2527. if (proxy || thisFunction->IsBoundFunction() || JavascriptGeneratorFunction::Is(thisFunction))
  2528. {
  2529. *name = GetDisplayNameImpl();
  2530. return true;
  2531. }
  2532. Assert(!ScriptFunction::Is(thisFunction));
  2533. return GetSourceStringName(name);
  2534. }
  2535. bool JavascriptFunction::GetSourceStringName(JavascriptString** name) const
  2536. {
  2537. Assert(name != nullptr);
  2538. ScriptContext* scriptContext = this->GetScriptContext();
  2539. Var sourceString = this->GetSourceString();
  2540. if (sourceString)
  2541. {
  2542. if (TaggedInt::Is(sourceString))
  2543. {
  2544. int32 propertyIdOfSourceString = TaggedInt::ToInt32(sourceString);
  2545. *name = scriptContext->GetPropertyString(propertyIdOfSourceString);
  2546. return true;
  2547. }
  2548. Assert(JavascriptString::Is(sourceString));
  2549. *name = JavascriptString::FromVar(sourceString);
  2550. return true;
  2551. }
  2552. return false;
  2553. }
  2554. JavascriptString* JavascriptFunction::GetDisplayName() const
  2555. {
  2556. ScriptContext* scriptContext = this->GetScriptContext();
  2557. FunctionProxy* proxy = this->GetFunctionProxy();
  2558. JavascriptLibrary* library = scriptContext->GetLibrary();
  2559. if (proxy)
  2560. {
  2561. ParseableFunctionInfo * func = proxy->EnsureDeserialized();
  2562. return LiteralString::NewCopySz(func->GetDisplayName(), scriptContext);
  2563. }
  2564. JavascriptString* sourceStringName = nullptr;
  2565. if (GetSourceStringName(&sourceStringName))
  2566. {
  2567. return sourceStringName;
  2568. }
  2569. return library->GetFunctionDisplayString();
  2570. }
  2571. Var JavascriptFunction::GetTypeOfString(ScriptContext * requestContext)
  2572. {
  2573. return requestContext->GetLibrary()->GetFunctionTypeDisplayString();
  2574. }
  2575. // Check if this function is native/script library code
  2576. bool JavascriptFunction::IsLibraryCode() const
  2577. {
  2578. return !this->IsScriptFunction() || this->GetFunctionProxy()->GetUtf8SourceInfo()->GetIsLibraryCode();
  2579. }
  2580. // Implementation of Function.prototype[@@hasInstance](V) as specified in 19.2.3.6 of ES6 spec
  2581. Var JavascriptFunction::EntrySymbolHasInstance(RecyclableObject* function, CallInfo callInfo, ...)
  2582. {
  2583. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  2584. ARGUMENTS(args, callInfo);
  2585. ScriptContext* scriptContext = function->GetScriptContext();
  2586. Assert(!(callInfo.Flags & CallFlags_New));
  2587. if (args.Info.Count < 2)
  2588. {
  2589. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, L"Function[Symbol.hasInstance]");
  2590. }
  2591. RecyclableObject * constructor = RecyclableObject::FromVar(args[0]);
  2592. Var instance = args[1];
  2593. if (!JavascriptConversion::IsCallable(constructor))
  2594. {
  2595. return JavascriptBoolean::ToVar(FALSE, scriptContext);
  2596. }
  2597. Assert(JavascriptProxy::Is(constructor) || JavascriptFunction::Is(constructor));
  2598. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, NULL), scriptContext);
  2599. }
  2600. BOOL JavascriptFunction::HasInstance(Var instance, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  2601. {
  2602. Var funcPrototype;
  2603. if (this->GetTypeHandler()->GetHasKnownSlot0())
  2604. {
  2605. Assert(this->GetDynamicType()->GetTypeHandler()->GetPropertyId(scriptContext, (PropertyIndex)0) == PropertyIds::prototype);
  2606. funcPrototype = this->GetSlot(0);
  2607. }
  2608. else
  2609. {
  2610. funcPrototype = JavascriptOperators::GetProperty(this, PropertyIds::prototype, scriptContext, nullptr);
  2611. }
  2612. funcPrototype = CrossSite::MarshalVar(scriptContext, funcPrototype);
  2613. return JavascriptFunction::HasInstance(funcPrototype, instance, scriptContext, inlineCache, this);
  2614. }
  2615. BOOL JavascriptFunction::HasInstance(Var funcPrototype, Var instance, ScriptContext * scriptContext, IsInstInlineCache* inlineCache, JavascriptFunction *function)
  2616. {
  2617. BOOL result = FALSE;
  2618. JavascriptBoolean * javascriptResult;
  2619. //
  2620. // if "instance" is not a JavascriptObject, return false
  2621. //
  2622. if (!JavascriptOperators::IsObject(instance))
  2623. {
  2624. // Only update the cache for primitive cache if it is empty already for the JIT fast path
  2625. if (inlineCache && inlineCache->function == nullptr
  2626. && scriptContext == function->GetScriptContext())// only register when function has same scriptContext
  2627. {
  2628. inlineCache->Cache(RecyclableObject::Is(instance) ?
  2629. RecyclableObject::FromVar(instance)->GetType() : nullptr,
  2630. function, scriptContext->GetLibrary()->GetFalse(), scriptContext);
  2631. }
  2632. return result;
  2633. }
  2634. // If we have an instance of inline cache, let's try to use it to speed up the operation.
  2635. // We would like to catch all cases when we already know (by having checked previously)
  2636. // that an object on the left of instance of has been created by a function on the right,
  2637. // as well as when we already know the object on the left has not been created by a function on the right.
  2638. // In practice, we can do so only if the function matches the function in the cache, and the object's type matches the
  2639. // type in the cache. Notably, this typically means that if some of the objects evolved after construction,
  2640. // while others did not, we will miss the cache for one of the two (sets of objects).
  2641. // An important subtlety here arises when a function is called from different script contexts.
  2642. // Suppose we called function foo from script context A, and we pass it an object o created in the same script context.
  2643. // When function foo checks if object o is an instance of itself (function foo) for the first time (from context A) we will
  2644. // populate the cache with function foo and object o's type (which is permanently bound to the script context A,
  2645. // in which object o was created). If we later invoked function foo from script context B and perform the same instance-of check,
  2646. // the function will still match the function in the cache (because objects' identities do not change during cross-context marshalling).
  2647. // However, object o's type (even if it is of the same "shape" as before) will be different, because the object types are permanently
  2648. // bound and unique to the script context from which they were created. Hence, the cache may miss, even if the function matches.
  2649. if (inlineCache != nullptr)
  2650. {
  2651. Assert(function != nullptr);
  2652. if (inlineCache->TryGetResult(instance, function, &javascriptResult))
  2653. {
  2654. return javascriptResult == scriptContext->GetLibrary()->GetTrue();
  2655. }
  2656. }
  2657. // If we are here, then me must have missed the cache. This may be because:
  2658. // a) the cache has never been populated in the first place,
  2659. // b) the cache has been populated, but for an object of a different type (even if the object was created by the same constructor function),
  2660. // c) the cache has been populated, but for a different function,
  2661. // d) the cache has been populated, even for the same object type and function, but has since been invalidated, because the function's
  2662. // prototype property has been changed (see JavascriptFunction::SetProperty and ThreadContext::InvalidateIsInstInlineCachesForFunction).
  2663. // We may even miss the cache if we ask again about the very same object the very same function the cache was populated with.
  2664. // This subtlety arises when a function is called from two (or more) different script contexts.
  2665. // Suppose we called function foo from script context A, and passed it an object o created in the same script context.
  2666. // When function foo checks if object o is an instance of itself (function foo) for the first time (from context A) we will
  2667. // populate the cache with function foo and object o's type (which is permanently bound to the script context A,
  2668. // in which object o was created). If we later invoked function foo from script context B and perform the same instance of check,
  2669. // the function will still match the function in the cache (because objects' identities do not change during cross-context marshalling).
  2670. // However, object o's type (even if it is of the same "shape" as before, and even if o is the very same object) will be different,
  2671. // because the object types are permanently bound and unique to the script context from which they were created.
  2672. Var prototype = JavascriptOperators::GetPrototype(RecyclableObject::FromVar(instance));
  2673. if (!JavascriptOperators::IsObject(funcPrototype))
  2674. {
  2675. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidPrototype);
  2676. }
  2677. // Since we missed the cache, we must now walk the prototype chain of the object to check if the given function's prototype is somewhere in
  2678. // that chain. If it is, we return true. Otherwise (i.e., we hit the end of the chain before finding the function's prototype) we return false.
  2679. while (JavascriptOperators::GetTypeId(prototype) != TypeIds_Null)
  2680. {
  2681. if (prototype == funcPrototype)
  2682. {
  2683. result = TRUE;
  2684. break;
  2685. }
  2686. prototype = JavascriptOperators::GetPrototype(RecyclableObject::FromVar(prototype));
  2687. }
  2688. // Now that we know the answer, let's cache it for next time if we have a cache.
  2689. if (inlineCache != NULL)
  2690. {
  2691. Assert(function != NULL);
  2692. JavascriptBoolean * boolResult = result ? scriptContext->GetLibrary()->GetTrue() :
  2693. scriptContext->GetLibrary()->GetFalse();
  2694. Type * instanceType = RecyclableObject::FromVar(instance)->GetType();
  2695. if (!instanceType->HasSpecialPrototype()
  2696. && scriptContext == function->GetScriptContext()) // only register when function has same scriptContext, otherwise when scriptContext close
  2697. // and the isInst inline cache chain will be broken by clearing the arenaAllocator
  2698. {
  2699. inlineCache->Cache(instanceType, function, boolResult, scriptContext);
  2700. }
  2701. }
  2702. return result;
  2703. }
  2704. }