Prime.hx 3.6 KB

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