2
0

HostClasses.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.cppia;
  23. import haxe.macro.Compiler;
  24. import haxe.macro.Context;
  25. import haxe.macro.Type;
  26. #if !macro
  27. #if cppia
  28. @:build(cpp.cppia.HostClasses.exclude())
  29. #end
  30. class HostClasses {}
  31. #else
  32. import Sys;
  33. import haxe.Constraints;
  34. @:noPackageRestrict
  35. class HostClasses {
  36. static var classes = [
  37. "cpp.Lib",
  38. "cpp.NativeArray",
  39. "cpp.NativeString",
  40. "cpp.vm.Debugger",
  41. "sys.thread.Deque",
  42. "sys.thread.Lock",
  43. "sys.thread.Mutex",
  44. "sys.thread.Thread",
  45. "sys.thread.Tls",
  46. "sys.thread.Semaphore",
  47. "sys.thread.Condition",
  48. "cpp.vm.ExecutionTrace",
  49. "cpp.vm.Gc",
  50. "cpp.vm.Profiler",
  51. "cpp.vm.Unsafe",
  52. "cpp.vm.WeakRef",
  53. "cpp.Object",
  54. "cpp.Int64",
  55. "cpp.Int64Map",
  56. "cpp.Finalizable",
  57. "Std",
  58. "StringBuf",
  59. "sys.FileSystem",
  60. "sys.io.File",
  61. "sys.io.FileInput",
  62. "sys.io.Process",
  63. "sys.net.UdpSocket",
  64. "sys.net.Socket",
  65. "sys.ssl.Certificate",
  66. "sys.ssl.Digest",
  67. "sys.ssl.Key",
  68. "sys.ssl.Socket",
  69. "Enum",
  70. "EnumValue",
  71. // "Sys",
  72. "Type",
  73. "Xml",
  74. "Date",
  75. "Lambda",
  76. "DateTools",
  77. "List",
  78. "Math",
  79. "Reflect",
  80. "StringBuf",
  81. "StringTools",
  82. "haxe.ds.IntMap",
  83. "haxe.ds.ObjectMap",
  84. "haxe.ds.StringMap",
  85. "haxe.ds.BalancedTree",
  86. "haxe.NativeStackTrace",
  87. "haxe.Serializer",
  88. "haxe.Unserializer",
  89. "haxe.Resource",
  90. "haxe.Template",
  91. "haxe.Utf8",
  92. "haxe.Log",
  93. "haxe.zip.Compress",
  94. "haxe.zip.Uncompress",
  95. "haxe.crypto.BaseCode",
  96. "haxe.crypto.Sha256",
  97. "haxe.crypto.Hmac",
  98. "haxe.crypto.Crc32",
  99. "haxe.crypto.Base64",
  100. "haxe.crypto.Adler32",
  101. "haxe.crypto.Md5",
  102. "haxe.crypto.Sha1",
  103. "haxe.io.BufferInput",
  104. "haxe.io.Bytes",
  105. "haxe.io.BytesBuffer",
  106. "haxe.io.BytesData",
  107. "haxe.io.BytesInput",
  108. "haxe.io.BytesOutput",
  109. "haxe.io.Eof",
  110. "haxe.io.Error",
  111. "haxe.io.FPHelper",
  112. "haxe.io.Input",
  113. "haxe.io.Output",
  114. "haxe.io.Path",
  115. "haxe.io.StringInput",
  116. "haxe.xml.Parser",
  117. "haxe.Json",
  118. "haxe.NativeStackTrace",
  119. "haxe.Resource",
  120. "haxe.Utf8",
  121. "haxe.Int64",
  122. "haxe.Int32",
  123. "haxe.Serializer",
  124. "haxe.Unserializer",
  125. "haxe.ds.ArraySort",
  126. "haxe.ds.GenericStack",
  127. "haxe.ds.ObjectMap",
  128. "haxe.ds.Vector",
  129. "haxe.ds.BalancedTree",
  130. "haxe.ds.HashMap",
  131. "haxe.ds.Option",
  132. "haxe.ds.WeakMap",
  133. "haxe.ds.EnumValueMap",
  134. "haxe.ds.IntMap",
  135. "haxe.ds.StringMap",
  136. "haxe.iterators.MapKeyValueIterator",
  137. "StdTypes",
  138. "Array",
  139. "Class",
  140. "Date",
  141. "EReg",
  142. "Enum",
  143. "EnumValue",
  144. // "IntIterator",
  145. "List",
  146. "Map",
  147. "String",
  148. "haxe.atomic.AtomicInt"
  149. ];
  150. static function parseClassInfo(externs:Map<String, Bool>, filename:String) {
  151. if (sys.FileSystem.exists(filename)) {
  152. var file = sys.io.File.read(filename);
  153. try {
  154. while (true) {
  155. var line = file.readLine();
  156. var parts = line.split(" ");
  157. if (parts[0] == "class" || parts[0] == "interface" || parts[0] == "enum")
  158. externs.set(parts[1], true);
  159. }
  160. } catch (e:Dynamic) {}
  161. if (file != null)
  162. file.close();
  163. }
  164. }
  165. static function onGenerateCppia(types:Array<Type>):Void {
  166. var externs = new Map<String, Bool>();
  167. externs.set("Sys", true);
  168. externs.set("haxe.IMap", true);
  169. externs.set("haxe.crypto.HashMethod", true);
  170. externs.set("haxe._Int64.Int64_Impl_", true);
  171. externs.set("haxe._Int64.___Int64", true);
  172. externs.set("haxe._Int32.Int32_Impl_", true);
  173. externs.set("haxe._Int32.___Int32", true);
  174. // Hidded in implementation classes
  175. // externs.set("sys.db.RecordType",true);
  176. externs.set("sys.net._Socket.SocketInput", true);
  177. externs.set("sys.net._Socket.SocketOutput", true);
  178. externs.set("sys.ssl._Socket.SocketInput", true);
  179. externs.set("sys.ssl._Socket.SocketOutput", true);
  180. externs.set("sys.thread._Thread.HaxeThread",true);
  181. externs.set("haxe.ds.TreeNode", true);
  182. externs.set("haxe.xml.XmlParserException", true);
  183. for (e in classes)
  184. externs.set(e, true);
  185. var define = Context.defined("dll_import") ? Context.definedValue("dll_import") : "1";
  186. if (define != "1")
  187. parseClassInfo(externs, define);
  188. else {
  189. var tried = new Map<String, Bool>();
  190. for (path in Context.getClassPath())
  191. if (!tried.exists(path)) {
  192. tried.set(path, true);
  193. parseClassInfo(externs, path + "/export_classes.info");
  194. }
  195. }
  196. for (type in types) {
  197. switch (type) {
  198. case TInst(classRef, params):
  199. if (externs.exists(classRef.toString()))
  200. classRef.get().exclude();
  201. case TEnum(enumRef, params):
  202. if (externs.exists(enumRef.toString()))
  203. enumRef.get().exclude();
  204. default:
  205. }
  206. }
  207. }
  208. // Exclude the standard classes, and any described in 'export_classes.info' files found in the classpath
  209. public static function exclude() {
  210. if (Context.defined("cppia"))
  211. Context.onGenerate(onGenerateCppia);
  212. else
  213. Context.error("cpp.cppia.excludeHostFiles is only for cppia code", Context.currentPos());
  214. return Context.getBuildFields();
  215. }
  216. // Ensure that the standard classes are included in the host
  217. public static function include() {
  218. Compiler.keep("haxe.IMap");
  219. Compiler.keep("haxe.crypto.HashMethod");
  220. Compiler.keep("haxe._Int64.Int64_Impl_");
  221. Compiler.keep("haxe._Int32.Int32_Impl_");
  222. Compiler.keep("haxe._Int64.___Int64");
  223. Compiler.keep("haxe._Int32.___Int32");
  224. for (cls in classes) {
  225. Context.getModule(cls);
  226. Compiler.keep(cls);
  227. }
  228. return Context.getBuildFields();
  229. }
  230. }
  231. #end