Lib.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. class Lib {
  24. /**
  25. Load and return a Cpp primitive from a DLL library.
  26. **/
  27. public static function load( lib : String, prim : String, nargs : Int ) : Dynamic {
  28. #if (iphone || emscripten)
  29. return loadLazy(lib,prim,nargs);
  30. #else
  31. return untyped __global__.__loadprim(lib,prim,nargs);
  32. #end
  33. }
  34. /**
  35. Unloaded all dynamic libraries in reverse order of loading.
  36. Returns the number of libraries unloaded.
  37. **/
  38. public static function unloadAllLibraries() : Int {
  39. return untyped __global__.__hxcpp_unload_all_libraries();
  40. }
  41. @:analyzer(no_simplification)
  42. public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
  43. var factory:Callable< ConstCharStar -> Object > =
  44. untyped __global__.__hxcpp_cast_get_proc_address(lib, prim + "__prime", quietFail);
  45. if (factory!=null)
  46. {
  47. var func:Dynamic = factory.call(signature);
  48. if (func==null && !quietFail)
  49. throw '$prim does not have signature $signature';
  50. return func;
  51. }
  52. return null;
  53. }
  54. /**
  55. Tries to load, and always returns a valid function, but the function may throw
  56. if called.
  57. **/
  58. public static function loadLazy(lib,prim,nargs) : Dynamic {
  59. try {
  60. return untyped __global__.__loadprim(lib,prim,nargs);
  61. } catch( e : Dynamic ) {
  62. switch(nargs) {
  63. case 0 : return function() { throw e; };
  64. case 2 : return function(_1,_2) { throw e; };
  65. case 3 : return function(_1,_2,_3) { throw e; };
  66. case 4 : return function(_1,_2,_3,_4) { throw e; };
  67. case 5 : return function(_1,_2,_3,_4,_5) { throw e; };
  68. default : return function(_1) { throw e; };
  69. }
  70. }
  71. return null;
  72. }
  73. public static function rethrow(inExp:Dynamic) { throw inExp; }
  74. public static function stringReference(inBytes:haxe.io.Bytes) : String
  75. {
  76. var result:String = "";
  77. untyped __global__.__hxcpp_string_of_bytes(inBytes.b, result, 0, 0, true);
  78. return result;
  79. }
  80. public static function pushDllSearchPath(inPath:String) : Void
  81. untyped __global__.__hxcpp_push_dll_path(inPath);
  82. public static function getDllExtension() : String
  83. return untyped __global__.__hxcpp_get_dll_extension();
  84. public static function getBinDirectory() : String
  85. return untyped __global__.__hxcpp_get_bin_dir();
  86. /**
  87. Returns bytes referencing the content of a string.
  88. Use with extreme caution - changing constant strings will crash.
  89. Changing one string can cause others to change unexpectedly.
  90. Only really safe if you are using it read-only or if it comes from stringReference above
  91. **/
  92. public inline static function bytesReference( s : String ) : haxe.io.Bytes {
  93. var bytes = new haxe.io.BytesData();
  94. untyped bytes.__unsafeStringReference(s);
  95. return haxe.io.Bytes.ofData(bytes);
  96. }
  97. /**
  98. Print the specified value on the default output.
  99. **/
  100. public static function print( v : Dynamic ) : Void {
  101. untyped __global__.__hxcpp_print(v);
  102. }
  103. /**
  104. This function is used to make porting from neko to cpp easy.
  105. It does not need to do anything because the c-code can work with any Dynamic
  106. **/
  107. public static function haxeToNeko( v : Dynamic ) : Dynamic {
  108. return v;
  109. }
  110. /**
  111. This function is used to make porting from neko to cpp easy.
  112. It does not need to do anything because the c-code can work with any Dynamic
  113. **/
  114. public static function nekoToHaxe( v : Dynamic ) : Dynamic {
  115. return v;
  116. }
  117. /**
  118. Print the specified value on the default output followed by a newline character.
  119. **/
  120. public static function println( v : Dynamic ) : Void {
  121. untyped __global__.__hxcpp_println(v);
  122. }
  123. public static function setFloatFormat(inFormat:String):Void
  124. {
  125. untyped __global__.__hxcpp_set_float_format(inFormat);
  126. }
  127. }