WrapperGenerator.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/File.h>
  31. #include <ctype.h>
  32. static String snake_name(const StringView& title_name)
  33. {
  34. StringBuilder builder;
  35. bool first = true;
  36. bool last_was_uppercase = false;
  37. for (auto ch : title_name) {
  38. if (isupper(ch)) {
  39. if (!first && !last_was_uppercase)
  40. builder.append('_');
  41. builder.append(tolower(ch));
  42. } else {
  43. builder.append(ch);
  44. }
  45. first = false;
  46. last_was_uppercase = isupper(ch);
  47. }
  48. return builder.to_string();
  49. }
  50. static String add_underscore_to_cpp_keywords(const String& input)
  51. {
  52. if (input == "class" || input == "template") {
  53. StringBuilder builder;
  54. builder.append(input);
  55. builder.append('_');
  56. return builder.to_string();
  57. }
  58. return input;
  59. }
  60. namespace IDL {
  61. struct Type {
  62. String name;
  63. bool nullable { false };
  64. };
  65. struct Parameter {
  66. Type type;
  67. String name;
  68. };
  69. struct Function {
  70. Type return_type;
  71. String name;
  72. Vector<Parameter> parameters;
  73. HashMap<String, String> extended_attributes;
  74. size_t length() const
  75. {
  76. // FIXME: Take optional arguments into account
  77. return parameters.size();
  78. }
  79. };
  80. struct Attribute {
  81. bool readonly { false };
  82. bool unsigned_ { false };
  83. Type type;
  84. String name;
  85. HashMap<String, String> extended_attributes;
  86. // Added for convenience after parsing
  87. String getter_callback_name;
  88. String setter_callback_name;
  89. };
  90. struct Interface {
  91. String name;
  92. String parent_name;
  93. Vector<Attribute> attributes;
  94. Vector<Function> functions;
  95. // Added for convenience after parsing
  96. String wrapper_class;
  97. String wrapper_base_class;
  98. };
  99. OwnPtr<Interface> parse_interface(const StringView& input)
  100. {
  101. auto interface = make<Interface>();
  102. size_t index = 0;
  103. auto peek = [&](size_t offset = 0) -> char {
  104. if (index + offset > input.length())
  105. return 0;
  106. return input[index + offset];
  107. };
  108. auto consume = [&] {
  109. return input[index++];
  110. };
  111. auto consume_if = [&](auto ch) {
  112. if (peek() == ch) {
  113. consume();
  114. return true;
  115. }
  116. return false;
  117. };
  118. auto consume_specific = [&](char ch) {
  119. auto consumed = consume();
  120. if (consumed != ch) {
  121. dbg() << "Expected '" << ch << "' at offset " << index << " but got '" << consumed << "'";
  122. ASSERT_NOT_REACHED();
  123. }
  124. };
  125. auto consume_whitespace = [&] {
  126. while (isspace(peek()))
  127. consume();
  128. };
  129. auto consume_string = [&](const StringView& string) {
  130. for (size_t i = 0; i < string.length(); ++i) {
  131. ASSERT(consume() == string[i]);
  132. }
  133. };
  134. auto next_is = [&](const StringView& string) {
  135. for (size_t i = 0; i < string.length(); ++i) {
  136. if (peek(i) != string[i])
  137. return false;
  138. }
  139. return true;
  140. };
  141. auto consume_while = [&](auto condition) {
  142. StringBuilder builder;
  143. while (index < input.length() && condition(peek())) {
  144. builder.append(consume());
  145. }
  146. return builder.to_string();
  147. };
  148. consume_string("interface");
  149. consume_whitespace();
  150. interface->name = consume_while([](auto ch) { return !isspace(ch); });
  151. consume_whitespace();
  152. if (consume_if(':')) {
  153. consume_whitespace();
  154. interface->parent_name = consume_while([](auto ch) { return !isspace(ch); });
  155. consume_whitespace();
  156. }
  157. consume_specific('{');
  158. auto parse_type = [&] {
  159. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != '?'; });
  160. auto nullable = peek() == '?';
  161. if (nullable)
  162. consume_specific('?');
  163. return Type { name, nullable };
  164. };
  165. auto parse_attribute = [&](HashMap<String, String>& extended_attributes) {
  166. bool readonly = false;
  167. bool unsigned_ = false;
  168. if (next_is("readonly")) {
  169. consume_string("readonly");
  170. readonly = true;
  171. consume_whitespace();
  172. }
  173. if (next_is("attribute")) {
  174. consume_string("attribute");
  175. consume_whitespace();
  176. }
  177. if (next_is("unsigned")) {
  178. consume_string("unsigned");
  179. unsigned_ = true;
  180. consume_whitespace();
  181. }
  182. auto type = parse_type();
  183. consume_whitespace();
  184. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != ';'; });
  185. consume_specific(';');
  186. Attribute attribute;
  187. attribute.readonly = readonly;
  188. attribute.unsigned_ = unsigned_;
  189. attribute.type = type;
  190. attribute.name = name;
  191. attribute.getter_callback_name = String::format("%s_getter", snake_name(attribute.name).characters());
  192. attribute.setter_callback_name = String::format("%s_setter", snake_name(attribute.name).characters());
  193. attribute.extended_attributes = move(extended_attributes);
  194. interface->attributes.append(move(attribute));
  195. };
  196. auto parse_function = [&](HashMap<String, String>& extended_attributes) {
  197. auto return_type = parse_type();
  198. consume_whitespace();
  199. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != '('; });
  200. consume_specific('(');
  201. Vector<Parameter> parameters;
  202. for (;;) {
  203. if (consume_if(')'))
  204. break;
  205. auto type = parse_type();
  206. consume_whitespace();
  207. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != ',' && ch != ')'; });
  208. parameters.append({ move(type), move(name) });
  209. if (consume_if(')'))
  210. break;
  211. consume_specific(',');
  212. consume_whitespace();
  213. }
  214. consume_specific(';');
  215. interface->functions.append(Function { return_type, name, move(parameters), move(extended_attributes) });
  216. };
  217. auto parse_extended_attributes = [&] {
  218. HashMap<String, String> extended_attributes;
  219. for (;;) {
  220. consume_whitespace();
  221. if (consume_if(']'))
  222. break;
  223. auto name = consume_while([](auto ch) { return ch != ']' && ch != '=' && ch != ','; });
  224. if (consume_if('=')) {
  225. auto value = consume_while([](auto ch) { return ch != ']' && ch != ','; });
  226. extended_attributes.set(name, value);
  227. } else {
  228. extended_attributes.set(name, {});
  229. }
  230. }
  231. consume_whitespace();
  232. return extended_attributes;
  233. };
  234. for (;;) {
  235. HashMap<String, String> extended_attributes;
  236. consume_whitespace();
  237. if (consume_if('}'))
  238. break;
  239. if (consume_if('[')) {
  240. extended_attributes = parse_extended_attributes();
  241. }
  242. if (next_is("readonly") || next_is("attribute")) {
  243. parse_attribute(extended_attributes);
  244. continue;
  245. }
  246. parse_function(extended_attributes);
  247. }
  248. interface->wrapper_class = String::format("%sWrapper", interface->name.characters());
  249. interface->wrapper_base_class = String::format("%sWrapper", interface->parent_name.is_empty() ? "" : interface->parent_name.characters());
  250. return interface;
  251. }
  252. }
  253. static void generate_header(const IDL::Interface&);
  254. static void generate_implementation(const IDL::Interface&);
  255. int main(int argc, char** argv)
  256. {
  257. Core::ArgsParser args_parser;
  258. const char* path = nullptr;
  259. bool header_mode = false;
  260. bool implementation_mode = false;
  261. args_parser.add_option(header_mode, "Generate the wrapper .h file", "header", 'H');
  262. args_parser.add_option(implementation_mode, "Generate the wrapper .cpp file", "implementation", 'I');
  263. args_parser.add_positional_argument(path, "IDL file", "idl-file");
  264. args_parser.parse(argc, argv);
  265. auto file_or_error = Core::File::open(path, Core::IODevice::ReadOnly);
  266. if (file_or_error.is_error()) {
  267. fprintf(stderr, "Cannot open %s\n", path);
  268. return 1;
  269. }
  270. auto data = file_or_error.value()->read_all();
  271. auto interface = IDL::parse_interface(data);
  272. if (!interface) {
  273. fprintf(stderr, "Cannot parse %s\n", path);
  274. return 1;
  275. }
  276. #if 0
  277. dbg() << "Attributes:";
  278. for (auto& attribute : interface->attributes) {
  279. dbg() << " " << (attribute.readonly ? "Readonly " : "")
  280. << attribute.type.name << (attribute.type.nullable ? "?" : "")
  281. << " " << attribute.name;
  282. }
  283. dbg() << "Functions:";
  284. for (auto& function : interface->functions) {
  285. dbg() << " " << function.return_type.name << (function.return_type.nullable ? "?" : "")
  286. << " " << function.name;
  287. for (auto& parameter : function.parameters) {
  288. dbg() << " " << parameter.type.name << (parameter.type.nullable ? "?" : "") << " " << parameter.name;
  289. }
  290. }
  291. #endif
  292. if (header_mode)
  293. generate_header(*interface);
  294. if (implementation_mode)
  295. generate_implementation(*interface);
  296. return 0;
  297. }
  298. static bool should_emit_wrapper_factory(const IDL::Interface& interface)
  299. {
  300. // FIXME: This is very hackish.
  301. if (interface.name == "EventTarget")
  302. return false;
  303. if (interface.name == "Node")
  304. return false;
  305. if (interface.name == "Text")
  306. return false;
  307. if (interface.name == "Document")
  308. return false;
  309. if (interface.name == "DocumentType")
  310. return false;
  311. if (interface.name.ends_with("Element"))
  312. return false;
  313. if (interface.name.ends_with("Event"))
  314. return false;
  315. return true;
  316. }
  317. static bool is_wrappable_type(const IDL::Type& type)
  318. {
  319. if (type.name == "Node")
  320. return true;
  321. if (type.name == "Document")
  322. return true;
  323. if (type.name == "Text")
  324. return true;
  325. if (type.name == "DocumentType")
  326. return true;
  327. if (type.name.ends_with("Element"))
  328. return true;
  329. if (type.name == "ImageData")
  330. return true;
  331. return false;
  332. }
  333. static void generate_header(const IDL::Interface& interface)
  334. {
  335. auto& wrapper_class = interface.wrapper_class;
  336. auto& wrapper_base_class = interface.wrapper_base_class;
  337. out() << "#pragma once";
  338. out() << "#include <LibWeb/Bindings/Wrapper.h>";
  339. out() << "#include <LibWeb/DOM/" << interface.name << ".h>";
  340. if (wrapper_base_class != "Wrapper")
  341. out() << "#include <LibWeb/Bindings/" << wrapper_base_class << ".h>";
  342. out() << "namespace Web {";
  343. out() << "namespace Bindings {";
  344. out() << "class " << wrapper_class << " : public " << wrapper_base_class << " {";
  345. out() << " JS_OBJECT(" << wrapper_class << ", " << wrapper_base_class << ");";
  346. out() << "public:";
  347. out() << " " << wrapper_class << "(JS::GlobalObject&, " << interface.name << "&);";
  348. out() << " virtual void initialize(JS::GlobalObject&) override;";
  349. out() << " virtual ~" << wrapper_class << "() override;";
  350. if (wrapper_base_class == "Wrapper") {
  351. out() << " " << interface.name << "& impl() { return *m_impl; }";
  352. out() << " const " << interface.name << "& impl() const { return *m_impl; }";
  353. } else {
  354. out() << " " << interface.name << "& impl() { return static_cast<" << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
  355. out() << " const " << interface.name << "& impl() const { return static_cast<const " << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
  356. }
  357. auto is_foo_wrapper_name = snake_name(String::format("Is%s", wrapper_class.characters()));
  358. out() << " virtual bool " << is_foo_wrapper_name << "() const final { return true; }";
  359. out() << "private:";
  360. for (auto& function : interface.functions) {
  361. out() << " JS_DECLARE_NATIVE_FUNCTION(" << snake_name(function.name) << ");";
  362. }
  363. for (auto& attribute : interface.attributes) {
  364. out() << " JS_DECLARE_NATIVE_GETTER(" << snake_name(attribute.name) << "_getter);";
  365. if (!attribute.readonly)
  366. out() << " JS_DECLARE_NATIVE_SETTER(" << snake_name(attribute.name) << "_setter);";
  367. }
  368. if (wrapper_base_class == "Wrapper") {
  369. out() << " NonnullRefPtr<" << interface.name << "> m_impl;";
  370. }
  371. out() << "};";
  372. if (should_emit_wrapper_factory(interface)) {
  373. out() << wrapper_class << "* wrap(JS::GlobalObject&, " << interface.name << "&);";
  374. }
  375. out() << "}";
  376. out() << "}";
  377. }
  378. void generate_implementation(const IDL::Interface& interface)
  379. {
  380. auto& wrapper_class = interface.wrapper_class;
  381. auto& wrapper_base_class = interface.wrapper_base_class;
  382. out() << "#include <AK/FlyString.h>";
  383. out() << "#include <LibJS/Interpreter.h>";
  384. out() << "#include <LibJS/Runtime/Array.h>";
  385. out() << "#include <LibJS/Runtime/Value.h>";
  386. out() << "#include <LibJS/Runtime/GlobalObject.h>";
  387. out() << "#include <LibJS/Runtime/Error.h>";
  388. out() << "#include <LibJS/Runtime/Function.h>";
  389. out() << "#include <LibJS/Runtime/Uint8ClampedArray.h>";
  390. out() << "#include <LibWeb/Bindings/NodeWrapperFactory.h>";
  391. out() << "#include <LibWeb/Bindings/" << wrapper_class << ".h>";
  392. out() << "#include <LibWeb/DOM/Element.h>";
  393. out() << "#include <LibWeb/DOM/HTMLElement.h>";
  394. out() << "#include <LibWeb/DOM/EventListener.h>";
  395. out() << "#include <LibWeb/Bindings/DocumentTypeWrapper.h>";
  396. out() << "#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>";
  397. out() << "#include <LibWeb/Bindings/HTMLImageElementWrapper.h>";
  398. out() << "#include <LibWeb/Bindings/ImageDataWrapper.h>";
  399. out() << "#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>";
  400. out() << "namespace Web {";
  401. out() << "namespace Bindings {";
  402. // Implementation: Wrapper constructor
  403. out() << wrapper_class << "::" << wrapper_class << "(JS::GlobalObject& global_object, " << interface.name << "& impl)";
  404. if (wrapper_base_class == "Wrapper") {
  405. out() << " : Wrapper(*global_object.object_prototype())";
  406. out() << " , m_impl(impl)";
  407. } else {
  408. out() << " : " << wrapper_base_class << "(global_object, impl)";
  409. }
  410. out() << "{";
  411. out() << "}";
  412. // Implementation: Wrapper initialize()
  413. out() << "void " << wrapper_class << "::initialize(JS::GlobalObject& global_object)";
  414. out() << "{";
  415. out() << " [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable;";
  416. out() << " " << wrapper_base_class << "::initialize(global_object);";
  417. for (auto& attribute : interface.attributes) {
  418. out() << " define_native_property(\"" << attribute.name << "\", " << attribute.getter_callback_name << ", " << (attribute.readonly ? "nullptr" : attribute.setter_callback_name) << ", default_attributes);";
  419. }
  420. for (auto& function : interface.functions) {
  421. out() << " define_native_function(\"" << function.name << "\", " << snake_name(function.name) << ", " << function.length() << ", default_attributes);";
  422. }
  423. out() << "}";
  424. // Implementation: Wrapper destructor
  425. out() << wrapper_class << "::~" << wrapper_class << "()";
  426. out() << "{";
  427. out() << "}";
  428. // Implementation: impl_from()
  429. if (!interface.attributes.is_empty() || !interface.functions.is_empty()) {
  430. out() << "static " << interface.name << "* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
  431. out() << "{";
  432. out() << " auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);";
  433. out() << " if (!this_object)";
  434. out() << " return {};";
  435. out() << " if (!this_object->inherits(\"" << wrapper_class << "\")) {";
  436. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << interface.name << "\");";
  437. out() << " return nullptr;";
  438. out() << " }";
  439. out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();";
  440. out() << "}";
  441. }
  442. auto generate_to_cpp = [&](auto& parameter, auto& js_name, auto& js_suffix, auto cpp_name, bool return_void = false) {
  443. auto generate_return = [&] {
  444. if (return_void)
  445. out() << " return;";
  446. else
  447. out() << " return {};";
  448. };
  449. if (parameter.type.name == "DOMString") {
  450. out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_string(interpreter);";
  451. out() << " if (interpreter.exception())";
  452. generate_return();
  453. } else if (parameter.type.name == "EventListener") {
  454. out() << " if (!" << js_name << js_suffix << ".is_function()) {";
  455. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"Function\");";
  456. generate_return();
  457. out() << " }";
  458. out() << " auto " << cpp_name << " = adopt(*new EventListener(JS::make_handle(&" << js_name << js_suffix << ".as_function())));";
  459. } else if (is_wrappable_type(parameter.type)) {
  460. out() << " auto " << cpp_name << "_object = " << js_name << js_suffix << ".to_object(interpreter, global_object);";
  461. out() << " if (interpreter.exception())";
  462. generate_return();
  463. out() << " if (!" << cpp_name << "_object->inherits(\"" << parameter.type.name << "Wrapper\")) {";
  464. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << parameter.type.name << "\");";
  465. generate_return();
  466. out() << " }";
  467. out() << " auto& " << cpp_name << " = static_cast<" << parameter.type.name << "Wrapper*>(" << cpp_name << "_object)->impl();";
  468. } else if (parameter.type.name == "double") {
  469. out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_double(interpreter);";
  470. out() << " if (interpreter.exception())";
  471. generate_return();
  472. } else {
  473. dbg() << "Unimplemented JS-to-C++ conversion: " << parameter.type.name;
  474. ASSERT_NOT_REACHED();
  475. }
  476. };
  477. auto generate_arguments = [&](auto& parameters, auto& arguments_builder, bool return_void = false) {
  478. Vector<String> parameter_names;
  479. size_t argument_index = 0;
  480. for (auto& parameter : parameters) {
  481. parameter_names.append(snake_name(parameter.name));
  482. out() << " auto arg" << argument_index << " = interpreter.argument(" << argument_index << ");";
  483. generate_to_cpp(parameter, "arg", argument_index, snake_name(parameter.name), return_void);
  484. ++argument_index;
  485. }
  486. arguments_builder.join(", ", parameter_names);
  487. };
  488. auto generate_return_statement = [&](auto& return_type) {
  489. if (return_type.name == "void") {
  490. out() << " return JS::js_undefined();";
  491. return;
  492. }
  493. if (return_type.nullable) {
  494. if (return_type.name == "DOMString") {
  495. out() << " if (retval.is_null())";
  496. } else {
  497. out() << " if (!retval)";
  498. }
  499. out() << " return JS::js_null();";
  500. }
  501. if (return_type.name == "DOMString") {
  502. out() << " return JS::js_string(interpreter, retval);";
  503. } else if (return_type.name == "ArrayFromVector") {
  504. // FIXME: Remove this fake type hack once it's no longer needed.
  505. // Basically once we have NodeList we can throw this out.
  506. out() << " auto* new_array = JS::Array::create(global_object);";
  507. out() << " for (auto& element : retval) {";
  508. out() << " new_array->indexed_properties().append(wrap(global_object, element));";
  509. out() << " }";
  510. out() << " return new_array;";
  511. } else if (return_type.name == "long" || return_type.name == "double") {
  512. out() << " return JS::Value(retval);";
  513. } else if (return_type.name == "Uint8ClampedArray") {
  514. out() << " return retval;";
  515. } else {
  516. out() << " return wrap(global_object, const_cast<" << return_type.name << "&>(*retval));";
  517. }
  518. };
  519. // Implementation: Attributes
  520. for (auto& attribute : interface.attributes) {
  521. out() << "JS_DEFINE_NATIVE_GETTER(" << wrapper_class << "::" << attribute.getter_callback_name << ")";
  522. out() << "{";
  523. out() << " auto* impl = impl_from(interpreter, global_object);";
  524. out() << " if (!impl)";
  525. out() << " return {};";
  526. if (attribute.extended_attributes.contains("Reflect")) {
  527. auto attribute_name = attribute.extended_attributes.get("Reflect").value();
  528. if (attribute_name.is_null())
  529. attribute_name = attribute.name;
  530. attribute_name = add_underscore_to_cpp_keywords(attribute_name);
  531. out() << " auto retval = impl->attribute(HTML::AttributeNames::" << attribute_name << ");";
  532. } else {
  533. out() << " auto retval = impl->" << snake_name(attribute.name) << "();";
  534. }
  535. generate_return_statement(attribute.type);
  536. out() << "}";
  537. if (!attribute.readonly) {
  538. out() << "JS_DEFINE_NATIVE_SETTER(" << wrapper_class << "::" << attribute.setter_callback_name << ")";
  539. out() << "{";
  540. out() << " auto* impl = impl_from(interpreter, global_object);";
  541. out() << " if (!impl)";
  542. out() << " return;";
  543. generate_to_cpp(attribute, "value", "", "cpp_value", true);
  544. if (attribute.extended_attributes.contains("Reflect")) {
  545. auto attribute_name = attribute.extended_attributes.get("Reflect").value();
  546. if (attribute_name.is_null())
  547. attribute_name = attribute.name;
  548. attribute_name = add_underscore_to_cpp_keywords(attribute_name);
  549. out() << " impl->set_attribute(HTML::AttributeNames::" << attribute_name << ", cpp_value);";
  550. } else {
  551. out() << " impl->set_" << snake_name(attribute.name) << "(cpp_value);";
  552. }
  553. out() << "}";
  554. }
  555. }
  556. // Implementation: Functions
  557. for (auto& function : interface.functions) {
  558. out() << "JS_DEFINE_NATIVE_FUNCTION(" << wrapper_class << "::" << snake_name(function.name) << ")";
  559. out() << "{";
  560. out() << " auto* impl = impl_from(interpreter, global_object);";
  561. out() << " if (!impl)";
  562. out() << " return {};";
  563. if (function.length() > 0) {
  564. out() << " if (interpreter.argument_count() < " << function.length() << ")";
  565. out() << " return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountMany, \"" << function.name << "\", \"" << function.length() << "\");";
  566. }
  567. StringBuilder arguments_builder;
  568. generate_arguments(function.parameters, arguments_builder);
  569. if (function.return_type.name != "void") {
  570. out() << " auto retval = impl->" << snake_name(function.name) << "(" << arguments_builder.to_string() << ");";
  571. } else {
  572. out() << " impl->" << snake_name(function.name) << "(" << arguments_builder.to_string() << ");";
  573. }
  574. generate_return_statement(function.return_type);
  575. out() << "}";
  576. }
  577. // Implementation: Wrapper factory
  578. if (should_emit_wrapper_factory(interface)) {
  579. out() << wrapper_class << "* wrap(JS::GlobalObject& global_object, " << interface.name << "& impl)";
  580. out() << "{";
  581. out() << " return static_cast<" << wrapper_class << "*>(wrap_impl(global_object, impl));";
  582. out() << "}";
  583. }
  584. out() << "}";
  585. out() << "}";
  586. }