Exit_status.show
[hiphop-php.git] / hphp / hack / src / facts / rust_facts_ffi.rs
blobf97469b98ff2dbc23affa4fa9fa98113b2a3af4b
1 // Copyright (c) 2019, Facebook, Inc.
2 // All rights reserved.
3 //
4 // This source code is licensed under the MIT license found in the
5 // LICENSE file in the "hack" directory of this source tree.
7 use facts_rust as facts;
8 use hhbc_string_utils_rust::without_xhp_mangling;
9 use ocamlrep::{bytes_from_ocamlrep, ptr::UnsafeOcamlPtr};
10 use ocamlrep_ocamlpool::ocaml_ffi_no_panic;
11 use oxidized::relative_path::RelativePath;
13 use facts::facts_parser::*;
15 ocaml_ffi_no_panic! {
16     fn extract_as_json_ffi(
17         flags: i32,
18         filename: RelativePath,
19         text_ptr: UnsafeOcamlPtr,
20         mangle_xhp: bool,
21     ) -> Option<String> {
22         // Safety: the OCaml garbage collector must not run as long as text_ptr
23         // and text_value exist. We don't call into OCaml here, so it won't.
24         let text_value = unsafe { text_ptr.as_value() };
25         let text = bytes_from_ocamlrep(text_value).expect("expected string");
26         extract_as_json_ffi0(
27             ((1 << 0) & flags) != 0, // php5_compat_mode
28             ((1 << 1) & flags) != 0, // hhvm_compat_mode
29             ((1 << 2) & flags) != 0, // allow_new_attribute_syntax
30             ((1 << 3) & flags) != 0, // enable_xhp_class_modifier
31             ((1 << 4) & flags) != 0, // disable_xhp_element_mangling
32             filename,
33             text,
34             mangle_xhp,
35         )
36     }
39 fn extract_as_json_ffi0(
40     php5_compat_mode: bool,
41     hhvm_compat_mode: bool,
42     allow_new_attribute_syntax: bool,
43     enable_xhp_class_modifier: bool,
44     disable_xhp_element_mangling: bool,
45     filename: RelativePath,
46     text: &[u8],
47     mangle_xhp: bool,
48 ) -> Option<String> {
49     let opts = ExtractAsJsonOpts {
50         php5_compat_mode,
51         hhvm_compat_mode,
52         allow_new_attribute_syntax,
53         enable_xhp_class_modifier,
54         disable_xhp_element_mangling,
55         filename,
56     };
57     if mangle_xhp {
58         extract_as_json(text, opts)
59     } else {
60         without_xhp_mangling(|| extract_as_json(text, opts))
61     }