#include "config.h"
#include "ParserArena.h"
#include "CatchScope.h"
#include "JSBigInt.h"
#include "JSCInlines.h"
#include "Nodes.h"
namespace JSC {
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(IdentifierArena);
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(ParserArena);
ParserArena::ParserArena()
: m_freeableMemory(nullptr)
, m_freeablePoolEnd(nullptr)
{
}
inline void* ParserArena::freeablePool()
{
ASSERT(m_freeablePoolEnd);
return m_freeablePoolEnd - freeablePoolSize;
}
inline void ParserArena::deallocateObjects()
{
size_t size = m_deletableObjects.size();
for (size_t i = 0; i < size; ++i)
m_deletableObjects[i]->~ParserArenaDeletable();
if (m_freeablePoolEnd)
ParserArenaMalloc::free(freeablePool());
size = m_freeablePools.size();
for (size_t i = 0; i < size; ++i)
ParserArenaMalloc::free(m_freeablePools[i]);
}
ParserArena::~ParserArena()
{
deallocateObjects();
}
void ParserArena::allocateFreeablePool()
{
if (m_freeablePoolEnd)
m_freeablePools.append(freeablePool());
char* pool = static_cast<char*>(ParserArenaMalloc::malloc(freeablePoolSize));
m_freeableMemory = pool;
m_freeablePoolEnd = pool + freeablePoolSize;
ASSERT(freeablePool() == pool);
}
const Identifier& IdentifierArena::makeBigIntDecimalIdentifier(VM& vm, const Identifier& identifier, uint8_t radix)
{
if (radix == 10)
return identifier;
auto scope = DECLARE_CATCH_SCOPE(vm);
JSValue bigInt = JSBigInt::parseInt(nullptr, vm, identifier.string(), radix, JSBigInt::ErrorParseMode::ThrowExceptions, JSBigInt::ParseIntSign::Unsigned);
scope.assertNoException();
JSBigInt* heapBigInt;
#if USE(BIGINT32)
if (bigInt.isBigInt32()) {
heapBigInt = JSBigInt::tryCreateFrom(vm, bigInt.bigInt32AsInt32());
RELEASE_ASSERT(heapBigInt);
} else
#endif
heapBigInt = bigInt.asHeapBigInt();
m_identifiers.append(Identifier::fromString(vm, JSBigInt::tryGetString(vm, heapBigInt, 10)));
return m_identifiers.last();
}
const Identifier& IdentifierArena::makePrivateIdentifier(VM& vm, ASCIILiteral prefix, unsigned identifier)
{
String symbolName = makeString(prefix, identifier);
auto symbol = vm.privateSymbolRegistry().symbolForKey(symbolName);
m_identifiers.append(Identifier::fromUid(symbol));
return m_identifiers.last();
}
}