diff --git a/src/hx/cppia/CppiaClasses.cpp b/src/hx/cppia/CppiaClasses.cpp index 953a36bc9..f9e9f3c9b 100644 --- a/src/hx/cppia/CppiaClasses.cpp +++ b/src/hx/cppia/CppiaClasses.cpp @@ -1240,6 +1240,143 @@ void CppiaClassInfo::compile() } #endif +static ExprType signatureExprType(char inSig) +{ + switch(inSig) + { + case sigVoid: return etVoid; + case sigBool: + case sigInt: return etInt; + case sigFloat: return etFloat; + case sigString: return etString; + default: return etObject; + } +} + +// Host code calls native interface functions with the native signature, which uses Dynamic +// for type parameters. Script implementations may use more specific types (eg, String), +// so the arguments and return value need converting. +struct NativeInterfaceAdapter : public CppiaExpr +{ + ScriptCallable *function; + const char *signature; + + NativeInterfaceAdapter(ScriptCallable *inFunction, const char *inSignature) + : function(inFunction), signature(inSignature) { } + + const char *getName() HXCPP_OVERRIDE { return "NativeInterfaceAdapter"; } + + void pushArgs(CppiaCtx *ctx) + { + int n = strlen(signature)-1; + Array args = Array_obj::__new(n,n); + int pos = sizeof(hx::Object *); + for(int a=0;agetInt(pos)!=0; pos += sizeof(int); break; + case sigInt: args[a] = ctx->getInt(pos); pos += sizeof(int); break; + case sigFloat: args[a] = ctx->getFloat(pos); pos += sizeof(Float); break; + case sigString: args[a] = ctx->getString(pos); pos += sizeof(String); break; + default: args[a] = ctx->getObject(pos); pos += sizeof(hx::Object *); + } + function->pushArgsDynamic(ctx, ctx->getThis(false), args); + } + + void runVoid(CppiaCtx *ctx) HXCPP_OVERRIDE + { + unsigned char *pointer = ctx->pointer; + switch(signature[0]) + { + case sigVoid: + { + pushArgs(ctx); + AutoStack save(ctx,pointer); + ctx->runVoid(function); + } + break; + case sigBool: + case sigInt: + { + int result; + { + pushArgs(ctx); + AutoStack save(ctx,pointer); + result = runContextConvertInt(ctx, function->getReturnType(), function); + } + ctx->returnInt(result); + } + break; + case sigFloat: + { + Float result; + { + pushArgs(ctx); + AutoStack save(ctx,pointer); + result = runContextConvertFloat(ctx, function->getReturnType(), function); + } + ctx->returnFloat(result); + } + break; + case sigString: + { + String result; + { + pushArgs(ctx); + AutoStack save(ctx,pointer); + result = runContextConvertString(ctx, function->getReturnType(), function); + } + ctx->returnString(result); + } + break; + default: + { + hx::Object *result; + { + pushArgs(ctx); + AutoStack save(ctx,pointer); + result = runContextConvertObject(ctx, function->getReturnType(), function); + } + ctx->returnObject(result); + } + } + } +}; + +static bool signatureMatches(ScriptCallable *inFunction, const char *inSignature) +{ + int n = strlen(inSignature)-1; + if (n!=inFunction->args.size()) + return false; + if (inSignature[0]!=sigVoid && signatureExprType(inSignature[0])!=inFunction->getReturnType()) + return false; + for(int a=0;aargs[a].argType) + return false; + return true; +} + +// Returns the function to use for a native interface slot - the script function itself +// if its signature matches the native one, or a wrapper that converts the values. +static ScriptCallable *createNativeInterfaceFunction(CppiaModule &cppia, ScriptCallable *inFunction, ScriptNamedFunction *inNative) +{ + if (signatureMatches(inFunction, inNative->signature)) + return inFunction; + + // Arguments and return type follow the native signature, so script code calling through the slot + // pushes the values the adapter expects + ScriptCallable *result = new ScriptCallable(cppia, inNative); + result->argCount = result->args.size(); + result->returnType = signatureExprType(inNative->signature[0]); + result->body = new NativeInterfaceAdapter(inFunction, inNative->signature); + result->position = inFunction->position; + #ifdef CPPIA_JIT + result->compiled = 0; + #endif + return result; +} + + void CppiaClassInfo::link() { int newPos = -1; @@ -1275,7 +1412,20 @@ void CppiaClassInfo::link() int interfaceSlot = cppia.findInterfaceSlot( memberFunctions[i]->name ); DBGLOG("Set %s : %s to %d @%p\n", name.c_str(), memberFunctions[i]->name.c_str(), interfaceSlot, vtable); if (interfaceSlot>0 && interfaceSlotfunExpr; + { + void *func = memberFunctions[i]->funExpr; + if (!isInterface) + { + std::vector &nativeInterfaceFuncs = type->cppiaClass->nativeInterfaceFunctions; + for(int f=0;fname==nativeInterfaceFuncs[f]->name) + { + func = createNativeInterfaceFunction(cppia, memberFunctions[i]->funExpr, nativeInterfaceFuncs[f]); + break; + } + } + vtable[ -interfaceSlot ] = func; + } } } diff --git a/test/cppia/Client.hx b/test/cppia/Client.hx index 7242f9a21..fab23c44d 100644 --- a/test/cppia/Client.hx +++ b/test/cppia/Client.hx @@ -36,7 +36,8 @@ class Client { new TestClient(), new LocalFunctionExceptions(), new ReturnExpressions(), - new MemberCrements() + new MemberCrements(), + new NativeInterfaceTypes() ]; } } diff --git a/test/cppia/cases/client/NativeInterfaceTypes.hx b/test/cppia/cases/client/NativeInterfaceTypes.hx new file mode 100644 index 000000000..50636f49a --- /dev/null +++ b/test/cppia/cases/client/NativeInterfaceTypes.hx @@ -0,0 +1,64 @@ +package cases.client; + +import haxe.Constraints.IMap; + +// Script implementations of a host interface whose functions use Dynamic for type parameters + +private class StringStringMap implements IMap { + final map = new Map(); + + public function new() {} + + public function get(k:String):Null return map.get(k); + public function set(k:String, v:String):Void map.set(k, v); + public function exists(k:String):Bool return map.exists(k); + public function remove(k:String):Bool return map.remove(k); + public function keys():Iterator return map.keys(); + public function iterator():Iterator return map.iterator(); + public function keyValueIterator():KeyValueIterator return map.keyValueIterator(); + public function copy():IMap return null; + public function toString():String return map.toString(); + public function clear():Void map.clear(); + public function size():Int return map.size(); +} + +private class IntFloatMap implements IMap { + final map = new Map(); + + public function new() {} + + public function get(k:Int):Null return map.get(k); + public function set(k:Int, v:Float):Void map.set(k, v); + public function exists(k:Int):Bool return map.exists(k); + public function remove(k:Int):Bool return map.remove(k); + public function keys():Iterator return map.keys(); + public function iterator():Iterator return map.iterator(); + public function keyValueIterator():KeyValueIterator return map.keyValueIterator(); + public function copy():IMap return null; + public function toString():String return map.toString(); + public function clear():Void map.clear(); + public function size():Int return map.size(); +} + +class NativeInterfaceTypes extends Test { + function testStringStringMap() { + final m:IMap = new StringStringMap(); + m.set("a", "x"); + Assert.equals("x", m.get("a")); + Assert.isTrue(m.exists("a")); + Assert.isFalse(m.exists("b")); + Assert.equals(1, m.size()); + Assert.isTrue(m.remove("a")); + Assert.isNull(m.get("a")); + } + + function testIntFloatMap() { + final m:IMap = new IntFloatMap(); + m.set(1, 1.5); + Assert.equals(1.5, m.get(1)); + Assert.isTrue(m.exists(1)); + Assert.isFalse(m.exists(2)); + Assert.isTrue(m.remove(1)); + Assert.isFalse(m.exists(1)); + } +}