JavascriptRegularExpressionResult.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. JavascriptArray* JavascriptRegularExpressionResult::Create(
  9. void *const stackAllocationPointer,
  10. JavascriptString* input,
  11. ScriptContext* const scriptContext)
  12. {
  13. JavascriptArray* arr = JavascriptArray::New<JavascriptArray, InlineSlotCount>(stackAllocationPointer, 0, scriptContext->GetLibrary()->GetRegexResultType()); // use default array capacity
  14. Assert(JavascriptRegularExpressionResult::Is(arr));
  15. arr->SetSlot(SetSlotArguments(BuiltInPropertyRecords::input.propertyRecord.GetPropertyId(), InputIndex, input));
  16. // SetMatch must be called with a valid match before the object is given to script code
  17. // Each item will be filled in by the match loop
  18. return arr;
  19. }
  20. JavascriptArray* JavascriptRegularExpressionResult::Create(
  21. void *const stackAllocationPointer,
  22. const int numGroups,
  23. JavascriptString* input,
  24. ScriptContext* const scriptContext)
  25. {
  26. Assert(numGroups > 0);
  27. JavascriptArray* arr = JavascriptArray::NewLiteral<JavascriptArray, InlineSlotCount>(stackAllocationPointer, numGroups, scriptContext->GetLibrary()->GetRegexResultType());
  28. Assert(JavascriptRegularExpressionResult::Is(arr));
  29. arr->SetSlot(SetSlotArguments(BuiltInPropertyRecords::input.propertyRecord.GetPropertyId(), InputIndex, input));
  30. // SetMatch must be called with a valid match before the object is given to script code
  31. // Each item will be filled in by the match loop
  32. return arr;
  33. }
  34. #if DEBUG
  35. //
  36. // Check if a given JavascriptArray is actually JavascriptRegularExpressionResult.
  37. //
  38. bool JavascriptRegularExpressionResult::Is(JavascriptArray* arr)
  39. {
  40. return arr->GetPropertyIndex(PropertyIds::input) == InputIndex
  41. && arr->GetPropertyIndex(PropertyIds::index) == IndexIndex;
  42. }
  43. #endif
  44. void JavascriptRegularExpressionResult::SetMatch(JavascriptArray* arr, const UnifiedRegex::GroupInfo match)
  45. {
  46. Assert(JavascriptRegularExpressionResult::Is(arr));
  47. Assert(!match.IsUndefined());
  48. ScriptContext* scriptContext = arr->GetScriptContext();
  49. arr->SetSlot(SetSlotArguments(BuiltInPropertyRecords::index.propertyRecord.GetPropertyId(), IndexIndex, JavascriptNumber::ToVar(match.offset, scriptContext)));
  50. }
  51. void JavascriptRegularExpressionResult::InstantiateForceInlinedMembers()
  52. {
  53. // Force-inlined functions defined in a translation unit need a reference from an extern non-force-inlined function in
  54. // the same translation unit to force an instantiation of the force-inlined function. Otherwise, if the force-inlined
  55. // function is not referenced in the same translation unit, it will not be generated and the linker is not able to find
  56. // the definition to inline the function in other translation units.
  57. Assert(false);
  58. Create(nullptr, nullptr, nullptr);
  59. Create(nullptr, 0, nullptr, nullptr);
  60. }
  61. CompileAssert(JavascriptRegularExpressionResult::InlineSlotCount >= 2);
  62. }