Internal.hx 5.5 KB

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