Skip to content
This repository
tree: 0c152d98be
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 93 lines (76 sloc) 2.576 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
(*
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/>.
*)
module Top = QmlGenTop.Top

let _start = ref None
let _table = Hashtbl.create 10
let _last_id = ref 1

let load_libs = ref true

type env_id = int

let initial () =
  QmlTopLevel.Properties.fatal_mode := false;
  match !_start with
  | None ->
      let loaders = BslLib.LoaderTable.finalize ~fatal:true () in
      let libs = List.map (
fun loader ->
load_libs, Printf.sprintf "customlib-%s" loader.BslLib.module_name, Some loader.BslLib.dynloader, List.map snd loader.BslLib.splitqmlinit
      ) loaders
      in
      let env = Top.dynload_and_init_env libs in
      _start := Some env;
      env
  | Some env -> env

let get_env id =
  try
    Hashtbl.find _table id
  with
  | Not_found ->
      let env = initial () in
      let res = env, "" in
      Hashtbl.add _table id res;
      res

let set_env (id:env_id) = Hashtbl.add _table id

let webtop = "webtop:input"
let fold_map_topexpr = Top.fold_map_topexpr ~dbgen:true ~dump:true ~loc:webtop

let fold_map env src =
  try
    let src =
      match QmlAstParser.TopLevel.of_string src with
      | QmlAstParser.ParsedExpr expr -> [QmlTopLevel.Top_eval_expr expr]
      | QmlAstParser.ParsedCode code -> List.map (fun elt -> QmlTopLevel.Top_code_elt elt) code
    in
    let env, out = Base.List.fold_left_map fold_map_topexpr env src in
    let out = Top.to_string out in
    env, out
  with
  | QmlAstParser.Exception e ->
      env, QmlAstParser.short_parse_error_message ~extra:webtop e
  | e ->
      env, Top.string_of_exception e

##register init : void -> void
let init () = ignore (initial ())

##register eval : int -> string -> int
let eval id src =
  let env, _ = get_env id in
  let res = fold_map env src in
  let id = !_last_id in
  incr _last_id;
  set_env id res;
  id

##register get_result : int -> string
let get_result id =
  let _, out = get_env id in
  out

##register delete_env : int -> unit
let delete_env id = Hashtbl.remove _table id
Something went wrong with that request. Please try again.