Prime.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C)2005-2017 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. public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
  32. var factory:Callable< ConstCharStar -> Object > =
  33. untyped __global__.__hxcpp_cast_get_proc_address(lib, prim + "__prime", quietFail);
  34. if (factory!=null)
  35. {
  36. var func:Dynamic = factory.call(signature);
  37. if (func==null && !quietFail)
  38. throw '$prim does not have signature $signature';
  39. return func;
  40. }
  41. return null;
  42. }
  43. #end
  44. #if (macro)
  45. static function codeToType(code:String,forCpp:Bool) : String
  46. {
  47. var isCpp = Context.defined("cpp");
  48. switch(code)
  49. {
  50. case "b" : return "Bool";
  51. case "i" : return "Int";
  52. case "d" : return "Float";
  53. case "s" : return "String";
  54. case "f" : return forCpp ? "cpp.Float32" : "Float";
  55. case "o" : return forCpp ? "cpp.Object" : "Dynamic";
  56. case "v" : return forCpp ? "cpp.Void" : "Dynamic";
  57. case "c" :
  58. if (forCpp)
  59. return "cpp.ConstCharStar";
  60. throw "const char * type only supported in cpp mode";
  61. default:
  62. throw "Unknown signature type :" + code;
  63. }
  64. }
  65. #end
  66. public static function nekoInit(inModuleName:String) : Bool
  67. {
  68. #if neko
  69. var init = neko.Lib.load(inModuleName, "neko_init", 5);
  70. if (init != null)
  71. {
  72. init( function(s) return new String(s),
  73. function(len:Int) { var r = []; if (len > 0) r[len - 1] = null; return r; },
  74. null,
  75. true,
  76. false);
  77. return true;
  78. }
  79. #end
  80. return false;
  81. }
  82. public static macro function load(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
  83. {
  84. var parts = inSig.split("");
  85. if (parts.length<1)
  86. throw "Invalid function signature " + inSig;
  87. var argCount = parts.length-1;
  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. if (argCount>5)
  101. argCount = -1;
  102. var lazy = inAllowFail ? "loadLazy" : "load";
  103. var expr = 'new cpp.Callable<$typeString>(neko.Lib.$lazy("$inModule","$inName",$argCount))';
  104. return Context.parse( expr, Context.currentPos() );
  105. }
  106. }
  107. }