From 62272dd428439350cb23b0fdcccabd34d0ea8220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aurel=20Bi=CC=81ly=CC=81?= Date: Mon, 5 Apr 2021 20:12:07 +0200 Subject: [PATCH] add always-zero Unsupported type --- src/ammer/Ammer.hx | 3 +++ src/ammer/FFITools.hx | 13 +++++++++---- src/ammer/FFIType.hx | 2 ++ src/ammer/ffi/Unsupported.hx | 3 +++ src/ammer/patch/PatchCpp.hx | 2 ++ src/ammer/patch/PatchHl.hx | 2 ++ src/ammer/patch/PatchLua.hx | 2 ++ src/ammer/stub/StubBaseC.hx | 1 + src/ammer/stub/StubCpp.hx | 1 + src/ammer/stub/StubHl.hx | 3 +++ src/ammer/stub/StubLua.hx | 2 ++ tests/Native.hx | 2 ++ tests/native/native.c | 6 +++++- tests/native/native.h | 2 ++ tests/test/TestSignature.hx | 4 ++++ 15 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/ammer/ffi/Unsupported.hx diff --git a/src/ammer/Ammer.hx b/src/ammer/Ammer.hx index 8e39e4e..2f11710 100644 --- a/src/ammer/Ammer.hx +++ b/src/ammer/Ammer.hx @@ -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() @@ -750,6 +751,7 @@ class Ammer { case LibIntEnum(_, true): continue; case Derived(_) | SizeOfReturn: continue; case ClosureData(_): continue; + case Unsupported(_): continue; case t: t.toComplexType(); }) } ]; @@ -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); diff --git a/src/ammer/FFITools.hx b/src/ammer/FFITools.hx index 11979e6..a2ff820 100644 --- a/src/ammer/FFITools.hx +++ b/src/ammer/FFITools.hx @@ -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 "!"; }); } @@ -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); @@ -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"}: diff --git a/src/ammer/FFIType.hx b/src/ammer/FFIType.hx index 316d19b..8267af9 100644 --- a/src/ammer/FFIType.hx +++ b/src/ammer/FFIType.hx @@ -51,6 +51,8 @@ enum FFIType { SizeOfReturn; SizeOfField(name:String); + Unsupported(cName:String); + // target specific NativeHl(t:ComplexType, ffiName:String, cName:String); } diff --git a/src/ammer/ffi/Unsupported.hx b/src/ammer/ffi/Unsupported.hx new file mode 100644 index 0000000..7017ca3 --- /dev/null +++ b/src/ammer/ffi/Unsupported.hx @@ -0,0 +1,3 @@ +package ammer.ffi; + +class Unsupported {} diff --git a/src/ammer/patch/PatchCpp.hx b/src/ammer/patch/PatchCpp.hx index 2928a5d..d5f3d27 100644 --- a/src/ammer/patch/PatchCpp.hx +++ b/src/ammer/patch/PatchCpp.hx @@ -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); diff --git a/src/ammer/patch/PatchHl.hx b/src/ammer/patch/PatchHl.hx index a34bc1f..d6637e7 100644 --- a/src/ammer/patch/PatchHl.hx +++ b/src/ammer/patch/PatchHl.hx @@ -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); diff --git a/src/ammer/patch/PatchLua.hx b/src/ammer/patch/PatchLua.hx index a5714da..4f40efa 100644 --- a/src/ammer/patch/PatchLua.hx +++ b/src/ammer/patch/PatchLua.hx @@ -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); diff --git a/src/ammer/stub/StubBaseC.hx b/src/ammer/stub/StubBaseC.hx index 8b624af..7847e32 100644 --- a/src/ammer/stub/StubBaseC.hx +++ b/src/ammer/stub/StubBaseC.hx @@ -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' : ""); } diff --git a/src/ammer/stub/StubCpp.hx b/src/ammer/stub/StubCpp.hx index 4939823..432bbc4 100644 --- a/src/ammer/stub/StubCpp.hx +++ b/src/ammer/stub/StubCpp.hx @@ -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(", ") + ')'; diff --git a/src/ammer/stub/StubHl.hx b/src/ammer/stub/StubHl.hx index 3757557..d8748c3 100644 --- a/src/ammer/stub/StubHl.hx +++ b/src/ammer/stub/StubHl.hx @@ -49,6 +49,7 @@ class StubHl { case SizeOfField(_): "_I32"; case SameSizeAs(t, _): mapTypeHlFFI(t); case NativeHl(_, ffiName, _): ffiName; + case Unsupported(_): "_I32"; // dummy case _: throw "!"; }); } @@ -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); }); } @@ -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(", ") + ')'; diff --git a/src/ammer/stub/StubLua.hx b/src/ammer/stub/StubLua.hx index a86e253..b705cc8 100644 --- a/src/ammer/stub/StubLua.hx +++ b/src/ammer/stub/StubLua.hx @@ -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 "!"; }); } @@ -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) diff --git a/tests/Native.hx b/tests/Native.hx index 852862d..67c9f9a 100644 --- a/tests/Native.hx +++ b/tests/Native.hx @@ -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"> { diff --git a/tests/native/native.c b/tests/native/native.c index 8eebc39..d0e80be 100644 --- a/tests/native/native.c +++ b/tests/native/native.c @@ -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; -} \ No newline at end of file +} + +LIB_EXPORT bool take_unsupported(void *a, double b) { + return a == 0 && abs(b) < .0001; +} diff --git a/tests/native/native.h b/tests/native/native.h index ac16cdd..371c0a8 100644 --- a/tests/native/native.h +++ b/tests/native/native.h @@ -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 diff --git a/tests/test/TestSignature.hx b/tests/test/TestSignature.hx index 54d108b..2bdf1f4 100644 --- a/tests/test/TestSignature.hx +++ b/tests/test/TestSignature.hx @@ -29,4 +29,8 @@ class TestSignature extends Test { Native.NativePrefixed.nop2(); eq(Native.NativePrefixed.take_0(), 0); } + + function testUnsupported() { + t(Native.take_unsupported()); + } }