HostClasses.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.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.FileSystem",
  62. "sys.io.File",
  63. "sys.io.FileInput",
  64. "sys.net.Socket",
  65. "Enum",
  66. "EnumValue",
  67. //"Sys",
  68. "Type",
  69. "Xml",
  70. "Date",
  71. "DateTools",
  72. "List",
  73. "Math",
  74. "Reflect",
  75. "StringBuf",
  76. "StringTools",
  77. "haxe.ds.IntMap",
  78. "haxe.ds.ObjectMap",
  79. "haxe.ds.StringMap",
  80. "haxe.CallStack",
  81. "haxe.Serializer",
  82. "haxe.Unserializer",
  83. "haxe.Resource",
  84. "haxe.Template",
  85. "haxe.Utf8",
  86. "haxe.Log",
  87. "haxe.zip.Uncompress",
  88. "haxe.crypto.BaseCode",
  89. "haxe.crypto.Sha256",
  90. "haxe.crypto.Hmac",
  91. "haxe.crypto.Crc32",
  92. "haxe.crypto.Base64",
  93. "haxe.crypto.Adler32",
  94. "haxe.crypto.Md5",
  95. "haxe.crypto.Sha1",
  96. "haxe.io.BufferInput",
  97. "haxe.io.Bytes",
  98. "haxe.io.BytesBuffer",
  99. "haxe.io.BytesData",
  100. "haxe.io.BytesInput",
  101. "haxe.io.BytesOutput",
  102. "haxe.io.Eof",
  103. "haxe.io.Error",
  104. "haxe.io.FPHelper",
  105. "haxe.io.Input",
  106. "haxe.io.Output",
  107. "haxe.io.Path",
  108. "haxe.io.StringInput",
  109. "haxe.xml.Parser",
  110. "haxe.Json",
  111. "haxe.CallStack",
  112. "haxe.Resource",
  113. "haxe.Utf8",
  114. "haxe.Int64",
  115. "haxe.Int32",
  116. "haxe.Serializer",
  117. "haxe.Unserializer",
  118. "haxe.ds.ArraySort",
  119. "haxe.ds.GenericStack",
  120. "haxe.ds.ObjectMap",
  121. "haxe.ds.Vector",
  122. "haxe.ds.BalancedTree",
  123. "haxe.ds.HashMap",
  124. "haxe.ds.Option",
  125. "haxe.ds.WeakMap",
  126. "haxe.ds.EnumValueMap",
  127. "haxe.ds.IntMap",
  128. "haxe.ds.StringMap",
  129. "StdTypes",
  130. "Array",
  131. "Class",
  132. "Date",
  133. "EReg",
  134. "Enum",
  135. "EnumValue",
  136. "IntIterator",
  137. "List",
  138. "Map",
  139. "String",
  140. ];
  141. static function parseClassInfo(externs:Map<String,Bool>, filename:String)
  142. {
  143. if (sys.FileSystem.exists(filename))
  144. {
  145. var file = sys.io.File.read(filename);
  146. try
  147. {
  148. while(true)
  149. {
  150. var line = file.readLine();
  151. var parts = line.split(" ");
  152. if (parts[0]=="class" || parts[0]=="interface" || parts[0]=="enum")
  153. externs.set(parts[1],true);
  154. }
  155. } catch( e : Dynamic ) { }
  156. if (file!=null)
  157. file.close();
  158. }
  159. }
  160. static function onGenerateCppia(types:Array<Type>):Void
  161. {
  162. var externs = new Map<String,Bool>();
  163. externs.set("Sys",true);
  164. externs.set("haxe.IMap",true);
  165. externs.set("haxe.crypto.HashMethod",true);
  166. externs.set("haxe._Int64.Int64_Impl_",true);
  167. externs.set("haxe._Int64.___Int64",true);
  168. externs.set("haxe._Int32.Int32_Impl_",true);
  169. externs.set("haxe._Int32.___Int32",true);
  170. for(e in classes)
  171. externs.set(e,true);
  172. var define = Context.defined("dll_import") ? Context.definedValue("dll_import") : "1";
  173. if (define!="1")
  174. parseClassInfo(externs,define);
  175. else
  176. {
  177. var tried = new Map<String, Bool>();
  178. for(path in Context.getClassPath())
  179. if (!tried.exists(path))
  180. {
  181. tried.set(path,true);
  182. parseClassInfo(externs,path + "/export_classes.info");
  183. }
  184. }
  185. for(type in types)
  186. {
  187. switch(type)
  188. {
  189. case TInst(classRef, params):
  190. if (externs.exists(classRef.toString()))
  191. classRef.get().exclude();
  192. case TEnum(enumRef, params):
  193. if (externs.exists(enumRef.toString()))
  194. enumRef.get().exclude();
  195. default:
  196. }
  197. }
  198. }
  199. // Exclude the standard classes, and any described in 'export_classes.info' files found in the classpath
  200. public static function exclude()
  201. {
  202. if (Context.defined("cppia"))
  203. Context.onGenerate(onGenerateCppia);
  204. else
  205. Context.error("cpp.cppia.excludeHostFiles is only for cppia code", Context.currentPos());
  206. return Context.getBuildFields();
  207. }
  208. // Ensure that the standard classes are included in the host
  209. public static function include()
  210. {
  211. Compiler.keep("haxe.IMap");
  212. Compiler.keep("haxe.crypto.HashMethod");
  213. Compiler.keep("haxe._Int64.Int64_Impl_");
  214. Compiler.keep("haxe._Int32.Int32_Impl_");
  215. Compiler.keep("haxe._Int64.___Int64");
  216. Compiler.keep("haxe._Int32.___Int32");
  217. for(cls in classes)
  218. {
  219. Context.getModule(cls);
  220. Compiler.keep(cls);
  221. }
  222. return Context.getBuildFields();
  223. }
  224. }
  225. #end