pass number straight away + renames for clarity

This commit is contained in:
dankeyy 2023-03-14 15:56:08 +02:00
parent d91a90774b
commit 1dd46c985f
No known key found for this signature in database
GPG key ID: 9EBEF7DB1A70533D
5 changed files with 15 additions and 22 deletions

View file

@ -9,7 +9,7 @@
#include <stdint.h> #include <stdint.h>
#include <jni.h> #include <jni.h>
#include "HelloJNI.h" #include "javaSource_Greeter.h"
void *roc_alloc(size_t size, unsigned int alignment) void *roc_alloc(size_t size, unsigned int alignment)
@ -207,19 +207,15 @@ size_t roc_str_len(struct RocStr str)
} }
} }
extern void roc__mainForHost_1_exposed_generic(struct RocBytes *ret, struct RocBytes *arg); extern void roc__stringInterpolation_1_exposed_generic(struct RocBytes *ret, int32_t arg);
JNIEXPORT jstring JNICALL Java_javaSource_Greeter_sayHello JNIEXPORT jstring JNICALL Java_javaSource_Greeter_sayHello
(JNIEnv *env, jobject thisObj, jint num) (JNIEnv *env, jobject thisObj, jint num)
{ {
char native_string[256] = {0};
sprintf(native_string, "%d", num);
struct RocBytes arg = init_rocbytes((uint8_t *)native_string, strlen(native_string));
struct RocBytes ret = {0}; struct RocBytes ret = {0};
// Call the Roc function to populate `ret`'s bytes. // Call the Roc function to populate `ret`'s bytes.
roc__mainForHost_1_exposed_generic(&ret, &arg); roc__stringInterpolation_1_exposed_generic(&ret, (int32_t) num);
// 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

View file

@ -41,4 +41,4 @@ clang \
clang -shared -o libinterop.so bridge.o -L. -lhello clang -shared -o libinterop.so bridge.o -L. -lhello
# then run # then run
# java javaSource.Greeter java javaSource.Greeter

View file

@ -1,12 +1,12 @@
app "libhello" app "libhello"
packages { pf: "platform/main.roc" } packages { pf: "platform/main.roc" }
imports [] imports []
provides [main] to pf provides [interpolate] to pf
main : U64 -> Str interpolate : I32 -> Str
main = \num -> interpolate = \num ->
if num == 0 then if num == 0 then
"I need a positive number here!" "I need a non-zero number here!"
else else
str = Num.toStr num str = Num.toStr num
"The number was \(str), OH YEAH!!! 🤘🤘" "The number was \(str), OH YEAH!!! 🤘🤘"

View file

@ -80,12 +80,12 @@ size_t roc_str_len(struct RocStr str) {
} }
} }
extern void roc__mainForHost_1_exposed_generic(struct RocStr *string); extern void roc__stringInterpolation_1_exposed_generic(struct RocStr *string);
int main() { int main() {
struct RocStr str; struct RocStr str;
roc__mainForHost_1_exposed_generic(&str); roc__stringInterpolation_1_exposed_generic(&str);
// Determine str_len and the str_bytes pointer, // Determine str_len and the str_bytes pointer,
// taking into account the small string optimization. // taking into account the small string optimization.

View file

@ -1,12 +1,9 @@
platform "jvm-interop" platform "jvm-interop"
requires {} { main : arg -> ret | arg has Decoding } requires {} { interpolate : arg -> ret }
exposes [] exposes []
packages {} packages {}
imports [Json] imports []
provides [mainForHost] provides [stringInterpolation]
mainForHost : List U8 -> Str stringInterpolation : I32 -> Str
mainForHost = \json -> stringInterpolation = \arg -> interpolate arg
when Decode.fromBytes json Json.fromUtf8 is
Ok arg -> main arg
Err _ -> "Decoding Error"