Skip to content

Commit

Permalink
fix win platform bug
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalgust committed May 12, 2024
1 parent 0944e66 commit 0d112ba
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 54 deletions.
4 changes: 2 additions & 2 deletions binary/build_jar.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for /f "tokens=1-3 delims=." %%a in ("%java_version%") do (
set /a major1=%%a, minor1=%%b, build1=%%c
)

echo java version :%major1%
echo java version :%minor1%



Expand Down Expand Up @@ -62,7 +62,7 @@ goto :eof
del /Q/S/F %3\%1
md classes
dir /S /B %2\java\*.java > source.txt
if %major1% gtr 8 (
if %minor1% gtr 8 (
%JAVAC% --release 8 -cp %4;%5 -encoding "utf-8" -d classes @source.txt
) else (
%JAVAC% -bootclasspath %4 -cp %5 -encoding "utf-8" -d classes @source.txt
Expand Down
42 changes: 0 additions & 42 deletions extlib/xgui/src/main/java/org/mini/apploader/AppManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,48 +492,6 @@ public boolean accept(File file) {
GToolkit.setStyle(new GStyleDark());
AppLoader.setGuiStyle(1);
break;
case "BT_RUN":
byte[] urls = GLUtil.toCstyleBytes("weixin://");
byte[] more = GLUtil.toCstyleBytes("");
Glfm.glfmOpenOtherApp(urls, more, 0);
// try {
// Process p = Runtime.getRuntime().exec("echo $(date)>>a.txt");
// Process p = Runtime.getRuntime().exec("ls -l");
// Process p = Runtime.getRuntime().exec("/Users/admin/Documents/GitHub/miniJVM/binary/mac_x64/mini_jvm");
// p.waitFor();
// System.out.println("exitValue:" + p.exitValue());
// InputStream in = p.getInputStream();
// while (true) {
// int b = in.read();
// if (b < 0) {
// break;
// }
// System.out.print((char) b);
// }
// System.out.println();
// in=p.getErrorStream();
// while (true) {
// int b = in.read();
// if (b < 0) {
// break;
// }
// System.out.print((char) b);
// }
// System.out.println();
//
// Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// System.out.println("shutdown hook");
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }));
// } catch (IOException e) {
// throw new RuntimeException(e);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
}
}

