Browse Source

Working on Atomic C#

Josh Engebretson 10 years ago
parent
commit
be2ae68d35

+ 6 - 0
Build/AtomicSharp/AtomicSharp.sln

@@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 2012
 # Visual Studio 2012
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AtomicSharp", "AtomicSharp.csproj", "{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}"
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AtomicSharp", "AtomicSharp.csproj", "{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}"
 EndProject
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AtomicSharpTest", "AtomicSharpTest\AtomicSharpTest.csproj", "{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}"
+EndProject
 Global
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		Debug|Any CPU = Debug|Any CPU
@@ -13,5 +15,9 @@ Global
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.Build.0 = Release|Any CPU
 		{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}.Release|Any CPU.Build.0 = Release|Any CPU
+		{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	EndGlobalSection
 EndGlobal
 EndGlobal

+ 44 - 0
Build/AtomicSharp/AtomicSharpTest/AtomicSharpTest.csproj

@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{DBD98CE5-11DE-47F8-BF9A-83BF8576794E}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>AtomicSharpTest</RootNamespace>
+    <AssemblyName>AtomicSharpTest</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>full</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <ItemGroup>
+    <ProjectReference Include="..\AtomicSharp.csproj">
+      <Project>{3A4C776B-61D6-412E-B1E9-7A1C84CD6B5B}</Project>
+      <Name>AtomicSharp</Name>
+    </ProjectReference>
+  </ItemGroup>
+</Project>

+ 17 - 0
Build/AtomicSharp/AtomicSharpTest/Program.cs

@@ -0,0 +1,17 @@
+using System;
+
+using AtomicSharp;
+
+namespace AtomicSharpTest
+{
+	class MainClass
+	{
+		public static void Main (string[] args)
+		{
+			AtomicSharp.AtomicSharp.Initialize ();
+
+			while (AtomicSharp.AtomicSharp.RunFrame ()) {
+			}
+		}
+	}
+}

+ 27 - 0
Build/AtomicSharp/AtomicSharpTest/Properties/AssemblyInfo.cs

@@ -0,0 +1,27 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes.
+// Change them to the values specific to your project.
+
+[assembly: AssemblyTitle ("AtomicSharpTest")]
+[assembly: AssemblyDescription ("")]
+[assembly: AssemblyConfiguration ("")]
+[assembly: AssemblyCompany ("")]
+[assembly: AssemblyProduct ("")]
+[assembly: AssemblyCopyright ("josh")]
+[assembly: AssemblyTrademark ("")]
+[assembly: AssemblyCulture ("")]
+
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+
+[assembly: AssemblyVersion ("1.0.*")]
+
+// The following attributes are used to specify the signing key for the assembly,
+// if desired. See the Mono documentation for more information about signing.
+
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+

+ 25 - 5
Build/AtomicSharp/MyClass.cs

@@ -7,20 +7,40 @@ namespace AtomicSharp
 
 
 	public class MyClass
 	public class MyClass
 	{
 	{
-		void DoSomething()
+		Light CreateLight()
 		{
 		{
 			var light = new Light ();
 			var light = new Light ();
-			light.SetLightType(LightType.
+			light.LightType = LightType.LIGHT_DIRECTIONAL;
+			return light;
 		}
 		}
 	}
 	}
 
 
+	public static class AtomicSharp
+	{
+		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
+		private static extern int atomicsharp_initialize ();
 
 
-
-	
-
+		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
+		private static extern bool atomicsharp_runframe ();
 
 
 
 
+		public static bool RunFrame()
+		{
+			return atomicsharp_runframe ();
+		}
 
 
+		public static void Initialize()
+		{
+			ContainerModule.Initialize ();
+			CoreModule.Initialize ();
+			IOModule.Initialize ();
+			ResourceModule.Initialize ();
+			GraphicsModule.Initialize ();
+			SceneModule.Initialize ();		
+
+			atomicsharp_initialize ();
+		}
+	}
 
 
 	public static partial class Constants
 	public static partial class Constants
 	{
 	{

+ 51 - 2
Source/ToolCore/JSBind/CSharp/CSClassWriter.cpp

@@ -38,6 +38,9 @@ void CSClassWriter::WriteNativeFunctions(String& source)
         if (function->IsDestructor())
         if (function->IsDestructor())
             continue;
             continue;
 
 
+        if (OmitFunction(function))
+            continue;
+
         CSFunctionWriter writer(function);
         CSFunctionWriter writer(function);
         writer.GenerateNativeSource(source);
         writer.GenerateNativeSource(source);
     }
     }
@@ -51,8 +54,10 @@ void CSClassWriter::GenerateNativeSource(String& sourceOut)
     if (klass_->IsNumberArray())
     if (klass_->IsNumberArray())
         return;
         return;
 
 
-    source.AppendWithFormat("ClassID csb_%s_GetClassID()\n{\n", klass_->GetNativeName().CString());
-    source.AppendWithFormat("return %s::GetClassIDStatic();\n}\n", klass_->GetNativeName().CString());
+    JSBPackage* package = klass_->GetPackage();
+
+    source.AppendWithFormat("ClassID csb_%s_%s_GetClassID()\n{\n", package->GetName().CString(),klass_->GetName().CString());
+    source.AppendWithFormat("   return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
 
 
     WriteNativeFunctions(source);
     WriteNativeFunctions(source);
 
 
@@ -77,6 +82,9 @@ void CSClassWriter::WriteManagedProperties(String& sourceOut)
             JSBFunctionType* getType = NULL;
             JSBFunctionType* getType = NULL;
             JSBFunctionType* setType = NULL;
             JSBFunctionType* setType = NULL;
 
 
+            if (OmitFunction(prop->getter_) || OmitFunction(prop->setter_))
+                continue;
+
             if (prop->getter_ && !prop->getter_->Skip())
             if (prop->getter_ && !prop->getter_->Skip())
             {
             {
                 fType = getType = prop->getter_->GetReturnType();
                 fType = getType = prop->getter_->GetReturnType();
@@ -87,8 +95,13 @@ void CSClassWriter::WriteManagedProperties(String& sourceOut)
 
 
                 if (!fType)
                 if (!fType)
                     fType = setType;
                     fType = setType;
+                //else if (fType->type_ != setType->type_)
+                //    continue;
             }
             }
 
 
+            if (!fType)
+                continue;
+
             String type = CSTypeHelper::GetManagedTypeString(fType, false);
             String type = CSTypeHelper::GetManagedTypeString(fType, false);
             String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
             String line = ToString("public %s %s\n", type.CString(), prop->name_.CString());
             source += IndentLine(line);
             source += IndentLine(line);
@@ -136,6 +149,30 @@ void CSClassWriter::WriteManagedProperties(String& sourceOut)
 
 
 }
 }
 
 
+bool CSClassWriter::OmitFunction(JSBFunction* function)
+{
+    if (!function)
+        return false;
+
+    // We need to rename GetType
+    if (function->GetName() == "GetType")
+        return true;
+
+    // avoid vector type for now
+    if (function->GetReturnType() && function->GetReturnType()->type_->asVectorType())
+        return true;
+
+    Vector<JSBFunctionType*>& parameters = function->GetParameters();
+
+    for (unsigned i = 0; i < parameters.Size(); i++)
+    {
+        if (parameters[i]->type_->asVectorType())
+            return true;
+    }
+
+    return false;
+}
+
 void CSClassWriter::GenerateManagedSource(String& sourceOut)
 void CSClassWriter::GenerateManagedSource(String& sourceOut)
 {
 {
     String source = "";
     String source = "";
@@ -161,6 +198,15 @@ void CSClassWriter::GenerateManagedSource(String& sourceOut)
 
 
     WriteManagedProperties(source);
     WriteManagedProperties(source);
 
 
+    Indent();
+    JSBPackage* package = klass_->GetPackage();
+    line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
+    source += IndentLine(line);
+    line = ToString("public static extern IntPtr csb_%s_%s_GetClassID();\n", package->GetName().CString(),klass_->GetName().CString());
+    source += IndentLine(line);
+    source += "\n";
+    Dedent();
+
     // managed functions
     // managed functions
     for (unsigned i = 0; i < klass_->functions_.Size(); i++)
     for (unsigned i = 0; i < klass_->functions_.Size(); i++)
     {
     {
@@ -172,6 +218,9 @@ void CSClassWriter::GenerateManagedSource(String& sourceOut)
         if (function->IsDestructor())
         if (function->IsDestructor())
             continue;
             continue;
 
 
+        if (OmitFunction(function))
+            continue;
+
         CSFunctionWriter fwriter(function);
         CSFunctionWriter fwriter(function);
         fwriter.GenerateManagedSource(source);
         fwriter.GenerateManagedSource(source);
 
 

+ 2 - 0
Source/ToolCore/JSBind/CSharp/CSClassWriter.h

@@ -18,6 +18,7 @@ namespace ToolCore
 
 
 class JSBPackage;
 class JSBPackage;
 class JSBClass;
 class JSBClass;
+class JSBFunction;
 
 
 class CSClassWriter : public JSBClassWriter
 class CSClassWriter : public JSBClassWriter
 {
 {
@@ -33,6 +34,7 @@ public:
 
 
 private:
 private:
 
 
+    bool OmitFunction(JSBFunction* function);
 
 
     void WriteNativeFunctions(String& source);
     void WriteNativeFunctions(String& source);
 
 

+ 160 - 165
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.cpp

@@ -110,39 +110,22 @@ void CSFunctionWriter::WriteNativeParameterMarshal(String& source)
 
 
 void CSFunctionWriter::WriteNativeConstructor(String& source)
 void CSFunctionWriter::WriteNativeConstructor(String& source)
 {
 {
-    JSBClass* klass = function_->class_;
-
-    if (klass->IsAbstract())
-        return;
-
-    // just object for now, as constructor takes a context
-    if (!klass->IsObject())
-        return;
-
-    // more than context arg, don't marshal yet
-    if (function_->GetParameters().Size() > 1)
-    {
-        return;
-    }
-
-
-    source.AppendWithFormat("RefCounted* csb_%s_Constructor()\n{\nreturn new %s(AtomicSharp::GetContext());\n}\n",
-                            klass->GetName().CString(), klass->GetNativeName().CString());
-
 
 
 }
 }
 
 
-void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
+void CSFunctionWriter::GenNativeCallParameters(String& sig)
 {
 {
-    // generate args
+    JSBClass* klass = function_->GetClass();
+
     Vector<JSBFunctionType*>& parameters = function_->GetParameters();
     Vector<JSBFunctionType*>& parameters = function_->GetParameters();
 
 
-    int cparam = 0;
+    Vector<String> args;
+
     if (parameters.Size())
     if (parameters.Size())
     {
     {
-        for (unsigned int i = 0; i < parameters.Size(); i++, cparam++)
+        for (unsigned int i = 0; i < parameters.Size(); i++)
         {
         {
-            JSBFunctionType * ptype = parameters.At(i);
+            JSBFunctionType* ptype = parameters.At(i);
 
 
             // ignore "Context" parameters
             // ignore "Context" parameters
             if (ptype->type_->asClassType())
             if (ptype->type_->asClassType())
@@ -151,175 +134,183 @@ void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
                 JSBClass* klass = classType->class_;
                 JSBClass* klass = classType->class_;
                 if (klass->GetName() == "Context")
                 if (klass->GetName() == "Context")
                 {
                 {
-                    cparam--;
                     continue;
                     continue;
                 }
                 }
-            }
-
-            String pstring = ptype->ToArgString(cparam);
-
-            if (ptype->type_->asClassType())
-            {
-                JSBClassType* classType = ptype->type_->asClassType();
-
-                JSBClass* klass = classType->class_;
 
 
-                if (!klass->IsNumberArray())
-                {
-                    sig.AppendWithFormat("%s", pstring.CString());
-                }
+                if (klass->IsNumberArray())
+                    args.Push(ToString("*%s", ptype->name_.CString()));
                 else
                 else
-                {
-                    sig.AppendWithFormat("%s __arg%i", klass->GetNativeName().CString(), cparam);
-                }
-            }
-            else if (ptype->type_->asStringType() || ptype->type_->asStringHashType())
-            {
-                sig.AppendWithFormat("char* __arg%i", cparam);
-            }
-            else if (ptype->type_->asHeapPtrType())
-            {
-                assert(0);
+                    args.Push(ToString("%s", ptype->name_.CString()));
+
             }
             }
-            else if (ptype->type_->asPrimitiveType())
+            else
             {
             {
-                JSBPrimitiveType* prtype = ptype->type_->asPrimitiveType();
-                sig.AppendWithFormat("%s __arg%i", prtype->ToString().CString(), cparam);
+                args.Push(ToString("%s", ptype->name_.CString()));
             }
             }
-            else if (ptype->type_->asEnumType())
-            {
-                JSBEnumType* etype = ptype->type_->asEnumType();
 
 
-                sig.AppendWithFormat("%s __arg%i", etype->enum_->GetName().CString(), cparam);
+        }
+    }
 
 
-            }
-            else if (ptype->type_->asVectorType())
+    sig.Join(args, ", ");
+}
+
+void CSFunctionWriter::GenNativeFunctionSignature(String& sig)
+{
+    JSBClass* klass = function_->GetClass();
+
+    Vector<JSBFunctionType*>& parameters = function_->GetParameters();
+
+    Vector<String> args;
+
+    if (!function_->IsConstructor())
+    {
+        args.Push(ToString("%s* self", klass->GetNativeName().CString()));
+    }
+
+    if (parameters.Size())
+    {
+        for (unsigned int i = 0; i < parameters.Size(); i++)
+        {
+            JSBFunctionType* ptype = parameters.At(i);
+
+            // ignore "Context" parameters
+            if (ptype->type_->asClassType())
             {
             {
-                // read only vector arguments
-                if (ptype->isConst_)
+                JSBClassType* classType = ptype->type_->asClassType();
+                JSBClass* klass = classType->class_;
+                if (klass->GetName() == "Context")
                 {
                 {
-                    JSBVectorType* vtype = ptype->type_->asVectorType();
-                    sig.AppendWithFormat("%s __arg%i", vtype->ToString().CString(), cparam);
+                    continue;
                 }
                 }
+
+                args.Push(ToString("%s* %s", klass->GetNativeName().CString(), ptype->name_.CString()));
             }
             }
             else
             else
             {
             {
-                assert(0);
+                args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
             }
             }
 
 
-            sig += ", ";
-
         }
         }
     }
     }
 
 
-    if (sig.EndsWith(", "))
-        sig = sig.Substring(0, sig.Length() - 2);
+    if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
+    {
+        args.Push(ToString("%s* returnValue", function_->GetReturnClass()->GetNativeName().CString()));
+    }
+
+    sig.Join(args, ", ");
 
 
 }
 }
 
 
 void CSFunctionWriter::WriteNativeFunction(String& source)
 void CSFunctionWriter::WriteNativeFunction(String& source)
 {
 {
-    JSBClass* klass = function_->class_;
-
-    String sig;
+    JSBClass* klass = function_->GetClass();
+    JSBPackage* package = klass->GetPackage();
+    String fname = function_->IsConstructor() ? "Constructor" : function_->GetName();
 
 
-    GenNativeFunctionSignature(sig);
+    String returnType = "void";
 
 
-    JSBFunctionType* returnType = function_->returnType_;
-    String rTypeString = "void";
-    // if the marshalling local variable type is differnt
-    // for example SharedPtr ->Object *
-    String rMarshalTypeString;
+    bool simpleReturn = true;
 
 
-    if (returnType)
+    if (function_->IsConstructor())
     {
     {
-        if (returnType->type_->asStringType())
-        {
-            rTypeString = "const String&";
-        }
-        else if (returnType->type_->asPrimitiveType())
+        returnType = "RefCounted*";
+    }
+    else if (function_->GetReturnType())
+    {
+        if (function_->IsConstructor())
         {
         {
-            JSBPrimitiveType* prtype = returnType->type_->asPrimitiveType();
-            rTypeString = prtype->ToString();
+            returnType = ToString("%s*", klass->GetNativeName().CString());
         }
         }
-        else if (returnType->type_->asClassType())
+        else if (function_->GetReturnClass())
         {
         {
-            JSBClassType* klassType = returnType->type_->asClassType();
-
-            if (returnType->isTemplate_)
+            if (!function_->GetReturnClass()->IsNumberArray())
             {
             {
-                if (klassType->class_->IsObject())
-                    rTypeString = "const Object*";
-                else
-                    rTypeString = "const RefCounted*";
-
-                rMarshalTypeString.AppendWithFormat("SharedPtr<%s>", klassType->class_->GetNativeName().CString());
-            }
-            else if (klassType->class_->IsObject())
-            {
-                rTypeString = "const Object*";
-            }
-            else if (klassType->class_->IsNumberArray())
-            {
-                rTypeString = klassType->class_->GetName().CString();
-            }
-            else
-            {
-                rTypeString = "const RefCounted*";
+                returnType = ToString("const %s*", function_->GetReturnClass()->GetNativeName().CString());
             }
             }
         }
         }
-        else if (returnType->type_->asEnumType())
+        else if (function_->GetReturnType()->type_->asStringHashType())
         {
         {
-            JSBEnumType* enumType = returnType->type_->asEnumType();
-            rTypeString = enumType->enum_->GetName().CString();
+            returnType = "unsigned";
         }
         }
-        else if (returnType->type_->asVectorType())
+        else
         {
         {
-            JSBVectorType* vtype = returnType->type_->asVectorType();
-            rTypeString = "";
-            rTypeString.AppendWithFormat("%s", vtype->ToString().CString());
+            returnType = ToString("%s", CSTypeHelper::GetNativeTypeString(function_->GetReturnType()).CString());
         }
         }
-
     }
     }
 
 
+    String line;
+    String sig;
+    GenNativeFunctionSignature(sig);
 
 
+    line = ToString("%s csb_%s_%s_%s(%s)\n",
+                    returnType.CString(), package->GetName().CString(), klass->GetName().CString(),
+                    fname.CString(), sig.CString());
 
 
-    source.AppendWithFormat("%s csb_%s_%s(%s* self%s)\n{\n", rTypeString == "const String&" ? "const char*" : rTypeString.CString(), klass->GetName().CString(),
-                            function_->name_.CString(), klass->GetNativeName().CString(), sig.Length() ? (", " + sig).CString() : "");
+    source += IndentLine(line);
 
 
-    if (rTypeString != "void")
-    {
-        source.AppendWithFormat("%s retValue = ", rMarshalTypeString.Length()? rMarshalTypeString.CString() : rTypeString.CString());
-    }
+    source += IndentLine("{\n");
 
 
-    // call
+    Indent();
 
 
-    source.AppendWithFormat("self->%s(", function_->name_.CString());
+    bool returnValue = false;
 
 
-    Vector<JSBFunctionType*>& parameters = function_->GetParameters();
+    String returnStatement;
 
 
-    for (unsigned int i = 0; i < parameters.Size(); i++)
+    if (returnType == "const char*")
     {
     {
-        source.AppendWithFormat("__arg%i",  i);
-
-        if (i != parameters.Size() - 1)
+        returnValue = true;
+        source += IndentLine("static String returnValue;\n");
+        returnStatement = "returnValue = ";
+    }
+    else if (function_->GetReturnClass() && function_->GetReturnClass()->IsNumberArray())
+    {
+        returnStatement = "*returnValue = ";
+    }
+    else
+    {
+        if (returnType != "void")
         {
         {
-            source += ", ";
+            if (simpleReturn)
+                returnStatement = "return ";
         }
         }
     }
     }
 
 
-    source += ");\n";
+    String callSig;
+    GenNativeCallParameters(callSig);
+    if (!function_->isConstructor_)
+        line = ToString("%sself->%s(%s);\n", returnStatement.CString(), function_->GetName().CString(), callSig.CString());
+    else
+    {
+        if (klass->IsAbstract())
+        {
+            line = "return 0; // Abstract Class\n";
+        }
+        else if (klass->IsObject())
+        {
+            if (callSig.Length())
+                line = ToString("return new %s(AtomicSharp::GetContext(), %s);\n", klass->GetNativeName().CString(), callSig.CString());
+            else
+                line = ToString("return new %s(AtomicSharp::GetContext());\n", klass->GetNativeName().CString());
+        }
+        else
+        {
+            line = ToString("return new %s(%s);\n", klass->GetNativeName().CString(), callSig.CString());
+        }
+    }
 
 
+    source += IndentLine(line);
 
 
-    if (rTypeString != "void")
+    if (returnType == "const char*")
     {
     {
-        if (rTypeString == "const String&")
-            source.AppendWithFormat("\nreturn retValue.CString();\n");
-        else
-            source.AppendWithFormat("\nreturn retValue;\n");
+        source += IndentLine("return returnValue.CString();\n");
     }
     }
 
 
-    source.AppendWithFormat("}\n\n");
+    Dedent();
+
+    source += IndentLine("}\n");
+
+    source += "\n";
 
 
 }
 }
 
 
@@ -327,14 +318,7 @@ void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
 {
 {
     String source = "";
     String source = "";
 
 
-    if (function_->IsConstructor())
-    {
-        WriteNativeConstructor(source);
-    }
-    else
-    {
-        WriteNativeFunction(source);
-    }
+    WriteNativeFunction(source);
 
 
     sourceOut += source;
     sourceOut += source;
 
 
@@ -351,7 +335,7 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
     JSBClass* klass = function_->GetClass();
     JSBClass* klass = function_->GetClass();
     JSBPackage* package = klass->GetPackage();
     JSBPackage* package = klass->GetPackage();
 
 
-    String returnType = CSTypeHelper::GetNativeTypeString(function_->GetReturnType());
+    String returnType = CSTypeHelper::GetPInvokeTypeString(function_->GetReturnType());
 
 
     if (function_->IsConstructor())
     if (function_->IsConstructor())
         returnType = "IntPtr";
         returnType = "IntPtr";
@@ -371,6 +355,11 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
         {
         {
             JSBFunctionType* ptype = parameters.At(i);
             JSBFunctionType* ptype = parameters.At(i);
 
 
+            String name = ptype->name_;
+
+            if (name == "object")
+                name = "_object";
+
             // ignore "Context" parameters
             // ignore "Context" parameters
             if (ptype->type_->asClassType())
             if (ptype->type_->asClassType())
             {
             {
@@ -383,32 +372,29 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
 
 
                 if (klass->IsNumberArray())
                 if (klass->IsNumberArray())
                 {
                 {
-                    args.Push("ref " + klass->GetName() + " " + ptype->name_);
+                    args.Push("ref " + klass->GetName() + " " + name);
                 }
                 }
                 else
                 else
                 {
                 {
-                    args.Push("IntPtr " + ptype->name_);
+                    args.Push("IntPtr " + name);
                 }
                 }
             }
             }
             else
             else
             {
             {
-                args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
+                args.Push(CSTypeHelper::GetPInvokeTypeString(ptype) + " " + name);
             }
             }
 
 
         }
         }
     }
     }
 
 
-    if (function_->GetReturnType())
+    if (function_->GetReturnClass())
     {
     {
-        if (function_->GetReturnType()->type_->asClassType())
+        JSBClass* retClass = function_->GetReturnClass();
+        if (retClass->IsNumberArray())
         {
         {
-            JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
-            if (retClass->IsNumberArray())
-            {
-                args.Push("ref " + retClass->GetName() + " retValue");
-            }
-
+            args.Push("ref " + retClass->GetName() + " retValue");
         }
         }
+
     }
     }
 
 
     String pstring;
     String pstring;
@@ -450,6 +436,10 @@ void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
 
 
             sig += CSTypeHelper::GetManagedTypeString(ptype);
             sig += CSTypeHelper::GetManagedTypeString(ptype);
 
 
+            // hack for Drawable as this causes a compilation error (need to add default params)
+            if (sig.EndsWith("drawableFlags"))
+                sig += " = '\\0'";
+
             if (i + 1 != parameters.Size())
             if (i + 1 != parameters.Size())
                 sig += ", ";
                 sig += ", ";
         }
         }
@@ -484,7 +474,7 @@ void CSFunctionWriter::WriteManagedConstructor(String& source)
 
 
     Indent();
     Indent();
 
 
-    source += IndentLine("if (nativeInstance == IntPtr.Zero)");
+    source += IndentLine(ToString("if (typeof(%s) == this.GetType())\n", klass->GetName().CString()));
     source += IndentLine("{\n");
     source += IndentLine("{\n");
 
 
     Indent();
     Indent();
@@ -528,22 +518,27 @@ void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
                 }
                 }
             }
             }
 
 
