Skip to content

Commit

Permalink
add always-zero Unsupported type
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurel300 committed Apr 5, 2021
1 parent 07c9ed7 commit 62272dd
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/ammer/Ammer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class Ammer {
case Derived(_, _) | SizeOfReturn: continue;
case ClosureDataUse: continue;
case ClosureData(_): continue;
case Unsupported(_): continue;
case t: {
name: '_arg$i',
type: t.toComplexType()
Expand Down Expand Up @@ -750,6 +751,7 @@ class Ammer {
case LibIntEnum(_, true): continue;
case Derived(_) | SizeOfReturn: continue;
case ClosureData(_): continue;
case Unsupported(_): continue;
case t: t.toComplexType();
})
} ];
Expand All @@ -759,6 +761,7 @@ class Ammer {
case LibIntEnum(_, true): macro this;
case Derived(_) | SizeOfReturn: continue;
case ClosureData(_): continue;
case Unsupported(_): continue;
case _: Utils.arg(i);
} ];
typeCtx.ffiMethods.push(ffi);
Expand Down
13 changes: 9 additions & 4 deletions src/ammer/FFITools.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class FFITools {
case SizeOfReturn: (macro:Int);
case SizeOfField(_): (macro:Int);
case NativeHl(ct, _, _): ct;
case Unsupported(_): (macro:Int); // pass dummy 0
case _: throw "!";
});
}
Expand Down Expand Up @@ -208,10 +209,10 @@ class FFITools {
SizeOfField(fieldName);
// context independent
case [TInst(_.get() => {name: "NativeHl", pack: ["ammer", "ffi"]}, [
inner,
TInst(_.get() => {kind: KExpr({expr: EConst(CString(ffiName))})}, []),
TInst(_.get() => {kind: KExpr({expr: EConst(CString(cName))})}, []),
]), _]:
inner,
TInst(_.get() => {kind: KExpr({expr: EConst(CString(ffiName))})}, []),
TInst(_.get() => {kind: KExpr({expr: EConst(CString(cName))})}, []),
]), _]:
NativeHl(Context.toComplexType(inner), ffiName, cName);
case [TInst(_.get() => {name: "ArrayDynamic", pack: ["ammer", "ffi"]}, [inner]), _]:
var inner = toFFITypeResolved(inner, ctx);
Expand Down Expand Up @@ -288,6 +289,10 @@ class FFITools {
if (!inner.match(LibType(_, _)))
Context.fatalError("Alloc must wrap a pointer type", ctx.pos);
Alloc(inner);
case [TInst(_.get() => {name: "Unsupported", pack: ["ammer", "ffi"]}, [
TInst(_.get() => {kind: KExpr({expr: EConst(CString(cName))})}, []),
]), _]:
Unsupported(cName);
case [TInst(_.get() => type, []), _] if (type.superClass != null):
switch (type.superClass.t.get()) {
case {name: "PointerProcessed", module: "ammer.Pointer"}:
Expand Down
2 changes: 2 additions & 0 deletions src/ammer/FFIType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ enum FFIType {
SizeOfReturn;
SizeOfField(name:String);

Unsupported(cName:String);

// target specific
NativeHl(t:ComplexType, ffiName:String, cName:String);
}
Expand Down
3 changes: 3 additions & 0 deletions src/ammer/ffi/Unsupported.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ammer.ffi;

class Unsupported<Const> {}
2 changes: 2 additions & 0 deletions src/ammer/patch/PatchCpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class PatchCppMethod extends ammer.patch.PatchMethod {
ctx.callArgs[i] = macro 0;
case OutPointer(LibType(_, _)):
ctx.callArgs[i] = macro untyped __cpp__("&{0}->ammerNative.ptr", $e{ctx.callArgs[i]});
case Unsupported(_):
ctx.callArgs[i] = macro 0;
case _:
}
super.visitArgument(i, ffi);
Expand Down
2 changes: 2 additions & 0 deletions src/ammer/patch/PatchHl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class PatchHlMethod extends ammer.patch.PatchMethod {
};
case ClosureData(_):
ctx.callArgs[i] = macro 0;
case Unsupported(_):
ctx.callArgs[i] = macro 0;
case _:
}
super.visitArgument(i, ffi);
Expand Down
2 changes: 2 additions & 0 deletions src/ammer/patch/PatchLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class PatchLuaMethod extends ammer.patch.PatchMethod {
var _retSize = 0;
${ctx.wrapExpr};
};
case Unsupported(_):
ctx.callArgs[i] = macro 0;
case _:
}
super.visitArgument(i, ffi);
Expand Down
1 change: 1 addition & 0 deletions src/ammer/stub/StubBaseC.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class StubBaseC {
case SizeOf(_): "int";
case SizeOfField(_): "int";
case SameSizeAs(t, _): return mapTypeC(t, name);
case Unsupported(cName): cName;
case NativeHl(_, _, _): throw "!";
}) + (name != "" ? ' $name' : "");
}
Expand Down
1 change: 1 addition & 0 deletions src/ammer/stub/StubCpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class StubCpp {
case ClosureData(f): 'arg_$f.mPtr';
case LibIntEnum(t, _): '(${t.nativeName})arg_$i';
case Nested(LibType(_, _)): '(*arg_$i)';
case Unsupported(cName): '($cName)0';
case _: 'arg_$i';
}
} ].join(", ") + ')';
Expand Down
3 changes: 3 additions & 0 deletions src/ammer/stub/StubHl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class StubHl {
case SizeOfField(_): "_I32";
case SameSizeAs(t, _): mapTypeHlFFI(t);
case NativeHl(_, ffiName, _): ffiName;
case Unsupported(_): "_I32"; // dummy
case _: throw "!";
});
}
Expand All @@ -61,6 +62,7 @@ class StubHl {
case OutPointer(LibType(_, _)): 'vdynamic *$name';
case Nested(LibType(t, _)) if (closure): '${t.nativeName} $name';
case NativeHl(_, _, cName): '$cName $name';
case Unsupported(_): 'int $name';
case _: StubBaseC.mapTypeC(t, name);
});
}
Expand Down Expand Up @@ -133,6 +135,7 @@ class StubHl {
case ClosureData(f): '(void *)arg_$f';
case OutPointer(LibType(t, _)): '(${t.nativeName} **)(&(((void **)arg_$i)[1]))';
case Nested(LibType(_, _)): '(*arg_$i)';
case Unsupported(cName): '($cName)0';
case _: 'arg_$i';
}
} ].join(", ") + ')';
Expand Down
2 changes: 2 additions & 0 deletions src/ammer/stub/StubLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class StubLua {
case WithSize(_, String | Bytes):
lb.ai('size_t arg_${i - 1}_size = 0;\n');
'lua_tolstring(L, $i, &arg_${i - 1}_size)';
case Unsupported(_): null;
case _: throw "!";
});
}
Expand Down Expand Up @@ -108,6 +109,7 @@ class StubLua {
lb.ai('${method.cPrereturn}\n');
var call = '${method.native}(' + [ for (i in 0...method.args.length) switch (method.args[i]) {
case SizeOfReturn: '&arg_$i';
case Unsupported(cName): '($cName)0';
case _: 'arg_$i';
} ].join(", ") + ')';
if (method.ret == Void)
Expand Down
2 changes: 2 additions & 0 deletions tests/Native.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Native extends Library<"native"> {

public static function take_enum(a:NativeEnum, b:NativeEnum, c:NativeEnum):Bool;
public static function give_enum():NativeEnum;

public static function take_unsupported(a:Unsupported<"void *">, b:Unsupported<"double">):Bool;
}

class Native2 extends Library<"native"> {
Expand Down
6 changes: 5 additions & 1 deletion tests/native/native.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,8 @@ LIB_EXPORT int take_array(int *a, size_t b) {
}
LIB_EXPORT void take_array_modify(int *a) {
a[1] = 42;
}
}

LIB_EXPORT bool take_unsupported(void *a, double b) {
return a == 0 && abs(b) < .0001;
}
2 changes: 2 additions & 0 deletions tests/native/native.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ LIB_EXPORT int take_array_fixed(int a[3]);
LIB_EXPORT int take_array(int *a, size_t b);
LIB_EXPORT void take_array_modify(int *a);

LIB_EXPORT bool take_unsupported(void *a, double b);

#ifdef __cplusplus
}
#endif
4 changes: 4 additions & 0 deletions tests/test/TestSignature.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ class TestSignature extends Test {
Native.NativePrefixed.nop2();
eq(Native.NativePrefixed.take_0(), 0);
}

function testUnsupported() {
t(Native.take_unsupported());
}
}

0 comments on commit 62272dd

Please sign in to comment.