Prime.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C)2005-2018 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. if (code=="c" && !forCpp)
  48. throw "const char * type only supported in cpp mode";
  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" : return "cpp.ConstCharStar";
  59. default:
  60. throw "Unknown signature type :" + code;
  61. }
  62. }
  63. #end
  64. public static function nekoInit(inModuleName:String) : Bool
  65. {
  66. #if neko
  67. var init = neko.Lib.load(inModuleName, "neko_init", 5);
  68. if (init != null)
  69. {
  70. init( function(s) return new String(s),
  71. function(len:Int) { var r = []; if (len > 0) r[len - 1] = null; return r; },
  72. null,
  73. true,
  74. false);
  75. return true;
  76. }
  77. #end
  78. return false;
  79. }
  80. public static macro function load(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
  81. {
  82. var parts = inSig.split("");
  83. if (parts.length<1)
  84. throw "Invalid function signature " + inSig;
  85. var argCount = parts.length-1;
  86. var cppiaMode = Context.defined("cppia");
  87. var cppMode = Context.defined("cpp") && !cppiaMode;
  88. var typeString = parts.length==1 ? "Void" : codeToType(parts.shift(),cppMode);
  89. for(p in parts)
  90. typeString += "->" + codeToType(p,cppMode);
  91. if (cppMode)
  92. {
  93. typeString = "cpp.Callable<" + typeString + ">";
  94. var expr = 'new $typeString(cpp.Prime._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
  95. return Context.parse( expr, Context.currentPos() );
  96. }
  97. else
  98. {
  99. if (argCount>5)
  100. argCount = -1;
  101. var lazy = inAllowFail ? "loadLazy" : "load";
  102. var lib = cppiaMode ? "cpp" : "neko";
  103. var expr = 'new cpp.Callable<$typeString>($lib.Lib.$lazy("$inModule","$inName",$argCount))';
  104. return Context.parse( expr, Context.currentPos() );
  105. }
  106. }
  107. }