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

[WIP] Fix caught callbacks #223

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Next Next commit
WIP: remove most callbacks from try blocks
Fix doubled callbacks, called once to handle results and then a second time to handle an error thrown in the callback

Does not compile!

Yet to do FS.ts

Need to revisit:
* AsyncMirror.Create
* IndexedDBROTransaction.get
* IndexedDBRWTransaction.put, .del
* IndexedDBStore.clear
* IndexedDBFileSystem.create
* OverlayFS.Create
* ZipFS.Create
* ZipFS._computeIndex
1j01 committed Mar 30, 2018
commit 73bac54ed2838fb7bfd1986bdbc4c8d5bb09e335
24 changes: 18 additions & 6 deletions src/backend/Emscripten.ts
Original file line number Diff line number Diff line change
@@ -60,10 +60,14 @@ export class EmscriptenFile extends BaseFile implements File {
}
}
public stat(cb: BFSCallback<Stats>): void {
let res;
let err: ApiError | null = null;
try {
cb(null, this.statSync());
res = this.statSync();
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, res);
}
}
public statSync(): Stats {
@@ -91,10 +95,14 @@ export class EmscriptenFile extends BaseFile implements File {
}
}
public write(buffer: NodeBuffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
let res;
let err: ApiError | null = null;
try {
cb(null, this.writeSync(buffer, offset, length, position), buffer);
res = this.writeSync(buffer, offset, length, position);
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, res, buffer);
}
}
public writeSync(buffer: NodeBuffer, offset: number, length: number, position: number | null): number {
@@ -108,10 +116,14 @@ export class EmscriptenFile extends BaseFile implements File {
}
}
public read(buffer: NodeBuffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
let res;
let err: ApiError | null = null;
try {
cb(null, this.readSync(buffer, offset, length, position), buffer);
res = this.readSync(buffer, offset, length, position);
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, res, buffer);
}
}
public readSync(buffer: NodeBuffer, offset: number, length: number, position: number | null): number {
16 changes: 12 additions & 4 deletions src/backend/HTTPRequest.ts
Original file line number Diff line number Diff line change
@@ -16,10 +16,14 @@ import {FileIndex, isFileInode, isDirInode} from '../generic/file_index';
* @hidden
*/
function tryToString(buff: Buffer, encoding: string, cb: BFSCallback<string>) {
let string: string | null = null;
let err: ApiError | null = null;
try {
cb(null, buff.toString(encoding));
string = buff.toString(encoding);
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, string);
}
}

@@ -339,10 +343,14 @@ export default class HTTPRequest extends BaseFileSystem implements FileSystem {
}

public readdir(path: string, cb: BFSCallback<string[]>): void {
let res;
let err: ApiError | null = null;
try {
cb(null, this.readdirSync(path));
res = this.readdirSync(path);
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, res);
}
}

6 changes: 3 additions & 3 deletions src/backend/IndexedDB.ts
Original file line number Diff line number Diff line change
@@ -115,13 +115,13 @@ export class IndexedDBRWTransaction extends IndexedDBROTransaction implements As
}

public abort(cb: BFSOneArgCallback): void {
let _e: ApiError | null = null;
let err: ApiError | null = null;
try {
this.tx.abort();
} catch (e) {
_e = convertError(e);
err = convertError(e);
} finally {
cb(_e);
cb(err);
}
}
}
10 changes: 8 additions & 2 deletions src/backend/ZipFS.ts
Original file line number Diff line number Diff line change
@@ -626,11 +626,17 @@ export default class ZipFS extends SynchronousFileSystem implements FileSystem {
}

private static _computeIndexResponsiveTrampoline(data: Buffer, index: FileIndex<CentralDirectory>, cdPtr: number, cdEnd: number, cb: BFSCallback<ZipTOC>, cdEntries: CentralDirectory[], eocd: EndOfCentralDirectory) {
let err: ApiError | null = null;
let zipTOC: ZipTOC | null = null;
try {
ZipFS._computeIndexResponsive(data, index, cdPtr, cdEnd, cb, cdEntries, eocd);
ZipFS._computeIndexResponsive(data, index, cdPtr, cdEnd, (_err: ApiError | null, _zipTOC: ZipTOC | null)=> {
err = _err;
zipTOC = _zipTOC;
}, cdEntries, eocd);
} catch (e) {
cb(e);
return cb(e);
}
cb(err, zipTOC);
}

