JavascriptSymbol.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. PropertyRecordUsageCache * JavascriptSymbol::GetPropertyRecordUsageCache()
  9. {
  10. return &this->propertyRecordUsageCache;
  11. }
  12. bool JavascriptSymbol::Is(Var aValue)
  13. {
  14. return JavascriptOperators::GetTypeId(aValue) == TypeIds_Symbol;
  15. }
  16. JavascriptSymbol* JavascriptSymbol::FromVar(Js::Var aValue)
  17. {
  18. AssertOrFailFastMsg(Is(aValue), "Ensure var is actually a 'JavascriptSymbol'");
  19. return static_cast<JavascriptSymbol *>(aValue);
  20. }
  21. JavascriptSymbol* JavascriptSymbol::UnsafeFromVar(Js::Var aValue)
  22. {
  23. AssertMsg(Is(aValue), "Ensure var is actually a 'JavascriptSymbol'");
  24. return static_cast<JavascriptSymbol *>(aValue);
  25. }
  26. Var JavascriptSymbol::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  27. {
  28. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  29. ARGUMENTS(args, callInfo);
  30. ScriptContext* scriptContext = function->GetScriptContext();
  31. AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
  32. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Symbol, scriptContext);
  33. // SkipDefaultNewObject function flag should have prevented the default object from
  34. // being created, except when call true a host dispatch.
  35. JavascriptOperators::GetAndAssertIsConstructorSuperCall(args);
  36. if (callInfo.Flags & CallFlags_New)
  37. {
  38. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew, _u("Symbol"));
  39. }
  40. JavascriptString* description;
  41. if (args.Info.Count > 1 && !JavascriptOperators::IsUndefined(args[1]))
  42. {
  43. description = JavascriptConversion::ToString(args[1], scriptContext);
  44. }
  45. else
  46. {
  47. description = scriptContext->GetLibrary()->GetEmptyString();
  48. }
  49. return scriptContext->GetLibrary()->CreateSymbol(description);
  50. }
  51. // Symbol.prototype.valueOf as described in ES 2015
  52. Var JavascriptSymbol::EntryValueOf(RecyclableObject* function, CallInfo callInfo, ...)
  53. {
  54. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  55. ARGUMENTS(args, callInfo);
  56. ScriptContext* scriptContext = function->GetScriptContext();
  57. Assert(!(callInfo.Flags & CallFlags_New));
  58. if (JavascriptSymbol::Is(args[0]))
  59. {
  60. return args[0];
  61. }
  62. else if (JavascriptSymbolObject::Is(args[0]))
  63. {
  64. JavascriptSymbolObject* obj = JavascriptSymbolObject::FromVar(args[0]);
  65. return CrossSite::MarshalVar(scriptContext, obj->Unwrap(), obj->GetScriptContext());
  66. }
  67. else
  68. {
  69. return TryInvokeRemotelyOrThrow(EntryValueOf, scriptContext, args, JSERR_This_NeedSymbol, _u("Symbol.prototype.valueOf"));
  70. }
  71. }
  72. // Symbol.prototype.toString as described in ES 2015
  73. Var JavascriptSymbol::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
  74. {
  75. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  76. ARGUMENTS(args, callInfo);
  77. AssertMsg(args.Info.Count, "Should always have implicit 'this'.");
  78. ScriptContext* scriptContext = function->GetScriptContext();
  79. Assert(!(callInfo.Flags & CallFlags_New));
  80. const PropertyRecord* val;
  81. Var aValue = args[0];
  82. if (JavascriptSymbol::Is(aValue))
  83. {
  84. val = JavascriptSymbol::FromVar(aValue)->GetValue();
  85. }
  86. else if (JavascriptSymbolObject::Is(aValue))
  87. {
  88. val = JavascriptSymbolObject::FromVar(aValue)->GetValue();
  89. }
  90. else
  91. {
  92. return TryInvokeRemotelyOrThrow(EntryToString, scriptContext, args, JSERR_This_NeedSymbol, _u("Symbol.prototype.toString"));
  93. }
  94. return JavascriptSymbol::ToString(val, scriptContext);
  95. }
  96. // Symbol.for as described in ES 2015
  97. Var JavascriptSymbol::EntryFor(RecyclableObject* function, CallInfo callInfo, ...)
  98. {
  99. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  100. ARGUMENTS(args, callInfo);
  101. AssertMsg(args.Info.Count, "Should always have implicit 'this'.");
  102. ScriptContext* scriptContext = function->GetScriptContext();
  103. JavascriptLibrary* library = scriptContext->GetLibrary();
  104. Assert(!(callInfo.Flags & CallFlags_New));
  105. JavascriptString* key;
  106. if (args.Info.Count > 1)
  107. {
  108. key = JavascriptConversion::ToString(args[1], scriptContext);
  109. }
  110. else
  111. {
  112. key = library->GetUndefinedDisplayString();
  113. }
  114. // Search the global symbol registration map for a symbol with description equal to the string key.
  115. // The map can only have one symbol with that description so if we found a symbol, that is the registered
  116. // symbol for the string key.
  117. const Js::PropertyRecord* propertyRecord = scriptContext->GetThreadContext()->GetSymbolFromRegistrationMap(key->GetString(), key->GetLength());
  118. // If we didn't find a PropertyRecord in the map, we'll create a new symbol with description equal to the key string.
  119. // This is the only place we add new PropertyRecords to the map, so we should never have multiple PropertyRecords in the
  120. // map with the same string key value (since we would return the one we found above instead of creating a new one).
  121. if (propertyRecord == nullptr)
  122. {
  123. propertyRecord = scriptContext->GetThreadContext()->AddSymbolToRegistrationMap(key->GetString(), key->GetLength());
  124. }
  125. Assert(propertyRecord != nullptr);
  126. return scriptContext->GetSymbol(propertyRecord);
  127. }
  128. // Symbol.keyFor as described in ES 2015
  129. Var JavascriptSymbol::EntryKeyFor(RecyclableObject* function, CallInfo callInfo, ...)
  130. {
  131. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  132. ARGUMENTS(args, callInfo);
  133. AssertMsg(args.Info.Count, "Should always have implicit 'this'.");
  134. ScriptContext* scriptContext = function->GetScriptContext();
  135. JavascriptLibrary* library = scriptContext->GetLibrary();
  136. Assert(!(callInfo.Flags & CallFlags_New));
  137. if (args.Info.Count < 2 || !JavascriptSymbol::Is(args[1]))
  138. {
  139. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedSymbol, _u("Symbol.keyFor"));
  140. }
  141. JavascriptSymbol* sym = JavascriptSymbol::FromVar(args[1]);
  142. const Js::PropertyRecord* symPropertyRecord = sym->GetValue();
  143. const char16* key = symPropertyRecord->GetBuffer();
  144. const charcount_t keyLength = symPropertyRecord->GetLength();
  145. // Search the global symbol registration map for a key equal to the description of the symbol passed into Symbol.keyFor.
  146. // Symbol.for creates a new symbol with description equal to the key and uses that key as a mapping to the new symbol.
  147. // There will only be one symbol in the map with that string key value.
  148. const Js::PropertyRecord* propertyRecord = scriptContext->GetThreadContext()->GetSymbolFromRegistrationMap(key, keyLength);
  149. // If we found a PropertyRecord in the map, make sure it is the same symbol that was passed to Symbol.keyFor.
  150. // If the two are different, it means the symbol passed to keyFor has the same description as a symbol registered via
  151. // Symbol.for _but_ is not the symbol returned from Symbol.for.
  152. if (propertyRecord != nullptr && propertyRecord == sym->GetValue())
  153. {
  154. return JavascriptString::NewCopyBuffer(key, sym->GetValue()->GetLength(), scriptContext);
  155. }
  156. return library->GetUndefined();
  157. }
  158. // Symbol.prototype[@@toPrimitive] as described in ES 2015
  159. Var JavascriptSymbol::EntrySymbolToPrimitive(RecyclableObject* function, CallInfo callInfo, ...)
  160. {
  161. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  162. ARGUMENTS(args, callInfo);
  163. ScriptContext* scriptContext = function->GetScriptContext();
  164. Assert(!(callInfo.Flags & CallFlags_New));
  165. if (JavascriptSymbol::Is(args[0]))
  166. {
  167. return args[0];
  168. }
  169. else if (JavascriptSymbolObject::Is(args[0]))
  170. {
  171. JavascriptSymbolObject* obj = JavascriptSymbolObject::FromVar(args[0]);
  172. return CrossSite::MarshalVar(scriptContext, obj->Unwrap(), obj->GetScriptContext());
  173. }
  174. else
  175. {
  176. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedSymbol, _u("Symbol[Symbol.toPrimitive]"));
  177. }
  178. }
  179. RecyclableObject * JavascriptSymbol::CloneToScriptContext(ScriptContext* requestContext)
  180. {
  181. // PropertyRecords are per-ThreadContext so we can just create a new primitive wrapper
  182. // around the PropertyRecord stored in this symbol via the other context library.
  183. return requestContext->GetSymbol(this->GetValue());
  184. }
  185. Var JavascriptSymbol::TryInvokeRemotelyOrThrow(JavascriptMethod entryPoint, ScriptContext * scriptContext, Arguments & args, int32 errorCode, PCWSTR varName)
  186. {
  187. if (JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch)
  188. {
  189. Var result;
  190. if (RecyclableObject::FromVar(args[0])->InvokeBuiltInOperationRemotely(entryPoint, args, &result))
  191. {
  192. return result;
  193. }
  194. }
  195. // Don't error if we disabled implicit calls
  196. if (scriptContext->GetThreadContext()->RecordImplicitException())
  197. {
  198. JavascriptError::ThrowTypeError(scriptContext, errorCode, varName);
  199. }
  200. else
  201. {
  202. return scriptContext->GetLibrary()->GetUndefined();
  203. }
  204. }
  205. BOOL JavascriptSymbol::Equals(Var other, BOOL* value, ScriptContext * requestContext)
  206. {
  207. return JavascriptSymbol::Equals(this, other, value, requestContext);
  208. }
  209. BOOL JavascriptSymbol::Equals(JavascriptSymbol* left, Var right, BOOL* value, ScriptContext * requestContext)
  210. {
  211. TypeId typeId = JavascriptOperators::GetTypeId(right);
  212. if (typeId != TypeIds_Symbol && typeId != TypeIds_SymbolObject)
  213. {
  214. right = JavascriptConversion::ToPrimitive<JavascriptHint::None>(right, requestContext);
  215. typeId = JavascriptOperators::GetTypeId(right);
  216. }
  217. switch (typeId)
  218. {
  219. case TypeIds_Symbol:
  220. *value = left == JavascriptSymbol::UnsafeFromVar(right);
  221. Assert((left->GetValue() == JavascriptSymbol::UnsafeFromVar(right)->GetValue()) == *value);
  222. break;
  223. case TypeIds_SymbolObject:
  224. *value = left == JavascriptSymbol::UnsafeFromVar(JavascriptSymbolObject::UnsafeFromVar(right)->Unwrap());
  225. Assert((left->GetValue() == JavascriptSymbolObject::UnsafeFromVar(right)->GetValue()) == *value);
  226. break;
  227. default:
  228. *value = FALSE;
  229. break;
  230. }
  231. return TRUE;
  232. }
  233. BOOL JavascriptSymbol::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  234. {
  235. if (this->GetValue())
  236. {
  237. stringBuilder->AppendCppLiteral(_u("Symbol("));
  238. stringBuilder->Append(this->GetValue()->GetBuffer(), this->GetValue()->GetLength());
  239. stringBuilder->Append(_u(')'));
  240. }
  241. return TRUE;
  242. }
  243. BOOL JavascriptSymbol::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
  244. {
  245. stringBuilder->AppendCppLiteral(_u("Symbol"));
  246. return TRUE;
  247. }
  248. RecyclableObject* JavascriptSymbol::ToObject(ScriptContext * requestContext)
  249. {
  250. return requestContext->GetLibrary()->CreateSymbolObject(this);
  251. }
  252. Var JavascriptSymbol::GetTypeOfString(ScriptContext * requestContext)
  253. {
  254. return requestContext->GetLibrary()->GetSymbolTypeDisplayString();
  255. }
  256. JavascriptString* JavascriptSymbol::ToString(ScriptContext * requestContext)
  257. {
  258. if (requestContext->GetThreadContext()->RecordImplicitException())
  259. {
  260. JavascriptError::ThrowTypeError(requestContext, VBSERR_OLENoPropOrMethod, _u("ToString"));
  261. }
  262. return requestContext->GetLibrary()->GetEmptyString();
  263. }
  264. JavascriptString* JavascriptSymbol::ToString(const PropertyRecord* propertyRecord, ScriptContext * requestContext)
  265. {
  266. const char16* description = propertyRecord->GetBuffer();
  267. uint len = propertyRecord->GetLength();
  268. CompoundString* str = CompoundString::NewWithCharCapacity(len + _countof(_u("Symbol()")), requestContext->GetLibrary());
  269. str->AppendChars(_u("Symbol("), _countof(_u("Symbol(")) - 1);
  270. str->AppendChars(description, len);
  271. str->AppendChars(_u(')'));
  272. return str;
  273. }
  274. } // namespace Js