diff --git a/src/me/topchetoeu/jscript/js/lib.d.ts b/src/me/topchetoeu/jscript/js/lib.d.ts index 490ea1a..141e7cf 100644 --- a/src/me/topchetoeu/jscript/js/lib.d.ts +++ b/src/me/topchetoeu/jscript/js/lib.d.ts @@ -489,8 +489,11 @@ interface FileStat { interface File { readonly pointer: Promise; readonly length: Promise; - read(buff: number[], n: number): Promise; + readonly mode: Promise<'' | 'r' | 'rw'>; + + read(n: number): Promise; write(buff: number[]): Promise; + close(): Promise; setPointer(val: number): Promise; } interface Filesystem { @@ -503,6 +506,11 @@ interface Filesystem { exists(path: string): Promise; } +interface Encoding { + encode(val: string): number[]; + decode(val: number[]): string; +} + declare var String: StringConstructor; //@ts-ignore declare const arguments: IArguments; @@ -529,6 +537,7 @@ declare var Object: ObjectConstructor; declare var Symbol: SymbolConstructor; declare var Promise: PromiseConstructor; declare var Math: MathObject; +declare var Encoding: Encoding; declare var fs: Filesystem; declare var Error: ErrorConstructor; diff --git a/src/me/topchetoeu/jscript/lib/FileLib.java b/src/me/topchetoeu/jscript/lib/FileLib.java index 11c042f..8918f10 100644 --- a/src/me/topchetoeu/jscript/lib/FileLib.java +++ b/src/me/topchetoeu/jscript/lib/FileLib.java @@ -12,6 +12,35 @@ import me.topchetoeu.jscript.interop.NativeGetter; public class FileLib { public final File file; + @NativeGetter public PromiseLib pointer(Context ctx) { + return PromiseLib.await(ctx, () -> { + try { + return file.getPtr(); + } + catch (FilesystemException e) { throw e.toEngineException(); } + }); + } + @NativeGetter public PromiseLib length(Context ctx) { + return PromiseLib.await(ctx, () -> { + try { + long curr = file.getPtr(); + file.setPtr(0, 2); + long res = file.getPtr(); + file.setPtr(curr, 0); + return res; + } + catch (FilesystemException e) { throw e.toEngineException(); } + }); + } + @NativeGetter public PromiseLib getMode(Context ctx) { + return PromiseLib.await(ctx, () -> { + try { + return file.mode().name; + } + catch (FilesystemException e) { throw e.toEngineException(); } + }); + } + @Native public PromiseLib read(Context ctx, int n) { return PromiseLib.await(ctx, () -> { try { @@ -44,14 +73,6 @@ public class FileLib { return null; }); } - @NativeGetter public PromiseLib pointer(Context ctx) { - return PromiseLib.await(ctx, () -> { - try { - return file.getPtr(); - } - catch (FilesystemException e) { throw e.toEngineException(); } - }); - } @Native public PromiseLib setPointer(Context ctx, long ptr) { return PromiseLib.await(ctx, () -> { try { @@ -61,26 +82,6 @@ public class FileLib { catch (FilesystemException e) { throw e.toEngineException(); } }); } - @NativeGetter("length") public PromiseLib getLength(Context ctx) { - return PromiseLib.await(ctx, () -> { - try { - long curr = file.getPtr(); - file.setPtr(0, 2); - long res = file.getPtr(); - file.setPtr(curr, 0); - return res; - } - catch (FilesystemException e) { throw e.toEngineException(); } - }); - } - @NativeGetter("mode") public PromiseLib getMode(Context ctx) { - return PromiseLib.await(ctx, () -> { - try { - return file.mode().name; - } - catch (FilesystemException e) { throw e.toEngineException(); } - }); - } public FileLib(File file) { this.file = file; diff --git a/src/me/topchetoeu/jscript/lib/PromiseLib.java b/src/me/topchetoeu/jscript/lib/PromiseLib.java index 9766ee6..4489041 100644 --- a/src/me/topchetoeu/jscript/lib/PromiseLib.java +++ b/src/me/topchetoeu/jscript/lib/PromiseLib.java @@ -289,7 +289,7 @@ import me.topchetoeu.jscript.interop.Native; ctx.engine.pushMsg(true, ctx, new NativeFunction((_ctx, _thisArg, _args) -> { for (var handle : handles) handle.rejected.call(handle.ctx, null, val); - if (handles.size() == 0) { + if (!handled) { Values.printError(new EngineException(val).setCtx(ctx.environment(), ctx.engine), "(in promise)"); } handles = null;