StringCell* StringCell::fromSymbol(World &world, SymbolCell *symbol) { alloc::SymbolRef symbolRef(world, symbol); void *cellPlacement = alloc::allocateCells(world); if (symbolRef->dataIsInline()) { auto inlineSymbol = static_cast<InlineSymbolCell*>(symbolRef.data()); auto inlineString = new (cellPlacement) InlineStringCell( inlineSymbol->inlineByteLength(), inlineSymbol->inlineCharLength() ); // Copy the inline data over const void *srcData = inlineSymbol->inlineData(); memcpy(inlineString->inlineData(), srcData, inlineSymbol->inlineByteLength()); return inlineString; } else { auto heapSymbol = static_cast<HeapSymbolCell*>(symbolRef.data()); // Share the heap symbols's byte array return new (cellPlacement) HeapStringCell( heapSymbol->heapByteArray()->ref(), heapSymbol->heapByteLength(), heapSymbol->heapCharLength() ); } }
SymbolCell* SymbolCell::copy(alloc::Heap &heap) { void *cellPlacement = heap.allocate(); if (dataIsInline()) { auto inlineThis = static_cast<InlineSymbolCell*>(this); auto inlineCopy = new (cellPlacement) InlineSymbolCell( inlineThis->inlineByteLength(), inlineThis->inlineCharLength() ); memcpy(inlineCopy->m_inlineData, constUtf8Data(), inlineThis->inlineByteLength()); return inlineCopy; } else { auto heapThis = static_cast<HeapSymbolCell*>(this); return new (cellPlacement) HeapSymbolCell( heapThis->heapByteArray()->ref(), heapThis->heapByteLength(), heapThis->heapCharLength() ); } }
SymbolCell* SymbolCell::fromString(World &world, StringCell *string) { void *cellPlacement = alloc::allocateCells(world); // Symbols must have the same inlining threshold as strings for the below logic to work static_assert( sizeof(InlineSymbolCell::m_inlineData) == sizeof(InlineStringCell::m_inlineData), "Symbols and strings must have the same inlining threshold" ); if (string->dataIsInline()) { auto inlineString = static_cast<InlineStringCell*>(string); auto inlineSymbol = new (cellPlacement) InlineSymbolCell( inlineString->inlineByteLength(), inlineString->inlineCharLength() ); // Copy the inline data over const void *srcData = inlineString->inlineData(); memcpy(inlineSymbol->inlineData(), srcData, inlineString->inlineByteLength()); return inlineSymbol; } else { auto heapString = static_cast<HeapStringCell*>(string); // Share the heap strings's byte array return new (cellPlacement) HeapSymbolCell( heapString->heapByteArray()->ref(), heapString->heapByteLength(), heapString->heapCharLength() ); } }