v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
bidirectional_bindings.cpp
Go to the documentation of this file.
1 #if 0
3  std::stringstream result;
4  auto matches = wrapped_class.annotations.get_regex("v8toolkit_generate_(.*)");
5  if (wrapped_class.annotations.has(V8TOOLKIT_BIDIRECTIONAL_CLASS_STRING)) {
6  result << fmt::format("class JS{} : public {}, public v8toolkit::JSWrapper<{}> {{\npublic:\n", // {{ is escaped {
7  short_name(), short_name(), short_name());
8  result << fmt::format(" JS{}(v8::Local<v8::Context> context, v8::Local<v8::Object> object,\n", short_name());
9  result << fmt::format(" v8::Local<v8::FunctionTemplate> created_by");
10  bool got_constructor = false;
11  int constructor_parameter_count;
12  vector<QualType> constructor_parameters;
13  foreach_constructor(wrapped_class.decl, [&](auto constructor_decl){
14  if (got_constructor) {
15  data_error(fmt::format("ERROR: Got more than one constructor for {}", wrapped_class.class_name));
16  return;
17  }
18  got_constructor = true;
19  result << get_method_parameters(compiler_instance, wrapped_class, constructor_decl, true, true);
20  constructor_parameter_count = constructor_decl->getNumParams();
21  constructor_parameters = get_method_param_qual_types(this->compiler_instance, constructor_decl);
22 
24  if (!got_constructor) {
25  data_error(fmt::format("ERROR: Got no bidirectional constructor for {}", wrapped_class.class_name));
26 
27  }
28  result << fmt::format(") :\n");
29 
30  // auto variable_names = generate_variable_names(constructor_parameter_count);
31  auto variable_names = generate_variable_names(constructor_parameters, true);
32 
33  result << fmt::format(" {}({}),\n", short_name(), join(variable_names));
34  result << fmt::format(" v8toolkit::JSWrapper<{}>(context, object, created_by) {{}}\n", short_name()); // {{}} is escaped {}
35  result << handle_class(wrapped_class.decl);
36  result << "};\n";
37  } else {
38 // printf("Class %s not marked bidirectional\n", short_name().c_str());
39  return;
40  }
41 
42  // dumps a file per class
43 // if (print_logging) cerr << "Dumping JSWrapper type for " << short_name() << endl;
44  ofstream bidirectional_class_file;
45  auto bidirectional_class_filename = fmt::format("v8toolkit_generated_bidirectional_{}.h", short_name());
46  bidirectional_class_file.open(bidirectional_class_filename, ios::out);
47  if (!bidirectional_class_file) {
48  llvm::report_fatal_error(fmt::format("Could not open file: {}", bidirectional_class_filename), false);
49  }
50 
51  bidirectional_class_file << "#pragma once\n\n";
52 
53 
54  // This needs include files because the IMPLEMENTATION goes in the file (via macros).
55  // If the implementation was moved out to a .cpp file, then the header file could
56  // rely soley on the primary type's includes
57  for (auto & include : this->wrapped_class.include_files) {
58  if (include == ""){continue;}
59  bidirectional_class_file << "#include " << include << "\n";
60  }
61 
62 
63  // need to include all the includes from the parent types because the implementation of this bidirectional
64  // type may need the types for things the parent type .h files don't need (like unique_ptr contained types)
65  auto all_base_type_includes = this->wrapped_class.get_base_type_includes();
66 
67  for (auto & include : all_base_type_includes) {
68  std::cerr << fmt::format("for bidirectional {}, adding base type include {}", this->short_name(), include) << std::endl;
69  bidirectional_class_file << "#include " << include << "\n";
70  }
71 
72  bidirectional_class_file << result.str();
73  bidirectional_class_file.close();
74 
75 
76 }
77 
78 #endif
void data_error(const string &error)
vector< QualType > get_method_param_qual_types(CompilerInstance &compiler_instance, const CXXMethodDecl *method, string const &annotation)
#define V8TOOLKIT_BIDIRECTIONAL_CONSTRUCTOR_STRING
Definition: class_parser.h:107
#define V8TOOLKIT_BIDIRECTIONAL_CLASS_STRING
Definition: class_parser.h:106
vector< string > generate_variable_names(vector< QualType > qual_types, bool with_std_move)
void generate_bindings()
std::string join(const T &source, const std::string &between=", ", bool leading_between=false)
std::string get_method_parameters(CompilerInstance &compiler_instance, WrappedClass &wrapped_class, const CXXMethodDecl *method, bool add_leading_comma=false, bool insert_variable_names=false, const string &annotation="")
void foreach_constructor(const CXXRecordDecl *klass, Callback &&callback, const std::string &annotation="")