Skip to content

Commit d776100

Browse files
Apprentice-AlchemistSimn
authored andcommitted
[eval] add native Int64Map implementation
1 parent c60e9f9 commit d776100

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

src/core/globals.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ module IntHashtbl = Hashtbl.Make(struct
1919
let hash = Int.hash
2020
end)
2121

22+
module Int64Hashtbl = Hashtbl.Make(struct
23+
type t = Signed.Int64.t
24+
25+
let equal =
26+
Signed.Int64.equal
27+
28+
let hash = Hashtbl.hash
29+
end)
30+
2231
module StringHashtbl = Hashtbl.Make(struct
2332
type t = string
2433

src/macro/eval/evalEncode.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ let encode_bytes =
242242
let encode_int_map_direct =
243243
create_cached_instance key_haxe_ds_IntMap (fun s -> IIntMap s)
244244

245+
let encode_int64_map_direct =
246+
create_cached_instance key_haxe_ds_Int64Map (fun s -> IInt64Map s)
247+
245248
let encode_string_map_direct =
246249
create_cached_instance key_haxe_ds_StringMap (fun s -> IStringMap s)
247250

src/macro/eval/evalHash.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ let key_haxe_Exception = hash "haxe.Exception"
5656
let key_haxe_ds_Option = hash "haxe.ds.Option"
5757
let key_haxe_ds_StringMap = hash "haxe.ds.StringMap"
5858
let key_haxe_ds_IntMap = hash "haxe.ds.IntMap"
59+
let key_haxe_ds_Int64Map = hash "haxe.ds.Int64Map"
5960
let key_haxe_ds_ObjectMap = hash "haxe.ds.ObjectMap"
6061
let key_haxe_macro_Position = hash "haxe.macro.Position"
6162
let key_haxe_macro_LazyType = hash "haxe.macro.LazyType"

src/macro/eval/evalIntegers.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ let encode_haxe_i64 low high =
1818
set_instance_field vi key_low (vint32 low);
1919
vinstance vi
2020

21+
let encode_haxe_i64_int64 value =
22+
let high = Stdlib.Int64.to_int32 (Stdlib.Int64.shift_right_logical value 32) in
23+
let low = Stdlib.Int64.to_int32 value in
24+
let vi = create_instance key_haxe__Int64____Int64 in
25+
set_instance_field vi key_high (vint32 high);
26+
set_instance_field vi key_low (vint32 low);
27+
vinstance vi
28+
2129
let encode_haxe_i64_direct i64 =
2230
let low = GInt64.to_int32 i64 in
2331
let high = GInt64.to_int32 (GInt64.shift_right_logical i64 32) in

src/macro/eval/evalMain.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ let value_signature v =
295295
) map;
296296
addc 'h'
297297
)
298+
| VInstance {ikind = IInt64Map map} ->
299+
cache v (fun () ->
300+
addc 'Q';
301+
RuntimeInt64Hashtbl.iter (fun i value ->
302+
addc ':';
303+
add (Int64.to_string i);
304+
loop value
305+
) map;
306+
addc 'h'
307+
)
298308
| VInstance {ikind = IObjectMap map} ->
299309
cache v (fun() ->
300310
addc 'M';
@@ -495,6 +505,12 @@ let rec value_to_expr v p =
495505
(make_map_entry e_key v) :: acc
496506
) m [] in
497507
(EArrayDecl el,p)
508+
| VInstance {ikind = IInt64Map m} ->
509+
let el = RuntimeInt64Hashtbl.fold (fun k v acc ->
510+
let e_key = (EConst (Int (Int64.to_string k, Some "i64")),p) in
511+
(make_map_entry e_key v) :: acc
512+
) m [] in
513+
(EArrayDecl el,p)
498514
| VInstance {ikind = IStringMap m} ->
499515
let el = RuntimeStringHashtbl.fold (fun k (_,v) acc ->
500516
let e_key = (EConst (String(k,SDoubleQuotes)),p) in

src/macro/eval/evalStdLib.ml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,70 @@ module StdIntMap = struct
15701570
)
15711571
end
15721572

