yosys/misc/yosysjs/yosysjs.js

313 lines
8.0 KiB
JavaScript
Raw Normal View History

2015-02-16 05:41:48 -06:00
var YosysJS = new function() {
this.script_element = document.currentScript;
this.viz_element = undefined;
2015-02-21 07:25:34 -06:00
this.viz_ready = true;
2015-02-16 05:41:48 -06:00
this.url_prefix = this.script_element.src.replace(/[^/]+$/, '')
this.load_viz = function() {
if (this.viz_element)
return;
this.viz_element = document.createElement('iframe')
this.viz_element.style.display = 'none'
document.body.appendChild(this.viz_element);
2015-02-21 07:25:34 -06:00
this.viz_element.contentWindow.document.open();
this.viz_element.contentWindow.document.write('<script type="text/javascript" onload="viz_ready = true;" src="' + this.url_prefix + 'viz.js"></' + 'script>');
this.viz_element.contentWindow.document.close();
var that = this;
function check_viz_ready() {
if (that.viz_element.contentWindow.viz_ready) {
console.log("YosysJS: Successfully loaded Viz.");
that.viz_ready = true;
} else
window.setTimeout(check_viz_ready, 100);
}
this.viz_ready = false;
window.setTimeout(check_viz_ready, 100);
2015-02-16 05:41:48 -06:00
}
this.dot_to_svg = function(dot_text) {
return this.viz_element.contentWindow.Viz(dot_text, "svg");
}
this.dot_into_svg = function(dot_text, svg_element) {
2015-02-16 07:10:00 -06:00
if (typeof(svg_element) == 'string' && svg_element != "")
2015-02-16 06:23:54 -06:00
svg_element = document.getElementById(svg_element);
2015-02-16 05:41:48 -06:00
svg_element.innerHTML = this.dot_to_svg(dot_text);
c = svg_element.firstChild;
while (c) {
if (c.tagName == 'svg') {
while (c.firstChild)
svg_element.appendChild(c.firstChild);
svg_element.setAttribute('viewBox', c.getAttribute('viewBox'));
// svg_element.removeChild(c);
break;
}
c = c.nextSibling;
}
}
this.create = function(reference_element, on_ready) {
var ys = new Object();
2015-02-21 07:25:34 -06:00
ys.YosysJS = this;
2015-02-16 05:41:48 -06:00
ys.init_script = "";
ys.ready = false;
ys.verbose = false;
2015-02-16 07:10:00 -06:00
ys.logprint = false;
2015-02-16 05:41:48 -06:00
ys.echo = false;
2015-02-19 06:36:54 -06:00
ys.errmsg = "";
2015-02-16 05:41:48 -06:00
2015-02-16 07:10:00 -06:00
if (typeof(reference_element) == 'string' && reference_element != "")
2015-02-16 05:41:48 -06:00
reference_element = document.getElementById(reference_element);
if (reference_element) {
if (reference_element.tagName == 'textarea')
ys.init_script = reference_element.value;
2015-07-02 04:14:30 -05:00
2015-02-16 05:41:48 -06:00
if (reference_element.tagName == 'iframe') {
ys.iframe_element = reference_element;
} else {
ys.iframe_element = document.createElement('iframe');
ys.iframe_element.id = reference_element.id;
for (i in reference_element.style)
ys.iframe_element.style[i] = reference_element.style[i];
reference_element.parentNode.insertBefore(ys.iframe_element, reference_element);
reference_element.parentNode.removeChild(reference_element);
}
} else {
ys.iframe_element = document.createElement('iframe');
2015-02-16 06:23:54 -06:00
ys.iframe_element.style.display = 'none';
2015-02-16 05:41:48 -06:00
document.body.appendChild(ys.iframe_element);
}
2015-02-16 06:23:54 -06:00
ys.print_buffer = "";
ys.last_line_empty = false;
ys.got_normal_log_message = false;
ys.window = ys.iframe_element.contentWindow;
2015-02-16 05:41:48 -06:00
2015-02-16 06:23:54 -06:00
var doc = ys.window.document;
var mod = ys.window.Module = {
2015-02-16 05:41:48 -06:00
print: function(text) {
2015-02-16 06:23:54 -06:00
if (typeof(text) == 'number')
return;
ys.print_buffer += text + "\n";
ys.got_normal_log_message = true;
2015-02-16 07:10:00 -06:00
if (ys.logprint)
console.log(text);
2015-02-16 05:41:48 -06:00
if (ys.verbose) {
2015-02-16 06:23:54 -06:00
ys.last_line_empty = text == "";
2015-02-16 07:10:00 -06:00
if (text == "") {
span = doc.createElement('br');
} else {
span = doc.createElement('span');
span.textContent = text + "\n";
span.style.fontFamily = 'monospace';
span.style.whiteSpace = 'pre';
}
doc.firstChild.appendChild(span);
if (doc.body)
ys.window.scrollTo(0, doc.body.scrollHeight);
else
ys.window.scrollBy(0, 100);
2015-02-16 05:41:48 -06:00
}
ys.ready = true;
},
printErr: function(text) {
2015-02-16 06:23:54 -06:00
if (typeof(text) == 'number')
return;
2015-02-16 07:10:00 -06:00
if (ys.logprint)
console.log(text);
2015-02-16 06:23:54 -06:00
if (ys.got_normal_log_message) {
ys.print_buffer += text + "\n";
ys.last_line_empty = text == "";
2015-02-16 07:10:00 -06:00
if (text == "") {
span = doc.createElement('br');
} else {
span = doc.createElement('span');
span.textContent = text + "\n";
span.style.fontFamily = 'monospace';
span.style.whiteSpace = 'pre';
span.style.color = 'red';
}
doc.firstChild.appendChild(span);
if (doc.body)
ys.window.scrollTo(0, doc.body.scrollHeight);
else
ys.window.scrollBy(0, 100);
} else
if (!ys.logprint)
2015-02-16 06:23:54 -06:00
console.log(text);
2015-02-16 05:41:48 -06:00
},
};
ys.write = function(text) {
2015-02-16 06:23:54 -06:00
ys.print_buffer += text + "\n";
ys.last_line_empty = text == "";
2015-02-16 05:41:48 -06:00
span = doc.createElement('span');
span.textContent = text + "\n";
span.style.fontFamily = 'monospace';
span.style.whiteSpace = 'pre';
2015-02-19 06:55:36 -06:00
doc.firstChild.appendChild(span);
if (doc.body)
ys.window.scrollTo(0, doc.body.scrollHeight);
else
ys.window.scrollBy(0, 100);
2015-02-16 05:41:48 -06:00
}
ys.prompt = function() {
return mod.ccall('prompt', 'string', [], [])
}
ys.run = function(cmd) {
2015-02-16 06:23:54 -06:00
ys.print_buffer = "";
2015-02-16 05:41:48 -06:00
if (ys.echo) {
2015-02-16 06:23:54 -06:00
if (!ys.last_line_empty)
2015-02-16 05:41:48 -06:00
ys.write("");
ys.write(ys.prompt() + cmd);
}
2015-02-19 06:36:54 -06:00
try {
mod.ccall('run', '', ['string'], [cmd]);
} catch (e) {
2015-06-28 10:47:58 -05:00
ys.errmsg = mod.ccall('errmsg', 'string', [], []);
2015-02-19 06:36:54 -06:00
}
2015-02-16 06:23:54 -06:00
return ys.print_buffer;
2015-02-16 05:41:48 -06:00
}
ys.read_file = function(filename) {
2015-02-19 06:36:54 -06:00
try {
return ys.window.FS.readFile(filename, {encoding: 'utf8'});
} catch (e) {
return "";
}
2015-02-16 05:41:48 -06:00
}
ys.write_file = function(filename, text) {
2015-02-16 06:23:54 -06:00
return ys.window.FS.writeFile(filename, text, {encoding: 'utf8'});
2015-02-16 05:41:48 -06:00
}
ys.read_dir = function(dirname) {
2015-02-16 06:23:54 -06:00
return ys.window.FS.readdir(dirname);
2015-02-16 05:41:48 -06:00
}
2015-02-19 06:36:54 -06:00
ys.remove_file = function(filename) {
try {
ys.window.FS.unlink(filename);
} catch (e) { }
}
2015-02-21 07:25:34 -06:00
doc.open();
2015-02-16 07:10:00 -06:00
doc.write('<script type="text/javascript" src="' + this.url_prefix + 'yosys.js"></' + 'script>');
2015-02-21 07:25:34 -06:00
doc.close();
2015-02-16 05:41:48 -06:00
if (on_ready || ys.init_script) {
function check_ready() {
2015-02-21 07:25:34 -06:00
if (ys.ready && ys.YosysJS.viz_ready) {
2015-02-16 05:41:48 -06:00
if (ys.init_script) {
ys.write_file("/script.ys", ys.init_script);
ys.run("script /script.ys");
}
if (on_ready)
on_ready(ys);
} else
window.setTimeout(check_ready, 100);
}
window.setTimeout(check_ready, 100);
}
return ys;
}
2015-06-28 10:47:58 -05:00
this.create_worker = function(on_ready) {
var ys = new Object();
ys.YosysJS = this;
ys.worker = new Worker(this.url_prefix + 'yosyswrk.js');
2015-06-28 10:47:58 -05:00
ys.callback_idx = 1;
ys.callback_cache = {};
2015-07-04 10:08:44 -05:00
ys.errmsg = "";
2015-06-28 10:47:58 -05:00
ys.callback_cache[0] = on_ready;
on_ready = null;
ys.worker.onmessage = function(e) {
var response = e.data[0];
var callback = ys.callback_cache[response.idx];
delete ys.callback_cache[response.idx];
2015-07-04 10:08:44 -05:00
if ("errmsg" in response) ys.errmsg = response.errmsg;
2015-06-28 10:47:58 -05:00
if (callback) callback.apply(null, response.args);
}
ys.run = function(cmd, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "run",
"cmd": cmd
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
ys.read_file = function(filename, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "read_file",
"filename": filename
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
ys.write_file = function(filename, text, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "write_file",
"filename": filename,
"text": text
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
ys.read_dir = function(dirname, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "read_dir",
"dirname": dirname
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
ys.remove_file = function(filename, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "remove_file",
"filename": filename
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
2015-07-04 10:08:44 -05:00
ys.verbose = function(value, callback) {
var request = {
"idx": ys.callback_idx,
"mode": "verbose",
"value": value
};
ys.callback_cache[ys.callback_idx++] = callback;
ys.worker.postMessage([request]);
}
2015-06-28 10:47:58 -05:00
return ys;
}
2015-02-16 05:41:48 -06:00
}