From 5ceca8550b82591a6a2661a26d3e0d5e6e3e15ff Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 5 Jul 2023 10:21:48 +0200 Subject: GlueGen: Add 'PascalString' string semantics (length + value-ptr), added prelim code for JavaCallback use-case emitBodyMapCToJNIType() It is common in toolkit APIs that a string might not be passed as a 'nul' terminated (EOS) C string, but as a Pascal string with a given length argument. A C string is specied as ArgumentIsString alEventCallbackInject 3 while allowing multiple indices .. A Pascal string can be specified as ArgumentIsPascalString ALEVENTPROCSOFT 3 4 while allowing multiple indice-tuples for length and value .. The tuple consist of the length agrument-index first (usually an int) followed by the value argument-index (usually a 'char*'). +++ CMethodBindingEmitter.emitBodyMapCToJNIType(), where PascalString is implemented, is currently being used for - JNI return statement (no PascalString impact possible) - JavaCallback C type -> JNI type, PascalString impacting --- src/java/com/jogamp/gluegen/JavaConfiguration.java | 93 ++++++++++++++++++++-- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'src/java/com/jogamp/gluegen/JavaConfiguration.java') diff --git a/src/java/com/jogamp/gluegen/JavaConfiguration.java b/src/java/com/jogamp/gluegen/JavaConfiguration.java index 10f43a7..5cea4c3 100644 --- a/src/java/com/jogamp/gluegen/JavaConfiguration.java +++ b/src/java/com/jogamp/gluegen/JavaConfiguration.java @@ -144,12 +144,47 @@ public class JavaConfiguration { private final Map returnedArrayLengths = new HashMap(); private final Set maxOneElement = new HashSet(); + /** Pascal string argument index tuple for length and value. */ + public static class PascalStringIdx { + public final int lengthIndex; + public final int valueIndex; + + PascalStringIdx(final int lenIdx, final int valIdx) { + lengthIndex = lenIdx; + valueIndex = valIdx; + } + + public void pushValueIndex(final List indices) { + indices.add(valueIndex); + } + public static final List pushValueIndex(final List source, List indices) { + if( null == indices ) { + indices = new ArrayList(2); + } + for(final PascalStringIdx p : source) { + p.pushValueIndex(indices); + } + return indices; + } + + @Override + public String toString() { + return "PascalString[lenIdx "+lengthIndex+", valIdx "+valueIndex+"]"; + } + } + /** * Key is function that has some byte[] or short[] arguments that should be * converted to String args; value is List of Integer argument indices */ private final Map> argumentsAreString = new HashMap>(); + /** + * Key is function that has a pascal string, i.e. length and some byte[] or short[] arguments that should be + * converted to String args; value is a list of PascalStringArg + */ + private final Map> argumentsArePascalString = new HashMap>(); + /** JavaCallback configuration definition (static) */ public static class JavaCallbackDef { final String cbFuncTypeName; @@ -607,13 +642,6 @@ public class JavaConfiguration { return maxOneElement.contains(symbol); } - /** Returns a list of Integers which are the indices of const char* - arguments that should be converted to Strings. Returns null if there are no - such hints for the given function name. */ - public List stringArguments(final String functionName) { - return argumentsAreString.get(functionName); - } - /** Returns a list of Integers which are the indices of const char* arguments that should be converted to Strings. Returns null if there are no such hints for the given function alias symbol. */ @@ -632,6 +660,36 @@ public class JavaConfiguration { return res; } + /** Returns a list of PascalStringIdx which are tuples of indices of int len, const char* + arguments that should be converted to Strings. Returns null if there are no + such hints for the given function alias symbol. */ + public List pascalStringArgument(final AliasedSymbol symbol) { + final String name = symbol.getName(); + final Set aliases = symbol.getAliasedNames(); + + List res = argumentsArePascalString.get(name); + if( null == res ) { + res = oneInMap(argumentsArePascalString, aliases); + if( null == res ) { + return null; + } + } + LOG.log(INFO, getASTLocusTag(symbol), "ArgumentIsPascalString: {0} -> {1}", symbol, res); + return res; + } + + public int pascalStringLengthIndex(final AliasedSymbol symbol, final int valueIndex) { + final List pascals = pascalStringArgument(symbol); + if( null != pascals ) { + for(final PascalStringIdx p : pascals) { + if( valueIndex == p.valueIndex ) { + return p.lengthIndex; + } + } + } + return -1; + } + public boolean isForceUsingNIOOnly4All() { return forceUseNIOOnly4All; } public void addUseNIOOnly(final String fname ) { @@ -1380,6 +1438,8 @@ public class JavaConfiguration { readMaxOneElement(tok, filename, lineNo); } else if (cmd.equalsIgnoreCase("ArgumentIsString")) { readArgumentIsString(tok, filename, lineNo); + } else if (cmd.equalsIgnoreCase("ArgumentIsPascalString")) { + readArgumentIsPascalString(tok, filename, lineNo); } else if (cmd.equalsIgnoreCase("JavaCallbackDef")) { readJavaCallbackDef(tok, filename, lineNo); } else if (cmd.equalsIgnoreCase("JavaCallbackKey")) { @@ -1948,6 +2008,25 @@ public class JavaConfiguration { } } + protected void readArgumentIsPascalString(final StringTokenizer tok, final String filename, final int lineNo) { + try { + final String methodName = tok.nextToken(); + final List pascalTuples = new ArrayList(2); + while (tok.countTokens() >= 2) { + final int lenIdx = Integer.valueOf(tok.nextToken()).intValue(); + final int valIdx = Integer.valueOf(tok.nextToken()).intValue(); + pascalTuples.add(new PascalStringIdx(lenIdx, valIdx)); + } + if( pascalTuples.size() > 0 ) { + argumentsArePascalString.put(methodName, pascalTuples); + } + } catch (final NoSuchElementException e) { + throw new RuntimeException( + "Error parsing \"ArgumentIsPascalString\" command at line " + lineNo + + " in file \"" + filename + "\"", e); + } + } + protected void readStructPackage(final StringTokenizer tok, final String filename, final int lineNo) { try { final String struct = tok.nextToken(); -- cgit v1.2.3