Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions src/parser/WASMComponentParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,55 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
uint32_t index;
};

struct CanonLess {
bool operator()(const Walrus::ComponentCanonOptions* first, const Walrus::ComponentCanonOptions* second) const
{
ASSERT(first != nullptr && second != nullptr);

uint8_t firstEncoding = static_cast<uint8_t>(first->encoding());
uint8_t secondEncoding = static_cast<uint8_t>(second->encoding());
if (firstEncoding != secondEncoding) {
return firstEncoding < secondEncoding;
}
if (first->isAsync() != second->isAsync()) {
return second->isAsync();
}
if (first->memoryIndex() != second->memoryIndex()) {
return first->memoryIndex() < second->memoryIndex();
}
if (first->reallocIndex() != second->reallocIndex()) {
return first->reallocIndex() < second->reallocIndex();
}
if (first->postReturnIndex() != second->postReturnIndex()) {
return first->postReturnIndex() < second->postReturnIndex();
}
return first->callbackIndex() < second->callbackIndex();
}
};

// Depth data for each component.
struct ComponentTypeInfo {
ComponentTypeInfo(ComponentTypeInfo* parent, Walrus::ComponentType* parentComponentType, Walrus::Component* parentComponent)
: parent(parent)
, parentComponentType(parentComponentType)
, parentComponent(parentComponent)
, coreInstanceCounter(0)
, canonOptionCounter(0)
{
}

ComponentTypeInfo* parent;
Walrus::ComponentType* parentComponentType;
Walrus::Component* parentComponent;
uint32_t coreInstanceCounter;
uint32_t canonOptionCounter;
std::vector<Walrus::FunctionType*> coreFuncTypes;
std::vector<bool> coreMemories;
std::vector<CoreInstanceType> coreInstanceTypes;
std::vector<Walrus::ComponentTypeFunc*> funcTypes;
std::vector<Walrus::ComponentType*> componentTypes;
std::vector<Walrus::ComponentType*> instanceTypes;
std::map<Walrus::ComponentCanonOptions*, uint32_t, CanonLess> m_canonOptions;
};

Walrus::ComponentTypeRef::Type getValueType(const ComponentType& type)
Expand Down Expand Up @@ -151,25 +180,25 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
return refType(type);
}

Walrus::CanonicalOptions parseCanonOptions(uint32_t optionCount, ComponentCanonOption* options)
uint32_t parseCanonOptions(uint32_t optionCount, ComponentCanonOption* options)
{
Walrus::CanonicalOptions::StringEncoding encoding = Walrus::CanonicalOptions::Utf8;
Walrus::ComponentCanonOptions::StringEncoding encoding = Walrus::ComponentCanonOptions::Utf8;
bool isAsync = false;
uint32_t memoryIndex = Walrus::CanonicalOptions::NoIndex;
uint32_t reallocIndex = Walrus::CanonicalOptions::NoIndex;
uint32_t postReturnIndex = Walrus::CanonicalOptions::NoIndex;
uint32_t callbackIndex = Walrus::CanonicalOptions::NoIndex;
uint32_t memoryIndex = Walrus::ComponentCanonOptions::NotDefined;
uint32_t reallocIndex = Walrus::ComponentCanonOptions::NotDefined;
uint32_t postReturnIndex = Walrus::ComponentCanonOptions::NotDefined;
uint32_t callbackIndex = Walrus::ComponentCanonOptions::NotDefined;

while (optionCount > 0) {
switch (options->option) {
case ComponentCanonOption::StrEncUtf8:
encoding = Walrus::CanonicalOptions::Utf8;
encoding = Walrus::ComponentCanonOptions::Utf8;
break;
case ComponentCanonOption::StrEncUtf16:
encoding = Walrus::CanonicalOptions::Utf16;
encoding = Walrus::ComponentCanonOptions::Utf16;
break;
case ComponentCanonOption::StrEncLatin1Utf16:
encoding = Walrus::CanonicalOptions::Latin1Utf16;
encoding = Walrus::ComponentCanonOptions::Latin1Utf16;
break;
case ComponentCanonOption::Memory:
memoryIndex = options->index;
Expand All @@ -190,7 +219,17 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
optionCount--;
options++;
}
return Walrus::CanonicalOptions(encoding, isAsync, memoryIndex, reallocIndex, postReturnIndex, callbackIndex);

Walrus::ComponentCanonOptions* canonOptions = new Walrus::ComponentCanonOptions(encoding, isAsync, memoryIndex, reallocIndex, postReturnIndex, callbackIndex);

auto it = m_currentInfo->m_canonOptions.insert(std::pair<Walrus::ComponentCanonOptions*, uint32_t>(canonOptions, m_currentInfo->canonOptionCounter));
if (it.second) {
m_currentComponent->pushDeclaration(canonOptions);
return m_currentInfo->canonOptionCounter++;
}

delete canonOptions;
return it.first->second;
}

Walrus::ComponentRefCounted* getTypeRef(ComponentSort sort, uint32_t index)
Expand Down Expand Up @@ -739,7 +778,7 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
ComponentCanonOption* options,
Index typeIndex)
{
Walrus::CanonicalOptions canonOptions = parseCanonOptions(optionCount, options);
uint32_t canonOptions = parseCanonOptions(optionCount, options);
Walrus::ComponentTypeFunc* funcType = m_current->getType(typeIndex)->asTypeFunc();
m_currentComponent->pushDeclaration(new Walrus::ComponentCanonLift(coreFuncIndex, canonOptions, funcType));
m_currentInfo->funcTypes.push_back(funcType);
Expand All @@ -749,7 +788,7 @@ class WASMComponentBinaryReader : public wabt::WASMComponentBinaryReaderDelegate
uint32_t optionCount,
ComponentCanonOption* options)
{
Walrus::CanonicalOptions canonOptions = parseCanonOptions(optionCount, options);
uint32_t canonOptions = parseCanonOptions(optionCount, options);
m_currentComponent->pushDeclaration(new Walrus::ComponentCanonLower(funcIndex, canonOptions));
}

