Skip to content
This repository
tag: v1690
Fetching contributors…

Cannot retrieve contributors at this time

file 465 lines (388 sloc) 14.001 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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
(*
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/>.
*)
(**
Interface of generated loaders and plugins

@author Mathieu Barbin
@author Mehdi Bouaziz
*)

module Format = BaseFormat

(** {6 Type alias (just for lisibility) } *)
(** *)
type filename = string
type contents = string
type ocaml_module_name = string (* OcamlUtils.module_name *)

(** the implementation name, expressed as concrete syntax of the target language *)
type implementation = string

(** the string version of the bsl key *)
type skey = string

(** the extension, like ["ml", "js", "c"] *)
type language = BslLanguage.t

(** a uniq id for identifying the plugin *)
type uniq_id = string

(**
The name of the opp directory, without the 'opp' extension
*)
type plugin_basename = string

(** {6 Plugin interface} *)

(**
Dealing with external primitives for opa.
*)

(**
Marshalable environment for separation of plugin building
These types are magically produced by [BslOcaml] and [BslJs]
*)

type javascript_env
type ocaml_env

(**
The type of the function that the plugin use to register external primitives.
It works with a side effect on some table in BslLib.

Label are volontary short for minimizing the generated code of plugins.

+ [ks] the hieararchy path until this primitive. The final bslkey will be
built by lowercase all the path, and separated items with an underscore.
+ [ty] the type of the primitive
+ [ips] all informations about the implementations in all target languages
+ [language] the extension of the language
+ [filename] the complete filname with dirname where is the file
+ [parsed_t] the row tags.
+ [implementation] the complete identifier for the implementation.
["OpabslgenMLRuntime.Foo.Bar.function"]
+ [obj] optional, a pointer to the function (in this case, the code is linked
with the runtime, this is no more just a plugin, but a loader for the interpreter)
*)
type register_primitive =
  ks:skey list ->
  ty:BslTypes.t ->
  ips:(language * filename * BslTags.parsed_t * implementation) list ->
  ?obj:Obj.t ->
  unit -> unit


type register_primitive_arguments = {
  rp_ks : skey list ;
  rp_ty : BslTypes.t ;
  rp_ips : ( language * filename * BslTags.parsed_t * implementation ) list ;
  rp_obj : Obj.t option ;
}


let apply_register_primitive ( register_primitive : register_primitive ) rp =
  let ks = rp.rp_ks in
  let ty = rp.rp_ty in
  let ips = rp.rp_ips in
  let obj = rp.rp_obj in
  register_primitive ~ks ~ty ~ips ?obj ()


let (~>) buf = FBuffer.printf buf

let pp_escaped fmt s = Format.fprintf fmt "%S" s
let pp_fc fmt (f, c) = Format.fprintf fmt "(%S,@\n %S)@\n" f c
let pp_ml_list p = Base.Format.pp_list " ; " p

let pp_js_conf fmt conf =
  if BslJsConf.is_default conf
  then
    Format.pp_print_string fmt "BslJsConf.default"
  else
    Format.fprintf fmt "(Marshal.from_string %S 0)"
      (Marshal.to_string (conf : BslJsConf.conf) [])

let pp_fc_conf fmt (f, c, conf) =
  Format.fprintf fmt "(%S,@\n %S,@\n %a)@\n"
    f
    c
    pp_js_conf conf

let pp_conf fmt conf =
  if BslConf.is_default conf
  then
    Format.pp_print_string fmt "BslConf.default_conf"
  else
    Format.fprintf fmt "(Marshal.from_string %S 0)"
      (Marshal.to_string (conf : BslConf.conf) [])

(**
Meta Generation of code for [register_primitive].
*)
let meta_register_primitive buf ~ks ~ty ~ips ?obj () =
  let register = "register" in
  let b = buf in
  let b = ~>b "%s " register in
  let b = ~>b "~ks:[ %a ] " (pp_ml_list pp_escaped) ks in
  let b = ~>b "~ty:(%a) " BslTypes.pp_meta ty in
  let b =
    let pp_impl fmt impl =
      let lang, filename, parsed_t, implementation = impl in
      Format.fprintf fmt "(%a, %S, %a, %S)"
        BslLanguage.pp_meta lang filename BslTags.pp_meta parsed_t implementation
    in
    ~>b "~ips:[ %a ] " (pp_ml_list pp_impl) ips
  in
  let b =
    let pp_obj fmt obj = Format.fprintf fmt "~obj:(Obj.repr %s) " obj in
    ~>b "%a" (Option.pp pp_obj) obj
  in
  let b =
    ~>b "();@\n"
  in
  b


