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
Fix field bindings
  • Loading branch information
GulgDev committed Apr 30, 2025
commit 6ff26de17ce17340eecf885d8df3a8469f864d4a
216 changes: 85 additions & 131 deletions src/binaryen-embind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ wasm::Block* Module::block(OptionalString name,
std::optional<wasm::Type> type) {
return wasm::Builder(*module).makeBlock(
name.isNull() || name.isUndefined() ? nullptr : name.as<std::string>(),
vecFromJSArray<wasm::Expression*>(children,
allow_raw_pointer<wasm::Expression>()),
vecFromJSArray<wasm::Expression*>(children, allow_raw_pointers()),
type);
}
wasm::If* Module::if_(wasm::Expression* condition,
Expand Down Expand Up @@ -59,8 +58,7 @@ wasm::Call* Module::call(const std::string& name,
wasm::Type type) {
return wasm::Builder(*module).makeCall(
name,
vecFromJSArray<wasm::Expression*>(operands,
allow_raw_pointer<wasm::Expression>()),
vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers()),
type);
}
wasm::CallIndirect* Module::call_indirect(const std::string& table,
Expand All @@ -71,17 +69,15 @@ wasm::CallIndirect* Module::call_indirect(const std::string& table,
return wasm::Builder(*module).makeCallIndirect(
table,
target,
vecFromJSArray<wasm::Expression*>(operands,
allow_raw_pointer<wasm::Expression>()),
vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers()),
wasm::Signature(params, results));
}
wasm::Call* Module::return_call(const std::string& name,
ExpressionList operands,
wasm::Type type) {
return wasm::Builder(*module).makeCall(
name,
vecFromJSArray<wasm::Expression*>(operands,
allow_raw_pointer<wasm::Expression>()),
vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers()),
type,
true);
}
Expand All @@ -93,8 +89,7 @@ wasm::CallIndirect* Module::return_call_indirect(const std::string& table,
return wasm::Builder(*module).makeCallIndirect(
table,
target,
vecFromJSArray<wasm::Expression*>(operands,
allow_raw_pointer<wasm::Expression>()),
vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers()),
wasm::Signature(params, results),
true);
}
Expand Down Expand Up @@ -238,6 +233,32 @@ static std::string capitalize(std::string str, int i = 0) {

#define GETTER(field) capitalize("get" field, 3).c_str()
#define SETTER(field) capitalize("set" field, 3).c_str()

#define FIELD(field, name, id, type, ...) \
.class_function( \
GETTER(#name), \
+[](const wasm::id& expr) { return expr.field; }, \
##__VA_ARGS__) \
.class_function( \
SETTER(#name), \
+[](wasm::id& expr, type value) { expr.field = value; }, \
##__VA_ARGS__) \
.function( \
GETTER(#name), \
+[](const wasm::id& expr) { return expr.field; }, \
##__VA_ARGS__) \
.function( \
SETTER(#name), \
+[](wasm::id& expr, type value) { expr.field = value; }, \
##__VA_ARGS__) \
.property(#name, &wasm::id::field, ##__VA_ARGS__)

#define FIELD_DYN(field, name, getter, setter, ...) \
.class_function(GETTER(#name), +getter, ##__VA_ARGS__) \
.class_function(SETTER(#name), +setter, ##__VA_ARGS__) \
.function(GETTER(#name), +getter, ##__VA_ARGS__) \
.function(SETTER(#name), +setter, ##__VA_ARGS__) \
.property(#name, +getter, +setter, ##__VA_ARGS__)
} // namespace

EMSCRIPTEN_BINDINGS(Binaryen) {
Expand Down Expand Up @@ -292,31 +313,31 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
class_<binaryen::Module::Local>("Module_Local")
.function("get",
&binaryen::Module::Local::get,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("set",
&binaryen::Module::Local::set,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("get",
&binaryen::Module::Local::tee,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>());

class_<binaryen::Module::Global>("Module_Global")
.function("get",
&binaryen::Module::Global::get,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("set",
&binaryen::Module::Global::set,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>());

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

class_<binaryen::Module>("Module")
Expand All @@ -325,47 +346,35 @@ EMSCRIPTEN_BINDINGS(Binaryen) {

.function("block",
&binaryen::Module::block,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("if",
&binaryen::Module::if_,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("loop",
&binaryen::Module::loop,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("br",
&binaryen::Module::br,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("break",
&binaryen::Module::br,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("br_if",
&binaryen::Module::br,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function(
"if", &binaryen::Module::if_, allow_raw_pointers(), nonnull<ret_val>())
.function(
"loop", &binaryen::Module::loop, allow_raw_pointers(), nonnull<ret_val>())
.function(
"br", &binaryen::Module::br, allow_raw_pointers(), nonnull<ret_val>())
.function(
"break", &binaryen::Module::br, allow_raw_pointers(), nonnull<ret_val>())
.function(
"br_if", &binaryen::Module::br, allow_raw_pointers(), nonnull<ret_val>())
.function("switch",
&binaryen::Module::switch_,
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>())
.function("call",
&binaryen::Module::call,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function(
"call", &binaryen::Module::call, allow_raw_pointers(), nonnull<ret_val>())
.function("call_indirect",
&binaryen::Module::call_indirect,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("return_call",
&binaryen::Module::return_call,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("return_call_indirect",
&binaryen::Module::return_call_indirect,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())
.property(
"local", &binaryen::Module::local, return_value_policy::reference())
Expand All @@ -374,16 +383,16 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
.property("i32", &binaryen::Module::i32, return_value_policy::reference())
.function("return",
&binaryen::Module::return_,
allow_raw_pointer<wasm::Expression>(),
allow_raw_pointers(),
nonnull<ret_val>())

.function("addFunction",
&binaryen::Module::addFunction,
allow_raw_pointer<wasm::Function>(),
allow_raw_pointers(),
nonnull<ret_val>())
.function("addFunctionExport",
&binaryen::Module::addFunctionExport,
allow_raw_pointer<wasm::Export>(),
allow_raw_pointers(),
nonnull<ret_val>())

.function("emitBinary", &binaryen::Module::emitBinary)
Expand All @@ -395,30 +404,22 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
.function("optimize", &binaryen::Module::optimize)
.function("optimizeFunction",
&binaryen::Module::optimizeFunction,
allow_raw_pointer<wasm::Expression>())
allow_raw_pointers())

.function(
"dispose",
&binaryen::Module::dispose) // for compatibility, should be removed later
// in favor of Module.delete()
;

function("parseText",
binaryen::parseText,
allow_raw_pointer<binaryen::Module>(),
nonnull<ret_val>());
function(
"parseText", binaryen::parseText, allow_raw_pointers(), nonnull<ret_val>());

function("createType", binaryen::createType);

function(
"Expression",
+[](wasm::Expression* expr) { return expr; },
allow_raw_pointer<wasm::Expression>(),
nonnull<ret_val>());

class_<wasm::Expression>("ExpressionWrapper")
.property("id", &wasm::Expression::_id)
.property("type", &wasm::Expression::type);
class_<wasm::Expression>("Expression")
FIELD(_id, id, Expression, wasm::Expression::Id)
FIELD(type, type, Expression, wasm::Type);

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

Expand All @@ -427,93 +428,46 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
#define DELEGATE_FIELD_MAIN_START

#define DELEGATE_FIELD_CASE_START(id) \
function( \
#id, \
+[](wasm::id* expr) { return expr; }, \
allow_raw_pointer<wasm::id>(), \
nonnull<ret_val>()); \
class_<wasm::id, base<wasm::Expression>>(#id "Wrapper")
class_<wasm::id, base<wasm::Expression>>(#id)

#define DELEGATE_FIELD_CASE_END(id) ;

#define DELEGATE_FIELD_MAIN_END

#define DELEGATE_FIELD_CHILD(id, field) \
.function( \
GETTER(#field), \
+[](const wasm::id& expr) { return expr.field; }, \
allow_raw_pointer<wasm::Expression>(), \
nonnull<ret_val>()) \
.function( \
SETTER(#field), \
+[](wasm::id& expr, wasm::Expression* value) { expr.field = value; }, \
allow_raw_pointer<wasm::Expression>() /* nonnull<arg>() */) \
.property( \
#field, \
&wasm::id::field, \
allow_raw_pointer< \
wasm::Expression>() /* nonnull<val>() */) // Embind doesn't support
// non-null properties yet
FIELD(field, \
field, \
id, \
wasm::Expression*, \
allow_raw_pointers(), \
nonnull<ret_val>())
#define DELEGATE_FIELD_OPTIONAL_CHILD(id, field) \
.function( \
GETTER(#field), \
+[](const wasm::id& expr) { return expr.field; }, \
allow_raw_pointer<wasm::Expression>()) \
.function( \
SETTER(#field), \
+[](wasm::id& expr, wasm::Expression* value) { expr.field = value; }, \
allow_raw_pointer<wasm::Expression>()) \
.property(#field, &wasm::id::field, allow_raw_pointer<wasm::Expression>())
FIELD(field, field, id, wasm::Expression*, allow_raw_pointers())
#define DELEGATE_FIELD_CHILD_VECTOR(id, field) \
.function( \
GETTER(#field), \
+[](const wasm::id& expr) { \
FIELD_DYN( \
field, \
field, \
[](const wasm::id& expr) { \
return binaryen::ExpressionList( \
val::array(std::vector(expr.field.begin(), expr.field.end()))); \
}, \
allow_raw_pointer<wasm::Expression>()) \
.function( \
SETTER(#field), \
+[](wasm::id& expr, binaryen::ExpressionList value) { \
expr.field.set(vecFromJSArray<wasm::Expression*>( \
value, allow_raw_pointer<wasm::Expression>())); \
}, \
allow_raw_pointer<wasm::Expression>()) \
.property( \
#field, \
+[](const wasm::id& expr) { \
return binaryen::ExpressionList( \
val::array(std::vector(expr.field.begin(), expr.field.end()))); \
}, \
+[](wasm::id& expr, binaryen::ExpressionList value) { \
expr.field.set(vecFromJSArray<wasm::Expression*>( \
value, allow_raw_pointer<wasm::Expression>())); \
}, \
allow_raw_pointer<wasm::Expression>())
#define DELEGATE_FIELD_INT(id, field) \
.function( \
GETTER(#field), +[](const wasm::id& expr) { return expr.field; }) \
.function( \
SETTER(#field), \
+[](wasm::id& expr, uint32_t value) { expr.field = value; }) \
.property(#field, &wasm::id::field)
[](wasm::id& expr, binaryen::ExpressionList value) { \
expr.field.set( \
vecFromJSArray<wasm::Expression*>(value, allow_raw_pointers())); \
})
#define DELEGATE_FIELD_INT(id, field) FIELD(field, field, id, uint32_t)
#define DELEGATE_FIELD_INT_ARRAY(id, field)
#define DELEGATE_FIELD_INT_VECTOR(id, field)
#define DELEGATE_FIELD_BOOL(id, field)
#define DELEGATE_FIELD_BOOL_VECTOR(id, field)
#define DELEGATE_FIELD_ENUM(id, field, type)
#define DELEGATE_FIELD_LITERAL(id, field)
#define DELEGATE_FIELD_NAME(id, field) \
.function( \
GETTER(#field), \
+[](const wasm::id& expr) { return expr.field.toString(); }) \
.function( \
SETTER(#field), \
+[](wasm::id& expr, const std::string& value) { expr.field = value; }) \
.property( \
#field, \
+[](const wasm::id& expr) { return expr.field.toString(); }, \
+[](wasm::id& expr, const std::string& value) { expr.field = value; })
FIELD_DYN( \
field, \
field, \
[](const wasm::id& expr) { return expr.field.toString(); }, \
[](wasm::id& expr, const std::string& value) { expr.field = value; })
#define DELEGATE_FIELD_NAME_VECTOR(id, field)
#define DELEGATE_FIELD_SCOPE_NAME_DEF(id, field) DELEGATE_FIELD_NAME(id, field)
#define DELEGATE_FIELD_SCOPE_NAME_USE(id, field) DELEGATE_FIELD_NAME(id, field)
Expand Down
2 changes: 1 addition & 1 deletion test/binaryen.js/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console.log("# Expression");

const ptr = module.block(null, []);

var theExpression = binaryen.Expression(ptr); // works without new
var theExpression = new binaryen.Block(ptr); // works without new
assert(theExpression instanceof binaryen.Block);
assert(theExpression instanceof binaryen.Expression);
assert(theExpression.constructor === binaryen.Block);
Expand Down
Loading