private static _computeIndexResponsive(data: Buffer, index: FileIndex<CentralDirectory>, cdPtr: number, cdEnd: number, cb: BFSCallback<ZipTOC>, cdEntries: CentralDirectory[], eocd: EndOfCentralDirectory) {
68 changes: 40 additions & 28 deletions src/core/file_system.ts
Original file line number Diff line number Diff line change
@@ -660,10 +660,14 @@ export class BaseFileSystem {
} else if (encoding === null) {
return cb(err, buf);
}
// TODO: Share tryToString helper with HTTPRequest.ts
let string: string;
try {
cb(null, buf.toString(encoding));
string = buf.toString(encoding);
} catch (e) {
cb(e);
err = e;
} finally {
cb(err, string);
}
});
});
@@ -803,113 +807,121 @@ export class SynchronousFileSystem extends BaseFileSystem {
public rename(oldPath: string, newPath: string, cb: BFSOneArgCallback): void {
try {
this.renameSync(oldPath, newPath);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public stat(p: string, isLstat: boolean | null, cb: BFSCallback<Stats>): void {
let res: Stats | null;
try {
cb(null, this.statSync(p, isLstat));
res = this.statSync(p, isLstat);
} catch (e) {
cb(e);
return cb(e);
}
cb(null, res);
}

public open(p: string, flags: FileFlag, mode: number, cb: BFSCallback<File>): void {
let res: File | null;
try {
cb(null, this.openSync(p, flags, mode));
res = this.openSync(p, flags, mode);
} catch (e) {
cb(e);
return cb(e);
}
cb(null, res);
}

public unlink(p: string, cb: BFSOneArgCallback): void {
try {
this.unlinkSync(p);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public rmdir(p: string, cb: BFSOneArgCallback): void {
try {
this.rmdirSync(p);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public mkdir(p: string, mode: number, cb: BFSOneArgCallback): void {
try {
this.mkdirSync(p, mode);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public readdir(p: string, cb: BFSCallback<string[]>): void {
let res: string[] | null;
try {
cb(null, this.readdirSync(p));
res = this.readdirSync(p);
} catch (e) {
cb(e);
return cb(e);
}
cb(null, res);
}

public chmod(p: string, isLchmod: boolean, mode: number, cb: BFSOneArgCallback): void {
try {
this.chmodSync(p, isLchmod, mode);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public chown(p: string, isLchown: boolean, uid: number, gid: number, cb: BFSOneArgCallback): void {
try {
this.chownSync(p, isLchown, uid, gid);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public utimes(p: string, atime: Date, mtime: Date, cb: BFSOneArgCallback): void {
try {
this.utimesSync(p, atime, mtime);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public link(srcpath: string, dstpath: string, cb: BFSOneArgCallback): void {
try {
this.linkSync(srcpath, dstpath);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public symlink(srcpath: string, dstpath: string, type: string, cb: BFSOneArgCallback): void {
try {
this.symlinkSync(srcpath, dstpath, type);
cb();
} catch (e) {
cb(e);
return cb(e);
}
cb();
}

public readlink(p: string, cb: BFSCallback<string>): void {
let res: string | null;
try {
cb(null, this.readlinkSync(p));
res = this.readlinkSync(p);
} catch (e) {
cb(e);
return cb(e);
}
cb(null, res);
}
}
7 changes: 5 additions & 2 deletions src/generic/key_value_filesystem.ts
Original file line number Diff line number Diff line change
@@ -1087,14 +1087,17 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
} else {
tx.get(inode.id, (e: ApiError, data?: Buffer): void => {
if (noError(e, cb)) {
let err: ApiError | null;
let res;
try {
cb(null, JSON.parse(data!.toString()));
res = JSON.parse(data!.toString());
} catch (e) {
// Occurs when data is undefined, or corresponds to something other
// than a directory listing. The latter should never occur unless
// the file system is corrupted.
cb(ApiError.ENOENT(p));
err = ApiError.ENOENT(p);
}
cb(err, res);
}
});
}
Loading