(**
The type of the function that the plugin use to register external types.
It works with a side effect on some table in BslLib.

+ [ks] the hieararchy path until this type defition
+ [ty] An External type, defined in the target language.
*)
type register_type =
  ks:skey list ->
  ty:BslTypes.t ->
  unit


type register_type_arguments = {
  rt_ks : skey list ;
  rt_ty : BslTypes.t ;
}


let apply_register_type ( register_type : register_type ) rt =
  let ks = rt.rt_ks in
  let ty = rt.rt_ty in
  register_type ~ks ~ty


(**
Meta Generation of code for [register_type].
*)
let meta_register_type buf ~ks ~ty =
  let register = "register_type" in
  let b = buf in
  let b = ~>b "%s " register in
  let b = ~>b "~ks:[ %a ] " (pp_ml_list pp_escaped) ks in
  let b = ~>b "~ty:(%a) " BslTypes.pp_meta ty in
  let b = ~>b ";@\n" in
  b

(**
When the plugin ask for regestering things, it could get such a record,
so that it can perform its registering.
*)
type dynloader_interface = {
  register_primitive : register_primitive ;
  register_type : register_type ;
}

(**
Whenever a plugin want to register some function, we should actually check
if it was not already registred. For that, there is an abtraction, handled
via a function named [multi_loading_safe_get_dynloader_interface], which given
an uniq id and the runtime name, will let the plugin have acces or not
to register funciton.
<!> This is the only chance for the plugin to register anything in
the bsl table. The next time it will ask, it will get a [None]
*)
type multi_loading_safe_get_dynloader_interface =
  uniq_id:string ->
  plugin_name:plugin_basename ->
  dynloader_interface option

(**
So, a dynloader in a plugin, is the function which is in charge
to get a dynloader_interface, and using it.
*)
type dynloader = multi_loading_safe_get_dynloader_interface -> unit

(**
Showing what is actually going on in the plugin ["examplePlugin.ml"] generated e.g. with {[bslregister -o example]}

{[
module Self : BslPluginInterface.PLUGIN =
struct
(* This is an example of minimal plugin *)
let self_module_name = "ExamplePlugin"
let uniq_id = "ExamplePlugin_17431-2010-08-08-(368c3b4-75fe11b-21cbb74)"
let ml_runtime_module_name = "ExampleMLRuntime"
let depends = [ "opabslgen" ]
let opa_code = [...]
let js_code = [...]
let dynloader ( get_register : BslPluginInterface.multi_loading_safe_get_dynloader_interface) : unit =
match get_register ~uniq_id ~plugin_name:basename with
| None ->
(* I should have been already loaded *)
()
| Some { register = register ; register_type = register_type } ->
begin
... (* a lot of code to use register and register_type for registering primitives *)
end

let self = {
self_module_name ;
uniq_id ;
ml_runtime_module_name ;
depends ;
opa_code ;
js_code ;
dynloader ;
ocaml_env ;
javascript_env ;
}

let self_store () = BslPluginTable.store self

end

let _ = Self.self_store ()

]}

*)
(** *)

let meta_plugin__01 buf
    ~basename
    ~self_module_name
    ~uniq_id
    ~conf
    ~ml_runtime
    ~depends
    ~js_code
    ~opa_code
    ~ocaml_env
    ~javascript_env
    =
  let static_part =
"(* Auto generated plugin / loader, DO NOT EDIT BY HAND *)
(* Part of this code is static and comes from BslPluginInterface.ml *)
open BslPluginInterface
module Self : BslPluginInterface.PLUGIN =
struct
"
  in
  (* I someone find something |> more style, bravo :) - hard to reverse ()format arguemnts... *)

  let b = buf in
  let b = FBuffer.add b static_part in

  let b = ~> b "let basename = %S\n" basename in
  let b = ~> b "let self_module_name = %S\n" self_module_name in
  let b = ~> b "let uniq_id = %S\n" uniq_id in
  let b = ~> b "let conf = %a\n" pp_conf conf in
  let b = ~> b "let ml_runtime = %S\n" ml_runtime in
  let b = ~> b "let depends = [ %a ]\n" (pp_ml_list pp_escaped) depends in
  let b = ~> b "let js_code = ( [ %a ] : (string * string * BslJsConf.conf) list )\n"
    (pp_ml_list pp_fc_conf)
    js_code
  in
  let b = ~> b "let opa_code = [ %a ]\n" (pp_ml_list pp_fc) opa_code in
  let b = ~> b "let ocaml_env = (Marshal.from_string %S 0)\n" (Marshal.to_string (ocaml_env : ocaml_env) []) in
  let b = ~> b "let javascript_env = (Marshal.from_string %S 0)\n" (Marshal.to_string (javascript_env : javascript_env) []) in
  b

