JSONParser.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. #include "JSON.h"
  7. #include "JSONParser.h"
  8. using namespace Js;
  9. namespace JSON
  10. {
  11. // -------- Parser implementation ------------//
  12. void JSONParser::Finalizer()
  13. {
  14. m_scanner.Finalizer();
  15. if(arenaAllocatorObject)
  16. {
  17. this->scriptContext->ReleaseTemporaryGuestAllocator(arenaAllocatorObject);
  18. }
  19. }
  20. Js::Var JSONParser::Parse(LPCWSTR str, uint length)
  21. {
  22. if (length > MIN_CACHE_LENGTH)
  23. {
  24. if (!this->arenaAllocatorObject)
  25. {
  26. this->arenaAllocatorObject = scriptContext->GetTemporaryGuestAllocator(_u("JSONParse"));
  27. this->arenaAllocator = arenaAllocatorObject->GetAllocator();
  28. }
  29. }
  30. m_scanner.Init(str, length, &m_token, scriptContext, str, this->arenaAllocator);
  31. Scan();
  32. Js::Var ret = ParseObject();
  33. if (m_token.tk != tkEOF)
  34. {
  35. m_scanner.ThrowSyntaxError(JSERR_JsonSyntax);
  36. }
  37. return ret;
  38. }
  39. Js::Var JSONParser::Parse(Js::JavascriptString* input)
  40. {
  41. return Parse(input->GetSz(), input->GetLength());
  42. }
  43. Js::Var JSONParser::Walk(Js::JavascriptString* name, Js::PropertyId id, Js::Var holder, uint32 index)
  44. {
  45. AssertMsg(reviver, "JSON post parse walk with null reviver");
  46. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault);
  47. Js::Var value;
  48. Js::Var values[3];
  49. Js::Arguments args(0, values);
  50. Js::RecyclableObject *undefined = scriptContext->GetLibrary()->GetUndefined();
  51. if (Js::DynamicObject::IsAnyArray(holder))
  52. {
  53. // when called from an array the key is NULL and the keyId is the index.
  54. value = Js::JavascriptArray::FromAnyArray(holder)->DirectGetItem(id);
  55. name = scriptContext->GetIntegerString(id);
  56. }
  57. else
  58. {
  59. AssertMsg(Js::JavascriptOperators::GetTypeId(holder) == Js::TypeIds_Object || Js::JavascriptOperators::GetTypeId(holder) == Js::TypeIds_Arguments,
  60. "The holder argument in a JSON::Walk function must be an object or an array");
  61. if (id == Constants::NoProperty)
  62. {
  63. if (!Js::VarTo<Js::RecyclableObject>(holder)->GetItem(holder, index, &value, scriptContext))
  64. {
  65. value = undefined;
  66. }
  67. }
  68. else
  69. {
  70. if (!Js::VarTo<Js::RecyclableObject>(holder)->GetProperty(holder, id, &value, NULL, scriptContext))
  71. {
  72. value = undefined;
  73. }
  74. }
  75. }
  76. // this is a post order walk. Visit the children before calling walk on this object
  77. if (Js::DynamicObject::IsAnyArray(value))
  78. {
  79. Js::JavascriptArray* arrayVal = JavascriptArray::EnsureNonNativeArray(Js::JavascriptArray::FromAnyArray(value));
  80. Assert(!Js::VarIs<Js::JavascriptNativeIntArray>(arrayVal) && !Js::VarIs<Js::JavascriptNativeFloatArray>(arrayVal));
  81. uint length = arrayVal->GetLength();
  82. if (!arrayVal->IsCrossSiteObject())
  83. {
  84. for(uint k = 0; k < length; k++)
  85. {
  86. Js::Var newElement = Walk(0, k, value);
  87. if(Js::JavascriptOperators::IsUndefinedObject(newElement, undefined))
  88. {
  89. arrayVal->DirectDeleteItemAt<Js::Var>(k);
  90. }
  91. else
  92. {
  93. arrayVal->DirectSetItemAt(k, newElement);
  94. }
  95. }
  96. }
  97. else
  98. {
  99. for(uint k = 0; k < length; k++)
  100. {
  101. Js::Var newElement = Walk(0, k, value);
  102. if(Js::JavascriptOperators::IsUndefinedObject(newElement, undefined))
  103. {
  104. arrayVal->DirectDeleteItemAt<Js::Var>(k);
  105. }
  106. else
  107. {
  108. arrayVal->SetItem(k, newElement, Js::PropertyOperation_None);
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. Js::TypeId typeId = Js::JavascriptOperators::GetTypeId(value);
  116. if (typeId == Js::TypeIds_Object || typeId == Js::TypeIds_Arguments)
  117. {
  118. Js::JavascriptStaticEnumerator enumerator;
  119. // normally we should have a JSON object here and the enumerator should be always be successful. However, the objects can be
  120. // modified by user code. It is better to skip a damaged object. ES5 spec doesn't specify an error here.
  121. if(Js::VarTo<Js::RecyclableObject>(value)->GetEnumerator(&enumerator, EnumeratorFlags::SnapShotSemantics, scriptContext))
  122. {
  123. Js::JavascriptString * propertyName;
  124. while (true)
  125. {
  126. Js::PropertyId idMember = Js::Constants::NoProperty;
  127. propertyName = enumerator.MoveAndGetNext(idMember);
  128. if (propertyName == nullptr)
  129. {
  130. break;
  131. }
  132. //NOTE: If testing key value call enumerator->GetCurrentValue() to confirm value is correct;
  133. if (idMember != Js::Constants::NoProperty)
  134. {
  135. Js::Var newElement = Walk(propertyName, idMember, value);
  136. if (Js::JavascriptOperators::IsUndefinedObject(newElement, undefined))
  137. {
  138. Js::JavascriptOperators::DeleteProperty(Js::VarTo<Js::RecyclableObject>(value), idMember);
  139. }
  140. else
  141. {
  142. Js::JavascriptOperators::SetProperty(value, Js::VarTo<Js::RecyclableObject>(value), idMember, newElement, scriptContext);
  143. }
  144. }
  145. // For the numeric cases the enumerator is set to a NullEnumerator (see class in ForInObjectEnumerator.h)
  146. // Numerals do not have property Ids so we need to set and delete items
  147. else
  148. {
  149. uint32 propertyIndex = enumerator.GetCurrentItemIndex();
  150. AssertMsg(Js::JavascriptArray::InvalidIndex != propertyIndex, "Not a numeric type");
  151. Js::Var newElement = Walk(propertyName, idMember, value, propertyIndex);
  152. if (Js::JavascriptOperators::IsUndefinedObject(newElement, undefined))
  153. {
  154. Js::JavascriptOperators::DeleteItem(Js::VarTo<Js::RecyclableObject>(value), propertyIndex);
  155. }
  156. else
  157. {
  158. Js::JavascriptOperators::SetItem(value, Js::VarTo<Js::RecyclableObject>(value), propertyIndex, newElement, scriptContext);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. // apply reviver on this node now
  166. args.Info.Count = 3;
  167. args.Values[0] = holder;
  168. args.Values[1] = name;
  169. args.Values[2] = value;
  170. BEGIN_SAFE_REENTRANT_CALL(reviver->GetScriptContext()->GetThreadContext())
  171. {
  172. value = Js::JavascriptFunction::CallFunction<true>(reviver, reviver->GetEntryPoint(), args);
  173. }
  174. END_SAFE_REENTRANT_CALL
  175. return value;
  176. }
  177. Js::Var JSONParser::ParseObject()
  178. {
  179. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault);
  180. Js::Var retVal;
  181. switch (m_token.tk)
  182. {
  183. case tkFltCon:
  184. retVal = Js::JavascriptNumber::ToVarIntCheck(m_token.GetDouble(), scriptContext);
  185. Scan();
  186. return retVal;
  187. case tkStrCon:
  188. {
  189. // will auto-null-terminate the string (as length=len+1)
  190. uint len = m_scanner.GetCurrentStringLen();
  191. retVal = Js::JavascriptString::NewCopyBuffer(m_scanner.GetCurrentString(), len, scriptContext);
  192. Scan();
  193. return retVal;
  194. }
  195. case tkTRUE:
  196. retVal = scriptContext->GetLibrary()->GetTrue();
  197. Scan();
  198. return retVal;
  199. case tkFALSE:
  200. retVal = scriptContext->GetLibrary()->GetFalse();
  201. Scan();
  202. return retVal;
  203. case tkNULL:
  204. retVal = scriptContext->GetLibrary()->GetNull();
  205. Scan();
  206. return retVal;
  207. case tkSub: // unary minus
  208. if (Scan() == tkFltCon)
  209. {
  210. retVal = Js::JavascriptNumber::ToVarIntCheck(-m_token.GetDouble(), scriptContext);
  211. Scan();
  212. return retVal;
  213. }
  214. else
  215. {
  216. m_scanner.ThrowSyntaxError(JSERR_JsonBadNumber);
  217. }
  218. case tkLBrack:
  219. {
  220. Js::JavascriptArray* arrayObj = scriptContext->GetLibrary()->CreateArray(0);
  221. //skip '['
  222. Scan();
  223. //iterate over the array members, get JSON objects and add them in the pArrayMemberList
  224. uint k = 0;
  225. while (true)
  226. {
  227. if(tkRBrack == m_token.tk)
  228. {
  229. break;
  230. }
  231. Js::Var value = ParseObject();
  232. arrayObj->SetItem(k++, value, Js::PropertyOperation_None);
  233. // if next token is not a comma consider the end of the array member list.
  234. if (tkComma != m_token.tk)
  235. break;
  236. Scan();
  237. if(tkRBrack == m_token.tk)
  238. {
  239. m_scanner.ThrowSyntaxError(JSERR_JsonIllegalChar);
  240. }
  241. }
  242. //check and consume the ending ']'
  243. CheckCurrentToken(tkRBrack, JSERR_JsonNoRbrack);
  244. return arrayObj;
  245. }
  246. case tkLCurly:
  247. {
  248. // Parse an object, "{"name1" : ObjMember1, "name2" : ObjMember2, ...} "
  249. if(IsCaching())
  250. {
  251. if(!typeCacheList)
  252. {
  253. typeCacheList = Anew(this->arenaAllocator, JsonTypeCacheList, this->arenaAllocator, 8);
  254. }
  255. }
  256. // first, create the object
  257. Js::DynamicObject* object = scriptContext->GetLibrary()->CreateObject();
  258. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  259. #if ENABLE_DEBUG_CONFIG_OPTIONS
  260. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  261. {
  262. object = VarTo<DynamicObject>(JavascriptProxy::AutoProxyWrapper(object));
  263. }
  264. #endif
  265. //next token after '{'
  266. Scan();
  267. //if empty object "{}" return;
  268. if(tkRCurly == m_token.tk)
  269. {
  270. Scan();
  271. return object;
  272. }
  273. JsonTypeCache* previousCache = nullptr;
  274. JsonTypeCache* currentCache = nullptr;
  275. //parse the list of members
  276. while(true)
  277. {
  278. // parse a list member: "name" : ObjMember
  279. // and add it to the object.
  280. //pick "name"
  281. if(tkStrCon != m_token.tk)
  282. {
  283. m_scanner.ThrowSyntaxError(JSERR_JsonIllegalChar);
  284. }
  285. // currentStrLength = length w/o null-termination
  286. WCHAR* currentStr = m_scanner.GetCurrentString();
  287. uint currentStrLength = m_scanner.GetCurrentStringLen();
  288. DynamicType* typeWithoutProperty = object->GetDynamicType();
  289. if(IsCaching())
  290. {
  291. if(!previousCache)
  292. {
  293. // This is the first property in the list - see if we have an existing cache for it.
  294. currentCache = typeCacheList->LookupWithKey(Js::HashedCharacterBuffer<WCHAR>(currentStr, currentStrLength), nullptr);
  295. }
  296. if(currentCache && currentCache->typeWithoutProperty == typeWithoutProperty &&
  297. currentCache->propertyRecord->Equals(JsUtil::CharacterBuffer<WCHAR>(currentStr, currentStrLength)))
  298. {
  299. //check and consume ":"
  300. if(Scan() != tkColon )
  301. {
  302. m_scanner.ThrowSyntaxError(JSERR_JsonNoColon);
  303. }
  304. Scan();
  305. // Cache all values from currentCache as there is a chance that ParseObject might change the cache
  306. DynamicType* typeWithProperty = currentCache->typeWithProperty;
  307. PropertyId propertyId = currentCache->propertyRecord->GetPropertyId();
  308. PropertyIndex propertyIndex = currentCache->propertyIndex;
  309. previousCache = currentCache;
  310. currentCache = currentCache->next;
  311. // fast path for type transition and property set
  312. object->EnsureSlots(typeWithoutProperty->GetTypeHandler()->GetSlotCapacity(),
  313. typeWithProperty->GetTypeHandler()->GetSlotCapacity(), scriptContext, typeWithProperty->GetTypeHandler());
  314. object->ReplaceType(typeWithProperty);
  315. Js::Var value = ParseObject();
  316. object->SetSlot(SetSlotArguments(propertyId, propertyIndex, value));
  317. // if the next token is not a comma consider the list of members done.
  318. if (tkComma != m_token.tk)
  319. break;
  320. Scan();
  321. continue;
  322. }
  323. }
  324. // slow path
  325. Js::PropertyRecord const * propertyRecord;
  326. scriptContext->GetOrAddPropertyRecord(currentStr, currentStrLength, &propertyRecord);
  327. //check and consume ":"
  328. if(Scan() != tkColon )
  329. {
  330. m_scanner.ThrowSyntaxError(JSERR_JsonNoColon);
  331. }
  332. Scan();
  333. Js::Var value = ParseObject();
  334. PropertyValueInfo info;
  335. object->SetProperty(propertyRecord->GetPropertyId(), value, PropertyOperation_None, &info);
  336. DynamicType* typeWithProperty = object->GetDynamicType();
  337. if(IsCaching() && !propertyRecord->IsNumeric() && !info.IsNoCache() && typeWithProperty->GetIsShared() && typeWithProperty->GetTypeHandler()->IsPathTypeHandler())
  338. {
  339. PropertyIndex propertyIndex = info.GetPropertyIndex();
  340. if(!previousCache)
  341. {
  342. // This is the first property in the set add it to the dictionary.
  343. currentCache = JsonTypeCache::New(this->arenaAllocator, propertyRecord, typeWithoutProperty, typeWithProperty, propertyIndex);
  344. typeCacheList->AddNew(propertyRecord, currentCache);
  345. }
  346. else if(!currentCache)
  347. {
  348. currentCache = JsonTypeCache::New(this->arenaAllocator, propertyRecord, typeWithoutProperty, typeWithProperty, propertyIndex);
  349. previousCache->next = currentCache;
  350. }
  351. else
  352. {
  353. // cache miss!!
  354. currentCache->Update(propertyRecord, typeWithoutProperty, typeWithProperty, propertyIndex);
  355. }
  356. previousCache = currentCache;
  357. currentCache = currentCache->next;
  358. }
  359. // if the next token is not a comma consider the list of members done.
  360. if (tkComma != m_token.tk)
  361. break;
  362. Scan();
  363. }
  364. // check and consume the ending '}"
  365. CheckCurrentToken(tkRCurly, JSERR_JsonNoRcurly);
  366. return object;
  367. }
  368. default:
  369. m_scanner.ThrowSyntaxError(JSERR_JsonSyntax);
  370. }
  371. }
  372. } // namespace JSON