Skip to content

Commit

Permalink
JNI Library now compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemer committed Jan 22, 2016
1 parent b41f774 commit 8e4de1b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
68 changes: 38 additions & 30 deletions JNIMethods.swift.gyb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- JNICallMethod.swift.gyb ------------------------------*- swift -*-===//
//===--- JNIMethods.swift.gyb ---------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
Expand Down Expand Up @@ -32,81 +32,89 @@ public extension JNI {

% # Standard Java method calls

public func GetMethodID(clazz: jclass, name: String, sig: String) -> jmethodID {
public func GetMethodID(targetClass: jclass, methodName name: String, methodSignature sig: String) -> jmethodID? {
let env = self._env
return env.memory.memory.GetMethodID(env, clazz, name, sig)
let result = env.memory.memory.GetMethodID(env, targetClass, name, sig)
return (result == nil) ? .None : result
}

% for (TypeName, type) in jTypes:
%for (TypeName, type) in jTypes:

public func Call${TypeName}Method(obj: jobject, methodID: jmethodID, args: jvalue...) -> ${type} {
return Call${TypeName}MethodA(object, method, args)
return Call${TypeName}MethodA(obj, methodID: methodID, args: args)
}

public func Call${TypeName}MethodV(obj: jobject, methodID: jmethodID, args: va_list) -> ${type} {
let env = self._env
return env.memory.memory.Call${TypeName}MethodV(env, object, method, args)
@available(*, unavailable, message="CVaListPointers are not supported, use `Call${TypeName}Method` or `Call${TypeName}MethodA` instead")
public func Call${TypeName}MethodV(obj: jobject, methodID: jmethodID, args: CVaListPointer) -> ${type} {
// let env = self._env
// return env.memory.memory.Call${TypeName}MethodV(env, obj, methodID, args)
return ${type}()
}

public func Call${TypeName}MethodA(obj: jobject, methodID: jmethodID, args: [jvalue]) -> ${type} {
let env = self._env
var mutableArgs = args
return env.memory.memory.Call${TypeName}MethodA(env, object, method, &mutableArgs)
return env.memory.memory.Call${TypeName}MethodA(env, obj, methodID, &mutableArgs)
}

% end #standard methods
%end #standard methods



% # Nonvirtual Java method calls
% for (TypeName, Type) in jTypes:
%for (TypeName, type) in jTypes:

public func CallNonvirtual${TypeName}Method(obj: jobject, class: jclass, methodID: jmethodID, args: jvalue...) -> ${type} {
return self.CallNonvirtual${TypeName}MethodA()
public func CallNonvirtual${TypeName}Method(obj: jobject, targetClass: jclass, methodID: jmethodID, args: jvalue...) -> ${type} {
return self.CallNonvirtual${TypeName}MethodA(obj, targetClass: targetClass, methodID: methodID, args: args)
}

public func CallNonvirtual${TypeName}MethodV(obj: jobject, class: jclass, methodID: jmethodID, args: va_list) -> ${type} {
let env = self._env
return env.memory.memory.CallNonvirtual${TypeName}MethodV(env, object, method, args)
@available(*, unavailable, message="CVaListPointers are not supported, use `CallNonvirtual${TypeName}Method` or `CallNonvirtual${TypeName}MethodA` instead")
public func CallNonvirtual${TypeName}MethodV(obj: jobject, targetClass: jclass, methodID: jmethodID, args: CVaListPointer) -> ${type} {
// let env = self._env
// return env.memory.memory.CallNonvirtual${TypeName}MethodV(env, obj, methodID, args)
return ${type}()
}

public func CallNonvirtual${TypeName}MethodA(obj: jobject, class: jclass, methodID: jmethodID, args: [jvalue]) -> ${type} {
public func CallNonvirtual${TypeName}MethodA(obj: jobject, targetClass: jclass, methodID: jmethodID, args: [jvalue]) -> ${type} {
let env = self._env
var mutableArgs = args
return env.memory.memory.CallNonvirtual${TypeName}MethodA(env, object, method, &mutableArgs)
return env.memory.memory.CallNonvirtual${TypeName}MethodA(env, obj, targetClass, methodID, &mutableArgs)
}

% end #nonvirtual methods
%end #nonvirtual methods



%# Static Java method calls

public func GetStaticMethodID(clazz: jclass, name: String, sig: String) -> jmethodID {
public func GetStaticMethodID(targetClass: jclass, name: String, sig: String) -> jmethodID {
let env = self._env
return env.memory.memory.GetStaticMethodID(env, clazz, name, sig)
return env.memory.memory.GetStaticMethodID(env, targetClass, name, sig)
}

% for (TypeName, Type) in jTypes:
%for (TypeName, type) in jTypes:

public func CallStatic${TypeName}Method(class: jclass, methodID: jmethodID, args: jvalue...) -> ${type} {
return CallStatic${TypeName}MethodA(object, method, args)
public func CallStatic${TypeName}Method(targetClass: jclass, methodID: jmethodID, args: jvalue...) -> ${type} {
return CallStatic${TypeName}MethodA(targetClass, methodID: methodID, args: args)
}

public func CallStatic${TypeName}MethodV(class: jclass, methodID: jmethodID, args: va_list) -> ${type} {
let env = self._env
return env.memory.memory.CallStatic${TypeName}MethodV(env, object, method, args)
@available(*, unavailable, message="CVaListPointers are not supported, use `CallStatic${TypeName}Method` or `CallStatic${TypeName}MethodA` instead")
public func CallStatic${TypeName}MethodV(targetClass: jclass, methodID: jmethodID, args: CVaListPointer) -> ${type} {
// let env = self._env
// return env.memory.memory.CallStatic${TypeName}MethodV(env, class, methodID, args)
return ${type}()
}

public func CallStatic${TypeName}MethodA(class: jclass, methodID: jmethodID, args: [jvalue]) -> ${type} {
public func CallStatic${TypeName}MethodA(targetClass: jclass, methodID: jmethodID, args: [jvalue]) -> ${type} {
let env = self._env
var mutableArgs = args
return env.memory.memory.CallStatic${TypeName}MethodA(env, object, method, &mutableArgs)
return env.memory.memory.CallStatic${TypeName}MethodA(env, targetClass, methodID, &mutableArgs)
}

% end #static methods
%end #static methods
}

// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End:

29 changes: 16 additions & 13 deletions JNIObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ import CJNI

public extension JNI {

public func AllocObject(clazz: jclass) -> jobject {
public func AllocObject(targetClass: jclass) -> jobject {
let env = self._env
return env.memory.memory.AllocObject(env, clazz)
return env.memory.memory.AllocObject(env, targetClass)
}

public func NewObject(clazz: jclass, methodID: jmethodID, ...) -> jobject {
let env = self._env
return self.env.memory.memory.NewObjectV(env, clazz, methodID, args)
public func NewObject(targetClass: jclass, methodID: jmethodID, args: jvalue...) -> jobject {
return self.NewObjectA(targetClass, methodID: methodID, args: args)
}

public func NewObjectV(clazz: jclass, methodID: jmethodID, va_list args) -> jobject {
let env = self._env
return env.memory.memory.NewObjectV(env, clazz, methodID, args)
@available(*, unavailable, message="CVaListPointer unavailable, use NewObject or NewObjectA")
public func NewObjectV(targetClass: jclass, methodID: jmethodID, args: CVaListPointer) -> jobject {
// let env = self._env
// return env.memory.memory.NewObjectV(env, targetClass, methodID, args)
return jobject()
}

public func NewObjectA(clazz: jclass, methodID: jmethodID, args: UnsafeMutablePointer<jvalue>) -> jobject {
public func NewObjectA(targetClass: jclass, methodID: jmethodID, args: [jvalue]) -> jobject {
let env = self._env
return env.memory.memory.NewObjectA(env, clazz, methodID, args)
var mutableArgs = args
return env.memory.memory.NewObjectA(env, targetClass, methodID, &mutableArgs)
}

public func GetObjectClass(obj: jobject) -> jclass {
public func GetObjectClass(obj: jobject) -> jclass? {
let env = self._env
return env.memory.memory.GetObjectClass(env, obj)
let result = env.memory.memory.GetObjectClass(env, obj)
return (result != nil) ? result : .None
}

}
}
8 changes: 4 additions & 4 deletions JNIStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public extension JNI {

public func GetStringUTFChars(string: jstring, isCopy: UnsafeMutablePointer<jboolean>) -> String {
let env = self._env
return env.memory.memory.GetStringUTFChars(env, string, isCopy)
return String(env.memory.memory.GetStringUTFChars(env, string, isCopy))
}

public func ReleaseStringUTFChars(string: jstring, utf: String) {
Expand All @@ -46,9 +46,9 @@ public extension JNI {
env.memory.memory.GetStringRegion(env, str, start, len, buf)
}

public func GetStringUTFRegion(str: jstring, start: jsize, len: jsize, buf: UnsafeMutablePointer<char>) {
public func GetStringUTFRegion(str: jstring, start: jsize, len: jsize, buf: UnsafeMutablePointer<CChar>) {
let env = self._env
return env.memory.memory.GetStringUTFRegion(env, str, start, len, buf)
env.memory.memory.GetStringUTFRegion(env, str, start, len, buf)
}

public func GetStringCritical(string: jstring, isCopy: UnsafeMutablePointer<jboolean>) -> UnsafePointer<jchar> {
Expand All @@ -60,4 +60,4 @@ public extension JNI {
let env = self._env
env.memory.memory.ReleaseStringCritical(env, string, carray)
}
}
}
20 changes: 4 additions & 16 deletions SwiftJNI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public class SwiftJNI : JNI {
return (result != nil) ? result : .None
}

public func DeleteLocalRef(object: jobject) {
let _env = self._env
_env.memory.memory.DeleteLocalRef(_env, object)
}


// MARK: Classes and Methods

public func FindClass(className: String) -> jclass? {
Expand All @@ -64,12 +58,6 @@ public class SwiftJNI : JNI {
return (result != nil) ? result : .None
}

public func GetObjectClass(object: jobject) -> jclass? {
let _env = self._env
let result = _env.memory.memory.GetObjectClass(_env, object)
return (result != nil) ? result : .None
}

public func GetMethodID(javaClass: jclass, methodName: UnsafePointer<CChar>, methodSignature: UnsafePointer<CChar>) -> jmethodID? {
let _env = self._env
let result = _env.memory.memory.GetMethodID(_env, javaClass, methodName, methodSignature)
Expand Down Expand Up @@ -216,13 +204,13 @@ public struct JavaCallback {
self.methodID = methodID
}

public func apply(parameters: [jvalue]) {
jni.CallVoidMethodA(jobj, methodID: methodID, parameters: parameters)
public func apply(args: [jvalue]) {
jni.CallVoidMethodA(jobj, methodID: methodID, args: args)
}

/// Send variadic parameters to the func that takes an array
public func call(parameters: jvalue...) {
self.apply(parameters)
public func call(args: jvalue...) {
self.apply(args)
}
}

0 comments on commit 8e4de1b

Please sign in to comment.