Expand Down
3 changes: 0 additions & 3 deletions extlib/xgui/src/main/resource/res/ui/AppManager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<table w="100%">
<tr w="100%">
<td w="float"></td>
<td w="50">
<button name="BT_RUN" w="50" h="30" preicon=""></button>
</td>
<td w="50">
<button name="BT_SETTING" w="50" h="30" preicon=""></button>
</td>
Expand Down
1 change: 1 addition & 0 deletions minijvm/c/jvm/jvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,7 @@ Instance *exception_create_str(s32 exception_type, Runtime *runtime, c8 const *e
#if _JVM_DEBUG_LOG_LEVEL > 5
jvm_printf("create exception : %s\n", STRS_CLASS_EXCEPTION[exception_type]);
#endif
if (!errmsg)errmsg = " ";
Utf8String *uerrmsg = utf8_create_c(errmsg);
Instance *jstr = jstring_create(uerrmsg, runtime);
instance_hold_to_thread(jstr, runtime);
Expand Down
40 changes: 33 additions & 7 deletions minijvm/c/jvm/os_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <WinSock2.h>
#include <Ws2tcpip.h>
#include <wspiapi.h>
#include <io.h>

#if __JVM_OS_VS__
#include "../utils/dirent_win.h"
Expand All @@ -19,6 +20,26 @@

#pragma comment(lib, "Ws2_32.lib")

#include <windows.h>
#include <stdio.h>

void get_last_error(Utf8String *error_msg) {
DWORD errorCode = GetLastError(); // 假设之前调用了某个API并发生了错误
LPSTR errorMessage = NULL;
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;

if (FormatMessageA(flags, NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &errorMessage, 0, NULL) != 0) {
printf("Error code %d: %s\n", errorCode, errorMessage);
if (error_msg)utf8_append_c(error_msg, errorMessage);
LocalFree(errorMessage); // 使用完毕后释放由FormatMessage分配的内存
} else {
printf("Failed to get error message for error code %d\n", errorCode);
}
}

/**
* implement clock_gettime windows version same as linux
* @param tv
Expand Down Expand Up @@ -63,7 +84,7 @@ void makePipe(HANDLE p[2], Runtime *runtime) {

s32 success = CreatePipe(p, p + 1, &sa, 0);
if (!success) {
exception_throw(JVM_EXCEPTION_IO, runtime, GetLastError());
exception_throw(JVM_EXCEPTION_IO, runtime, NULL);
}
}

Expand All @@ -74,7 +95,7 @@ s32 descriptor(HANDLE h, Runtime *runtime) {
}


s32 os_execute(Runtime *runtime, Instance *jstrArr, Instance *jlongArr, ArrayList *cstrList, const c8* cmd) {
s32 os_execute(Runtime *runtime, Instance *jstrArr, Instance *jlongArr, ArrayList *cstrList, const c8 *cmd) {

HANDLE in[] = {0, 0};
HANDLE out[] = {0, 0};
Expand Down Expand Up @@ -127,39 +148,44 @@ s32 os_execute(Runtime *runtime, Instance *jstrArr, Instance *jlongArr, ArrayLis
&si,
&pi);


CloseHandle(in[1]);
CloseHandle(out[0]);
CloseHandle(err[1]);

if (!success) {
exception_throw(JVM_EXCEPTION_IO, runtime, NULL);
Utf8String *cstr = utf8_create();
//get_last_error(cstr);
exception_throw(JVM_EXCEPTION_IO, runtime, utf8_cstr(cstr));
return RUNTIME_STATUS_EXCEPTION;
}

s64 pid = (s64) (intptr_t) (pi.hProcess);
jarray_set_field(jlongArr, 0, pid);
s64 tid = (s64) (intptr_t) (pi.hThread);
jarray_set_field(jlongArr, 1, tid);
return 0;
}

s32 os_kill_process(s64 pid) {
TerminateProcess(pid, 1);
TerminateProcess((HANDLE) (intptr_t) pid, 1);
return 0;
}

s32 os_waitfor_process(Runtime *runtime, s64 pid, s64 tid, s32 *pExitCode) {
DWORD exitCode;
WaitForSingleObject((__refer) (intptr_t) (pid), INFINITE);
BOOL success = GetExitCodeProcess((HANDLE)(pid), &exitCode);
BOOL success = GetExitCodeProcess((HANDLE) (pid), &exitCode);
if (!success) {
exception_throw(JVM_EXCEPTION_ILLEGALARGUMENT, runtime, NULL);
return RUNTIME_STATUS_EXCEPTION;
}

CloseHandle((HANDLE)(pid));
CloseHandle((HANDLE)(tid));
CloseHandle((HANDLE) (pid));
CloseHandle((HANDLE) (tid));

*pExitCode = exitCode;
return 0;
}

Utf8String *os_get_tmp_dir() {
Expand Down
2 changes: 2 additions & 0 deletions test/jvm_vs/mini_jvm.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@
<ClCompile Include="..\..\minijvm\c\jvm\jni_std.c" />
<ClCompile Include="..\..\minijvm\c\jvm\jvm.c" />
<ClCompile Include="..\..\minijvm\c\jvm\jvm_util.c" />
<ClCompile Include="..\..\minijvm\c\jvm\os_posix.c" />
<ClCompile Include="..\..\minijvm\c\jvm\os_win.c" />
<ClCompile Include="..\..\minijvm\c\jvm\runtime.c" />
<ClCompile Include="..\..\minijvm\c\main.c" />
<ClCompile Include="..\..\minijvm\c\utils\arraylist.c" />
Expand Down

0 comments on commit 0d112ba

Please sign in to comment.