1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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() );
}
}
}
|