2
0

Lib.hx 4.8 KB

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