WrapperGenerator.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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/StringBuilder.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <ctype.h>
  31. static String snake_name(const StringView& title_name)
  32. {
  33. StringBuilder builder;
  34. bool first = true;
  35. bool last_was_uppercase = false;
  36. for (auto ch : title_name) {
  37. if (isupper(ch)) {
  38. if (!first && !last_was_uppercase)
  39. builder.append('_');
  40. builder.append(tolower(ch));
  41. } else {
  42. builder.append(ch);
  43. }
  44. first = false;
  45. last_was_uppercase = isupper(ch);
  46. }
  47. return builder.to_string();
  48. }
  49. namespace IDL {
  50. struct Type {
  51. String name;
  52. bool nullable { false };
  53. };
  54. struct Parameter {
  55. Type type;
  56. String name;
  57. };
  58. struct Function {
  59. Type return_type;
  60. String name;
  61. Vector<Parameter> parameters;
  62. size_t length() const
  63. {
  64. // FIXME: Take optional arguments into account
  65. return parameters.size();
  66. }
  67. };
  68. struct Attribute {
  69. bool readonly { false };
  70. bool unsigned_ { false };
  71. Type type;
  72. String name;
  73. // Added for convenience after parsing
  74. String getter_callback_name;
  75. String setter_callback_name;
  76. };
  77. struct Interface {
  78. String name;
  79. String parent_name;
  80. Vector<Attribute> attributes;
  81. Vector<Function> functions;
  82. // Added for convenience after parsing
  83. String wrapper_class;
  84. String wrapper_base_class;
  85. };
  86. OwnPtr<Interface> parse_interface(const StringView& input)
  87. {
  88. auto interface = make<Interface>();
  89. size_t index = 0;
  90. auto peek = [&](size_t offset = 0) -> char {
  91. if (index + offset > input.length())
  92. return 0;
  93. return input[index + offset];
  94. };
  95. auto consume = [&] {
  96. return input[index++];
  97. };
  98. auto consume_if = [&](auto ch) {
  99. if (peek() == ch) {
  100. consume();
  101. return true;
  102. }
  103. return false;
  104. };
  105. auto consume_specific = [&](char ch) {
  106. auto consumed = consume();
  107. if (consumed != ch) {
  108. dbg() << "Expected '" << ch << "' at offset " << index << " but got '" << consumed << "'";
  109. ASSERT_NOT_REACHED();
  110. }
  111. };
  112. auto consume_whitespace = [&] {
  113. while (isspace(peek()))
  114. consume();
  115. };
  116. auto consume_string = [&](const StringView& string) {
  117. for (size_t i = 0; i < string.length(); ++i) {
  118. ASSERT(consume() == string[i]);
  119. }
  120. };
  121. auto next_is = [&](const StringView& string) {
  122. for (size_t i = 0; i < string.length(); ++i) {
  123. if (peek(i) != string[i])
  124. return false;
  125. }
  126. return true;
  127. };
  128. auto consume_while = [&](auto condition) {
  129. StringBuilder builder;
  130. while (index < input.length() && condition(peek())) {
  131. builder.append(consume());
  132. }
  133. return builder.to_string();
  134. };
  135. consume_string("interface");
  136. consume_whitespace();
  137. interface->name = consume_while([](auto ch) { return !isspace(ch); });
  138. consume_whitespace();
  139. if (consume_if(':')) {
  140. consume_whitespace();
  141. interface->parent_name = consume_while([](auto ch) { return !isspace(ch); });
  142. consume_whitespace();
  143. }
  144. consume_specific('{');
  145. auto parse_type = [&] {
  146. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != '?'; });
  147. auto nullable = peek() == '?';
  148. if (nullable)
  149. consume_specific('?');
  150. return Type { name, nullable };
  151. };
  152. auto parse_attribute = [&] {
  153. bool readonly = false;
  154. bool unsigned_ = false;
  155. if (next_is("readonly")) {
  156. consume_string("readonly");
  157. readonly = true;
  158. consume_whitespace();
  159. }
  160. if (next_is("attribute")) {
  161. consume_string("attribute");
  162. consume_whitespace();
  163. }
  164. if (next_is("unsigned")) {
  165. consume_string("unsigned");
  166. unsigned_ = true;
  167. consume_whitespace();
  168. }
  169. auto type = parse_type();
  170. consume_whitespace();
  171. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != ';'; });
  172. consume_specific(';');
  173. Attribute attribute;
  174. attribute.readonly = readonly;
  175. attribute.unsigned_ = unsigned_;
  176. attribute.type = type;
  177. attribute.name = name;
  178. attribute.getter_callback_name = String::format("%s_getter", snake_name(attribute.name).characters());
  179. attribute.setter_callback_name = String::format("%s_setter", snake_name(attribute.name).characters());
  180. interface->attributes.append(move(attribute));
  181. };
  182. auto parse_function = [&] {
  183. auto return_type = parse_type();
  184. consume_whitespace();
  185. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != '('; });
  186. consume_specific('(');
  187. Vector<Parameter> parameters;
  188. for (;;) {
  189. if (consume_if(')'))
  190. break;
  191. auto type = parse_type();
  192. consume_whitespace();
  193. auto name = consume_while([](auto ch) { return !isspace(ch) && ch != ',' && ch != ')'; });
  194. parameters.append({ move(type), move(name) });
  195. if (consume_if(')'))
  196. break;
  197. consume_specific(',');
  198. consume_whitespace();
  199. }
  200. consume_specific(';');
  201. interface->functions.append(Function { return_type, name, move(parameters) });
  202. };
  203. for (;;) {
  204. consume_whitespace();
  205. if (consume_if('}'))
  206. break;
  207. if (next_is("readonly") || next_is("attribute")) {
  208. parse_attribute();
  209. continue;
  210. }
  211. parse_function();
  212. }
  213. interface->wrapper_class = String::format("%sWrapper", interface->name.characters());
  214. interface->wrapper_base_class = String::format("%sWrapper", interface->parent_name.is_empty() ? "" : interface->parent_name.characters());
  215. return interface;
  216. }
  217. }
  218. static void generate_header(const IDL::Interface&);
  219. static void generate_implementation(const IDL::Interface&);
  220. int main(int argc, char** argv)
  221. {
  222. Core::ArgsParser args_parser;
  223. const char* path = nullptr;
  224. bool header_mode = false;
  225. bool implementation_mode = false;
  226. args_parser.add_option(header_mode, "Generate the wrapper .h file", "header", 'H');
  227. args_parser.add_option(implementation_mode, "Generate the wrapper .cpp file", "implementation", 'I');
  228. args_parser.add_positional_argument(path, "IDL file", "idl-file");
  229. args_parser.parse(argc, argv);
  230. auto file_or_error = Core::File::open(path, Core::IODevice::ReadOnly);
  231. if (file_or_error.is_error()) {
  232. fprintf(stderr, "Cannot open %s\n", path);
  233. return 1;
  234. }
  235. auto data = file_or_error.value()->read_all();
  236. auto interface = IDL::parse_interface(data);
  237. if (!interface) {
  238. fprintf(stderr, "Cannot parse %s\n", path);
  239. return 1;
  240. }
  241. #if 0
  242. dbg() << "Attributes:";
  243. for (auto& attribute : interface->attributes) {
  244. dbg() << " " << (attribute.readonly ? "Readonly " : "")
  245. << attribute.type.name << (attribute.type.nullable ? "?" : "")
  246. << " " << attribute.name;
  247. }
  248. dbg() << "Functions:";
  249. for (auto& function : interface->functions) {
  250. dbg() << " " << function.return_type.name << (function.return_type.nullable ? "?" : "")
  251. << " " << function.name;
  252. for (auto& parameter : function.parameters) {
  253. dbg() << " " << parameter.type.name << (parameter.type.nullable ? "?" : "") << " " << parameter.name;
  254. }
  255. }
  256. #endif
  257. if (header_mode)
  258. generate_header(*interface);
  259. if (implementation_mode)
  260. generate_implementation(*interface);
  261. return 0;
  262. }
  263. static bool should_emit_wrapper_factory(const IDL::Interface& interface)
  264. {
  265. // FIXME: This is very hackish.
  266. if (interface.name == "EventTarget")
  267. return false;
  268. if (interface.name == "Node")
  269. return false;
  270. if (interface.name == "Text")
  271. return false;
  272. if (interface.name == "Document")
  273. return false;
  274. if (interface.name == "DocumentType")
  275. return false;
  276. if (interface.name.ends_with("Element"))
  277. return false;
  278. if (interface.name.ends_with("Event"))
  279. return false;
  280. return true;
  281. }
  282. static bool is_wrappable_type(const IDL::Type& type)
  283. {
  284. if (type.name == "Node")
  285. return true;
  286. if (type.name == "Document")
  287. return true;
  288. if (type.name == "Text")
  289. return true;
  290. if (type.name == "DocumentType")
  291. return true;
  292. if (type.name.ends_with("Element"))
  293. return true;
  294. if (type.name == "ImageData")
  295. return true;
  296. return false;
  297. }
  298. static void generate_header(const IDL::Interface& interface)
  299. {
  300. auto& wrapper_class = interface.wrapper_class;
  301. auto& wrapper_base_class = interface.wrapper_base_class;
  302. out() << "#pragma once";
  303. out() << "#include <LibWeb/Bindings/Wrapper.h>";
  304. out() << "#include <LibWeb/DOM/" << interface.name << ".h>";
  305. if (wrapper_base_class != "Wrapper")
  306. out() << "#include <LibWeb/Bindings/" << wrapper_base_class << ".h>";
  307. out() << "namespace Web {";
  308. out() << "namespace Bindings {";
  309. out() << "class " << wrapper_class << " : public " << wrapper_base_class << " {";
  310. out() << " JS_OBJECT(" << wrapper_class << ", " << wrapper_base_class << ");";
  311. out() << "public:";
  312. out() << " " << wrapper_class << "(JS::GlobalObject&, " << interface.name << "&);";
  313. out() << " virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override;";
  314. out() << " virtual ~" << wrapper_class << "() override;";
  315. if (wrapper_base_class == "Wrapper") {
  316. out() << " " << interface.name << "& impl() { return *m_impl; }";
  317. out() << " const " << interface.name << "& impl() const { return *m_impl; }";
  318. } else {
  319. out() << " " << interface.name << "& impl() { return static_cast<" << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
  320. out() << " const " << interface.name << "& impl() const { return static_cast<const " << interface.name << "&>(" << wrapper_base_class << "::impl()); }";
  321. }
  322. auto is_foo_wrapper_name = snake_name(String::format("Is%s", wrapper_class.characters()));
  323. out() << " virtual bool " << is_foo_wrapper_name << "() const final { return true; }";
  324. out() << "private:";
  325. for (auto& function : interface.functions) {
  326. out() << " JS_DECLARE_NATIVE_FUNCTION(" << snake_name(function.name) << ");";
  327. }
  328. for (auto& attribute : interface.attributes) {
  329. out() << " JS_DECLARE_NATIVE_GETTER(" << snake_name(attribute.name) << "_getter);";
  330. if (!attribute.readonly)
  331. out() << " JS_DECLARE_NATIVE_SETTER(" << snake_name(attribute.name) << "_setter);";
  332. }
  333. if (wrapper_base_class == "Wrapper") {
  334. out() << " NonnullRefPtr<" << interface.name << "> m_impl;";
  335. }
  336. out() << "};";
  337. if (should_emit_wrapper_factory(interface)) {
  338. out() << wrapper_class << "* wrap(JS::GlobalObject&, " << interface.name << "&);";
  339. }
  340. out() << "}";
  341. out() << "}";
  342. }
  343. void generate_implementation(const IDL::Interface& interface)
  344. {
  345. auto& wrapper_class = interface.wrapper_class;
  346. auto& wrapper_base_class = interface.wrapper_base_class;
  347. out() << "#include <AK/FlyString.h>";
  348. out() << "#include <LibJS/Interpreter.h>";
  349. out() << "#include <LibJS/Runtime/Array.h>";
  350. out() << "#include <LibJS/Runtime/Value.h>";
  351. out() << "#include <LibJS/Runtime/GlobalObject.h>";
  352. out() << "#include <LibJS/Runtime/Error.h>";
  353. out() << "#include <LibJS/Runtime/Function.h>";
  354. out() << "#include <LibJS/Runtime/Uint8ClampedArray.h>";
  355. out() << "#include <LibWeb/Bindings/NodeWrapperFactory.h>";
  356. out() << "#include <LibWeb/Bindings/" << wrapper_class << ".h>";
  357. out() << "#include <LibWeb/DOM/Element.h>";
  358. out() << "#include <LibWeb/DOM/HTMLElement.h>";
  359. out() << "#include <LibWeb/DOM/EventListener.h>";
  360. out() << "#include <LibWeb/Bindings/DocumentTypeWrapper.h>";
  361. out() << "#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>";
  362. out() << "#include <LibWeb/Bindings/HTMLImageElementWrapper.h>";
  363. out() << "#include <LibWeb/Bindings/ImageDataWrapper.h>";
  364. out() << "#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>";
  365. out() << "namespace Web {";
  366. out() << "namespace Bindings {";
  367. // Implementation: Wrapper constructor
  368. out() << wrapper_class << "::" << wrapper_class << "(JS::GlobalObject& global_object, " << interface.name << "& impl)";
  369. if (wrapper_base_class == "Wrapper") {
  370. out() << " : Wrapper(*global_object.object_prototype())";
  371. out() << " , m_impl(impl)";
  372. } else {
  373. out() << " : " << wrapper_base_class << "(global_object, impl)";
  374. }
  375. out() << "{";
  376. out() << "}";
  377. // Implementation: Wrapper initialize()
  378. out() << "void " << wrapper_class << "::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
  379. out() << "{";
  380. out() << " [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable;";
  381. out() << " " << wrapper_base_class << "::initialize(interpreter, global_object);";
  382. for (auto& attribute : interface.attributes) {
  383. out() << " define_native_property(\"" << attribute.name << "\", " << attribute.getter_callback_name << ", " << (attribute.readonly ? "nullptr" : attribute.setter_callback_name) << ", default_attributes);";
  384. }
  385. for (auto& function : interface.functions) {
  386. out() << " define_native_function(\"" << function.name << "\", " << snake_name(function.name) << ", " << function.length() << ", default_attributes);";
  387. }
  388. out() << "}";
  389. // Implementation: Wrapper destructor
  390. out() << wrapper_class << "::~" << wrapper_class << "()";
  391. out() << "{";
  392. out() << "}";
  393. // Implementation: impl_from()
  394. if (!interface.attributes.is_empty() || !interface.functions.is_empty()) {
  395. out() << "static " << interface.name << "* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
  396. out() << "{";
  397. out() << " auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);";
  398. out() << " if (!this_object)";
  399. out() << " return {};";
  400. out() << " if (!this_object->inherits(\"" << wrapper_class << "\")) {";
  401. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << interface.name << "\");";
  402. out() << " return nullptr;";
  403. out() << " }";
  404. out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();";
  405. out() << "}";
  406. }
  407. auto generate_to_cpp = [&](auto& parameter, auto& js_name, auto& js_suffix, auto cpp_name, bool return_void = false) {
  408. auto generate_return = [&] {
  409. if (return_void)
  410. out() << " return;";
  411. else
  412. out() << " return {};";
  413. };
  414. if (parameter.type.name == "DOMString") {
  415. out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_string(interpreter);";
  416. out() << " if (interpreter.exception())";
  417. generate_return();
  418. } else if (parameter.type.name == "EventListener") {
  419. out() << " if (!" << js_name << js_suffix << ".is_function()) {";
  420. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"Function\");";
  421. generate_return();
  422. out() << " }";
  423. out() << " auto " << cpp_name << " = adopt(*new EventListener(JS::make_handle(&" << js_name << js_suffix << ".as_function())));";
  424. } else if (is_wrappable_type(parameter.type)) {
  425. out() << " auto " << cpp_name << "_object = " << js_name << js_suffix << ".to_object(interpreter, global_object);";
  426. out() << " if (interpreter.exception())";
  427. generate_return();
  428. out() << " if (!" << cpp_name << "_object->inherits(\"" << parameter.type.name << "Wrapper\")) {";
  429. out() << " interpreter.throw_exception<JS::TypeError>(JS::ErrorType::NotA, \"" << parameter.type.name << "\");";
  430. generate_return();
  431. out() << " }";
  432. out() << " auto& " << cpp_name << " = static_cast<" << parameter.type.name << "Wrapper*>(" << cpp_name << "_object)->impl();";
  433. } else if (parameter.type.name == "double") {
  434. out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_double(interpreter);";
  435. out() << " if (interpreter.exception())";
  436. generate_return();
  437. } else {
  438. dbg() << "Unimplemented JS-to-C++ conversion: " << parameter.type.name;
  439. ASSERT_NOT_REACHED();
  440. }
  441. };
  442. auto generate_arguments = [&](auto& parameters, auto& arguments_builder, bool return_void = false) {
  443. Vector<String> parameter_names;
  444. size_t argument_index = 0;
  445. for (auto& parameter : parameters) {
  446. parameter_names.append(snake_name(parameter.name));
  447. out() << " auto arg" << argument_index << " = interpreter.argument(" << argument_index << ");";
  448. generate_to_cpp(parameter, "arg", argument_index, snake_name(parameter.name), return_void);
  449. ++argument_index;
  450. }
  451. arguments_builder.join(", ", parameter_names);
  452. };
  453. auto generate_return_statement = [&](auto& return_type) {
  454. if (return_type.name == "void") {
  455. out() << " return JS::js_undefined();";
  456. return;
  457. }
  458. if (return_type.nullable) {
  459. if (return_type.name == "DOMString") {
  460. out() << " if (retval.is_null())";
  461. } else {
  462. out() << " if (!retval)";
  463. }
  464. out() << " return JS::js_null();";
  465. }
  466. if (return_type.name == "DOMString") {
  467. out() << " return JS::js_string(interpreter, retval);";
  468. } else if (return_type.name == "ArrayFromVector") {
  469. // FIXME: Remove this fake type hack once it's no longer needed.
  470. // Basically once we have NodeList we can throw this out.
  471. out() << " auto* new_array = JS::Array::create(global_object);";
  472. out() << " for (auto& element : retval) {";
  473. out() << " new_array->indexed_properties().append(wrap(global_object, element));";
  474. out() << " }";
  475. out() << " return new_array;";
  476. } else if (return_type.name == "long" || return_type.name == "double") {
  477. out() << " return JS::Value(retval);";
  478. } else if (return_type.name == "Uint8ClampedArray") {
  479. out() << " return retval;";
  480. } else {
  481. out() << " return wrap(global_object, const_cast<" << return_type.name << "&>(*retval));";
  482. }
  483. };
  484. // Implementation: Attributes
  485. for (auto& attribute : interface.attributes) {
  486. out() << "JS_DEFINE_NATIVE_GETTER(" << wrapper_class << "::" << attribute.getter_callback_name << ")";
  487. out() << "{";
  488. out() << " auto* impl = impl_from(interpreter, global_object);";
  489. out() << " if (!impl)";
  490. out() << " return {};";
  491. out() << " auto retval = impl->" << snake_name(attribute.name) << "();";
  492. generate_return_statement(attribute.type);
  493. out() << "}";
  494. if (!attribute.readonly) {
  495. out() << "JS_DEFINE_NATIVE_SETTER(" << wrapper_class << "::" << attribute.setter_callback_name << ")";
  496. out() << "{";
  497. out() << " auto* impl = impl_from(interpreter, global_object);";
  498. out() << " if (!impl)";
  499. out() << " return;";
  500. generate_to_cpp(attribute, "value", "", "cpp_value", true);
  501. out() << " impl->set_" << snake_name(attribute.name) << "(cpp_value);";
  502. out() << "}";
  503. }
  504. }
  505. // Implementation: Functions
  506. for (auto& function : interface.functions) {
  507. out() << "JS_DEFINE_NATIVE_FUNCTION(" << wrapper_class << "::" << snake_name(function.name) << ")";
  508. out() << "{";
  509. out() << " auto* impl = impl_from(interpreter, global_object);";
  510. out() << " if (!impl)";
  511. out() << " return {};";
  512. if (function.length() > 0) {
  513. out() << " if (interpreter.argument_count() < " << function.length() << ")";
  514. out() << " return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountMany, \"" << function.name << "\", \"" << function.length() << "\");";
  515. }
  516. StringBuilder arguments_builder;
  517. generate_arguments(function.parameters, arguments_builder);
  518. if (function.return_type.name != "void") {
  519. out() << " auto retval = impl->" << snake_name(function.name) << "(" << arguments_builder.to_string() << ");";
  520. } else {
  521. out() << " impl->" << snake_name(function.name) << "(" << arguments_builder.to_string() << ");";
  522. }
  523. generate_return_statement(function.return_type);
  524. out() << "}";
  525. }
  526. // Implementation: Wrapper factory
  527. if (should_emit_wrapper_factory(interface)) {
  528. out() << wrapper_class << "* wrap(JS::GlobalObject& global_object, " << interface.name << "& impl)";
  529. out() << "{";
  530. out() << " return static_cast<" << wrapper_class << "*>(wrap_impl(global_object, impl));";
  531. out() << "}";
  532. }
  533. out() << "}";
  534. out() << "}";
  535. }