Skip to content
This repository
tag: v1314
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 405 lines (364 sloc) 11.268 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
(*
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 *)

(* depends *)
module Format = Base.Format
module List = Base.List

(* refactoring in progress *)

(* alias *)
module JsSerializer = Qmljs_Serializer.JsSerializer
module QmlSerializer = Qmljs_Serializer.QmlSerializer

(* shorthands *)
module BPI = BslPluginInterface
module O = OpaEnv

(* -- *)

(*
Compositionality.
Used to know what js files have already been registred, to avoid duplication
of js insertion.
*)
module S =
struct
  type t = {
    (**
Indexed by the name of the file.
*)
    extralibs : (string, unit) Hashtbl.t ;

    (**
Indexed by a plugin_id (module_name)
*)
    plugins : (string, unit) Hashtbl.t ;
  }

  let pass = "JavascriptCompilation"
  let pp f _ = Format.pp_print_string f "<dummy>"
end

module R = ObjectFiles.Make(S)

(*
Traduction of options.
*)
let pass_OpaOptionsToJsOptions _backend options =
  let argv_options = Qml2jsOptions.Argv.default () in
  { argv_options with Qml2jsOptions.
    command_line = false;
    cps = options.O.cps_client;
    cps_toplevel_concurrency = options.O.cps_toplevel_concurrency ;
    qml_closure = options.O.closure;
    extra_lib = options.O.extrajs;
    alpha_renaming = options.O.js_local_renaming;
    check_bsl_types = options.O.js_check_bsl_types;
    cleanup = options.O.js_cleanup;
    inlining = options.O.js_local_inlining;
    global_inlining = options.O.js_global_inlining;
    no_assert = options.O.no_assert;
  }


