123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2021 Trili Tech, <contact@trili.tech> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openAlpha_context(* The outer option is for gas exhaustion. The inner [result] is for all other
errors. *)type('a,'trace)t=Local_gas_counter.local_gas_counter->(('a,'trace)result*Local_gas_counter.local_gas_counter)optiontype('a,'trace)gas_monad=('a,'trace)tletof_resultxgas=Some(x,gas)[@@ocaml.inlinealways]letreturnx=of_result(okx)[@@ocaml.inlinealways]letreturn_unit=return()(* Inlined [Option.bind] for performance. *)let(>>??)mf=matchmwithNone->None|Somex->fx[@@ocaml.inlinealways]letbindmfgas=mgas>>??fun(res,gas)->matchreswithOky->fygas|Error_aserr->of_resulterrgas[@@ocaml.inlinealways]letmapfmgas=mgas>>??fun(x,gas)->of_result(x>|?f)gas[@@ocaml.inlinealways]letbind_resultmf=bind(of_resultm)f[@@ocaml.inlinealways]letbind_recovermfgas=mgas>>??fun(x,gas)->fxgas[@@ocaml.inlinealways]letconsume_gascostgas=matchLocal_gas_counter.consume_optgascostwith|None->None|Somegas->Some(ok(),gas)letrunctxtm=letopenLocal_gas_counterinmatchGas.levelctxtwith|Gas.Unaccounted->(matchm(Local_gas_counter(Saturation_repr.saturated:>int))with|Some(res,_new_gas_counter)->ok(res,ctxt)|None->errorGas.Operation_quota_exceeded)|Limited{remaining=_}->(letgas_counter,outdated_ctxt=local_gas_counter_and_outdated_contextctxtinmatchmgas_counterwith|Some(res,new_gas_counter)->letctxt=update_contextnew_gas_counteroutdated_ctxtinok(res,ctxt)|None->errorGas.Operation_quota_exceeded)letrecord_trace_eval:typeerror_traceerror_context.error_details:(error_context,error_trace)Script_tc_errors.error_details->(error_context->error)->('a,error_trace)t->('a,error_trace)t=fun~error_details->matcherror_detailswith|Fast->fun_fm->m|Informativeerr_ctxt->funfmgas->mgas>>??fun(x,gas)->of_result(record_trace_eval(fun()->ferr_ctxt)x)gasletfaile=of_result(Errore)[@@ocaml.inlinealways]moduleSyntax=structletreturn=returnletreturn_unit=return_unitletreturn_none=returnNoneletreturn_somex=return(Somex)letreturn_nil=return[]letreturn_true=returntrueletreturn_false=returnfalseletfail=faillet(let*)=bindlet(let+)mf=mapfmlet(let*?)=bind_resultend