Lib.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C)2005-2012 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. using cpp.NativeString;
  29. using cpp.RawConstPointer;
  30. using cpp.Char;
  31. class Lib {
  32. #if !macro
  33. /**
  34. Load and return a Cpp primitive from a DLL library.
  35. **/
  36. public static function load( lib : String, prim : String, nargs : Int ) : Dynamic {
  37. #if (iphone || emscripten)
  38. return loadLazy(lib,prim,nargs);
  39. #else
  40. return untyped __global__.__loadprim(lib,prim,nargs);
  41. #end
  42. }
  43. @:analyzer(no_simplification)
  44. public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
  45. var factory:Function< RawConstPointer<Char> -> RawPointer<Object> > =
  46. untyped __global__.__hxcpp_cast_get_proc_address(lib, prim + "__prime", quietFail);
  47. if (factory!=null)
  48. {
  49. var func:Dynamic = factory.call(signature.raw());
  50. if (func==null && !quietFail)
  51. throw '$prim does not have signature $signature';
  52. return func;
  53. }
  54. return null;
  55. }
  56. /**
  57. Tries to load, and always returns a valid function, but the function may throw
  58. if called.
  59. **/
  60. public static function loadLazy(lib,prim,nargs) : Dynamic {
  61. try {
  62. return untyped __global__.__loadprim(lib,prim,nargs);
  63. } catch( e : Dynamic ) {
  64. switch(nargs) {
  65. case 0 : return function() { throw e; };
  66. case 2 : return function(_1,_2) { throw e; };
  67. case 3 : return function(_1,_2,_3) { throw e; };
  68. case 4 : return function(_1,_2,_3,_4) { throw e; };
  69. case 5 : return function(_1,_2,_3,_4,_5) { throw e; };
  70. default : return function(_1) { throw e; };
  71. }
  72. }
  73. return null;
  74. }
  75. public static function rethrow(inExp:Dynamic) { throw inExp; }
  76. public static function stringReference(inExp:Dynamic) { throw inExp; }
  77. /**
  78. Print the specified value on the default output.
  79. **/
  80. public static function print( v : Dynamic ) : Void {
  81. untyped __global__.__hxcpp_print(v);
  82. }
  83. /**
  84. This function is used to make porting from neko to cpp easy.
  85. It does not need to do anything because the c-code can work with any Dynamic
  86. **/
  87. public static function haxeToNeko( v : Dynamic ) : Dynamic {
  88. return v;
  89. }
  90. /**
  91. This function is used to make porting from neko to cpp easy.
  92. It does not need to do anything because the c-code can work with any Dynamic
  93. **/
  94. public static function nekoToHaxe( v : Dynamic ) : Dynamic {
  95. return v;
  96. }
  97. /**
  98. Print the specified value on the default output followed by a newline character.
  99. **/
  100. public static function println( v : Dynamic ) : Void {
  101. untyped __global__.__hxcpp_println(v);
  102. }
  103. #else
  104. static function codeToType(code:String) : String
  105. {
  106. switch(code)
  107. {
  108. case "b" : return "Bool";
  109. case "i" : return "Int";
  110. case "d" : return "Float";
  111. case "f" : return "cpp.Float32";
  112. case "s" : return "String";
  113. case "o" : return "cpp.Object";
  114. case "v" : return "cpp.Void";
  115. case "c" : return "cpp.RawConstPtr<cpp.Char> ";
  116. default:
  117. throw "Unknown signature type :" + code;
  118. }
  119. }
  120. #end
  121. public static macro function loadPrime(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
  122. {
  123. var parts = inSig.split("");
  124. if (parts.length<1)
  125. throw "Invalid function signature " + inSig;
  126. var typeString = parts.length==1 ? "Void" : codeToType(parts.shift());
  127. for(p in parts)
  128. typeString += "->" + codeToType(p);
  129. typeString = "cpp.Function<" + typeString + ">";
  130. var expr = 'new $typeString(cpp.Lib._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
  131. return Context.parse( expr, Context.currentPos() );
  132. }
  133. }