HostClasses.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.FileSystem",
  40. "sys.io.File",
  41. "sys.io.FileInput",
  42. "sys.net.Socket",
  43. "Enum",
  44. "EnumValue",
  45. //"Sys",
  46. "Type",
  47. "Xml",
  48. "Date",
  49. "DateTools",
  50. "List",
  51. "Math",
  52. "Reflect",
  53. "StringBuf",
  54. "StringTools",
  55. "haxe.ds.IntMap",
  56. "haxe.ds.ObjectMap",
  57. "haxe.ds.StringMap",
  58. "haxe.CallStack",
  59. "haxe.Serializer",
  60. "haxe.Unserializer",
  61. "haxe.Resource",
  62. "haxe.Template",
  63. "haxe.Utf8",
  64. "haxe.Log",
  65. "haxe.zip.Uncompress",
  66. "haxe.crypto.BaseCode",
  67. "haxe.crypto.Sha256",
  68. "haxe.crypto.Hmac",
  69. "haxe.crypto.Crc32",
  70. "haxe.crypto.Base64",
  71. "haxe.crypto.Adler32",
  72. "haxe.crypto.Md5",
  73. "haxe.crypto.Sha1",
  74. "haxe.io.BufferInput",
  75. "haxe.io.Bytes",
  76. "haxe.io.BytesBuffer",
  77. "haxe.io.BytesData",
  78. "haxe.io.BytesInput",
  79. "haxe.io.BytesOutput",
  80. "haxe.io.Eof",
  81. "haxe.io.Error",
  82. "haxe.io.FPHelper",
  83. "haxe.io.Input",
  84. "haxe.io.Output",
  85. "haxe.io.Path",
  86. "haxe.io.StringInput",
  87. "haxe.xml.Parser",
  88. "haxe.Json",
  89. "haxe.CallStack",
  90. "haxe.Resource",
  91. "haxe.Utf8",
  92. "haxe.Int64",
  93. "haxe.Serializer",
  94. "haxe.Unserializer",
  95. "haxe.ds.ArraySort",
  96. "haxe.ds.GenericStack",
  97. "haxe.ds.ObjectMap",
  98. "haxe.ds.Vector",
  99. "haxe.ds.BalancedTree",
  100. "haxe.ds.HashMap",
  101. "haxe.ds.Option",
  102. "haxe.ds.WeakMap",
  103. "haxe.ds.EnumValueMap",
  104. "haxe.ds.IntMap",
  105. "haxe.ds.StringMap",
  106. "StdTypes",
  107. "Array",
  108. "Class",
  109. "Date",
  110. "EReg",
  111. "Enum",
  112. "EnumValue",
  113. "IntIterator",
  114. "List",
  115. "Map",
  116. "String",
  117. ];
  118. static function onGenerateCppia(types:Array<Type>):Void
  119. {
  120. var externs = new Map<String,Bool>();
  121. externs.set("Sys",true);
  122. externs.set("haxe.IMap",true);
  123. for(e in classes)
  124. externs.set(e,true);
  125. for(path in Context.getClassPath())
  126. {
  127. var filename = path + "/export_classes.info";
  128. if (sys.FileSystem.exists(filename))
  129. {
  130. try
  131. {
  132. var contents = sys.io.File.getContent(filename);
  133. contents = contents.split("\r").join("");
  134. for(cls in contents.split("\n"))
  135. {
  136. if (cls!="")
  137. {
  138. var parts = cls.split("|");
  139. if (parts.length==1)
  140. externs.set(cls,true);
  141. }
  142. }
  143. } catch( e : Dynamic ) { }
  144. }
  145. }
  146. for(type in types)
  147. {
  148. switch(type)
  149. {
  150. case TInst(classRef, params):
  151. if (externs.exists(classRef.toString()))
  152. classRef.get().exclude();
  153. case TEnum(enumRef, params):
  154. if (externs.exists(enumRef.toString()))
  155. enumRef.get().exclude();
  156. default:
  157. }
  158. }
  159. }
  160. // Exclude the standard classes, and any described in 'export_classes.info' files found in the classpath
  161. public static function exclude()
  162. {
  163. if (Context.defined("cppia"))
  164. Context.onGenerate(onGenerateCppia);
  165. else
  166. Context.error("cpp.cppia.excludeHostFiles is only for cppia code", Context.currentPos());
  167. return Context.getBuildFields();
  168. }
  169. // Ensure that the standard classes are included in the host
  170. public static function include()
  171. {
  172. Compiler.keep("haxe.IMap");
  173. for(cls in classes)
  174. {
  175. Context.getModule(cls);
  176. Compiler.keep(cls);
  177. }
  178. return Context.getBuildFields();
  179. }
  180. }
  181. #end