Prime.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cpp;
  23. #if macro
  24. import haxe.macro.Context;
  25. import haxe.macro.Type;
  26. import haxe.macro.Expr;
  27. #end
  28. @:noPackageRestrict
  29. class Prime {
  30. #if (!macro && cpp)
  31. @:analyzer(no_simplification)
  32. public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
  33. var factory:Callable< ConstCharStar -> Object > =
  34. untyped __global__.__hxcpp_cast_get_proc_address(lib, prim + "__prime", quietFail);
  35. if (factory!=null)
  36. {
  37. var func:Dynamic = factory.call(signature);
  38. if (func==null && !quietFail)
  39. throw '$prim does not have signature $signature';
  40. return func;
  41. }
  42. return null;
  43. }
  44. #end
  45. #if (macro)
  46. static function codeToType(code:String,forCpp:Bool) : String
  47. {
  48. var isCpp = Context.defined("cpp");
  49. switch(code)
  50. {
  51. case "b" : return "Bool";
  52. case "i" : return "Int";
  53. case "d" : return "Float";
  54. case "s" : return "String";
  55. case "f" : return forCpp ? "cpp.Float32" : "Float";
  56. case "o" : return forCpp ? "cpp.Object" : "Dynamic";
  57. case "v" : return forCpp ? "cpp.Void" : "Dynamic";
  58. case "c" :
  59. if (forCpp)
  60. return "cpp.ConstCharStar";
  61. throw "const char * type only supported in cpp mode";
  62. default:
  63. throw "Unknown signature type :" + code;
  64. }
  65. }
  66. #end
  67. public static function nekoInit(inModuleName:String) : Bool
  68. {
  69. #if neko
  70. var init = neko.Lib.load(inModuleName, "neko_init", 5);
  71. if (init != null)
  72. {
  73. init( function(s) return new String(s),
  74. function(len:Int) { var r = []; if (len > 0) r[len - 1] = null; return r; },
  75. null,
  76. true,
  77. false);
  78. return true;
  79. }
  80. #end
  81. return false;
  82. }
  83. public static macro function load(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
  84. {
  85. var parts = inSig.split("");
  86. if (parts.length<1)
  87. throw "Invalid function signature " + inSig;
  88. var cppMode = Context.defined("cpp");
  89. var typeString = parts.length==1 ? "Void" : codeToType(parts.shift(),cppMode);
  90. for(p in parts)
  91. typeString += "->" + codeToType(p,cppMode);
  92. if (cppMode)
  93. {
  94. typeString = "cpp.Callable<" + typeString + ">";
  95. var expr = 'new $typeString(cpp.Prime._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
  96. return Context.parse( expr, Context.currentPos() );
  97. }
  98. else
  99. {
  100. var len = parts.length;
  101. if (len>5)
  102. len = -1;
  103. var lazy = inAllowFail ? "loadLazy" : "load";
  104. var expr = 'new cpp.Callable<$typeString>(neko.Lib.$lazy("$inModule","$inName",$len))';
  105. return Context.parse( expr, Context.currentPos() );
  106. }
  107. }
  108. }