Verified Commit 1194a86b authored by Camil Staps's avatar Camil Staps 🙂

Pass arrays to/from javascript; leaflets sort of start to work

parent e3f954c8
This diff is collapsed.
......@@ -21,6 +21,7 @@ import Text
| JSTypeOf !JSVal
| JSObject !{!JSObjectElement}
| JSArray !{!JSVal}
| JSSel !JSVal !JSVal // x[y]
| JSSelPath !JSVal !String // x.path1.path2...pathn
......@@ -51,6 +52,7 @@ where
JSTypeOf v -> "typeof "+++toString v
JSObject elems -> foldl (+++) "{" [key+++":"+++toString val+++"," \\ {key,val} <-: elems] +++ "}"
JSArray elems -> foldl (+++) "[" [toString v+++"," \\ v <-: elems] +++ "]"
JSSel obj attr -> toString obj+++"["+++toString attr+++"]"
JSSelPath obj path -> toString obj+++"."+++path
......@@ -147,9 +149,18 @@ where
collect_elems (JSTempField k v) = [!{key=k,val=v}!]
collect_elems (JSTempPair a b) = collect_elems a ++| collect_elems b
instance .# String where .# obj path = JSSelPath obj path
instance .# Int where .# arr i = JSSel arr (JSInt i)
instance .# String
where
.# obj path
| contains_dot (size path-1) path
= JSSelPath obj path
= JSSel obj (JSString path)
where
contains_dot -1 _ = False
contains_dot i s = if (s.[i]=='.') True (contains_dot (i-1) s)
(.?) infixl 1 :: !JSVal !*JSWorld -> *(!JSVal, !*JSWorld)
(.?) sel w = (eval_js_with_return_value (toString sel), w)
......@@ -274,19 +285,26 @@ where
}
cast_value_from_js :: !a -> JSVal
cast_value_from_js _ = code {
cast_value_from_js x = case cast_value_from_js` x of
JSArray arr -> JSArray {cast_value_from_js x \\ x <-: arr} // TODO exploit uniqueness
v -> v
cast_value_from_js` :: !a -> JSVal
cast_value_from_js` _ = code {
eq_desc dINT 0 0
jmp_true return_int
eq_desc BOOL 0 0
jmp_true return_bool
eq_desc _STRING_ 0 0
jmp_true return_string
eq_desc REAL 0 0
jmp_true return_real
pushD_a 0
pushI 5290 | 661*8+2; DOMNode (bcprelink.c)
eqI
jmp_true return_ref
eq_desc REAL 0 0
jmp_true return_real
eq_desc _ARRAY_ 0 0
jmp_true return_array
print "cast_value_from_js: return type unknown:\n"
.d 1 0
jsr _print_graph
......@@ -327,6 +345,10 @@ cast_value_from_js _ = code {
fill_r e_iTasks.UI.JS.Interface_kJSString 1 0 1 0 0
pop_a 1
jmp return
:return_array
fill_r e_iTasks.UI.JS.Interface_kJSArray 1 0 1 0 0
pop_a 1
jmp return
:return_ref
pushI 1 | for shiftr%
repl_r_args 0 1
......
......@@ -82,7 +82,7 @@ const abc_interpreter={
return f;
},
copy_js_to_clean: function (values, store_ptrs, hp, hp_free) {
copy_js_to_clean: function (values, store_ptrs, hp, hp_free, wrap_array) {
for (var i=0; i<values.length; i++) {
if (values[i]===null) {
abc_interpreter.memory_array[store_ptrs/4]=hp;
......@@ -144,19 +144,23 @@ const abc_interpreter={
hp_free-=2+((values[i].length+7)>>3);
} else if (Array.isArray(values[i])) {
abc_interpreter.memory_array[store_ptrs/4]=hp;
abc_interpreter.memory_array[hp/4]=2; // fake ARRAY, needed because we use jmp_ap
if (wrap_array) {
abc_interpreter.memory_array[hp/4]=2;
abc_interpreter.memory_array[hp/4+1]=0;
abc_interpreter.memory_array[hp/4+2]=hp+16;
abc_interpreter.memory_array[hp/4+3]=0;
hp+=16;
hp_free-=2;
}
abc_interpreter.memory_array[hp/4]=1*8+2; // _ARRAY_
abc_interpreter.memory_array[hp/4+1]=0;
abc_interpreter.memory_array[hp/4+2]=hp+16;
abc_interpreter.memory_array[hp/4+2]=values[i].length;
abc_interpreter.memory_array[hp/4+3]=0;
abc_interpreter.memory_array[hp/4+4]=1*8+2; // _ARRAY_
abc_interpreter.memory_array[hp/4+4]=0;
abc_interpreter.memory_array[hp/4+5]=0;
abc_interpreter.memory_array[hp/4+6]=values[i].length;
abc_interpreter.memory_array[hp/4+7]=0;
abc_interpreter.memory_array[hp/4+8]=0;
abc_interpreter.memory_array[hp/4+9]=0;
hp+=40;
hp_free-=5;
var copied=abc_interpreter.copy_js_to_clean(values[i], hp, hp+8*values[i].length, hp_free);
hp+=24;
hp_free-=3;
var copied=abc_interpreter.copy_js_to_clean(values[i], hp, hp+8*values[i].length, hp_free, false);
hp=copied.hp;
hp_free=copied.hp_free-values[i].length;
} else if ('shared_clean_value_index' in values[i]) {
......@@ -279,7 +283,7 @@ abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
if (ABC_DEBUG)
console.log('eval',string);
var result=eval('('+string+')'); // the parentheses are needed for {}, for instance
var copied=abc_interpreter.copy_js_to_clean([result], asp, hp, hp_free);
var copied=abc_interpreter.copy_js_to_clean([result], asp, hp, hp_free, false);
abc_interpreter.interpreter.instance.exports.set_hp(copied.hp);
abc_interpreter.interpreter.instance.exports.set_hp_free(copied.hp_free);
break;
......@@ -310,6 +314,7 @@ abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
var callback=abc_interpreter.get_clean_string(abc_interpreter.memory_array[asp/4-2]);
var js=document.createElement('script');
js.type='text/javascript';
js.async=false;
if (callback.length>0)
js.onload=Function(callback+'();');
document.head.appendChild(js);
......@@ -434,7 +439,7 @@ abc_interpreter.loading_promise=fetch('js/app.pbc').then(function(resp){
abc_interpreter.memory_array[asp/4+2]=hp;
abc_interpreter.memory_array[asp/4+4]=abc_interpreter.shared_clean_values[f.shared_clean_value_index];
const copied=abc_interpreter.copy_js_to_clean([args], asp+8, hp, hp_free);
const copied=abc_interpreter.copy_js_to_clean([args], asp+8, hp, hp_free, true);
asp+=16;
hp=copied.hp;
hp_free=copied.hp_free;
......
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