(*
A external root elemt (plugin and extra libs)
*)
let make_root key content =
  let content = [ JsSerializer.Verbatim content ] in
  let code_elt = { JsSerializer.
    ident = JsSerializer.KI_key key ;
    root = true ;
    definition = `Nothing ;
    content = content ;
  } in
  code_elt

(*
Serialization of the client code
*)
let client_serialization
    ~client_roots
    rev_code ( env_js_input : Qml2jsOptions.env_js_input ) =
  (*
bsl projection: They are no longer roots since the generation
of bypass projection uses Ident.
*)
  let rev_code =
    List.fold_left (
      fun rev_code (_name, elts) ->
        match elts with
        | `ast elts ->
            List.fold_left (
              fun rev_code (unicity_index, js_elt) ->
                let js_elt = JsUtils.globalize_native_ident js_elt in
                let js_elt =
                  JsSerializer.serialize
                    ~client_roots
                    ~key:unicity_index
                    js_elt in
                js_elt :: rev_code
            ) rev_code elts
        | `string s ->
            make_root (Digest.string s) s :: rev_code
    ) rev_code env_js_input.Qml2jsOptions.js_init_contents
  in

  (*
serialization of the compiled client code.
*)
  let rev_code =
    List.fold_left (
      fun rev_code ( code_elt : JsAst.code_elt ) ->
        (*
No need to globalize native ident, done in the compiler directly.
*)
        let code_elt =
          JsSerializer.serialize
            ~client_roots
            code_elt in
        code_elt :: rev_code
    ) rev_code env_js_input.Qml2jsOptions.js_code
  in
  rev_code

(*
A special function for parsing and serializing external js files,
such than bsl files, and/or extralibs
*)
let parse_js_content ~optimized_conf ~key_prefix ~filename ~content =
  let parsed_code =
    try JsParse.String.code ~throw_exn:true content
    with JsParse.Exception e ->
      OManager.error (
          "External Javascript serialization@\n"^^
          "Cannot serialize external js-code @{<bright>%s@}@\n"^^
          "File %S: %a@\n"
        )
          key_prefix
          filename
          JsParse.pp e in

  let parsed_code = if optimized_conf.BslJsConf.localrenaming then Imp_Renaming.rename parsed_code else parsed_code in
  (* cleanup does not always reaches a fixpoint on the first try, it is worth applying it twice *)
  let parsed_code = if optimized_conf.BslJsConf.cleanup then Imp_CleanUp.clean ~use_shortcut_assignment:true parsed_code else parsed_code in
  let parsed_code = if optimized_conf.BslJsConf.cleanup then Imp_CleanUp.clean ~use_shortcut_assignment:true parsed_code else parsed_code in
  let parsed_code = List.map JsUtils.globalize_native_ident parsed_code in
  parsed_code

let serialize_js_content
    ~client_roots
    ~key_prefix ~parsed_code
    rev_code
    =
  (*
We use a counter for distinguing statements from external files.
We assume that if we parse 2 time the same plugin, or external files,
the order returned by the parser is the same.
*)
  let count = ref 0 in
  let fold rev_code js_elt =
    let key =
      incr(count) ;
      key_prefix ^ "_item_" ^ (string_of_int !count)
    in
    let js_elt =
      JsSerializer.serialize
        ~client_roots
        ~key
        js_elt in
    js_elt :: rev_code
  in
  List.fold_left fold rev_code parsed_code

(*
Process all the code.
Handle the serialization of extralibs, plugins, code, and reinjection of the client code
in the server code.
*)
let full_serialize
    ~options
    ~closure_map
    ~renaming_server
    ~renaming_client
    ~client_roots
    ~typing:_
    ~bsl_pp
    ~bsl_client
    ~client
    =

  let back_end = options.OpaEnv.js_back_end in
  let jsoptions = pass_OpaOptionsToJsOptions back_end options in

  (* compositionality -- load *)
  let all_extralibs = Hashtbl.create 16 in
  let all_plugins = Hashtbl.create 16 in
  let this_extralibs = Hashtbl.create 4 in
  let this_plugins = Hashtbl.create 4 in

  let () =
    let iter t =
      Hashtbl.iter (Hashtbl.add all_extralibs) t.S.extralibs ;
      Hashtbl.iter (Hashtbl.add all_plugins) t.S.plugins ;
    in
    R.iter ~deep:true iter
  in
  let register_extralib lib_id =
    Hashtbl.add all_extralibs lib_id () ;
    Hashtbl.add this_extralibs lib_id () ;
    ()
  in
  let register_plugin plugin_id =
    Hashtbl.add all_plugins plugin_id () ;
    Hashtbl.add this_plugins plugin_id () ;
    ()
  in
  (* --- *)

  let rev_ast : ([ `unparsed of JsSerializer.jsast_code_elt | `parsed of JsAst.code] * string) list = [] in

  (* 1) extra libs *)
  (*
Each extra lib is traduced as a [JsSerializer.code_elt]
*)
  let rev_ast =
    List.fold_left
      (fun rev_ast (extra_lib, conf) ->
         (*
Avoid to register several time the same extra lib with different packages:
1. detected at compile time if we have already compiled the same elt
2. detected at runtime for independant packages (key)
*)
         if Hashtbl.mem all_extralibs extra_lib
         then rev_ast
         else (
           register_extralib extra_lib ;
           let filename, content, hash = ObjectFiles.find_js_file_content_digest extra_lib in
           let key_prefix = File.concat "extralib" hash in
           match conf with
           | BslJsConf.Verbatim ->
               let code_elt = make_root key_prefix content in
               (`unparsed code_elt, key_prefix) :: rev_ast
           | BslJsConf.Optimized optimized_conf ->
               (`parsed (parse_js_content ~optimized_conf ~key_prefix ~filename ~content), key_prefix) :: rev_ast
         )
      ) rev_ast jsoptions.Qml2jsOptions.extra_lib in


  (* 2) plugins *)
  (*
Each plugin is also traduced as a list of [JsSerializer.code_elt]
*)
  let rev_ast =
    List.fold_left
      (fun rev_ast plugin ->
         let plugin_id = plugin.BPI.self_module_name in
         if Hashtbl.mem all_plugins plugin_id
         then rev_ast
         else (
           register_plugin plugin_id ;
           let fold rev_ast (filename, content, conf) =
             let key_prefix = plugin_id ^ filename in
             let content = bsl_pp content in
             match conf with
             | BslJsConf.Verbatim ->
                 let code_elt = make_root key_prefix content in
                 (`unparsed code_elt, key_prefix) :: rev_ast
             | BslJsConf.Optimized optimized_conf ->
                 (`parsed (parse_js_content ~optimized_conf ~key_prefix ~filename ~content), key_prefix) :: rev_ast in
           List.fold_left fold rev_ast plugin.BPI.js_code
         )
      ) rev_ast bsl_client.BslLib.plugins in

  let bsl_and_plugin_ast =
    List.flatten (
      List.filter_map
        (function
         | `parsed parsed -> Some parsed
         | `unparsed _ -> None)
        (List.rev_map fst rev_ast)
    ) in

  (* compilation of js code *)
  let env_js_input_val_ name =
    try
      let name = Hashtbl.find Opacapi.table name in
      OpaMapToIdent.val_ ~side:`client name
    with Not_found ->
      OManager.error "Function %S not registered in Opacapi@\n" name in
  let env_js_input =
    Qml2js.Sugar.for_opa
      ~bsl:bsl_and_plugin_ast
      ~val_:env_js_input_val_
      ~closure_map
      ~renaming_server
      ~renaming_client
      back_end
      jsoptions
      bsl_client
      client.QmlBlender.env
      client.QmlBlender.code
  in

  let rev_code : JsSerializer.jsast_code = [] in
  let rev_code = List.fold_left
    (fun rev_code (ast,key_prefix) ->
       match ast with
       | `parsed parsed_code ->
           serialize_js_content
             ~client_roots
             ~key_prefix
             ~parsed_code
             rev_code
       | `unparsed code_elt ->
           code_elt :: rev_code
    ) rev_code (List.rev rev_ast) in

  (* 3) client code *)
  let rev_code =
    client_serialization
      ~client_roots
      rev_code env_js_input
  in

  (* compositionality -- save *)
  let () =
    let t = { S.
       extralibs = this_extralibs ;
       plugins = this_plugins ;
    } in
    R.save t
  in
  (* -- *)

  rev_code


(*
Serialize the js, and reinject it in the server code.
*)
let reinjection ~options ~server ~rev_code =
  let server_code = server.QmlBlender.code in
  let server_code =
    QmlSerializer.insert_code
      ~kind:options.OpaEnv.js_serialize
      (List.rev rev_code)
      server_code in
  { server with QmlBlender.
      code = server_code ;
  }

(*
Main function, exported to be used by the pass.
*)
let process
    ~options
    ~closure_map
    ~renaming_server
    ~renaming_client
    ~client_roots
    ~typing
    ~bsl_pp
    ~bsl_client
    ~server
    ~client
    =

  if client.QmlBlender.code = [] then (
    R.save {S.extralibs = Hashtbl.create 0; S.plugins = Hashtbl.create 0};
    Qml2js.Sugar.dummy_for_opa options.OpaEnv.js_back_end;
    server
  ) else (

    let rev_code = full_serialize
      ~options
      ~closure_map
      ~renaming_server
      ~renaming_client
      ~client_roots
      ~typing
      ~bsl_pp
      ~bsl_client
      ~client
    in
    let server = reinjection ~options ~server ~rev_code in
    server
  )
Something went wrong with that request. Please try again.