Skip to content

Commit

Permalink
Update Table api to handle external ref address
Browse files Browse the repository at this point in the history
Signed-off-by: HyukWoo Park <[email protected]>
  • Loading branch information
clover2123 authored and ksh8281 committed Jul 24, 2024
1 parent f5eca2d commit 7800a31
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
30 changes: 26 additions & 4 deletions src/api/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,20 @@ void wasm_global_set(wasm_global_t* glob, const wasm_val_t* val)
}

// Table Instances

// used to mark special ref address passed from external modules
static const size_t OTHER_EXTERN_REF_TAG = 1;

own wasm_table_t* wasm_table_new(
wasm_store_t* store, const wasm_tabletype_t* tt, wasm_ref_t* init)
{
Table* table = Table::createTable(store->get(), tt->valtype.type, tt->limits.min, tt->limits.max);
Table* table = nullptr;
if (UNLIKELY((size_t)init & OTHER_EXTERN_REF_TAG)) {
table = Table::createTable(store->get(), tt->valtype.type, tt->limits.min, tt->limits.max, reinterpret_cast<Object*>(init));
} else {
table = Table::createTable(store->get(), tt->valtype.type, tt->limits.min, tt->limits.max, init ? const_cast<Object*>(init->get()) : nullptr);
}

return new wasm_table_t(table, tt->clone());
}

Expand All @@ -1069,8 +1079,12 @@ own wasm_ref_t* wasm_table_get(const wasm_table_t* table, wasm_table_size_t inde
return nullptr;
}

Value val(table->get()->uncheckedGetElement(index));
wasm_ref_t* ref = static_cast<wasm_ref_t*>(table->get()->uncheckedGetElement(index));
if (UNLIKELY((size_t)ref & OTHER_EXTERN_REF_TAG)) {
return ref;
}

Value val(ref);
if (val.isNull()) {
return nullptr;
}
Expand All @@ -1085,7 +1099,11 @@ bool wasm_table_set(wasm_table_t* table, wasm_table_size_t index, wasm_ref_t* re
return false;
}

table->get()->uncheckedSetElement(index, ref ? const_cast<Object*>(ref->get()) : reinterpret_cast<void*>(Value::NullBits));
if (UNLIKELY((size_t)ref & OTHER_EXTERN_REF_TAG)) {
table->get()->uncheckedSetElement(index, reinterpret_cast<Object*>(ref));
} else {
table->get()->uncheckedSetElement(index, ref ? const_cast<Object*>(ref->get()) : reinterpret_cast<void*>(Value::NullBits));
}
return true;
}

Expand All @@ -1100,7 +1118,11 @@ bool wasm_table_grow(wasm_table_t* table, wasm_table_size_t delta, wasm_ref_t* i
return false;
}

table->get()->grow(delta + table->get()->size(), init ? const_cast<Object*>(init->get()) : reinterpret_cast<void*>(Value::NullBits));
if (UNLIKELY((size_t)init & OTHER_EXTERN_REF_TAG)) {
table->get()->grow(delta + table->get()->size(), reinterpret_cast<Object*>(init));
} else {
table->get()->grow(delta + table->get()->size(), init ? const_cast<Object*>(init->get()) : reinterpret_cast<void*>(Value::NullBits));
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/api/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ enum wasm_valkind_enum {
WASM_I64,
WASM_F32,
WASM_F64,
WASM_V128,
WASM_ANYREF = 128,
WASM_FUNCREF,
};
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/Table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@

namespace Walrus {

Table* Table::createTable(Store* store, Value::Type type, uint32_t initialSize, uint32_t maximumSize)
Table* Table::createTable(Store* store, Value::Type type, uint32_t initialSize, uint32_t maximumSize, void* init)
{
Table* tbl = new Table(type, initialSize, maximumSize);
Table* tbl = new Table(type, initialSize, maximumSize, init ? init : reinterpret_cast<void*>(Value::NullBits));
store->appendExtern(tbl);

return tbl;
}

Table::Table(Value::Type type, uint32_t initialSize, uint32_t maximumSize)
Table::Table(Value::Type type, uint32_t initialSize, uint32_t maximumSize, void* init)
: m_type(type)
, m_size(initialSize)
, m_maximumSize(maximumSize)
{
m_elements.resize(initialSize, reinterpret_cast<void*>(Value::NullBits));
m_elements.resize(initialSize, init);
}

void Table::init(ExecutionState& state, Instance* instance, ElementSegment* source, uint32_t dstStart, uint32_t srcStart, uint32_t srcSize)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Table : public Extern {
friend class JITFieldAccessor;

public:
static Table* createTable(Store* store, Value::Type type, uint32_t initialSize, uint32_t maximumSize);
static Table* createTable(Store* store, Value::Type type, uint32_t initialSize, uint32_t maximumSize, void* init = nullptr);

virtual Object::Kind kind() const override
{
Expand Down Expand Up @@ -101,7 +101,7 @@ class Table : public Extern {
void fillTable(uint32_t n, void* value, uint32_t index);

private:
Table(Value::Type type, uint32_t initialSize, uint32_t maximumSize);
Table(Value::Type type, uint32_t initialSize, uint32_t maximumSize, void* init);

void throwException(ExecutionState& state) const;

Expand Down

0 comments on commit 7800a31

Please sign in to comment.