def visitOperation(self, node): rti = jnutils.GetTypeInformation(node.returnType().unalias()) params = [] for p in node.parameters(): dirn = [jnutils.Type.IN, jnutils.Type.OUT, jnutils.Type.INOUT][p.direction()] pti = jnutils.GetTypeInformation(p.paramType().unalias()) params.append([p.identifier(), pti, dirn]) self.writeMethod(jnutils.CppName(node.identifier()), jnutils.JavaMangleName(node.identifier()), rti, params)
def visitOperation(self, node): rti = jnutils.GetTypeInformation(node.returnType().unalias()) params = [] for p in node.parameters(): dirn = [jnutils.Type.IN, jnutils.Type.OUT, jnutils.Type.INOUT][p.direction()] pti = jnutils.GetTypeInformation(p.paramType().unalias()) params.append([p.identifier(), pti, dirn]) self.pushManglePart(node.identifier()) self.calculateMangled() self.writeMethod(self.mangled, jnutils.CppName(node.identifier()), rti, params, node.raises()) self.popManglePart()
def visitOperation(self, node): paramsig = '' for p in node.parameters(): direction = [ jnutils.Type.IN, jnutils.Type.OUT, jnutils.Type.INOUT ][p.direction()] v = jnutils.GetTypeInformation(p.paramType().unalias()).\ javaType(direction) + ' ' +\ jnutils.JavaName(p) if paramsig != '': paramsig = paramsig + ', ' paramsig = paramsig + v rti = jnutils.GetTypeInformation(node.returnType().unalias()) self.out.out('public native ' + rti.javaType(jnutils.Type.RETURN) + ' ' + jnutils.JavaName(node) + '(' + paramsig + ');')
def visitAttribute(self, node): ti = jnutils.GetTypeInformation(node.attrType().unalias()) for n in node.declarators(): self.writeMethod( jnutils.CppName(n.identifier()), jnutils.JavaMangleName(jnutils.AccessorName(n, 0)), ti, []) if not node.readonly(): for n in node.declarators(): self.writeMethod( jnutils.CppName(n.identifier()), jnutils.JavaMangleName(jnutils.AccessorName(n, 1)), None, [['param', ti, jnutils.Type.IN]])
def visitMember(self, node): alln = '' needcomma = 0 for n in node.declarators(): if needcomma: alln = alln + ', ' needcomma = 1 alln = alln + n.simplename sa = n.sizes() if sa != None: for s in sa: alln = alln + '[%u]' % s self.out.out('public ' +\ jnutils.GetTypeInformation(node.memberType()).javaType(jnutils.Type.IN) +\ ' ' + alln + ';')
def visitAttribute(self, node): ti = jnutils.GetTypeInformation(node.attrType().unalias()) for n in node.declarators(): self.pushManglePart(jnutils.AccessorName(n, 0)) self.calculateMangled() self.writeMethod(self.mangled, jnutils.CppName(n.identifier()), ti, [], []) self.popManglePart() if not node.readonly(): itype = ti.jniType(jnutils.Type.IN) for n in node.declarators(): self.pushManglePart(jnutils.AccessorName(n, 1)) self.calculateMangled() self.writeMethod(self.mangled, jnutils.CppName(n.identifier()), None, [['param', ti, jnutils.Type.IN]], []) self.popManglePart()
def visitException(self, node): setupOut = 0 if self.out == None: if not node.mainFile(): return self.out = output.Stream( open(self.directory + '/' + jnutils.JavaName(node) + ".java", 'w')) self.out.out('package ' + self.directory + ';') setupOut = 1 self.out.out('public class ' + jnutils.JavaName(node) + ' extends RuntimeException') self.out.out('{') self.out.inc_indent() constructorArgs = '' constructorSave = '' for n in node.members(): if constructorArgs != '': constructorArgs = constructorArgs + ', ' for dn in n.declarators(): constructorSave = constructorSave + ( '%s = _%s;' % (jnutils.JavaName(dn), jnutils.JavaName(dn))) constructorArgs = \ constructorArgs + \ jnutils.GetTypeInformation(n.memberType()).javaType(jnutils.Type.IN) +\ ' _' + jnutils.JavaName(dn) if dn.sizes() != None: constructorArgs = constructorArgs + string.join( map(lambda x: '[%s]' % x, dn.sizes()), '') self.out.out(' public ' + node.simplename + '(' + constructorArgs + '){ ' + constructorSave + ' }') for n in node.members(): n.accept(self) self.out.dec_indent() self.out.out('};') if setupOut: self.out = None
def writeMethod(self, name, pcmName, rtype, params, excepts): if rtype == None: rtypeName = 'void' else: rtypeName = rtype.jniType(jnutils.Type.RETURN) paramString = 'JNIEnv* env, jobject thisptr' for (pname, ti, dirn) in params: tiName = ti.jniType(dirn) paramString = paramString + ', ' + tiName + ' ' + pname self.hxx.out('extern "C" { PUBLIC_JAVAMOD_PRE ' + rtypeName + ' ' + name + '(' + paramString + ') PUBLIC_JAVAMOD_POST; };') self.cppMod.out(rtypeName + ' ' + name + '(' + paramString + ')') self.cppMod.out('{') self.cppMod.inc_indent() if (rtype != None and rtypeName != 'void'): needRet = 1 self.cppMod.out(rtype.pcmType(jnutils.Type.DERIVE) + ' _pcm_ret;') else: needRet = 0 pcmParams = '' for (pname, ti, dirn) in params: # We need to convert in parameters to the CXX type... indirect = '' if dirn != jnutils.Type.IN: indirect = ti.cref if pcmParams != '': pcmParams = pcmParams + ', ' self.cppMod.out( ti.pcmType(jnutils.Type.DERIVE) + ' _pcm_' + pname + ';') pcmParams = pcmParams + indirect + '_pcm_' + pname if dirn == jnutils.Type.OUT: continue self.cppMod.out( ti.convertToPCM(pname, '_pcm_' + pname, dirn != jnutils.Type.IN)) # Next, we need to extract the 'this' pointer... self.cppMod.out(self.cxxclass + '* pcm_this;') self.cppMod.out('{') self.cppMod.inc_indent() self.cppMod.out('jclass thisclazz = env->GetObjectClass(thisptr);') self.cppMod.out( 'jfieldID fid = env->GetFieldID(thisclazz, "nativePtr", "J");') self.cppMod.out('pcm_this = reinterpret_cast<' + self.cxxclass + '*>(env->GetLongField(thisptr, fid));') self.cppMod.dec_indent() self.cppMod.out('}') # Make the call to the PCM interface... if needRet: retsave = '_pcm_ret = ' else: retsave = '' self.cppMod.out('try') self.cppMod.out('{') self.cppMod.inc_indent() self.cppMod.out(retsave + 'pcm_this->' + pcmName + '(' + pcmParams + ');') self.cppMod.dec_indent() self.cppMod.out('}') for e in excepts: self.cppMod.out('catch (%s& _except)' % ('iface::' + jnutils.ScopedCppName(e))) self.cppMod.out('{') self.cppMod.inc_indent() # Clean up parameters... for (pname, ti, dirn) in params: self.cppMod.out(ti.pcmDestroy('_pcm_' + pname)) sigArgs = '' invokeArgs = '' for mem in e.members(): ti = jnutils.GetTypeInformation(mem.memberType()) for d in mem.declarators(): jniName = '_ejni_' + d.identifier() self.cppMod.out('%s %s;' % (ti.jniType(jnutils.Type.IN), jniName)) self.cppMod.out( ti.convertToJNI( jniName, '_except.%s' % (jnutils.CppName(d.identifier())))) sigArgs = sigArgs + ti.javaSig(jnutils.Type.IN) invokeArgs = invokeArgs + ',' + jniName self.cppMod.out('jclass eclazz = env->FindClass("%s");' % string.join(e.scopedName(), '/')) self.cppMod.out( 'jmethodID meth = env->GetMethodID(eclazz, "<init>", "(%s)V");' % sigArgs) self.cppMod.out('jobject eobj = env->NewObject(eclazz, meth%s);' % invokeArgs) self.cppMod.out('env->Throw((jthrowable)eobj);') if needRet: self.cppMod.out('return ' + rtype.failure_return + ';') else: self.cppMod.out('return;') self.cppMod.dec_indent() self.cppMod.out('}') self.cppMod.out('catch (...)') self.cppMod.out('{') self.cppMod.inc_indent() # Clean up parameters... for (pname, ti, dirn) in params: self.cppMod.out(ti.pcmDestroy('_pcm_' + pname)) # Raise an exception... self.cppMod.out( 'jclass eclazz = env->FindClass("java/lang/RuntimeException");') self.cppMod.out( 'env->ThrowNew(eclazz, "Native code threw exception");') if needRet: self.cppMod.out('return ' + rtype.failure_return + ';') else: self.cppMod.out('return;') self.cppMod.dec_indent() self.cppMod.out('}') # Convert out / inout parameters to JNI... for (pname, ti, dirn) in params: if dirn == jnutils.Type.IN: continue self.cppMod.out( ti.convertToJNI(pname, '_pcm_' + pname, indirectOut=1)) if needRet: self.cppMod.out(rtypeName + ' _jni_ret;') self.cppMod.out(rtype.convertToJNI('_jni_ret', '_pcm_ret')) # Clean up parameters... for (pname, ti, dirn) in params: self.cppMod.out(ti.pcmDestroy('_pcm_' + pname)) if needRet: self.cppMod.out(rtype.pcmDestroy('_pcm_ret')) self.cppMod.out('return _jni_ret;') self.cppMod.dec_indent() self.cppMod.out('}')
def visitAttribute(self, node): ti = jnutils.GetTypeInformation(node.attrType().unalias()) for n in node.declarators(): if not node.readonly(): self.writeSetter(n, ti) self.writeGetter(n, ti)