Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing options and arguments to worklets #8

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions android/src/main/java/to/holepunch/bare/kit/MessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,75 +20,75 @@ public interface ReplyCallback {
protected Worklet worklet;
protected ReplyCallback callback;

protected MessagingService(ReplyCallback callback) {
protected MessagingService(Worklet.Options options, ReplyCallback callback) {
super();

this.worklet = new Worklet();
this.worklet = new Worklet(options);
this.callback = callback;
}

protected MessagingService(String filename, ByteBuffer source, ReplyCallback callback) {
this(callback);
start(filename, source);
protected MessagingService(String filename, ByteBuffer source, String[] arguments, Worklet.Options options, ReplyCallback callback) {
this(options, callback);
start(filename, source, arguments);
}

protected MessagingService(String filename, String source, Charset charset, ReplyCallback callback) {
this(callback);
start(filename, source, charset);
protected MessagingService(String filename, String source, Charset charset, String[] arguments, Worklet.Options options, ReplyCallback callback) {
this(options, callback);
start(filename, source, charset, arguments);
}

protected MessagingService(String filename, String source, String charset, ReplyCallback callback) {
this(callback);
start(filename, source, charset);
protected MessagingService(String filename, String source, String charset, String[] arguments, Worklet.Options options, ReplyCallback callback) {
this(options, callback);
start(filename, source, charset, arguments);
}

protected MessagingService(String filename, InputStream source, ReplyCallback callback) throws IOException {
this(callback);
start(filename, source);
protected MessagingService(String filename, InputStream source, String[] arguments, Worklet.Options options, ReplyCallback callback) throws IOException {
this(options, callback);
start(filename, source, arguments);
}

protected MessagingService() {
this(null);
protected MessagingService(Worklet.Options options) {
this(options, null);
}

protected MessagingService(String filename, ByteBuffer source) {
this();
start(filename, source);
protected MessagingService(String filename, ByteBuffer source, String[] arguments, Worklet.Options options) {
this(options);
start(filename, source, arguments);
}

protected MessagingService(String filename, String source, Charset charset) {
this();
start(filename, source, charset);
protected MessagingService(String filename, String source, Charset charset, String[] arguments, Worklet.Options options) {
this(options);
start(filename, source, charset, arguments);
}

protected MessagingService(String filename, String source, String charset) {
this();
start(filename, source, charset);
protected MessagingService(String filename, String source, String charset, String[] arguments, Worklet.Options options) {
this(options);
start(filename, source, charset, arguments);
}

protected MessagingService(String filename, InputStream source) throws IOException {
this();
start(filename, source);
protected MessagingService(String filename, InputStream source, String[] arguments, Worklet.Options options) throws IOException {
this(options);
start(filename, source, arguments);
}

protected void
start (String filename, ByteBuffer source) {
this.worklet.start(filename, source);
start (String filename, ByteBuffer source, String[] arguments) {
this.worklet.start(filename, source, arguments);
}

protected void
start (String filename, String source, Charset charset) {
this.worklet.start(filename, source, charset);
start (String filename, String source, Charset charset, String[] arguments) {
this.worklet.start(filename, source, charset, arguments);
}

protected void
start (String filename, String source, String charset) {
this.worklet.start(filename, source, charset);
start (String filename, String source, String charset, String[] arguments) {
this.worklet.start(filename, source, charset, arguments);
}

protected void
start (String filename, InputStream source) throws IOException {
this.worklet.start(filename, source);
start (String filename, InputStream source, String[] arguments) throws IOException {
this.worklet.start(filename, source, arguments);
}

@Override
Expand Down
40 changes: 26 additions & 14 deletions android/src/main/java/to/holepunch/bare/kit/Worklet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,34 @@ private interface NativePushCallback {
apply (ByteBuffer reply, String exception);
}

public static class Options {
public int memoryLimit = 0;

public Options
memoryLimit (int memoryLimit) {
this.memoryLimit = memoryLimit;
return this;
}
}

private ByteBuffer handle;
private FileDescriptor incoming;
private FileDescriptor outgoing;
private Handler handler;

public Worklet() {
handle = init();
public Worklet(Options options) {
if (options == null) options = new Options();

handle = init(options.memoryLimit);

handler = Handler.createAsync(Looper.getMainLooper());
}

private native ByteBuffer
init ();
init (int memoryLimit);

private native void
start (ByteBuffer handle, String filename, ByteBuffer source, int len);
start (ByteBuffer handle, String filename, ByteBuffer source, int len, String[] arguments);

private native void
suspend (ByteBuffer handle, int linger);
Expand All @@ -64,15 +76,15 @@ public Worklet() {
push (ByteBuffer handle, ByteBuffer payload, int len, NativePushCallback callback);

private void
start (String filename, ByteBuffer source, int len) {
start(handle, filename, source, len);
start (String filename, ByteBuffer source, int len, String[] arguments) {
start(handle, filename, source, len, arguments);

incoming = incoming(handle);
outgoing = outgoing(handle);
}

public void
start (String filename, ByteBuffer source) {
start (String filename, ByteBuffer source, String[] arguments) {
ByteBuffer buffer;

if (source.isDirect()) {
Expand All @@ -83,21 +95,21 @@ public Worklet() {
buffer.flip();
}

start(filename, buffer, buffer.limit());
start(filename, buffer, buffer.limit(), arguments);
}

public void
start (String filename, String source, Charset charset) {
start(filename, ByteBuffer.wrap(source.getBytes(charset)));
start (String filename, String source, Charset charset, String[] arguments) {
start(filename, ByteBuffer.wrap(source.getBytes(charset)), arguments);
}

public void
start (String filename, String source, String charset) {
start(filename, source, Charset.forName(charset));
start (String filename, String source, String charset, String[] arguments) {
start(filename, source, Charset.forName(charset), arguments);
}

public void
start (String filename, InputStream source) throws IOException {
start (String filename, InputStream source, String[] arguments) throws IOException {
source.reset();

ByteBuffer buffer = ByteBuffer.allocateDirect(Math.max(4096, source.available()));
Expand All @@ -118,7 +130,7 @@ public Worklet() {
buffer.flip();
channel.close();

start(filename, buffer, buffer.limit());
start(filename, buffer, buffer.limit(), arguments);
}

public void
Expand Down
43 changes: 34 additions & 9 deletions android/src/main/jni/Worklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,60 @@ typedef struct {
} bare_worklet_push_context_t;

JNIEXPORT jobject JNICALL
Java_to_holepunch_bare_kit_Worklet_init (JNIEnv *env, jobject self) {
Java_to_holepunch_bare_kit_Worklet_init (JNIEnv *env, jobject self, jint jmemory_limit) {
int err;

bare_worklet_t *worklet = malloc(sizeof(bare_worklet_t));

jobject handle = (*env)->NewDirectByteBuffer(env, (void *) worklet, sizeof(bare_worklet_t));
bare_worklet_options_t options = {
.memory_limit = (int) jmemory_limit,
};

err = bare_worklet_init(worklet, NULL);
err = bare_worklet_init(worklet, &options);
assert(err == 0);

jobject handle = (*env)->NewDirectByteBuffer(env, (void *) worklet, sizeof(bare_worklet_t));

return handle;
}

JNIEXPORT void JNICALL
Java_to_holepunch_bare_kit_Worklet_start (JNIEnv *env, jobject self, jobject handle, jstring jfilename, jobject jsource, jint jlen) {
Java_to_holepunch_bare_kit_Worklet_start (JNIEnv *env, jobject self, jobject handle, jstring jfilename, jobject jsource, jint jlen, jobjectArray jarguments) {
int err;

bare_worklet_t *worklet = (bare_worklet_t *) (*env)->GetDirectBufferAddress(env, handle);

const char *filename = (*env)->GetStringUTFChars(env, jfilename, NULL);

char *base = (*env)->GetDirectBufferAddress(env, jsource);
int argc = (*env)->IsSameObject(env, jarguments, NULL) ? 0 : (*env)->GetArrayLength(env, jarguments);

int len = (int) jlen;
const char **argv = calloc(argc, sizeof(char *));

uv_buf_t source = uv_buf_init(base, len);
for (int i = 0; i < argc; i++) {
jstring arg = (jstring) (*env)->GetObjectArrayElement(env, jarguments, i);

err = bare_worklet_start(worklet, filename, &source);
assert(err == 0);
argv[i] = (*env)->GetStringUTFChars(env, arg, NULL);
}

if ((*env)->IsSameObject(env, jsource, NULL)) {
err = bare_worklet_start(worklet, filename, NULL, argc, argv);
assert(err == 0);
} else {
char *base = (*env)->GetDirectBufferAddress(env, jsource);

int len = (int) jlen;

uv_buf_t source = uv_buf_init(base, len);

err = bare_worklet_start(worklet, filename, &source, argc, argv);
assert(err == 0);
}

for (int i = 0; i < argc; i++) {
jstring arg = (jstring) (*env)->GetObjectArrayElement(env, jarguments, i);

(*env)->ReleaseStringUTFChars(env, arg, argv[i]);
}

(*env)->ReleaseStringUTFChars(env, jfilename, filename);
}
Expand Down
Loading