Updates from pmp-p

This commit is contained in:
Themba Dube 2019-07-07 10:31:42 -04:00
parent 571f60f4a0
commit ead74c494f
6 changed files with 153 additions and 48 deletions

2
.gitignore vendored
View File

@ -48,7 +48,9 @@ __pycache__/
######################
GNUmakefile
user.props
ports/javascript/node_modules
# Generated rst files
######################
genrst/

View File

@ -104,4 +104,5 @@ test: $(BUILD)/micropython.js $(TOP)/tests/run-tests
$(eval DIRNAME=ports/$(notdir $(CURDIR)))
cd $(TOP)/tests && MICROPY_MICROPYTHON=../ports/javascript/node_run.sh ./run-tests
include $(TOP)/py/mkrules.mk

View File

@ -75,7 +75,6 @@
line-height: normal;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://littlevgl.com/bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.4/ace.js" type="text/javascript" charset="utf-8"></script>
@ -83,7 +82,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/3.13.2/xterm.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/3.13.2/addons/fit/fit.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.js" type="text/javascript" charset="utf-8"></script>
<script src="query.js"></script>
</head>
<body>
<div class="flex-container">
@ -99,8 +98,14 @@
<p>
<input type="button" disabled="disabled" id="run-button" value="Run the script" onclick="runScript();"/>
<!-- HTML input control is disabled currently -->
<span style="display: none;">
Copy/paste script here:
<input onClick="this.setSelectionRange(0, this.value.length)" id="script-compressed" oninput="input_change();"/>
</span>
</div>
<div id="editor">print("Hello world")</div>
<div class="display-objects">
@ -120,12 +125,17 @@
iframe.contentWindow.document.dispatchEvent(new MouseEvent('mouseup'));
}
}
document.onmouseup = forward_event;
function input_change() {
editor.session.setValue(LZString.decompressFromEncodedURIComponent($("#script-compressed").val()));
}
function editor_change() {
$("#script-compressed").val(LZString.compressToEncodedURIComponent(editor.getValue()));
var script_string = LZString.compressToEncodedURIComponent(editor.getValue());
$("#script-compressed").val(script_string);
var new_href = update_query_string(window.location.href, "script_direct", script_string);
// Create a dummy element to parse the URI with
window.history.replaceState({path:new_href}, document.title,new_href);
}
function runScript() {
var $this = $("#run-button");
@ -143,39 +153,52 @@
clear_iframe(iframe);
iframe.src = get_iframe_url() + "&timestamp=" + new Date().getTime();
iframe.contentWindow.location.href = iframe.src;
console.log("Iframe src: " + iframe.src);
}
function reenableButton() {
$("#run-button").removeProp('disabled');
}
function get_iframe_url() {
/* Assemble the URL */
var newPathname = window.location.href.substr(0, window.location.href.lastIndexOf('/'));
newPathname += "/lvgl.html" + (window.location.href.indexOf('?') != -1 ? '&' : '?') + "env=dev";
console.log(newPathname);
var num_url_chars = (window.location.href.indexOf('?'));
var base_url = window.location.href.substr(0, (num_url_chars == -1) ? undefined : num_url_chars);
var newPathname = base_url.substr(0, base_url.lastIndexOf('/'));
newPathname += "/lvgl.html" + '?' + "env=dev";
return newPathname;
}
function processScriptArg(url){
// read text from URL location
var request = new XMLHttpRequest();
console.log("GET " + url);
request.open('GET', url, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
function processScriptArg(url, lzstring){
var script_passed_handler = function() {
console.log("Script passed: " + script_passed);
if(script_passed)
runScript();
else
reenableButton();
script_passed = false;
};
if(!lzstring) {
// read text from URL location
var request = new XMLHttpRequest();
console.log("GET " + url);
request.open('GET', url, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
console.log(request.reponseText);
if(request.responseText === undefined)
return;
editor.session.getUndoManager().reset();
editor.session.setValue(request.responseText, -1);
console.log("Script passed: " + script_passed);
if(script_passed)
runScript();
else
reenableButton();
script_passed = false;
console.log(request.reponseText);
if(request.responseText === undefined)
return;
editor.session.getUndoManager().reset();
editor.session.setValue(request.responseText, -1);
script_passed_handler();
}
}
} else {
// decompress LZString
editor.session.setValue(LZString.decompressFromEncodedURIComponent(url));
script_passed_handler();
}
}
function getSearchArg(argname) {
/* Run custom script if passed */
@ -199,13 +222,11 @@
editor.session.setMode(new PythonMode());
iframe = document.getElementById("emscripten-iframe");
iframe.src = "about:blank";
iframe.contentWindow.location.href = iframe.src;
clear_iframe(iframe);
var script = getSearchArg("script");
if(script !== undefined && script !== null) {
script_passed = true;
processScriptArg(script);
} else
processScriptArg("https://raw.githubusercontent.com/littlevgl/lv_binding_micropython/dev-6.0/examples/advanced_demo.py");
Terminal.applyAddon(fit);
term = new Terminal();
mp_js_stdout = document.getElementById('mp_js_stdout');
@ -225,6 +246,17 @@
}, false);
editor.getSession().on('change', editor_change);
editor.resize();
var script = getSearchArg("script");
var script_direct = getSearchArg("script_direct");
if(script_direct !== undefined && script_direct !== null) {
script_passed = true;
processScriptArg(script_direct, true);
} else if(script !== undefined && script !== null) {
script_passed = true;
processScriptArg(script);
} else
processScriptArg("https://raw.githubusercontent.com/littlevgl/lv_binding_micropython/dev-6.0/examples/advanced_demo.py");
});
$(window).resize(function() {
editor.resize();

File diff suppressed because one or more lines are too long

View File

@ -76,10 +76,10 @@
#define MICROPY_EMIT_X64 (0) //BROKEN
#define MICROPY_EMIT_THUMB (0) //BROKEN
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_COMP_MODULE_CONST (0)
#define MICROPY_COMP_MODULE_CONST (1)
#define MICROPY_COMP_CONST (1)
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (1)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1)
#define MICROPY_MEM_STATS (0) //BROKEN
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (1)

70
ports/javascript/query.js Normal file
View File

@ -0,0 +1,70 @@
/**
* Add or update a query string parameter. If no URI is given, we use the current
* window.location.href value for the URI.
*
* Based on the DOM URL parser described here:
* http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
*
* @param (string) uri Optional: The URI to add or update a parameter in
* @param (string) key The key to add or update
* @param (string) value The new value to set for key
*
* Tested on Chrome 34, Firefox 29, IE 7 and 11
*/
function update_query_string( uri, key, value ) {
// Use window URL if no query string is provided
if ( ! uri ) { uri = window.location.href; }
// Create a dummy element to parse the URI with
var a = document.createElement( 'a' ),
// match the key, optional square brackets, an equals sign or end of string, the optional value
reg_ex = new RegExp( key + '((?:\\[[^\\]]*\\])?)(=|$)(.*)' ),
// Setup some additional variables
qs,
qs_len,
key_found = false;
// Use the JS API to parse the URI
a.href = uri;
// If the URI doesn't have a query string, add it and return
if ( ! a.search ) {
a.search = '?' + key + '=' + value;
return a.href;
}
// Split the query string by ampersands
qs = a.search.replace( /^\?/, '' ).split( /&(?:amp;)?/ );
qs_len = qs.length;
// Loop through each query string part
while ( qs_len > 0 ) {
qs_len--;
// Remove empty elements to prevent double ampersands
if ( ! qs[qs_len] ) { qs.splice(qs_len, 1); continue; }
// Check if the current part matches our key
if ( reg_ex.test( qs[qs_len] ) ) {
// Replace the current value
qs[qs_len] = qs[qs_len].replace( reg_ex, key + '$1' ) + '=' + value;
key_found = true;
}
}
// If we haven't replaced any occurrences above, add the new parameter and value
if ( ! key_found ) { qs.push( key + '=' + value ); }
// Set the new query string
a.search = '?' + qs.join( '&' );
return a.href;
}