JavascriptStringObject.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. JavascriptStringObject::JavascriptStringObject(DynamicType * type)
  9. : DynamicObject(type), value(nullptr)
  10. {
  11. Assert(type->GetTypeId() == TypeIds_StringObject);
  12. this->GetTypeHandler()->ClearHasOnlyWritableDataProperties(); // length is non-writable
  13. if(GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag)
  14. {
  15. // No need to invalidate store field caches for non-writable properties here. Since this type is just being created, it cannot represent
  16. // an object that is already a prototype. If it becomes a prototype and then we attempt to add a property to an object derived from this
  17. // object, then we will check if this property is writable, and only if it is will we do the fast path for add property.
  18. GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
  19. }
  20. }
  21. JavascriptStringObject::JavascriptStringObject(JavascriptString* value, DynamicType * type)
  22. : DynamicObject(type), value(value)
  23. {
  24. Assert(type->GetTypeId() == TypeIds_StringObject);
  25. this->GetTypeHandler()->ClearHasOnlyWritableDataProperties(); // length is non-writable
  26. if(GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag)
  27. {
  28. // No need to invalidate store field caches for non-writable properties here. Since this type is just being created, it cannot represent
  29. // an object that is already a prototype. If it becomes a prototype and then we attempt to add a property to an object derived from this
  30. // object, then we will check if this property is writable, and only if it is will we do the fast path for add property.
  31. GetLibrary()->NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties();
  32. }
  33. }
  34. bool JavascriptStringObject::Is(Var aValue)
  35. {
  36. return JavascriptOperators::GetTypeId(aValue) == TypeIds_StringObject;
  37. }
  38. JavascriptStringObject* JavascriptStringObject::FromVar(Var aValue)
  39. {
  40. AssertMsg(Is(aValue), "Ensure var is actually a 'JavascriptString'");
  41. return static_cast<JavascriptStringObject *>(RecyclableObject::FromVar(aValue));
  42. }
  43. void JavascriptStringObject::Initialize(JavascriptString* value)
  44. {
  45. Assert(this->value == nullptr);
  46. this->value = value;
  47. }
  48. JavascriptString* JavascriptStringObject::InternalUnwrap()
  49. {
  50. if (value == nullptr)
  51. {
  52. ScriptContext* scriptContext = GetScriptContext();
  53. value = scriptContext->GetLibrary()->GetEmptyString();
  54. }
  55. return value;
  56. }
  57. /*static*/
  58. PropertyId const JavascriptStringObject::specialPropertyIds[] =
  59. {
  60. PropertyIds::length
  61. };
  62. bool JavascriptStringObject::IsValidIndex(PropertyId propertyId, bool conditionMetBehavior)
  63. {
  64. ScriptContext*scriptContext = GetScriptContext();
  65. uint32 index;
  66. if (scriptContext->IsNumericPropertyId(propertyId, &index))
  67. {
  68. if (index < (uint32)this->InternalUnwrap()->GetLength())
  69. {
  70. return conditionMetBehavior;
  71. }
  72. }
  73. return !conditionMetBehavior;
  74. }
  75. BOOL JavascriptStringObject::HasProperty(PropertyId propertyId)
  76. {
  77. if (propertyId == PropertyIds::length)
  78. {
  79. return true;
  80. }
  81. if (DynamicObject::HasProperty(propertyId))
  82. {
  83. return true;
  84. }
  85. return JavascriptStringObject::IsValidIndex(propertyId, true);
  86. }
  87. DescriptorFlags JavascriptStringObject::GetSetter(PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  88. {
  89. DescriptorFlags flags;
  90. if (GetSetterBuiltIns(propertyId, info, &flags))
  91. {
  92. return flags;
  93. }
  94. uint32 index;
  95. if (requestContext->IsNumericPropertyId(propertyId, &index))
  96. {
  97. return JavascriptStringObject::GetItemSetter(index, setterValue, requestContext);
  98. }
  99. return DynamicObject::GetSetter(propertyId, setterValue, info, requestContext);
  100. }
  101. DescriptorFlags JavascriptStringObject::GetSetter(JavascriptString* propertyNameString, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  102. {
  103. DescriptorFlags flags;
  104. PropertyRecord const* propertyRecord;
  105. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  106. if (propertyRecord != nullptr)
  107. {
  108. PropertyId propertyId = propertyRecord->GetPropertyId();
  109. if (GetSetterBuiltIns(propertyId, info, &flags))
  110. {
  111. return flags;
  112. }
  113. uint32 index;
  114. if (requestContext->IsNumericPropertyId(propertyId, &index))
  115. {
  116. return JavascriptStringObject::GetItemSetter(index, setterValue, requestContext);
  117. }
  118. }
  119. return DynamicObject::GetSetter(propertyNameString, setterValue, info, requestContext);
  120. }
  121. bool JavascriptStringObject::GetSetterBuiltIns(PropertyId propertyId, PropertyValueInfo* info, DescriptorFlags* descriptorFlags)
  122. {
  123. if (propertyId == PropertyIds::length)
  124. {
  125. PropertyValueInfo::SetNoCache(info, this);
  126. *descriptorFlags = Data;
  127. return true;
  128. }
  129. return false;
  130. }
  131. BOOL JavascriptStringObject::IsConfigurable(PropertyId propertyId)
  132. {
  133. if (propertyId == PropertyIds::length)
  134. {
  135. return false;
  136. }
  137. // From DynamicObject::IsConfigurable we can't tell if the result is from a property or just default
  138. // value. Call HasProperty to find out.
  139. if (DynamicObject::HasProperty(propertyId))
  140. {
  141. return DynamicObject::IsConfigurable(propertyId);
  142. }
  143. return JavascriptStringObject::IsValidIndex(propertyId, false);
  144. }
  145. BOOL JavascriptStringObject::IsEnumerable(PropertyId propertyId)
  146. {
  147. if (propertyId == PropertyIds::length)
  148. {
  149. return false;
  150. }
  151. // Index properties of String objects are always enumerable, same as default value. No need to test.
  152. return DynamicObject::IsEnumerable(propertyId);
  153. }
  154. BOOL JavascriptStringObject::IsWritable(PropertyId propertyId)
  155. {
  156. if (propertyId == PropertyIds::length)
  157. {
  158. return false;
  159. }
  160. // From DynamicObject::IsWritable we can't tell if the result is from a property or just default
  161. // value. Call HasProperty to find out.
  162. if (DynamicObject::HasProperty(propertyId))
  163. {
  164. return DynamicObject::IsWritable(propertyId);
  165. }
  166. return JavascriptStringObject::IsValidIndex(propertyId, false);
  167. }
  168. BOOL JavascriptStringObject::GetSpecialPropertyName(uint32 index, Var *propertyName, ScriptContext * requestContext)
  169. {
  170. if (index == 0)
  171. {
  172. *propertyName = requestContext->GetPropertyString(PropertyIds::length);
  173. return true;
  174. }
  175. return false;
  176. }
  177. // Returns the number of special non-enumerable properties this type has.
  178. uint JavascriptStringObject::GetSpecialPropertyCount() const
  179. {
  180. return _countof(specialPropertyIds);
  181. }
  182. // Returns the list of special non-enumerable properties for the type.
  183. PropertyId const * JavascriptStringObject::GetSpecialPropertyIds() const
  184. {
  185. return specialPropertyIds;
  186. }
  187. BOOL JavascriptStringObject::GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  188. {
  189. return JavascriptStringObject::GetProperty(originalInstance, propertyId, value, info, requestContext);
  190. }
  191. BOOL JavascriptStringObject::GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  192. {
  193. BOOL result;
  194. if (GetPropertyBuiltIns(propertyId, value, requestContext, &result))
  195. {
  196. return result;
  197. }
  198. if (DynamicObject::GetProperty(originalInstance, propertyId, value, info, requestContext))
  199. {
  200. return true;
  201. }
  202. // For NumericPropertyIds check that index is less than JavascriptString length
  203. ScriptContext*scriptContext = GetScriptContext();
  204. uint32 index;
  205. if (scriptContext->IsNumericPropertyId(propertyId, &index))
  206. {
  207. JavascriptString* str = JavascriptString::FromVar(CrossSite::MarshalVar(requestContext, this->InternalUnwrap()));
  208. return str->GetItemAt(index, value);
  209. }
  210. *value = requestContext->GetLibrary()->GetUndefined();
  211. return false;
  212. }
  213. BOOL JavascriptStringObject::GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  214. {
  215. AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetString(), propertyNameString->GetLength()),
  216. "Numeric property names should have been converted to uint or PropertyRecord*");
  217. BOOL result;
  218. PropertyRecord const* propertyRecord;
  219. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  220. if (propertyRecord != nullptr && GetPropertyBuiltIns(propertyRecord->GetPropertyId(), value, requestContext, &result))
  221. {
  222. return result;
  223. }
  224. return DynamicObject::GetProperty(originalInstance, propertyNameString, value, info, requestContext);
  225. }
  226. bool JavascriptStringObject::GetPropertyBuiltIns(PropertyId propertyId, Var* value, ScriptContext* requestContext, BOOL* result)
  227. {
  228. if (propertyId == PropertyIds::length)
  229. {
  230. *value = JavascriptNumber::ToVar(this->InternalUnwrap()->GetLength(), requestContext);
  231. *result = true;
  232. return true;
  233. }
  234. return false;
  235. }
  236. BOOL JavascriptStringObject::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  237. {
  238. bool result;
  239. if (SetPropertyBuiltIns(propertyId, flags, &result))
  240. {
  241. return result;
  242. }
  243. return DynamicObject::SetProperty(propertyId, value, flags, info);
  244. }
  245. BOOL JavascriptStringObject::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  246. {
  247. bool result;
  248. PropertyRecord const* propertyRecord;
  249. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  250. if (propertyRecord != nullptr && SetPropertyBuiltIns(propertyRecord->GetPropertyId(), flags, &result))
  251. {
  252. return result;
  253. }
  254. return DynamicObject::SetProperty(propertyNameString, value, flags, info);
  255. }
  256. bool JavascriptStringObject::SetPropertyBuiltIns(PropertyId propertyId, PropertyOperationFlags flags, bool* result)
  257. {
  258. if (propertyId == PropertyIds::length)
  259. {
  260. JavascriptError::ThrowCantAssignIfStrictMode(flags, this->GetScriptContext());
  261. *result = false;
  262. return true;
  263. }
  264. return false;
  265. }
  266. BOOL JavascriptStringObject::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
  267. {
  268. if (propertyId == PropertyIds::length)
  269. {
  270. JavascriptError::ThrowCantDeleteIfStrictMode(flags, this->GetScriptContext(), this->GetScriptContext()->GetPropertyName(propertyId)->GetBuffer());
  271. return FALSE;
  272. }
  273. return DynamicObject::DeleteProperty(propertyId, flags);
  274. }
  275. BOOL JavascriptStringObject::HasItem(uint32 index)
  276. {
  277. if (this->InternalUnwrap()->HasItem(index))
  278. {
  279. return true;
  280. }
  281. return DynamicObject::HasItem(index);
  282. }
  283. BOOL JavascriptStringObject::GetItem(Var originalInstance, uint32 index, Var* value, ScriptContext* requestContext)
  284. {
  285. JavascriptString* str = JavascriptString::FromVar(CrossSite::MarshalVar(requestContext, this->InternalUnwrap()));
  286. if (str->GetItemAt(index, value))
  287. {
  288. return true;
  289. }
  290. return DynamicObject::GetItem(originalInstance, index, value, requestContext);
  291. }
  292. BOOL JavascriptStringObject::GetItemReference(Var originalInstance, uint32 index, Var* value, ScriptContext* requestContext)
  293. {
  294. return this->GetItem(originalInstance, index, value, requestContext);
  295. }
  296. DescriptorFlags JavascriptStringObject::GetItemSetter(uint32 index, Var* setterValue, ScriptContext* requestContext)
  297. {
  298. if (index < (uint32)this->InternalUnwrap()->GetLength())
  299. {
  300. return DescriptorFlags::Data;
  301. }
  302. return DynamicObject::GetItemSetter(index, setterValue, requestContext);
  303. }
  304. BOOL JavascriptStringObject::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
  305. {
  306. if (index < (uint32)this->InternalUnwrap()->GetLength())
  307. {
  308. return false;
  309. }
  310. return DynamicObject::SetItem(index, value, flags);
  311. }
  312. BOOL JavascriptStringObject::GetEnumerator(BOOL enumNonEnumerable, Var* enumerator, ScriptContext * requestContext, bool preferSnapshotSemantics, bool enumSymbols)
  313. {
  314. *enumerator = RecyclerNew(GetScriptContext()->GetRecycler(), JavascriptStringObjectEnumerator, this, requestContext, enumNonEnumerable, enumSymbols);
  315. return true;
  316. }
  317. BOOL JavascriptStringObject::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  318. {
  319. stringBuilder->Append(L'"');
  320. stringBuilder->Append(this->InternalUnwrap()->GetString(), this->InternalUnwrap()->GetLength());
  321. stringBuilder->Append(L'"');
  322. return TRUE;
  323. }
  324. BOOL JavascriptStringObject::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  325. {
  326. stringBuilder->AppendCppLiteral(L"String");
  327. return TRUE;
  328. }
  329. } // namespace Js