write to fd
This commit is contained in:
26
cmd/plt-build/src/main/go/pltbuild.c
Normal file
26
cmd/plt-build/src/main/go/pltbuild.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "pltbuild.h"
|
||||
|
||||
jint throwIOException( JNIEnv *env, char *message) {
|
||||
jclass exClass;
|
||||
char *className = "java/io/IOException";
|
||||
|
||||
exClass = (*env)->FindClass(env, className);
|
||||
return (*env)->ThrowNew(env, exClass, message);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_moe_rosa_planterette_jni_GoFile_write__ILnet_java_String_2(JNIEnv *env, jobject obj, jint fd, jstring str) {
|
||||
char *s = (*env)->GetStringUTFChars(env, str, 0);
|
||||
size_t sz = (*env)->GetStringUTFLength(env, str);
|
||||
|
||||
uintptr_t *errno_p = NULL;
|
||||
char **err_str_p = NULL;
|
||||
|
||||
planterette_write(fd, s, sz, errno_p, err_str_p);
|
||||
|
||||
if(errno_p || *err_str_p ) {
|
||||
throwIOException(env, **err_str_p ? *err_str_p : strerror(*errno_p));
|
||||
}
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, str, s);
|
||||
free(err_str_p);
|
||||
}
|
||||
38
cmd/plt-build/src/main/go/pltbuild.go
Normal file
38
cmd/plt-build/src/main/go/pltbuild.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package _go
|
||||
|
||||
//#cgo CFLAGS: -I "$JAVA_HOME/include" -I "$JAVA_HOME/include/linux"
|
||||
//#include "pltbuild.h"
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
//export planterette_write
|
||||
func planterette_write(
|
||||
fd C.int,
|
||||
str_p *C.char, str_sz C.size_t,
|
||||
errno_p *C.uintptr_t,
|
||||
err_str_p **C.char,
|
||||
) {
|
||||
f := os.NewFile(uintptr(fd), strconv.Itoa(int(fd)))
|
||||
defer f.Close()
|
||||
_, err := f.WriteString(C.GoStringN(str_p, C.int(str_sz)))
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var pathError *os.PathError
|
||||
if errors.As(err, &pathError) {
|
||||
var errno syscall.Errno
|
||||
if errors.As(pathError.Err, &errno) {
|
||||
*errno_p = C.uintptr_t(errno)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
*err_str_p = C.CString(err.Error())
|
||||
return
|
||||
}
|
||||
17
cmd/plt-build/src/main/go/pltbuild.h
Normal file
17
cmd/plt-build/src/main/go/pltbuild.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <jni.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef _PLTBUILD_H
|
||||
#define _PLTBUILD_H
|
||||
|
||||
/*
|
||||
* moe/rosa/planterette/jni/GoFile#write(ILnet/java/String;)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_moe_rosa_planterette_jni_GoFile_write__ILnet_java_String_2(JNIEnv *, jobject, jint, jstring);
|
||||
|
||||
extern void planterette_write(int fd, char *str_p, size_t str_sz, uintptr_t *errno_p, char **err_str_p);
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,11 @@
|
||||
@file:Suppress("unused")
|
||||
package moe.rosa.planterette.dsl
|
||||
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import moe.rosa.planterette.api.Capability
|
||||
import moe.rosa.planterette.api.Manifest
|
||||
import moe.rosa.planterette.jni.GoFile
|
||||
|
||||
|
||||
@Target(AnnotationTarget.TYPE, AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
|
||||
@@ -38,8 +41,21 @@ data class PlanteretteBlock(
|
||||
var metadata: MetadataBlock,
|
||||
var executable: ExecutableBlock,
|
||||
var capabilities: CapabilitiesBlock,
|
||||
var permissions: PermissionsBlock
|
||||
)
|
||||
var permissions: PermissionsBlock,
|
||||
var buildGuard: Boolean = false
|
||||
) {
|
||||
// NOTE the two underscores are because this function is only intended to be added at runtime
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@Suppress("FunctionName")
|
||||
fun __build(args: Array<out String>): PlanteretteBlock {
|
||||
if(!buildGuard) {
|
||||
val manifest = Manifest(metadata.metadata, executable.executable, capabilities.capabilities, permissions.permissions)
|
||||
GoFile.write(args[0].toInt(), Json.encodeToString(manifest))
|
||||
buildGuard = true
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@MetadataBlockMarker
|
||||
data class MetadataBlock(var metadata: Manifest.Metadata)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package moe.rosa.planterette.jni
|
||||
|
||||
object GoFile {
|
||||
external fun write(fd: Int, str: String)
|
||||
}
|
||||
Reference in New Issue
Block a user