diff --git a/src/lib_scoru_wasm/init_encodings.ml b/src/lib_scoru_wasm/init_encodings.ml index 89abb29e60f159ecef0ae2cd69792920de80be29..1fbfa9a501cad2b1a45835915caba82877ca69a2 100644 --- a/src/lib_scoru_wasm/init_encodings.ml +++ b/src/lib_scoru_wasm/init_encodings.ml @@ -80,6 +80,28 @@ let fold_left_kont_encoding enc_a enc_acc = let lazy_vec_encoding enc = int32_lazy_vector (value [] Data_encoding.int32) enc +let eval_const_kont_encoding ~host_funcs = + tagged_union + tag_encoding + [ + case + "EC_Next" + (Wasm_encoding.config_encoding ~host_funcs) + (function EC_Next c -> Some c | _ -> None) + (fun c -> EC_Next c); + case + "EC_Stop" + Wasm_encoding.value_encoding + (function EC_Stop v -> Some v | _ -> None) + (fun v -> EC_Stop v); + ] + +let create_global_kont_encoding ~host_funcs = + tup2 + ~flatten:true + (value ["global_type"] Interpreter_encodings.Types.global_type_encoding) + (scope ["kont"] (eval_const_kont_encoding ~host_funcs)) + type (_, _) eq = Eq : ('a, 'a) eq let init_section_eq : @@ -227,9 +249,10 @@ let init_kont_encoding ~host_funcs = Func Parser.Code.func_encoding Wasm_encoding.function_encoding - @ aggregate_cases_either + @ aggregate_cases "global" Global + (create_global_kont_encoding ~host_funcs) (value [] Interpreter_encodings.Ast.global_encoding) Wasm_encoding.global_encoding @ aggregate_cases_either diff --git a/src/lib_webassembly/exec/eval.ml b/src/lib_webassembly/exec/eval.ml index 386884996ed3cb04013b1069297d3c5bf54debcc..d55950a564075c2afacfe15ba4ce095d308d21a4 100644 --- a/src/lib_webassembly/exec/eval.ml +++ b/src/lib_webassembly/exec/eval.ml @@ -951,11 +951,22 @@ let create_memory (mem : memory) : memory_inst = let {mtype} = mem.it in Memory.alloc mtype -let create_global module_reg (inst : module_key) (glob : global) : - global_inst Lwt.t = - let {gtype; ginit} = glob.it in - let+ v = eval_const module_reg inst ginit in - Global.alloc gtype v +type create_global_kont = global_type * eval_const_kont + +let create_global_kont inst glob = + (glob.it.gtype, eval_const_kont inst glob.it.ginit) + +let create_global_completed (gtype, kont) = + match eval_const_completed kont with + | Some v -> Some (Global.alloc gtype v) + | None -> None + +let create_global_step module_reg ((gtype, ekont) as kont) = + match create_global_completed kont with + | Some _ -> assert false + | None -> + let+ ekont = eval_const_step module_reg ekont in + (gtype, ekont) let create_export (inst : module_inst) (ex : export) : export_inst Lwt.t = let {name; edesc} = ex.it in @@ -1195,7 +1206,7 @@ let tick_map_step first_kont kont_completed kont_step = function type (_, _, _) init_section = | Func : ((func, func_inst) Either.t, func, func_inst) init_section - | Global : ((global, global_inst) Either.t, global, global_inst) init_section + | Global : (create_global_kont, global, global_inst) init_section | Table : ((table, table_inst) Either.t, table, table_inst) init_section | Memory : ((memory, memory_inst) Either.t, memory, memory_inst) init_section @@ -1310,11 +1321,12 @@ let section_next_init_kont : | Table -> IK_Aggregate (inst0, Memory, tick_map_kont m.it.memories) | Memory -> IK_Exports (inst0, fold_left_kont m.it.exports (NameMap.create ())) -let section_inner_kont : type kont a b. (kont, a, b) init_section -> a -> kont = - fun sec x -> +let section_inner_kont : + type kont a b. module_key -> (kont, a, b) init_section -> a -> kont = + fun self sec x -> match sec with | Func -> Either.Left x - | Global -> Left x + | Global -> create_global_kont self x | Table -> Left x | Memory -> Left x @@ -1323,7 +1335,7 @@ let section_inner_completed : fun sec kont -> match (sec, kont) with | Func, Right y -> Some y - | Global, Right y -> Some y + | Global, kont -> create_global_completed kont | Table, Right y -> Some y | Memory, Right y -> Some y | _ -> None @@ -1346,7 +1358,7 @@ let section_inner_step : in function | Func -> lift_either (create_func module_reg self) - | Global -> lift_either (create_global module_reg self) + | Global -> create_global_step module_reg | Table -> lift_either (fun x -> Lwt.return (create_table x)) | Memory -> lift_either (fun x -> Lwt.return (create_memory x)) @@ -1395,7 +1407,7 @@ let init_step ~module_reg ~self host_funcs (m : module_) (exts : extern list) = | IK_Aggregate (inst0, sec, tick) -> let+ tick = tick_map_step - (section_inner_kont sec) + (section_inner_kont self sec) (section_inner_completed sec) (section_inner_step module_reg self sec) tick diff --git a/src/lib_webassembly/exec/eval.mli b/src/lib_webassembly/exec/eval.mli index 2b12bd28487e2ac38c9b5aff02147756a8f866fd..6eeb23e6b72a8fc1a51c76936a08e30cb5df23a5 100644 --- a/src/lib_webassembly/exec/eval.mli +++ b/src/lib_webassembly/exec/eval.mli @@ -69,13 +69,13 @@ type 'a concat_kont = { type ('a, 'b) fold_left_kont = {origin : 'a Vector.t; acc : 'b; offset : int32} +type eval_const_kont = EC_Next of config | EC_Stop of value + +type create_global_kont = Types.global_type * eval_const_kont + type (_, _, _) init_section = | Func : ((Ast.func, func_inst) Either.t, Ast.func, func_inst) init_section - | Global - : ( (Ast.global, global_inst) Either.t, - Ast.global, - global_inst ) - init_section + | Global : (create_global_kont, Ast.global, global_inst) init_section | Table : ((Ast.table, table_inst) Either.t, Ast.table, table_inst) init_section | Memory