let meta_plugin__02 = "
(* the generated code seems to enjoy following some weird guidelines *)
module Q = QmlAst
module B = BslTypes
let (~$) = B.(~$)
module BPI = BslPluginInterface
module L = BslLanguage
let dynloader ( get_register : BPI.multi_loading_safe_get_dynloader_interface ) : unit =
match get_register ~uniq_id ~plugin_name:basename with
| None ->
(* I should have been already loaded *)
()
| Some { BPI.register_primitive = register ; BPI.register_type = register_type } ->
begin
let mp = B.meta_pos in
(* from there, the code is generated, cf in BslRegisterLib and in BslPluginInterface *)
"
let meta_plugin__03 = "
()
end
(* Back there, the code in staticly known, cf BslPluginInterface.meta_plugin__03 *)
let self = {
basename ;
self_module_name ;
uniq_id ;
conf ;
ml_runtime ;
depends ;
opa_code ;
js_code ;
dynloader ;
ocaml_env ;
javascript_env ;
}

let self_store () = BslPluginTable.store self

end

let _ = Self.self_store ()
"

(**
The type record use for manipulating the module as a first level value
Each field correspond to a value of the interface [PLUGIN].
The documentation is in the documentation of this interface.

This is an unmodularization of the plugin, used to access dynamicly
the value of a plugin Module.
*)
type plugin = {
  basename : plugin_basename ;
  self_module_name : ocaml_module_name ;
  uniq_id : uniq_id ;
  conf : BslConf.conf ;
  ml_runtime : ocaml_module_name ;
  depends : plugin_basename list ;
  opa_code : (filename * contents) list ;
  js_code : (filename * contents * BslJsConf.conf) list ;
  dynloader : dynloader ;
  ocaml_env : ocaml_env ;
  javascript_env : javascript_env ;
}

let pp fmt plugin =
  Format.fprintf fmt "@[<2>%s.opp: {@\n" plugin.basename ;
  Format.fprintf fmt "plugin: %S@\n" plugin.self_module_name ;
  Format.fprintf fmt "id: %S@\n" plugin.uniq_id ;
  Format.fprintf fmt "conf: %a@\n" BslConf.pp plugin.conf ;
  Format.fprintf fmt "mlruntime: %S@\n" plugin.ml_runtime ;
  Format.fprintf fmt "depends: %a@\n" (Format.pp_list " ; " Format.pp_print_string) plugin.depends ;
  Format.fprintf fmt "@]}" ;

module type PLUGIN =
sig

  (** {6 Identification} *)

  (**
The name asked for the plugin, without the extension opp.
*)
  val basename : plugin_basename

  (**
The name of the Ocaml module corresponding to this plugin.
This name is already capitalized.
e.g. [OpabslgenPlugin]
*)
  val self_module_name : ocaml_module_name

  (**
Used to identify different conflicting version of the same Plugin
This contains the version of the compiler, and the date of generation.
*)
  val uniq_id : string

  (**
Configuration
*)
  val conf : BslConf.conf

  (**
This is the name of the ML runtime corresponding to this plugin.
When we generate code, we need to know where are the bypass.
*)
  val ml_runtime : ocaml_module_name

  (**
The module_name list from parents.
This correspond to the list of all [basename] of plugins
which was loaded when [bslregister] generated this plugin.
(cf option --use-plugin of bslregister)
*)
  val depends : plugin_basename list

  (** {6 Embeded code} *)

  (**
This is an escaped string optained by preprocessing {b opa} files
with [bslregister].
The files are given file by file, indexed by the name of the file.
*)
  val opa_code : (filename * contents) list

  (**
This is an escaped string optained by preprocessing {b javascript} files
with [bslregister].
The files are given file by file, indexed by the name of the file.
*)
  val js_code : (filename * contents * BslJsConf.conf) list

  (** {6 Registering primitives and types} *)

  val dynloader : dynloader

  (**
The plugin defines a record with its contains
*)
  val self : plugin

  (**
Make the plugin store itself in the BslPluginTable.
It works with a side-effect on the plugin table,
after that, we have a structure for folding plugins,
etc...
*)
  val self_store : unit -> unit

(**
A side effect at the end call the function [self_store]
*)

end
Something went wrong with that request. Please try again.