1573+
module StdInt64Map = struct
1574+
let this vthis = match vthis with
1575+
| VInstance {ikind = IInt64Map h} -> h
1576+
| v -> unexpected_value v "int64 map"
1577+
1578+
let copy = vifun0 (fun vthis ->
1579+
let copied = RuntimeInt64Hashtbl.copy (this vthis) in
1580+
encode_int64_map_direct copied
1581+
)
1582+
1583+
let exists = vifun1 (fun vthis vkey ->
1584+
vbool (RuntimeInt64Hashtbl.mem (this vthis) (EvalIntegers.decode_haxe_i64 vkey))
1585+
)
1586+
1587+
let get = vifun1 (fun vthis vkey ->
1588+
try RuntimeInt64Hashtbl.find (this vthis) (EvalIntegers.decode_haxe_i64 vkey)
1589+
with Not_found -> vnull
1590+
)
1591+
1592+
let iterator = vifun0 (fun vthis ->
1593+
let keys = RuntimeInt64Hashtbl.fold (fun _ v acc -> v :: acc) (this vthis) [] in
1594+
encode_list_iterator keys
1595+
)
1596+
1597+
let keys = vifun0 (fun vthis ->
1598+
let keys = RuntimeInt64Hashtbl.fold (fun k _ acc -> EvalIntegers.encode_haxe_i64_int64 k :: acc) (this vthis) [] in
1599+
encode_list_iterator keys
1600+
)
1601+
1602+
let keyValueIterator = map_key_value_iterator key_haxe_iterators_map_key_value_iterator
1603+
1604+
let remove = vifun1 (fun vthis vkey ->
1605+
let this = this vthis in
1606+
let key = EvalIntegers.decode_haxe_i64 vkey in
1607+
let b = RuntimeInt64Hashtbl.mem this key in
1608+
RuntimeInt64Hashtbl.remove this key;
1609+
vbool b
1610+
)
1611+
1612+
let set = vifun2 (fun vthis vkey vvalue ->
1613+
RuntimeInt64Hashtbl.add (this vthis) (EvalIntegers.decode_haxe_i64 vkey) vvalue;
1614+
vnull
1615+
)
1616+
1617+
let toString = vifun0 (fun vthis ->
1618+
let this = this vthis in
1619+
let l = RuntimeInt64Hashtbl.fold (fun key vvalue acc ->
1620+
(join empty_string [create_ascii (Int64.to_string key); create_ascii " => "; s_value 0 vvalue]) :: acc) this [] in
1621+
let s = join rcomma l in
1622+
let s = join empty_string [rbkopen;s;rbkclose] in
1623+
vstring s
1624+
)
1625+
1626+
let clear = vifun0 (fun vthis ->
1627+
RuntimeInt64Hashtbl.clear (this vthis);
1628+
vnull
1629+
)
1630+
1631+
let size = vifun0 (fun vthis ->
1632+
vint (RuntimeInt64Hashtbl.size (this vthis))
1633+
)
1634+
end
1635+
1636+
15731637
module StdStringMap = struct
15741638
let this vthis = match vthis with
15751639
| VInstance {ikind = IStringMap h} -> h
@@ -3229,6 +3293,19 @@ let init_maps builtins =
32293293
"clear",StdIntMap.clear;
32303294
"size",StdIntMap.size;
32313295
];
3296+
init_fields builtins (["haxe";"ds"],"Int64Map") [] [
3297+
"copy",StdInt64Map.copy;
3298+
"exists",StdInt64Map.exists;
3299+
"get",StdInt64Map.get;
3300+
"iterator",StdInt64Map.iterator;
3301+
"keys",StdInt64Map.keys;
3302+
"keyValueIterator",StdInt64Map.keyValueIterator;
3303+
"remove",StdInt64Map.remove;
3304+
"set",StdInt64Map.set;
3305+
"toString",StdInt64Map.toString;
3306+
"clear",StdInt64Map.clear;
3307+
"size",StdInt64Map.size;
3308+
];
32323309
init_fields builtins (["haxe";"ds"],"ObjectMap") [] [
32333310
"copy",StdObjectMap.copy;
32343311
"exists",StdObjectMap.exists;
@@ -3296,6 +3373,7 @@ let init_constructors builtins =
32963373
);
32973374
add key_haxe_ds_StringMap (fun _ -> encode_string_map_direct (RuntimeStringHashtbl.create ()));
32983375
add key_haxe_ds_IntMap (fun _ -> encode_int_map_direct (RuntimeIntHashtbl.create ()));
3376+
add key_haxe_ds_Int64Map (fun _ -> encode_int64_map_direct (RuntimeInt64Hashtbl.create ()));
32993377
add key_haxe_ds_ObjectMap (fun _ -> encode_object_map_direct (Obj.magic (ValueHashtbl.create 0)));
33003378
add key_haxe_io_BytesBuffer (fun _ -> encode_instance key_haxe_io_BytesBuffer ~kind:(IOutput (Buffer.create 0)));
33013379
add key_haxe_io_Bytes
@@ -3387,6 +3465,7 @@ let init_empty_constructors builtins =
33873465
IntHashtbl.add h key_String (fun () -> v_empty_string);
33883466
IntHashtbl.add h key_haxe_ds_StringMap (fun () -> encode_instance key_haxe_ds_StringMap ~kind:(IStringMap (RuntimeStringHashtbl.create ())));
33893467
IntHashtbl.add h key_haxe_ds_IntMap (fun () -> encode_instance key_haxe_ds_IntMap ~kind:(IIntMap (RuntimeIntHashtbl.create ())));
3468+
IntHashtbl.add h key_haxe_ds_Int64Map (fun () -> encode_instance key_haxe_ds_Int64Map ~kind:(IInt64Map (RuntimeInt64Hashtbl.create ())));
33903469
IntHashtbl.add h key_haxe_ds_ObjectMap (fun () -> encode_instance key_haxe_ds_ObjectMap ~kind:(IObjectMap (Obj.magic (ValueHashtbl.create 0))));
33913470
IntHashtbl.add h key_haxe_io_BytesBuffer (fun () -> encode_instance key_haxe_io_BytesBuffer ~kind:(IOutput (Buffer.create 0)))
33923471

