SourceProviderCacheItem.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef SourceProviderCacheItem_h
  26. #define SourceProviderCacheItem_h
  27. #include "ParserTokens.h"
  28. #include <wtf/PassOwnPtr.h>
  29. #include <wtf/Vector.h>
  30. #include <wtf/text/WTFString.h>
  31. namespace JSC {
  32. struct SourceProviderCacheItemCreationParameters {
  33. unsigned functionStart;
  34. unsigned closeBraceLine;
  35. unsigned closeBraceOffset;
  36. unsigned closeBraceLineStartOffset;
  37. bool needsFullActivation;
  38. bool usesEval;
  39. bool strictMode;
  40. Vector<RefPtr<StringImpl> > usedVariables;
  41. Vector<RefPtr<StringImpl> > writtenVariables;
  42. };
  43. #if COMPILER(MSVC)
  44. #pragma warning(push)
  45. #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
  46. #endif
  47. class SourceProviderCacheItem {
  48. WTF_MAKE_FAST_ALLOCATED;
  49. public:
  50. static PassOwnPtr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&);
  51. ~SourceProviderCacheItem();
  52. JSToken closeBraceToken() const
  53. {
  54. JSToken token;
  55. token.m_type = CLOSEBRACE;
  56. token.m_data.offset = closeBraceOffset;
  57. token.m_location.startOffset = closeBraceOffset;
  58. token.m_location.endOffset = closeBraceOffset + 1;
  59. token.m_location.line = closeBraceLine;
  60. token.m_location.lineStartOffset = closeBraceLineStartOffset;
  61. // token.m_location.sourceOffset is initialized once by the client. So,
  62. // we do not need to set it here.
  63. return token;
  64. }
  65. unsigned functionStart : 31;
  66. bool needsFullActivation : 1;
  67. unsigned closeBraceLine : 31;
  68. bool usesEval : 1;
  69. unsigned closeBraceOffset : 31;
  70. bool strictMode : 1;
  71. unsigned closeBraceLineStartOffset;
  72. unsigned usedVariablesCount;
  73. unsigned writtenVariablesCount;
  74. StringImpl** usedVariables() const { return const_cast<StringImpl**>(m_variables); }
  75. StringImpl** writtenVariables() const { return const_cast<StringImpl**>(&m_variables[usedVariablesCount]); }
  76. private:
  77. SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&);
  78. StringImpl* m_variables[0];
  79. };
  80. inline SourceProviderCacheItem::~SourceProviderCacheItem()
  81. {
  82. for (unsigned i = 0; i < usedVariablesCount + writtenVariablesCount; ++i)
  83. m_variables[i]->deref();
  84. }
  85. inline PassOwnPtr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters)
  86. {
  87. size_t variableCount = parameters.writtenVariables.size() + parameters.usedVariables.size();
  88. size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(StringImpl*) * variableCount;
  89. void* slot = fastMalloc(objectSize);
  90. return adoptPtr(new (slot) SourceProviderCacheItem(parameters));
  91. }
  92. inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters)
  93. : functionStart(parameters.functionStart)
  94. , needsFullActivation(parameters.needsFullActivation)
  95. , closeBraceLine(parameters.closeBraceLine)
  96. , usesEval(parameters.usesEval)
  97. , closeBraceOffset(parameters.closeBraceOffset)
  98. , strictMode(parameters.strictMode)
  99. , closeBraceLineStartOffset(parameters.closeBraceLineStartOffset)
  100. , usedVariablesCount(parameters.usedVariables.size())
  101. , writtenVariablesCount(parameters.writtenVariables.size())
  102. {
  103. unsigned j = 0;
  104. for (unsigned i = 0; i < usedVariablesCount; ++i, ++j) {
  105. m_variables[j] = parameters.usedVariables[i].get();
  106. m_variables[j]->ref();
  107. }
  108. for (unsigned i = 0; i < writtenVariablesCount; ++i, ++j) {
  109. m_variables[j] = parameters.writtenVariables[i].get();
  110. m_variables[j]->ref();
  111. }
  112. }
  113. #if COMPILER(MSVC)
  114. #pragma warning(pop)
  115. #endif
  116. }
  117. #endif // SourceProviderCacheItem_h