Verified Commit 8861fde4 authored by Camil Staps's avatar Camil Staps 🙂

Add back add{CSS,JS}FromUrl in iTasks.UI.JS.Interface

parent 1a173867
definition module iTasks.UI.JS.Interface
from StdMaybe import :: Maybe
:: *JSWorld
:: JSVal a
......@@ -49,3 +51,18 @@ referenceToJS :: !Int -> JSVal a
* @param A function of the type (a b .. z *JSWorld -> *JSWorld)
*/
jsWrapFun :: !f !*JSWorld -> *(!JSFun f, !*JSWorld)
/**
* Load external CSS stylesheet by its URL.
*
* @param The URL.
*/
addCSSFromUrl :: !String !*JSWorld -> *JSWorld
/**
* Load an external JavaScript file by its URL.
*
* @param The URL.
* @param An optional callback function for when the script has loaded.
*/
addJSFromUrl :: !String !(Maybe (JSFun a)) !*JSWorld -> *JSWorld
implementation module iTasks.UI.JS.Interface
import StdEnv
import StdMaybe
import Text
:: *JSWorld = JSWorld
......@@ -77,6 +78,32 @@ jsWrapFun f world = (cast (share f), world)
referenceToJS :: !Int -> JSVal a
referenceToJS ref = JSRef ref
addCSSFromUrl :: !String !*JSWorld -> *JSWorld
addCSSFromUrl css w = case add_css css of
True -> w
where
add_css :: !String -> Bool
add_css _ = code {
instruction 10
pop_a 1
pushB TRUE
}
addJSFromUrl :: !String !(Maybe (JSFun a)) !*JSWorld -> *JSWorld
addJSFromUrl js mbCallback w = case add_js js callback of
True -> w
where
callback = case mbCallback of
Just cb -> toString cb
Nothing -> ""
add_js :: !String !String -> Bool
add_js _ _ = code {
instruction 11
pop_a 2
pushB TRUE
}
eval_js :: !String -> Bool
eval_js s = code {
instruction 1
......
......@@ -83,6 +83,15 @@ const abc_interpreter={
return abc_interpreter.interpret.apply(null, args);
};
},
get_clean_string: function (hp_ptr) {
var size=abc_interpreter.memory_array[hp_ptr/4+2];
var string_buffer=new Uint8Array(abc_interpreter.memory.buffer, hp_ptr+16);
var string='';
for (var i=0; i<size; i++)
string+=String.fromCharCode(string_buffer[i]);
return string;
},
};
abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
......@@ -157,12 +166,7 @@ abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
case 0: /* evaluation finished */
return 0;
case 1: /* iTasks.UI.JS.Interface: eval_js */
var clean_string=abc_interpreter.memory_array[asp/4];
var size=abc_interpreter.memory_array[clean_string/4+2];
var string_buffer=new Uint8Array(abc_interpreter.memory.buffer, clean_string+16);
var string='';
for (var i=0; i<size; i++)
string+=String.fromCharCode(string_buffer[i]);
var string=abc_interpreter.get_clean_string(abc_interpreter.memory_array[asp/4]);
console.log('eval',string);
Function(string)();
break;
......@@ -170,6 +174,25 @@ abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
abc_interpreter.memory_array[bsp/4]=abc_interpreter.shared_clean_values.length;
abc_interpreter.shared_clean_values.push(abc_interpreter.memory_array[asp/4]);
break;
case 10: /* iTasks.UI.JS.Interface: add CSS */
var url=abc_interpreter.get_clean_string(abc_interpreter.memory_array[asp/4]);
var css=document.createElement('link');
css.rel='stylesheet';
css.type='text/css';
css.async=true;
css.href=url;
document.head.appendChild(css);
break;
case 11: /* iTasks.UI.JS.Interface: add JS */
var url=abc_interpreter.get_clean_string(abc_interpreter.memory_array[asp/4]);
var callback=abc_interpreter.get_clean_string(abc_interpreter.memory_array[asp/4-2]);
var js=document.createElement('script');
js.type='text/javascript';
js.onload=Function(callback+'();');
console.log(url,callback,js);
document.head.appendChild(js);
js.src=url;
break;
default:
throw ('unknown instruction '+arg);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment