-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bad(Swingset): Releasing a constrained Zalgo on syscall replay
- Loading branch information
Showing
6 changed files
with
94 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @template T | ||
* @typedef {T extends PromiseLike<any> ? never : T} SyncOnly | ||
*/ | ||
|
||
/** | ||
* @template {boolean} AllowAsync | ||
* @template T | ||
* @typedef {true extends AllowAsync ? (Promise<T> | T) : T} MaybePromiseResult | ||
*/ | ||
|
||
/** | ||
* WARNING: Do not use, this is releasing Zalgo | ||
* | ||
* @template T | ||
* @template R | ||
* @param {T} v | ||
* @param {(v: Awaited<T>) => R} handle | ||
* @returns {T extends PromiseLike<any> ? Promise<Awaited<R>> : R} | ||
*/ | ||
export function maybeAsync(v, handle) { | ||
if (typeof v === 'object' && v !== null && 'then' in v) { | ||
return Promise.resolve(v).then(handle); | ||
} else { | ||
return handle(v); | ||
} | ||
} | ||
|
||
/** | ||
* Somewhat tames Zalgo | ||
* | ||
* @template {(...args: any[]) => any} T | ||
* @param {T} fn | ||
* @returns {(...args: Parameters<T>) => SyncOnly<ReturnType<T>>} | ||
*/ | ||
export function ensureSync(fn) { | ||
// eslint-disable-next-line func-names | ||
return function (...args) { | ||
const result = Reflect.apply(fn, this, args); | ||
if (typeof result === 'object' && result !== null && 'then' in result) { | ||
throw new Error('Unexpected async result'); | ||
} else { | ||
return result; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters