June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,14 @@
|
|||
// Kotlin Native v0.6
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.*
|
||||
|
||||
fun query(data: CPointer<ByteVar>, length: CPointer<size_tVar>): Int {
|
||||
val s = "Here am I"
|
||||
val strLen = s.length
|
||||
val bufferSize = length.pointed.value
|
||||
if (strLen > bufferSize) return 0 // buffer not large enough
|
||||
for (i in 0 until strLen) data[i] = s[i].toByte()
|
||||
length.pointed.value = strLen.signExtend<size_t>()
|
||||
return 1
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef KONAN_LIBQUERY_H
|
||||
#define KONAN_LIBQUERY_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef unsigned char libQuery_KBoolean;
|
||||
typedef char libQuery_KByte;
|
||||
typedef unsigned short libQuery_KChar;
|
||||
typedef short libQuery_KShort;
|
||||
typedef int libQuery_KInt;
|
||||
typedef long long libQuery_KLong;
|
||||
typedef float libQuery_KFloat;
|
||||
typedef double libQuery_KDouble;
|
||||
typedef void* libQuery_KNativePtr;
|
||||
struct libQuery_KType;
|
||||
typedef struct libQuery_KType libQuery_KType;
|
||||
|
||||
typedef struct {
|
||||
/* Service functions. */
|
||||
void (*DisposeStablePointer)(libQuery_KNativePtr ptr);
|
||||
void (*DisposeString)(const char* string);
|
||||
libQuery_KBoolean (*IsInstance)(libQuery_KNativePtr ref, const libQuery_KType* type);
|
||||
|
||||
/* User functions. */
|
||||
struct {
|
||||
struct {
|
||||
libQuery_KInt (*query)(void* data, void* length);
|
||||
} root;
|
||||
} kotlin;
|
||||
} libQuery_ExportedSymbols;
|
||||
extern libQuery_ExportedSymbols* libQuery_symbols(void);
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
#endif /* KONAN_LIBQUERY_H */
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libQuery_api.h"
|
||||
|
||||
static int Query (char * Data, size_t * Length)
|
||||
{
|
||||
return libQuery_symbols() -> kotlin.root.query(Data, Length);
|
||||
}
|
||||
|
||||
int main (int argc, char * argv [])
|
||||
{
|
||||
char Buffer [1024];
|
||||
size_t Size = sizeof (Buffer);
|
||||
|
||||
if (0 == Query (Buffer, &Size))
|
||||
{
|
||||
printf ("failed to call Query\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
char * Ptr = Buffer;
|
||||
while (Size-- > 0) putchar (*Ptr++);
|
||||
putchar ('\n');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
/* Query.scala */
|
||||
object Query {
|
||||
def call(data: Array[Byte], length: Array[Int]): Boolean = {
|
||||
val message = "Here am I"
|
||||
val mb = message.getBytes("utf-8")
|
||||
if (length(0) >= mb.length) {
|
||||
length(0) = mb.length
|
||||
System.arraycopy(mb, 0, data, 0, mb.length)
|
||||
true
|
||||
} else false
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
/* query-jni.c */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <jni.h>
|
||||
|
||||
static JavaVM *jvm = NULL;
|
||||
static JNIEnv *jenv = NULL;
|
||||
|
||||
static void die(const char *message) {
|
||||
fprintf(stderr, "%s\n", message);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void oom(void) {
|
||||
die("Query: out of memory");
|
||||
}
|
||||
|
||||
static void except(void) {
|
||||
if ((*jenv)->ExceptionCheck(jenv))
|
||||
die("Query: unexpected Java exception");
|
||||
}
|
||||
|
||||
static void do_at_exit(void) {
|
||||
(*jvm)->DestroyJavaVM(jvm);
|
||||
}
|
||||
|
||||
static void require_jvm(void) {
|
||||
JavaVMInitArgs args;
|
||||
|
||||
if (jvm)
|
||||
return;
|
||||
|
||||
args.version = JNI_VERSION_1_4;
|
||||
args.nOptions = 0;
|
||||
args.options = NULL;
|
||||
args.ignoreUnrecognized = JNI_FALSE;
|
||||
if (JNI_CreateJavaVM(&jvm, (void **)&jenv, &args) != JNI_OK)
|
||||
die("Query: can't create Java VM");
|
||||
atexit(do_at_exit);
|
||||
}
|
||||
|
||||
int Query(char *data, size_t *length) {
|
||||
jclass cQuery;
|
||||
jmethodID mcall;
|
||||
jintArray jlength;
|
||||
jint jlength0;
|
||||
jbyteArray jdata;
|
||||
jboolean result;
|
||||
|
||||
jlength0 = (jint)length[0];
|
||||
if ((size_t)jlength0 != length[0])
|
||||
die("Query: length is too large for Scala array");
|
||||
|
||||
require_jvm();
|
||||
|
||||
/* Create a local frame for references to Scala objects. */
|
||||
if ((*jenv)->PushLocalFrame(jenv, 16))
|
||||
oom();
|
||||
|
||||
/* Look for class Query, static boolean call(byte[], int[]) */
|
||||
cQuery = (*jenv)->FindClass(jenv, "Query");
|
||||
if (cQuery == NULL)
|
||||
die("Query: can't find Query.class");
|
||||
mcall = (*jenv)->GetStaticMethodID(jenv, cQuery, "call", "([B[I)Z");
|
||||
if (mcall == NULL)
|
||||
die("Query: missing call() method");
|
||||
|
||||
/*
|
||||
* Make arguments to Query.call(). We can't pass data[] and
|
||||
* length[] to Scala, so we make new Scala arrays jdata[] and
|
||||
* jlength[].
|
||||
*/
|
||||
jdata = (*jenv)->NewByteArray(jenv, (jsize)jlength0);
|
||||
if (jdata == NULL)
|
||||
oom();
|
||||
jlength = (*jenv)->NewIntArray(jenv, 1);
|
||||
if (jlength == NULL)
|
||||
oom();
|
||||
|
||||
/* Set jlength[0] = length[0]. */
|
||||
(*jenv)->SetIntArrayRegion(jenv, jlength, 0, 1, &jlength0);
|
||||
except();
|
||||
|
||||
/*
|
||||
* Call our Scala method.
|
||||
*/
|
||||
result = (*jenv)->CallStaticBooleanMethod
|
||||
(jenv, cQuery, mcall, jdata, jlength);
|
||||
except();
|
||||
|
||||
/*
|
||||
* Set length[0] = jlength[0].
|
||||
* Copy length[0] bytes from jdata[] to data[].
|
||||
*/
|
||||
(*jenv)->GetIntArrayRegion(jenv, jlength, 0, 1, &jlength0);
|
||||
except();
|
||||
length[0] = (size_t)jlength0;
|
||||
(*jenv)->GetByteArrayRegion
|
||||
(jenv, jdata, 0, (jsize)jlength0, (jbyte *)data);
|
||||
|
||||
/* Drop our local frame and its references. */
|
||||
(*jenv)->PopLocalFrame(jenv, NULL);
|
||||
|
||||
return (int)result;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# Makefile
|
||||
|
||||
# Edit these lines to match your JDK.
|
||||
JAVA_HOME = /Library/Java/Home
|
||||
CPPFLAGS = -I$(JAVA_HOME)/include
|
||||
LIBS = -framework JavaVM
|
||||
JAVAC = $(JAVA_HOME)/bin/javac
|
||||
CC = cc
|
||||
|
||||
all: calljava Query.class
|
||||
|
||||
calljava: main.o query-jni.o
|
||||
$(CC) -o calljava main.o query-jni.o $(LIBS)
|
||||
|
||||
.SUFFIXES: .c .class .java .o
|
||||
.c.o:
|
||||
$(CC) $(CPPFLAGS) -c $<
|
||||
.java.class:
|
||||
$(JAVAC) $<
|
||||
|
||||
clean:
|
||||
rm -f calljava main.o query-jni.o Query.class
|
||||
Loading…
Add table
Add a link
Reference in a new issue