Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite getPackageName() to reduce the chance of errors #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mirror/Jni.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mirror;

#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
Expand Down Expand Up @@ -102,3 +103,4 @@ class Jni
return 'mirror_jni_$name';
}
}
#end
217 changes: 98 additions & 119 deletions src/mirror/JniFieldTool.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mirror;
package mirror;

#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
Expand All @@ -11,125 +12,101 @@ using tools.ExprTool;
using tools.FieldTool;
using tools.MetadataTools;

class JniFieldTool
{
public static inline var TagJni = 'JNI';
public static inline var TagDefaultClassName = 'JNI_DEFAULT_CLASS_NAME';
public static inline var TagDefaultLibrary = 'JNI_DEFAULT_PACKAGE';

public static function isJni(field:Field):Bool
{
return field.meta.has(TagJni);
}

public static function getPackageName(field:Field):String
{
var result:String;
var metas = Context.getLocalClass().get().meta.get();
var entry:MetadataEntry = field.meta.get(TagJni);
var hasFileName:Bool;
if (metas.has(TagDefaultLibrary))
{
if (metas.get(TagDefaultLibrary).params.length == 0)
{
#if (haxe_ver >= 3.1)
Context.fatalError("Default package is defined "
+ "(JNI_DEFAULT_PACKAGE) without argument", field.pos);
#end
}

result = entry.params.length > 0 ? entry.params[0].getString()
: metas.get(TagDefaultLibrary).params[0].getString();
}
else
{
var params = entry.params;
if (params.length > 1)
class JniFieldTool
{
public static inline var TagJni = "JNI";
public static inline var TagDefaultClassName = "JNI_DEFAULT_CLASS_NAME";
public static inline var TagDefaultLibrary = "JNI_DEFAULT_PACKAGE";

public static function isJni(field:Field):Bool
{
return field.meta.has(TagJni);
}

public static function getPackageName(field:Field):String
{
var pack:Array<String> = null;
var className:String = null;

var classMetas = Context.getLocalClass().get().meta.get();

if (classMetas.has(TagDefaultLibrary))
{
var tag = classMetas.get(TagDefaultLibrary).params[0].getString();
if(tag != null)
{
result = params[0].getString();
pack = tag.split(".");
}
else
}

if (classMetas.has(TagDefaultClassName))
{
className = classMetas.get(TagDefaultClassName).params[0].getString();
}

if (field.meta.has(TagJni) && field.meta.get(TagJni).params.length > 1)
{
var tag = field.meta.get(TagJni).params[0].getString();
if (tag != null)
{
if (metas.has(TagDefaultClassName)
&& metas.get(TagDefaultClassName).params.length > 0)
{
hasFileName = false;
}
else
{
result = Context.getLocalModule();
hasFileName = true;
}
pack = tag.split(".");
className = pack.pop();
}
}

var splitted = result.split('.');
if (!hasFileName)
{
if (metas.has(TagDefaultClassName))
{
if (metas.get(TagDefaultClassName).params.length == 0)
{
#if (haxe_ver >= 3.1)
Context.fatalError("Default package is defined "
+ '($TagDefaultClassName) without argument', field.pos);
#end
}
else
{
splitted.push(metas.get(TagDefaultClassName).params[0].getString());
}
}
else
{
splitted.push(Context.getLocalClass().get().name);
}
}

result = splitted.join("/");

return result;
}

public static function getPrimitiveName(field:Field):String
{
var entry:MetadataEntry = field.meta.get(TagJni);
var params = entry.params;
var length = params.length;

var result = switch (length)
{
case 0 : field.name;
case 1 : entry.params[0].getString();
case 2 : entry.params[1].getString();
case _ :
#if (haxe_ver >= 3.1)
}

if (pack == null)
{
pack = Context.getLocalClass().get().pack;
}

if (className == null)
{
className = Context.getLocalClass().get().name;
}

return pack.join("/") + "/" + className;
}

public static function getPrimitiveName(field:Field):String
{
var entry:MetadataEntry = field.meta.get(TagJni);
var params = entry.params;
var length = params.length;

var result = switch (length)
{
case 0 : field.name;
case 1 : entry.params[0].getString();
case 2 : entry.params[1].getString();
case _ :
#if (haxe_ver >= 3.1)
Context.fatalError("Invalid number of arguments for the JNI tag", field.pos);
#end
}

return result;
}

public static function getSignature(field:Field):String
{
var reference = FieldTool.getFunction(field);
var result = '(';
var args = reference.args.copy();
if (!field.isStaticField())
args.shift();

for (arg in args)
{
result += translateArg(arg, field.pos);
}

var returnType:Null<Type> = reference.ret.toType();
result += ")" + translateType(returnType, field.pos);
return result;
}

public static function translateArg(arg:FunctionArg, pos:Position):String
}

return result;
}

public static function getSignature(field:Field):String
{
var reference = FieldTool.getFunction(field);
var result = "(";
var args = reference.args.copy();
if (!field.isStaticField())
args.shift();

for (arg in args)
{
result += translateArg(arg, field.pos);
}

var returnType:Null<Type> = reference.ret.toType();
result += ")" + translateType(returnType, field.pos);

return result;
}

public static function translateArg(arg:FunctionArg, pos:Position):String
{
var argType = arg.type.toType();
return translateType(argType, pos);
Expand Down Expand Up @@ -223,10 +200,10 @@ using tools.MetadataTools;
if (Context.getLocalClass().get().module == classType.module
&& metas.has(TagDefaultLibrary))
{
var entry:MetadataEntry = metas.get(TagDefaultLibrary);
var raw = entry.params[0].getString();
var parts = raw.split('.');
result = "L" + parts.join('/') + '/' + Context.getLocalClass().get().name + ';';
var entry:MetadataEntry = metas.get(TagDefaultLibrary);
var raw = entry.params[0].getString();
var parts = raw.split(".");
result = "L" + parts.join("/") + "/" + Context.getLocalClass().get().name + ';';
}
else
{
Expand All @@ -238,4 +215,6 @@ using tools.MetadataTools;

return result;
}
}
}

#end
2 changes: 2 additions & 0 deletions src/tools/FieldTool.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools;

#if macro
import haxe.macro.Context;
import haxe.macro.Expr;

Expand All @@ -25,3 +26,4 @@ class FieldTool
return result;
}
}
#end