Skip to content

Commit

Permalink
also encode package name in [cmj] files
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongbo Zhang committed May 10, 2016
1 parent c4a680c commit 83a04f3
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 37 deletions.
7 changes: 6 additions & 1 deletion jscomp/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/sh

set -e
. ./env.sh

export OCAMLPARAM='_,bin-annot=1,annot=1'
export OCAMLRUNPARAM=b
export OCAMLBUILD_CFLAGS=-g,-w,-40-30,-warn-error,+a-40-30,-keep-locs,-I,+compiler-libs
export npm_package_name=bs-platform


## Only make sense for dev
make js_map.ml js_fold.ml >> build.compile
Expand Down
8 changes: 0 additions & 8 deletions jscomp/env.sh

This file was deleted.

3 changes: 3 additions & 0 deletions jscomp/ext_pervasives.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ let is_pos_pow n =
let failwithf fmt = Format.ksprintf failwith fmt

let invalid_argf fmt = Format.ksprintf invalid_arg fmt

let bad_argf fmt = Format.ksprintf (fun x -> raise (Arg.Bad x ) ) fmt

2 changes: 2 additions & 0 deletions jscomp/ext_pervasives.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ val is_pos_pow : Int32.t -> int
val failwithf : ('a, unit, string, 'b) format4 -> 'a

val invalid_argf : ('a, unit, string, 'b) format4 -> 'a

