Skip to content

Commit

Permalink
fix: mac support
Browse files Browse the repository at this point in the history
  • Loading branch information
xxDark committed May 1, 2022
1 parent f1d2070 commit 5704398
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'dev.xdark'
version '1.6.0'
version '1.6.1'

repositories {
mavenCentral()
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/xdark/ssvm/fs/FileDescriptorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,9 @@ public interface FileDescriptorManager {
* if not found.
*/
ZipFile getZipFile(long handle);

/**
* @return current working directory.
*/
String getCurrentWorkingDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,9 @@ public long openZipFile(String path, int mode) throws IOException {
public ZipFile getZipFile(long handle) {
return zipFiles.get(handle);
}

@Override
public String getCurrentWorkingDirectory() {
return new File("").getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.xdark.ssvm.fs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -74,4 +75,9 @@ public long openZipFile(String path, int mode) throws IOException {
public ZipFile getZipFile(long handle) {
return null;
}

@Override
public String getCurrentWorkingDirectory() {
return new File("").getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import dev.xdark.ssvm.VirtualMachine;
import dev.xdark.ssvm.api.MethodInvoker;
import dev.xdark.ssvm.execution.Result;
import dev.xdark.ssvm.mirror.InstanceJavaClass;
import lombok.experimental.UtilityClass;
import lombok.val;

import java.nio.charset.StandardCharsets;

/**
* Initializes sun/nio/fs/WindowsNativeDispatcher.
*
Expand All @@ -20,12 +23,35 @@ public class FileSystemNativeDispatcherNatives {
*/
public void init(VirtualMachine vm) {
val vmi = vm.getInterface();
InstanceJavaClass nativeDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/WindowsNativeDispatcher");
if (nativeDispatcher == null) {
nativeDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/LinuxNativeDispatcher");
vmi.setInvoker(nativeDispatcher, "init", "()V", MethodInvoker.noop());
val windowsDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/WindowsNativeDispatcher");
if (windowsDispatcher == null) {
val unixDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/UnixNativeDispatcher") ;
vmi.setInvoker(unixDispatcher, "getcwd", "()[B", ctx -> {
val cwd = vm.getFileDescriptorManager().getCurrentWorkingDirectory().getBytes(StandardCharsets.UTF_8);
ctx.setResult(vm.getHelper().toVMBytes(cwd));
return Result.ABORT;
});
vmi.setInvoker(unixDispatcher, "init", "()V", MethodInvoker.noop());
val linuxDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/LinuxNativeDispatcher");
if (linuxDispatcher != null) {
vmi.setInvoker(linuxDispatcher, "init", "()V", MethodInvoker.noop());
} else {
val bsdDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nio/fs/BsdNativeDispatcher");
vmi.setInvoker(bsdDispatcher, "initIDs", "()V", MethodInvoker.noop());
val macDispatcher = (InstanceJavaClass) vm.findBootstrapClass("sun/nui/fs/MacOSXNativeDispatcher");
if (macDispatcher != null) {
vmi.setInvoker(macDispatcher, "normalizepath", "([CI)[C", ctx -> {
val locals = ctx.getLocals();
val helper = vm.getHelper();
val path = helper.checkNotNullArray(locals.load(0));
// int form = locals.load(1).asInt();
ctx.setResult(path);
return Result.ABORT;
});
}
}
} else {
vmi.setInvoker(nativeDispatcher, "initIDs", "()V", MethodInvoker.noop());
vmi.setInvoker(windowsDispatcher, "initIDs", "()V", MethodInvoker.noop());
}
}
}

0 comments on commit 5704398

Please sign in to comment.