From c69100f2cca8f1d89dcca80155c4a879d6719e20 Mon Sep 17 00:00:00 2001 From: shoebox Date: Sun, 1 Jun 2014 22:08:24 +0100 Subject: [PATCH 01/15] Refactoring in progress --- src/org/shoebox/macros/MacroMirrors.hx | 679 ++++++++++++------------- 1 file changed, 339 insertions(+), 340 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 9f43937..b81fa3f 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -26,412 +26,411 @@ using haxe.macro.Tools; * ... * @author shoe[box] */ -class MacroMirrors{ - - // -------o constructor - - /** - * constructor - * - * @param - * @return void - */ - private function new() { - +class MacroMirrors +{ + #if macro + + public static function build( ) : Array + { + + var fields:Array = Context.getBuildFields( ); + var localClass:Null> = Context.getLocalClass( ); + + if(!Context.defined("openfl")) + return fields; + + var metadatas : Array; + + for(field in fields.copy()) + { + + metadatas = [ for( m in field.meta ) if( m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m ]; + if( metadatas.length == 0 ) + continue; + + for(m in metadatas) + { + if(m.name == "CPP" && Context.defined("cpp")) + { + fields.push( _cpp( + field , + ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).name, + ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ) ); + } + else if( m.name == "JNI" && Context.defined("android")) + { + fields.push( _jni( + field , + ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, + ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ) ); + } + else if( m.name == "IOS" && Context.defined("ios")) + { + fields.push( _cpp( + field , + ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, + ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ) ); + } + } } - // -------o public + return fields; + } + + static function _jni(oField:Field , sPackage:String , ?sName:String ):Field + { + sPackage = sPackage.split(".").join("/"); + + //The function + var f : Function = _getFunc( oField ); + if(f.ret == null) + f.ret = TPath({ name : "Void", pack : [], params : [] }); + + var isStaticMethod = Lambda.has( oField.access , AStatic ); - #if macro + //Arguments + var iArgs : Int = f.args.length; + var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; - /** - * - * - * @public - * @return void - */ - static public function build( ) : Array{ - - //Fields - var aFields : Array = Context.getBuildFields( ); - var oClass : Null> = Context.getLocalClass( ); - - //OpenFL only - if(!Context.defined("openfl")) - return aFields; - - // - var sClass_name : String = oClass.get( ).name; - - // - var aMetas : Array; - var bCPP : Bool; - var bJNI : Bool; - for( field in aFields.copy( ) ){ - - // - aMetas = [ for( m in field.meta ) if( m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m ]; - - // - if( aMetas.length == 0 ) - continue; - - // - for(m in aMetas) - { - if(m.name == "CPP" && Context.defined("cpp")) - { - aFields.push( _cpp( - field , - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : sClass_name, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name - ) ); - } - else if( m.name == "JNI" && Context.defined("android")) - { - aFields.push( _jni( - field , - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : oClass.get( ).module, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name - ) ); - } - else if( m.name == "IOS" && Context.defined("ios")) - { - aFields.push( _cpp( - field , - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : oClass.get( ).module, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name - ) ); - } - - } + //JNI Arguments translation + var ct : Null; + var tp : Null; + var sgnature : String = "("; + var i = 0; + for( arg in f.args ){ + //For non static member the first argument is not include in the JNI definition + if( i++ == 0 && !isStaticMethod ) + continue; - } + sgnature += JniTools.translateType(arg.type.toType(), oField.pos); + } + sgnature += ")"; - return aFields; - } + //For non static we convert the first argument type to dynamic + if( !isStaticMethod ){ + f.args[ 0 ].type = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); + } - // -------o protected - - /** - * Making the JNI mirror - * - * @private - * @param oField : the targetted field ( Field ) - * @param sPackage : the package name ( String ) - * @param sPackage : the function name ( String ) - * @return the new instance field ( Field ) - */ - static private function _jni( oField : Field , sPackage : String , ?sName : String ) : Field{ - - sPackage = sPackage.split(".").join("/"); + //Return Type + if(f.ret==null) + sgnature += "V"; + else + sgnature += _translateType( ComplexTypeTools.toType( f.ret ) ); + + //Verbose - //The function - var f : Function = _getFunc( oField ); - if(f.ret == null) - f.ret = TPath({ name : "Void", pack : [], params : [] }); - - var bStatic = Lambda.has( oField.access , AStatic ); - - //Arguments - var iArgs : Int = f.args.length; - var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; - - //JNI Arguments translation - var ct : Null; - var tp : Null; - var sJNI : String = "("; - var i = 0; - for( arg in f.args ){ - - //For non static member the first argument is not include in the JNI definition - if( i++ == 0 && !bStatic ) - continue; - - ct = arg.type; - tp = ComplexTypeTools.toType( ct ); - sJNI += _translateType( tp ); - } - sJNI += ")"; + Sys.println('[MIRROR] - JNI $sPackage::$sName $sgnature'); + #if verbose_mirrors + #end + + //Variable + var sVar_name : String = "mirror_jni_"+sName; + var fVar = _createVariable( sVar_name , f ); + + //Return response + if( f.ret.getParameters( )[0].name != "Void") + f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); //Switching the return type to dynamic + + var eRet = null; + if( f.ret.getParameters( )[ 0 ].name == "Void" ){ + eRet = macro $i{sVar_name}( $a{aNames} ); + }else{ + eRet = macro{ + var args : Array = $a{ aNames }; + #if verbose_mirrors + trace( "call with args ::: "+args); + #end + return $i{sVar_name}( $a{aNames} ); + }; + } - //For non static we convert the first argument type to dynamic - if( !bStatic ){ - f.args[ 0 ].type = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); - } + //Result - //Return Type - if(f.ret==null) - sJNI += "V"; - else - sJNI += _translateType( ComplexTypeTools.toType( f.ret ) ); - - //Verbose - #if verbose_mirrors - trace( '[MIRROR] - JNI $sPackage::$sName $sJNI' ); - #end - - //Variable - var sVar_name : String = "mirror_jni_"+sName; - var fVar = _createVariable( sVar_name , f ); - - //Return response - if( f.ret.getParameters( )[0].name != "Void") - f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); //Switching the return type to dynamic - - var eRet = null; - if( f.ret.getParameters( )[ 0 ].name == "Void" ){ - eRet = macro $i{sVar_name}( $a{aNames} ); - }else{ - eRet = macro{ - var args : Array = $a{ aNames }; + f.expr = macro{ + + //Already loaded ? + if( $i{ sVar_name } == null ){ #if verbose_mirrors - trace( "call with args ::: "+args); + trace("Lib not loaded, loading it"); + trace( $v{ sPackage }+"::"+$v{ sName }+' :: signature '+$v{ sgnature } ); #end - return $i{sVar_name}( $a{aNames} ); - }; - } - //Result + // + if( $v{ isStaticMethod } ) + $i{ sVar_name } = openfl.utils.JNI.createStaticMethod( + $v{sPackage}, + $v{sName}, + $v{sgnature} + ); + else + $i{ sVar_name } = openfl.utils.JNI.createMemberMethod( + $v{sPackage}, + $v{sName}, + $v{sgnature} + ); - f.expr = macro{ - //Already loaded ? - if( $i{ sVar_name } == null ){ - #if verbose_mirrors - trace("Lib not loaded, loading it"); - trace( $v{ sPackage }+"::"+$v{ sName }+' :: signature '+$v{ sJNI } ); - #end + } - // - if( $v{ bStatic } ) - $i{ sVar_name } = openfl.utils.JNI.createStaticMethod( - $v{sPackage}, - $v{sName}, - $v{sJNI} - ); - else - $i{ sVar_name } = openfl.utils.JNI.createMemberMethod( - $v{sPackage}, - $v{sName}, - $v{sJNI} - ); + //Making the call + $eRet; + } - } + return fVar; + } - //Making the call - $eRet; - } + static function _translateType( tp : Null ) : String{ + var c : ClassType; + return switch( tp ){ - return fVar; - } + case TAbstract( cf , a ): + _jniAbstract_type( cf.get( ) ); - /** - * JNI translation of the type - * - * @private - * @param tp : The type to be translated ( Null ) - * @return JNI translation of the Type ( String ) - */ - static private function _translateType( tp : Null ) : String{ + case TDynamic( t ): + "Lorg/haxe/nme/HaxeObject;"; - var c : ClassType; - return switch( tp ){ + default: + c = tp.getParameters( )[ 0 ].get( ); + switch( c.name ){ - case TAbstract( cf , a ): - _jniAbstract_type( cf.get( ) ); + case "String": + "Ljava/lang/String;"; - case TDynamic( t ): - "Lorg/haxe/nme/HaxeObject;"; + case "Array": + "["+_translateType( tp.getParameters( )[ 1 ][0] ); - default: - c = tp.getParameters( )[ 0 ].get( ); - switch( c.name ){ + default: + "L"+c.pack.join("/")+( c.pack.length == 0 ? "" : "/" ) + c.name+";"; - case "String": - "Ljava/lang/String;"; + } + } + } - case "Array": - "["+_translateType( tp.getParameters( )[ 1 ][0] ); + static function _jniAbstract_type( cf : AbstractType ) : String{ - default: - "L"+c.pack.join("/")+( c.pack.length == 0 ? "" : "/" ) + c.name+";"; + return switch( cf.name ){ - } + case "Float": + "F"; + case "Bool": + "Z"; + case "Int": + "I"; + + case "Void": + "V"; + + default: + Context.error("Unknow abstract type ::: "+cf.name , Context.currentPos( ) ); - } } - /** - * - * - * @private - * @return void - */ - static private function _jniAbstract_type( cf : AbstractType ) : String{ + return null; + //return cf.get( ).name; + } - return switch( cf.name ){ + static function _cpp( oField : Field , sPackage : String , ?sName : String ) : Field{ - case "Float": - "F"; + //The function + var f : Function = _getFunc( oField ); - case "Bool": - "Z"; + //Arguments + var iArgs : Int = f.args.length; + var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; + + //Verbose + trace( '[MIRROR] - CPP $sPackage::'+oField.name+'($iArgs)' ); + #if verbose_mirrors + #end - case "Int": - "I"; + //Variable + var sVar_name : String = "mirror_cpp_"+sName; + var fVar = _createVariable( sVar_name , f ); - case "Void": - "V"; + //Return response - default: - Context.error("Unknow abstract type ::: "+cf.name , Context.currentPos( ) ); + var eRet = macro ""; + if( f.ret.getParameters( )[ 0 ].name == "Void" ) + eRet = macro $i{sVar_name}( $a{aNames} ); + else + eRet = macro return $i{sVar_name}( $a{aNames} ); - } + //Result + f.expr = macro{ - return null; - //return cf.get( ).name; - } + //Alread#if verbose_mirrorsy loaded ? + if( $i{ sVar_name } == null ){ + trace("Lib not loaded, loading it"); + trace( $v{ sPackage }+"::"+$v{ sName }+'($iArgs)' ); + #end - /** - * Making the CPP mirror - * - * @private - * @param oField : the targetted field ( Field ) - * @param sPackage : the package name ( String ) - * @param sPackage : the function name ( String ) - * @return the new instance field ( Field ) - */ - static private function _cpp( oField : Field , sPackage : String , ?sName : String ) : Field{ - - //The function - var f : Function = _getFunc( oField ); - - //Arguments - var iArgs : Int = f.args.length; - var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; - - //Verbose - #if verbose_mirrors - trace( '[MIRROR] - CPP $sPackage::'+oField.name+'($iArgs)' ); - #end - - //Variable - var sVar_name : String = "mirror_cpp_"+sName; - var fVar = _createVariable( sVar_name , f ); - - //Return response - - var eRet = macro ""; - if( f.ret.getParameters( )[ 0 ].name == "Void" ) - eRet = macro $i{sVar_name}( $a{aNames} ); - else - eRet = macro return $i{sVar_name}( $a{aNames} ); + // + $i{ sVar_name } = cpp.Lib.load( $v{ sPackage } , $v{ sName } , $v{ iArgs }); - //Result - f.expr = macro{ + } - //Already loaded ? - if( $i{ sVar_name } == null ){ - #if verbose_mirrors - trace("Lib not loaded, loading it"); - trace( $v{ sPackage }+"::"+$v{ sName }+'($iArgs)' ); - #end + $eRet; + } - // - $i{ sVar_name } = cpp.Lib.load( $v{ sPackage } , $v{ sName } , $v{ iArgs }); - } + return fVar; + } - $eRet; - } + static function _createVariable( sName : String , f : Function ) : Field{ + var aTypes : Array> = [ for( a in f.args ) a.type ]; + var k : FieldType = FVar(TFunction(aTypes , f.ret)); + + return { + name : sName , + doc : null, + meta : [], + access : [APublic,AStatic], + kind : k , + pos : haxe.macro.Context.currentPos() + }; + } + + static function _getString( e : Expr ) : String{ + + if( e == null ) + return null; - return fVar; - } + return switch( e.expr.getParameters( )[ 0 ] ){ - /** - * Create the variable who will contains the instance of mirrored method - * - * @private - * @return void - */ - static private function _createVariable( sName : String , f : Function ) : Field{ - - - //Variable type - var aTypes : Array> = [ for( a in f.args ) a.type ]; - - // - var k : FieldType = FVar(TFunction(aTypes , f.ret)); - /* - return { - name : sName , - doc : null, - meta : [], - access : [APublic,AStatic], - kind : FVar(TPath({ pack : [], name : "Dynamic", params : [], sub : null }),null), - pos : Context.currentPos() - }; - */ - // - return { - name : sName , - doc : null, - meta : [], - access : [APrivate,AStatic], - kind : k , - pos : haxe.macro.Context.currentPos() - }; - } + case CString( s ): + s; - /** - * - * - * @private - * @return void - */ - static private function _getString( e : Expr ) : String{ + default: + null; - if( e == null ) - return null; + } - return switch( e.expr.getParameters( )[ 0 ] ){ + } - case CString( s ): - s; + static function _getFunc( f : Field ) : Function{ + return switch( f.kind ){ - default: - null; + case FFun( f ): + f; - } + default: + Context.error("Only function are supported",f.pos); + + } + } + +} + +class JniTools +{ + public static function translateArg(arg:FunctionArg, pos:Position):String + { + var argType:Null = arg.type.toType(); + return translateType(argType, pos); + } + + public static function translateType(argType:Null, pos:Position):String + { + return switch(argType) + { + case TAbstract(cf, a ): + translateAbstractType(cf.get(), pos); + + case TDynamic(t): + if (Context.defined("openfl")) + "Lorg/haxe/lime/HaxeObject;"; + else + "Lorg/haxe/nme/HaxeObject;"; + default: + translateArgType(argType, pos); + } + } + + public static function translateArgType(type:Null, pos:Position):String + { + return switch(type) + { + case TInst(t, params): + translateSubArgType(type, params, pos); + + default: + Context.fatalError( + "Unsupported Type ::: " + type.getParameters()[0], pos); + } + } + + public static function translateSubArgType(type:Null, params:Array, + pos:Position):String + { + var result:String; + switch(type.getParameters()[0].get().name) + { + case "String": + result = "Ljava/lang/String;"; + + case "Array": + result = "[" + translateType(params[0], pos); + + default: + var classType:ClassType = type.getParameters()[0].get(); + result = "L"+classType.pack.join("/") + + (classType.pack.length == 0 ? "" : "/" ) + + classType.name+";"; } - /** - * Get the field function - * - * @private - * @return void - */ - static private function _getFunc( f : Field ) : Function{ - return switch( f.kind ){ + return result; + } - case FFun( f ): - f; + public static function translateAbstractType(a:AbstractType, pos:Position):String + { + var result:String = null; + result = switch(a.name) + { + case "Float": + "F"; - default: - Context.error("Only function are supported",f.pos); + case "Bool": + "Z"; - } + case "Int": + "I"; + + case "Void": + "V"; + + default: + Context.fatalError("Unsupported abstract type ::: "+a.name, pos); } - #end + return result; + } +} + +class FieldTool +{ + public static function getFunction(field:Field):Function + { + var result:Function; + switch(field.kind) + { + case FFun(f): + result = f; + + default: + Context.error("Only function are supported",field.pos); + } - // -------o misc + return result; + } -} \ No newline at end of file +} From fca026293c2f3ec24e59e6d5f744b7e21731e591 Mon Sep 17 00:00:00 2001 From: shoebox Date: Sun, 1 Jun 2014 22:11:57 +0100 Subject: [PATCH 02/15] Refactoring in progress --- src/org/shoebox/macros/MacroMirrors.hx | 68 +++++++++++++------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index b81fa3f..ae3b72c 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -30,7 +30,7 @@ class MacroMirrors { #if macro - public static function build( ) : Array + public static function build():Array { var fields:Array = Context.getBuildFields( ); @@ -44,32 +44,31 @@ class MacroMirrors for(field in fields.copy()) { - metadatas = [ for( m in field.meta ) if( m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m ]; + metadatas = [for( m in field.meta ) + if( m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m ]; + if( metadatas.length == 0 ) continue; for(m in metadatas) { - if(m.name == "CPP" && Context.defined("cpp")) + if (m.name == "CPP" && Context.defined("cpp")) { - fields.push( _cpp( - field , + fields.push( cpp(field, ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).name, ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name ) ); } - else if( m.name == "JNI" && Context.defined("android")) + else if (m.name == "JNI" && Context.defined("android")) { - fields.push( _jni( - field , + fields.push(jni(field, ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name ) ); } - else if( m.name == "IOS" && Context.defined("ios")) + else if (m.name == "IOS" && Context.defined("ios")) { - fields.push( _cpp( - field , + fields.push( cpp(field, ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name ) ); @@ -80,20 +79,21 @@ class MacroMirrors return fields; } - static function _jni(oField:Field , sPackage:String , ?sName:String ):Field + static function jni(oField:Field, packageName:String, + ?variableName:String ):Field { - sPackage = sPackage.split(".").join("/"); + packageName = packageName.split(".").join("/"); //The function - var f : Function = _getFunc( oField ); + var f : Function = FieldTool.getFunction(oField); if(f.ret == null) f.ret = TPath({ name : "Void", pack : [], params : [] }); var isStaticMethod = Lambda.has( oField.access , AStatic ); //Arguments - var iArgs : Int = f.args.length; - var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; + var argsCount : Int = f.args.length; + var argumentNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; //JNI Arguments translation var ct : Null; @@ -123,12 +123,12 @@ class MacroMirrors //Verbose - Sys.println('[MIRROR] - JNI $sPackage::$sName $sgnature'); + Sys.println('[MIRROR] - JNI $packageName::$variableName $sgnature'); #if verbose_mirrors #end //Variable - var sVar_name : String = "mirror_jni_"+sName; + var sVar_name : String = "mirror_jni_"+variableName; var fVar = _createVariable( sVar_name , f ); //Return response @@ -137,14 +137,14 @@ class MacroMirrors var eRet = null; if( f.ret.getParameters( )[ 0 ].name == "Void" ){ - eRet = macro $i{sVar_name}( $a{aNames} ); + eRet = macro $i{sVar_name}( $a{argumentNames} ); }else{ eRet = macro{ - var args : Array = $a{ aNames }; + var args : Array = $a{ argumentNames }; #if verbose_mirrors trace( "call with args ::: "+args); #end - return $i{sVar_name}( $a{aNames} ); + return $i{sVar_name}( $a{argumentNames} ); }; } @@ -156,20 +156,20 @@ class MacroMirrors if( $i{ sVar_name } == null ){ #if verbose_mirrors trace("Lib not loaded, loading it"); - trace( $v{ sPackage }+"::"+$v{ sName }+' :: signature '+$v{ sgnature } ); + trace( $v{ packageName }+"::"+$v{ variableName }+' :: signature '+$v{ sgnature } ); #end // if( $v{ isStaticMethod } ) $i{ sVar_name } = openfl.utils.JNI.createStaticMethod( - $v{sPackage}, - $v{sName}, + $v{packageName}, + $v{variableName}, $v{sgnature} ); else $i{ sVar_name } = openfl.utils.JNI.createMemberMethod( - $v{sPackage}, - $v{sName}, + $v{packageName}, + $v{variableName}, $v{sgnature} ); @@ -237,17 +237,17 @@ class MacroMirrors //return cf.get( ).name; } - static function _cpp( oField : Field , sPackage : String , ?sName : String ) : Field{ + static function cpp( oField : Field , packageName : String , ?sName : String ) : Field{ //The function var f : Function = _getFunc( oField ); //Arguments - var iArgs : Int = f.args.length; - var aNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; + var argsCount : Int = f.args.length; + var argumentNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; //Verbose - trace( '[MIRROR] - CPP $sPackage::'+oField.name+'($iArgs)' ); + trace( '[MIRROR] - CPP $packageName::'+oField.name+'($argsCount)' ); #if verbose_mirrors #end @@ -259,9 +259,9 @@ class MacroMirrors var eRet = macro ""; if( f.ret.getParameters( )[ 0 ].name == "Void" ) - eRet = macro $i{sVar_name}( $a{aNames} ); + eRet = macro $i{sVar_name}( $a{argumentNames} ); else - eRet = macro return $i{sVar_name}( $a{aNames} ); + eRet = macro return $i{sVar_name}( $a{argumentNames} ); //Result f.expr = macro{ @@ -269,11 +269,11 @@ class MacroMirrors //Alread#if verbose_mirrorsy loaded ? if( $i{ sVar_name } == null ){ trace("Lib not loaded, loading it"); - trace( $v{ sPackage }+"::"+$v{ sName }+'($iArgs)' ); + trace( $v{ packageName }+"::"+$v{ sName }+'($argsCount)' ); #end // - $i{ sVar_name } = cpp.Lib.load( $v{ sPackage } , $v{ sName } , $v{ iArgs }); + $i{ sVar_name } = cpp.Lib.load( $v{ packageName } , $v{ sName } , $v{ argsCount }); } From ee3385eb2c295b3d99be22e0ae4796a93aecea00 Mon Sep 17 00:00:00 2001 From: shoebox Date: Sun, 1 Jun 2014 22:49:09 +0100 Subject: [PATCH 03/15] Refactoring in progress --- src/org/shoebox/macros/MacroMirrors.hx | 223 ++++++++++++++----------- 1 file changed, 125 insertions(+), 98 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index ae3b72c..d0f4ef5 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -28,49 +28,46 @@ using haxe.macro.Tools; */ class MacroMirrors { - #if macro - public static function build():Array { var fields:Array = Context.getBuildFields( ); var localClass:Null> = Context.getLocalClass( ); - if(!Context.defined("openfl")) + if (!Context.defined("openfl")) return fields; var metadatas : Array; - for(field in fields.copy()) + for (field in fields.copy()) { + metadatas = [for (m in field.meta ) + if (m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m]; - metadatas = [for( m in field.meta ) - if( m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m ]; - - if( metadatas.length == 0 ) + if ( metadatas.length == 0 ) continue; - for(m in metadatas) + for (m in metadatas) { if (m.name == "CPP" && Context.defined("cpp")) { fields.push( cpp(field, - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).name, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).name, + ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name ) ); } else if (m.name == "JNI" && Context.defined("android")) { fields.push(jni(field, - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).module, + ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name ) ); } else if (m.name == "IOS" && Context.defined("ios")) { fields.push( cpp(field, - ( m.params.length > 0 ) ? _getString( m.params[ 0 ] ) : localClass.get( ).module, - ( m.params.length > 1 ) ? _getString( m.params[ 1 ] ) : field.name + ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).module, + ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name ) ); } } @@ -79,67 +76,52 @@ class MacroMirrors return fields; } - static function jni(oField:Field, packageName:String, + static function jni(field:Field, packageName:String, ?variableName:String ):Field { packageName = packageName.split(".").join("/"); //The function - var f : Function = FieldTool.getFunction(oField); - if(f.ret == null) + var f : Function = FieldTool.getFunction(field); + if (f.ret == null) f.ret = TPath({ name : "Void", pack : [], params : [] }); - var isStaticMethod = Lambda.has( oField.access , AStatic ); + var isStaticMethod = Lambda.has( field.access , AStatic ); //Arguments var argsCount : Int = f.args.length; - var argumentNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; + var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; //JNI Arguments translation - var ct : Null; - var tp : Null; - var sgnature : String = "("; - var i = 0; - for( arg in f.args ){ - - //For non static member the first argument is not include in the JNI definition - if( i++ == 0 && !isStaticMethod ) - continue; - - sgnature += JniTools.translateType(arg.type.toType(), oField.pos); - } - sgnature += ")"; + var signature = JniTools.getSignature(field); //For non static we convert the first argument type to dynamic - if( !isStaticMethod ){ + if ( !isStaticMethod ) f.args[ 0 ].type = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); - } - - //Return Type - if(f.ret==null) - sgnature += "V"; - else - sgnature += _translateType( ComplexTypeTools.toType( f.ret ) ); //Verbose - Sys.println('[MIRROR] - JNI $packageName::$variableName $sgnature'); + Sys.println('[MIRROR] - JNI $packageName::$variableName $signature'); #if verbose_mirrors #end //Variable var sVar_name : String = "mirror_jni_"+variableName; - var fVar = _createVariable( sVar_name , f ); + var fVar = createVariable( sVar_name , f ); //Return response - if( f.ret.getParameters( )[0].name != "Void") + if ( f.ret.getParameters( )[0].name != "Void") f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); //Switching the return type to dynamic var eRet = null; - if( f.ret.getParameters( )[ 0 ].name == "Void" ){ + if ( f.ret.getParameters( )[ 0 ].name == "Void" ) + { eRet = macro $i{sVar_name}( $a{argumentNames} ); - }else{ - eRet = macro{ + } + else + { + eRet = macro + { var args : Array = $a{ argumentNames }; #if verbose_mirrors trace( "call with args ::: "+args); @@ -150,27 +132,29 @@ class MacroMirrors //Result - f.expr = macro{ + f.expr = macro + { //Already loaded ? - if( $i{ sVar_name } == null ){ + if ( $i{ sVar_name } == null ) + { #if verbose_mirrors trace("Lib not loaded, loading it"); - trace( $v{ packageName }+"::"+$v{ variableName }+' :: signature '+$v{ sgnature } ); + trace( $v{ packageName }+"::"+$v{ variableName }+' :: signature '+$v{ signature } ); #end // - if( $v{ isStaticMethod } ) + if ( $v{ isStaticMethod } ) $i{ sVar_name } = openfl.utils.JNI.createStaticMethod( $v{packageName}, $v{variableName}, - $v{sgnature} + $v{signature} ); else $i{ sVar_name } = openfl.utils.JNI.createMemberMethod( $v{packageName}, $v{variableName}, - $v{sgnature} + $v{signature} ); @@ -232,65 +216,54 @@ class MacroMirrors Context.error("Unknow abstract type ::: "+cf.name , Context.currentPos( ) ); } - - return null; - //return cf.get( ).name; } - static function cpp( oField : Field , packageName : String , ?sName : String ) : Field{ + static function cpp( field:Field, packageName : String, ?name : String ) : Field{ - //The function - var f : Function = _getFunc( oField ); + var f:Function = getFunc( field ); - //Arguments - var argsCount : Int = f.args.length; - var argumentNames : Array = [ for( a in f.args ) macro $i{ a.name } ]; + var argsCount : Int = f.args.length; + var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; - //Verbose - trace( '[MIRROR] - CPP $packageName::'+oField.name+'($argsCount)' ); - #if verbose_mirrors - #end - - //Variable - var sVar_name : String = "mirror_cpp_"+sName; - var fVar = _createVariable( sVar_name , f ); - - //Return response - - var eRet = macro ""; - if( f.ret.getParameters( )[ 0 ].name == "Void" ) - eRet = macro $i{sVar_name}( $a{argumentNames} ); - else - eRet = macro return $i{sVar_name}( $a{argumentNames} ); + trace( '[MIRROR] - CPP $packageName::'+field.name+'($argsCount)' ); + #if verbose_mirrors + #end - //Result - f.expr = macro{ + var variableName : String = "mirror_cpp_"+name; + var fieldVariable = createVariable(variableName, f); - //Alread#if verbose_mirrorsy loaded ? - if( $i{ sVar_name } == null ){ - trace("Lib not loaded, loading it"); - trace( $v{ packageName }+"::"+$v{ sName }+'($argsCount)' ); - #end + var returnExpr = macro ""; - // - $i{ sVar_name } = cpp.Lib.load( $v{ packageName } , $v{ sName } , $v{ argsCount }); + if ( f.ret.getParameters( )[ 0 ].name == "Void" ) + returnExpr = macro $i{variableName}( $a{argumentNames} ); + else + returnExpr = macro return $i{variableName}( $a{argumentNames} ); - } + f.expr = macro + { + if ($i{variableName} == null) + { + #if verbose_mirrors + trace("Lib not loaded, loading it"); + trace($v{packageName}+"::"+$v{name}+'($argsCount)'); + #end - $eRet; + $i{ variableName } = cpp.Lib.load($v{packageName}, $v{name}, + $v{argsCount}); } + $returnExpr; + } - - return fVar; + return fieldVariable; } - static function _createVariable( sName : String , f : Function ) : Field{ - - var aTypes : Array> = [ for( a in f.args ) a.type ]; + static function createVariable(name:String, f:Function):Field + { + var aTypes : Array> = [ for ( a in f.args ) a.type ]; var k : FieldType = FVar(TFunction(aTypes , f.ret)); return { - name : sName , + name : name , doc : null, meta : [], access : [APublic,AStatic], @@ -299,9 +272,10 @@ class MacroMirrors }; } - static function _getString( e : Expr ) : String{ + static function getString(e:Expr):String + { - if( e == null ) + if ( e == null ) return null; return switch( e.expr.getParameters( )[ 0 ] ){ @@ -316,7 +290,8 @@ class MacroMirrors } - static function _getFunc( f : Field ) : Function{ + static function getFunc(f:Field):Function + { return switch( f.kind ){ case FFun( f ): @@ -332,6 +307,22 @@ class MacroMirrors class JniTools { + public static function getSignature(field:Field):String + { + var func:Function = FieldTool.getFunction(field); + var signature = "("; + for(arg in func.args) + { + signature += translateArg(arg, field.pos); + } + + var returnType:Null = func.ret.toType(); + + signature += ")" + translateType(returnType, field.pos); + + return signature; + } + public static function translateArg(arg:FunctionArg, pos:Position):String { var argType:Null = arg.type.toType(); @@ -434,3 +425,39 @@ class FieldTool } } + +class MetaDataTools +{ + + public static function has(field:Field, metaName:String):Bool + { + var result = false; + + for(meta in field.meta) + { + if (meta.name == metaName) + { + result = true; + break; + } + } + + return result; + } + + public static function get(field:Field, metaName:String):MetadataEntry + { + var result:MetadataEntry = null; + for(meta in field.meta) + { + if (meta.name == metaName) + { + result = meta; + break; + } + } + + return result; + } + +} From ec753e0c892f365c0275c7bf34f47941ca47342e Mon Sep 17 00:00:00 2001 From: shoebox Date: Sun, 1 Jun 2014 23:37:56 +0100 Subject: [PATCH 04/15] Refactoring in progress --- src/org/shoebox/macros/MacroMirrors.hx | 200 ++++++++----------------- 1 file changed, 65 insertions(+), 135 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index d0f4ef5..bb0fdfd 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -81,141 +81,72 @@ class MacroMirrors { packageName = packageName.split(".").join("/"); - //The function - var f : Function = FieldTool.getFunction(field); - if (f.ret == null) - f.ret = TPath({ name : "Void", pack : [], params : [] }); + var f : Function = FieldTool.getFunction(field); + if (f.ret == null) + f.ret = TPath({ name : "Void", pack : [], params : [] }); - var isStaticMethod = Lambda.has( field.access , AStatic ); + var isStaticMethod = Lambda.has( field.access , AStatic ); - //Arguments - var argsCount : Int = f.args.length; - var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; - - //JNI Arguments translation - var signature = JniTools.getSignature(field); + var argsCount : Int = f.args.length; + var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; + var signature = JniTools.getSignature(field); - //For non static we convert the first argument type to dynamic - if ( !isStaticMethod ) - f.args[ 0 ].type = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); + if ( !isStaticMethod ) + f.args[ 0 ].type = TPath({name:"Dynamic", pack:[], + params:[], sub:null}); - //Verbose - - Sys.println('[MIRROR] - JNI $packageName::$variableName $signature'); - #if verbose_mirrors - #end + + //#if verbose_mirrors + Sys.println('[JNI] $packageName::$variableName $signature'); + //#end - //Variable - var sVar_name : String = "mirror_jni_"+variableName; - var fVar = createVariable( sVar_name , f ); + var mirrorName:String = "mirror_jni_"+variableName; + var fVar = createVariable(mirrorName ,f, field.pos); - //Return response - if ( f.ret.getParameters( )[0].name != "Void") - f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); //Switching the return type to dynamic + var returnType:String = f.ret.getParameters( )[0].name; + var returnExpr = null; + if(returnType == "Void" ) + { + returnExpr = macro $i{mirrorName}($a{argumentNames}); + } + else + { + //Switching the return type to dynamic + f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); - var eRet = null; - if ( f.ret.getParameters( )[ 0 ].name == "Void" ) - { - eRet = macro $i{sVar_name}( $a{argumentNames} ); - } - else + returnExpr = macro { - eRet = macro - { - var args : Array = $a{ argumentNames }; - #if verbose_mirrors - trace( "call with args ::: "+args); - #end - return $i{sVar_name}( $a{argumentNames} ); - }; - } - - //Result + var args : Array = $a{ argumentNames }; + #if verbose_mirrors + trace( "call with args ::: "+args); + #end + return $i{mirrorName}( $a{argumentNames} ); + }; + } - f.expr = macro + f.expr = macro + { + //Already loaded ? + if ($i{mirrorName} == null) { + #if verbose_mirrors + trace("Lib not loaded, loading it"); + trace( $v{ packageName } + " :: " + $v{mirrorName} + + ' :: signature '+$v{ signature } ); + #end - //Already loaded ? - if ( $i{ sVar_name } == null ) - { - #if verbose_mirrors - trace("Lib not loaded, loading it"); - trace( $v{ packageName }+"::"+$v{ variableName }+' :: signature '+$v{ signature } ); - #end - - // - if ( $v{ isStaticMethod } ) - $i{ sVar_name } = openfl.utils.JNI.createStaticMethod( - $v{packageName}, - $v{variableName}, - $v{signature} - ); - else - $i{ sVar_name } = openfl.utils.JNI.createMemberMethod( - $v{packageName}, - $v{variableName}, - $v{signature} - ); - - - } - - //Making the call - $eRet; + if ($v{isStaticMethod}) + $i{mirrorName} = openfl.utils.JNI.createStaticMethod( + $v{packageName}, $v{variableName}, $v{signature}); + else + $i{mirrorName} = openfl.utils.JNI.createMemberMethod( + $v{packageName}, $v{variableName}, $v{signature}); } - - - return fVar; - } - - static function _translateType( tp : Null ) : String{ - - var c : ClassType; - return switch( tp ){ - - case TAbstract( cf , a ): - _jniAbstract_type( cf.get( ) ); - - case TDynamic( t ): - "Lorg/haxe/nme/HaxeObject;"; - - default: - c = tp.getParameters( )[ 0 ].get( ); - switch( c.name ){ - - case "String": - "Ljava/lang/String;"; - - case "Array": - "["+_translateType( tp.getParameters( )[ 1 ][0] ); - - default: - "L"+c.pack.join("/")+( c.pack.length == 0 ? "" : "/" ) + c.name+";"; - - } + + $returnExpr; } - } - - static function _jniAbstract_type( cf : AbstractType ) : String{ - - return switch( cf.name ){ - - case "Float": - "F"; - - case "Bool": - "Z"; - case "Int": - "I"; - - case "Void": - "V"; - - default: - Context.error("Unknow abstract type ::: "+cf.name , Context.currentPos( ) ); - - } + return fVar; } static function cpp( field:Field, packageName : String, ?name : String ) : Field{ @@ -225,30 +156,29 @@ class MacroMirrors var argsCount : Int = f.args.length; var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; - trace( '[MIRROR] - CPP $packageName::'+field.name+'($argsCount)' ); #if verbose_mirrors + Sys.println('[CPP] $packageName::'+field.name+'($argsCount)'); #end - var variableName : String = "mirror_cpp_"+name; - var fieldVariable = createVariable(variableName, f); - + var mirrorName : String = "mirror_cpp_"+name; + var fieldVariable = createVariable(mirrorName, f, field.pos); var returnExpr = macro ""; if ( f.ret.getParameters( )[ 0 ].name == "Void" ) - returnExpr = macro $i{variableName}( $a{argumentNames} ); + returnExpr = macro $i{mirrorName}($a{argumentNames}); else - returnExpr = macro return $i{variableName}( $a{argumentNames} ); + returnExpr = macro return $i{mirrorName}($a{argumentNames}); f.expr = macro { - if ($i{variableName} == null) + if ($i{mirrorName} == null) { #if verbose_mirrors trace("Lib not loaded, loading it"); trace($v{packageName}+"::"+$v{name}+'($argsCount)'); #end - $i{ variableName } = cpp.Lib.load($v{packageName}, $v{name}, + $i{mirrorName} = cpp.Lib.load($v{packageName}, $v{name}, $v{argsCount}); } $returnExpr; @@ -257,18 +187,18 @@ class MacroMirrors return fieldVariable; } - static function createVariable(name:String, f:Function):Field + static function createVariable(variableName:String, refFunction:Function, positon:Position):Field { - var aTypes : Array> = [ for ( a in f.args ) a.type ]; - var k : FieldType = FVar(TFunction(aTypes , f.ret)); + var types = [for (arg in refFunction.args) arg.type]; + var fieldType : FieldType = FVar(TFunction(types, refFunction.ret)); return { - name : name , + name : variableName, doc : null, meta : [], access : [APublic,AStatic], - kind : k , - pos : haxe.macro.Context.currentPos() + kind : fieldType, + pos : positon }; } From b338ba06c91306142ac98a9285c391d8d57410b4 Mon Sep 17 00:00:00 2001 From: shoebox Date: Mon, 2 Jun 2014 00:47:25 +0100 Subject: [PATCH 05/15] Refactoring --- src/org/shoebox/macros/MacroMirrors.hx | 89 ++++++++++++++++---------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index bb0fdfd..47848fb 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -28,54 +28,77 @@ using haxe.macro.Tools; */ class MacroMirrors { + public static inline var CPP_META:String = "CPP"; + public static inline var IOS_META:String = "IOS"; + public static inline var JNI_META:String = "JNI"; + public static function build():Array { - var fields:Array = Context.getBuildFields( ); - var localClass:Null> = Context.getLocalClass( ); - + var localClass:ClassType = Context.getLocalClass( ).get(); if (!Context.defined("openfl")) return fields; - var metadatas : Array; - + var result:Field; for (field in fields.copy()) { - metadatas = [for (m in field.meta ) - if (m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m]; - - if ( metadatas.length == 0 ) - continue; - - for (m in metadatas) + result = parseField(field, localClass); + if(result != null) { - if (m.name == "CPP" && Context.defined("cpp")) - { - fields.push( cpp(field, - ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).name, - ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name - ) ); - } - else if (m.name == "JNI" && Context.defined("android")) - { - fields.push(jni(field, - ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).module, - ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name - ) ); - } - else if (m.name == "IOS" && Context.defined("ios")) - { - fields.push( cpp(field, - ( m.params.length > 0 ) ? getString( m.params[ 0 ] ) : localClass.get( ).module, - ( m.params.length > 1 ) ? getString( m.params[ 1 ] ) : field.name - ) ); - } + fields.push(result); + result = null; } } return fields; } + static function parseField(field:Field, localClass:ClassType):Field + { + var metadatas = [for (m in field.meta ) + if (m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m]; + + if ( metadatas.length == 0 ) + return null; + + var result:Field; + var meta:MetadataEntry; + var metaLength:Int; + + if(MetaDataTools.has(field, CPP_META) && Context.defined("cpp")) + { + meta = MetaDataTools.get(field, CPP_META); + metaLength = meta.params.length; + + result = cpp(field, + (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.name, + (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name + ); + } + else if(MetaDataTools.has(field, JNI_META) && Context.defined("android")) + { + meta = MetaDataTools.get(field, JNI_META); + metaLength = meta.params.length; + + result = jni(field, + (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, + (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name + ); + } + else if(MetaDataTools.has(field, IOS_META) && Context.defined("ios")) + { + meta = MetaDataTools.get(field, IOS_META); + metaLength = meta.params.length; + + result = cpp(field, + (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, + (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name + ); + } + + return result; + } + static function jni(field:Field, packageName:String, ?variableName:String ):Field { From f66a50338487d86503c50df9eae1fc0dc163b5a1 Mon Sep 17 00:00:00 2001 From: shoebox Date: Mon, 2 Jun 2014 00:53:59 +0100 Subject: [PATCH 06/15] Meta argument count tester --- src/org/shoebox/macros/MacroMirrors.hx | 39 +++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 47848fb..8a85be2 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -55,12 +55,6 @@ class MacroMirrors static function parseField(field:Field, localClass:ClassType):Field { - var metadatas = [for (m in field.meta ) - if (m.name == "CPP" || m.name == "JNI" || m.name == "IOS" ) m]; - - if ( metadatas.length == 0 ) - return null; - var result:Field; var meta:MetadataEntry; var metaLength:Int; @@ -69,6 +63,7 @@ class MacroMirrors { meta = MetaDataTools.get(field, CPP_META); metaLength = meta.params.length; + checkMetaArgsCount(meta, 2, 2); result = cpp(field, (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.name, @@ -79,6 +74,7 @@ class MacroMirrors { meta = MetaDataTools.get(field, JNI_META); metaLength = meta.params.length; + checkMetaArgsCount(meta, 0, 2); result = jni(field, (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, @@ -89,6 +85,7 @@ class MacroMirrors { meta = MetaDataTools.get(field, IOS_META); metaLength = meta.params.length; + checkMetaArgsCount(meta, 0, 2); result = cpp(field, (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, @@ -99,6 +96,15 @@ class MacroMirrors return result; } + static function checkMetaArgsCount(meta:MetadataEntry, min:Int, max:Int):Void + { + var metaName = meta.name; + var count = meta.params.length; + if (count > max || count < min) + Context.error('Invalid arguments count for the meta $metaName', + meta.pos); + } + static function jni(field:Field, packageName:String, ?variableName:String ):Field { @@ -179,15 +185,15 @@ class MacroMirrors var argsCount : Int = f.args.length; var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; - #if verbose_mirrors + //#if verbose_mirrors Sys.println('[CPP] $packageName::'+field.name+'($argsCount)'); - #end + //#end var mirrorName : String = "mirror_cpp_"+name; var fieldVariable = createVariable(mirrorName, f, field.pos); var returnExpr = macro ""; - if ( f.ret.getParameters( )[ 0 ].name == "Void" ) + if (f.ret.getParameters( )[ 0 ].name == "Void") returnExpr = macro $i{mirrorName}($a{argumentNames}); else returnExpr = macro return $i{mirrorName}($a{argumentNames}); @@ -215,7 +221,8 @@ class MacroMirrors var types = [for (arg in refFunction.args) arg.type]; var fieldType : FieldType = FVar(TFunction(types, refFunction.ret)); - return { + return + { name : variableName, doc : null, meta : [], @@ -231,7 +238,8 @@ class MacroMirrors if ( e == null ) return null; - return switch( e.expr.getParameters( )[ 0 ] ){ + return switch( e.expr.getParameters( )[ 0 ] ) + { case CString( s ): s; @@ -240,19 +248,18 @@ class MacroMirrors null; } - } static function getFunc(f:Field):Function { - return switch( f.kind ){ + return switch(f.kind) + { - case FFun( f ): + case FFun(f): f; default: Context.error("Only function are supported",f.pos); - } } @@ -371,7 +378,7 @@ class FieldTool result = f; default: - Context.error("Only function are supported",field.pos); + Context.error("Only function are supported", field.pos); } return result; From 57bcf594c796e6687577a986d2e11305c730e491 Mon Sep 17 00:00:00 2001 From: shoebox Date: Mon, 2 Jun 2014 01:23:09 +0100 Subject: [PATCH 07/15] Reusing code --- src/org/shoebox/macros/MacroMirrors.hx | 83 +++++++++++++------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 8a85be2..57ce098 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -85,7 +85,7 @@ class MacroMirrors { meta = MetaDataTools.get(field, IOS_META); metaLength = meta.params.length; - checkMetaArgsCount(meta, 0, 2); + checkMetaArgsCount(meta, 2, 2); result = cpp(field, (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, @@ -110,38 +110,31 @@ class MacroMirrors { packageName = packageName.split(".").join("/"); - var f : Function = FieldTool.getFunction(field); - if (f.ret == null) - f.ret = TPath({ name : "Void", pack : [], params : [] }); - - var isStaticMethod = Lambda.has( field.access , AStatic ); - - var argsCount : Int = f.args.length; - var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; + var result:Function = FieldTool.getFunction(field); + if (result.ret == null) + result.ret = TPath({name:"Void", pack:[], params:[] }); + + var argumentNames:Array = getArgsNames(result); var signature = JniTools.getSignature(field); - if ( !isStaticMethod ) - f.args[ 0 ].type = TPath({name:"Dynamic", pack:[], + if (!isStaticField(field)) + result.args[ 0 ].type = TPath({name:"Dynamic", pack:[], params:[], sub:null}); - - //#if verbose_mirrors + #if verbose_mirrors Sys.println('[JNI] $packageName::$variableName $signature'); - //#end + #end var mirrorName:String = "mirror_jni_"+variableName; - var fVar = createVariable(mirrorName ,f, field.pos); - - var returnType:String = f.ret.getParameters( )[0].name; + var resultVariable = createVariable(mirrorName, result, field.pos); + var returnType:String = result.ret.getParameters( )[0].name; var returnExpr = null; - if(returnType == "Void" ) - { - returnExpr = macro $i{mirrorName}($a{argumentNames}); - } - else + var isStaticMethod = isStaticField(field); + + if(returnType != "Void") { //Switching the return type to dynamic - f.ret = TPath({ name : "Dynamic" , pack : [], params : [], sub : null }); + result.ret = TPath({name:"Dynamic", pack:[], params:[], sub:null}); returnExpr = macro { @@ -152,16 +145,16 @@ class MacroMirrors return $i{mirrorName}( $a{argumentNames} ); }; } + else + returnExpr = macro $i{mirrorName}($a{argumentNames}); - f.expr = macro + result.expr = macro { - //Already loaded ? if ($i{mirrorName} == null) { #if verbose_mirrors trace("Lib not loaded, loading it"); - trace( $v{ packageName } + " :: " + $v{mirrorName} - + ' :: signature '+$v{ signature } ); + trace($v{packageName} + " :: " + $v{mirrorName} + ' :: signature '+$v{signature}); #end if ($v{isStaticMethod}) @@ -175,30 +168,31 @@ class MacroMirrors $returnExpr; } - return fVar; + return resultVariable; } - static function cpp( field:Field, packageName : String, ?name : String ) : Field{ + static function cpp(field:Field, packageName:String, ?name:String ) : Field + { - var f:Function = getFunc( field ); + var func:Function = getFunc(field); - var argsCount : Int = f.args.length; - var argumentNames : Array = [ for ( a in f.args ) macro $i{ a.name } ]; + var argsCount:Int = func.args.length; + var argumentNames:Array = getArgsNames(func); - //#if verbose_mirrors + #if verbose_mirrors Sys.println('[CPP] $packageName::'+field.name+'($argsCount)'); - //#end + #end var mirrorName : String = "mirror_cpp_"+name; - var fieldVariable = createVariable(mirrorName, f, field.pos); + var fieldVariable = createVariable(mirrorName, func, field.pos); var returnExpr = macro ""; - if (f.ret.getParameters( )[ 0 ].name == "Void") + if (func.ret.getParameters( )[ 0 ].name == "Void") returnExpr = macro $i{mirrorName}($a{argumentNames}); else returnExpr = macro return $i{mirrorName}($a{argumentNames}); - f.expr = macro + func.expr = macro { if ($i{mirrorName} == null) { @@ -263,6 +257,17 @@ class MacroMirrors } } + static inline function getArgsNames(func:Function):Array + { + var result:Array = [for (a in func.args) macro $i{ a.name }]; + return result; + } + + static inline function isStaticField(field:Field):Bool + { + var result = Lambda.has(field.access, AStatic); + return result; + } } class JniTools @@ -272,10 +277,8 @@ class JniTools var func:Function = FieldTool.getFunction(field); var signature = "("; for(arg in func.args) - { signature += translateArg(arg, field.pos); - } - + var returnType:Null = func.ret.toType(); signature += ")" + translateType(returnType, field.pos); From bf7ab6f386a5f0da6894829f2f55be746f15bb7f Mon Sep 17 00:00:00 2001 From: shoebox Date: Fri, 6 Jun 2014 09:10:22 +0100 Subject: [PATCH 08/15] CPP Metas for Default Library / Auto prefixing of primitives --- src/org/shoebox/macros/MacroMirrors.hx | 76 ++++++++++++++++++-------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 57ce098..be58b90 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -31,14 +31,23 @@ class MacroMirrors public static inline var CPP_META:String = "CPP"; public static inline var IOS_META:String = "IOS"; public static inline var JNI_META:String = "JNI"; + public static inline var CPP_DEFAULT_LIB:String = "CPP_DEFAULT_LIBRARY"; + public static inline var CPP_PRIM_PREFIX:String = "CPP_PRIMITIVE_PREFIX"; + + + static var VOID = TPath({name:"Void", pack:[], params:[] }); + static var DYNAMIC = TPath({name:"Dynamic", pack:[], params:[], sub:null}); public static function build():Array { var fields:Array = Context.getBuildFields( ); - var localClass:ClassType = Context.getLocalClass( ).get(); + if (!Context.defined("openfl")) return fields; + var localClass:ClassType = Context.getLocalClass( ).get(); + var config = parseConfig(localClass); + var result:Field; for (field in fields.copy()) { @@ -53,15 +62,30 @@ class MacroMirrors return fields; } + static function parseConfig(localClass:ClassType):ContextConfig + { + var config:ContextConfig = {}; + var metas:Metadata = localClass.meta.get(); + if(MetaDataTools.has(metas, CPP_DEFAULT_LIB)) + config.cppDefaultLibrary = MetaDataTools.get(metas, + CPP_DEFAULT_LIB).params[0].toString(); + + if(MetaDataTools.has(metas, CPP_PRIM_PREFIX)) + config.cppPrimitivePrefix = MetaDataTools.get(metas, + CPP_PRIM_PREFIX).params[0].toString(); + + return config; + } + static function parseField(field:Field, localClass:ClassType):Field { var result:Field; var meta:MetadataEntry; var metaLength:Int; - if(MetaDataTools.has(field, CPP_META) && Context.defined("cpp")) + if(MetaDataTools.has(field.meta, CPP_META) && Context.defined("cpp")) { - meta = MetaDataTools.get(field, CPP_META); + meta = MetaDataTools.get(field.meta, CPP_META); metaLength = meta.params.length; checkMetaArgsCount(meta, 2, 2); @@ -70,9 +94,9 @@ class MacroMirrors (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name ); } - else if(MetaDataTools.has(field, JNI_META) && Context.defined("android")) + else if(MetaDataTools.has(field.meta, JNI_META) && Context.defined("android")) { - meta = MetaDataTools.get(field, JNI_META); + meta = MetaDataTools.get(field.meta, JNI_META); metaLength = meta.params.length; checkMetaArgsCount(meta, 0, 2); @@ -81,9 +105,9 @@ class MacroMirrors (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name ); } - else if(MetaDataTools.has(field, IOS_META) && Context.defined("ios")) + else if(MetaDataTools.has(field.meta, IOS_META) && Context.defined("ios")) { - meta = MetaDataTools.get(field, IOS_META); + meta = MetaDataTools.get(field.meta, IOS_META); metaLength = meta.params.length; checkMetaArgsCount(meta, 2, 2); @@ -112,20 +136,19 @@ class MacroMirrors var result:Function = FieldTool.getFunction(field); if (result.ret == null) - result.ret = TPath({name:"Void", pack:[], params:[] }); + result.ret = VOID; var argumentNames:Array = getArgsNames(result); var signature = JniTools.getSignature(field); if (!isStaticField(field)) - result.args[ 0 ].type = TPath({name:"Dynamic", pack:[], - params:[], sub:null}); + result.args[ 0 ].type = DYNAMIC; #if verbose_mirrors Sys.println('[JNI] $packageName::$variableName $signature'); #end - var mirrorName:String = "mirror_jni_"+variableName; + var mirrorName:String = getMirrorName(variableName, "jni"); var resultVariable = createVariable(mirrorName, result, field.pos); var returnType:String = result.ret.getParameters( )[0].name; var returnExpr = null; @@ -134,7 +157,7 @@ class MacroMirrors if(returnType != "Void") { //Switching the return type to dynamic - result.ret = TPath({name:"Dynamic", pack:[], params:[], sub:null}); + result.ret = DYNAMIC; returnExpr = macro { @@ -154,7 +177,8 @@ class MacroMirrors { #if verbose_mirrors trace("Lib not loaded, loading it"); - trace($v{packageName} + " :: " + $v{mirrorName} + ' :: signature '+$v{signature}); + trace($v{packageName} + " :: " + $v{mirrorName} + + ' :: signature '+$v{signature}); #end if ($v{isStaticMethod}) @@ -171,6 +195,11 @@ class MacroMirrors return resultVariable; } + static function getMirrorName(name:String, target:String = "cpp"):String + { + return 'mirror_' + target + '_' + name; + } + static function cpp(field:Field, packageName:String, ?name:String ) : Field { @@ -183,7 +212,7 @@ class MacroMirrors Sys.println('[CPP] $packageName::'+field.name+'($argsCount)'); #end - var mirrorName : String = "mirror_cpp_"+name; + var mirrorName : String = getMirrorName(name, "cpp"); var fieldVariable = createVariable(mirrorName, func, field.pos); var returnExpr = macro ""; @@ -210,7 +239,8 @@ class MacroMirrors return fieldVariable; } - static function createVariable(variableName:String, refFunction:Function, positon:Position):Field + static function createVariable(variableName:String, refFunction:Function, + positon:Position):Field { var types = [for (arg in refFunction.args) arg.type]; var fieldType : FieldType = FVar(TFunction(types, refFunction.ret)); @@ -228,19 +258,16 @@ class MacroMirrors static function getString(e:Expr):String { - if ( e == null ) return null; return switch( e.expr.getParameters( )[ 0 ] ) { - case CString( s ): s; default: null; - } } @@ -392,11 +419,11 @@ class FieldTool class MetaDataTools { - public static function has(field:Field, metaName:String):Bool + public static function has(metas:Metadata, metaName:String):Bool { var result = false; - for(meta in field.meta) + for(meta in metas) { if (meta.name == metaName) { @@ -408,10 +435,10 @@ class MetaDataTools return result; } - public static function get(field:Field, metaName:String):MetadataEntry + public static function get(metas:Metadata, metaName:String):MetadataEntry { var result:MetadataEntry = null; - for(meta in field.meta) + for(meta in metas) { if (meta.name == metaName) { @@ -422,5 +449,10 @@ class MetaDataTools return result; } +} +typedef ContextConfig= +{ + @:optional var cppPrimitivePrefix:String; + @:optional var cppDefaultLibrary:String; } From 9e59e8caec682faab6dba637e9574266ff0fa2fc Mon Sep 17 00:00:00 2001 From: shoebox Date: Fri, 6 Jun 2014 15:48:28 +0200 Subject: [PATCH 09/15] CPP Default Library & Prefix --- src/org/shoebox/macros/MacroMirrors.hx | 71 ++++++++++++++++++-------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index be58b90..73310eb 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -51,7 +51,7 @@ class MacroMirrors var result:Field; for (field in fields.copy()) { - result = parseField(field, localClass); + result = parseField(field, localClass, config); if(result != null) { fields.push(result); @@ -67,32 +67,61 @@ class MacroMirrors var config:ContextConfig = {}; var metas:Metadata = localClass.meta.get(); if(MetaDataTools.has(metas, CPP_DEFAULT_LIB)) - config.cppDefaultLibrary = MetaDataTools.get(metas, - CPP_DEFAULT_LIB).params[0].toString(); + config.cppDefaultLibrary = getString( MetaDataTools.get(metas, + CPP_DEFAULT_LIB).params[0]); if(MetaDataTools.has(metas, CPP_PRIM_PREFIX)) - config.cppPrimitivePrefix = MetaDataTools.get(metas, - CPP_PRIM_PREFIX).params[0].toString(); + config.cppPrimitivePrefix = getString( MetaDataTools.get(metas, + CPP_PRIM_PREFIX).params[0]); return config; } - static function parseField(field:Field, localClass:ClassType):Field + static function getLibraryName(field:Field, meta:MetadataEntry, metaLength:Int, + config:ContextConfig):String + { + var result:String; + + if(metaLength == 0 && config.cppDefaultLibrary == null) + Context.error('The primitive name is not defined, and not ' + + ' for field ${field.name}', field.pos); + + if(config.cppDefaultLibrary != null && metaLength == 0) + result = config.cppDefaultLibrary; + else + result = getString(meta.params[0]); + + return result; + } + + static function getPrimitiveName(field:Field, meta:MetadataEntry, metaLength:Int, + config:ContextConfig):String + { + if(metaLength == 2) + return meta.params[1].toString(); + + return (config.cppPrimitivePrefix != null ? + config.cppPrimitivePrefix + "_" : "") + field.name; + } + + static function parseField(field:Field, localClass:ClassType, + config:ContextConfig):Field { var result:Field; var meta:MetadataEntry; var metaLength:Int; + var libraryName:String; + var primiveName:String; if(MetaDataTools.has(field.meta, CPP_META) && Context.defined("cpp")) { meta = MetaDataTools.get(field.meta, CPP_META); metaLength = meta.params.length; - checkMetaArgsCount(meta, 2, 2); - result = cpp(field, - (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.name, - (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name - ); + libraryName = getLibraryName(field, meta, metaLength, config); + primiveName = getPrimitiveName(field, meta, metaLength, config); + + result = cpp(field, libraryName, primiveName, "CPP"); } else if(MetaDataTools.has(field.meta, JNI_META) && Context.defined("android")) { @@ -109,12 +138,10 @@ class MacroMirrors { meta = MetaDataTools.get(field.meta, IOS_META); metaLength = meta.params.length; - checkMetaArgsCount(meta, 2, 2); - result = cpp(field, - (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, - (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name - ); + libraryName = getLibraryName(field, meta, metaLength, config); + primiveName = getPrimitiveName(field, meta, metaLength, config); + result = cpp(field, libraryName, primiveName, "IOS"); } return result; @@ -200,22 +227,22 @@ class MacroMirrors return 'mirror_' + target + '_' + name; } - static function cpp(field:Field, packageName:String, ?name:String ) : Field + static function cpp(field:Field, packageName:String, ?name:String, + ?type:String ) : Field { - var func:Function = getFunc(field); var argsCount:Int = func.args.length; var argumentNames:Array = getArgsNames(func); - #if verbose_mirrors - Sys.println('[CPP] $packageName::'+field.name+'($argsCount)'); - #end - var mirrorName : String = getMirrorName(name, "cpp"); var fieldVariable = createVariable(mirrorName, func, field.pos); var returnExpr = macro ""; + #if verbose_mirrors + Sys.println('[$type] $packageName::$name ($argsCount)'); + #end + if (func.ret.getParameters( )[ 0 ].name == "Void") returnExpr = macro $i{mirrorName}($a{argumentNames}); else From 8d6a9ec4154d873873ddefa83bef728bb26b2c6d Mon Sep 17 00:00:00 2001 From: shoebox Date: Sat, 7 Jun 2014 20:22:39 +0200 Subject: [PATCH 10/15] Cleaning up --- src/org/shoebox/macros/MacroMirrors.hx | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 73310eb..27570e2 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -98,7 +98,7 @@ class MacroMirrors config:ContextConfig):String { if(metaLength == 2) - return meta.params[1].toString(); + return getString(meta.params[1]); return (config.cppPrimitivePrefix != null ? config.cppPrimitivePrefix + "_" : "") + field.name; @@ -230,7 +230,7 @@ class MacroMirrors static function cpp(field:Field, packageName:String, ?name:String, ?type:String ) : Field { - var func:Function = getFunc(field); + var func:Function = FieldTool.getFunction(field); var argsCount:Int = func.args.length; var argumentNames:Array = getArgsNames(func); @@ -298,19 +298,6 @@ class MacroMirrors } } - static function getFunc(f:Field):Function - { - return switch(f.kind) - { - - case FFun(f): - f; - - default: - Context.error("Only function are supported",f.pos); - } - } - static inline function getArgsNames(func:Function):Array { var result:Array = [for (a in func.args) macro $i{ a.name }]; From 49ba6450ba435fec125a98d0f39523554ed55203 Mon Sep 17 00:00:00 2001 From: shoebox Date: Sat, 7 Jun 2014 20:33:02 +0200 Subject: [PATCH 11/15] A bit of cleanup --- src/org/shoebox/macros/MacroMirrors.hx | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 27570e2..60e34da 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -31,9 +31,9 @@ class MacroMirrors public static inline var CPP_META:String = "CPP"; public static inline var IOS_META:String = "IOS"; public static inline var JNI_META:String = "JNI"; - public static inline var CPP_DEFAULT_LIB:String = "CPP_DEFAULT_LIBRARY"; - public static inline var CPP_PRIM_PREFIX:String = "CPP_PRIMITIVE_PREFIX"; + public static inline var TAG_CPP_DEFAULT_LIB:String = "CPP_DEFAULT_LIBRARY"; + public static inline var TAG_CPP_PRIM_PREFIX:String = "CPP_PRIMITIVE_PREFIX"; static var VOID = TPath({name:"Void", pack:[], params:[] }); static var DYNAMIC = TPath({name:"Dynamic", pack:[], params:[], sub:null}); @@ -66,13 +66,13 @@ class MacroMirrors { var config:ContextConfig = {}; var metas:Metadata = localClass.meta.get(); - if(MetaDataTools.has(metas, CPP_DEFAULT_LIB)) - config.cppDefaultLibrary = getString( MetaDataTools.get(metas, - CPP_DEFAULT_LIB).params[0]); + if(MetaDataTools.has(metas, TAG_CPP_DEFAULT_LIB)) + config.cppDefaultLibrary = getString(MetaDataTools.get(metas, + TAG_CPP_DEFAULT_LIB).params[0]); - if(MetaDataTools.has(metas, CPP_PRIM_PREFIX)) - config.cppPrimitivePrefix = getString( MetaDataTools.get(metas, - CPP_PRIM_PREFIX).params[0]); + if(MetaDataTools.has(metas, TAG_CPP_PRIM_PREFIX)) + config.cppPrimitivePrefix = getString(MetaDataTools.get(metas, + TAG_CPP_PRIM_PREFIX).params[0]); return config; } @@ -188,11 +188,11 @@ class MacroMirrors returnExpr = macro { - var args : Array = $a{ argumentNames }; + var args:Array = $a{ argumentNames}; #if verbose_mirrors trace( "call with args ::: "+args); #end - return $i{mirrorName}( $a{argumentNames} ); + return $i{mirrorName}($a{argumentNames}); }; } else @@ -424,10 +424,8 @@ class FieldTool default: Context.error("Only function are supported", field.pos); } - return result; } - } class MetaDataTools @@ -436,7 +434,6 @@ class MetaDataTools public static function has(metas:Metadata, metaName:String):Bool { var result = false; - for(meta in metas) { if (meta.name == metaName) @@ -445,7 +442,6 @@ class MetaDataTools break; } } - return result; } @@ -460,7 +456,6 @@ class MetaDataTools break; } } - return result; } } From 74ab5d53a44ef23772272ba8e68de365fb3887f9 Mon Sep 17 00:00:00 2001 From: shoebox Date: Sat, 7 Jun 2014 20:47:56 +0200 Subject: [PATCH 12/15] Better error message --- src/org/shoebox/macros/MacroMirrors.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 60e34da..4d681d5 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -83,8 +83,8 @@ class MacroMirrors var result:String; if(metaLength == 0 && config.cppDefaultLibrary == null) - Context.error('The primitive name is not defined, and not ' - + ' for field ${field.name}', field.pos); + Context.error('The primitive name is not defined for field' + + '${field.name} and no CPP_DEFAULT_LIBRARY setup', field.pos); if(config.cppDefaultLibrary != null && metaLength == 0) result = config.cppDefaultLibrary; From d890f66ff0520cc38ac92516ae152dc880bef96d Mon Sep 17 00:00:00 2001 From: shoebox Date: Sun, 8 Jun 2014 00:09:30 +0200 Subject: [PATCH 13/15] Cleanup and better syntax --- src/org/shoebox/macros/MacroMirrors.hx | 68 ++++++++++++-------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 4d681d5..db9d81a 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -28,6 +28,8 @@ using haxe.macro.Tools; */ class MacroMirrors { + #if macro + public static inline var CPP_META:String = "CPP"; public static inline var IOS_META:String = "IOS"; public static inline var JNI_META:String = "JNI"; @@ -52,7 +54,7 @@ class MacroMirrors for (field in fields.copy()) { result = parseField(field, localClass, config); - if(result != null) + if (result != null) { fields.push(result); result = null; @@ -66,11 +68,11 @@ class MacroMirrors { var config:ContextConfig = {}; var metas:Metadata = localClass.meta.get(); - if(MetaDataTools.has(metas, TAG_CPP_DEFAULT_LIB)) + if (MetaDataTools.has(metas, TAG_CPP_DEFAULT_LIB)) config.cppDefaultLibrary = getString(MetaDataTools.get(metas, TAG_CPP_DEFAULT_LIB).params[0]); - if(MetaDataTools.has(metas, TAG_CPP_PRIM_PREFIX)) + if (MetaDataTools.has(metas, TAG_CPP_PRIM_PREFIX)) config.cppPrimitivePrefix = getString(MetaDataTools.get(metas, TAG_CPP_PRIM_PREFIX).params[0]); @@ -82,11 +84,11 @@ class MacroMirrors { var result:String; - if(metaLength == 0 && config.cppDefaultLibrary == null) + if (metaLength == 0 && config.cppDefaultLibrary == null) Context.error('The primitive name is not defined for field' + '${field.name} and no CPP_DEFAULT_LIBRARY setup', field.pos); - if(config.cppDefaultLibrary != null && metaLength == 0) + if (config.cppDefaultLibrary != null && metaLength == 0) result = config.cppDefaultLibrary; else result = getString(meta.params[0]); @@ -97,7 +99,7 @@ class MacroMirrors static function getPrimitiveName(field:Field, meta:MetadataEntry, metaLength:Int, config:ContextConfig):String { - if(metaLength == 2) + if (metaLength == 2) return getString(meta.params[1]); return (config.cppPrimitivePrefix != null ? @@ -113,7 +115,7 @@ class MacroMirrors var libraryName:String; var primiveName:String; - if(MetaDataTools.has(field.meta, CPP_META) && Context.defined("cpp")) + if (MetaDataTools.has(field.meta, CPP_META) && Context.defined("cpp")) { meta = MetaDataTools.get(field.meta, CPP_META); metaLength = meta.params.length; @@ -123,18 +125,17 @@ class MacroMirrors result = cpp(field, libraryName, primiveName, "CPP"); } - else if(MetaDataTools.has(field.meta, JNI_META) && Context.defined("android")) + else if (MetaDataTools.has(field.meta, JNI_META) && Context.defined("android")) { meta = MetaDataTools.get(field.meta, JNI_META); metaLength = meta.params.length; - checkMetaArgsCount(meta, 0, 2); - + result = jni(field, - (metaLength > 0) ? getString(meta.params[ 0 ]) : localClass.module, - (metaLength > 1) ? getString(meta.params[ 1 ]) : field.name + (metaLength > 0) ? getString(meta.params[0]) : localClass.module, + (metaLength > 1) ? getString(meta.params[1]) : field.name ); } - else if(MetaDataTools.has(field.meta, IOS_META) && Context.defined("ios")) + else if (MetaDataTools.has(field.meta, IOS_META) && Context.defined("ios")) { meta = MetaDataTools.get(field.meta, IOS_META); metaLength = meta.params.length; @@ -147,15 +148,6 @@ class MacroMirrors return result; } - static function checkMetaArgsCount(meta:MetadataEntry, min:Int, max:Int):Void - { - var metaName = meta.name; - var count = meta.params.length; - if (count > max || count < min) - Context.error('Invalid arguments count for the meta $metaName', - meta.pos); - } - static function jni(field:Field, packageName:String, ?variableName:String ):Field { @@ -169,10 +161,10 @@ class MacroMirrors var signature = JniTools.getSignature(field); if (!isStaticField(field)) - result.args[ 0 ].type = DYNAMIC; + result.args[0].type = DYNAMIC; #if verbose_mirrors - Sys.println('[JNI] $packageName::$variableName $signature'); + Sys.println('[JNI] $packageName \t $variableName $signature'); #end var mirrorName:String = getMirrorName(variableName, "jni"); @@ -181,16 +173,16 @@ class MacroMirrors var returnExpr = null; var isStaticMethod = isStaticField(field); - if(returnType != "Void") + if (returnType != "Void") { //Switching the return type to dynamic result.ret = DYNAMIC; returnExpr = macro { - var args:Array = $a{ argumentNames}; #if verbose_mirrors - trace( "call with args ::: "+args); + var args:Array = $a{argumentNames}; + trace( "call with args ::: " + args); #end return $i{mirrorName}($a{argumentNames}); }; @@ -235,12 +227,12 @@ class MacroMirrors var argsCount:Int = func.args.length; var argumentNames:Array = getArgsNames(func); - var mirrorName : String = getMirrorName(name, "cpp"); + var mirrorName:String = getMirrorName(name, "cpp"); var fieldVariable = createVariable(mirrorName, func, field.pos); var returnExpr = macro ""; #if verbose_mirrors - Sys.println('[$type] $packageName::$name ($argsCount)'); + Sys.println('[$type] $packageName \t $name ($argsCount)'); #end if (func.ret.getParameters( )[ 0 ].name == "Void") @@ -285,12 +277,12 @@ class MacroMirrors static function getString(e:Expr):String { - if ( e == null ) + if (e == null) return null; - return switch( e.expr.getParameters( )[ 0 ] ) + return switch ( e.expr.getParameters( )[ 0 ] ) { - case CString( s ): + case CString(s): s; default: @@ -335,7 +327,7 @@ class JniTools public static function translateType(argType:Null, pos:Position):String { - return switch(argType) + return switch (argType) { case TAbstract(cf, a ): translateAbstractType(cf.get(), pos); @@ -353,7 +345,7 @@ class JniTools public static function translateArgType(type:Null, pos:Position):String { - return switch(type) + return switch (type) { case TInst(t, params): translateSubArgType(type, params, pos); @@ -368,7 +360,7 @@ class JniTools pos:Position):String { var result:String; - switch(type.getParameters()[0].get().name) + switch (type.getParameters()[0].get().name) { case "String": result = "Ljava/lang/String;"; @@ -389,7 +381,7 @@ class JniTools public static function translateAbstractType(a:AbstractType, pos:Position):String { var result:String = null; - result = switch(a.name) + result = switch (a.name) { case "Float": "F"; @@ -416,7 +408,7 @@ class FieldTool public static function getFunction(field:Field):Function { var result:Function; - switch(field.kind) + switch (field.kind) { case FFun(f): result = f; @@ -458,6 +450,8 @@ class MetaDataTools } return result; } + + #end } typedef ContextConfig= From 0b027a738ac3dd3596ca5380008b5609f16542f5 Mon Sep 17 00:00:00 2001 From: shoebox Date: Wed, 11 Jun 2014 15:24:13 +0100 Subject: [PATCH 14/15] Cleaning up --- src/org/shoebox/macros/MacroMirrors.hx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index db9d81a..9dce720 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -231,6 +231,7 @@ class MacroMirrors var fieldVariable = createVariable(mirrorName, func, field.pos); var returnExpr = macro ""; + #if verbose_mirrors Sys.println('[$type] $packageName \t $name ($argsCount)'); #end From 9f242c63ba173c1886e6cf87f4a1e73e3cf7802b Mon Sep 17 00:00:00 2001 From: shoebox Date: Fri, 13 Jun 2014 10:40:59 +0100 Subject: [PATCH 15/15] Haxelib update --- haxelib.json | 4 ++-- src/org/shoebox/macros/MacroMirrors.hx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/haxelib.json b/haxelib.json index fa4b73d..b0c937e 100644 --- a/haxelib.json +++ b/haxelib.json @@ -5,7 +5,7 @@ "tags": ["hyperfiction", "macro","shoebox","openfl"], "description": "Essential macros for native extensions", "classPath":"src/", - "version": "1.0.4", - "releasenote": "The library is now usable as a haxelib (missing 'classPath' node)", + "version": "1.1.0", + "releasenote": "Refactoring | CPP default primtive prefix | CPP default library", "contributors": ["shoebox"] } \ No newline at end of file diff --git a/src/org/shoebox/macros/MacroMirrors.hx b/src/org/shoebox/macros/MacroMirrors.hx index 9dce720..576929c 100644 --- a/src/org/shoebox/macros/MacroMirrors.hx +++ b/src/org/shoebox/macros/MacroMirrors.hx @@ -196,7 +196,7 @@ class MacroMirrors { #if verbose_mirrors trace("Lib not loaded, loading it"); - trace($v{packageName} + " :: " + $v{mirrorName} + trace($v{packageName} + " :: " + $v{variableName} + ' :: signature '+$v{signature}); #end