val bad_argf : ('a, unit, string, 'b) format4 -> 'a
14 changes: 8 additions & 6 deletions jscomp/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ type cmj_value = {

type effect = string option

type npm_package_path = string * string

type t = {
values : cmj_value String_map.t;
effect : effect;
goog_package : string option;
npm_package_path : string option ;
npm_package_path : npm_package_path option ;
}

let cmj_magic_number = "BUCKLE20160506"
let cmj_magic_number = "BUCKLE20160510"
let cmj_magic_number_length =
String.length cmj_magic_number

Expand All @@ -60,7 +62,7 @@ let pure_dummy =
let no_pure_dummy =
{
values = String_map.empty;
effect = (Some "");
effect = Some "";
goog_package = None;
npm_package_path = None;
}
Expand All @@ -85,9 +87,9 @@ let from_string s : t =
if magic_number = cmj_magic_number then
Marshal.from_string s cmj_magic_number_length
else
failwith
("cmj files have incompatible versions, please rebuilt using the new compiler : "
^ __LOC__)
Ext_pervasives.failwithf
"cmj files have incompatible versions, please rebuilt using the new compiler : %s"
__LOC__

let to_file name (v : t) =
let oc = open_out_bin name in
Expand Down
4 changes: 3 additions & 1 deletion jscomp/js_cmj_format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ type cmj_value = {

type effect = string option

type npm_package_path = string * string

type t = {
values : cmj_value String_map.t;
effect : effect;
goog_package : string option;
npm_package_path : string option;
npm_package_path : npm_package_path option;
}

val pure_dummy : t
Expand Down
22 changes: 18 additions & 4 deletions jscomp/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ let cmd_set_module str =
ext := ".g.js";
end
else
raise (Arg.Bad (Printf.sprintf "invalid module system %s" str))
Ext_pervasives.bad_argf "invalid module system %s" str
else
raise (Arg.Bad (Printf.sprintf "invalid module system %s" str))
Ext_pervasives.bad_argf "invalid module system %s" str


let get_goog_package_name () =
match !default_env with
Expand All @@ -75,9 +76,22 @@ let get_goog_package_name () =
| NodeJS -> None

let npm_package_path = ref None
let set_npm_package_path s = npm_package_path := Some s

let set_npm_package_path s =
match Ext_string.split ~keep_empty:false s ':' with
| [ package_name; path] ->
if String.length package_name = 0 then
(* TODO: check more [package_name] if it is a valid package name *)

Ext_pervasives.bad_argf "invalid npm package path: %s" s
else
npm_package_path := Some (package_name, path)
| _ ->
Ext_pervasives.bad_argf "invalid npm package path: %s" s

let get_npm_package_path () = !npm_package_path


let (//) = Filename.concat
(* for a single pass compilation, [output_dir]
can be cached
Expand All @@ -90,7 +104,7 @@ let get_output_dir filename =
Filename.dirname filename
else
Filename.dirname filename
| Some x ->
| Some (_package_name, x) ->
Lazy.force Ext_filename.package_dir // x


Expand Down
2 changes: 1 addition & 1 deletion jscomp/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ val get_output_file : string -> string
val get_goog_package_name : unit -> string option

val set_npm_package_path : string -> unit
val get_npm_package_path : unit -> string option
val get_npm_package_path : unit -> (string * string) option

val set_env : env -> unit
val cmd_set_module : string -> unit
Expand Down
7 changes: 4 additions & 3 deletions jscomp/js_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,17 @@ module Options = Main_args.Make_bytecomp_options (struct
end)

let add_include_path s =
let (//) = Filename.concat in
let path =
Ext_filename.resolve
(Lazy.force Ext_filename.cwd) s in
(Lazy.force Ext_filename.cwd) s // "lib"// "ocaml" in
Clflags.include_dirs := path :: ! Clflags.include_dirs

let buckle_script_flags =
("-js-npm-output-path", Arg.String Js_config.set_npm_package_path,
" set npm-output-path, for example `lib/js`")
" set npm-output-path: package-name:path, for example `bs-platform:lib/js`")
::
("-npm-package", Arg.String add_include_path,
("-js-npm-package-include", Arg.String add_include_path,
" set package names, for example bs-platform " )
:: ("-js-module", Arg.String Js_config.cmd_set_module,
" set module system: commonjs (default), amdjs, google:package_name")
Expand Down
24 changes: 20 additions & 4 deletions jscomp/js_program_loader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ let string_of_module_id (x : Lam_module_ident.t) : string =
Ext_filename.node_relative_path
(`Dir (Js_config.get_output_dir !Location.input_name)) dep
in
begin match Lam_compile_env.get_npm_package_path x with
| Some x ->
let filename = String.uncapitalize id.name in
rebase (`File (Lazy.force Ext_filename.package_dir // x // filename))
begin match Lam_compile_env.get_npm_package_path x
with
| Some (package_name, x) ->
let filename = String.uncapitalize id.name in
begin match Js_config.get_npm_package_path () with
| None
->
(*TODO: decide which default is better later *)
rebase (`File (
Lazy.force Ext_filename.package_dir // x // filename))

| Some (current_package, path) ->
if current_package <> package_name then
(*TODO: fix platform specific issue *)
package_name // x // filename
else
rebase (`File (
Lazy.force Ext_filename.package_dir // x // filename))

end
| None ->
begin match Config_util.find file with
(* maybe from third party library*)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let rec no_side_effects (lam : Lambda.lambda) : bool =
| "caml_set_oo_id"
| "caml_is_js"
| "caml_int64_float_of_bits"
(* more safe to check if arguments are constant *)
(* more safe to check if arguments are constant *)
(* non-observable side effect *)
| "caml_sys_get_config"
| "caml_sys_get_argv" (* should be fine *)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/lam_compile_env.mli
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ val reset : unit -> unit

val is_pure : Lam_module_ident.t -> bool
val get_goog_package_name : Lam_module_ident.t -> string option
val get_npm_package_path : Lam_module_ident.t -> string option
val get_npm_package_path : Lam_module_ident.t -> (string * string) option

(* The second argument is mostly from [runtime] modules
will change the input [hard_dependencies]
Expand Down
2 changes: 1 addition & 1 deletion jscomp/runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $(addsuffix .cmj, $(OTHERS)): caml_builtin_exceptions.cmj block.cmj js.cmj fn.c
RUNTIME := $(addsuffix .cmj, $(SOURCE_LIST))


COMPFLAGS += $(MODULE_FLAGS) -w -40 -js-npm-output-path lib/js
COMPFLAGS += $(MODULE_FLAGS) -w -40 -js-npm-output-path $(npm_package_name):lib/js



Expand Down
2 changes: 1 addition & 1 deletion jscomp/stdlib/Makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CAMLC=$(CAMLRUN) $(COMPILER)
#COMPFLAGS=-strict-sequence -w +33..39 -g -warn-error A -bin-annot -nostdlib \
# -safe-string
COMPFLAGS= $(MODULE_FLAGS) -strict-sequence -w +33..39 -g -warn-error A -nostdlib \
-safe-string -I ../runtime -js-npm-output-path lib/js
-safe-string -I ../runtime -js-npm-output-path $(npm_package_name):lib/js


# OPTCOMPILER=ocamlopt.opt
Expand Down
8 changes: 4 additions & 4 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ ext_list.cmx : ../stdlib/list.cmx ../stdlib/array.cmx
ext_log.cmj : lam_current_unit.cmj ../stdlib/format.cmi
ext_log.cmx : lam_current_unit.cmx ../stdlib/format.cmx
ext_pervasives.cmj : ../stdlib/int32.cmi ../stdlib/format.cmi \
ext_pervasives.cmi
../stdlib/arg.cmi ext_pervasives.cmi
ext_pervasives.cmx : ../stdlib/int32.cmx ../stdlib/format.cmx \
ext_pervasives.cmi
../stdlib/arg.cmx ext_pervasives.cmi
ext_string.cmj : ../stdlib/string.cmi ext_bytes.cmj ../stdlib/char.cmi \
../stdlib/bytes.cmi
ext_string.cmx : ../stdlib/string.cmx ext_bytes.cmx ../stdlib/char.cmx \
Expand Down Expand Up @@ -772,9 +772,9 @@ ext_list.cmj : ../stdlib/list.cmj ../stdlib/array.cmj
ext_log.cmo : lam_current_unit.cmo ../stdlib/format.cmi
ext_log.cmj : lam_current_unit.cmj ../stdlib/format.cmj
ext_pervasives.cmo : ../stdlib/int32.cmi ../stdlib/format.cmi \
ext_pervasives.cmi
../stdlib/arg.cmi ext_pervasives.cmi
ext_pervasives.cmj : ../stdlib/int32.cmj ../stdlib/format.cmj \
ext_pervasives.cmi
../stdlib/arg.cmj ext_pervasives.cmi
ext_string.cmo : ../stdlib/string.cmi ext_bytes.cmo ../stdlib/char.cmi \
../stdlib/bytes.cmi
ext_string.cmj : ../stdlib/string.cmj ext_bytes.cmj ../stdlib/char.cmj \
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SOURCE_LIST := $(shell cat test.mllib)
TESTS := $(addsuffix .cmj, $(SOURCE_LIST) )

COMPFLAGS+= $(MODULE_FLAGS) -w -40
COMPFLAGS+= -js-npm-output-path lib/js/test/
COMPFLAGS+= -js-npm-output-path $(npm_package_name):lib/js/test/


$(TESTS): $(CAMLC)
Expand Down
11 changes: 11 additions & 0 deletions lib/js/test/ext_pervasives.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var Pervasives = require("../pervasives");
var Caml_exceptions = require("../caml_exceptions");
var Arg = require("../arg");
var Curry = require("../curry");
var Format = require("../format");

Expand Down Expand Up @@ -82,10 +83,20 @@ function invalid_argf(fmt) {
return Format.ksprintf(Pervasives.invalid_arg, fmt);
}

function bad_argf(fmt) {
return Format.ksprintf(function (x) {
throw [
Arg.Bad,
x
];
}, fmt);
}

exports.$$finally = $$finally;
exports.with_file_as_chan = with_file_as_chan;
exports.with_file_as_pp = with_file_as_pp;
exports.is_pos_pow = is_pos_pow;
exports.failwithf = failwithf;
exports.invalid_argf = invalid_argf;
exports.bad_argf = bad_argf;
/* Format Not a pure module */

0 comments on commit 83a04f3

Please sign in to comment.