Skip to content

Type-safe Embind port, compatible with the current API #7553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9cd6c7d
Initial commit: update CMakeLists and add some basic embind functiona…
GulgDev Apr 25, 2025
a8f3995
Extend embind functionality
GulgDev Apr 25, 2025
515e6a1
Add automatic expression generation and move away from C API
GulgDev Apr 25, 2025
f23396f
Switch to direct bindings
GulgDev Apr 26, 2025
38354b1
Transform expression factories into inline structs
GulgDev Apr 26, 2025
4216539
Merge branch 'main' of https://github.com/WebAssembly/binaryen into c…
GulgDev Apr 26, 2025
f87350c
Switch to C++ API
GulgDev Apr 26, 2025
9da6d8b
Extend API
GulgDev Apr 27, 2025
89e7f4f
Clean up
GulgDev Apr 27, 2025
8440c8f
Add more pass functions
GulgDev Apr 27, 2025
91cf1ee
Fix bugs
GulgDev Apr 27, 2025
d1b425b
Fix memory corruption
GulgDev Apr 27, 2025
5fde9ba
Fix the `Maximum call stack size exceeded` bug!
GulgDev Apr 29, 2025
b81cd4b
Extend expression wrapper field support
GulgDev Apr 29, 2025
2341097
Make expression building functions return specific classes
GulgDev Apr 29, 2025
6ff26de
Fix field bindings
GulgDev Apr 30, 2025
cb2bf97
Fix
GulgDev Apr 30, 2025
0fb5df7
Add boolean fields & do some fixes
GulgDev Apr 30, 2025
2fd493e
Add expression info & fix UB
GulgDev Apr 30, 2025
fd1d914
Refactor
GulgDev May 1, 2025
df7edea
Add vector field type
GulgDev May 1, 2025
c9eb24b
Add vector type to expression info
GulgDev May 1, 2025
2e1fff2
Extend instruction set
GulgDev May 1, 2025
c1f9fd0
Add missing instructions
GulgDev May 1, 2025
db647e2
Update instructions
GulgDev May 1, 2025
f865de4
Fix const
GulgDev May 1, 2025
64ee7d4
Fix bugs & update tests
GulgDev May 1, 2025
7a35cd1
Add general instructions
GulgDev May 1, 2025
9555a78
Add name list field & fix vector set
GulgDev May 1, 2025
478a4f5
Add CallIndirect
GulgDev May 1, 2025
a3ef118
Fix `createType`
GulgDev May 1, 2025
ed6d36b
Add `isTee`
GulgDev May 1, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Transform expression factories into inline structs
  • Loading branch information
GulgDev committed Apr 26, 2025
commit 38354b1ccb58633426180d6dcccdb0e524a38b04
127 changes: 61 additions & 66 deletions src/binaryen-embind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,46 @@
using namespace emscripten;