+            String name = ptype->name_;
+
+            if (name == "object")
+                name = "_object";
+
             if (ptype->type_->asClassType())
             if (ptype->type_->asClassType())
             {
             {
                 JSBClass* pclass = ptype->type_->asClassType()->class_;
                 JSBClass* pclass = ptype->type_->asClassType()->class_;
                 if (pclass->IsNumberArray())
                 if (pclass->IsNumberArray())
                 {
                 {
-                    sig += "ref " + ptype->name_;
+                    sig += "ref " + name;
                 }
                 }
                 else
                 else
                 {
                 {
-                    sig += ptype->name_ + " == null ? IntPtr.Zero : " + ptype->name_+ ".nativeInstance";
+                    sig += name + " == null ? IntPtr.Zero : " + name + ".nativeInstance";
                 }
                 }
 
 
             }
             }
             else
             else
             {
             {
-                sig += ptype->name_;
+                sig += name;
             }
             }
 
 
             if (i + 1 != parameters.Size())
             if (i + 1 != parameters.Size())
@@ -554,7 +549,7 @@ void CSFunctionWriter::GenPInvokeCallParameters(String& sig)
     // data marshaller
     // data marshaller
     if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
     if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
     {
     {
-        if (function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
+        if (function_->GetReturnClass()->IsNumberArray())
         {
         {
             if (sig.Length())
             if (sig.Length())
                 sig += ", ";
                 sig += ", ";
@@ -593,9 +588,9 @@ void CSFunctionWriter::WriteManagedFunction(String& source)
             line += "return ";
             line += "return ";
         else
         else
         {
         {
-            if (function_->GetReturnType()->type_->asClassType())
+            if (function_->GetReturnClass())
             {
             {
-                if (!function_->GetReturnType()->type_->asClassType()->class_->IsNumberArray())
+                if (!function_->GetReturnClass()->IsNumberArray())
                     line += "IntPtr retNativeInstance = ";
                     line += "IntPtr retNativeInstance = ";
             }
             }
         }
         }
@@ -620,7 +615,7 @@ void CSFunctionWriter::WriteManagedFunction(String& source)
     {
     {
         if (function_->GetReturnType()->type_->asClassType())
         if (function_->GetReturnType()->type_->asClassType())
         {
         {
-            JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
+            JSBClass* retClass = function_->GetReturnClass();
             JSBClass* klass = function_->GetClass();
             JSBClass* klass = function_->GetClass();
 
 
             if (retClass->IsNumberArray())
             if (retClass->IsNumberArray())
@@ -663,9 +658,9 @@ void CSFunctionWriter::GenerateManagedSource(String& sourceOut)
     // data marshaller
     // data marshaller
     if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
     if (function_->GetReturnType() && !CSTypeHelper::IsSimpleReturn(function_->GetReturnType()))
     {
     {
-        if (function_->GetReturnType()->type_->asClassType())
+        if (function_->GetReturnClass())
         {
         {
-            JSBClass* retClass = function_->GetReturnType()->type_->asClassType()->class_;
+            JSBClass* retClass = function_->GetReturnClass();
             if (retClass->IsNumberArray())
             if (retClass->IsNumberArray())
             {
             {
                 JSBClass* klass = function_->GetClass();
                 JSBClass* klass = function_->GetClass();

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

@@ -34,6 +34,7 @@ public:
 
 
 private:
 private:
 
 
+    void GenNativeCallParameters(String& sig);
     void GenNativeFunctionSignature(String& sig);
     void GenNativeFunctionSignature(String& sig);
 
 
     void WriteNativeFunction(String& source);
     void WriteNativeFunction(String& source);

+ 60 - 0
Source/ToolCore/JSBind/CSharp/CSModuleWriter.cpp

@@ -275,11 +275,70 @@ void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
 
 
     }
     }
 
 
+    source += "\n";
 
 
     Dedent();
     Dedent();
 
 
 }
 }
 
 
+void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
+{
+    Indent();
+
+    String source;
+    String line = ToString("public static partial class %sModule\n", module_->GetName().CString());
+
+    source += IndentLine(line);
+
+    source += IndentLine("{\n");
+
+    Indent();
+
+    source += IndentLine("public static void Initialize()\n");
+
+    source += IndentLine("{\n");
+
+    Indent();
+
+    Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
+
+    for (unsigned i = 0; i < classes.Size(); i++)
+    {
+        JSBClass* klass = classes.At(i);
+        JSBPackage* package = module_->GetPackage();
+
+        if (klass->IsNumberArray() || klass->IsAbstract())
+            continue;
+
+
+        line = ToString("NativeCore.nativeClassIDToManagedConstructor [ %s.csb_%s_%s_GetClassID ()] = (IntPtr x) => {\n",
+                        klass->GetName().CString(), package->GetName().CString(), klass->GetName().CString());
+
+        source += IndentLine(line);
+
+        Indent();
+
+        source += IndentLine(ToString("return new %s (x);\n", klass->GetName().CString()));
+
+        Dedent();
+
+        source += IndentLine("};\n");
+
+    }
+
+    Dedent();
+
+    source += IndentLine("}\n");
+
+    Dedent();
+
+    source += IndentLine("}\n");
+
+    sourceOut += source;
+
+    Dedent();
+}
+
 void CSModuleWriter::GenerateManagedSource()
 void CSModuleWriter::GenerateManagedSource()
 {
 {
     String source = "// Autogenerated";
     String source = "// Autogenerated";
@@ -294,6 +353,7 @@ void CSModuleWriter::GenerateManagedSource()
     source += "{\n";
     source += "{\n";
 
 
     GenerateManagedEnumsAndConstants(source);
     GenerateManagedEnumsAndConstants(source);
+    GenerateManagedModuleClass(source);
     GenerateManagedClasses(source);
     GenerateManagedClasses(source);
 
 
     source += "}\n";
     source += "}\n";

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

@@ -35,6 +35,7 @@ private:
 
 
     String GetManagedPrimitiveType(JSBPrimitiveType* ptype);
     String GetManagedPrimitiveType(JSBPrimitiveType* ptype);
 
 
+    void GenerateManagedModuleClass(String& sourceOut);
     void GenerateManagedClasses(String& source);
     void GenerateManagedClasses(String& source);
     void GenerateManagedEnumsAndConstants(String& source);
     void GenerateManagedEnumsAndConstants(String& source);
 
 

+ 58 - 3
Source/ToolCore/JSBind/CSharp/CSTypeHelper.cpp

@@ -66,7 +66,17 @@ String CSTypeHelper::GetManagedTypeString(JSBFunctionType* ftype, bool addName)
     if (ftype->name_.Length())
     if (ftype->name_.Length())
     {
     {
         if (addName)
         if (addName)
-            parameter += " " + ftype->name_;
+        {
+            if (ftype->name_ == "object")
+            {
+                parameter += " _object";
+            }
+            else
+            {
+                parameter += " " + ftype->name_;
+            }
+
+        }
 
 
         /*
         /*
         if (ftype->initializer_.Length())
         if (ftype->initializer_.Length())
@@ -82,6 +92,51 @@ String CSTypeHelper::GetNativeTypeString(JSBType* type)
 {
 {
     String value;
     String value;
 
 
+    if (type->asClassType())
+    {
+        JSBClassType* classType = type->asClassType();
+        if (classType->class_->IsNumberArray())
+            value = ToString("%s*", classType->class_->GetNativeName().CString());
+    }
+    else if (type->asStringType())
+    {
+        value = "const char*";
+    }
+    else if (type->asStringHashType())
+    {
+        value = "const char*";
+    }
+    else if (type->asEnumType())
+    {
+        value = type->asEnumType()->enum_->GetName();
+    }
+    else if (type->asPrimitiveType())
+    {
+        value = type->asPrimitiveType()->ToString();
+    }
+    else if (type->asVectorType())
+    {
+        assert(0);
+    }
+
+    return value;
+}
+
+String CSTypeHelper::GetNativeTypeString(JSBFunctionType* ftype)
+{
+    if (!ftype)
+        return "void";
+
+    String value = GetNativeTypeString(ftype->type_);
+
+    return value;
+
+}
+
+String CSTypeHelper::GetPInvokeTypeString(JSBType* type)
+{
+    String value;
+
     if (type->asClassType())
     if (type->asClassType())
     {
     {
         JSBClassType* classType = type->asClassType();
         JSBClassType* classType = type->asClassType();
@@ -112,12 +167,12 @@ String CSTypeHelper::GetNativeTypeString(JSBType* type)
     return value;
     return value;
 }
 }
 
 
-String CSTypeHelper::GetNativeTypeString(JSBFunctionType* ftype)
+String CSTypeHelper::GetPInvokeTypeString(JSBFunctionType* ftype)
 {
 {
     if (!ftype)
     if (!ftype)
         return "void";
         return "void";
 
 
-    String value = GetNativeTypeString(ftype->type_);
+    String value = GetPInvokeTypeString(ftype->type_);
 
 
     return value;
     return value;
 
 

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

@@ -30,6 +30,9 @@ public:
     static String GetNativeTypeString(JSBType* type);
     static String GetNativeTypeString(JSBType* type);
     static String GetNativeTypeString(JSBFunctionType* ftype);
     static String GetNativeTypeString(JSBFunctionType* ftype);
 
 
+    static String GetPInvokeTypeString(JSBType* type);
+    static String GetPInvokeTypeString(JSBFunctionType* ftype);
+
     static String GetManagedTypeString(JSBType* type);
     static String GetManagedTypeString(JSBType* type);
     static String GetManagedTypeString(JSBFunctionType* ftype, bool addName = true);
     static String GetManagedTypeString(JSBFunctionType* ftype, bool addName = true);
 
 

+ 13 - 0
Source/ToolCore/JSBind/JSBFunction.cpp

@@ -54,4 +54,17 @@ void JSBFunction::Process()
         class_->AddPropertyFunction(this);
         class_->AddPropertyFunction(this);
 
 
 }
 }
+
+JSBClass* JSBFunction::GetReturnClass()
+{
+    if (!returnType_)
+        return 0;
+
+    if (!returnType_->type_->asClassType())
+        return 0;
+
+    return returnType_->type_->asClassType()->class_;
+
+}
+
 }
 }

+ 4 - 0
Source/ToolCore/JSBind/JSBFunction.h

@@ -106,6 +106,10 @@ public:
     JSBClass* GetClass() { return class_; }
     JSBClass* GetClass() { return class_; }
     const String& GetPropertyName() { return propertyName_; }
     const String& GetPropertyName() { return propertyName_; }
     JSBFunctionType* GetReturnType() { return returnType_; }
     JSBFunctionType* GetReturnType() { return returnType_; }
+
+    /// Get class return type or null
+    JSBClass* GetReturnClass();
+
     Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
     Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
 
 
     const String& GetDocString() { return docString_; }
     const String& GetDocString() { return docString_; }

+ 2 - 0
Source/ToolCore/JSBind/JSBType.h

@@ -83,6 +83,8 @@ public:
         case Short:
         case Short:
             return "short";
             return "short";
         case Int:
         case Int:
+            if (isUnsigned_)
+                return "unsigned";
             return "int";
             return "int";
         case Long:
         case Long:
             return "long";
             return "long";