src/macro/eval/evalValue.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ module RuntimeIntHashtbl = struct
7575
let size this = IntHashtbl.length this
7676
end
7777

78+
module RuntimeInt64Hashtbl = struct
79+
type 'value t = 'value Int64Hashtbl.t
80+
81+
let add this key v = Int64Hashtbl.replace this key v
82+
let copy this = Int64Hashtbl.copy this
83+
let create () = Int64Hashtbl.create 0
84+
let find this key = Int64Hashtbl.find this key
85+
let fold f this acc = Int64Hashtbl.fold f this acc
86+
let is_empty this = Int64Hashtbl.length this = 0
87+
let iter f this = Int64Hashtbl.iter f this
88+
let mem this key = Int64Hashtbl.mem this key
89+
let remove this key = Int64Hashtbl.remove this key
90+
let clear this = Int64Hashtbl.clear this
91+
let size this = Int64Hashtbl.length this
92+
end
93+
7894
type vregex = {
7995
r : Pcre2.regexp;
8096
r_rex_string : vstring;
@@ -190,6 +206,7 @@ and vinstance_kind =
190206
| IDate of float
191207
| IStringMap of value RuntimeStringHashtbl.t
192208
| IIntMap of value RuntimeIntHashtbl.t
209+
| IInt64Map of value RuntimeInt64Hashtbl.t
193210
| IObjectMap of (value,value) Hashtbl.t
194211
| IOutput of Buffer.t (* BytesBuffer *)
195212
| IBuffer of vstring_buffer(* StringBuf *)

0 commit comments

Comments
 (0)