From ba6462458cdd6b57f8de1de3dc9e94dfa00928c9 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 20 Apr 2024 22:18:47 +0300 Subject: [PATCH] fix: some fixes in the filesystem --- .../topchetoeu/jscript/lib/FilesystemLib.java | 2 +- .../topchetoeu/jscript/utils/JScriptRepl.java | 44 +++++++++++++++++++ .../jscript/utils/filesystem/BaseFile.java | 2 +- .../utils/filesystem/FilesystemException.java | 6 +-- .../utils/filesystem/PhysicalFilesystem.java | 11 ++--- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/java/me/topchetoeu/jscript/lib/FilesystemLib.java b/src/java/me/topchetoeu/jscript/lib/FilesystemLib.java index b8e5479..6582495 100644 --- a/src/java/me/topchetoeu/jscript/lib/FilesystemLib.java +++ b/src/java/me/topchetoeu/jscript/lib/FilesystemLib.java @@ -51,7 +51,7 @@ public class FilesystemLib { try { if (fs.stat(path).type != EntryType.FILE) { - throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN); + throw new FilesystemException(ErrorReason.DOESNT_EXIST, "Not a file").setAction(ActionType.OPEN).setPath(path); } return new FileLib(fs.open(path, _mode)); diff --git a/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java b/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java index 4ea4b56..bd6085e 100644 --- a/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java +++ b/src/java/me/topchetoeu/jscript/utils/JScriptRepl.java @@ -29,7 +29,11 @@ import me.topchetoeu.jscript.utils.filesystem.Mode; import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem; import me.topchetoeu.jscript.utils.filesystem.RootFilesystem; import me.topchetoeu.jscript.utils.filesystem.STDFilesystem; +import me.topchetoeu.jscript.utils.interop.Arguments; +import me.topchetoeu.jscript.utils.interop.Expose; +import me.topchetoeu.jscript.utils.interop.ExposeType; import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider; +import me.topchetoeu.jscript.utils.interop.WrapperName; import me.topchetoeu.jscript.utils.modules.ModuleRepo; import me.topchetoeu.jscript.utils.permissions.PermissionsManager; import me.topchetoeu.jscript.utils.permissions.PermissionsProvider; @@ -43,6 +47,37 @@ public class JScriptRepl { static int j = 0; static String[] args; + public static interface Interface { + void print(); + } + + public static class Test implements Interface { + public final int a = 10; + public final int b = 5; + + @Override + public void print() { + System.out.println("test!"); + } + } + + @WrapperName("Interface") + public static class InterfaceLib { + @Expose + public static void __print(Arguments args) { args.self(Interface.class).print(); } + } + + @WrapperName("Testificate") + public static class TestLib { + @Expose(type = ExposeType.GETTER) + public static int __a(Arguments args) { return args.self(Test.class).a; } + @Expose(type = ExposeType.GETTER) + public static int __b(Arguments args) { return args.self(Test.class).b; } + + @Expose + public static void __print(Arguments args) { System.out.println("NO!"); } + } + private static void reader() { try { for (var arg : args) { @@ -92,6 +127,9 @@ public class JScriptRepl { var wp = NativeWrapperProvider.get(environment); var glob = GlobalScope.get(environment); + wp.set(Interface.class, InterfaceLib.class); + wp.set(Test.class, TestLib.class); + glob.define(null, false, new NativeFunction("exit", args -> { throw new InterruptException(); })); @@ -113,6 +151,12 @@ public class JScriptRepl { return null; })); + var test = new Test(); + + glob.define(environment, "test1", false, test); + glob.define(environment, "test2", false, test); + glob.define(environment, false, wp.getConstr(TestLib.class)); + var fs = new RootFilesystem(PermissionsProvider.get(environment)); fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE)); fs.protocols.put("file", new PhysicalFilesystem(".")); diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java b/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java index 0253a9d..f552bba 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/BaseFile.java @@ -32,7 +32,7 @@ public abstract class BaseFile implements File { @Override public synchronized long seek(long offset, int pos) { try { if (handle == null) throw new FilesystemException(ErrorReason.CLOSED); - if (!mode.writable) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking."); + if (mode == Mode.NONE) throw new FilesystemException(ErrorReason.NO_PERMISSION, "File not open for seeking."); return onSeek(offset, pos); } catch (FilesystemException e) { throw e.setAction(ActionType.SEEK); } diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/FilesystemException.java b/src/java/me/topchetoeu/jscript/utils/filesystem/FilesystemException.java index cfb434a..802179a 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/FilesystemException.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/FilesystemException.java @@ -54,16 +54,12 @@ public class FilesystemException extends RuntimeException { @Override public String getMessage() { var parts = new ArrayList(10); - path = String.join(" ", parts).trim(); - if (path.isEmpty()) path = null; - parts.clear(); - parts.add(action == null ? "An action performed upon " : action.readable(reason.usePast)); if (entry == EntryType.FILE) parts.add("file"); if (entry == EntryType.FOLDER) parts.add("folder"); - if (path != null) parts.add(path); + if (path != null && !path.isBlank()) parts.add(path.trim()); parts.add(reason.readable); diff --git a/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java b/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java index 52c8323..9ff0c0e 100644 --- a/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java +++ b/src/java/me/topchetoeu/jscript/utils/filesystem/PhysicalFilesystem.java @@ -1,5 +1,6 @@ package me.topchetoeu.jscript.utils.filesystem; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -35,7 +36,8 @@ public class PhysicalFilesystem implements Filesystem { )); else return handles.put(new PhysicalFile(path, perms)); } - catch (IOException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); } + catch (FileNotFoundException e) { throw new FilesystemException(ErrorReason.DOESNT_EXIST); } + catch (IOException e) { throw new FilesystemException(ErrorReason.NO_PERMISSION); } } catch (FilesystemException e) { throw e.setAction(ActionType.OPEN).setPath(_path); } } @@ -68,12 +70,7 @@ public class PhysicalFilesystem implements Filesystem { if (!Files.exists(path)) return new FileStat(Mode.NONE, EntryType.NONE); - var perms = Mode.NONE; - - if (Files.isReadable(path)) { - if (Files.isWritable(path)) perms = Mode.READ_WRITE; - else perms = Mode.READ; - } + var perms = Mode.of(Files.isReadable(path), Files.isWritable(path)); if (perms == Mode.NONE) return new FileStat(Mode.NONE, EntryType.NONE);