Skip to content

Commit

Permalink
Merge 6977cca into 25c5ffa
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Dec 24, 2017
2 parents 25c5ffa + 6977cca commit 0fdbe54
Show file tree
Hide file tree
Showing 18 changed files with 3,354 additions and 15 deletions.
18 changes: 11 additions & 7 deletions jscomp/core/lam.ml
Expand Up @@ -1799,32 +1799,32 @@ let convert exports lam : _ * _ =
and convert_js_primitive (p: Primitive.description) (args : Lambda.lambda list) loc =
let s = p.prim_name in
match () with
| () when s = "#raw_expr" ->
| _ when s = "#raw_expr" ->
begin match args with
| [Lconst( Const_base (Const_string(s,_)))] ->
prim ~primitive:(Praw_js_code_exp s)
~args:[] loc
| _ -> assert false
end
| () when s = "#raw_stmt" ->
| _ when s = "#raw_stmt" ->
begin match args with
| [Lconst( Const_base (Const_string(s,_)))] ->
prim ~primitive:(Praw_js_code_stmt s)
~args:[] loc
| _ -> assert false
end
| () when s = "#debugger" ->
| _ when s = "#debugger" ->
(* ATT: Currently, the arity is one due to PPX *)
prim ~primitive:Pdebugger ~args:[] loc
| () when s = "#null" ->
| _ when s = "#null" ->
Lconst (Const_js_null)

| () when s = "#undefined" ->
| _ when s = "#undefined" ->
Lconst (Const_js_undefined)
| () ->
| _ ->
let primitive =
match s with
| "#apply" -> Pjs_runtime_apply
| "#apply" -> Pjs_runtime_apply
| "#apply1"
| "#apply2"
| "#apply3"
Expand All @@ -1833,6 +1833,10 @@ let convert exports lam : _ * _ =
| "#apply6"
| "#apply7"
| "#apply8" -> Pjs_apply
| "#makemutablelist" ->
Pmakeblock(0,Lambda.Blk_constructor("::",1),Mutable)
| "#setfield1" ->
Psetfield(1, true, Fld_set_na)
| "#undefined_to_opt" -> Pundefined_to_opt
| "#null_undefined_to_opt" -> Pnull_undefined_to_opt
| "#null_to_opt" -> Pnull_to_opt
Expand Down
4 changes: 4 additions & 0 deletions jscomp/others/.depend
Expand Up @@ -22,6 +22,9 @@ bs_internalAVLtree.cmj :
bs_internalMutableAVLSet.cmj : bs_internalAVLset.cmj
bs_Hash.cmj : bs_Hash.cmi
bs_Queue.cmj : bs_Array.cmj bs_Queue.cmi
bs_internalLinkList.cmj :
bs_LinkList.cmj : bs_Array.cmj bs.cmj
bs_List.cmj : bs_Array.cmj bs_List.cmi
bs_internalBucketsType.cmj : bs_Array.cmj
bs_internalSetBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj bs.cmj
bs_internalBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
Expand Down Expand Up @@ -68,6 +71,7 @@ js_mapperRt.cmi :
bs_Array.cmi :
bs_Hash.cmi :
bs_Queue.cmi :
bs_List.cmi :
bs_HashMap.cmi : bs_Hash.cmi bs_Bag.cmj
bs_HashSet.cmi : bs_Hash.cmi bs_Bag.cmj
bs_HashSetString.cmi :
Expand Down
3 changes: 3 additions & 0 deletions jscomp/others/Makefile
Expand Up @@ -17,6 +17,9 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
bs_internalMutableAVL\
bs_Hash\
bs_Queue\
bs_internalLinkList\
bs_LinkList\
bs_List\
bs_internalBucketsType\
bs_internalSetBuckets\
bs_internalBuckets\
Expand Down
3 changes: 2 additions & 1 deletion jscomp/others/bs.ml
Expand Up @@ -43,5 +43,6 @@ module MapInt = Bs_MapInt
module MapString = Bs_MapString
module SetInt = Bs_SetInt
module SetString = Bs_SetString

module LinkList = Bs_LinkList
module List = Bs_List

115 changes: 115 additions & 0 deletions jscomp/others/bs_LinkList.ml
@@ -0,0 +1,115 @@


type 'a cell = {
mutable head : 'a;
mutable tail : 'a opt_cell
}

and 'a opt_cell = 'a cell Js.null

and 'a t = {
length : int ;
data : 'a opt_cell
} [@@bs.deriving abstract]


external assertAsNonNull : 'a Js.null -> 'a = "%identity"
external tailOption : 'a cell -> 'a cell option = "tail" [@@bs.get] [@@bs.return null_to_opt]

let toOpt = Js.nullToOption
let return = Js.Null.return
let empty = Js.Null.empty

let headOpt ( x : _ t) =
toOpt (data x)

let tailOpt (x : _ t) =
match toOpt (data x ) with
| None -> None
| Some x -> tailOption x

let rec lengthCellAux (x : _ opt_cell) acc =
match toOpt x with
| None -> acc
| Some x -> lengthCellAux (tail x) (acc + 1)

let checkInvariant (x : _ t) : unit =
[%assert length x = lengthCellAux ( data x ) 0]

let rec nextAuxAssert (opt_cell : 'a opt_cell) n =
let cell = (assertAsNonNull opt_cell) in
if n = 0 then
(head cell)
else
nextAuxAssert (tail cell) (n - 1)

let nthOpt x n =
if n < 0 then None
else if n < (length x) then
Some (nextAuxAssert (data x) n)
else
None

let nthAssert x n =
if n < 0 then [%assert "Neg"]
else nextAuxAssert (data x) n

let rec copyAux (cellX : _ opt_cell) (prec : _ cell) =
match toOpt cellX with
| None -> prec
| Some cellX ->
let h, t = head cellX, tail cellX in
let next = cell ~head:h ~tail:empty in
tailSet prec (return next);
copyAux t next

let copyNonEmptyTo xs ys =
let cell = cell ~head:(head xs) ~tail:empty in
let newTail = copyAux (tail xs) cell in
tailSet newTail ys;
cell

let append (x : 'a t) (y : 'a t) : 'a t =
let lenX = length x in
if lenX = 0 then y
else
let lenY = length y in
if lenY = 0 then x
else
let h = assertAsNonNull (data x) in
(* let cell = cell ~head:(head h) ~tail:empty in
let newTail = copyAux (tail h) cell in
tailSet newTail (data y) ; *)
let cell = copyNonEmptyTo h (data y) in
t ~length:(lenX + lenY) ~data:(return cell )


let init n f =
if n < 0 then [%assert "Invalid_argument"]
else
if n = 0 then
t ~length:0 ~data:empty (* TODO could be shared *)
else
let headX = (cell ~head:(f 0 [@bs]) ~tail:empty) in
let cur = ref headX in
let i = ref 1 in
while !i < n do
let v = cell ~head:(f !i [@bs]) ~tail:empty in
tailSet !cur (return v);
cur := v;
incr i;
done ;
t ~length:n ~data:(return headX)

let rec fillAux arr i (cell_opt : _ opt_cell) =
match toOpt cell_opt with
| None -> ()
| Some x ->
Bs_Array.unsafe_set arr i (head x) ;
fillAux arr (i + 1) (tail x)

let toArray (x : _ t) =
let len = length x in
let arr = Bs.Array.makeUninitializedUnsafe len in
fillAux arr 0 (data x);
arr

0 comments on commit 0fdbe54

Please sign in to comment.