| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #include "RuntimeLibraryPch.h"
- #include "Types/NullTypeHandler.h"
- namespace Js
- {
- Var JavascriptObject::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
- // SkipDefaultNewObject function flag should have prevented the default object from
- // being created, except when call true a host dispatch.
- Var newTarget = callInfo.Flags & CallFlags_NewTarget ? args.Values[args.Info.Count] : args[0];
- bool isCtorSuperCall = (callInfo.Flags & CallFlags_New) && newTarget != nullptr && !JavascriptOperators::IsUndefined(newTarget);
- Assert(isCtorSuperCall || !(callInfo.Flags & CallFlags_New) || args[0] == nullptr
- || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
- if (args.Info.Count > 1)
- {
- switch (JavascriptOperators::GetTypeId(args[1]))
- {
- case TypeIds_Undefined:
- case TypeIds_Null:
- // Break to return a new object
- break;
- case TypeIds_StringObject:
- case TypeIds_Function:
- case TypeIds_Array:
- case TypeIds_ES5Array:
- case TypeIds_RegEx:
- case TypeIds_NumberObject:
- case TypeIds_SIMDObject:
- case TypeIds_Date:
- case TypeIds_BooleanObject:
- case TypeIds_Error:
- case TypeIds_Object:
- case TypeIds_Arguments:
- case TypeIds_ActivationObject:
- case TypeIds_SymbolObject:
- return isCtorSuperCall ?
- JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), RecyclableObject::FromVar(args[1]), nullptr, scriptContext) :
- args[1];
- default:
- RecyclableObject* result = nullptr;
- if (FALSE == JavascriptConversion::ToObject(args[1], scriptContext, &result))
- {
- // JavascriptConversion::ToObject should only return FALSE for null and undefined.
- Assert(false);
- }
- return isCtorSuperCall ?
- JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), result, nullptr, scriptContext) :
- result;
- }
- }
- if (callInfo.Flags & CallFlags_NotUsed)
- {
- return args[0];
- }
- Var newObj = scriptContext->GetLibrary()->CreateObject(true);
- return isCtorSuperCall ?
- JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject::FromVar(newTarget), RecyclableObject::FromVar(newObj), nullptr, scriptContext) :
- newObj;
- }
- Var JavascriptObject::EntryHasOwnProperty(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
- RecyclableObject* dynamicObject = nullptr;
- if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.hasOwnProperty"));
- }
- // no property specified
- if (args.Info.Count == 1)
- {
- return scriptContext->GetLibrary()->GetFalse();
- }
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(args[1], scriptContext, &propertyRecord);
- if (JavascriptOperators::HasOwnProperty(dynamicObject, propertyRecord->GetPropertyId(), scriptContext))
- {
- return scriptContext->GetLibrary()->GetTrue();
- }
- return scriptContext->GetLibrary()->GetFalse();
- }
- Var JavascriptObject::EntryPropertyIsEnumerable(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
- RecyclableObject* dynamicObject = nullptr;
- if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.propertyIsEnumerable"));
- }
- if (args.Info.Count >= 2)
- {
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(args[1], scriptContext, &propertyRecord);
- PropertyId propertyId = propertyRecord->GetPropertyId();
- PropertyDescriptor currentDescriptor;
- BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(dynamicObject, propertyId, scriptContext, ¤tDescriptor);
- if (isCurrentDescriptorDefined == TRUE)
- {
- if (currentDescriptor.IsEnumerable())
- {
- return scriptContext->GetLibrary()->GetTrue();
- }
- }
- }
- return scriptContext->GetLibrary()->GetFalse();
- }
- BOOL JavascriptObject::ChangePrototype(RecyclableObject* object, RecyclableObject* newPrototype, bool shouldThrow, ScriptContext* scriptContext)
- {
- // 8.3.2 [[SetInheritance]] (V)
- // When the [[SetInheritance]] internal method of O is called with argument V the following steps are taken:
- // 1. Assert: Either Type(V) is Object or Type(V) is Null.
- Assert(JavascriptOperators::IsObject(object));
- Assert(JavascriptOperators::IsObjectOrNull(newPrototype));
- if (JavascriptProxy::Is(object))
- {
- JavascriptProxy* proxy = JavascriptProxy::FromVar(object);
- CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
- return proxy->SetPrototypeTrap(newPrototype, shouldThrow);
- }
-
- // 2. Let extensible be the value of the [[Extensible]] internal data property of O.
- // 3. Let current be the value of the [[Prototype]] internal data property of O.
- // 4. If SameValue(V, current), then return true.
- if (newPrototype == JavascriptObject::GetPrototypeOf(object, scriptContext))
- {
- return TRUE;
- }
- // 5. If extensible is false, then return false.
- if (!object->IsExtensible())
- {
- if (shouldThrow)
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
- }
- return FALSE;
- }
- if (object->IsProtoImmutable())
- {
- // ES2016 19.1.3:
- // The Object prototype object is the intrinsic object %ObjectPrototype%.
- // The Object prototype object is an immutable prototype exotic object.
- // ES2016 9.4.7:
- // An immutable prototype exotic object is an exotic object that has an immutable [[Prototype]] internal slot.
- JavascriptError::ThrowTypeError(scriptContext, JSERR_ImmutablePrototypeSlot);
- }
- // 6. If V is not null, then
- // a. Let p be V.
- // b. Repeat, while p is not null
- // i. If SameValue(p, O) is true, then return false.
- // ii. Let nextp be the result of calling the [[GetInheritance]] internal method of p with no arguments.
- // iii. ReturnIfAbrupt(nextp).
- // iv. Let p be nextp.
- if (IsPrototypeOf(object, newPrototype, scriptContext)) // Reject cycle
- {
- if (shouldThrow)
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_CyclicProtoValue);
- }
- return FALSE;
- }
- // 7. Set the value of the [[Prototype]] internal data property of O to V.
- // 8. Return true.
- bool isInvalidationOfInlineCacheNeeded = true;
- DynamicObject * obj = DynamicObject::FromVar(object);
- // If this object was not prototype object, then no need to invalidate inline caches.
- // Simply assign it a new type so if this object used protoInlineCache in past, it will
- // be invalidated because of type mismatch and subsequently we will update its protoInlineCache
- if (!(obj->GetDynamicType()->GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag))
- {
- // If object has locked type, skip changing its type here as it will be changed anyway below
- // when object gets newPrototype object.
- if (!obj->HasLockedType())
- {
- obj->ChangeType();
- }
- Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInProtoInlineCaches(obj));
- Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInStoreFieldInlineCaches(obj));
- isInvalidationOfInlineCacheNeeded = false;
- }
- if (isInvalidationOfInlineCacheNeeded)
- {
- // Notify old prototypes that they are being removed from a prototype chain. This triggers invalidating protocache, etc.
- JavascriptOperators::MapObjectAndPrototypes<true>(object->GetPrototype(), [=](RecyclableObject* obj)
- {
- obj->RemoveFromPrototype(scriptContext);
- });
- // Examine new prototype chain. If it brings in any non-WritableData property, we need to invalidate related caches.
- bool objectAndPrototypeChainHasOnlyWritableDataProperties =
- JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(newPrototype);
- if (!objectAndPrototypeChainHasOnlyWritableDataProperties
- || object->GetScriptContext() != newPrototype->GetScriptContext())
- {
- // The HaveOnlyWritableDataProperties cache is cleared when a property is added or changed,
- // but only for types in the same script context. Therefore, if the prototype is in another
- // context, the object's cache won't be cleared when a property is added or changed on the prototype.
- // Moreover, an object is added to the cache only when its whole prototype chain is in the same
- // context.
- //
- // Since we don't have a way to find out which objects have a certain object as their prototype,
- // we clear the cache here instead.
- // Invalidate fast prototype chain writable data test flag
- object->GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
- }
- if (!objectAndPrototypeChainHasOnlyWritableDataProperties)
- {
- // Invalidate StoreField/PropertyGuards for any non-WritableData property in the new chain
- JavascriptOperators::MapObjectAndPrototypes<true>(newPrototype, [=](RecyclableObject* obj)
- {
- if (!obj->HasOnlyWritableDataProperties())
- {
- obj->AddToPrototype(scriptContext);
- }
- });
- }
- }
- // Set to new prototype
- if (object->IsExternal() || (DynamicType::Is(object->GetTypeId()) && (DynamicObject::FromVar(object))->IsCrossSiteObject()))
- {
- CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
- }
- object->SetPrototype(newPrototype);
- return TRUE;
- }
- Var JavascriptObject::EntryIsPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
- // no property specified
- if (args.Info.Count == 1 || !JavascriptOperators::IsObject(args[1]))
- {
- return scriptContext->GetLibrary()->GetFalse();
- }
- RecyclableObject* dynamicObject = nullptr;
- if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.isPrototypeOf"));
- }
- RecyclableObject* value = RecyclableObject::FromVar(args[1]);
- if (dynamicObject->GetTypeId() == TypeIds_GlobalObject)
- {
- dynamicObject = RecyclableObject::FromVar(static_cast<Js::GlobalObject*>(dynamicObject)->ToThis());
- }
- while (JavascriptOperators::GetTypeId(value) != TypeIds_Null)
- {
- value = JavascriptOperators::GetPrototype(value);
- if (dynamicObject == value)
- {
- return scriptContext->GetLibrary()->GetTrue();
- }
- }
- return scriptContext->GetLibrary()->GetFalse();
- }
- // 19.1.3.5 - Object.prototype.toLocaleString as of ES6 (6.0)
- Var JavascriptObject::EntryToLocaleString(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count, "Should always have implicit 'this'");
- Var thisValue = args[0];
- RecyclableObject* dynamicObject = nullptr;
- if (FALSE == JavascriptConversion::ToObject(thisValue, scriptContext, &dynamicObject))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.toLocaleString"));
- }
- Var toStringVar = nullptr;
- if (!JavascriptOperators::GetProperty(thisValue, dynamicObject, Js::PropertyIds::toString, &toStringVar, scriptContext) || !JavascriptConversion::IsCallable(toStringVar))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.toLocaleString"));
- }
- RecyclableObject* toStringFunc = RecyclableObject::FromVar(toStringVar);
- return CALL_FUNCTION(toStringFunc, CallInfo(CallFlags_Value, 1), thisValue);
- }
- Var JavascriptObject::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count, "Should always have implicit 'this'");
- return ToStringHelper(args[0], scriptContext);
- }
- // ES2017 19.1.3.6 Object.prototype.toString()
- JavascriptString* JavascriptObject::ToStringTagHelper(Var thisArg, ScriptContext *scriptContext, TypeId type)
- {
- JavascriptLibrary *library = scriptContext->GetLibrary();
- // 1. If the this value is undefined, return "[object Undefined]".
- if (type == TypeIds_Undefined)
- {
- return library->CreateStringFromCppLiteral(_u("[object Undefined]"));
- }
- // 2. If the this value is null, return "[object Null]".
- if (type == TypeIds_Null)
- {
- return library->CreateStringFromCppLiteral(_u("[object Null]"));
- }
- // 3. Let O be ToObject(this value).
- RecyclableObject *thisArgAsObject = RecyclableObject::FromVar(JavascriptOperators::ToObject(thisArg, scriptContext));
- // 4. Let isArray be ? IsArray(O).
- // There is an implicit check for a null proxy handler in IsArray, so use the operator.
- BOOL isArray = JavascriptOperators::IsArray(thisArgAsObject);
- // 15. Let tag be ? Get(O, @@toStringTag).
- Var tag = JavascriptOperators::GetProperty(thisArgAsObject, PropertyIds::_symbolToStringTag, scriptContext); // Let tag be the result of Get(O, @@toStringTag).
- // 17. Return the String that is the result of concatenating "[object ", tag, and "]".
- auto buildToString = [&scriptContext](Var tag) {
- JavascriptString *tagStr = JavascriptString::FromVar(tag);
- CompoundString::Builder<32> stringBuilder(scriptContext);
- stringBuilder.AppendChars(_u("[object "));
- stringBuilder.AppendChars(tagStr);
- stringBuilder.AppendChars(_u(']'));
- return stringBuilder.ToString();
- };
- if (tag != nullptr && JavascriptString::Is(tag))
- {
- return buildToString(tag);
- }
- // If we don't have a tag or it's not a string, use the 'built in tag'.
- if (isArray)
- {
- // 5. If isArray is true, let builtinTag be "Array".
- return library->CreateStringFromCppLiteral(_u("[object Array]"));
- }
- JavascriptString* builtInTag = nullptr;
- switch (type)
- {
- // 6. Else if O is an exotic String object, let builtinTag be "String".
- case TypeIds_String:
- case TypeIds_StringObject:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object String]"));
- break;
- // 7. Else if O has an[[ParameterMap]] internal slot, let builtinTag be "Arguments".
- case TypeIds_Arguments:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object Arguments]"));
- break;
- // 8. Else if O has a [[Call]] internal method, let builtinTag be "Function".
- case TypeIds_Function:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object Function]"));
- break;
- // 9. Else if O has an [[ErrorData]] internal slot, let builtinTag be "Error".
- case TypeIds_Error:
- builtInTag = library->GetErrorDisplayString();
- break;
- // 10. Else if O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
- case TypeIds_Boolean:
- case TypeIds_BooleanObject:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object Boolean]"));
- break;
- // 11. Else if O has a [[NumberData]] internal slot, let builtinTag be "Number".
- case TypeIds_Number:
- case TypeIds_Int64Number:
- case TypeIds_UInt64Number:
- case TypeIds_Integer:
- case TypeIds_NumberObject:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object Number]"));
- break;
- // 12. Else if O has a [[DateValue]] internal slot, let builtinTag be "Date".
- case TypeIds_Date:
- case TypeIds_WinRTDate:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object Date]"));
- break;
- // 13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
- case TypeIds_RegEx:
- builtInTag = library->CreateStringFromCppLiteral(_u("[object RegExp]"));
- break;
- // 14. Else, let builtinTag be "Object".
- default:
- {
- if (thisArgAsObject->IsExternal())
- {
- builtInTag = buildToString(thisArgAsObject->GetClassName(scriptContext));
- }
- else
- {
- builtInTag = library->GetObjectDisplayString(); // [object Object]
- }
- break;
- }
- }
- Assert(builtInTag != nullptr);
- return builtInTag;
- }
- Var JavascriptObject::ToStringHelper(Var thisArg, ScriptContext* scriptContext)
- {
- TypeId type = JavascriptOperators::GetTypeId(thisArg);
- // We first need to make sure we are in the right context.
- if (type == TypeIds_HostDispatch)
- {
- RecyclableObject* hostDispatchObject = RecyclableObject::FromVar(thisArg);
- DynamicObject* remoteObject = hostDispatchObject->GetRemoteObject();
- if (!remoteObject)
- {
- Var result = nullptr;
- Js::Var values[1];
- Js::CallInfo info(Js::CallFlags_Value, 1);
- Js::Arguments args(info, values);
- values[0] = thisArg;
- if (hostDispatchObject->InvokeBuiltInOperationRemotely(EntryToString, args, &result))
- {
- return result;
- }
- }
- }
- // Dispatch to @@toStringTag implementation.
- if (type >= TypeIds_TypedArrayMin && type <= TypeIds_TypedArrayMax && !scriptContext->GetThreadContext()->IsScriptActive())
- {
- // Use external call for typedarray in the debugger.
- Var toStringValue = nullptr;
- BEGIN_JS_RUNTIME_CALL_EX(scriptContext, false);
- toStringValue = ToStringTagHelper(thisArg, scriptContext, type);
- END_JS_RUNTIME_CALL(scriptContext);
- return toStringValue;
- }
- // 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.)
- return ToStringTagHelper(CrossSite::MarshalVar(scriptContext, thisArg), scriptContext, type);
- }
- // -----------------------------------------------------------
- // Object.prototype.valueOf
- // 1. Let O be the result of calling ToObject passing the this value as the argument.
- // 2. If O is the result of calling the Object constructor with a host object (15.2.2.1), then
- // 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.
- // 3. Return O.
- // -----------------------------------------------------------
- Var JavascriptObject::EntryValueOf(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
- TypeId argType = JavascriptOperators::GetTypeId(args[0]);
- // throw a TypeError if TypeId is null or undefined, and apply ToObject to the 'this' value otherwise.
- if ((argType == TypeIds_Null) || (argType == TypeIds_Undefined))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.valueOf"));
- }
- else
- {
- return JavascriptOperators::ToObject(args[0], scriptContext);
- }
- }
- Var JavascriptObject::EntryGetOwnPropertyDescriptor(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- RecyclableObject* obj = nullptr;
- if (args.Info.Count < 2)
- {
- obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext));
- }
- else
- {
- // Convert the argument to object first
- obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(args[1], scriptContext));
- }
- // If the object is HostDispatch try to invoke the operation remotely
- if (obj->GetTypeId() == TypeIds_HostDispatch)
- {
- Var result;
- if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptor, args, &result))
- {
- return result;
- }
- }
- Var propertyKey = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
- return JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyKey, scriptContext);
- }
- Var JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, Var propertyKey, ScriptContext* scriptContext)
- {
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- PropertyId propertyId = propertyRecord->GetPropertyId();
- obj->ThrowIfCannotGetOwnPropertyDescriptor(propertyId);
- PropertyDescriptor propertyDescriptor;
- BOOL isPropertyDescriptorDefined;
- isPropertyDescriptorDefined = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor);
- if (!isPropertyDescriptorDefined)
- {
- return scriptContext->GetLibrary()->GetUndefined();
- }
- return JavascriptOperators::FromPropertyDescriptor(propertyDescriptor, scriptContext);
- }
- BOOL JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor& propertyDescriptor)
- {
- BOOL isPropertyDescriptorDefined;
- if (obj->CanHaveInterceptors())
- {
- isPropertyDescriptorDefined = obj->HasOwnProperty(propertyId) ?
- JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor) : obj->GetDefaultPropertyDescriptor(propertyDescriptor);
- }
- else
- {
- isPropertyDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor) ||
- obj->GetDefaultPropertyDescriptor(propertyDescriptor);
- }
- return isPropertyDescriptorDefined;
- }
- Var JavascriptObject::EntryGetOwnPropertyDescriptors(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- RecyclableObject* obj = nullptr;
- if (args.Info.Count < 2)
- {
- obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext));
- }
- else
- {
- // Convert the argument to object first
- obj = RecyclableObject::FromVar(JavascriptOperators::ToObject(args[1], scriptContext));
- }
- // If the object is HostDispatch try to invoke the operation remotely
- if (obj->GetTypeId() == TypeIds_HostDispatch)
- {
- Var result;
- if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptors, args, &result))
- {
- return result;
- }
- }
- JavascriptArray* ownPropertyKeys = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
- RecyclableObject* resultObj = scriptContext->GetLibrary()->CreateObject(true, (Js::PropertyIndex) ownPropertyKeys->GetLength());
-
- PropertyDescriptor propDesc;
- Var propKey = nullptr;
- for (uint i = 0; i < ownPropertyKeys->GetLength(); i++)
- {
- BOOL getPropResult = ownPropertyKeys->DirectGetItemAt(i, &propKey);
- Assert(getPropResult);
- if (!getPropResult)
- {
- continue;
- }
-
- PropertyRecord const * propertyRecord;
- JavascriptConversion::ToPropertyKey(propKey, scriptContext, &propertyRecord);
- Var newDescriptor = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propKey, scriptContext);
- if (!JavascriptOperators::IsUndefined(newDescriptor))
- {
- resultObj->SetProperty(propertyRecord->GetPropertyId(), newDescriptor, PropertyOperation_None, nullptr);
- }
- }
- return resultObj;
- }
- Var JavascriptObject::EntryGetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getPrototypeOf);
- // 19.1.2.9
- // Object.getPrototypeOf ( O )
- // When the getPrototypeOf function is called with argument O, the following steps are taken:
- RecyclableObject *object = nullptr;
- // 1. Let obj be ToObject(O).
- // 2. ReturnIfAbrupt(obj).
- if (args.Info.Count < 2 || !JavascriptConversion::ToObject(args[1], scriptContext, &object))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.getPrototypeOf"));
- }
- // 3. Return obj.[[GetPrototypeOf]]().
- return CrossSite::MarshalVar(scriptContext, GetPrototypeOf(object, scriptContext));
- }
- Var JavascriptObject::EntrySetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- Assert(!(callInfo.Flags & CallFlags_New));
- ScriptContext* scriptContext = function->GetScriptContext();
- // 19.1.2.18
- // Object.setPrototypeOf ( O, proto )
- // When the setPrototypeOf function is called with arguments O and proto, the following steps are taken:
- // 1. Let O be RequireObjectCoercible(O).
- // 2. ReturnIfAbrupt(O).
- // 3. If Type(proto) is neither Object or Null, then throw a TypeError exception.
- int32 errCode = NOERROR;
- if (args.Info.Count < 2 || !JavascriptConversion::CheckObjectCoercible(args[1], scriptContext))
- {
- errCode = JSERR_FunctionArgument_NeedObject;
- }
- else if (args.Info.Count < 3 || !JavascriptOperators::IsObjectOrNull(args[2]))
- {
- errCode = JSERR_FunctionArgument_NotObjectOrNull;
- }
- if (errCode != NOERROR)
- {
- JavascriptError::ThrowTypeError(scriptContext, errCode, _u("Object.setPrototypeOf"));
- }
- // 4. If Type(O) is not Object, return O.
- if (!JavascriptOperators::IsObject(args[1]))
- {
- return args[1];
- }
- #if ENABLE_COPYONACCESS_ARRAY
- JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
- #endif
- RecyclableObject* object = RecyclableObject::FromVar(args[1]);
- RecyclableObject* newPrototype = RecyclableObject::FromVar(args[2]);
- // 5. Let status be O.[[SetPrototypeOf]](proto).
- // 6. ReturnIfAbrupt(status).
- // 7. If status is false, throw a TypeError exception.
- ChangePrototype(object, newPrototype, /*shouldThrow*/true, scriptContext);
- // 8. Return O.
- return object;
- }
- Var JavascriptObject::EntrySeal(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_seal);
- // Spec update in Rev29 under section 19.1.2.17
- if (args.Info.Count < 2)
- {
- return scriptContext->GetLibrary()->GetUndefined();
- }
- else if (!JavascriptOperators::IsObject(args[1]))
- {
- return args[1];
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- globalObject->Seal();
- }
- object->Seal();
- return object;
- }
- Var JavascriptObject::EntryFreeze(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_freeze);
- // Spec update in Rev29 under section 19.1.2.5
- if (args.Info.Count < 2)
- {
- return scriptContext->GetLibrary()->GetUndefined();
- }
- else if (!JavascriptOperators::IsObject(args[1]))
- {
- return args[1];
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- globalObject->Freeze();
- }
- object->Freeze();
- return object;
- }
- Var JavascriptObject::EntryPreventExtensions(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_preventExtensions);
- // Spec update in Rev29 under section 19.1.2.15
- if (args.Info.Count < 2)
- {
- return scriptContext->GetLibrary()->GetUndefined();
- }
- else if (!JavascriptOperators::IsObject(args[1]))
- {
- return args[1];
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- globalObject->PreventExtensions();
- }
- object->PreventExtensions();
- return object;
- }
- Var JavascriptObject::EntryIsSealed(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isSealed);
- if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
- {
- return scriptContext->GetLibrary()->GetTrue();
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- BOOL isSealed = object->IsSealed();
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (isSealed && globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- isSealed = globalObject->IsSealed();
- }
- return scriptContext->GetLibrary()->GetTrueOrFalse(isSealed);
- }
- Var JavascriptObject::EntryIsFrozen(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isFrozen);
- if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
- {
- return scriptContext->GetLibrary()->GetTrue();
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- BOOL isFrozen = object->IsFrozen();
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (isFrozen && globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- isFrozen = globalObject->IsFrozen();
- }
- return scriptContext->GetLibrary()->GetTrueOrFalse(isFrozen);
- }
- Var JavascriptObject::EntryIsExtensible(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_isExtensible);
- Assert(!(callInfo.Flags & CallFlags_New));
- if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
- {
- return scriptContext->GetLibrary()->GetFalse();
- }
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- BOOL isExtensible = object->IsExtensible();
- GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
- if (isExtensible && globalObject != object && globalObject && (globalObject->ToThis() == object))
- {
- isExtensible = globalObject->IsExtensible();
- }
- return scriptContext->GetLibrary()->GetTrueOrFalse(isExtensible);
- }
- Var JavascriptObject::EntryGetOwnPropertyNames(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertyNames);
- Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
- RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
- if (object->GetTypeId() == TypeIds_HostDispatch)
- {
- Var result;
- if (object->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyNames, args, &result))
- {
- return result;
- }
- }
- return JavascriptOperators::GetOwnPropertyNames(object, scriptContext);
- }
- Var JavascriptObject::EntryGetOwnPropertySymbols(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
- RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
- if (object->GetTypeId() == TypeIds_HostDispatch)
- {
- Var result;
- if (object->InvokeBuiltInOperationRemotely(EntryGetOwnPropertySymbols, args, &result))
- {
- return result;
- }
- }
- return JavascriptOperators::GetOwnPropertySymbols(object, scriptContext);
- }
- Var JavascriptObject::EntryKeys(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_keys);
- Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
- RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
- if (object->GetTypeId() == TypeIds_HostDispatch)
- {
- Var result;
- if (object->InvokeBuiltInOperationRemotely(EntryKeys, args, &result))
- {
- return result;
- }
- }
- return JavascriptOperators::GetOwnEnumerablePropertyNames(object, scriptContext);
- }
- Var JavascriptObject::GetValuesOrEntries(RecyclableObject* object, bool valuesToReturn, ScriptContext* scriptContext)
- {
- Assert(object != nullptr);
- Assert(scriptContext != nullptr);
- JavascriptArray* valuesArray = scriptContext->GetLibrary()->CreateArray(0);
- JavascriptArray* ownKeysResult = JavascriptOperators::GetOwnPropertyNames(object, scriptContext);
- uint32 length = ownKeysResult->GetLength();
- Var nextKey;
- const PropertyRecord* propertyRecord = nullptr;
- PropertyId propertyId;
- for (uint32 i = 0, index = 0; i < length; i++)
- {
- nextKey = ownKeysResult->DirectGetItem(i);
- Assert(JavascriptString::Is(nextKey));
- PropertyDescriptor propertyDescriptor;
- JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
- propertyId = propertyRecord->GetPropertyId();
- Assert(propertyId != Constants::NoProperty);
- if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyId, scriptContext, &propertyDescriptor))
- {
- if (propertyDescriptor.IsEnumerable())
- {
- Var value = JavascriptOperators::GetProperty(object, propertyId, scriptContext);
- if (!valuesToReturn)
- {
- // For Object.entries each entry is key, value pair
- JavascriptArray* entry = scriptContext->GetLibrary()->CreateArray(2);
- entry->DirectSetItemAt(0, CrossSite::MarshalVar(scriptContext, nextKey));
- entry->DirectSetItemAt(1, CrossSite::MarshalVar(scriptContext, value));
- value = entry;
- }
- valuesArray->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, value));
- }
- }
- }
- return valuesArray;
- }
- Var JavascriptObject::EntryValues(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_values);
- Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
- RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
- return GetValuesOrEntries(object, true /*valuesToReturn*/, scriptContext);
- }
- Var JavascriptObject::EntryEntries(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_entries);
- Var tempVar = args.Info.Count < 2 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
- RecyclableObject *object = RecyclableObject::FromVar(JavascriptOperators::ToObject(tempVar, scriptContext));
- return GetValuesOrEntries(object, false /*valuesToReturn*/, scriptContext);
- }
- JavascriptArray* JavascriptObject::CreateOwnSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
- {
- return CreateKeysHelper(object, scriptContext, TRUE, true /*includeSymbolsOnly */, false, true /*includeSpecialProperties*/);
- }
- JavascriptArray* JavascriptObject::CreateOwnStringPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
- {
- return CreateKeysHelper(object, scriptContext, TRUE, false, true /*includeStringsOnly*/, true /*includeSpecialProperties*/);
- }
- JavascriptArray* JavascriptObject::CreateOwnStringSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
- {
- return CreateKeysHelper(object, scriptContext, TRUE, true/*includeSymbolsOnly*/, true /*includeStringsOnly*/, true /*includeSpecialProperties*/);
- }
- JavascriptArray* JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
- {
- return CreateKeysHelper(object, scriptContext, FALSE, false, true/*includeStringsOnly*/, false);
- }
- JavascriptArray* JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(RecyclableObject* object, ScriptContext* scriptContext)
- {
- return CreateKeysHelper(object, scriptContext, FALSE, true/*includeSymbolsOnly*/, true/*includeStringsOnly*/, false);
- }
- // 9.1.12 [[OwnPropertyKeys]] () in RC#4 dated April 3rd 2015.
- JavascriptArray* JavascriptObject::CreateKeysHelper(RecyclableObject* object, ScriptContext* scriptContext, BOOL includeNonEnumerable, bool includeSymbolProperties, bool includeStringProperties, bool includeSpecialProperties)
- {
- //1. Let keys be a new empty List.
- //2. For each own property key P of O that is an integer index, in ascending numeric index order
- // a. Add P as the last element of keys.
- //3. For each own property key P of O that is a String but is not an integer index, in property creation order
- // a. Add P as the last element of keys.
- //4. For each own property key P of O that is a Symbol, in property creation order
- // a. Add P as the last element of keys.
- //5. Return keys.
- AssertMsg(includeStringProperties || includeSymbolProperties, "Should either get string or symbol properties.");
- JavascriptStaticEnumerator enumerator;
- JavascriptArray* newArr = scriptContext->GetLibrary()->CreateArray(0);
- JavascriptArray* newArrForSymbols = scriptContext->GetLibrary()->CreateArray(0);
- EnumeratorFlags flags = EnumeratorFlags::None;
- if (includeNonEnumerable)
- {
- flags |= EnumeratorFlags::EnumNonEnumerable;
- }
- if (includeSymbolProperties)
- {
- flags |= EnumeratorFlags::EnumSymbols;
- }
- if (!object->GetEnumerator(&enumerator, flags, scriptContext))
- {
- return newArr; // Return an empty array if we don't have an enumerator
- }
- RecyclableObject *undefined = scriptContext->GetLibrary()->GetUndefined();
- Var propertyName = nullptr;
- PropertyId propertyId;
- uint32 propertyIndex = 0;
- uint32 symbolIndex = 0;
- const PropertyRecord* propertyRecord;
- JavascriptSymbol* symbol;
- while ((propertyName = enumerator.MoveAndGetNext(propertyId)) != NULL)
- {
- if (!JavascriptOperators::IsUndefinedObject(propertyName, undefined)) //There are some code paths in which GetCurrentIndex can return undefined
- {
- if (includeSymbolProperties)
- {
- propertyRecord = scriptContext->GetPropertyName(propertyId);
- if (propertyRecord->IsSymbol())
- {
- symbol = scriptContext->GetLibrary()->CreateSymbol(propertyRecord);
- newArrForSymbols->DirectSetItemAt(symbolIndex++, CrossSite::MarshalVar(scriptContext, symbol));
- continue;
- }
- }
- if (includeStringProperties)
- {
- newArr->DirectSetItemAt(propertyIndex++, CrossSite::MarshalVar(scriptContext, propertyName));
- }
- }
- }
- // Special properties
- if (includeSpecialProperties && includeStringProperties)
- {
- uint32 index = 0;
- while (object->GetSpecialPropertyName(index, &propertyName, scriptContext))
- {
- if (!JavascriptOperators::IsUndefinedObject(propertyName, undefined))
- {
- newArr->DirectSetItemAt(propertyIndex++, propertyName);
- }
- index++;
- }
- }
- // Append all the symbols at the end of list
- uint32 totalSymbols = newArrForSymbols->GetLength();
- for (uint32 symIndex = 0; symIndex < totalSymbols; symIndex++)
- {
- newArr->DirectSetItemAt(propertyIndex++, newArrForSymbols->DirectGetItem(symIndex));
- }
- return newArr;
- }
- // args[1] this object to operate on.
- // args[2] property name.
- // args[3] object that attributes for the new descriptor.
- Var JavascriptObject::EntryDefineProperty(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.defineProperty"));
- }
- #if ENABLE_COPYONACCESS_ARRAY
- JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
- #endif
- RecyclableObject* obj = RecyclableObject::FromVar(args[1]);
- // If the object is HostDispatch try to invoke the operation remotely
- if (obj->GetTypeId() == TypeIds_HostDispatch)
- {
- if (obj->InvokeBuiltInOperationRemotely(EntryDefineProperty, args, NULL))
- {
- return obj;
- }
- }
- Var propertyKey = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
- PropertyRecord const * propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- Var descVar = args.Info.Count > 3 ? args[3] : obj->GetLibrary()->GetUndefined();
- PropertyDescriptor propertyDescriptor;
- if (!JavascriptOperators::ToPropertyDescriptor(descVar, &propertyDescriptor, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propertyRecord->GetPropertyId())->GetBuffer());
- }
- if (CONFIG_FLAG(UseFullName))
- {
- ModifyGetterSetterFuncName(propertyRecord, propertyDescriptor, scriptContext);
- }
- DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
- return obj;
- }
- Var JavascriptObject::EntryDefineProperties(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_defineProperties);
- Assert(!(callInfo.Flags & CallFlags_New));
- if (args.Info.Count < 2 || !JavascriptOperators::IsObject(args[1]))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.defineProperties"));
- }
- #if ENABLE_COPYONACCESS_ARRAY
- JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
- #endif
- RecyclableObject *object = RecyclableObject::FromVar(args[1]);
- // If the object is HostDispatch try to invoke the operation remotely
- if (object->GetTypeId() == TypeIds_HostDispatch)
- {
- if (object->InvokeBuiltInOperationRemotely(EntryDefineProperties, args, NULL))
- {
- return object;
- }
- }
- Var propertiesVar = args.Info.Count > 2 ? args[2] : object->GetLibrary()->GetUndefined();
- RecyclableObject* properties = nullptr;
- if (FALSE == JavascriptConversion::ToObject(propertiesVar, scriptContext, &properties))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NullOrUndefined, _u("Object.defineProperties"));
- }
- return DefinePropertiesHelper(object, properties, scriptContext);
- }
- // args[1] property name.
- // args[2] function object to use as the getter function.
- Var JavascriptObject::EntryDefineGetter(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- // For browser interop, simulate LdThis by calling OP implementation directly.
- // Do not have module id here so use the global id, 0.
- //
- #if ENABLE_COPYONACCESS_ARRAY
- JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[0]);
- #endif
- Var thisArg = JavascriptOperators::OP_GetThisNoFastPath(args[0], 0, scriptContext);
- RecyclableObject* obj = RecyclableObject::FromVar(thisArg);
- Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- Var getterFunc = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
- if (!JavascriptConversion::IsCallable(getterFunc))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.__defineGetter__"));
- }
- PropertyDescriptor propertyDescriptor;
- propertyDescriptor.SetEnumerable(true);
- propertyDescriptor.SetConfigurable(true);
- propertyDescriptor.SetGetter(getterFunc);
- DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
- return obj->GetLibrary()->GetUndefined();
- }
- // args[1] property name.
- // args[2] function object to use as the setter function.
- Var JavascriptObject::EntryDefineSetter(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- // For browser interop, simulate LdThis by calling OP implementation directly.
- // Do not have module id here so use the global id, 0.
- //
- Var thisArg = JavascriptOperators::OP_GetThisNoFastPath(args[0], 0, scriptContext);
- RecyclableObject* obj = RecyclableObject::FromVar(thisArg);
- Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- Var setterFunc = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
- if (!JavascriptConversion::IsCallable(setterFunc))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.__defineSetter__"));
- }
- PropertyDescriptor propertyDescriptor;
- propertyDescriptor.SetEnumerable(true);
- propertyDescriptor.SetConfigurable(true);
- propertyDescriptor.SetSetter(setterFunc);
- DefineOwnPropertyHelper(obj, propertyRecord->GetPropertyId(), propertyDescriptor, scriptContext);
- return obj->GetLibrary()->GetUndefined();
- }
- // args[1] property name.
- Var JavascriptObject::EntryLookupGetter(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- RecyclableObject* obj = nullptr;
- if (!JavascriptConversion::ToObject(args[0], scriptContext, &obj))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.__lookupGetter__"));
- }
- Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- Var getter = nullptr;
- Var unused = nullptr;
- if (JavascriptOperators::GetAccessors(obj, propertyRecord->GetPropertyId(), scriptContext, &getter, &unused))
- {
- if (getter != nullptr)
- {
- return getter;
- }
- }
- return obj->GetLibrary()->GetUndefined();
- }
- // args[1] property name.
- Var JavascriptObject::EntryLookupSetter(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- RecyclableObject* obj = nullptr;
- if (!JavascriptConversion::ToObject(args[0], scriptContext, &obj))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.__lookupSetter__"));
- }
- Var propertyKey = args.Info.Count > 1 ? args[1] : obj->GetLibrary()->GetUndefined();
- const PropertyRecord* propertyRecord;
- JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord);
- Var unused = nullptr;
- Var setter = nullptr;
- if (JavascriptOperators::GetAccessors(obj, propertyRecord->GetPropertyId(), scriptContext, &unused, &setter))
- {
- if (setter != nullptr)
- {
- return setter;
- }
- }
- return obj->GetLibrary()->GetUndefined();
- }
- Var JavascriptObject::EntryIs(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- Var x = args.Info.Count > 1 ? args[1] : scriptContext->GetLibrary()->GetUndefined();
- Var y = args.Info.Count > 2 ? args[2] : scriptContext->GetLibrary()->GetUndefined();
- return JavascriptBoolean::ToVar(JavascriptConversion::SameValue(x, y), scriptContext);
- }
- //ES6 19.1.2.1
- Var JavascriptObject::EntryAssign(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Assert(!(callInfo.Flags & CallFlags_New));
- // 1. Let to be ToObject(target).
- // 2. ReturnIfAbrupt(to).
- // 3 If only one argument was passed, return to.
- RecyclableObject* to = nullptr;
- if (args.Info.Count == 1 || !JavascriptConversion::ToObject(args[1], scriptContext, &to))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.assign"));
- }
- if (args.Info.Count < 3)
- {
- return to;
- }
- // 4. Let sources be the List of argument values starting with the second argument.
- // 5. For each element nextSource of sources, in ascending index order,
- for (unsigned int i = 2; i < args.Info.Count; i++)
- {
- // a. If nextSource is undefined or null, let keys be an empty List.
- // b. Else,
- // i.Let from be ToObject(nextSource).
- // ii.ReturnIfAbrupt(from).
- // iii.Let keys be from.[[OwnPropertyKeys]]().
- // iv.ReturnIfAbrupt(keys).
- if (JavascriptOperators::IsUndefinedOrNull(args[i]))
- {
- continue;
- }
- RecyclableObject* from = nullptr;
- if (!JavascriptConversion::ToObject(args[i], scriptContext, &from))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.assign"));
- }
- #if ENABLE_COPYONACCESS_ARRAY
- JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(from);
- #endif
- // if proxy, take slow path by calling [[OwnPropertyKeys]] on source
- if (JavascriptProxy::Is(from))
- {
- AssignForProxyObjects(from, to, scriptContext);
- }
- // else use enumerator to extract keys from source
- else
- {
- AssignForGenericObjects(from, to, scriptContext);
- }
- }
- // 6. Return to.
- return to;
- }
- void JavascriptObject::AssignForGenericObjects(RecyclableObject* from, RecyclableObject* to, ScriptContext* scriptContext)
- {
- JavascriptStaticEnumerator enumerator;
- if (!from->GetEnumerator(&enumerator, EnumeratorFlags::SnapShotSemantics | EnumeratorFlags::EnumSymbols, scriptContext))
- {
- //nothing to enumerate, continue with the nextSource.
- return;
- }
- PropertyId nextKey = Constants::NoProperty;
- Var propValue = nullptr;
- Var propertyVar = nullptr;
- //enumerate through each property of properties and fetch the property descriptor
- while ((propertyVar = enumerator.MoveAndGetNext(nextKey)) != NULL)
- {
- if (nextKey == Constants::NoProperty)
- {
- if (JavascriptOperators::IsUndefinedObject(propertyVar)) //There are some code paths in which GetCurrentIndex can return undefined
- {
- continue;
- }
- PropertyRecord const * propertyRecord = nullptr;
- JavascriptString* propertyName = JavascriptString::FromVar(propertyVar);
- scriptContext->GetOrAddPropertyRecord(propertyName->GetString(), propertyName->GetLength(), &propertyRecord);
- nextKey = propertyRecord->GetPropertyId();
- }
- if (!JavascriptOperators::GetOwnProperty(from, nextKey, &propValue, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
- }
- if (!JavascriptOperators::SetProperty(to, to, nextKey, propValue, scriptContext, PropertyOperationFlags::PropertyOperation_ThrowIfNonWritable))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
- }
- }
- }
- void JavascriptObject::AssignForProxyObjects(RecyclableObject* from, RecyclableObject* to, ScriptContext* scriptContext)
- {
- JavascriptArray *keys = JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(from, scriptContext);
- // c. Repeat for each element nextKey of keys in List order,
- // i. Let desc be from.[[GetOwnProperty]](nextKey).
- // ii. ReturnIfAbrupt(desc).
- // iii. if desc is not undefined and desc.[[Enumerable]] is true, then
- // 1. Let propValue be Get(from, nextKey).
- // 2. ReturnIfAbrupt(propValue).
- // 3. Let status be Set(to, nextKey, propValue, true);
- // 4. ReturnIfAbrupt(status).
- uint32 length = keys->GetLength();
- Var nextKey;
- const PropertyRecord* propertyRecord = nullptr;
- PropertyId propertyId;
- Var propValue = nullptr;
- for (uint32 j = 0; j < length; j++)
- {
- PropertyDescriptor propertyDescriptor;
- nextKey = keys->DirectGetItem(j);
- 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)");
- // 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
- JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
- propertyId = propertyRecord->GetPropertyId();
- AssertMsg(propertyId != Constants::NoProperty, "AssignForProxyObjects - OwnPropertyKeys returned a propertyId with value NoProperty.");
- if (JavascriptOperators::GetOwnPropertyDescriptor(from, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
- {
- if (propertyDescriptor.IsEnumerable())
- {
- if (!JavascriptOperators::GetOwnProperty(from, propertyId, &propValue, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
- }
- if (!JavascriptOperators::SetProperty(to, to, propertyId, propValue, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("Object.assign"));
- }
- }
- }
- }
- }
- //ES5 15.2.3.5
- Var JavascriptObject::EntryCreate(RecyclableObject* function, CallInfo callInfo, ...)
- {
- PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
- ARGUMENTS(args, callInfo);
- ScriptContext* scriptContext = function->GetScriptContext();
- Recycler *recycler = scriptContext->GetRecycler();
- CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_create)
- Assert(!(callInfo.Flags & CallFlags_New));
- if (args.Info.Count < 2)
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
- }
- TypeId typeId = JavascriptOperators::GetTypeId(args[1]);
- if (typeId != TypeIds_Null && !JavascriptOperators::IsObjectType(typeId))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NotObjectOrNull, _u("Object.create"));
- }
- //Create a new DynamicType with first argument as prototype and non shared type
- RecyclableObject *prototype = RecyclableObject::FromVar(args[1]);
- DynamicType *objectType = DynamicType::New(scriptContext, TypeIds_Object, prototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), false);
- //Create a new Object using this type.
- DynamicObject* object = DynamicObject::New(recycler, objectType);
- JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
- #if ENABLE_DEBUG_CONFIG_OPTIONS
- if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
- {
- object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
- }
- #endif
- if (args.Info.Count > 2 && JavascriptOperators::GetTypeId(args[2]) != TypeIds_Undefined)
- {
- RecyclableObject* properties = nullptr;
- if (FALSE == JavascriptConversion::ToObject(args[2], scriptContext, &properties))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NullOrUndefined, _u("Object.create"));
- }
- return DefinePropertiesHelper(object, properties, scriptContext);
- }
- return object;
- }
- Var JavascriptObject::DefinePropertiesHelper(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
- {
- if (JavascriptProxy::Is(props))
- {
- return DefinePropertiesHelperForProxyObjects(object, props, scriptContext);
- }
- else
- {
- return DefinePropertiesHelperForGenericObjects(object, props, scriptContext);
- }
- }
- Var JavascriptObject::DefinePropertiesHelperForGenericObjects(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
- {
- size_t descSize = 16;
- size_t descCount = 0;
- struct DescriptorMap
- {
- PropertyRecord const * propRecord;
- PropertyDescriptor descriptor;
- Var originalVar;
- };
- JavascriptStaticEnumerator enumerator;
- if (!props->GetEnumerator(&enumerator, EnumeratorFlags::EnumSymbols, scriptContext))
- {
- return object;
- }
- ENTER_PINNED_SCOPE(DescriptorMap, descriptors);
- descriptors = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, descSize);
- PropertyId propId;
- PropertyRecord const * propertyRecord;
- JavascriptString* propertyName = nullptr;
- RecyclableObject *undefined = scriptContext->GetLibrary()->GetUndefined();
- Var tempVar;
- //enumerate through each property of properties and fetch the property descriptor
- while ((tempVar = enumerator.MoveAndGetNext(propId)) != NULL)
- {
- if (propId == Constants::NoProperty) //try current property id query first
- {
- if (!JavascriptOperators::IsUndefinedObject(tempVar, undefined)) //There are some enumerators returning propertyName but not propId
- {
- propertyName = JavascriptString::FromVar(tempVar);
- scriptContext->GetOrAddPropertyRecord(propertyName->GetString(), propertyName->GetLength(), &propertyRecord);
- propId = propertyRecord->GetPropertyId();
- }
- else
- {
- continue;
- }
- }
- else
- {
- propertyRecord = scriptContext->GetPropertyName(propId);
- }
- if (descCount == descSize)
- {
- //reallocate - consider linked list of DescriptorMap if the descSize is too high
- descSize = AllocSizeMath::Mul(descCount, 2);
- __analysis_assume(descSize == descCount * 2);
- DescriptorMap *temp = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, descSize);
- for (size_t i = 0; i < descCount; i++)
- {
- temp[i] = descriptors[i];
- }
- descriptors = temp;
- }
- tempVar = JavascriptOperators::GetProperty(props, propId, scriptContext);
- if (!JavascriptOperators::ToPropertyDescriptor(tempVar, &descriptors[descCount].descriptor, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propId)->GetBuffer());
- }
- // In proxy, we need to get back the original ToPropertDescriptor var in [[defineProperty]] trap.
- descriptors[descCount].originalVar = tempVar;
- if (CONFIG_FLAG(UseFullName))
- {
- ModifyGetterSetterFuncName(propertyRecord, descriptors[descCount].descriptor, scriptContext);
- }
- descriptors[descCount].propRecord = propertyRecord;
- descCount++;
- }
- //Once all the property descriptors are in place set each property descriptor to the object
- for (size_t i = 0; i < descCount; i++)
- {
- DefineOwnPropertyHelper(object, descriptors[i].propRecord->GetPropertyId(), descriptors[i].descriptor, scriptContext);
- }
- LEAVE_PINNED_SCOPE();
- return object;
- }
- //ES5 15.2.3.7
- Var JavascriptObject::DefinePropertiesHelperForProxyObjects(RecyclableObject *object, RecyclableObject* props, ScriptContext *scriptContext)
- {
- Assert(JavascriptProxy::Is(props));
- //1. If Type(O) is not Object throw a TypeError exception.
- //2. Let props be ToObject(Properties).
- size_t descCount = 0;
- struct DescriptorMap
- {
- PropertyRecord const * propRecord;
- PropertyDescriptor descriptor;
- };
- //3. Let keys be props.[[OwnPropertyKeys]]().
- //4. ReturnIfAbrupt(keys).
- //5. Let descriptors be an empty List.
- JavascriptArray* keys = JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(props, scriptContext);
- uint32 length = keys->GetLength();
- ENTER_PINNED_SCOPE(DescriptorMap, descriptors);
- descriptors = RecyclerNewArray(scriptContext->GetRecycler(), DescriptorMap, length);
- //6. Repeat for each element nextKey of keys in List order,
- // 1. Let propDesc be props.[[GetOwnProperty]](nextKey).
- // 2. ReturnIfAbrupt(propDesc).
- // 3. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
- // 1. Let descObj be Get(props, nextKey).
- // 2. ReturnIfAbrupt(descObj).
- // 3. Let desc be ToPropertyDescriptor(descObj).
- // 4. ReturnIfAbrupt(desc).
- // 5. Append the pair(a two element List) consisting of nextKey and desc to the end of descriptors.
- Var nextKey;
- const PropertyRecord* propertyRecord = nullptr;
- PropertyId propertyId;
- Var descObj;
- for (uint32 j = 0; j < length; j++)
- {
- PropertyDescriptor propertyDescriptor;
- nextKey = keys->DirectGetItem(j);
- 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)");
- JavascriptConversion::ToPropertyKey(nextKey, scriptContext, &propertyRecord);
- propertyId = propertyRecord->GetPropertyId();
- AssertMsg(propertyId != Constants::NoProperty, "DefinePropertiesHelper - OwnPropertyKeys returned a propertyId with value NoProperty.");
- if (JavascriptOperators::GetOwnPropertyDescriptor(props, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
- {
- if (propertyDescriptor.IsEnumerable())
- {
- descObj = JavascriptOperators::GetProperty(props, propertyId, scriptContext);
- if (!JavascriptOperators::ToPropertyDescriptor(descObj, &descriptors[descCount].descriptor, scriptContext))
- {
- JavascriptError::ThrowTypeError(scriptContext, JSERR_PropertyDescriptor_Invalid, scriptContext->GetPropertyName(propertyId)->GetBuffer());
- }
- if (CONFIG_FLAG(UseFullName))
- {
- ModifyGetterSetterFuncName(propertyRecord, descriptors[descCount].descriptor, scriptContext);
- }
- descriptors[descCount].propRecord = propertyRecord;
- descCount++;
- }
- }
- }
- //7. For each pair from descriptors in list order,
- // 1. Let P be the first element of pair.
- // 2. Let desc be the second element of pair.
- // 3. Let status be DefinePropertyOrThrow(O, P, desc).
- // 4. ReturnIfAbrupt(status).
- for (size_t i = 0; i < descCount; i++)
- {
- DefineOwnPropertyHelper(object, descriptors[i].propRecord->GetPropertyId(), descriptors[i].descriptor, scriptContext);
- }
- LEAVE_PINNED_SCOPE();
- //8. Return O.
- return object;
- }
- Var JavascriptObject::GetPrototypeOf(RecyclableObject* obj, ScriptContext* scriptContext)
- {
- return obj->IsExternal() ? obj->GetConfigurablePrototype(scriptContext) : obj->GetPrototype();
- }
- //
- // Check if "proto" is a prototype of "object" (on its prototype chain).
- //
- bool JavascriptObject::IsPrototypeOf(RecyclableObject* proto, RecyclableObject* object, ScriptContext* scriptContext)
- {
- return JavascriptOperators::MapObjectAndPrototypesUntil<false>(object, [=](RecyclableObject* obj)
- {
- return obj == proto;
- });
- }
- static const size_t ConstructNameGetSetLength = 5; // 5 = 1 ( for .) + 3 (get or set) + 1 for null)
- /*static*/
- char16 * JavascriptObject::ConstructName(const PropertyRecord * propertyRecord, const char16 * getOrSetStr, ScriptContext* scriptContext)
- {
- Assert(propertyRecord);
- Assert(scriptContext);
- char16 * finalName = nullptr;
- size_t propertyLength = (size_t)propertyRecord->GetLength();
- if (propertyLength > 0)
- {
- size_t totalChars;
- if (SizeTAdd(propertyLength, ConstructNameGetSetLength, &totalChars) == S_OK)
- {
- finalName = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, totalChars);
- Assert(finalName != nullptr);
- const char16* propertyName = propertyRecord->GetBuffer();
- Assert(propertyName != nullptr);
- wcscpy_s(finalName, totalChars, propertyName);
- Assert(getOrSetStr != nullptr);
- Assert(wcslen(getOrSetStr) == 4);
- wcscpy_s(finalName + propertyLength, ConstructNameGetSetLength, getOrSetStr);
- }
- }
- return finalName;
- }
- /*static*/
- void JavascriptObject::ModifyGetterSetterFuncName(const PropertyRecord * propertyRecord, const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
- {
- Assert(scriptContext);
- Assert(propertyRecord);
- if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
- {
- charcount_t propertyLength = propertyRecord->GetLength();
- if (descriptor.GetterSpecified()
- && Js::ScriptFunction::Is(descriptor.GetGetter())
- && _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->GetDisplayName(), _u("get")) == 0)
- {
- // modify to name.get
- char16* finalName = ConstructName(propertyRecord, _u(".get"), scriptContext);
- if (finalName != nullptr)
- {
- FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
- Js::ScriptFunction::FromVar(descriptor.GetGetter())->GetFunctionProxy()->SetDisplayName(finalName,
- propertyLength + 4 /*".get"*/, propertyLength + 1, flags);
- }
- }
- if (descriptor.SetterSpecified()
- && Js::ScriptFunction::Is(descriptor.GetSetter())
- && _wcsicmp(Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->GetDisplayName(), _u("set")) == 0)
- {
- // modify to name.set
- char16* finalName = ConstructName(propertyRecord, _u(".set"), scriptContext);
- if (finalName != nullptr)
- {
- FunctionProxy::SetDisplayNameFlags flags = (FunctionProxy::SetDisplayNameFlags) (FunctionProxy::SetDisplayNameFlagsDontCopy | FunctionProxy::SetDisplayNameFlagsRecyclerAllocated);
- Js::ScriptFunction::FromVar(descriptor.GetSetter())->GetFunctionProxy()->SetDisplayName(finalName,
- propertyLength + 4 /*".set"*/, propertyLength + 1, flags);
- }
- }
- }
- }
- BOOL JavascriptObject::DefineOwnPropertyHelper(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, ScriptContext* scriptContext, bool throwOnError /* = true*/)
- {
- BOOL returnValue;
- obj->ThrowIfCannotDefineProperty(propId, descriptor);
- Type* oldType = obj->GetType();
- obj->ClearWritableDataOnlyDetectionBit();
- // HostDispatch: it doesn't support changing property attributes and default attributes are not per ES5,
- // so there is no benefit in using ES5 DefineOwnPropertyDescriptor for it, use old implementation.
- if (TypeIds_HostDispatch != obj->GetTypeId())
- {
- if (DynamicObject::IsAnyArray(obj))
- {
- returnValue = JavascriptOperators::DefineOwnPropertyForArray(
- JavascriptArray::FromAnyArray(obj), propId, descriptor, throwOnError, scriptContext);
- }
- else
- {
- returnValue = JavascriptOperators::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
- if (propId == PropertyIds::__proto__)
- {
- scriptContext->GetLibrary()->GetObjectPrototypeObject()->PostDefineOwnProperty__proto__(obj);
- }
- }
- }
- else
- {
- returnValue = JavascriptOperators::SetPropertyDescriptor(obj, propId, descriptor);
- }
- if (propId == PropertyIds::_symbolSpecies && obj == scriptContext->GetLibrary()->GetArrayConstructor())
- {
- scriptContext->GetLibrary()->SetArrayObjectHasUserDefinedSpecies(true);
- }
- if (obj->IsWritableDataOnlyDetectionBitSet())
- {
- if (obj->GetType() == oldType)
- {
- // Also, if the object's type has not changed, we need to ensure that
- // the cached property string for this property, if any, does not
- // specify this object's type.
- scriptContext->InvalidatePropertyStringCache(propId, obj->GetType());
- }
- }
- if (descriptor.IsAccessorDescriptor())
- {
- scriptContext->optimizationOverrides.SetSideEffects(Js::SideEffects_Accessor);
- }
- return returnValue;
- }
- }
|