Skip to content

Commit

Permalink
add Compiler.getHxbWriterConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jan 25, 2024
1 parent 15b7c95 commit 6b0e57d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/compiler/hxb/hxbWriterConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ end

module WriterConfigReaderJson = WriterConfigReader(JsonDataApi.JsonReaderApi)

module WriterConfigWriter (API : DataWriterApi.DataWriterApi) = struct
let write_target_config config =
API.write_object [
"generate",API.write_bool config.generate;
"exclude",API.write_array (List.map (fun sl -> API.write_string (String.concat "." sl)) config.exclude);
"include",API.write_array (List.map (fun sl -> API.write_string (String.concat "." sl)) config.include');
"hxbVersion",API.write_int config.hxb_version;
]

let write_writer_config config =
API.write_object [
"archivePath",API.write_string config.archive_path;
"targetConfig",write_target_config config.target_config;
"macroConfig",write_target_config config.macro_config;
]
end

let process_json config target_name json =
WriterConfigReaderJson.read_writer_config config target_name json

Expand Down
15 changes: 15 additions & 0 deletions src/core/data/dataWriterApi.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module type DataWriterApi = sig
type data

val write_optional : data option -> data

val write_object : (string * data) list -> data

val write_array : data list -> data

val write_string : string -> data

val write_bool : bool -> data

val write_int : int -> data
end
30 changes: 29 additions & 1 deletion src/macro/eval/evalDataApi.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
open EvalValue
open EvalContext
open EvalDecode

module EvalReaderApi = struct
open EvalDecode

type data = value

let read_optional v f = match v with
Expand Down Expand Up @@ -30,4 +31,31 @@ module EvalReaderApi = struct

let data_to_string v =
(EvalPrinting.s_value 0 v).sstring
end

module EvalWriterApi = struct
open EvalEncode

type data = value

let write_optional vo = match vo with
| None -> vnull
| Some v -> v

let write_object fl =
encode_obj (List.map (fun (s,v) ->
EvalHash.hash s,v
) fl)

let write_array vl =
encode_array vl

let write_string s =
encode_string s

let write_bool b =
vbool b

let write_int i =
vint i
end
4 changes: 4 additions & 0 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type 'value compiler_api = {
with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
exc_string : 'a . string -> 'a;
get_hxb_writer_config : unit -> 'value;
set_hxb_writer_config : 'value -> unit;
}

Expand Down Expand Up @@ -2406,6 +2407,9 @@ let macro_api ccom get_api =
vbool false
end
);
"get_hxb_writer_config", vfun0 (fun () ->
(get_api()).get_hxb_writer_config ()
);
"set_hxb_writer_config", vfun1 (fun v ->
(get_api()).set_hxb_writer_config v;
vnull
Expand Down
22 changes: 17 additions & 5 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module Interp = struct
end


module HxbWriterConfigReaderJson = HxbWriterConfig.WriterConfigReader(EvalDataApi.EvalReaderApi)
module HxbWriterConfigReaderEval = HxbWriterConfig.WriterConfigReader(EvalDataApi.EvalReaderApi)
module HxbWriterConfigWriterEval = HxbWriterConfig.WriterConfigWriter(EvalDataApi.EvalWriterApi)

let macro_interp_cache = ref None

Expand Down Expand Up @@ -308,13 +309,24 @@ let make_macro_com_api com mcom p =
com.warning ~depth w [] msg p
);
exc_string = Interp.exc_string;
set_hxb_writer_config = (fun v ->
get_hxb_writer_config = (fun () ->
match com.hxb_writer_config with
| Some config ->
HxbWriterConfigReaderJson.read_writer_config config (platform_name com.platform) v
HxbWriterConfigWriterEval.write_writer_config config
| None ->
()
)
VNull
);
set_hxb_writer_config = (fun v ->
let config = match com.hxb_writer_config with
| Some config ->
config
| None ->
let config = HxbWriterConfig.create () in
com.hxb_writer_config <- Some config;
config
in
HxbWriterConfigReaderEval.read_writer_config config (platform_name com.platform) v
);
}

let make_macro_api ctx mctx p =
Expand Down
8 changes: 8 additions & 0 deletions std/haxe/macro/Compiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ class Compiler {
}
#end

static public function getHxbWriterConfiguration():Null<WriterConfig> {
#if macro
return load("get_hxb_writer_config", 0)();
#else
return null;
#end
}

static public function setHxbWriterConfiguration(config:WriterConfig) {
#if macro
load("set_hxb_writer_config", 1)(config);
Expand Down

0 comments on commit 6b0e57d

Please sign in to comment.