Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 151 additions & 1 deletion src/hx/cppia/CppiaClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dynamic> args = Array_obj<Dynamic>::__new(n,n);
int pos = sizeof(hx::Object *);
for(int a=0;a<n;a++)
switch(signature[a+1])
{
case sigBool: args[a] = ctx->getInt(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;a<n;a++)
if (signatureExprType(inSignature[a+1])!=inFunction->args[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;
Expand Down Expand Up @@ -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 && interfaceSlot<interfaceSlotSize)
vtable[ -interfaceSlot ] = memberFunctions[i]->funExpr;
{
void *func = memberFunctions[i]->funExpr;
if (!isInterface)
{
std::vector<ScriptNamedFunction *> &nativeInterfaceFuncs = type->cppiaClass->nativeInterfaceFunctions;
for(int f=0;f<nativeInterfaceFuncs.size();f++)
if (memberFunctions[i]->name==nativeInterfaceFuncs[f]->name)
{
func = createNativeInterfaceFunction(cppia, memberFunctions[i]->funExpr, nativeInterfaceFuncs[f]);
break;
}
}
vtable[ -interfaceSlot ] = func;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/cppia/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Client {
new TestClient(),
new LocalFunctionExceptions(),
new ReturnExpressions(),
new MemberCrements()
new MemberCrements(),
new NativeInterfaceTypes()
];
}
}
64 changes: 64 additions & 0 deletions test/cppia/cases/client/NativeInterfaceTypes.hx
Original file line number Diff line number Diff line change
@@ -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<String, String> {
final map = new Map<String, String>();

public function new() {}

public function get(k:String):Null<String> 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<String> return map.keys();
public function iterator():Iterator<String> return map.iterator();
public function keyValueIterator():KeyValueIterator<String, String> return map.keyValueIterator();
public function copy():IMap<String, String> 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<Int, Float> {
final map = new Map<Int, Float>();

public function new() {}

public function get(k:Int):Null<Float> 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<Int> return map.keys();
public function iterator():Iterator<Float> return map.iterator();
public function keyValueIterator():KeyValueIterator<Int, Float> return map.keyValueIterator();
public function copy():IMap<Int, Float> 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<String, String> = 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<Int, Float> = 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));
}
}
Loading