@@ -23,11 +23,11 @@ const uintptr_t& Module::ptr() const {
2323
2424wasm::Block* Module::block (OptionalString name,
2525 ExpressionList children,
26- std::optional<wasm::Type > type) {
26+ std::optional<TypeID > type) {
2727 return wasm::Builder (*module ).makeBlock (
28- name.isNull () || name. isUndefined () ? nullptr : name.as <std::string>(),
28+ name.isNull () ? nullptr : name.as <std::string>(),
2929 vecFromJSArray<wasm::Expression*>(children, allow_raw_pointers ()),
30- type);
30+ (std::optional<wasm::Type>) type);
3131}
3232wasm::If* Module::if_ (wasm::Expression* condition,
3333 wasm::Expression* ifTrue,
@@ -53,58 +53,57 @@ wasm::Switch* Module::switch_(NameList names,
5353 return wasm::Builder (*module ).makeSwitch (
5454 namesVec, defaultName, condition, value);
5555}
56- wasm::Call* Module::call (const std::string& name,
57- ExpressionList operands,
58- wasm::Type type) {
56+ wasm::Call*
57+ Module::call (const std::string& name, ExpressionList operands, TypeID type) {
5958 return wasm::Builder (*module ).makeCall (
6059 name,
6160 vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers ()),
62- type);
61+ wasm::Type ( type) );
6362}
6463wasm::CallIndirect* Module::call_indirect (const std::string& table,
6564 wasm::Expression* target,
6665 ExpressionList operands,
67- wasm::Type params,
68- wasm::Type results) {
66+ TypeID params,
67+ TypeID results) {
6968 return wasm::Builder (*module ).makeCallIndirect (
7069 table,
7170 target,
7271 vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers ()),
73- wasm::Signature (params, results));
72+ wasm::Signature (wasm::Type ( params), wasm::Type ( results) ));
7473}
7574wasm::Call* Module::return_call (const std::string& name,
7675 ExpressionList operands,
77- wasm::Type type) {
76+ TypeID type) {
7877 return wasm::Builder (*module ).makeCall (
7978 name,
8079 vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers ()),
81- type,
80+ wasm::Type ( type) ,
8281 true );
8382}
8483wasm::CallIndirect* Module::return_call_indirect (const std::string& table,
8584 wasm::Expression* target,
8685 ExpressionList operands,
87- wasm::Type params,
88- wasm::Type results) {
86+ TypeID params,
87+ TypeID results) {
8988 return wasm::Builder (*module ).makeCallIndirect (
9089 table,
9190 target,
9291 vecFromJSArray<wasm::Expression*>(operands, allow_raw_pointers ()),
93- wasm::Signature (params, results),
92+ wasm::Signature (wasm::Type ( params), wasm::Type ( results) ),
9493 true );
9594}
96- wasm::LocalGet* Module::Local::get (Index index, wasm::Type type) {
97- return wasm::Builder (*module ).makeLocalGet (index, type);
95+ wasm::LocalGet* Module::Local::get (Index index, TypeID type) {
96+ return wasm::Builder (*module ).makeLocalGet (index, wasm::Type ( type) );
9897}
9998wasm::LocalSet* Module::Local::set (Index index, wasm::Expression* value) {
10099 return wasm::Builder (*module ).makeLocalSet (index, value);
101100}
102101wasm::LocalSet*
103- Module::Local::tee (Index index, wasm::Expression* value, wasm::Type type) {
104- return wasm::Builder (*module ).makeLocalTee (index, value, type);
102+ Module::Local::tee (Index index, wasm::Expression* value, TypeID type) {
103+ return wasm::Builder (*module ).makeLocalTee (index, value, wasm::Type ( type) );
105104}
106- wasm::GlobalGet* Module::Global::get (const std::string& name, wasm::Type type) {
107- return wasm::Builder (*module ).makeGlobalGet (name, type);
105+ wasm::GlobalGet* Module::Global::get (const std::string& name, TypeID type) {
106+ return wasm::Builder (*module ).makeGlobalGet (name, wasm::Type ( type) );
108107}
109108wasm::GlobalSet* Module::Global::set (const std::string& name,
110109 wasm::Expression* value) {
@@ -122,13 +121,13 @@ wasm::Return* Module::return_(wasm::Expression* value) {
122121static std::mutex ModuleAddFunctionMutex;
123122
124123wasm::Function* Module::addFunction (const std::string& name,
125- wasm::Type params,
126- wasm::Type results,
124+ TypeID params,
125+ TypeID results,
127126 TypeList varTypes,
128127 wasm::Expression* body) {
129128 auto * ret = new wasm::Function;
130129 ret->setExplicitName (name);
131- ret->type = wasm::Signature (params, results);
130+ ret->type = wasm::Signature (wasm::Type ( params), wasm::Type ( results) );
132131 ret->vars = vecFromJSArray<wasm::Type>(varTypes);
133132 ret->body = body;
134133
@@ -218,39 +217,72 @@ Module* parseText(const std::string& text) {
218217 return new Module (wasm);
219218}
220219
221- wasm::Type createType (TypeList types) {
220+ TypeID createType (TypeList types) {
222221 auto typesVec = vecFromJSArray<wasm::Type>(types);
223- return wasm::Type (typesVec);
222+ return wasm::Type (typesVec). getID () ;
224223}
224+
225+ val getExpressionInfo (wasm::Expression* expr) { return val (expr); }
225226} // namespace binaryen
226227
227228namespace {
228- static std::string capitalize (std::string str, int i = 0 ) {
229- assert (!str.empty ());
229+ static std::string uncapitalize (std::string str, int i) {
230+ str[i] = std::tolower (str[i]);
231+ return str;
232+ }
233+
234+ static std::string capitalize (std::string str, int i) {
230235 str[i] = std::toupper (str[i]);
231236 return str;
232237}
233238
239+ static std::string normalize (std::string str) {
240+ if (str.back () == ' _' )
241+ str.pop_back ();
242+ if (str.substr (0 , 2 ) == " is" )
243+ str = uncapitalize (str, 2 ).substr (2 );
244+ return str;
245+ }
246+
234247#define GETTER (field ) capitalize(" get" field, 3 )
235248#define SETTER (field ) capitalize(" set" field, 3 )
236249
250+ #define FIELD_GS (target, field, name, getterName, setterName, id, type, ...) \
251+ auto getter = [](const wasm::id& expr) { return expr.field ; }; \
252+ auto setter = [](wasm::id& expr, type value) { expr.field = value; }; \
253+ target.class_function(getterName.c_str(), +getter, ##__VA_ARGS__) \
254+ .class_function(setterName.c_str(), +setter, ##__VA_ARGS__) \
255+ .function(getterName.c_str(), +getter, ##__VA_ARGS__) \
256+ .function(setterName.c_str(), +setter, ##__VA_ARGS__) \
257+ .property(name, &wasm::id::field, ##__VA_ARGS__);
258+
259+ #define FIELD_BOOL (target, field, name, id, ...) \
260+ { \
261+ std::string propName = normalize (#name); \
262+ std::string getterName = capitalize (" is" + propName, 2 ); \
263+ std::string setterName = capitalize (" set" + propName, 3 ); \
264+ FIELD_GS (target, \
265+ field, \
266+ propName.c_str (), \
267+ getterName, \
268+ setterName, \
269+ id, \
270+ bool , \
271+ ##__VA_ARGS__) \
272+ }
273+
237274#define FIELD (target, field, name, id, type, ...) \
238275 { \
239- auto getterName = GETTER (#name); \
240- auto setterName = SETTER (#name); \
241- auto getter = [](const wasm::id& expr) { return expr.field ; }; \
242- auto setter = [](wasm::id& expr, type value) { expr.field = value; }; \
243- target.class_function (getterName.c_str (), +getter, ##__VA_ARGS__) \
244- .class_function (setterName.c_str (), +setter, ##__VA_ARGS__) \
245- .function (getterName.c_str (), +getter, ##__VA_ARGS__) \
246- .function (setterName.c_str (), +setter, ##__VA_ARGS__) \
247- .property (#name, &wasm::id::field, ##__VA_ARGS__); \
276+ std::string getterName = GETTER (#name); \
277+ std::string setterName = SETTER (#name); \
278+ FIELD_GS ( \
279+ target, field, #name, getterName, setterName, id, type, ##__VA_ARGS__) \
248280 }
249281
250282#define FIELD_DYN (target, field, name, getter, setter, ...) \
251283 { \
252- auto getterName = GETTER (#name); \
253- auto setterName = SETTER (#name); \
284+ std::string getterName = GETTER (#name); \
285+ std::string setterName = SETTER (#name); \
254286 target.class_function (getterName.c_str (), +getter, ##__VA_ARGS__) \
255287 .class_function (setterName.c_str (), +setter, ##__VA_ARGS__) \
256288 .function (getterName.c_str (), +getter, ##__VA_ARGS__) \
@@ -262,32 +294,41 @@ static std::string capitalize(std::string str, int i = 0) {
262294EMSCRIPTEN_BINDINGS (Binaryen) {
263295 register_type<binaryen::Binary>(" Uint8Array" );
264296
265- register_type<binaryen::OptionalString>(" string | null | undefined" );
266-
267- class_<wasm::Type>(" Type" );
268-
269- register_optional<wasm::Type>();
270-
271- constant (" none" , wasm::Type (wasm::Type::none));
272- constant (" i32" , wasm::Type (wasm::Type::i32 ));
273- constant (" i64" , wasm::Type (wasm::Type::i64 ));
274- constant (" f32" , wasm::Type (wasm::Type::f32 ));
275- constant (" f64" , wasm::Type (wasm::Type::f64 ));
276- constant (" v128" , wasm::Type (wasm::Type::v128));
277- constant (" funcref" , wasm::Type (wasm::HeapType::func, wasm::Nullable));
278- constant (" externref" , wasm::Type (wasm::HeapType::ext, wasm::Nullable));
279- constant (" anyref" , wasm::Type (wasm::HeapType::any, wasm::Nullable));
280- constant (" eqref" , wasm::Type (wasm::HeapType::eq, wasm::Nullable));
281- constant (" i31ref" , wasm::Type (wasm::HeapType::i31, wasm::Nullable));
282- constant (" structref" , wasm::Type (wasm::HeapType::struct_, wasm::Nullable));
283- constant (" stringref" , wasm::Type (wasm::HeapType::string, wasm::Nullable));
284- constant (" nullref" , wasm::Type (wasm::HeapType::none, wasm::Nullable));
285- constant (" nullexternref" , wasm::Type (wasm::HeapType::noext, wasm::Nullable));
286- constant (" nullfuncref" , wasm::Type (wasm::HeapType::nofunc, wasm::Nullable));
287- constant (" unreachable" , wasm::Type (wasm::Type::unreachable));
288- constant (" auto" , val::null ());
289-
290- register_type<binaryen::TypeList>(" Type[]" );
297+ register_type<binaryen::OptionalString>(" string | null" );
298+
299+ register_optional<binaryen::TypeID>();
300+
301+ constant<binaryen::TypeID>(" none" , wasm::Type (wasm::Type::none).getID ());
302+ constant<binaryen::TypeID>(" i32" , wasm::Type (wasm::Type::i32 ).getID ());
303+ constant<binaryen::TypeID>(" i64" , wasm::Type (wasm::Type::i64 ).getID ());
304+ constant<binaryen::TypeID>(" f32" , wasm::Type (wasm::Type::f32 ).getID ());
305+ constant<binaryen::TypeID>(" f64" , wasm::Type (wasm::Type::f64 ).getID ());
306+ constant<binaryen::TypeID>(" v128" , wasm::Type (wasm::Type::v128).getID ());
307+ constant<binaryen::TypeID>(
308+ " funcref" , wasm::Type (wasm::HeapType::func, wasm::Nullable).getID ());
309+ constant<binaryen::TypeID>(
310+ " externref" , wasm::Type (wasm::HeapType::ext, wasm::Nullable).getID ());
311+ constant<binaryen::TypeID>(
312+ " anyref" , wasm::Type (wasm::HeapType::any, wasm::Nullable).getID ());
313+ constant<binaryen::TypeID>(
314+ " eqref" , wasm::Type (wasm::HeapType::eq, wasm::Nullable).getID ());
315+ constant<binaryen::TypeID>(
316+ " i31ref" , wasm::Type (wasm::HeapType::i31, wasm::Nullable).getID ());
317+ constant<binaryen::TypeID>(
318+ " structref" , wasm::Type (wasm::HeapType::struct_, wasm::Nullable).getID ());
319+ constant<binaryen::TypeID>(
320+ " stringref" , wasm::Type (wasm::HeapType::string, wasm::Nullable).getID ());
321+ constant<binaryen::TypeID>(
322+ " nullref" , wasm::Type (wasm::HeapType::none, wasm::Nullable).getID ());
323+ constant<binaryen::TypeID>(
324+ " nullexternref" , wasm::Type (wasm::HeapType::noext, wasm::Nullable).getID ());
325+ constant<binaryen::TypeID>(
326+ " nullfuncref" , wasm::Type (wasm::HeapType::nofunc, wasm::Nullable).getID ());
327+ constant<binaryen::TypeID>(" unreachable" ,
328+ wasm::Type (wasm::Type::unreachable).getID ());
329+ constant (" auto" , val::undefined ());
330+
331+ register_type<binaryen::TypeList>(" number[]" );
291332
292333 constant<uintptr_t >(" notPacked" , wasm::Field::PackedType::not_packed);
293334 constant<uintptr_t >(" i8" , wasm::Field::PackedType::i8 );
@@ -416,8 +457,25 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
416457 function (" createType" , binaryen::createType);
417458
418459 auto ExpressionWrapper = class_<wasm::Expression>(" Expression" );
419- FIELD (ExpressionWrapper, _id, id, Expression, wasm::Expression::Id);
420- FIELD (ExpressionWrapper, type, type, Expression, wasm::Type);
460+ {
461+ auto getter = [](const wasm::Expression& expr) { return expr._id ; };
462+ ExpressionWrapper.class_function (" getId" , +getter)
463+ .function (" getId" , +getter)
464+ .property (" id" , &wasm::Expression::_id);
465+ };
466+ {
467+ auto getter = [](const wasm::Expression& expr) {
468+ return binaryen::TypeID (expr.type .getID ());
469+ };
470+ auto setter = [](wasm::Expression& expr, binaryen::TypeID value) {
471+ expr.type = wasm::Type (value);
472+ };
473+ ExpressionWrapper.class_function (" getType" , +getter)
474+ .class_function (" setType" , +setter)
475+ .function (" getType" , +getter)
476+ .function (" setType" , +setter)
477+ .property (" type" , +getter, +setter);
478+ };
421479
422480 register_type<binaryen::ExpressionList>(" Expression[]" );
423481
@@ -426,7 +484,21 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
426484#define DELEGATE_FIELD_MAIN_START
427485
428486#define DELEGATE_FIELD_CASE_START (id ) \
429- auto id##Wrapper = class_<wasm::id, base<wasm::Expression>>(#id);
487+ auto id##Wrapper = class_<wasm::id, base<wasm::Expression>>(#id); \
488+ { \
489+ auto getter = [](const wasm::Expression& expr) { return expr._id ; }; \
490+ id##Wrapper.class_function (" getId" , +getter); \
491+ }; \
492+ { \
493+ auto getter = [](const wasm::Expression& expr) { \
494+ return binaryen::TypeID (expr.type .getID ()); \
495+ }; \
496+ auto setter = [](wasm::Expression& expr, binaryen::TypeID value) { \
497+ expr.type = wasm::Type (value); \
498+ }; \
499+ id##Wrapper.class_function (" getType" , +getter) \
500+ .class_function (" setType" , +setter); \
501+ };
430502
431503#define DELEGATE_FIELD_CASE_END (id ) id##Wrapper;
432504
@@ -459,7 +531,8 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
459531 FIELD (id##Wrapper, field, field, id, uint32_t );
460532#define DELEGATE_FIELD_INT_ARRAY (id, field )
461533#define DELEGATE_FIELD_INT_VECTOR (id, field )
462- #define DELEGATE_FIELD_BOOL (id, field )
534+ #define DELEGATE_FIELD_BOOL (id, field ) \
535+ FIELD_BOOL (id##Wrapper, field, field, id);
463536#define DELEGATE_FIELD_BOOL_VECTOR (id, field )
464537#define DELEGATE_FIELD_ENUM (id, field, type )
465538#define DELEGATE_FIELD_LITERAL (id, field )
@@ -471,7 +544,18 @@ EMSCRIPTEN_BINDINGS(Binaryen) {
471544 [](const wasm::id& expr) { return expr.field .toString (); }, \
472545 [](wasm::id& expr, const std::string& value) { expr.field = value; });
473546#define DELEGATE_FIELD_NAME_VECTOR (id, field )
474- #define DELEGATE_FIELD_SCOPE_NAME_DEF (id, field ) DELEGATE_FIELD_NAME(id, field)
547+ #define DELEGATE_FIELD_SCOPE_NAME_DEF (id, field ) \
548+ FIELD_DYN ( \
549+ id##Wrapper, \
550+ field, \
551+ field, \
552+ [](const wasm::id& expr) { \
553+ return binaryen::OptionalString ( \
554+ expr.field .size () ? val (expr.field .toString ()) : val::null ()); \
555+ }, \
556+ [](wasm::id& expr, binaryen::OptionalString value) { \
557+ expr.field = value.isNull () ? nullptr : value.as <std::string>(); \
558+ });
475559#define DELEGATE_FIELD_SCOPE_NAME_USE (id, field ) DELEGATE_FIELD_NAME(id, field)
476560#define DELEGATE_FIELD_SCOPE_NAME_USE_VECTOR (id, field ) \
477561 DELEGATE_FIELD_NAME_VECTOR (id, field)
0 commit comments