<commit_before>import { Event } from './events';

const { Uri } = Windows.Foundation;

const arch = ['x86-32', 'arm', 'x86-64'][Windows.ApplicationModel.Package.current.id.architecture] || 'unknown';

interface Callback<R> {
	(result?: R): void;
}

var lastError: Error = undefined;
var checkedLastError: boolean = true;

export async function doAsync<R>(func: (...args: any[]) => PromiseLike<R>, context: any, args: any[]) {
	let i = args.length - 1;
	for (; i >= 0 && args[i] === undefined; i--);
	let callback: Callback<R>;
	if (i >= 0) {
		let arg = args[i];
		if (typeof arg === 'function') {
			callback = arg as Callback<R>;
			i--;
		}
	}
	let res: R;
	try {
		res = await func.apply(context, args.slice(0, i + 1));
		lastError = undefined;
		checkedLastError = true;
	} catch (e) {
		lastError = e;
		checkedLastError = false;
	}
	if (callback) {
		callback();
	}
	if (!checkedLastError) {
		checkedLastError = true;
		throw new Error(`Unchecked runtime.lastError: ${lastError}.`);
	}
}

export function wrapAsync<R>(func: () => PromiseLike<R>): (callback: Callback<R>) => void;
export function wrapAsync<T0, R>(func: (arg0: T0) => PromiseLike<R>): (arg0: T0, callback?: Callback<R>) => void;
export function wrapAsync<T0, T1, R>(func: (arg0: T0, arg1: T1) => PromiseLike<R>): (arg0: T0, arg1: T1, callback?: Callback<R>) => void;
export function wrapAsync<T0, T1, T2, R>(func: (arg0: T0, arg1: T1, arg2: T2) => PromiseLike<R>): (arg0: T0, arg1: T1, arg2: T2, callback?: Callback<R>) => void;
export function wrapAsync<T, R>(func: (...args: T[]) => PromiseLike<R>): (...args: (T | Callback<R>)[]) => void {
	return function(...args) {
		doAsync(func, this, args);
	};
}

const onConnect: chrome.runtime.ExtensionConnectEvent = new Event();
const onConnectExternal: chrome.runtime.RuntimeEvent = new Event();
const onSuspend: chrome.runtime.RuntimeEvent = new Event();
const onStartup: chrome.runtime.RuntimeEvent = new Event();
const onInstalled: chrome.runtime.RuntimeInstalledEvent = new Event();
const onSuspendCanceled: chrome.runtime.RuntimeEvent = new Event();
const onMessage: chrome.runtime.ExtensionMessageEvent = new Event();
const onMessageExternal: chrome.runtime.ExtensionMessageEvent = new Event();
const onRestartRequired: chrome.runtime.RuntimeRestartRequiredEvent = new Event();
const onUpdateAvailable: chrome.runtime.RuntimeUpdateAvailableEvent = new Event();
const onBrowserUpdateAvailable: chrome.runtime.RuntimeEvent = new Event();
<commit_msg>Fix doAsync not passing result to the callback.<commit_after>import { Event } from './events';

const { Uri } = Windows.Foundation;

const arch = ['x86-32', 'arm', 'x86-64'][Windows.ApplicationModel.Package.current.id.architecture] || 'unknown';

interface Callback<R> {
	(result?: R): void;
}

var lastError: Error = undefined;
var checkedLastError: boolean = true;

export async function doAsync<R>(func: (...args: any[]) => PromiseLike<R>, context: any, args: any[]) {
	let i = args.length - 1;
	for (; i >= 0 && args[i] === undefined; i--);
	let callback: Callback<R>;
	if (i >= 0) {
		let arg = args[i];
		if (typeof arg === 'function') {
			callback = arg as Callback<R>;
			i--;
		}
	}
	let res: R;
	try {
		res = await func.apply(context, args.slice(0, i + 1));
		lastError = undefined;
		checkedLastError = true;
	} catch (e) {
		lastError = e;
		checkedLastError = false;
	}
	if (callback) {
		callback(res);
	}
	if (!checkedLastError) {
		checkedLastError = true;
		throw new Error(`Unchecked runtime.lastError: ${lastError}.`);
	}
}

export function wrapAsync<R>(func: () => PromiseLike<R>): (callback: Callback<R>) => void;
export function wrapAsync<T0, R>(func: (arg0: T0) => PromiseLike<R>): (arg0: T0, callback?: Callback<R>) => void;
export function wrapAsync<T0, T1, R>(func: (arg0: T0, arg1: T1) => PromiseLike<R>): (arg0: T0, arg1: T1, callback?: Callback<R>) => void;
export function wrapAsync<T0, T1, T2, R>(func: (arg0: T0, arg1: T1, arg2: T2) => PromiseLike<R>): (arg0: T0, arg1: T1, arg2: T2, callback?: Callback<R>) => void;
export function wrapAsync<T, R>(func: (...args: T[]) => PromiseLike<R>): (...args: (T | Callback<R>)[]) => void {
	return function(...args) {
		doAsync(func, this, args);
	};
}

const onConnect: chrome.runtime.ExtensionConnectEvent = new Event();
const onConnectExternal: chrome.runtime.RuntimeEvent = new Event();
const onSuspend: chrome.runtime.RuntimeEvent = new Event();
const onStartup: chrome.runtime.RuntimeEvent = new Event();
const onInstalled: chrome.runtime.RuntimeInstalledEvent = new Event();
const onSuspendCanceled: chrome.runtime.RuntimeEvent = new Event();
const onMessage: chrome.runtime.ExtensionMessageEvent = new Event();
const onMessageExternal: chrome.runtime.ExtensionMessageEvent = new Event();
const onRestartRequired: chrome.runtime.RuntimeRestartRequiredEvent = new Event();
const onUpdateAvailable: chrome.runtime.RuntimeUpdateAvailableEvent = new Event();
const onBrowserUpdateAvailable: chrome.runtime.RuntimeEvent = new Event();

{'fraction_non_alphanumeric': 0.16823741728494085, 'fraction_numerical': 0.01724483657509525, 'mean_word_length': 5.120245398773006, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4575522', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1656, 'n_words': 459}