Allow function pointer builtins in constant initializers
[hiphop-php.git] / hphp / hack / src / facts / rust_facts_ffi.rs
blob604f0dccf5ef5876a8d35b38469f796520da58cf
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 ocaml::caml;
9 use oxidized::relative_path::RelativePath;
11 use facts::facts_parser::*;
13 caml!(extract_as_json_ffi(
14     flags,
15     filename,
16     text,
17     mangle_xhp
18 ) {
19     use ocaml::ToValue;
20     let flags = flags.i32_val();
21     return extract_as_json_ffi0(
22         ((1 << 0) & flags) != 0, // php5_compat_mode
23         ((1 << 1) & flags) != 0, // hhvm_compat_mode
24         ((1 << 2) & flags) != 0, // allow_new_attribute_syntax
25         ((1 << 3) & flags) != 0, // disallow_func_ptrs_in_constants
26         RelativePath::from_ocamlvalue(&filename),
27         ocaml::Str::from(text).as_str(),
28         mangle_xhp.i32_val() != 0,
29     ).to_value();
30 });
32 fn extract_as_json_ffi0(
33     php5_compat_mode: bool,
34     hhvm_compat_mode: bool,
35     allow_new_attribute_syntax: bool,
36     disallow_func_ptrs_in_constants: bool,
37     filename: RelativePath,
38     text: &str,
39     mangle_xhp: bool,
40 ) -> String {
41     let opts = ExtractAsJsonOpts {
42         php5_compat_mode,
43         hhvm_compat_mode,
44         allow_new_attribute_syntax,
45         disallow_func_ptrs_in_constants,
46         filename,
47     };
48     // return empty string in case of failure (ambiguous because "" is not a valid JSON)
49     // as ocaml-rs doesn't offer conversion from/to option (caller will use None for failure)
50     let f = || extract_as_json(text, opts).unwrap_or(String::from(""));
51     if mangle_xhp {
52         f()
53     } else {
54         without_xhp_mangling(f)
55     }