namespace binaryen {

namespace {
ExpressionFactory::ExpressionFactory(wasm::Module* module) : module(module) {}

uintptr_t LocalExpressionFactory::get(Index index, Type type) {
return reinterpret_cast<uintptr_t>(
BinaryenLocalGet(reinterpret_cast<BinaryenModuleRef>(module), index, type));
}

uintptr_t I32ExpressionFactory::add(uintptr_t left, uintptr_t right) {
return reinterpret_cast<uintptr_t>(
BinaryenBinary(reinterpret_cast<BinaryenModuleRef>(module),
BinaryenAddInt32(),
reinterpret_cast<BinaryenExpressionRef>(left),
reinterpret_cast<BinaryenExpressionRef>(right)));
}
} // namespace

Module::Module() : Module(BinaryenModuleCreate()) {}
Module::Module() : Module(new wasm::Module()) {}

Module::Module(wasm::Module* module) : module(module) {}

const uintptr_t& Module::ptr() const {
return reinterpret_cast<const uintptr_t&>(module);
}

uintptr_t Module::block(const std::string& name,
ExpressionList children,
uintptr_t type) {
std::vector<uintptr_t> childrenVec =
convertJSArrayToNumberVector<uintptr_t>(children);
return reinterpret_cast<uintptr_t>(BinaryenBlock(
module,
name.c_str(),
reinterpret_cast<BinaryenExpressionRef*>(childrenVec.begin().base()),
childrenVec.size(),
type));
wasm::Expression* Module::block(const std::string& name,
ExpressionList children,
uintptr_t type) {
auto childrenVec = vecFromJSArray<wasm::Expression*>(
children, allow_raw_pointer<wasm::Expression>());
return BinaryenBlock(
module, name.c_str(), childrenVec.begin().base(), childrenVec.size(), type);
}
uintptr_t
Module::if_(uintptr_t condition, uintptr_t ifTrue, uintptr_t ifFalse) {
return reinterpret_cast<uintptr_t>(
BinaryenIf(module,
reinterpret_cast<BinaryenExpressionRef>(condition),
reinterpret_cast<BinaryenExpressionRef>(ifTrue),
reinterpret_cast<BinaryenExpressionRef>(ifFalse)));
wasm::Expression* Module::if_(wasm::Expression* condition,
wasm::Expression* ifTrue,
wasm::Expression* ifFalse) {
return BinaryenIf(module, condition, ifTrue, ifFalse);
}
uintptr_t Module::loop(const std::string& label, uintptr_t body) {
return reinterpret_cast<uintptr_t>(BinaryenLoop(
module, label.c_str(), reinterpret_cast<BinaryenExpressionRef>(body)));
wasm::Expression* Module::loop(const std::string& label,
wasm::Expression* body) {
return BinaryenLoop(module, label.c_str(), body);
}
uintptr_t
Module::br(const std::string& label, uintptr_t condition, uintptr_t value) {
return reinterpret_cast<uintptr_t>(
BinaryenBreak(module,
label.c_str(),
reinterpret_cast<BinaryenExpressionRef>(condition),
reinterpret_cast<BinaryenExpressionRef>(value)));
wasm::Expression* Module::br(const std::string& label,
wasm::Expression* condition,
wasm::Expression* value) {
return BinaryenBreak(module, label.c_str(), condition, value);
}
uintptr_t Module::return_(uintptr_t value) {
return reinterpret_cast<uintptr_t>(
BinaryenReturn(module, reinterpret_cast<BinaryenExpressionRef>(value)));
wasm::Expression* Module::Local::get(Index index, Type type) {
return BinaryenLocalGet(module, index, type);
}

wasm::Expression* Module::I32::add(wasm::Expression* left,
wasm::Expression* right) {
return BinaryenBinary(module, BinaryenAddInt32(), left, right);
}
wasm::Expression* Module::return_(wasm::Expression* value) {
return BinaryenReturn(module, value);
}

uintptr_t Module::addFunction(const std::string& name,
Expand Down Expand Up @@ -162,27 +141,40 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
wasm::Expression::Id::CLASS_TO_VISIT##Id);
#include "wasm-delegations.def"

class_<binaryen::LocalExpressionFactory>("LocalExpressionFactory")
.function("get", &binaryen::LocalExpressionFactory::get);
class_<binaryen::Module::Local>("Module_Local")
.function("get",
&binaryen::Module::Local::get,
allow_raw_pointer<wasm::Expression>());

class_<binaryen::I32ExpressionFactory>("I32ExpressionFactory")
.function("add", &binaryen::I32ExpressionFactory::add);
class_<binaryen::Module::I32>("Module_I32")
.function("add",
&binaryen::Module::I32::add,
allow_raw_pointer<wasm::Expression>());

class_<binaryen::Module>("Module")
.constructor()
.property("ptr", &binaryen::Module::ptr)

/*.function("block", &binaryen::Module::block)
.function("if", &binaryen::Module::if_)
.function("loop", &binaryen::Module::loop)
.function("br", &binaryen::Module::br)
.function("break", &binaryen::Module::br)
.function("br_if", &binaryen::Module::br)
.property("local", &binaryen::Module::local)
.property("i32", &binaryen::Module::i32)
.function("return", &binaryen::Module::return_)

.function("addFunction", &binaryen::Module::addFunction)
.function(
"block", &binaryen::Module::block, allow_raw_pointer<wasm::Expression>())
.function(
"if", &binaryen::Module::if_, allow_raw_pointer<wasm::Expression>())
.function(
"loop", &binaryen::Module::loop, allow_raw_pointer<wasm::Expression>())
.function(
"br", &binaryen::Module::br, allow_raw_pointer<wasm::Expression>())
.function(
"break", &binaryen::Module::br, allow_raw_pointer<wasm::Expression>())
.function(
"br_if", &binaryen::Module::br, allow_raw_pointer<wasm::Expression>())
.property(
"local", &binaryen::Module::local, return_value_policy::reference())
.property("i32", &binaryen::Module::i32, return_value_policy::reference())
.function("return",
&binaryen::Module::return_,
allow_raw_pointer<wasm::Expression>())

/*.function("addFunction", &binaryen::Module::addFunction)
.function("addFunctionExport", &binaryen::Module::addFunctionExport)*/

.function("emitText", &binaryen::Module::emitText);
Expand All @@ -197,9 +189,12 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
.property("type", &wasm::Expression::type)*/
;

register_type<binaryen::ExpressionList>("Expression[]");

#define DELEGATE_FIELD_MAIN_START

#define DELEGATE_FIELD_CASE_START(id) class_<wasm::id>(#id)
#define DELEGATE_FIELD_CASE_START(id) \
class_<wasm::id, base<wasm::Expression>>(#id)

#define DELEGATE_FIELD_CASE_END(id) ;

Expand Down
44 changes: 16 additions & 28 deletions src/binaryen-embind.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,9 @@ EMSCRIPTEN_DECLARE_VAL_TYPE(ExpressionList);

EMSCRIPTEN_DECLARE_VAL_TYPE(TypeList);

namespace {
class ExpressionFactory {
protected:
struct ExpressionFactory {
wasm::Module* module;

public:
ExpressionFactory(wasm::Module* module);
};

class LocalExpressionFactory : public ExpressionFactory {
public:
using ExpressionFactory::ExpressionFactory;

uintptr_t get(Index index, Type type);
};

class I32ExpressionFactory : public ExpressionFactory {
public:
using ExpressionFactory::ExpressionFactory;

uintptr_t add(uintptr_t left, uintptr_t right);
};
} // namespace

class Module {
private:
Expand All @@ -46,14 +26,22 @@ class Module {

const uintptr_t& ptr() const;

uintptr_t
wasm::Expression*
block(const std::string& name, ExpressionList children, uintptr_t type);
uintptr_t if_(uintptr_t condition, uintptr_t ifTrue, uintptr_t ifFalse);
uintptr_t loop(const std::string& label, uintptr_t body);
uintptr_t br(const std::string& label, uintptr_t condition, uintptr_t value);
const LocalExpressionFactory local = LocalExpressionFactory(this->module);
const I32ExpressionFactory i32 = I32ExpressionFactory(this->module);
uintptr_t return_(uintptr_t value);
wasm::Expression* if_(wasm::Expression* condition,
wasm::Expression* ifTrue,
wasm::Expression* ifFalse);
wasm::Expression* loop(const std::string& label, wasm::Expression* body);
wasm::Expression* br(const std::string& label,
wasm::Expression* condition,
wasm::Expression* value);
const struct Local : ExpressionFactory {
wasm::Expression* get(Index index, Type type);
} local{module};
const struct I32 : ExpressionFactory {
wasm::Expression* add(wasm::Expression* left, wasm::Expression* right);
} i32{module};
wasm::Expression* return_(wasm::Expression* value);

uintptr_t addFunction(const std::string& name,
Type params,
Expand Down
Loading