Expand Down
136 changes: 75 additions & 61 deletions src/runtime/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,66 +543,12 @@ class ComponentType : public ComponentRefCounted {
std::vector<External> m_imports;
};

class CanonicalOptions {
public:
enum StringEncoding {
Utf8,
Utf16,
Latin1Utf16,
};

static constexpr uint32_t NoIndex = ~static_cast<uint32_t>(0);

CanonicalOptions(StringEncoding encoding, bool isAsync, uint32_t memoryIndex,
uint32_t reallocIndex, uint32_t postReturnIndex, uint32_t callbackIndex)
: m_encoding(encoding)
, m_isAsync(isAsync)
, m_memoryIndex(memoryIndex)
, m_reallocIndex(reallocIndex)
, m_postReturnIndex(postReturnIndex)
, m_callbackIndex(callbackIndex)
{
}

StringEncoding encoding() const
{
return m_encoding;
}

bool isAsync() const
{
return m_isAsync;
}

uint32_t memoryIndex() const
{
return m_memoryIndex;
}

uint32_t reallocIndex() const
{
return m_reallocIndex;
}

uint32_t postReturnIndex() const
{
return m_postReturnIndex;
}

private:
StringEncoding m_encoding;
bool m_isAsync;
uint32_t m_memoryIndex;
uint32_t m_reallocIndex;
uint32_t m_postReturnIndex;
uint32_t m_callbackIndex;
};

class ComponentCoreInstantiate;
class ComponentInstantiate;
class ComponentInstantiateInline;
class ComponentAliasExport;
class ComponentAliasInline;
class ComponentCanonOptions;
class ComponentCanonLift;
class ComponentCanonLower;
class ComponentCanonType;
Expand All @@ -617,6 +563,7 @@ class ComponentDeclaration {
AliasExportKind,
AliasCoreExportKind,
AliasInlineKind,
CanonOptionsKind,
CanonLiftKind,
CanonLowerKind,
CanonResourceNew,
Expand Down Expand Up @@ -672,6 +619,12 @@ class ComponentDeclaration {
return reinterpret_cast<ComponentAliasInline*>(this);
}

ComponentCanonOptions* asCanonOptions()
{
ASSERT(kind() == CanonOptionsKind);
return reinterpret_cast<ComponentCanonOptions*>(this);
}

ComponentCanonLift* asCanonLift()
{
ASSERT(kind() == CanonLiftKind);
Expand Down Expand Up @@ -841,17 +794,78 @@ class ComponentAliasInline : public ComponentDeclaration {
uint32_t m_exportIndex;
};

class ComponentCanonOptions : public ComponentDeclaration {
public:
enum StringEncoding : uint8_t {
Utf8,
Utf16,
Latin1Utf16,
};

static constexpr uint32_t NotDefined = ~static_cast<uint32_t>(0);

ComponentCanonOptions(StringEncoding encoding, bool isAsync, uint32_t memoryIndex,
uint32_t reallocIndex, uint32_t postReturnIndex, uint32_t callbackIndex)
: ComponentDeclaration(CanonOptionsKind)
, m_encoding(encoding)
, m_isAsync(isAsync)
, m_memoryIndex(memoryIndex)
, m_reallocIndex(reallocIndex)
, m_postReturnIndex(postReturnIndex)
, m_callbackIndex(callbackIndex)
{
}

StringEncoding encoding() const
{
return m_encoding;
}

bool isAsync() const
{
return m_isAsync;
}

uint32_t memoryIndex() const
{
return m_memoryIndex;
}

uint32_t reallocIndex() const
{
return m_reallocIndex;
}

uint32_t postReturnIndex() const
{
return m_postReturnIndex;
}

uint32_t callbackIndex() const
{
return m_callbackIndex;
}

private:
StringEncoding m_encoding;
bool m_isAsync;
uint32_t m_memoryIndex;
uint32_t m_reallocIndex;
uint32_t m_postReturnIndex;
uint32_t m_callbackIndex;
};

class ComponentCanonLift : public ComponentDeclaration {
public:
ComponentCanonLift(uint32_t coreFuncIndex, const CanonicalOptions& options, ComponentTypeFunc* funcType)
ComponentCanonLift(uint32_t coreFuncIndex, uint32_t options, ComponentTypeFunc* funcType)
: ComponentDeclaration(CanonLiftKind)
, m_options(options)
, m_coreFuncIndex(coreFuncIndex)
, m_funcType(funcType)
{
}

const CanonicalOptions& options() const
uint32_t options() const
{
return m_options;
}
Expand All @@ -867,21 +881,21 @@ class ComponentCanonLift : public ComponentDeclaration {
}

private:
CanonicalOptions m_options;
uint32_t m_options;
uint32_t m_coreFuncIndex;
ComponentTypeFunc* m_funcType;
};

class ComponentCanonLower : public ComponentDeclaration {
public:
ComponentCanonLower(uint32_t funcIndex, const CanonicalOptions& options)
ComponentCanonLower(uint32_t funcIndex, uint32_t options)
: ComponentDeclaration(CanonLowerKind)
, m_options(options)
, m_funcIndex(funcIndex)
{
}

const CanonicalOptions& options() const
uint32_t options() const
{
return m_options;
}
Expand All @@ -892,7 +906,7 @@ class ComponentCanonLower : public ComponentDeclaration {
}

private:
CanonicalOptions m_options;
uint32_t m_options;
uint32_t m_funcIndex;
};

Expand Down
Loading
Loading