HostClasses.hx 6.9 KB

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