From 7882127351431738dfd34534d63739ef56d65874 Mon Sep 17 00:00:00 2001 From: tobil4sk Date: Sat, 11 Sep 2021 12:02:51 +0100 Subject: [PATCH] Clean up - Error for `-D haxe-ver` solved more generally - Add temporary function to be removed for #8690 - Minor refactoring --- src/compiler/haxe.ml | 24 +++++------------ src/context/common.ml | 62 ++++++++++++++++++++++++++++++++++++++----- src/core/define.ml | 24 ----------------- src/macro/macroApi.ml | 11 ++++---- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/src/compiler/haxe.ml b/src/compiler/haxe.ml index 2d613d28002..4c67b358949 100644 --- a/src/compiler/haxe.ml +++ b/src/compiler/haxe.ml @@ -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 = @@ -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); @@ -800,18 +793,13 @@ try ),"","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; ),"","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; ),"","define a conditional compilation flag"); ("Debug",["-v";"--verbose"],[],Arg.Unit (fun () -> com.verbose <- true diff --git a/src/context/common.ml b/src/context/common.ml index 4dba5609ae1..97ec2668fef 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -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 @@ -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 diff --git a/src/core/define.ml b/src/core/define.ml index f12fdfd5623..fb5f14503a6 100644 --- a/src/core/define.ml +++ b/src/core/define.ml @@ -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 @@ -54,10 +50,6 @@ 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 @@ -65,28 +57,12 @@ let raw_define_value ctx k v = 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 diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index 602cce86843..7833688a107 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -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