mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
stabilize record usage in requires, add examples
This commit is contained in:
parent
8e40725d25
commit
94f9c6712d
4 changed files with 90 additions and 62 deletions
|
@ -31,7 +31,9 @@ void roc_dealloc(void *ptr, unsigned int alignment)
|
||||||
__attribute__((noreturn)) void roc_panic(void *ptr, unsigned int alignment)
|
__attribute__((noreturn)) void roc_panic(void *ptr, unsigned int alignment)
|
||||||
{
|
{
|
||||||
// TODO throw a RuntimeException from JNI
|
// TODO throw a RuntimeException from JNI
|
||||||
exit(0);
|
if (ptr != NULL)
|
||||||
|
printf("%s", ptr);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *roc_memcpy(void *dest, const void *src, size_t n)
|
void *roc_memcpy(void *dest, const void *src, size_t n)
|
||||||
|
@ -215,11 +217,11 @@ size_t roc_str_len(struct RocStr str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extern void roc__interpolateStringy_1_exposed_generic(struct RocStr *ret, struct RocStr *name);
|
extern void roc__programForHost_1__InterpolateString_caller(struct RocStr *name, char *closure_data, struct RocStr *ret);
|
||||||
// uncomment to test record
|
|
||||||
/* extern void roc__programForHost_1__InterpolateString_caller(struct RocStr *ret, struct RocStr *name); */
|
|
||||||
|
|
||||||
/* extern void roc__programForHost_1__MulArrByScalar_caller(struct RocBytesI32 *ret, struct RocBytesI32 *arr); */
|
extern void roc__programForHost_1__MulArrByScalar_caller(struct RocBytesI32 *arr, int32_t *scalar, char *closure_data, struct RocBytesI32 *ret);
|
||||||
|
|
||||||
|
extern void roc__programForHost_1__Factorial_caller(int64_t *scalar, char *closure_data, int64_t *ret);
|
||||||
|
|
||||||
|
|
||||||
JNIEXPORT jstring JNICALL Java_javaSource_Demo_sayHello
|
JNIEXPORT jstring JNICALL Java_javaSource_Demo_sayHello
|
||||||
|
@ -236,10 +238,7 @@ JNIEXPORT jstring JNICALL Java_javaSource_Demo_sayHello
|
||||||
struct RocStr ret = {0};
|
struct RocStr ret = {0};
|
||||||
|
|
||||||
// Call the Roc function to populate `ret`'s bytes.
|
// Call the Roc function to populate `ret`'s bytes.
|
||||||
// uncomment to test record
|
roc__programForHost_1__InterpolateString_caller(&rocName, 0, &ret);
|
||||||
/* roc__programForHost_1__InterpolateString_caller(&ret, &rocName); */
|
|
||||||
roc__interpolateStringy_1_exposed_generic(&ret, &rocName);
|
|
||||||
|
|
||||||
|
|
||||||
// java being java making this a lot harder than it needs to be
|
// java being java making this a lot harder than it needs to be
|
||||||
// https://stackoverflow.com/questions/32205446/getting-true-utf-8-characters-in-java-jni
|
// https://stackoverflow.com/questions/32205446/getting-true-utf-8-characters-in-java-jni
|
||||||
|
@ -270,28 +269,39 @@ JNIEXPORT jstring JNICALL Java_javaSource_Demo_sayHello
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* JNIEXPORT jintArray JNICALL Java_javaSource_Greeter_mularr */
|
JNIEXPORT jintArray JNICALL Java_javaSource_Demo_mulArrByScalar
|
||||||
/* (JNIEnv *env, jobject thisObj, jintArray arr) */
|
(JNIEnv *env, jobject thisObj, jintArray arr, jint scalar)
|
||||||
/* { */
|
{
|
||||||
/* jsize len = (*env)->GetArrayLength(env, arr); */
|
// extract data from jvm types
|
||||||
|
jint* jarr = (*env)->GetIntArrayElements(env, arr, NULL);
|
||||||
|
jsize len = (*env)->GetArrayLength(env, arr);
|
||||||
|
|
||||||
/* struct RocBytesI32 ret = {0}; */
|
// copying because better safe than sorry to mess with jvm array contents ig
|
||||||
/* int* jarr = (int*) (*env)->GetIntArrayElements(env, arr, NULL); */
|
int* carr = malloc(len * sizeof(int));
|
||||||
/* /\* int *cArray = new int[len]; *\/ */
|
memcpy(carr, (int*) jarr, len * sizeof(int));
|
||||||
|
|
||||||
/* /\* memcpy(cArray, jArray, len * sizeof(int)); *\/ */
|
// pass data to platform
|
||||||
/* /\* (env*)->ReleaseIntArrayElements(env, arr, jArray, JNI_ABORT); *\/ */
|
struct RocBytesI32 originalArray = { .bytes = carr, .len = len, .capacity = len };
|
||||||
|
struct RocBytesI32 ret = {0};
|
||||||
|
roc__programForHost_1__MulArrByScalar_caller(&originalArray, &scalar, 0, &ret);
|
||||||
|
|
||||||
/* struct RocBytesI32 originalArray = { .bytes = jarr, .len = len, .capacity = len }; */
|
// create jvm constructs
|
||||||
|
jintArray multiplied = (*env)->NewIntArray(env, ret.len);
|
||||||
|
(*env)->SetIntArrayRegion(env, multiplied, 0, ret.len, (jint*) ret.bytes);
|
||||||
|
|
||||||
/* roc__programForHost_1__MulArrByScalar_caller(&ret, &originalArray); */
|
// cleanup
|
||||||
|
(*env)->ReleaseIntArrayElements(env, arr, jarr, 0);
|
||||||
|
/* decref((void *)&originalArray, alignof(int32_t*));*/
|
||||||
|
decref((void *)&ret, alignof(int32_t *));
|
||||||
|
free(carr);
|
||||||
|
|
||||||
/* jintArray multiplied = (*env)->NewIntArray(env, len); */
|
return multiplied;
|
||||||
/* (*env)->SetIntArrayRegion(env, multiplied, 0, len, (jint*) ret.bytes); */
|
}
|
||||||
|
|
||||||
/* decref((void *)&originalArray, alignof(int32_t*)); */
|
JNIEXPORT jlong JNICALL Java_javaSource_Demo_factorial
|
||||||
/* decref((void *)&ret, alignof(int32_t *)); */
|
(JNIEnv *env, jobject thisObj, jlong n)
|
||||||
|
{
|
||||||
/* return multiplied; */
|
int64_t ret;
|
||||||
|
roc__programForHost_1__Factorial_caller(&n, 0, &ret);
|
||||||
/* } */
|
return ret;
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
app "rocdemo"
|
app "rocdemo"
|
||||||
packages { pf: "platform.roc" }
|
packages { pf: "platform.roc" }
|
||||||
imports []
|
imports []
|
||||||
|
provides [program] to pf
|
||||||
# uncomment to test record
|
|
||||||
# provides [program] to pf
|
|
||||||
provides [interpolateString] to pf
|
|
||||||
|
|
||||||
|
|
||||||
interpolateString : Str -> Str
|
interpolateString : Str -> Str
|
||||||
|
@ -13,11 +10,20 @@ interpolateString = \name ->
|
||||||
|
|
||||||
|
|
||||||
# jint is i32
|
# jint is i32
|
||||||
# mulArrByScalar : List I32 -> List I32
|
mulArrByScalar : List I32, I32 -> List I32
|
||||||
# mulArrByScalar = \arr ->
|
mulArrByScalar = \arr, scalar ->
|
||||||
# List.map arr \x -> x * 2
|
List.map arr \x -> x * scalar
|
||||||
|
|
||||||
# program = { interpolateString, mulArrByScalar }
|
# java doesn't have unsigned numbers so we cope with long
|
||||||
|
factorial : I64 -> I64
|
||||||
|
factorial = \n ->
|
||||||
|
if n < 0 then
|
||||||
|
# while we get the chance, examplify a roc panic in an interop
|
||||||
|
crash "No negatives here!!!"
|
||||||
|
# else if n == 0 then
|
||||||
|
# 1
|
||||||
|
else n
|
||||||
|
# factorial n * (n - 1)
|
||||||
|
|
||||||
# uncomment to test record
|
|
||||||
# program = { interpolateString }
|
program = { interpolateString, factorial, mulArrByScalar }
|
||||||
|
|
|
@ -1,15 +1,39 @@
|
||||||
package javaSource;
|
package javaSource;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Demo {
|
public class Demo {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
System.loadLibrary("interop");
|
System.loadLibrary("interop");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static native String sayHello(String num);
|
public static native String sayHello(String num);
|
||||||
// public static native int[] mularr(int[] arr);
|
|
||||||
|
public static native int[] mulArrByScalar(int[] arr, int scalar);
|
||||||
|
|
||||||
|
public static native long factorial(long n);
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(sayHello("Brendan"));
|
|
||||||
// int[] arr = {10, 20, 30, 40};
|
// string demo
|
||||||
// System.out.println(mularr(arr));
|
System.out.println(sayHello("Brendan") + "\n");
|
||||||
|
|
||||||
|
// array demo
|
||||||
|
int[] arr = {10, 20, 30, 40};
|
||||||
|
int x = 3;
|
||||||
|
System.out.println("Array " + Arrays.toString(arr) +
|
||||||
|
" multipled by " + x +
|
||||||
|
" results in " + Arrays.toString(mulArrByScalar(arr, x)) +
|
||||||
|
"\n");
|
||||||
|
|
||||||
|
// number + panic demo
|
||||||
|
// This can be implemented more peacefully but for sake of demonstration-
|
||||||
|
// this will panic from the roc side if n is negative
|
||||||
|
// and in turn will throw a JVM RuntimeException
|
||||||
|
long n = -1;
|
||||||
|
System.out.println("Factorial of " + n + " is " + factorial(n) + "\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,14 @@
|
||||||
platform "jvm-interop"
|
platform "jvm-interop"
|
||||||
requires {} { interpolateString : _}
|
requires {} { program : _ }
|
||||||
# uncomment to test record
|
|
||||||
# requires {} { program : _ }
|
|
||||||
exposes []
|
exposes []
|
||||||
packages {}
|
packages {}
|
||||||
imports []
|
imports []
|
||||||
# uncomment to test record
|
provides [programForHost]
|
||||||
# provides [programForHost]
|
|
||||||
provides [interpolateStringy]
|
|
||||||
|
|
||||||
|
|
||||||
# uncomment to test record
|
programForHost : {
|
||||||
# programForHost : {
|
interpolateString : (Str -> Str) as InterpolateString,
|
||||||
# interpolateString : (Str -> Str) as InterpolateString,
|
mulArrByScalar : (List I32, I32 -> List I32) as MulArrByScalar,
|
||||||
# # mulArrByScalar : (List I32 -> List I32) as MulArrByScalar,
|
factorial : (I64 -> I64) as Factorial,
|
||||||
# }
|
}
|
||||||
# programForHost = program
|
programForHost = program
|
||||||
|
|
||||||
|
|
||||||
# interpolateStringy : Str -> Str
|
|
||||||
interpolateStringy = \arg -> interpolateString arg
|
|
||||||
|
|
||||||
|
|
||||||
# mulArrByScalar : List I32 -> List I32
|
|
||||||
# mulArrByScalar = \arr -> mulArrayByScalar arr
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue