Skip to content

bug fixes and browser stdlib support #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ ocaml/man
jscomp/bench/*.js
*.bak
.vscode
osc
osc
jscomp/pre_load.js
1 change: 1 addition & 0 deletions jscomp/compiler.mllib
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ lam_group
lam_current_unit

j
js_config
js_program_loader
js_output
js_dump
Expand Down
7 changes: 7 additions & 0 deletions jscomp/js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ js_of_ocaml -I +compiler-libs --toplevel +dynlink.js +toplevel.js +weak.js _bui

rm -rf $OCAMLSCRIPT_DOC/js-demo/exports.js && cp _build/exports.js $OCAMLSCRIPT_DOC/js-demo/exports.js

ocamlbuild -use-ocamlfind -no-hygiene -no-links js_generate_require.byte --
rm -rf $OCAMLSCRIPT_DOC/js-demo/pre_load.js
cp ./pre_load.js $OCAMLSCRIPT_DOC/js-demo/
# TODO: build with amd first
cp runtime/*.js $OCAMLSCRIPT_DOC/js-demo/runtime
cp stdlib/*.js $OCAMLSCRIPT_DOC/js-demo/stdlib

104 changes: 104 additions & 0 deletions jscomp/js_config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
(* OCamlScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

(* Author: Hongbo Zhang *)


type env =
| Browser
| NodeJS


let default_env = ref NodeJS

let get_env () = !default_env

let set_env env = default_env := env

let stdlib_set = String_set.of_list [
"arg.js";
"gc.js";
"printexc.js";
"array.js";
"genlex.js";
"printf.js";
"arrayLabels.js";
"hashtbl.js";
"queue.js";
"buffer.js";
"int32.js";
"random.js";
"bytes.js";
"int64.js";
"scanf.js";
"bytesLabels.js";
"lazy.js";
"set.js";
"callback.js";
"lexing.js";
"sort.js";
"camlinternalFormat.js";
"list.js";
"stack.js";
"camlinternalFormatBasics.js";
"listLabels.js";
"stdLabels.js";
"camlinternalLazy.js";
"map.js";
"std_exit.js";
"camlinternalMod.js";
"marshal.js";
"stream.js";
"camlinternalOO.js";
"moreLabels.js";
"string.js";
"char.js";
"nativeint.js";
"stringLabels.js";
"complex.js";
"obj.js";
"sys.js";
"digest.js";
"oo.js";
"weak.js";
"filename.js";
"parsing.js";
"format.js";
"pervasives.js"
]

let runtime_set = String_set.of_list [
"caml_array.js";
"caml_float.js";
"caml_obj_runtime.js";
(* "caml_sys.js"; *)
"caml_bigarray.js";
"caml_format.js";
"caml_oo.js";
(* "caml_unix.js"; *)
"caml_c_ffi.js";
"caml_int64.js";
"caml_primitive.js";
"caml_utils.js";
"caml_exceptions.js";
(* "caml_io.js"; *)
"curry.js";
"caml_file.js";
"caml_lexer.js";
"caml_string.js"
]
33 changes: 33 additions & 0 deletions jscomp/js_config.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(* OCamlScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

(* Author: Hongbo Zhang *)

type env =
| Browser
| NodeJS


val get_env : unit -> env

val set_env : env -> unit

val runtime_set : String_set.t
val stdlib_set : String_set.t


126 changes: 68 additions & 58 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module L = struct
let json = "JSON"
let stringify = "stringify"
let console = "console"
let define = "define"
end
let return_indent = (String.length L.return / Ext_pp.indent_length)

Expand Down Expand Up @@ -284,8 +285,10 @@ let rec pp_function
action return
else
let lexical = Ident_set.elements lexical in

(if return then P.string f "return " else ());
(if return then begin
P.string f L.return ;
P.space f
end else ());
P.string f "(function(";
ignore @@ aux inner_cxt f lexical;
P.string f ")";
Expand Down Expand Up @@ -1350,69 +1353,76 @@ let amd_program f ({modules; block = b ; exports = exp ; side_effect } : J.prog
list ~pp_sep pp_v ppf vs in

P.vgroup f 1 @@ fun _ ->
P.string f "define([";
list ~pp_sep:(fun f _ -> P.string f L.comma)
(fun f (_,s) ->
pp_string f ~utf:true ~quote:(best_string_quote s) s; ) f modules;
P.string f "]";
P.string f L.comma;
P.newline f;
P.string f L.function_;
P.string f "(";
let cxt = aux cxt f modules in
P.string f ")";

P.brace_vgroup f 1 @@ (fun _ ->
let cxt = statement_list true cxt f b in
(* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
P.newline f;
P.string f L.return;
P.space f;
P.brace_vgroup f 1 @@ fun _ ->
let rec aux cxt f (idents : Ident.t list) =
match idents with
| [] -> cxt
| [id] ->
P.string f (Ext_ident.convert id.name);
P.space f ;
P.string f L.colon;
P.space f ;
ident cxt f id
| id :: rest
->
P.string f (Ext_ident.convert id.name);
P.space f ;
P.string f L.colon;
P.space f;
let cxt = ident cxt f id in
P.string f L.comma;
P.space f ;
P.newline f ;
aux cxt f rest
P.string f L.define;
P.string f "([";
list ~pp_sep:(fun f _ -> P.string f L.comma)
(fun f (_,s) ->
pp_string f ~utf:true ~quote:(best_string_quote s) s; ) f modules;
P.string f "]";
P.string f L.comma;
P.newline f;
P.string f L.function_;
P.string f "(";
let cxt = aux cxt f modules in
P.string f ")";
P.brace_vgroup f 1 @@ (fun _ ->
let () = P.string f "'use strict';" in
let () = P.newline f in
let cxt = statement_list true cxt f b in
(* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
P.newline f;
P.string f L.return;
P.space f;
P.brace_vgroup f 1 @@ fun _ ->
let rec aux cxt f (idents : Ident.t list) =
match idents with
| [] -> cxt
| [id] ->
P.string f (Ext_ident.convert id.name);
P.space f ;
P.string f L.colon;
P.space f ;
ident cxt f id
| id :: rest
->
P.string f (Ext_ident.convert id.name);
P.space f ;
P.string f L.colon;
P.space f;
let cxt = ident cxt f id in
P.string f L.comma;
P.space f ;
P.newline f ;
aux cxt f rest

in
ignore @@ aux cxt f exp);
P.string f ")";
in
ignore @@ aux cxt f exp);
P.string f ")";
;;

let pp_program (program : J.program) (f : Ext_pp.t) =
let () =
begin
P.string f "// Generated CODE, PLEASE EDIT WITH CARE";
P.newline f;
P.string f {|"use strict";|};
P.string f "\"use strict\";"; (* TODO: use single quote in another commit*)
P.newline f ;
in
(match Sys.getenv "OCAML_AMD_MODULE" with
| exception Not_found ->
ignore (node_program f program)
| _ -> amd_program f program ) ;
P.string f (
match program.side_effect with
| None -> "/* No side effect */"
| Some v -> Printf.sprintf "/* %s Not a pure module */" v );
P.newline f;
P.flush f ()

(match Js_config.get_env () with
| Browser ->
ignore (node_program f program)
| NodeJS ->
begin match Sys.getenv "OCAML_AMD_MODULE" with
| exception Not_found ->
ignore (node_program f program)
| _ -> amd_program f program
end ) ;
P.newline f ;
P.string f (
match program.side_effect with
| None -> "/* No side effect */"
| Some v -> Printf.sprintf "/* %s Not a pure module */" v );
P.newline f;
P.flush f ()
end
let dump_program
(program : J.program)
(oc : out_channel) =
Expand Down
14 changes: 14 additions & 0 deletions jscomp/js_generate_require.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

let std_files =
String_set.elements Js_config.stdlib_set
|> List.map (fun x -> "./stdlib/" ^ x )
let runtime_files =
String_set.elements Js_config.runtime_set
|> List.map (fun x -> "./runtime/" ^ x)

let () =
Ext_pervasives.with_file_as_chan "./pre_load.js" (fun chan ->
output_string chan (Printf.sprintf "require([%s], function(){})"
(String.concat ","
(List.map (Printf.sprintf "%S" ) (std_files @ runtime_files))))
)
Loading