Skip to content

Commit 2373b2c

Browse files
committed
fix ruby exporter returning void* instead of properly wrapped objects when emitting references
1 parent 7348a8a commit 2373b2c

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

lib/conversions/src/RLCToRuby.cpp

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,49 @@ namespace mlir::rlc
352352
// extract to convert it to a python builtin type instead
353353
if (returnsVoid(type).failed())
354354
{
355-
if (isUserDefined(resultType))
355+
if (auto casted =
356+
mlir::dyn_cast<mlir::rlc::ReferenceType>(type.getResults()[0]))
357+
{
358+
auto realType = casted.getUnderlying();
359+
w.writenl(
360+
"__result2 = __result[0, "
361+
"Fiddle::SIZEOF_VOIDP].unpack(\"J!\").first");
362+
if (mlir::isa<mlir::rlc::ClassType>(realType))
363+
{
364+
w.writenl("RLC::");
365+
w.writeType(realType);
366+
w.writenl(".new(__result2)");
367+
}
368+
else if (mlir::isa<mlir::rlc::AlternativeType>(realType))
369+
{
370+
w.write("RLC::");
371+
w.writeType(realType);
372+
w.write(".new(");
373+
374+
w.write("MyLib::RLC_");
375+
w.writeType(realType, 1);
376+
w.write(".new(__result2))");
377+
}
378+
else if (mlir::isa<mlir::rlc::StringLiteralType>(resultType))
379+
{
380+
w.write("__result2[0, ");
381+
printTypeSize(w, resultType);
382+
w.write("].to_s");
383+
}
384+
else
385+
{
386+
w.write("__result2[0, ");
387+
printTypeSize(w, resultType);
388+
w.write("].unpack1('");
389+
printTypeUnpack(w, resultType);
390+
w.write("')");
391+
if (mlir::isa<mlir::rlc::BoolType>(resultType))
392+
{
393+
w.write(" != 0");
394+
}
395+
}
396+
}
397+
else if (isUserDefined(resultType))
356398
{
357399
w.writenl("__to_return");
358400
}
@@ -776,6 +818,7 @@ namespace mlir::rlc
776818
auto _ = w.indent();
777819
w.writenl("__result = ", enumDecl.getName(), ".malloc");
778820
w.writenl("__result.value = ", value.index());
821+
779822
w.writenl("return __result").endLine();
780823
}
781824
}
@@ -1032,6 +1075,37 @@ namespace mlir::rlc
10321075
}
10331076
w.writenl("end");
10341077
emitSpecialFunctions(type, w, table, builder);
1078+
w.writenl("def unwrap()");
1079+
{
1080+
auto _2 = w.indent();
1081+
for (auto enumeration : llvm::enumerate(type.getUnderlying()))
1082+
{
1083+
w.write("if ");
1084+
w.writenl("@content.active_index.content == ", enumeration.index());
1085+
{
1086+
auto _ = w.indent();
1087+
w.write("return ");
1088+
1089+
if (not isUserDefined(enumeration.value()))
1090+
w.writenl(
1091+
"@content._data._alternative",
1092+
enumeration.index(),
1093+
".content");
1094+
else
1095+
{
1096+
w.writeType(enumeration.value());
1097+
w.writenl(
1098+
".new(",
1099+
"@content._data._alternative",
1100+
enumeration.index(),
1101+
")");
1102+
}
1103+
}
1104+
w.writenl("end");
1105+
}
1106+
w.writenl("return nil");
1107+
}
1108+
w.writenl("end");
10351109

10361110
w.writenl("def [](index)");
10371111
{

tool/rlc/test/wrappers/ruby_tic_tac_toe.rl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ puts(RLC::to_ruby_str(RLC::to_string(RLC::play)))
157157
action = RLC::RLCAnyGameAction.new
158158
_actions = RLC::enumerate(action)
159159
puts(_actions)
160-
actions = (0..._actions.size).map { |i| RLC::RLCAnyGameAction.new(_actions.get(i)) }
160+
actions = (0..._actions.size).map { |i| _actions.get(i) }
161161
puts(actions[0])
162162
puts(actions[1])
163163
puts(actions[2])

0 commit comments

Comments
 (0)