Josh Engebretson 10 vuotta sitten
vanhempi
sitoutus
76109af88e

+ 93 - 21
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.cpp

@@ -351,26 +351,70 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
     JSBClass* klass = function_->GetClass();
     JSBPackage* package = klass->GetPackage();
 
-    line = ToString("private static extern IntPtr csb_%s_%s_%s(IntPtr self);\n",
-                    package->GetName().CString(), klass->GetName().CString(), function_->GetName().CString());
+    String returnType = CSTypeHelper::GetNativeTypeString(function_->GetReturnType());
 
-    source += IndentLine(line);
+    Vector<JSBFunctionType*>& parameters = function_->GetParameters();
 
-    source += "\n";
+    Vector<String> args;
 
-}
+    if (!function_->IsConstructor())
+    {
+        args.Push("IntPtr self");
+    }
 
-void CSFunctionWriter::WriteManagedPInvokeConstructorSignature(String& source)
-{
-    source += "\n";
+    if (parameters.Size())
+    {
+        for (unsigned int i = 0; i < parameters.Size(); i++)
+        {
+            JSBFunctionType* ptype = parameters.At(i);
 
-    String line = "[DllImport (LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
-    source += IndentLine(line);
-    JSBClass* klass = function_->GetClass();
-    JSBPackage* package = klass->GetPackage();
+            // ignore "Context" parameters
+            if (ptype->type_->asClassType())
+            {
+                JSBClassType* classType = ptype->type_->asClassType();
+                JSBClass* klass = classType->class_;
+                if (klass->GetName() == "Context")
+                {
+                    continue;
+                }
+
+                if (klass->IsNumberArray())
+                {
+                    args.Push("ref " + klass->GetName() + " " + ptype->name_);
+                }
+                else
+                {
+                    args.Push("IntPtr " + ptype->name_);
+                }
+            }
+            else
+            {
+                args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
+            }
+
+        }
+    }
 
-    line = ToString("private static extern IntPtr csb_%s_%s_Constructor(IntPtr self);\n",
-                    package->GetName().CString(), klass->GetName().CString());
+    if (function_->GetReturnType())
+    {
+        if (function_->GetReturnType()->type_->asClassType())
+        {
+            JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
+            if (retClass->IsNumberArray())
+            {
+                args.Push("ref " + retClass->GetName() + " retValue");
+            }
+
+        }
+    }
+
+    String pstring;
+    pstring.Join(args, ", ");
+
+    String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
+    line = ToString("private static extern %s csb_%s_%s_%s(%s);\n",
+                    returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
+                    fname.CString(), pstring.CString());
 
     source += IndentLine(line);
 
@@ -412,18 +456,49 @@ void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
 void CSFunctionWriter::WriteManagedConstructor(String& source)
 {
     JSBClass* klass = function_->GetClass();
+    JSBPackage* package = klass->GetPackage();
+
+    // wrapping constructor
+
+    String line;
+
+    if (klass->GetName() != "RefCounted")
+    {
+        line = ToString("public %s (IntPtr native) : base (native)\n", klass->GetName().CString());
+        source += IndentLine(line);
+        source += IndentLine("{\n");
+        source += IndentLine("}\n\n");
+    }
 
     String sig;
     GenManagedFunctionParameters(sig);
 
-    String line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
+    line = ToString("public %s (%s)\n", klass->GetName().CString(), sig.CString());
 
     source += IndentLine(line);
 
-    source += IndentLine("{");
+    source += IndentLine("{\n");
 
+    Indent();
 
-    source+= "\n";
+    source += IndentLine("if (nativeInstance == IntPtr.Zero)");
+    source += IndentLine("{\n");
+
+    Indent();
+
+    String callSig;
+    GenPInvokeCallParameters(callSig);
+
+    line = ToString("nativeInstance = NativeCore.RegisterNative (csb_%s_%s_Constructor(%s), this);\n",
+                     package->GetName().CString(), klass->GetName().CString(), callSig.CString());
+
+    source += IndentLine(line);
+
+    Dedent();
+
+    source += IndentLine("}\n");
+
+    Dedent();
 
     source += IndentLine("}\n");
 }
@@ -580,10 +655,7 @@ void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
     else
         WriteManagedFunction(source);
 
-    if (function_->IsConstructor())
-        WriteManagedPInvokeConstructorSignature(source);
-    else
-        WriteManagedPInvokeFunctionSignature(source);
+    WriteManagedPInvokeFunctionSignature(source);
 
     // data marshaller
     if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))

+ 0 - 1
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.h

@@ -48,7 +48,6 @@ private:
     void WriteManagedConstructor(String& source);
     void WriteManagedFunction(String& source);
 
-    void WriteManagedPInvokeConstructorSignature(String& source);
     void WriteManagedPInvokeFunctionSignature(String& source);
 
 };

+ 46 - 1
Source/ToolCore/JSBind/CSharp/CSTypeHelper.cpp

@@ -36,7 +36,7 @@ String CSTypeHelper::GetManagedTypeString(JSBType* type)
     }
     else if (type->asStringType() || type->asStringHashType())
     {
-        value = "String";
+        value = "string";
     }
     else if (type->asEnumType())
     {
@@ -74,6 +74,51 @@ String CSTypeHelper::GetManagedTypeString(JSBFunctionType* ftype)
 
 }
 
+String CSTypeHelper::GetNativeTypeString(JSBType* type)
+{
+    String value;
+
+    if (type->asClassType())
+    {
+        JSBClassType* classType = type->asClassType();
+        if (classType->class_->IsNumberArray())
+            value = "void";
+        else
+            value = "IntPtr";
+    }
+    else if (type->asStringType() || type->asStringHashType())
+    {
+        value = "string";
+    }
+    else if (type->asEnumType())
+    {
+        value = type->asEnumType()->enum_->GetName();
+    }
+    else if (type->asPrimitiveType())
+    {
+        value = GetManagedPrimitiveType(type->asPrimitiveType());
+    }
+    else if (type->asVectorType())
+    {
+        JSBVectorType* vectorType = type->asVectorType();
+
+        value = GetManagedTypeString(vectorType->vectorType_) + "[]";
+    }
+
+    return value;
+}
+
+String CSTypeHelper::GetNativeTypeString(JSBFunctionType* ftype)
+{
+    if (!ftype)
+        return "void";
+
+    String value = GetNativeTypeString(ftype->type_);
+
+    return value;
+
+}
+
 bool CSTypeHelper::IsSimpleReturn(JSBType* type)
 {
     if (type->asClassType())

+ 3 - 0
Source/ToolCore/JSBind/CSharp/CSTypeHelper.h

@@ -27,6 +27,9 @@ public:
 
     static String GetManagedPrimitiveType(JSBPrimitiveType* ptype);
 
+    static String GetNativeTypeString(JSBType* type);
+    static String GetNativeTypeString(JSBFunctionType* ftype);
+
     static String GetManagedTypeString(JSBType* type);
     static String GetManagedTypeString(JSBFunctionType* ftype);