JavascriptLibrary.inl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #pragma once
  6. namespace Js
  7. {
  8. // Create a string literal from a C++ string (const wchar_t array with compile-time determined size)
  9. // Note: The template arg is the string length in characters, including the NUL terminator.
  10. template< size_t N > JavascriptString* JavascriptLibrary::CreateStringFromCppLiteral(const wchar_t(&value)[N]) const
  11. {
  12. CompileAssert(N>2); // Other values are handled by the specializations below
  13. return LiteralString::New(GetStringTypeStatic(), value, N - 1 /*don't include terminating NUL*/, this->GetRecycler());
  14. }
  15. // Specialization for the empty string
  16. template<> JavascriptString* JavascriptLibrary::CreateStringFromCppLiteral(const wchar_t(&value)[1]) const
  17. {
  18. return GetEmptyString();
  19. }
  20. // Specialization for single-char strings
  21. template<> JavascriptString* JavascriptLibrary::CreateStringFromCppLiteral(const wchar_t(&value)[2]) const
  22. {
  23. return charStringCache.GetStringForChar(value[0]);
  24. }
  25. template <size_t N>
  26. JavascriptFunction * JavascriptLibrary::AddFunctionToLibraryObjectWithPropertyName(DynamicObject* object, const wchar_t(&propertyName)[N], FunctionInfo * functionInfo, int length)
  27. {
  28. // The PID need to be tracked because it is assigned to the runtime function's nameId
  29. return AddFunctionToLibraryObject(object, scriptContext->GetOrAddPropertyIdTracked(propertyName), functionInfo, length);
  30. }
  31. #if ENABLE_COPYONACCESS_ARRAY
  32. template <>
  33. inline void JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(const Var instance)
  34. {
  35. if (instance && JavascriptCopyOnAccessNativeIntArray::Is(instance))
  36. {
  37. JavascriptCopyOnAccessNativeIntArray::FromVar(instance)->ConvertCopyOnAccessSegment();
  38. }
  39. }
  40. template <typename T>
  41. void JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(const T instance)
  42. {
  43. // dummy template function
  44. }
  45. #endif
  46. }