
class Downie {
	
	__objToString(obj) {
		var str = "[\n";
		for (const [p, val] of Object.entries(obj)) {
			str += `\t${p} -> ${val},\n`;
		}
		str += "]";
		return str;
	}
	
	context = __context_placeholder__;
	
	log(message) {
		var msg = "";
		if (message === undefined) {
			msg = "undefined";
		} else if (message === null) {
			msg = "null";
		} else if (!isNaN(message)) {
			// Number.
			msg = "" + message;
		} else if (typeof message === 'string' || message instanceof String) {
			msg = message;
		} else {
			msg = this.__objToString(message);
		}
		window.webkit.messageHandlers.log.postMessage(msg);
	}
	
	reportError(err) {
		window.webkit.messageHandlers.reportError.postMessage(err);
	}
	
	reportDownload(download) {
		window.webkit.messageHandlers.reportDownload.postMessage(download);
	}
	
	reportEmbeddedDownload(download) {
		window.webkit.messageHandlers.reportEmbeddedDownload.postMessage(download);
	}
	
	reportDone() {
		window.webkit.messageHandlers.reportDone.postMessage(true);
	}
	
};


window.downie = new Downie();
