Internal.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 python.internal;
  23. #if macro
  24. import haxe.macro.Context;
  25. import haxe.macro.Expr;
  26. #end
  27. @:noPackageRestrict
  28. class Internal {
  29. #if macro
  30. static var _prefix = "_hx_";
  31. static var _className = _prefix + "class_name";
  32. static var _class = _prefix + "class";
  33. static var _fields = _prefix + "fields";
  34. static var _super = _prefix + "super";
  35. static var _methods = _prefix + "methods";
  36. static var _statics = _prefix + "statics";
  37. static var _interfaces = _prefix + "interfaces";
  38. static var _emptyInit = _prefix + "empty_init";
  39. static var _constructs = _prefix + "constructs";
  40. static var _classes = _prefix + "classes";
  41. static var _dict = "__dict__";
  42. static function _getPrefixed(x:Expr):Expr {
  43. return switch (x.expr) {
  44. case EConst(CString(x)): macro @:pos(Context.currentPos()) $v{_prefix + x};
  45. case _: macro @:pos(Context.currentPos()) (python.Syntax.binop($v{_prefix}, "+", $x) : String);
  46. }
  47. }
  48. static function withPos(x:String):Expr {
  49. return macro @:pos(Context.currentPos()) $v{x};
  50. }
  51. static function fieldWithPos(o:Expr, x:String):Expr {
  52. return macro @:pos(Context.currentPos()) (untyped __define_feature__($v{"python." + x}, python.Syntax.field($o, $v{x})));
  53. }
  54. static function has(o:Expr, field:String):Expr {
  55. return macro(untyped __define_feature__($v{"python." + field}, python.internal.UBuiltins.hasattr($o, $v{field})) : Bool);
  56. }
  57. #end
  58. macro public static function getPrefixed(x:ExprOf<String>):Expr {
  59. return switch (x.expr) {
  60. case EConst(CString(x)): macro @:pos(Context.currentPos()) $v{_prefix + x};
  61. case _: macro @:pos(Context.currentPos()) (python.Syntax.binop($v{_prefix}, "+", $x) : String);
  62. }
  63. }
  64. macro public static function classRegistry():Expr {
  65. return macro(untyped __define_feature__($v{"python." + _classes}, python.Syntax.code($v{_classes})) : python.Dict<String, Class<Dynamic>>);
  66. }
  67. macro public static function callFieldPrefixed(o:Expr, x:String, params:Array<Expr>):Expr {
  68. var args = [o, macro @:pos(Context.currentPos()) $v{_prefix + x}].concat(params);
  69. return macro @:pos(Context.currentPos()) python.Syntax.callField($a{args});
  70. }
  71. macro public static function fieldPrefixed(o:Expr, x:String):Expr {
  72. var args = [o, macro @:pos(Context.currentPos()) $v{_prefix + x}];
  73. return macro @:pos(Context.currentPos()) python.Syntax.field($a{args});
  74. }
  75. macro public static function hasAttrPrefixed(o:Expr, x:String):Expr {
  76. var args = [o, macro @:pos(Context.currentPos()) $v{_prefix + x}];
  77. return macro @:pos(Context.currentPos()) python.Syntax.field($a{args});
  78. }
  79. macro public static function importAsPrefixed(o:String, x:String) {
  80. return macro @:pos(Context.currentPos()) python.Syntax.importAs($v{o}, $v{_prefix + x});
  81. }
  82. macro public static function prefix():Expr {
  83. return macro @:pos(Context.currentPos()) $v{_prefix};
  84. }
  85. macro public static function pythonCodePrefixed(x:String):Expr {
  86. return macro python.Syntax.code($v{_prefix + x});
  87. }
  88. macro public static function hasClassName(o:Expr):Expr {
  89. return has(o, _className);
  90. }
  91. macro public static function hasInterfaces(o:Expr):Expr {
  92. return has(o, _interfaces);
  93. }
  94. macro public static function hasClass(o:Expr):Expr {
  95. return has(o, _class);
  96. }
  97. macro public static function hasConstructs(o:Expr):Expr {
  98. return has(o, _constructs);
  99. }
  100. macro public static function hasEmptyInit(o:Expr):Expr {
  101. return has(o, _emptyInit);
  102. }
  103. macro public static function hasMethods(o:Expr):Expr {
  104. return has(o, _methods);
  105. }
  106. macro public static function hasFields(o:Expr):Expr {
  107. return has(o, _fields);
  108. }
  109. macro public static function hasSuper(o:Expr):Expr {
  110. return has(o, _super);
  111. }
  112. macro public static function hasStatics(o:Expr):Expr {
  113. return has(o, _statics);
  114. }
  115. macro public static function classNameVal():Expr {
  116. return withPos(_className);
  117. }
  118. macro public static function methodsVal():Expr {
  119. return withPos(_methods);
  120. }
  121. macro public static function classVal():Expr {
  122. return withPos(_className);
  123. }
  124. macro public static function superVal():Expr {
  125. return withPos(_super);
  126. }
  127. macro public static function interfacesVal():Expr {
  128. return withPos(_interfaces);
  129. }
  130. macro public static function fieldsVal():Expr {
  131. return withPos(_fields);
  132. }
  133. macro public static function staticsVal():Expr {
  134. return withPos(_statics);
  135. }
  136. macro public static function constructsVal():Expr {
  137. return withPos(_constructs);
  138. }
  139. macro public static function emptyInitVal():Expr {
  140. return withPos(_emptyInit);
  141. }
  142. macro public static function fieldClassName(o:Expr):Expr {
  143. return fieldWithPos(o, _className);
  144. }
  145. macro public static function fieldInterfaces(o:Expr):Expr {
  146. return fieldWithPos(o, _interfaces);
  147. }
  148. macro public static function fieldClass(o:Expr):Expr {
  149. return fieldWithPos(o, _class);
  150. }
  151. macro public static function fieldSuper(o:Expr):Expr {
  152. return fieldWithPos(o, _super);
  153. }
  154. macro public static function fieldStatics(o:Expr):Expr {
  155. return fieldWithPos(o, _statics);
  156. }
  157. macro public static function fieldMethods(o:Expr):Expr {
  158. return fieldWithPos(o, _methods);
  159. }
  160. macro public static function fieldFields(o:Expr):Expr {
  161. return fieldWithPos(o, _fields);
  162. }
  163. macro public static function fieldConstructs(o:Expr):Expr {
  164. return fieldWithPos(o, _constructs);
  165. }
  166. macro public static function fieldDict(o:Expr):Expr {
  167. return macro @:pos(Context.currentPos()) python.Syntax.field($o, $v{_dict});
  168. }
  169. macro public static function fieldEmptyInit(o:Expr):Expr {
  170. return fieldWithPos(o, _emptyInit);
  171. }
  172. macro public static function callEmptyInit(o:Expr, instance:Expr):Expr {
  173. return macro @:pos(Context.currentPos()) python.Syntax.callField($o, $v{_emptyInit}, $instance);
  174. }
  175. }