HostClasses.hx 5.0 KB

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