Skip to content
This repository
tag: v735
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 200 lines (168 sloc) 6.783 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)

(* refactoring in progress *)

(* alias *)
module JsIdent = Qmljs_Serializer.JsIdent

(* shorthand *)
module Q = QmlAst
module QCT = QmlAstCons.TypedExpr
module TC = QmlTypesUtils.Basic

let field_expr = "expr"
let field_value = "value"

(* MERGE WITH THE ONE IN opa_InsertRemote *)
module TyIdent = struct

  let get name ~side = fun annotmap gamma->
    try
      let ident = OpaMapToIdent.val_ ~side name in
      let (ty:Q.ty) = QmlTypes.Scheme.instantiate (QmlTypes.Env.Ident.find ident gamma) in
      QCT.ident annotmap ident ty
    with Not_found -> assert false (*QmlError.i_error (Some QmlCheck.Ident.mandatory_undefined_id) (QmlError.context ~annotmap posexpr) "Missing ident"*)

end

let bad_case e = Format.printf "\n%a\n" QmlPrint.pp#expr e;;

(**
OPA utility functions names
*)
let _FunActionServer_serialize_call = TyIdent.get Opacapi.FunActionServer.serialize_call
let _FunActionServer_serialize_ty_arg = TyIdent.get Opacapi.FunActionServer.serialize_ty_argument

(**
@funaction[Deserialize](arg1_) => arg1_
*)
let process_arg = function
  | Q.Directive (_, `fun_action (Some Q.Deserialize), [id], []) -> id
  | _ -> assert false

(**
[@funaction[Deserialize](arg1_);...]_caml => [arg1_, ...]_qml
*)
let process_args gamma annotmap (arg_el: Q.expr list) =
  QCT.list (annotmap,gamma) (match arg_el with
  | Q.Directive (_, `fun_action _,_,_) :: _ -> List.map process_arg arg_el
  | [_] -> []
  | _ -> assert false
  )

(**
ty_arg1 => FunActionServer_serialize_ty_arg(ty_arg1)
*)
let process_ty_arg ~stdlib_gamma gamma annotmap ty_e =
  let annotmap,serialize_ty_arg = _FunActionServer_serialize_ty_arg ~side:`server annotmap stdlib_gamma in
  QCT.apply gamma annotmap serialize_ty_arg [ty_e]

(**
[ty_arg1]_caml => [FunActionServer_serialize_ty_arg(ty_arg1),...]_qml
*)
let process_ty_args ~stdlib_gamma gamma annotmap (ty_el: Q.expr list) =
  let annotmap,l = Base.List.fold_left_map (process_ty_arg ~stdlib_gamma gamma) annotmap ty_el in
  QCT.list (annotmap,gamma) l

(**

id => @js_ident("~id")

where ~id is the stringified name of id
*)
let process_funaction annotmap e =
  match e with
  | Q.Ident (_, id) ->
      let jsident = JsIdent.resolve id in
      QCT.ident annotmap jsident QCT.ty_string

(*
FIXME: use QmlError for an internal error.
*)
  | _ -> bad_case e ; assert false

(** get the final expr of nested letin *)
let rec get_final_expr e =
  match e with
  | Q.LetIn (_, _, e ) -> get_final_expr e
  | _ -> e

(** change the final expr of nested letin given the old final expr and the new final expr
fail if the old final expr cannot be found
assume the type has not changed (if not regenerate annotmap with final expr type)
TODO the assumption is erroneous in our case
*)
let rec change_final_expr e ~olde ~newe =
  match e with
  | Q.LetIn (label, l, e) -> Q.LetIn (label, l, change_final_expr e ~olde ~newe)
  | _ ->
      assert (olde == e);
      newe

(**
{ expr = @funaction(
arg1_ = ...
...
(@funaction[Client_id](f(tyarg1..)))(
@funaction[Deserialize](arg1_)
@funaction[Deserialize](arg2_)
)
)}

==>

{ string =
arg1_ = ...
...
FunActionServer_serialize_call(
@js_ident(f),
[ FunActionServer_serialize_ty_arg(tyarg1) , ...],
[ arg1_, arg2_, ...]
)
)}

*)
(**
create the string record
*)
let generate_string_record stdlib_gamma gamma annotmap
    ~initial_record ~initial_letin ~oldcall
    ~funaction ~ty_args ~args =
  let annotmap,serialize_call = _FunActionServer_serialize_call ~side:`server annotmap stdlib_gamma in
  let annotmap, string_call = QCT.apply gamma annotmap
    serialize_call[funaction ; ty_args ; args] in
  let letin_string = change_final_expr initial_letin
    ~olde:oldcall ~newe:string_call in
  annotmap, Q.Record (Q.Label.expr initial_record, [field_value,letin_string])


(**
deconstruct the initial record and create the new one
*)
let process_fun_action_record stdlib_gamma gamma annotmap e =
  match e with
  | Q.Record (_, [f_expr,dir]) when f_expr = field_expr ->
      begin match dir with
      | Q.Directive (_, `fun_action None, [initial_letin], _) ->
          let full_funaction_call = get_final_expr initial_letin in
          (* getting the processed funaction and eventuals ty_args, args *)
          (* 1) getting instantiated funaction and standard args *)
          let funaction_ty,(annotmap,args) =
            match full_funaction_call with
            | Q.Apply (_, funaction, args) ->
                funaction,process_args gamma annotmap args
            | _ -> bad_case full_funaction_call ; assert false
          in
          (* 2) getting non instantiated funaction and type args *)
          let funactionid,(annotmap,ty_args) =
            match funaction_ty with
            | Q.Directive (_, `fun_action (Some Q.Client_id), [funactionid], _) ->
                begin match funactionid with
                (* with explicit instantiation type parameter *)
                | Q.Apply (_, funactionid, ty_args) -> funactionid, process_ty_args ~stdlib_gamma gamma annotmap ty_args
                (* with explicit instantiation type parameter *)
                | Q.Ident _ -> funactionid, process_ty_args ~stdlib_gamma gamma annotmap []
                | _ -> bad_case funaction_ty ; assert false
                end
            | _ -> bad_case funaction_ty ; assert false
          in
          let annotmap, funaction = process_funaction annotmap funactionid in
          (* putting everything together *)
          generate_string_record stdlib_gamma gamma annotmap
            ~initial_record:e ~initial_letin ~oldcall:full_funaction_call
            ~funaction ~ty_args ~args
      | _ -> annotmap, e
      end
  | _ ->
      annotmap, e


let process_server_code stdlib_gamma gamma annotmap code =
  let annotmap, code = QmlAstWalk.CodeExpr.fold_map (QmlAstWalk.Expr.foldmap (process_fun_action_record stdlib_gamma gamma)) annotmap code in
  (gamma, annotmap), code
Something went wrong with that request. Please try again.