Procházet zdrojové kódy

[cpp] Move cpp.Prime.load into its own file, and allow for neko compatibility

hughsando před 10 roky
rodič
revize
7f86f1ae47
3 změnil soubory, kde provedl 140 přidání a 47 odebrání
  1. 17 0
      std/cpp/Callable.hx
  2. 0 47
      std/cpp/Lib.hx
  3. 123 0
      std/cpp/Prime.hx

+ 17 - 0
std/cpp/Callable.hx

@@ -1,5 +1,22 @@
 package cpp;
 
+#if cpp
+
 typedef Callable<T> = Function<T, cpp.abi.Abi >
 
+#else
+
+@:noPackageRestrict
+abstract Callable<T>(T)
+{
+   public var call(get,never):T;
+
+   inline public function new(inValue:T) this = inValue;
+
+   inline function get_call():T return this;
+}
+
+
+#end
+
 

+ 0 - 47
std/cpp/Lib.hx

@@ -21,24 +21,9 @@
  */
 package cpp;
 
-#if macro
-import haxe.macro.Context;
-import haxe.macro.Type;
-import haxe.macro.Expr;
-#else
 
-using cpp.NativeString;
-using cpp.RawConstPointer;
-using cpp.Char;
-
-#end
-
-#if macro
-@:noPackageRestrict
-#end
 class Lib {
 
-   #if !macro
 	/**
 		Load and return a Cpp primitive from a DLL library.
 	**/
@@ -144,41 +129,9 @@ class Lib {
 		untyped __global__.__hxcpp_println(v);
 	}
 
-   #else
-   static function codeToType(code:String) : String
-   {
-      switch(code)
-      {
-         case "b" : return "Bool";
-         case "i" : return "Int";
-         case "d" : return "Float";
-         case "f" : return "cpp.Float32";
-         case "s" : return "String";
-         case "o" : return "cpp.Object";
-         case "v" : return "cpp.Void";
-         case "c" : return "cpp.ConstCharStar";
-         default:
-            throw "Unknown signature type :" + code;
-      }
-   }
-   #end
-
    public static function setFloatFormat(inFormat:String):Void
    {
       untyped __global__.__hxcpp_set_float_format(inFormat);
    }
 
-   public static macro function loadPrime(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
-   {
-      var parts = inSig.split("");
-      if (parts.length<1)
-         throw "Invalid function signature " + inSig;
-      var typeString = parts.length==1 ? "Void" : codeToType(parts.shift());
-      for(p in parts)
-         typeString += "->" + codeToType(p);
-      typeString = "cpp.Callable<" + typeString + ">";
-      var expr = 'new $typeString(cpp.Lib._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
-      return Context.parse( expr, Context.currentPos() );
-   }
-
 }

+ 123 - 0
std/cpp/Prime.hx

@@ -0,0 +1,123 @@
+/*
+ * Copyright (C)2005-2012 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package cpp;
+
+#if macro
+import haxe.macro.Context;
+import haxe.macro.Type;
+import haxe.macro.Expr;
+#end
+
+@:noPackageRestrict
+class Prime {
+
+   #if (!macro && cpp)
+
+   @:analyzer(no_simplification)
+	public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
+		var factory:Callable< ConstCharStar -> Object > =
+               untyped __global__.__hxcpp_cast_get_proc_address(lib, prim + "__prime", quietFail);
+      if (factory!=null)
+      {
+         var func:Dynamic = factory.call(signature);
+         if (func==null && !quietFail)
+            throw '$prim does not have signature $signature';
+         return func;
+      }
+      return null;
+	}
+   #end
+
+   #if (macro)
+   static function codeToType(code:String,forCpp:Bool) : String
+   {
+      var isCpp = Context.defined("cpp");
+
+      switch(code)
+      {
+         case "b" : return "Bool";
+         case "i" : return "Int";
+         case "d" : return "Float";
+         case "s" : return "String";
+         case "f" : return forCpp ? "cpp.Float32" : "Float";
+         case "o" : return forCpp ? "cpp.Object" : "Dynamic";
+         case "v" : return forCpp ? "cpp.Void" : "Dynamic";
+         case "c" :
+             if (forCpp)
+                return "cpp.ConstCharStar";
+             throw "const char * type only supported in cpp mode";
+         default:
+            throw "Unknown signature type :" + code;
+      }
+   }
+   #end
+
+   public static function nekoInit(inModuleName:String) : Bool
+   {
+      #if neko
+      var init = neko.Lib.load(inModuleName, "neko_init", 5);
+
+      if (init != null) 
+      {
+         init( function(s) return new String(s),
+               function(len:Int) { var r = []; if (len > 0) r[len - 1] = null; return r; },
+               null,
+               true,
+               false);
+         return true;
+
+      }
+      #end
+      return false;
+   }
+
+
+   public static macro function load(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
+   {
+      var parts = inSig.split("");
+      if (parts.length<1)
+         throw "Invalid function signature " + inSig;
+
+      var cppMode = Context.defined("cpp");
+
+      var typeString = parts.length==1 ? codeToType("v",cppMode) : codeToType(parts.shift(),cppMode);
+      for(p in parts)
+         typeString += "->" + codeToType(p,cppMode);
+
+      if (cppMode)
+      {
+         typeString = "cpp.Callable<" + typeString + ">";
+         var expr = 'new $typeString(cpp.Prime._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
+         return Context.parse( expr, Context.currentPos() );
+      }
+      else
+      {
+         var len = parts.length;
+         if (len>5)
+            len = -1;
+         var lazy = inAllowFail ? "loadLazy" : "load";
+         var expr = 'new cpp.Callable<$typeString>(neko.Lib.$lazy("$inModule","$inName",$len))';
+         return Context.parse( expr, Context.currentPos() );
+      }
+   }
+
+}