Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
- Error for `-D haxe-ver` solved more generally
  - Add temporary function to be removed for HaxeFoundation#8690
- Minor refactoring
  • Loading branch information
tobil4sk committed Sep 11, 2021
1 parent 3c87faf commit 7882127
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
24 changes: 6 additions & 18 deletions src/compiler/haxe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ let error ctx msg p =
message ctx (CMError(msg,p));
ctx.has_error <- true

let reserved_flags = [
"true";"false";"null";"cross";"js";"lua";"neko";"flash";"php";"cpp";"cs";"java";"python";
"swc";"macro";"sys";"static";"utf16";"haxe";"haxe_ver";"haxe-ver"
]

let reserved_flag_namespaces = ["target"]

let delete_file f = try Sys.remove f with _ -> ()

let expand_env ?(h=None) path =
Expand Down Expand Up @@ -497,7 +490,7 @@ let create_typer_context ctx native_libs =
let buffer = Buffer.create 64 in
Buffer.add_string buffer "Defines: ";
PMap.iter (fun k v -> match v with
"1" -> Printf.bprintf buffer "%s;" k
| "1" -> Printf.bprintf buffer "%s;" k
| _ -> Printf.bprintf buffer "%s=%s;" k v
) com.defines.values;
Buffer.truncate buffer (Buffer.length buffer - 1);
Expand Down Expand Up @@ -800,18 +793,13 @@ try
),"<class>","select startup class");
("Compilation",["-L";"--library"],["-lib"],Arg.String (fun l ->
cp_libs := l :: !cp_libs;
Define.external_define com.defines l;
Common.external_define com l;
),"<name[:ver]>","use a haxelib library");
("Compilation",["-D";"--define"],[],Arg.String (fun var ->
let flag, value = try ExtString.String.split var "=" with _ -> var, "1" in
let raise_reserved description =
raise (Arg.Bad (description ^ " and cannot be defined from the command line"))
in
if List.mem flag reserved_flags then raise_reserved (Printf.sprintf "`%s` is a reserved compiler flag" flag);
List.iter (fun ns ->
if ExtString.String.starts_with flag (ns ^ ".") then raise_reserved (Printf.sprintf "`%s` uses the reserved compiler flag namespace `%s.*`" flag ns)
) reserved_flag_namespaces;
Define.external_define_value com.defines flag value;
let flag, value = try let split = ExtString.String.split var "=" in (fst split, Some (snd split)) with _ -> var, None in
match value with
| Some value -> Common.external_define_value com flag value
| None -> Common.external_define com flag;
),"<var[=value]>","define a conditional compilation flag");
("Debug",["-v";"--verbose"],[],Arg.Unit (fun () ->
com.verbose <- true
Expand Down
62 changes: 55 additions & 7 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,56 @@ let raw_define com v =
let define_value com k v =
Define.define_value com.defines k v

let raw_defined_value com k =
Define.raw_defined_value com.defines k
let convert_define k =
String.concat "_" (ExtString.String.nsplit k "-")

let external_defined ctx k =
Define.raw_defined ctx.defines (convert_define k)

let external_defined_value ctx k =
Define.raw_defined_value ctx.defines (convert_define k)

let reserved_flags = [
"true";"false";"null";"cross";"js";"lua";"neko";"flash";"php";"cpp";"cs";"java";"python";
"swc";"macro";"sys";"static";"utf16";"haxe";"haxe_ver"
]

let reserved_flag_namespaces = ["target"]

let convert_and_validate k =
let converted_flag = convert_define k in
let raise_reserved description =
raise (Arg.Bad (description ^ " and cannot be defined from the command line"))
in
if List.mem converted_flag reserved_flags then
raise_reserved (Printf.sprintf "`%s` is a reserved compiler flag" k);
List.iter (fun ns ->
if ExtString.String.starts_with converted_flag (ns ^ ".") then
raise_reserved (Printf.sprintf "`%s` uses the reserved compiler flag namespace `%s.*`" k ns)
) reserved_flag_namespaces;
converted_flag

let external_define_value ctx k v =
raw_define_value ctx.defines (convert_and_validate k) v

(* TODO: Temporary function until #8690, remove after *)
let external_define_value_no_check ctx k v =
Define.raw_define_value ctx.defines (convert_define k) v

let external_define ctx k =
Define.raw_define ctx.defines (convert_and_validate k)

(* TODO: Temporary function until #8690, remove after *)
let external_define_no_check ctx k =
Define.raw_define ctx.defines (convert_define k)

let defines_for_external ctx =
PMap.foldi (fun k v acc ->
let added_underscore = PMap.add k v acc in
match ExtString.String.nsplit k "_" with
| [_] -> added_underscore
| split -> PMap.add (String.concat "-" split) v added_underscore;
) ctx.defines.values PMap.empty

let get_es_version com =
try int_of_string (defined_value com Define.JsEs) with _ -> 0
Expand Down Expand Up @@ -787,23 +835,23 @@ let init_platform com pf =
com.package_rules <- List.fold_left forbid com.package_rules ("jvm" :: (List.map platform_name platforms));
com.config <- get_config com;
if com.config.pf_static then begin
raw_define_value com.defines "target.static" "true";
raw_define com "target.static";
define com Define.Static;
end;
if com.config.pf_sys then begin
raw_define_value com.defines "target.sys" "true";
raw_define com "target.sys";
define com Define.Sys
end else
com.package_rules <- PMap.add "sys" Forbidden com.package_rules;
if com.config.pf_uses_utf16 then begin
raw_define_value com.defines "target.utf16" "true";
raw_define com "target.utf16";
define com Define.Utf16;
end;
if com.config.pf_supports_threads then begin
raw_define_value com.defines "target.threaded" "true";
raw_define com "target.threaded";
end;
if com.config.pf_supports_unicode then begin
raw_define_value com.defines "target.unicode" "true";
raw_define com "target.unicode";
end;
raw_define_value com.defines "target.name" name;
raw_define com name
Expand Down
24 changes: 0 additions & 24 deletions src/core/define.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ let raw_defined ctx k =
let defined ctx k =
raw_defined ctx (get_define_key k)

let external_defined ctx k =
let k = String.concat "_" (ExtString.String.nsplit k "-") in
raw_defined ctx k

let raw_defined_value ctx k =
PMap.find k ctx.values

Expand All @@ -54,39 +50,19 @@ let defined_value_safe ?default ctx v =
try defined_value ctx v
with Not_found -> match default with Some s -> s | None -> ""

let external_defined_value ctx k =
let k = String.concat "_" (ExtString.String.nsplit k "-") in
PMap.find k ctx.values

let raw_define_value ctx k v =
ctx.values <- PMap.add k v ctx.values;
ctx.defines_signature <- None

let define_value ctx k v =
raw_define_value ctx (get_define_key k) v

let external_define_value ctx k v =
let k = String.concat "_" (ExtString.String.nsplit k "-") in
raw_define_value ctx k v

let raw_define ctx k =
raw_define_value ctx k "1"

let define ctx k =
raw_define_value ctx (get_define_key k) "1"

let external_define ctx k =
let k = String.concat "_" (ExtString.String.nsplit k "-") in
raw_define_value ctx k "1"

let defines_for_external ctx =
PMap.foldi (fun k v acc ->
let added_underscore = PMap.add k v acc in
match ExtString.String.nsplit k "_" with
| [_] -> added_underscore
| split -> PMap.add (String.concat "-" split) v added_underscore;
) ctx.values PMap.empty

let get_signature def =
match def.defines_signature with
| Some s -> s
Expand Down
11 changes: 6 additions & 5 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1610,20 +1610,21 @@ let macro_api ccom get_api =
let v = if v = vnull then "" else ", " ^ (decode_string v) in
com.warning ("Should be used in initialization macros only: haxe.macro.Compiler.define(" ^ s ^ v ^ ")") Globals.null_pos;
end;
(* TODO: use external_define and external_define_value for #8690 *)
if v = vnull then
Define.external_define com.defines s
Common.external_define_no_check com s
else
Define.external_define_value com.defines s (decode_string v);
Common.external_define_value_no_check com s (decode_string v);
vnull
);
"defined", vfun1 (fun s ->
vbool (Define.external_defined (ccom()).defines (decode_string s))
vbool (Common.external_defined (ccom()) (decode_string s))
);
"defined_value", vfun1 (fun s ->
try encode_string (Define.external_defined_value (ccom()).defines (decode_string s)) with Not_found -> vnull
try encode_string (Common.external_defined_value (ccom()) (decode_string s)) with Not_found -> vnull
);
"get_defines", vfun0 (fun() ->
encode_string_map encode_string (Define.defines_for_external (ccom()).defines)
encode_string_map encode_string (Common.defines_for_external (ccom()))
);
"get_type", vfun1 (fun s ->
let tname = decode_string s in
Expand Down

0 comments on commit 7882127

Please sign in to comment.