JavascriptObject.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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 "Types/NullTypeHandler.h"
  7. namespace Js
  8. {
  9. Var JavascriptObject::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  10. {
  11. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  12. ARGUMENTS(args, callInfo);
  13. ScriptContext* scriptContext = function->GetScriptContext();
  14. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  15. // SkipDefaultNewObject function flag should have prevented the default object from
  16. // being created, except when call true a host dispatch.
  17. Var newTarget = callInfo.Flags & CallFlags_NewTarget ? args.Values[args.Info.Count] : args[0];
  18. bool isCtorSuperCall = (callInfo.Flags & CallFlags_New) && newTarget != nullptr && !JavascriptOperators::IsUndefined(newTarget);
  19. Assert(isCtorSuperCall || !(callInfo.Flags & CallFlags_New) || args[0] == nullptr
  20. || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  21. if (args.Info.Count > 1)
  22. {
  23. switch (JavascriptOperators::GetTypeId(args[1]))
  24. {
  25. case TypeIds_Undefined:
  26. case TypeIds_Null:
  27. // Break to return a new object
  28. break;
  29. case TypeIds_StringObject:
  30. case TypeIds_Function:
  31. case TypeIds_Array:
  32. case TypeIds_ES5Array:
  33. case TypeIds_RegEx:
  34. case TypeIds_NumberObject:
  35. case TypeIds_SIMDObject:
  36. case TypeIds_Date:
  37. case TypeIds_BooleanObject:
  38. case TypeIds_Error:
  39. case TypeIds_Object:
  40. case TypeIds_Arguments:
  41. case TypeIds_ActivationObject:
  42. case TypeIds_SymbolObject:
  43. return isCtorSuperCall ?
  44. JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), RecyclableObject::FromVar(args[1]), nullptr, scriptContext) :
  45. args[1];
  46. default:
  47. RecyclableObject* result = nullptr;
  48. if (FALSE == JavascriptConversion::ToObject(args[1], scriptContext, &result))
  49. {
  50. // JavascriptConversion::ToObject should only return FALSE for null and undefined.
  51. Assert(false);
  52. }
  53. return isCtorSuperCall ?
  54. JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), result, nullptr, scriptContext) :
  55. result;
  56. }
  57. }
  58. if (callInfo.Flags & CallFlags_NotUsed)
  59. {
  60. return args[0];
  61. }
  62. Var newObj = scriptContext->GetLibrary()->CreateObject(true);
  63. return isCtorSuperCall ?
  64. JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), RecyclableObject::FromVar(newObj), nullptr, scriptContext) :
  65. newObj;
  66. }
  67. Var JavascriptObject::EntryHasOwnProperty(RecyclableObject* function, CallInfo callInfo, ...)
  68. {
  69. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  70. ARGUMENTS(args, callInfo);
  71. ScriptContext* scriptContext = function->GetScriptContext();
  72. Assert(!(callInfo.Flags & CallFlags_New));
  73. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  74. RecyclableObject* dynamicObject = nullptr;
  75. if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
  76. {
  77. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.hasOwnProperty"));
  78. }
  79. // no property specified
  80. if (args.Info.Count == 1)
  81. {
  82. return scriptContext->GetLibrary()->GetFalse();
  83. }
  84. const PropertyRecord* propertyRecord;
  85. JavascriptConversion::ToPropertyKey(args[1], scriptContext, &propertyRecord);
  86. if (JavascriptOperators::HasOwnProperty(dynamicObject, propertyRecord->GetPropertyId(), scriptContext))
  87. {
  88. return scriptContext->GetLibrary()->GetTrue();
  89. }
  90. return scriptContext->GetLibrary()->GetFalse();
  91. }
  92. Var JavascriptObject::EntryPropertyIsEnumerable(RecyclableObject* function, CallInfo callInfo, ...)
  93. {
  94. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  95. ARGUMENTS(args, callInfo);
  96. ScriptContext* scriptContext = function->GetScriptContext();
  97. Assert(!(callInfo.Flags & CallFlags_New));
  98. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  99. RecyclableObject* dynamicObject = nullptr;
  100. if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
  101. {
  102. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.propertyIsEnumerable"));
  103. }
  104. if (args.Info.Count >= 2)
  105. {
  106. const PropertyRecord* propertyRecord;
  107. JavascriptConversion::ToPropertyKey(args[1], scriptContext, &propertyRecord);
  108. PropertyId propertyId = propertyRecord->GetPropertyId();
  109. PropertyDescriptor currentDescriptor;
  110. BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(dynamicObject, propertyId, scriptContext, &currentDescriptor);
  111. if (isCurrentDescriptorDefined == TRUE)
  112. {
  113. if (currentDescriptor.IsEnumerable())
  114. {
  115. return scriptContext->GetLibrary()->GetTrue();
  116. }
  117. }
  118. }
  119. return scriptContext->GetLibrary()->GetFalse();
  120. }
  121. BOOL JavascriptObject::ChangePrototype(RecyclableObject* object, RecyclableObject* newPrototype, bool shouldThrow, ScriptContext* scriptContext)
  122. {
  123. // 8.3.2 [[SetInheritance]] (V)
  124. // When the [[SetInheritance]] internal method of O is called with argument V the following steps are taken:
  125. // 1. Assert: Either Type(V) is Object or Type(V) is Null.
  126. Assert(JavascriptOperators::IsObject(object));
  127. Assert(JavascriptOperators::IsObjectOrNull(newPrototype));
  128. if (JavascriptProxy::Is(object))
  129. {
  130. JavascriptProxy* proxy = JavascriptProxy::FromVar(object);
  131. CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
  132. return proxy->SetPrototypeTrap(newPrototype, shouldThrow);
  133. }
  134. // 2. Let extensible be the value of the [[Extensible]] internal data property of O.
  135. // 3. Let current be the value of the [[Prototype]] internal data property of O.
  136. // 4. If SameValue(V, current), then return true.
  137. if (newPrototype == JavascriptObject::GetPrototypeOf(object, scriptContext))
  138. {
  139. return TRUE;
  140. }
  141. // 5. If extensible is false, then return false.
  142. if (!object->IsExtensible())
  143. {
  144. if (shouldThrow)
  145. {
  146. JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
  147. }
  148. return FALSE;
  149. }
  150. if (object->IsProtoImmutable())
  151. {
  152. // ES2016 19.1.3:
  153. // The Object prototype object is the intrinsic object %ObjectPrototype%.
  154. // The Object prototype object is an immutable prototype exotic object.
  155. // ES2016 9.4.7:
  156. // An immutable prototype exotic object is an exotic object that has an immutable [[Prototype]] internal slot.
  157. JavascriptError::ThrowTypeError(scriptContext, JSERR_ImmutablePrototypeSlot);
  158. }
  159. // 6. If V is not null, then
  160. // a. Let p be V.
  161. // b. Repeat, while p is not null
  162. // i. If SameValue(p, O) is true, then return false.
  163. // ii. Let nextp be the result of calling the [[GetInheritance]] internal method of p with no arguments.
  164. // iii. ReturnIfAbrupt(nextp).
  165. // iv. Let p be nextp.
  166. if (IsPrototypeOf(object, newPrototype, scriptContext)) // Reject cycle
  167. {
  168. if (shouldThrow)
  169. {
  170. JavascriptError::ThrowTypeError(scriptContext, JSERR_CyclicProtoValue);
  171. }
  172. return FALSE;
  173. }
  174. // 7. Set the value of the [[Prototype]] internal data property of O to V.
  175. // 8. Return true.
  176. bool isInvalidationOfInlineCacheNeeded = true;
  177. DynamicObject * obj = DynamicObject::FromVar(object);
  178. // If this object was not prototype object, then no need to invalidate inline caches.
  179. // Simply assign it a new type so if this object used protoInlineCache in past, it will
  180. // be invalidated because of type mismatch and subsequently we will update its protoInlineCache
  181. if (!(obj->GetDynamicType()->GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag))
  182. {
  183. // If object has locked type, skip changing its type here as it will be changed anyway below
  184. // when object gets newPrototype object.
  185. if (!obj->HasLockedType())
  186. {
  187. obj->ChangeType();
  188. }
  189. Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInProtoInlineCaches(obj));
  190. Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInStoreFieldInlineCaches(obj));
  191. isInvalidationOfInlineCacheNeeded = false;
  192. }
  193. if (isInvalidationOfInlineCacheNeeded)
  194. {
  195. // Notify old prototypes that they are being removed from a prototype chain. This triggers invalidating protocache, etc.
  196. JavascriptOperators::MapObjectAndPrototypes<true>(object->GetPrototype(), [=](RecyclableObject* obj)
  197. {
  198. obj->RemoveFromPrototype(scriptContext);
  199. });
  200. // Examine new prototype chain. If it brings in any non-WritableData property, we need to invalidate related caches.
  201. bool objectAndPrototypeChainHasOnlyWritableDataProperties =
  202. JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(newPrototype);
  203. if (!objectAndPrototypeChainHasOnlyWritableDataProperties
  204. || object->GetScriptContext() != newPrototype->GetScriptContext())
  205. {
  206. // The HaveOnlyWritableDataProperties cache is cleared when a property is added or changed,
  207. // but only for types in the same script context. Therefore, if the prototype is in another
  208. // context, the object's cache won't be cleared when a property is added or changed on the prototype.
  209. // Moreover, an object is added to the cache only when its whole prototype chain is in the same
  210. // context.
  211. //
  212. // Since we don't have a way to find out which objects have a certain object as their prototype,
  213. // we clear the cache here instead.
  214. // Invalidate fast prototype chain writable data test flag
  215. object->GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
  216. }
  217. if (!objectAndPrototypeChainHasOnlyWritableDataProperties)
  218. {
  219. // Invalidate StoreField/PropertyGuards for any non-WritableData property in the new chain
  220. JavascriptOperators::MapObjectAndPrototypes<true>(newPrototype, [=](RecyclableObject* obj)
  221. {
  222. if (!obj->HasOnlyWritableDataProperties())
  223. {
  224. obj->AddToPrototype(scriptContext);
  225. }
  226. });
  227. }
  228. }
  229. // Set to new prototype
  230. if (object->IsExternal() || (DynamicType::Is(object->GetTypeId()) && (DynamicObject::FromVar(object))->IsCrossSiteObject()))
  231. {
  232. CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
  233. }
  234. object->SetPrototype(newPrototype);
  235. return TRUE;
  236. }
  237. Var JavascriptObject::EntryIsPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
  238. {
  239. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  240. ARGUMENTS(args, callInfo);
  241. ScriptContext* scriptContext = function->GetScriptContext();
  242. Assert(!(callInfo.Flags & CallFlags_New));
  243. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  244. // no property specified
  245. if (args.Info.Count == 1 || !JavascriptOperators::IsObject(args[1]))
  246. {
  247. return scriptContext->GetLibrary()->GetFalse();
  248. }
  249. RecyclableObject* dynamicObject = nullptr;
  250. if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
  251. {
  252. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.isPrototypeOf"));
  253. }
  254. RecyclableObject* value = RecyclableObject::FromVar(args[1]);
  255. if (dynamicObject->GetTypeId() == TypeIds_GlobalObject)
  256. {
  257. dynamicObject = RecyclableObject::FromVar(static_cast<Js::GlobalObject*>(dynamicObject)->ToThis());
  258. }
  259. while (JavascriptOperators::GetTypeId(value) != TypeIds_Null)
  260. {
  261. value = JavascriptOperators::GetPrototype(value);
  262. if (dynamicObject == value)
  263. {
  264. return scriptContext->GetLibrary()->GetTrue();
  265. }
  266. }
  267. return scriptContext->GetLibrary()->GetFalse();
  268. }
  269. // 19.1.3.5 - Object.prototype.toLocaleString as of ES6 (6.0)
  270. Var JavascriptObject::EntryToLocaleString(RecyclableObject* function, CallInfo callInfo, ...)
  271. {
  272. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  273. ARGUMENTS(args, callInfo);
  274. ScriptContext* scriptContext = function->GetScriptContext();
  275. Assert(!(callInfo.Flags & CallFlags_New));
  276. AssertMsg(args.Info.Count, "Should always have implicit 'this'");
  277. Var thisValue = args[0];
  278. RecyclableObject* dynamicObject = nullptr;
  279. if (FALSE == JavascriptConversion::ToObject(thisValue, scriptContext, &dynamicObject))
  280. {
  281. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.toLocaleString"));
  282. }
  283. Var toStringVar = nullptr;
  284. if (!JavascriptOperators::GetProperty(thisValue, dynamicObject, Js::PropertyIds::toString, &toStringVar, scriptContext) || !JavascriptConversion::IsCallable(toStringVar))
  285. {
  286. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.toLocaleString"));
  287. }
  288. RecyclableObject* toStringFunc = RecyclableObject::FromVar(toStringVar);
  289. return CALL_FUNCTION(scriptContext->GetThreadContext(), toStringFunc, CallInfo(CallFlags_Value, 1), thisValue);
  290. }
  291. Var JavascriptObject::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
  292. {
  293. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  294. ARGUMENTS(args, callInfo);
  295. ScriptContext* scriptContext = function->GetScriptContext();
  296. Assert(!(callInfo.Flags & CallFlags_New));
  297. AssertMsg(args.Info.Count, "Should always have implicit 'this'");
  298. return ToStringHelper(args[0], scriptContext);
  299. }
  300. // ES2017 19.1.3.6 Object.prototype.toString()
  301. JavascriptString* JavascriptObject::ToStringTagHelper(Var thisArg, ScriptContext *scriptContext, TypeId type)
  302. {
  303. JavascriptLibrary *library = scriptContext->GetLibrary();
  304. // 1. If the this value is undefined, return "[object Undefined]".
  305. if (type == TypeIds_Undefined)
  306. {
  307. return library->CreateStringFromCppLiteral(_u("[object Undefined]"));
  308. }
  309. // 2. If the this value is null, return "[object Null]".
  310. if (type == TypeIds_Null)
  311. {
  312. return library->CreateStringFromCppLiteral(_u("[object Null]"));
  313. }
  314. // 3. Let O be ToObject(this value).
  315. RecyclableObject *thisArgAsObject = RecyclableObject::FromVar(JavascriptOperators::ToObject(thisArg, scriptContext));
  316. // 4. Let isArray be ? IsArray(O).
  317. // There is an implicit check for a null proxy handler in IsArray, so use the operator.
  318. BOOL isArray = JavascriptOperators::IsArray(thisArgAsObject);
  319. // 15. Let tag be ? Get(O, @@toStringTag).
  320. Var tag = JavascriptOperators::GetProperty(thisArgAsObject, PropertyIds::_symbolToStringTag, scriptContext); // Let tag be the result of Get(O, @@toStringTag).
  321. // 17. Return the String that is the result of concatenating "[object ", tag, and "]".
  322. auto buildToString = [&scriptContext](Var tag) {
  323. JavascriptString *tagStr = JavascriptString::FromVar(tag);
  324. CompoundString::Builder<32> stringBuilder(scriptContext);
  325. stringBuilder.AppendChars(_u("[object "));
  326. stringBuilder.AppendChars(tagStr);
  327. stringBuilder.AppendChars(_u(']'));
  328. return stringBuilder.ToString();
  329. };
  330. if (tag != nullptr && JavascriptString::Is(tag))
  331. {
  332. return buildToString(tag);
  333. }
  334. // If we don't have a tag or it's not a string, use the 'built in tag'.
  335. if (isArray)
  336. {
  337. // 5. If isArray is true, let builtinTag be "Array".
  338. return library->CreateStringFromCppLiteral(_u("[object Array]"));
  339. }
  340. JavascriptString* builtInTag = nullptr;
  341. switch (type)
  342. {
  343. // 6. Else if O is an exotic String object, let builtinTag be "String".
  344. case TypeIds_String:
  345. case TypeIds_StringObject:
  346. builtInTag = library->CreateStringFromCppLiteral(_u("[object String]"));
  347. break;
  348. // 7. Else if O has an[[ParameterMap]] internal slot, let builtinTag be "Arguments".
  349. case TypeIds_Arguments:
  350. builtInTag = library->CreateStringFromCppLiteral(_u("[object Arguments]"));
  351. break;
  352. // 8. Else if O has a [[Call]] internal method, let builtinTag be "Function".
  353. case TypeIds_Function:
  354. builtInTag = library->CreateStringFromCppLiteral(_u("[object Function]"));
  355. break;
  356. // 9. Else if O has an [[ErrorData]] internal slot, let builtinTag be "Error".
  357. case TypeIds_Error:
  358. builtInTag = library->GetErrorDisplayString();
  359. break;
  360. // 10. Else if O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
  361. case TypeIds_Boolean:
  362. case TypeIds_BooleanObject:
  363. builtInTag = library->CreateStringFromCppLiteral(_u("[object Boolean]"));
  364. break;
  365. // 11. Else if O has a [[NumberData]] internal slot, let builtinTag be "Number".
  366. case TypeIds_Number:
  367. case TypeIds_Int64Number:
  368. case TypeIds_UInt64Number:
  369. case TypeIds_Integer:
  370. case TypeIds_NumberObject:
  371. builtInTag = library->CreateStringFromCppLiteral(_u("[object Number]"));
  372. break;
  373. // 12. Else if O has a [[DateValue]] internal slot, let builtinTag be "Date".
  374. case TypeIds_Date:
  375. case TypeIds_WinRTDate:
  376. builtInTag = library->CreateStringFromCppLiteral(_u("[object Date]"));
  377. break;
  378. // 13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
  379. case TypeIds_RegEx:
  380. builtInTag = library->CreateStringFromCppLiteral(_u("[object RegExp]"));
  381. break;
  382. // 14. Else, let builtinTag be "Object".
  383. default:
  384. {
  385. if (thisArgAsObject->IsExternal())
  386. {
  387. builtInTag = buildToString(thisArgAsObject->GetClassName(scriptContext));
  388. }
  389. else
  390. {
  391. builtInTag = library->GetObjectDisplayString(); // [object Object]
  392. }
  393. break;
  394. }
  395. }
  396. Assert(builtInTag != nullptr);
  397. return builtInTag;
  398. }
  399. Var JavascriptObject::ToStringHelper(Var thisArg, ScriptContext* scriptContext)
  400. {
  401. TypeId type = JavascriptOperators::GetTypeId(thisArg);
  402. // We first need to make sure we are in the right context.
  403. if (type == TypeIds_HostDispatch)
  404. {
  405. RecyclableObject* hostDispatchObject = RecyclableObject::FromVar(thisArg);
  406. const DynamicObject* remoteObject = hostDispatchObject->GetRemoteObject();
  407. if (!remoteObject)
  408. {
  409. Var result = nullptr;
  410. Js::Var values[1];
  411. Js::CallInfo info(Js::CallFlags_Value, 1);
  412. Js::Arguments args(info, values);
  413. values[0] = thisArg;
  414. if (hostDispatchObject->InvokeBuiltInOperationRemotely(EntryToString, args, &result))
  415. {
  416. return result;
  417. }
  418. }
  419. }
  420. // Dispatch to @@toStringTag implementation.
  421. if (type >= TypeIds_TypedArrayMin && type <= TypeIds_TypedArrayMax && !scriptContext->GetThreadContext()->IsScriptActive())
  422. {
  423. // Use external call for typedarray in the debugger.
  424. Var toStringValue = nullptr;
  425. BEGIN_JS_RUNTIME_CALL_EX(scriptContext, false);
  426. toStringValue = ToStringTagHelper(thisArg, scriptContext, type);
  427. END_JS_RUNTIME_CALL(scriptContext);
  428. return toStringValue;
  429. }
  430. // By this point, we should be in the correct context, but the thisArg may still need to be marshalled (for to the implicit ToObject conversion call.)
  431. return ToStringTagHelper(CrossSite::MarshalVar(scriptContext, thisArg), scriptContext, type);
  432. }
  433. // -----------------------------------------------------------
  434. // Object.prototype.valueOf
  435. // 1. Let O be the result of calling ToObject passing the this value as the argument.
  436. // 2. If O is the result of calling the Object constructor with a host object (15.2.2.1), then
  437. // a. Return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined.
  438. // 3. Return O.
  439. // -----------------------------------------------------------
  440. Var JavascriptObject::EntryValueOf(RecyclableObject* function, CallInfo callInfo, ...)
  441. {
  442. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  443. ARGUMENTS(args, callInfo);
  444. ScriptContext* scriptContext = function->GetScriptContext();
  445. Assert(!(callInfo.Flags & CallFlags_New));
  446. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  447. TypeId argType = JavascriptOperators::GetTypeId(args[0]);
  448. // throw a TypeError if TypeId is null or undefined, and apply ToObject to the 'this' value otherwise.
  449. if ((argType == TypeIds_Null) || (argType == TypeIds_Undefined))
  450. {
  451. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.valueOf"));
  452. }
  453. else
  454. {
  455. return JavascriptOperators::ToObject(args[0], scriptContext);
  456. }
  457. }
  458. Var JavascriptObject::EntryGetOwnPropertyDescriptor(RecyclableObject* function, CallInfo callInfo, ...)
  459. {
  460. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  461. ARGUMENTS(args, callInfo);
  462. ScriptContext* scriptContext = function->GetScriptContext();
  463. Assert(!(callInfo.Flags & CallFlags_New));
  464. RecyclableObject* obj = nullptr;
  465. if (args.Info.Count < 2)
  466. {
  467. obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext));
  468. }
  469. else
  470. {
  471. // Convert the argument to object first
  472. obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(args[1], scriptContext));
  473. }
  474. // If the object is HostDispatch try to invoke the operation remotely
  475. if (obj->GetTypeId() == TypeIds_HostDispatch)
  476. {
  477. Var result;
  478. if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptor, args, &result))
  479. {
  480. return result;
  481. }
  482. }
  483. Var propertyKey = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
  484. return JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyKey, scriptContext);
  485. }
  486. Var JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, Var propertyKey, ScriptContext* scriptContext)
  487. {
  488. const PropertyRecord* propertyRecord;
  489. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  490. PropertyId propertyId = propertyRecord->GetPropertyId();
  491. obj->ThrowIfCannotGetOwnPropertyDescriptor(propertyId);
  492. PropertyDescriptor propertyDescriptor;
  493. BOOL isPropertyDescriptorDefined;
  494. isPropertyDescriptorDefined = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor);
  495. if (!isPropertyDescriptorDefined)
  496. {
  497. return scriptContext->GetLibrary()->GetUndefined();
  498. }
  499. return JavascriptOperators::FromPropertyDescriptor(propertyDescriptor, scriptContext);
  500. }
  501. BOOL JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor& propertyDescriptor)
  502. {
  503. BOOL isPropertyDescriptorDefined;
  504. if (obj->CanHaveInterceptors())
  505. {
  506. isPropertyDescriptorDefined = obj->HasOwnProperty(propertyId) ?
  507. JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor) : obj->GetDefaultPropertyDescriptor(propertyDescriptor);
  508. }
  509. else
  510. {
  511. isPropertyDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor) ||
  512. obj->GetDefaultPropertyDescriptor(propertyDescriptor);
  513. }
  514. return isPropertyDescriptorDefined;
  515. }
  516. Var JavascriptObject::EntryGetOwnPropertyDescriptors(RecyclableObject* function, CallInfo callInfo, ...)
  517. {
  518. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  519. ARGUMENTS(args, callInfo);
  520. ScriptContext* scriptContext = function->GetScriptContext();
  521. Assert(!(callInfo.Flags & CallFlags_New));
  522. RecyclableObject* obj = nullptr;
  523. if (args.Info.Count < 2)
  524. {
  525. obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext));
  526. }
  527. else
  528. {
  529. // Convert the argument to object first
  530. obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(args[1], scriptContext));
  531. }
  532. // If the object is HostDispatch try to invoke the operation remotely
  533. if (obj->GetTypeId() == TypeIds_HostDispatch)
  534. {
  535. Var result;
  536. if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptors, args, &result))
  537. {
  538. return result;
  539. }
  540. }
  541. JavascriptArray* ownPropertyKeys = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
  542. RecyclableObject* resultObj = scriptContext->GetLibrary()->CreateObject(true, (Js::PropertyIndex) ownPropertyKeys->GetLength());
  543. PropertyDescriptor propDesc;
  544. Var propKey = nullptr;
  545. for (uint i = 0; i < ownPropertyKeys->GetLength(); i++)
  546. {
  547. BOOL getPropResult = ownPropertyKeys->DirectGetItemAt(i, &propKey);
  548. Assert(getPropResult);
  549. if (!getPropResult)
  550. {
  551. continue;
  552. }
  553. PropertyRecord const * propertyRecord;
  554. JavascriptConversion::ToPropertyKey(propKey, scriptContext, &propertyRecord);
  555. Var newDescriptor = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propKey, scriptContext);
  556. if (!JavascriptOperators::IsUndefined(newDescriptor))
  557. {
  558. resultObj->SetProperty(propertyRecord->GetPropertyId(), newDescriptor, PropertyOperation_None, nullptr);
  559. }
  560. }
  561. return resultObj;
  562. }
  563. Var JavascriptObject::EntryGetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
  564. {
  565. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  566. ARGUMENTS(args, callInfo);
  567. ScriptContext* scriptContext = function->GetScriptContext();
  568. Assert(!(callInfo.Flags & CallFlags_New));
  569. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getPrototypeOf);
  570. // 19.1.2.9
  571. // Object.getPrototypeOf ( O )
  572. // When the getPrototypeOf function is called with argument O, the following steps are taken:
  573. RecyclableObject *object = nullptr;
  574. // 1. Let obj be ToObject(O).
  575. // 2. ReturnIfAbrupt(obj).
  576. if (args.Info.Count < 2 || !JavascriptConversion::ToObject(args[1], scriptContext, &object))
  577. {
  578. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.getPrototypeOf"));
  579. }
  580. // 3. Return obj.[[GetPrototypeOf]]().
  581. return CrossSite::MarshalVar(scriptContext, GetPrototypeOf(object, scriptContext));
  582. }
  583. Var JavascriptObject::EntrySetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
  584. {
  585. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  586. ARGUMENTS(args, callInfo);
  587. Assert(!(callInfo.Flags & CallFlags_New));
  588. ScriptContext* scriptContext = function->GetScriptContext();
  589. // 19.1.2.18
  590. // Object.setPrototypeOf ( O, proto )
  591. // When the setPrototypeOf function is called with arguments O and proto, the following steps are taken:
  592. // 1. Let O be RequireObjectCoercible(O).
  593. // 2. ReturnIfAbrupt(O).
  594. // 3. If Type(proto) is neither Object or Null, then throw a TypeError exception.
  595. int32 errCode = NOERROR;
  596. if (args.Info.Count < 2 || !JavascriptConversion::CheckObjectCoercible(args[1], scriptContext))
  597. {
  598. errCode = JSERR_FunctionArgument_NeedObject;
  599. }
  600. else if (args.Info.Count < 3 || !JavascriptOperators::IsObjectOrNull(args[2]))
  601. {
  602. errCode = JSERR_FunctionArgument_NotObjectOrNull;
  603. }
  604. if (errCode != NOERROR)
  605. {
  606. JavascriptError::ThrowTypeError(scriptContext, errCode, _u("Object.setPrototypeOf"));
  607. }
  608. // 4. If Type(O) is not Object, return O.
  609. if (!JavascriptOperators::IsObject(args[1]))
  610. {
  611. return args[1];
  612. }
  613. #if ENABLE_COPYONACCESS_ARRAY
  614. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
  615. #endif
  616. RecyclableObject* object = RecyclableObject::FromVar(args[1]);
  617. RecyclableObject* newPrototype = RecyclableObject::FromVar(args[2]);
  618. // 5. Let status be O.[[SetPrototypeOf]](proto).
  619. // 6. ReturnIfAbrupt(status).
  620. // 7. If status is false, throw a TypeError exception.
  621. ChangePrototype(object, newPrototype, /*shouldThrow*/true, scriptContext);
  622. // 8. Return O.
  623. return object;
  624. }
  625. Var JavascriptObject::EntrySeal(RecyclableObject* function, CallInfo callInfo, ...)
  626. {
  627. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  628. ARGUMENTS(args, callInfo);
  629. ScriptContext* scriptContext = function->GetScriptContext();
  630. Assert(!(callInfo.Flags & CallFlags_New));
  631. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_seal);
  632. // Spec update in Rev29 under section 19.1.2.17
  633. if (args.Info.Count < 2)
  634. {
  635. return scriptContext->GetLibrary()->GetUndefined();
  636. }
  637. else if (!JavascriptOperators::IsObject(args[1]))
  638. {
  639. return args[1];
  640. }
  641. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  642. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  643. if (globalObject != object && globalObject && (globalObject->ToThis() == object))
  644. {
  645. globalObject->Seal();
  646. }
  647. object->Seal();
  648. return object;
  649. }
  650. Var JavascriptObject::EntryFreeze(RecyclableObject* function, CallInfo callInfo, ...)
  651. {
  652. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  653. ARGUMENTS(args, callInfo);
  654. ScriptContext* scriptContext = function->GetScriptContext();
  655. Assert(!(callInfo.Flags & CallFlags_New));
  656. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_freeze);
  657. // Spec update in Rev29 under section 19.1.2.5
  658. if (args.Info.Count < 2)
  659. {
  660. return scriptContext->GetLibrary()->GetUndefined();
  661. }
  662. else if (!JavascriptOperators::IsObject(args[1]))
  663. {
  664. return args[1];
  665. }
  666. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  667. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  668. if (globalObject != object && globalObject && (globalObject->ToThis() == object))
  669. {
  670. globalObject->Freeze();
  671. }
  672. object->Freeze();
  673. return object;
  674. }
  675. Var JavascriptObject::EntryPreventExtensions(RecyclableObject* function, CallInfo callInfo, ...)
  676. {
  677. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  678. ARGUMENTS(args, callInfo);
  679. ScriptContext* scriptContext = function->GetScriptContext();
  680. Assert(!(callInfo.Flags & CallFlags_New));
  681. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_preventExtensions);
  682. // Spec update in Rev29 under section 19.1.2.15
  683. if (args.Info.Count < 2)
  684. {
  685. return scriptContext->GetLibrary()->GetUndefined();
  686. }
  687. else if (!JavascriptOperators::IsObject(args[1]))
  688. {
  689. return args[1];
  690. }
  691. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  692. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  693. if (globalObject != object && globalObject && (globalObject->ToThis() == object))
  694. {
  695. globalObject->PreventExtensions();
  696. }
  697. object->PreventExtensions();
  698. return object;
  699. }
  700. Var JavascriptObject::EntryIsSealed(RecyclableObject* function, CallInfo callInfo, ...)
  701. {
  702. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  703. ARGUMENTS(args, callInfo);
  704. ScriptContext* scriptContext = function->GetScriptContext();
  705. Assert(!(callInfo.Flags & CallFlags_New));
  706. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isSealed);
  707. if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
  708. {
  709. return scriptContext->GetLibrary()->GetTrue();
  710. }
  711. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  712. BOOL isSealed = object->IsSealed();
  713. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  714. if (isSealed && globalObject != object && globalObject && (globalObject->ToThis() == object))
  715. {
  716. isSealed = globalObject->IsSealed();
  717. }
  718. return scriptContext->GetLibrary()->GetTrueOrFalse(isSealed);
  719. }
  720. Var JavascriptObject::EntryIsFrozen(RecyclableObject* function, CallInfo callInfo, ...)
  721. {
  722. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  723. ARGUMENTS(args, callInfo);
  724. ScriptContext* scriptContext = function->GetScriptContext();
  725. Assert(!(callInfo.Flags & CallFlags_New));
  726. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isFrozen);
  727. if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
  728. {
  729. return scriptContext->GetLibrary()->GetTrue();
  730. }
  731. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  732. BOOL isFrozen = object->IsFrozen();
  733. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  734. if (isFrozen && globalObject != object && globalObject && (globalObject->ToThis() == object))
  735. {
  736. isFrozen = globalObject->IsFrozen();
  737. }
  738. return scriptContext->GetLibrary()->GetTrueOrFalse(isFrozen);
  739. }
  740. Var JavascriptObject::EntryIsExtensible(RecyclableObject* function, CallInfo callInfo, ...)
  741. {
  742. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  743. ARGUMENTS(args, callInfo);
  744. ScriptContext* scriptContext = function->GetScriptContext();
  745. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isExtensible);
  746. Assert(!(callInfo.Flags & CallFlags_New));
  747. if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
  748. {
  749. return scriptContext->GetLibrary()->GetFalse();
  750. }
  751. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  752. BOOL isExtensible = object->IsExtensible();
  753. GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
  754. if (isExtensible && globalObject != object && globalObject && (globalObject->ToThis() == object))
  755. {
  756. isExtensible = globalObject->IsExtensible();
  757. }
  758. return scriptContext->GetLibrary()->GetTrueOrFalse(isExtensible);
  759. }
  760. Var JavascriptObject::EntryGetOwnPropertyNames(RecyclableObject* function, CallInfo callInfo, ...)
  761. {
  762. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  763. ARGUMENTS(args, callInfo);
  764. ScriptContext* scriptContext = function->GetScriptContext();
  765. Assert(!(callInfo.Flags & CallFlags_New));
  766. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertyNames);
  767. Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
  768. RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
  769. if (object->GetTypeId() == TypeIds_HostDispatch)
  770. {
  771. Var result;
  772. if (object->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyNames, args, &result))
  773. {
  774. return result;
  775. }
  776. }
  777. return JavascriptOperators::GetOwnPropertyNames(object, scriptContext);
  778. }
  779. Var JavascriptObject::EntryGetOwnPropertySymbols(RecyclableObject* function, CallInfo callInfo, ...)
  780. {
  781. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  782. ARGUMENTS(args, callInfo);
  783. ScriptContext* scriptContext = function->GetScriptContext();
  784. Assert(!(callInfo.Flags & CallFlags_New));
  785. Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
  786. RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
  787. if (object->GetTypeId() == TypeIds_HostDispatch)
  788. {
  789. Var result;
  790. if (object->InvokeBuiltInOperationRemotely(EntryGetOwnPropertySymbols, args, &result))
  791. {
  792. return result;
  793. }
  794. }
  795. return JavascriptOperators::GetOwnPropertySymbols(object, scriptContext);
  796. }
  797. Var JavascriptObject::EntryKeys(RecyclableObject* function, CallInfo callInfo, ...)
  798. {
  799. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  800. ARGUMENTS(args, callInfo);
  801. ScriptContext* scriptContext = function->GetScriptContext();
  802. Assert(!(callInfo.Flags & CallFlags_New));
  803. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_keys);
  804. Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
  805. RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
  806. if (object->GetTypeId() == TypeIds_HostDispatch)
  807. {
  808. Var result;
  809. if (object->InvokeBuiltInOperationRemotely(EntryKeys, args, &result))
  810. {
  811. return result;
  812. }
  813. }
  814. return JavascriptOperators::GetOwnEnumerablePropertyNames(object, scriptContext);
  815. }
  816. Var JavascriptObject::GetValuesOrEntries(RecyclableObject* object, bool valuesToReturn, ScriptContext* scriptContext)
  817. {
  818. Assert(object != nullptr);
  819. Assert(scriptContext != nullptr);
  820. JavascriptArray* valuesArray = scriptContext->GetLibrary()->CreateArray(0);
  821. JavascriptArray* ownKeysResult = JavascriptOperators::GetOwnPropertyNames(object, scriptContext);
  822. uint32 length = ownKeysResult->GetLength();
  823. Var nextKey;
  824. const PropertyRecord* propertyRecord = nullptr;
  825. PropertyId propertyId;
  826. for (uint32 i = 0, index = 0; i < length; i++)
  827. {
  828. nextKey = ownKeysResult->DirectGetItem(i);
  829. Assert(JavascriptString::Is(nextKey));
  830. PropertyDescriptor propertyDescriptor;
  831. JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
  832. propertyId = propertyRecord->GetPropertyId();
  833. Assert(propertyId != Constants::NoProperty);
  834. if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyId, scriptContext, &propertyDescriptor))
  835. {
  836. if (propertyDescriptor.IsEnumerable())
  837. {
  838. Var value = JavascriptOperators::GetProperty(object, propertyId, scriptContext);
  839. if (!valuesToReturn)
  840. {
  841. // For Object.entries each entry is key, value pair
  842. JavascriptArray* entry = scriptContext->GetLibrary()->CreateArray(2);
  843. entry->DirectSetItemAt(0, CrossSite::MarshalVar(scriptContext, nextKey));
  844. entry->DirectSetItemAt(1, CrossSite::MarshalVar(scriptContext, value));
  845. value = entry;
  846. }
  847. valuesArray->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, value));
  848. }
  849. }
  850. }
  851. return valuesArray;
  852. }
  853. Var JavascriptObject::EntryValues(RecyclableObject* function, CallInfo callInfo, ...)
  854. {
  855. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  856. ARGUMENTS(args, callInfo);
  857. ScriptContext* scriptContext = function->GetScriptContext();
  858. Assert(!(callInfo.Flags & CallFlags_New));
  859. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_values);
  860. Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
  861. RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
  862. return GetValuesOrEntries(object, true /*valuesToReturn*/, scriptContext);
  863. }
  864. Var JavascriptObject::EntryEntries(RecyclableObject* function, CallInfo callInfo, ...)
  865. {
  866. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  867. ARGUMENTS(args, callInfo);
  868. ScriptContext* scriptContext = function->GetScriptContext();
  869. Assert(!(callInfo.Flags & CallFlags_New));
  870. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_entries);
  871. Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
  872. RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
  873. return GetValuesOrEntries(object, false /*valuesToReturn*/, scriptContext);
  874. }
  875. JavascriptArray* JavascriptObject::CreateOwnSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
  876. {
  877. return CreateKeysHelper(object, scriptContext, TRUE, true /*includeSymbolsOnly */, false, true /*includeSpecialProperties*/);
  878. }
  879. JavascriptArray* JavascriptObject::CreateOwnStringPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
  880. {
  881. return CreateKeysHelper(object, scriptContext, TRUE, false, true /*includeStringsOnly*/, true /*includeSpecialProperties*/);
  882. }
  883. JavascriptArray* JavascriptObject::CreateOwnStringSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
  884. {
  885. return CreateKeysHelper(object, scriptContext, TRUE, true/*includeSymbolsOnly*/, true /*includeStringsOnly*/, true /*includeSpecialProperties*/);
  886. }
  887. JavascriptArray* JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
  888. {
  889. return CreateKeysHelper(object, scriptContext, FALSE, false, true/*includeStringsOnly*/, false);
  890. }
  891. JavascriptArray* JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
  892. {
  893. return CreateKeysHelper(object, scriptContext, FALSE, true/*includeSymbolsOnly*/, true/*includeStringsOnly*/, false);
  894. }
  895. // 9.1.12 [[OwnPropertyKeys]] () in RC#4 dated April 3rd 2015.
  896. JavascriptArray* JavascriptObject::CreateKeysHelper(RecyclableObject* object, ScriptContext* scriptContext, BOOL includeNonEnumerable, bool includeSymbolProperties, bool includeStringProperties, bool includeSpecialProperties)
  897. {
  898. //1. Let keys be a new empty List.
  899. //2. For each own property key P of O that is an integer index, in ascending numeric index order
  900. // a. Add P as the last element of keys.
  901. //3. For each own property key P of O that is a String but is not an integer index, in property creation order
  902. // a. Add P as the last element of keys.
  903. //4. For each own property key P of O that is a Symbol, in property creation order
  904. // a. Add P as the last element of keys.
  905. //5. Return keys.
  906. AssertMsg(includeStringProperties || includeSymbolProperties, "Should either get string or symbol properties.");
  907. JavascriptStaticEnumerator enumerator;
  908. JavascriptArray* newArr = scriptContext->GetLibrary()->CreateArray(0);
  909. JavascriptArray* newArrForSymbols = scriptContext->GetLibrary()->CreateArray(0);
  910. EnumeratorFlags flags = EnumeratorFlags::None;
  911. if (includeNonEnumerable)
  912. {
  913. flags |= EnumeratorFlags::EnumNonEnumerable;
  914. }
  915. if (includeSymbolProperties)
  916. {
  917. flags |= EnumeratorFlags::EnumSymbols;
  918. }
  919. if (!object->GetEnumerator(&enumerator, flags, scriptContext))
  920. {
  921. return newArr; // Return an empty array if we don't have an enumerator
  922. }
  923. RecyclableObject *undefined = scriptContext->GetLibrary()->GetUndefined();
  924. Var propertyName = nullptr;
  925. PropertyId propertyId;
  926. uint32 propertyIndex = 0;
  927. uint32 symbolIndex = 0;
  928. const PropertyRecord* propertyRecord;
  929. JavascriptSymbol* symbol;
  930. while ((propertyName = enumerator.MoveAndGetNext(propertyId)) != NULL)
  931. {
  932. if (!JavascriptOperators::IsUndefinedObject(propertyName, undefined)) //There are some code paths in which GetCurrentIndex can return undefined
  933. {
  934. if (includeSymbolProperties)
  935. {
  936. propertyRecord = scriptContext->GetPropertyName(propertyId);
  937. if (propertyRecord->IsSymbol())
  938. {
  939. symbol = scriptContext->GetLibrary()->CreateSymbol(propertyRecord);
  940. // no need to marshal symbol because it is created from scriptContext
  941. newArrForSymbols->DirectSetItemAt(symbolIndex++, symbol);
  942. continue;
  943. }
  944. }
  945. if (includeStringProperties)
  946. {
  947. newArr->DirectSetItemAt(propertyIndex++, CrossSite::MarshalVar(scriptContext, propertyName));
  948. }
  949. }
  950. }
  951. // Special properties
  952. if (includeSpecialProperties && includeStringProperties)
  953. {
  954. uint32 index = 0;
  955. while (object->GetSpecialPropertyName(index, &propertyName, scriptContext))
  956. {
  957. if (!JavascriptOperators::IsUndefinedObject(propertyName, undefined))
  958. {
  959. newArr->DirectSetItemAt(propertyIndex++, propertyName);
  960. }
  961. index++;
  962. }
  963. }
  964. // Append all the symbols at the end of list
  965. uint32 totalSymbols = newArrForSymbols->GetLength();
  966. for (uint32 symIndex = 0; symIndex < totalSymbols; symIndex++)
  967. {
  968. newArr->DirectSetItemAt(propertyIndex++, newArrForSymbols->DirectGetItem(symIndex));
  969. }
  970. return newArr;
  971. }
  972. // args[1] this object to operate on.
  973. // args[2] property name.
  974. // args[3] object that attributes for the new descriptor.
  975. Var JavascriptObject::EntryDefineProperty(RecyclableObject* function, CallInfo callInfo, ...)
  976. {
  977. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  978. ARGUMENTS(args, callInfo);
  979. ScriptContext* scriptContext = function->GetScriptContext();
  980. Assert(!(callInfo.Flags & CallFlags_New));
  981. if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
  982. {
  983. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.defineProperty"));
  984. }
  985. #if ENABLE_COPYONACCESS_ARRAY
  986. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
  987. #endif
  988. RecyclableObject* obj = RecyclableObject::FromVar(args[1]);
  989. // If the object is HostDispatch try to invoke the operation remotely
  990. if (obj->GetTypeId() == TypeIds_HostDispatch)
  991. {
  992. if (obj->InvokeBuiltInOperationRemotely(EntryDefineProperty, args, NULL))
  993. {
  994. return obj;
  995. }
  996. }
  997. Var propertyKey = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
  998. PropertyRecord const * propertyRecord;
  999. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  1000. Var descVar = args.Info.Count > 3 ? args[3] : obj->GetLibrary()->GetUndefined();
  1001. PropertyDescriptor propertyDescriptor;
  1002. if (!JavascriptOperators::ToPropertyDescriptor(descVar, &propertyDescriptor, scriptContext))
  1003. {
  1004. JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propertyRecord->GetPropertyId())->GetBuffer());
  1005. }
  1006. if (CONFIG_FLAG(UseFullName))
  1007. {
  1008. ModifyGetterSetterFuncName(propertyRecord, propertyDescriptor, scriptContext);
  1009. }
  1010. DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
  1011. return obj;
  1012. }
  1013. Var JavascriptObject::EntryDefineProperties(RecyclableObject* function, CallInfo callInfo, ...)
  1014. {
  1015. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1016. ARGUMENTS(args, callInfo);
  1017. ScriptContext* scriptContext = function->GetScriptContext();
  1018. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_defineProperties);
  1019. Assert(!(callInfo.Flags & CallFlags_New));
  1020. if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
  1021. {
  1022. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.defineProperties"));
  1023. }
  1024. #if ENABLE_COPYONACCESS_ARRAY
  1025. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
  1026. #endif
  1027. RecyclableObject *object = RecyclableObject::FromVar(args[1]);
  1028. // If the object is HostDispatch try to invoke the operation remotely
  1029. if (object->GetTypeId() == TypeIds_HostDispatch)
  1030. {
  1031. if (object->InvokeBuiltInOperationRemotely(EntryDefineProperties, args, NULL))
  1032. {
  1033. return object;
  1034. }
  1035. }
  1036. Var propertiesVar = args.Info.Count > 2 ? args[2] : object->GetLibrary()->GetUndefined();
  1037. RecyclableObject* properties = nullptr;
  1038. if (FALSE == JavascriptConversion::ToObject(propertiesVar, scriptContext, &properties))
  1039. {
  1040. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NullOrUndefined, _u("Object.defineProperties"));
  1041. }
  1042. return DefinePropertiesHelper(object, properties, scriptContext);
  1043. }
  1044. // args[1] property name.
  1045. // args[2] function object to use as the getter function.
  1046. Var JavascriptObject::EntryDefineGetter(RecyclableObject* function, CallInfo callInfo, ...)
  1047. {
  1048. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1049. ARGUMENTS(args, callInfo);
  1050. ScriptContext* scriptContext = function->GetScriptContext();
  1051. Assert(!(callInfo.Flags & CallFlags_New));
  1052. // For browser interop, simulate LdThis by calling OP implementation directly.
  1053. // Do not have module id here so use the global id, 0.
  1054. //
  1055. #if ENABLE_COPYONACCESS_ARRAY
  1056. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[0]);
  1057. #endif
  1058. Var thisArg = JavascriptOperators::OP_GetThisNoFastPath(args[0], 0, scriptContext);
  1059. RecyclableObject* obj = RecyclableObject::FromVar(thisArg);
  1060. Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
  1061. const PropertyRecord* propertyRecord;
  1062. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  1063. Var getterFunc = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
  1064. if (!JavascriptConversion::IsCallable(getterFunc))
  1065. {
  1066. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.__defineGetter__"));
  1067. }
  1068. PropertyDescriptor propertyDescriptor;
  1069. propertyDescriptor.SetEnumerable(true);
  1070. propertyDescriptor.SetConfigurable(true);
  1071. propertyDescriptor.SetGetter(getterFunc);
  1072. DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
  1073. return obj->GetLibrary()->GetUndefined();
  1074. }
  1075. // args[1] property name.
  1076. // args[2] function object to use as the setter function.
  1077. Var JavascriptObject::EntryDefineSetter(RecyclableObject* function, CallInfo callInfo, ...)
  1078. {
  1079. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1080. ARGUMENTS(args, callInfo);
  1081. ScriptContext* scriptContext = function->GetScriptContext();
  1082. Assert(!(callInfo.Flags & CallFlags_New));
  1083. // For browser interop, simulate LdThis by calling OP implementation directly.
  1084. // Do not have module id here so use the global id, 0.
  1085. //
  1086. Var thisArg = JavascriptOperators::OP_GetThisNoFastPath(args[0], 0, scriptContext);
  1087. RecyclableObject* obj = RecyclableObject::FromVar(thisArg);
  1088. Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
  1089. const PropertyRecord* propertyRecord;
  1090. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  1091. Var setterFunc = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
  1092. if (!JavascriptConversion::IsCallable(setterFunc))
  1093. {
  1094. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.__defineSetter__"));
  1095. }
  1096. PropertyDescriptor propertyDescriptor;
  1097. propertyDescriptor.SetEnumerable(true);
  1098. propertyDescriptor.SetConfigurable(true);
  1099. propertyDescriptor.SetSetter(setterFunc);
  1100. DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
  1101. return obj->GetLibrary()->GetUndefined();
  1102. }
  1103. // args[1] property name.
  1104. Var JavascriptObject::EntryLookupGetter(RecyclableObject* function, CallInfo callInfo, ...)
  1105. {
  1106. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1107. ARGUMENTS(args, callInfo);
  1108. ScriptContext* scriptContext = function->GetScriptContext();
  1109. Assert(!(callInfo.Flags & CallFlags_New));
  1110. RecyclableObject* obj = nullptr;
  1111. if (!JavascriptConversion::ToObject(args[0], scriptContext, &obj))
  1112. {
  1113. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.__lookupGetter__"));
  1114. }
  1115. Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
  1116. const PropertyRecord* propertyRecord;
  1117. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  1118. Var getter = nullptr;
  1119. Var unused = nullptr;
  1120. if (JavascriptOperators::GetAccessors(obj, propertyRecord->GetPropertyId(), scriptContext, &getter, &unused))
  1121. {
  1122. if (getter != nullptr)
  1123. {
  1124. return getter;
  1125. }
  1126. }
  1127. return obj->GetLibrary()->GetUndefined();
  1128. }
  1129. // args[1] property name.
  1130. Var JavascriptObject::EntryLookupSetter(RecyclableObject* function, CallInfo callInfo, ...)
  1131. {
  1132. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1133. ARGUMENTS(args, callInfo);
  1134. ScriptContext* scriptContext = function->GetScriptContext();
  1135. Assert(!(callInfo.Flags & CallFlags_New));
  1136. RecyclableObject* obj = nullptr;
  1137. if (!JavascriptConversion::ToObject(args[0], scriptContext, &obj))
  1138. {
  1139. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.__lookupSetter__"));
  1140. }
  1141. Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
  1142. const PropertyRecord* propertyRecord;
  1143. JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
  1144. Var unused = nullptr;
  1145. Var setter = nullptr;
  1146. if (JavascriptOperators::GetAccessors(obj, propertyRecord->GetPropertyId(), scriptContext, &unused, &setter))
  1147. {
  1148. if (setter != nullptr)
  1149. {
  1150. return setter;
  1151. }
  1152. }
  1153. return obj->GetLibrary()->GetUndefined();
  1154. }
  1155. Var JavascriptObject::EntryIs(RecyclableObject* function, CallInfo callInfo, ...)
  1156. {
  1157. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1158. ARGUMENTS(args, callInfo);
  1159. ScriptContext* scriptContext = function->GetScriptContext();
  1160. Assert(!(callInfo.Flags & CallFlags_New));
  1161. Var x = args.Info.Count > 1 ? args[1] : scriptContext->GetLibrary()->GetUndefined();
  1162. Var y = args.Info.Count > 2 ? args[2] : scriptContext->GetLibrary()->GetUndefined();
  1163. return JavascriptBoolean::ToVar(JavascriptConversion::SameValue(x, y), scriptContext);
  1164. }
  1165. //ES6 19.1.2.1
  1166. Var JavascriptObject::EntryAssign(RecyclableObject* function, CallInfo callInfo, ...)
  1167. {
  1168. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1169. ARGUMENTS(args, callInfo);
  1170. ScriptContext* scriptContext = function->GetScriptContext();
  1171. Assert(!(callInfo.Flags & CallFlags_New));
  1172. // 1. Let to be ToObject(target).
  1173. // 2. ReturnIfAbrupt(to).
  1174. // 3 If only one argument was passed, return to.
  1175. RecyclableObject* to = nullptr;
  1176. if (args.Info.Count == 1 || !JavascriptConversion::ToObject(args[1], scriptContext, &to))
  1177. {
  1178. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.assign"));
  1179. }
  1180. if (args.Info.Count < 3)
  1181. {
  1182. return to;
  1183. }
  1184. // 4. Let sources be the List of argument values starting with the second argument.
  1185. // 5. For each element nextSource of sources, in ascending index order,
  1186. for (unsigned int i = 2; i < args.Info.Count; i++)
  1187. {
  1188. // a. If nextSource is undefined or null, let keys be an empty List.
  1189. // b. Else,
  1190. // i.Let from be ToObject(nextSource).
  1191. // ii.ReturnIfAbrupt(from).
  1192. // iii.Let keys be from.[[OwnPropertyKeys]]().
  1193. // iv.ReturnIfAbrupt(keys).
  1194. if (JavascriptOperators::IsUndefinedOrNull(args[i]))
  1195. {
  1196. continue;
  1197. }
  1198. RecyclableObject* from = nullptr;
  1199. if (!JavascriptConversion::ToObject(args[i], scriptContext, &from))
  1200. {
  1201. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.assign"));
  1202. }
  1203. #if ENABLE_COPYONACCESS_ARRAY
  1204. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(from);
  1205. #endif
  1206. // if proxy, take slow path by calling [[OwnPropertyKeys]] on source
  1207. if (JavascriptProxy::Is(from))
  1208. {
  1209. AssignForProxyObjects(from, to, scriptContext);
  1210. }
  1211. // else use enumerator to extract keys from source
  1212. else
  1213. {
  1214. AssignForGenericObjects(from, to, scriptContext);
  1215. }
  1216. }
  1217. // 6. Return to.
  1218. return to;
  1219. }
  1220. void JavascriptObject::AssignForGenericObjects(RecyclableObject* from, RecyclableObject* to, ScriptContext* scriptContext)
  1221. {
  1222. JavascriptStaticEnumerator enumerator;
  1223. if (!from->GetEnumerator(&enumerator, EnumeratorFlags::SnapShotSemantics | EnumeratorFlags::EnumSymbols, scriptContext))
  1224. {
  1225. //nothing to enumerate, continue with the nextSource.
  1226. return;
  1227. }
  1228. PropertyId nextKey = Constants::NoProperty;
  1229. Var propValue = nullptr;
  1230. Var propertyVar = nullptr;
  1231. //enumerate through each property of properties and fetch the property descriptor
  1232. while ((propertyVar = enumerator.MoveAndGetNext(nextKey)) != NULL)
  1233. {
  1234. if (nextKey == Constants::NoProperty)
  1235. {
  1236. if (JavascriptOperators::IsUndefinedObject(propertyVar)) //There are some code paths in which GetCurrentIndex can return undefined
  1237. {
  1238. continue;
  1239. }
  1240. PropertyRecord const * propertyRecord = nullptr;
  1241. JavascriptString* propertyName = JavascriptString::FromVar(propertyVar);
  1242. scriptContext->GetOrAddPropertyRecord(propertyName->GetString(), propertyName->GetLength(), &propertyRecord);
  1243. nextKey = propertyRecord->GetPropertyId();
  1244. }
  1245. if (!JavascriptOperators::GetOwnProperty(from, nextKey, &propValue, scriptContext))
  1246. {
  1247. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
  1248. }
  1249. if (!JavascriptOperators::SetProperty(to, to, nextKey, propValue, scriptContext, PropertyOperationFlags::PropertyOperation_ThrowIfNonWritable))
  1250. {
  1251. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
  1252. }
  1253. }
  1254. }
  1255. void JavascriptObject::AssignForProxyObjects(RecyclableObject* from, RecyclableObject* to, ScriptContext* scriptContext)
  1256. {
  1257. JavascriptArray *keys = JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(from, scriptContext);
  1258. // c. Repeat for each element nextKey of keys in List order,
  1259. // i. Let desc be from.[[GetOwnProperty]](nextKey).
  1260. // ii. ReturnIfAbrupt(desc).
  1261. // iii. if desc is not undefined and desc.[[Enumerable]] is true, then
  1262. // 1. Let propValue be Get(from, nextKey).
  1263. // 2. ReturnIfAbrupt(propValue).
  1264. // 3. Let status be Set(to, nextKey, propValue, true);
  1265. // 4. ReturnIfAbrupt(status).
  1266. uint32 length = keys->GetLength();
  1267. Var nextKey;
  1268. const PropertyRecord* propertyRecord = nullptr;
  1269. PropertyId propertyId;
  1270. Var propValue = nullptr;
  1271. for (uint32 j = 0; j < length; j++)
  1272. {
  1273. PropertyDescriptor propertyDescriptor;
  1274. nextKey = keys->DirectGetItem(j);
  1275. AssertMsg(JavascriptSymbol::Is(nextKey) || JavascriptString::Is(nextKey), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  1276. // Spec doesn't strictly call for us to use ToPropertyKey but since we know nextKey is already a symbol or string primitive, ToPropertyKey will be a nop and return us the propertyRecord
  1277. JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
  1278. propertyId = propertyRecord->GetPropertyId();
  1279. AssertMsg(propertyId != Constants::NoProperty, "AssignForProxyObjects - OwnPropertyKeys returned a propertyId with value NoProperty.");
  1280. if (JavascriptOperators::GetOwnPropertyDescriptor(from, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1281. {
  1282. if (propertyDescriptor.IsEnumerable())
  1283. {
  1284. if (!JavascriptOperators::GetOwnProperty(from, propertyId, &propValue, scriptContext))
  1285. {
  1286. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
  1287. }
  1288. if (!JavascriptOperators::SetProperty(to, to, propertyId, propValue, scriptContext))
  1289. {
  1290. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
  1291. }
  1292. }
  1293. }
  1294. }
  1295. }
  1296. //ES5 15.2.3.5
  1297. Var JavascriptObject::EntryCreate(RecyclableObject* function, CallInfo callInfo, ...)
  1298. {
  1299. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  1300. ARGUMENTS(args, callInfo);
  1301. ScriptContext* scriptContext = function->GetScriptContext();
  1302. Recycler *recycler = scriptContext->GetRecycler();
  1303. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_create)
  1304. Assert(!(callInfo.Flags & CallFlags_New));
  1305. if (args.Info.Count < 2)
  1306. {
  1307. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
  1308. }
  1309. TypeId typeId = JavascriptOperators::GetTypeId(args[1]);
  1310. if (typeId != TypeIds_Null && !JavascriptOperators::IsObjectType(typeId))
  1311. {
  1312. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
  1313. }
  1314. //Create a new DynamicType with first argument as prototype and non shared type
  1315. RecyclableObject *prototype = RecyclableObject::FromVar(args[1]);
  1316. DynamicType *objectType = DynamicType::New(scriptContext, TypeIds_Object, prototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), false);
  1317. //Create a new Object using this type.
  1318. DynamicObject* object = DynamicObject::New(recycler, objectType);
  1319. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  1320. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1321. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  1322. {
  1323. object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
  1324. }
  1325. #endif
  1326. if (args.Info.Count > 2 && JavascriptOperators::GetTypeId(args[2]) != TypeIds_Undefined)
  1327. {
  1328. RecyclableObject* properties = nullptr;
  1329. if (FALSE == JavascriptConversion::ToObject(args[2], scriptContext, &properties))
  1330. {
  1331. JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NullOrUndefined, _u("Object.create"));
  1332. }
  1333. return DefinePropertiesHelper(object, properties, scriptContext);
  1334. }
  1335. return object;
  1336. }
  1337. Var JavascriptObject::DefinePropertiesHelper(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
  1338. {
  1339. if (JavascriptProxy::Is(props))
  1340. {
  1341. return DefinePropertiesHelperForProxyObjects(object, props, scriptContext);
  1342. }
  1343. else
  1344. {
  1345. return DefinePropertiesHelperForGenericObjects(object, props, scriptContext);
  1346. }
  1347. }
  1348. Var JavascriptObject::DefinePropertiesHelperForGenericObjects(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
  1349. {
  1350. size_t descSize = 16;
  1351. size_t descCount = 0;
  1352. struct DescriptorMap
  1353. {
  1354. Field(PropertyRecord const *) propRecord;
  1355. Field(PropertyDescriptor) descriptor;
  1356. Field(Var) originalVar;
  1357. };
  1358. JavascriptStaticEnumerator enumerator;
  1359. if (!props->GetEnumerator(&enumerator, EnumeratorFlags::EnumSymbols, scriptContext))
  1360. {
  1361. return object;
  1362. }
  1363. ENTER_PINNED_SCOPE(DescriptorMap, descriptors);
  1364. descriptors = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, descSize);
  1365. PropertyId propId;
  1366. PropertyRecord const * propertyRecord;
  1367. JavascriptString* propertyName = nullptr;
  1368. RecyclableObject *undefined = scriptContext->GetLibrary()->GetUndefined();
  1369. Var tempVar;
  1370. //enumerate through each property of properties and fetch the property descriptor
  1371. while ((tempVar = enumerator.MoveAndGetNext(propId)) != NULL)
  1372. {
  1373. if (propId == Constants::NoProperty) //try current property id query first
  1374. {
  1375. if (!JavascriptOperators::IsUndefinedObject(tempVar, undefined)) //There are some enumerators returning propertyName but not propId
  1376. {
  1377. propertyName = JavascriptString::FromVar(tempVar);
  1378. scriptContext->GetOrAddPropertyRecord(propertyName->GetString(), propertyName->GetLength(), &propertyRecord);
  1379. propId = propertyRecord->GetPropertyId();
  1380. }
  1381. else
  1382. {
  1383. continue;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. propertyRecord = scriptContext->GetPropertyName(propId);
  1389. }
  1390. if (descCount == descSize)
  1391. {
  1392. //reallocate - consider linked list of DescriptorMap if the descSize is too high
  1393. descSize = AllocSizeMath::Mul(descCount, 2);
  1394. __analysis_assume(descSize == descCount * 2);
  1395. DescriptorMap *temp = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, descSize);
  1396. for (size_t i = 0; i < descCount; i++)
  1397. {
  1398. temp[i] = descriptors[i];
  1399. }
  1400. descriptors = temp;
  1401. }
  1402. tempVar = JavascriptOperators::GetProperty(props, propId, scriptContext);
  1403. AnalysisAssert(descCount < descSize);
  1404. if (!JavascriptOperators::ToPropertyDescriptor(tempVar, &descriptors[descCount].descriptor, scriptContext))
  1405. {
  1406. JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propId)->GetBuffer());
  1407. }
  1408. // In proxy, we need to get back the original ToPropertDescriptor var in [[defineProperty]] trap.
  1409. descriptors[descCount].originalVar = tempVar;
  1410. if (CONFIG_FLAG(UseFullName))
  1411. {
  1412. ModifyGetterSetterFuncName(propertyRecord, descriptors[descCount].descriptor, scriptContext);
  1413. }
  1414. descriptors[descCount].propRecord = propertyRecord;
  1415. descCount++;
  1416. }
  1417. //Once all the property descriptors are in place set each property descriptor to the object
  1418. for (size_t i = 0; i < descCount; i++)
  1419. {
  1420. DefineOwnPropertyHelper(object, descriptors[i].propRecord->GetPropertyId(), descriptors[i].descriptor, scriptContext);
  1421. }
  1422. LEAVE_PINNED_SCOPE();
  1423. return object;
  1424. }
  1425. //ES5 15.2.3.7
  1426. Var JavascriptObject::DefinePropertiesHelperForProxyObjects(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
  1427. {
  1428. Assert(JavascriptProxy::Is(props));
  1429. //1. If Type(O) is not Object throw a TypeError exception.
  1430. //2. Let props be ToObject(Properties).
  1431. size_t descCount = 0;
  1432. struct DescriptorMap
  1433. {
  1434. Field(PropertyRecord const *) propRecord;
  1435. Field(PropertyDescriptor) descriptor;
  1436. };
  1437. //3. Let keys be props.[[OwnPropertyKeys]]().
  1438. //4. ReturnIfAbrupt(keys).
  1439. //5. Let descriptors be an empty List.
  1440. JavascriptArray* keys = JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(props, scriptContext);
  1441. uint32 length = keys->GetLength();
  1442. ENTER_PINNED_SCOPE(DescriptorMap, descriptors);
  1443. descriptors = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, length);
  1444. //6. Repeat for each element nextKey of keys in List order,
  1445. // 1. Let propDesc be props.[[GetOwnProperty]](nextKey).
  1446. // 2. ReturnIfAbrupt(propDesc).
  1447. // 3. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
  1448. // 1. Let descObj be Get(props, nextKey).
  1449. // 2. ReturnIfAbrupt(descObj).
  1450. // 3. Let desc be ToPropertyDescriptor(descObj).
  1451. // 4. ReturnIfAbrupt(desc).
  1452. // 5. Append the pair(a two element List) consisting of nextKey and desc to the end of descriptors.
  1453. Var nextKey;
  1454. const PropertyRecord* propertyRecord = nullptr;
  1455. PropertyId propertyId;
  1456. Var descObj;
  1457. for (uint32 j = 0; j < length; j++)
  1458. {
  1459. PropertyDescriptor propertyDescriptor;
  1460. nextKey = keys->DirectGetItem(j);
  1461. AssertMsg(JavascriptSymbol::Is(nextKey) || JavascriptString::Is(nextKey), "Invariant check during ownKeys proxy trap should make sure we only get property key here. (symbol or string primitives)");
  1462. JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
  1463. propertyId = propertyRecord->GetPropertyId();
  1464. AssertMsg(propertyId != Constants::NoProperty, "DefinePropertiesHelper - OwnPropertyKeys returned a propertyId with value NoProperty.");
  1465. if (JavascriptOperators::GetOwnPropertyDescriptor(props, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1466. {
  1467. if (propertyDescriptor.IsEnumerable())
  1468. {
  1469. descObj = JavascriptOperators::GetProperty(props, propertyId, scriptContext);
  1470. if (!JavascriptOperators::ToPropertyDescriptor(descObj, &descriptors[descCount].descriptor, scriptContext))
  1471. {
  1472. JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  1473. }
  1474. if (CONFIG_FLAG(UseFullName))
  1475. {
  1476. ModifyGetterSetterFuncName(propertyRecord, descriptors[descCount].descriptor, scriptContext);
  1477. }
  1478. descriptors[descCount].propRecord = propertyRecord;
  1479. descCount++;
  1480. }
  1481. }
  1482. }
  1483. //7. For each pair from descriptors in list order,
  1484. // 1. Let P be the first element of pair.
  1485. // 2. Let desc be the second element of pair.
  1486. // 3. Let status be DefinePropertyOrThrow(O, P, desc).
  1487. // 4. ReturnIfAbrupt(status).
  1488. for (size_t i = 0; i < descCount; i++)
  1489. {
  1490. DefineOwnPropertyHelper(object, descriptors[i].propRecord->GetPropertyId(), descriptors[i].descriptor, scriptContext);
  1491. }
  1492. LEAVE_PINNED_SCOPE();
  1493. //8. Return O.
  1494. return object;
  1495. }
  1496. Var JavascriptObject::GetPrototypeOf(RecyclableObject* obj, ScriptContext* scriptContext)
  1497. {
  1498. return obj->IsExternal() ? obj->GetConfigurablePrototype(scriptContext) : obj->GetPrototype();
  1499. }
  1500. //
  1501. // Check if "proto" is a prototype of "object" (on its prototype chain).
  1502. //
  1503. bool JavascriptObject::IsPrototypeOf(RecyclableObject* proto, RecyclableObject* object, ScriptContext* scriptContext)
  1504. {
  1505. return JavascriptOperators::MapObjectAndPrototypesUntil<false>(object, [=](RecyclableObject* obj)
  1506. {
  1507. return obj == proto;
  1508. });
  1509. }
  1510. static const size_t ConstructNameGetSetLength = 5; // 5 = 1 ( for .) + 3 (get or set) + 1 for null)
  1511. /*static*/
  1512. char16 * JavascriptObject::ConstructName(const PropertyRecord * propertyRecord, const char16 * getOrSetStr, ScriptContext* scriptContext)
  1513. {
  1514. Assert(propertyRecord);
  1515. Assert(scriptContext);
  1516. char16 * finalName = nullptr;
  1517. size_t propertyLength = (size_t)propertyRecord->GetLength();
  1518. if (propertyLength > 0)
  1519. {
  1520. size_t totalChars;
  1521. if (SizeTAdd(propertyLength, ConstructNameGetSetLength, &totalChars) == S_OK)
  1522. {
  1523. finalName = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, totalChars);
  1524. Assert(finalName != nullptr);
  1525. const char16* propertyName = propertyRecord->GetBuffer();
  1526. Assert(propertyName != nullptr);
  1527. wcscpy_s(finalName, totalChars, propertyName);
  1528. Assert(getOrSetStr != nullptr);
  1529. Assert(wcslen(getOrSetStr) == 4);
  1530. wcscpy_s(finalName + propertyLength, ConstructNameGetSetLength, getOrSetStr);
  1531. }
  1532. }
  1533. return finalName;
  1534. }
  1535. /*static*/
  1536. void JavascriptObject::ModifyGetterSetterFuncName(const PropertyRecord * propertyRecord, const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
  1537. {
  1538. Assert(scriptContext);
  1539. Assert(propertyRecord);
  1540. if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
  1541. {
  1542. charcount_t propertyLength = propertyRecord->GetLength();
  1543. if (descriptor.GetterSpecified()
  1544. && Js::ScriptFunction::Is(descriptor.GetGetter())
  1545. && _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->GetDisplayName(), _u("get")) == 0)
  1546. {
  1547. // modify to name.get
  1548. const char16* finalName = ConstructName(propertyRecord, _u(".get"), scriptContext);
  1549. if (finalName != nullptr)
  1550. {
  1551. FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
  1552. Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->SetDisplayName(finalName,
  1553. propertyLength + 4 /*".get"*/, propertyLength + 1, flags);
  1554. }
  1555. }
  1556. if (descriptor.SetterSpecified()
  1557. && Js::ScriptFunction::Is(descriptor.GetSetter())
  1558. && _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->GetDisplayName(), _u("set")) == 0)
  1559. {
  1560. // modify to name.set
  1561. const char16* finalName = ConstructName(propertyRecord, _u(".set"), scriptContext);
  1562. if (finalName != nullptr)
  1563. {
  1564. FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
  1565. Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->SetDisplayName(finalName,
  1566. propertyLength + 4 /*".set"*/, propertyLength + 1, flags);
  1567. }
  1568. }
  1569. }
  1570. }
  1571. BOOL JavascriptObject::DefineOwnPropertyHelper(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, ScriptContext* scriptContext, bool throwOnError /* = true*/)
  1572. {
  1573. BOOL returnValue;
  1574. obj->ThrowIfCannotDefineProperty(propId, descriptor);
  1575. const Type* oldType = obj->GetType();
  1576. obj->ClearWritableDataOnlyDetectionBit();
  1577. // HostDispatch: it doesn't support changing property attributes and default attributes are not per ES5,
  1578. // so there is no benefit in using ES5 DefineOwnPropertyDescriptor for it, use old implementation.
  1579. if (TypeIds_HostDispatch != obj->GetTypeId())
  1580. {
  1581. if (DynamicObject::IsAnyArray(obj))
  1582. {
  1583. returnValue = JavascriptOperators::DefineOwnPropertyForArray(
  1584. JavascriptArray::FromAnyArray(obj), propId, descriptor, throwOnError, scriptContext);
  1585. }
  1586. else
  1587. {
  1588. returnValue = JavascriptOperators::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  1589. if (propId == PropertyIds::__proto__)
  1590. {
  1591. scriptContext->GetLibrary()->GetObjectPrototypeObject()->PostDefineOwnProperty__proto__(obj);
  1592. }
  1593. }
  1594. }
  1595. else
  1596. {
  1597. returnValue = JavascriptOperators::SetPropertyDescriptor(obj, propId, descriptor);
  1598. }
  1599. if (propId == PropertyIds::_symbolSpecies && obj == scriptContext->GetLibrary()->GetArrayConstructor())
  1600. {
  1601. scriptContext->GetLibrary()->SetArrayObjectHasUserDefinedSpecies(true);
  1602. }
  1603. if (obj->IsWritableDataOnlyDetectionBitSet())
  1604. {
  1605. if (obj->GetType() == oldType)
  1606. {
  1607. // Also, if the object's type has not changed, we need to ensure that
  1608. // the cached property string for this property, if any, does not
  1609. // specify this object's type.
  1610. scriptContext->InvalidatePropertyStringCache(propId, obj->GetType());
  1611. }
  1612. }
  1613. if (descriptor.IsAccessorDescriptor())
  1614. {
  1615. scriptContext->optimizationOverrides.SetSideEffects(Js::SideEffects_Accessor);
  1616. }
  1617. return returnValue;
  1618. }
  1619. }