t_as3_generator.cc 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <sstream>
  20. #include <string>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <vector>
  24. #include <cctype>
  25. #include <sys/stat.h>
  26. #include <stdexcept>
  27. #include "thrift/platform.h"
  28. #include "thrift/generate/t_oop_generator.h"
  29. using std::map;
  30. using std::ostream;
  31. using std::ostringstream;
  32. using std::string;
  33. using std::stringstream;
  34. using std::vector;
  35. static const string endl = "\n"; // avoid ostream << std::endl flushes
  36. /**
  37. * AS3 code generator.
  38. *
  39. */
  40. class t_as3_generator : public t_oop_generator {
  41. public:
  42. t_as3_generator(t_program* program,
  43. const std::map<std::string, std::string>& parsed_options,
  44. const std::string& option_string)
  45. : t_oop_generator(program) {
  46. (void)option_string;
  47. std::map<std::string, std::string>::const_iterator iter;
  48. bindable_ = false;
  49. for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
  50. if( iter->first.compare("bindable") == 0) {
  51. bindable_ = true;
  52. } else {
  53. throw "unknown option as3:" + iter->first;
  54. }
  55. }
  56. out_dir_base_ = "gen-as3";
  57. }
  58. /**
  59. * Init and close methods
  60. */
  61. void init_generator();
  62. void close_generator();
  63. void generate_consts(std::vector<t_const*> consts);
  64. /**
  65. * Program-level generation functions
  66. */
  67. void generate_typedef(t_typedef* ttypedef);
  68. void generate_enum(t_enum* tenum);
  69. void generate_struct(t_struct* tstruct);
  70. void generate_xception(t_struct* txception);
  71. void generate_service(t_service* tservice);
  72. void print_const_value(std::ostream& out,
  73. std::string name,
  74. t_type* type,
  75. t_const_value* value,
  76. bool in_static,
  77. bool defval = false);
  78. std::string render_const_value(ostream& out,
  79. std::string name,
  80. t_type* type,
  81. t_const_value* value);
  82. /**
  83. * Service-level generation functions
  84. */
  85. void generate_as3_struct(t_struct* tstruct, bool is_exception);
  86. void generate_as3_struct_definition(std::ostream& out,
  87. t_struct* tstruct,
  88. bool is_xception = false,
  89. bool in_class = false,
  90. bool is_result = false);
  91. // removed -- equality,compare_to
  92. void generate_as3_struct_reader(std::ostream& out, t_struct* tstruct);
  93. void generate_as3_validator(std::ostream& out, t_struct* tstruct);
  94. void generate_as3_struct_result_writer(std::ostream& out, t_struct* tstruct);
  95. void generate_as3_struct_writer(std::ostream& out, t_struct* tstruct);
  96. void generate_as3_struct_tostring(std::ostream& out, t_struct* tstruct, bool bindable);
  97. void generate_as3_meta_data_map(std::ostream& out, t_struct* tstruct);
  98. void generate_field_value_meta_data(std::ostream& out, t_type* type);
  99. std::string get_as3_type_string(t_type* type);
  100. void generate_reflection_setters(std::ostringstream& out,
  101. t_type* type,
  102. std::string field_name,
  103. std::string cap_name);
  104. void generate_reflection_getters(std::ostringstream& out,
  105. t_type* type,
  106. std::string field_name,
  107. std::string cap_name);
  108. void generate_generic_field_getters_setters(std::ostream& out, t_struct* tstruct);
  109. void generate_generic_isset_method(std::ostream& out, t_struct* tstruct);
  110. void generate_as3_bean_boilerplate(std::ostream& out, t_struct* tstruct, bool bindable);
  111. void generate_function_helpers(t_function* tfunction);
  112. std::string get_cap_name(std::string name);
  113. std::string generate_isset_check(t_field* field);
  114. std::string generate_isset_check(std::string field);
  115. void generate_isset_set(ostream& out, t_field* field);
  116. // removed std::string isset_field_id(t_field* field);
  117. void generate_service_interface(t_service* tservice);
  118. void generate_service_helpers(t_service* tservice);
  119. void generate_service_client(t_service* tservice);
  120. void generate_service_server(t_service* tservice);
  121. void generate_process_function(t_service* tservice, t_function* tfunction);
  122. /**
  123. * Serialization constructs
  124. */
  125. void generate_deserialize_field(std::ostream& out, t_field* tfield, std::string prefix = "");
  126. void generate_deserialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = "");
  127. void generate_deserialize_container(std::ostream& out, t_type* ttype, std::string prefix = "");
  128. void generate_deserialize_set_element(std::ostream& out, t_set* tset, std::string prefix = "");
  129. void generate_deserialize_map_element(std::ostream& out, t_map* tmap, std::string prefix = "");
  130. void generate_deserialize_list_element(std::ostream& out,
  131. t_list* tlist,
  132. std::string prefix = "");
  133. void generate_serialize_field(std::ostream& out, t_field* tfield, std::string prefix = "");
  134. void generate_serialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = "");
  135. void generate_serialize_container(std::ostream& out, t_type* ttype, std::string prefix = "");
  136. void generate_serialize_map_element(std::ostream& out,
  137. t_map* tmap,
  138. std::string iter,
  139. std::string map);
  140. void generate_serialize_set_element(std::ostream& out, t_set* tmap, std::string iter);
  141. void generate_serialize_list_element(std::ostream& out, t_list* tlist, std::string iter);
  142. void generate_as3_doc(std::ostream& out, t_doc* tdoc);
  143. void generate_as3_doc(std::ostream& out, t_function* tdoc);
  144. /**
  145. * Helper rendering functions
  146. */
  147. std::string as3_package();
  148. std::string as3_type_imports();
  149. std::string as3_thrift_imports();
  150. std::string as3_thrift_gen_imports(t_struct* tstruct, string& imports);
  151. std::string as3_thrift_gen_imports(t_service* tservice);
  152. std::string type_name(t_type* ttype, bool in_container = false, bool in_init = false);
  153. std::string base_type_name(t_base_type* tbase, bool in_container = false);
  154. std::string declare_field(t_field* tfield, bool init = false);
  155. std::string function_signature(t_function* tfunction, std::string prefix = "");
  156. std::string argument_list(t_struct* tstruct);
  157. std::string type_to_enum(t_type* ttype);
  158. std::string get_enum_class_name(t_type* type);
  159. bool type_can_be_null(t_type* ttype) {
  160. ttype = get_true_type(ttype);
  161. return ttype->is_container() || ttype->is_struct() || ttype->is_xception()
  162. || ttype->is_string();
  163. }
  164. std::string constant_name(std::string name);
  165. private:
  166. /**
  167. * File streams
  168. */
  169. std::string package_name_;
  170. ofstream_with_content_based_conditional_update f_service_;
  171. std::string package_dir_;
  172. bool bindable_;
  173. };
  174. /**
  175. * Prepares for file generation by opening up the necessary file output
  176. * streams.
  177. *
  178. * @param tprogram The program to generate
  179. */
  180. void t_as3_generator::init_generator() {
  181. // Make output directory
  182. MKDIR(get_out_dir().c_str());
  183. package_name_ = program_->get_namespace("as3");
  184. string dir = package_name_;
  185. string subdir = get_out_dir();
  186. string::size_type loc;
  187. while ((loc = dir.find(".")) != string::npos) {
  188. subdir = subdir + "/" + dir.substr(0, loc);
  189. MKDIR(subdir.c_str());
  190. dir = dir.substr(loc + 1);
  191. }
  192. if (dir.size() > 0) {
  193. subdir = subdir + "/" + dir;
  194. MKDIR(subdir.c_str());
  195. }
  196. package_dir_ = subdir;
  197. }
  198. /**
  199. * Packages the generated file
  200. *
  201. * @return String of the package, i.e. "package org.apache.thriftdemo;"
  202. */
  203. string t_as3_generator::as3_package() {
  204. if (!package_name_.empty()) {
  205. return string("package ") + package_name_ + " ";
  206. }
  207. return "package ";
  208. }
  209. /**
  210. * Prints standard as3 imports
  211. *
  212. * @return List of imports for As3 types that are used in here
  213. */
  214. string t_as3_generator::as3_type_imports() {
  215. return string() + "import org.apache.thrift.Set;\n" + "import flash.utils.ByteArray;\n"
  216. + "import flash.utils.Dictionary;\n\n";
  217. }
  218. /**
  219. * Prints standard as3 imports
  220. *
  221. * @return List of imports necessary for thrift
  222. */
  223. string t_as3_generator::as3_thrift_imports() {
  224. return string() + "import org.apache.thrift.*;\n" + "import org.apache.thrift.meta_data.*;\n"
  225. + "import org.apache.thrift.protocol.*;\n\n";
  226. }
  227. /**
  228. * Prints imports needed for a given type
  229. *
  230. * @return List of imports necessary for a given t_struct
  231. */
  232. string t_as3_generator::as3_thrift_gen_imports(t_struct* tstruct, string& imports) {
  233. const vector<t_field*>& members = tstruct->get_members();
  234. vector<t_field*>::const_iterator m_iter;
  235. // For each type check if it is from a differnet namespace
  236. for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
  237. t_program* program = (*m_iter)->get_type()->get_program();
  238. if (program != NULL && program != program_) {
  239. string package = program->get_namespace("as3");
  240. if (!package.empty()) {
  241. if (imports.find(package + "." + (*m_iter)->get_type()->get_name()) == string::npos) {
  242. imports.append("import " + package + "." + (*m_iter)->get_type()->get_name() + ";\n");
  243. }
  244. }
  245. }
  246. }
  247. return imports;
  248. }
  249. /**
  250. * Prints imports needed for a given type
  251. *
  252. * @return List of imports necessary for a given t_service
  253. */
  254. string t_as3_generator::as3_thrift_gen_imports(t_service* tservice) {
  255. string imports;
  256. const vector<t_function*>& functions = tservice->get_functions();
  257. vector<t_function*>::const_iterator f_iter;
  258. // For each type check if it is from a differnet namespace
  259. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  260. t_program* program = (*f_iter)->get_returntype()->get_program();
  261. if (program != NULL && program != program_) {
  262. string package = program->get_namespace("as3");
  263. if (!package.empty()) {
  264. if (imports.find(package + "." + (*f_iter)->get_returntype()->get_name()) == string::npos) {
  265. imports.append("import " + package + "." + (*f_iter)->get_returntype()->get_name()
  266. + ";\n");
  267. }
  268. }
  269. }
  270. as3_thrift_gen_imports((*f_iter)->get_arglist(), imports);
  271. as3_thrift_gen_imports((*f_iter)->get_xceptions(), imports);
  272. }
  273. return imports;
  274. }
  275. /**
  276. * Nothing in As3
  277. */
  278. void t_as3_generator::close_generator() {
  279. }
  280. /**
  281. * Generates a typedef. This is not done in As3, since it does
  282. * not support arbitrary name replacements, and it'd be a wacky waste
  283. * of overhead to make wrapper classes.
  284. *
  285. * @param ttypedef The type definition
  286. */
  287. void t_as3_generator::generate_typedef(t_typedef* ttypedef) {
  288. (void)ttypedef;
  289. }
  290. /**
  291. * Enums are a class with a set of static constants.
  292. *
  293. * @param tenum The enumeration
  294. */
  295. void t_as3_generator::generate_enum(t_enum* tenum) {
  296. // Make output file
  297. string f_enum_name = package_dir_ + "/" + (tenum->get_name()) + ".as";
  298. ofstream_with_content_based_conditional_update f_enum;
  299. f_enum.open(f_enum_name);
  300. // Comment and package it
  301. f_enum << autogen_comment() << as3_package() << endl;
  302. scope_up(f_enum);
  303. // Add as3 imports
  304. f_enum << string() + "import org.apache.thrift.Set;" << endl << "import flash.utils.Dictionary;"
  305. << endl;
  306. indent(f_enum) << "public class " << tenum->get_name() << " ";
  307. scope_up(f_enum);
  308. vector<t_enum_value*> constants = tenum->get_constants();
  309. vector<t_enum_value*>::iterator c_iter;
  310. for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
  311. int value = (*c_iter)->get_value();
  312. indent(f_enum) << "public static const " << (*c_iter)->get_name() << ":int = " << value << ";"
  313. << endl;
  314. }
  315. // Create a static Set with all valid values for this enum
  316. f_enum << endl;
  317. indent(f_enum) << "public static const VALID_VALUES:Set = new Set(";
  318. indent_up();
  319. bool firstValue = true;
  320. for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
  321. // populate set
  322. f_enum << (firstValue ? "" : ", ") << (*c_iter)->get_name();
  323. firstValue = false;
  324. }
  325. indent_down();
  326. f_enum << ");" << endl;
  327. indent(f_enum) << "public static const VALUES_TO_NAMES:Dictionary = new Dictionary();" << endl;
  328. scope_up(f_enum);
  329. for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
  330. indent(f_enum) << "VALUES_TO_NAMES[" << (*c_iter)->get_name() << "] = \""
  331. << (*c_iter)->get_name() << "\";" << endl;
  332. }
  333. f_enum << endl;
  334. scope_down(f_enum);
  335. scope_down(f_enum); // end class
  336. scope_down(f_enum); // end package
  337. f_enum.close();
  338. }
  339. /**
  340. * Generates a class that holds all the constants.
  341. */
  342. void t_as3_generator::generate_consts(std::vector<t_const*> consts) {
  343. if (consts.empty()) {
  344. return;
  345. }
  346. string f_consts_name = package_dir_ + "/" + program_name_ + "Constants.as";
  347. ofstream_with_content_based_conditional_update f_consts;
  348. f_consts.open(f_consts_name);
  349. // Print header
  350. f_consts << autogen_comment() << as3_package();
  351. scope_up(f_consts);
  352. f_consts << endl;
  353. f_consts << as3_type_imports();
  354. indent(f_consts) << "public class " << program_name_ << "Constants {" << endl << endl;
  355. indent_up();
  356. vector<t_const*>::iterator c_iter;
  357. for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
  358. print_const_value(f_consts,
  359. (*c_iter)->get_name(),
  360. (*c_iter)->get_type(),
  361. (*c_iter)->get_value(),
  362. false);
  363. }
  364. indent_down();
  365. indent(f_consts) << "}" << endl;
  366. scope_down(f_consts);
  367. f_consts.close();
  368. }
  369. void t_as3_generator::print_const_value(std::ostream& out,
  370. string name,
  371. t_type* type,
  372. t_const_value* value,
  373. bool in_static,
  374. bool defval) {
  375. type = get_true_type(type);
  376. indent(out);
  377. if (!defval) {
  378. out << (in_static ? "var " : "public static const ");
  379. }
  380. if (type->is_base_type()) {
  381. string v2 = render_const_value(out, name, type, value);
  382. out << name;
  383. if (!defval) {
  384. out << ":" << type_name(type);
  385. }
  386. out << " = " << v2 << ";" << endl << endl;
  387. } else if (type->is_enum()) {
  388. out << name;
  389. if (!defval) {
  390. out << ":" << type_name(type);
  391. }
  392. out << " = " << value->get_integer() << ";" << endl << endl;
  393. } else if (type->is_struct() || type->is_xception()) {
  394. const vector<t_field*>& fields = ((t_struct*)type)->get_members();
  395. vector<t_field*>::const_iterator f_iter;
  396. const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
  397. map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
  398. out << name << ":" << type_name(type) << " = new " << type_name(type, false, true) << "();"
  399. << endl;
  400. if (!in_static) {
  401. indent(out) << "{" << endl;
  402. indent_up();
  403. indent(out) << "new function():void {" << endl;
  404. indent_up();
  405. }
  406. for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
  407. t_type* field_type = NULL;
  408. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  409. if ((*f_iter)->get_name() == v_iter->first->get_string()) {
  410. field_type = (*f_iter)->get_type();
  411. }
  412. }
  413. if (field_type == NULL) {
  414. throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
  415. }
  416. string val = render_const_value(out, name, field_type, v_iter->second);
  417. indent(out) << name << ".";
  418. out << v_iter->first->get_string() << " = " << val << ";" << endl;
  419. }
  420. if (!in_static) {
  421. indent_down();
  422. indent(out) << "}();" << endl;
  423. indent_down();
  424. indent(out) << "}" << endl;
  425. }
  426. out << endl;
  427. } else if (type->is_map()) {
  428. out << name;
  429. if (!defval) {
  430. out << ":" << type_name(type);
  431. }
  432. out << " = new " << type_name(type, false, true) << "();" << endl;
  433. if (!in_static) {
  434. indent(out) << "{" << endl;
  435. indent_up();
  436. indent(out) << "new function():void {" << endl;
  437. indent_up();
  438. }
  439. t_type* ktype = ((t_map*)type)->get_key_type();
  440. t_type* vtype = ((t_map*)type)->get_val_type();
  441. const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
  442. map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
  443. for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
  444. string key = render_const_value(out, name, ktype, v_iter->first);
  445. string val = render_const_value(out, name, vtype, v_iter->second);
  446. indent(out) << name << "[" << key << "] = " << val << ";" << endl;
  447. }
  448. if (!in_static) {
  449. indent_down();
  450. indent(out) << "}();" << endl;
  451. indent_down();
  452. indent(out) << "}" << endl;
  453. }
  454. out << endl;
  455. } else if (type->is_list() || type->is_set()) {
  456. out << name;
  457. if (!defval) {
  458. out << ":" << type_name(type);
  459. }
  460. out << " = new " << type_name(type, false, true) << "();" << endl;
  461. if (!in_static) {
  462. indent(out) << "{" << endl;
  463. indent_up();
  464. indent(out) << "new function():void {" << endl;
  465. indent_up();
  466. }
  467. t_type* etype;
  468. if (type->is_list()) {
  469. etype = ((t_list*)type)->get_elem_type();
  470. } else {
  471. etype = ((t_set*)type)->get_elem_type();
  472. }
  473. const vector<t_const_value*>& val = value->get_list();
  474. vector<t_const_value*>::const_iterator v_iter;
  475. for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
  476. string val = render_const_value(out, name, etype, *v_iter);
  477. indent(out) << name << "." << (type->is_list() ? "push" : "add") << "(" << val << ");"
  478. << endl;
  479. }
  480. if (!in_static) {
  481. indent_down();
  482. indent(out) << "}();" << endl;
  483. indent_down();
  484. indent(out) << "}" << endl;
  485. }
  486. out << endl;
  487. } else {
  488. throw "compiler error: no const of type " + type->get_name();
  489. }
  490. }
  491. string t_as3_generator::render_const_value(ostream& out,
  492. string name,
  493. t_type* type,
  494. t_const_value* value) {
  495. (void)name;
  496. type = get_true_type(type);
  497. std::ostringstream render;
  498. if (type->is_base_type()) {
  499. t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
  500. switch (tbase) {
  501. case t_base_type::TYPE_STRING:
  502. render << '"' << get_escaped_string(value) << '"';
  503. break;
  504. case t_base_type::TYPE_BOOL:
  505. render << ((value->get_integer() > 0) ? "true" : "false");
  506. break;
  507. case t_base_type::TYPE_I8:
  508. render << "(byte)" << value->get_integer();
  509. break;
  510. case t_base_type::TYPE_I16:
  511. render << "(short)" << value->get_integer();
  512. break;
  513. case t_base_type::TYPE_I32:
  514. render << value->get_integer();
  515. break;
  516. case t_base_type::TYPE_I64:
  517. render << value->get_integer() << "L";
  518. break;
  519. case t_base_type::TYPE_DOUBLE:
  520. if (value->get_type() == t_const_value::CV_INTEGER) {
  521. render << "(double)" << value->get_integer();
  522. } else {
  523. render << value->get_double();
  524. }
  525. break;
  526. default:
  527. throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
  528. }
  529. } else if (type->is_enum()) {
  530. render << value->get_integer();
  531. } else {
  532. string t = tmp("tmp");
  533. print_const_value(out, t, type, value, true);
  534. render << t;
  535. }
  536. return render.str();
  537. }
  538. /**
  539. * Generates a struct definition for a thrift data type. This is a class
  540. * with data members, read(), write(), and an inner Isset class.
  541. *
  542. * @param tstruct The struct definition
  543. */
  544. void t_as3_generator::generate_struct(t_struct* tstruct) {
  545. generate_as3_struct(tstruct, false);
  546. }
  547. /**
  548. * Exceptions are structs, but they inherit from Exception
  549. *
  550. * @param tstruct The struct definition
  551. */
  552. void t_as3_generator::generate_xception(t_struct* txception) {
  553. generate_as3_struct(txception, true);
  554. }
  555. /**
  556. * As3 struct definition.
  557. *
  558. * @param tstruct The struct definition
  559. */
  560. void t_as3_generator::generate_as3_struct(t_struct* tstruct, bool is_exception) {
  561. // Make output file
  562. string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".as";
  563. ofstream_with_content_based_conditional_update f_struct;
  564. f_struct.open(f_struct_name.c_str());
  565. f_struct << autogen_comment() << as3_package();
  566. scope_up(f_struct);
  567. f_struct << endl;
  568. string imports;
  569. f_struct << as3_type_imports() << as3_thrift_imports() << as3_thrift_gen_imports(tstruct, imports)
  570. << endl;
  571. if (bindable_ && !is_exception) {
  572. f_struct << "import flash.events.Event;" << endl << "import flash.events.EventDispatcher;"
  573. << endl << "import mx.events.PropertyChangeEvent;" << endl;
  574. }
  575. generate_as3_struct_definition(f_struct, tstruct, is_exception);
  576. scope_down(f_struct); // end of package
  577. f_struct.close();
  578. }
  579. /**
  580. * As3 struct definition. This has various parameters, as it could be
  581. * generated standalone or inside another class as a helper. If it
  582. * is a helper than it is a static class.
  583. *
  584. * @param tstruct The struct definition
  585. * @param is_exception Is this an exception?
  586. * @param in_class If inside a class, needs to be static class
  587. * @param is_result If this is a result it needs a different writer
  588. */
  589. void t_as3_generator::generate_as3_struct_definition(ostream& out,
  590. t_struct* tstruct,
  591. bool is_exception,
  592. bool in_class,
  593. bool is_result) {
  594. generate_as3_doc(out, tstruct);
  595. bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
  596. bool bindable = !is_exception && !in_class && bindable_;
  597. indent(out) << (in_class ? "" : "public ") << (is_final ? "final " : "") << "class "
  598. << tstruct->get_name() << " ";
  599. if (is_exception) {
  600. out << "extends Error ";
  601. } else if (bindable) {
  602. out << "extends EventDispatcher ";
  603. }
  604. out << "implements TBase ";
  605. scope_up(out);
  606. indent(out) << "private static const STRUCT_DESC:TStruct = new TStruct(\"" << tstruct->get_name()
  607. << "\");" << endl;
  608. // Members are public for -as3, private for -as3bean
  609. const vector<t_field*>& members = tstruct->get_members();
  610. vector<t_field*>::const_iterator m_iter;
  611. for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
  612. indent(out) << "private static const " << constant_name((*m_iter)->get_name())
  613. << "_FIELD_DESC:TField = new TField(\"" << (*m_iter)->get_name() << "\", "
  614. << type_to_enum((*m_iter)->get_type()) << ", " << (*m_iter)->get_key() << ");"
  615. << endl;
  616. }
  617. out << endl;
  618. for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
  619. generate_as3_doc(out, *m_iter);
  620. indent(out) << "private var _" << (*m_iter)->get_name() + ":" + type_name((*m_iter)->get_type())
  621. << ";" << endl;
  622. indent(out) << "public static const " << upcase_string((*m_iter)->get_name())
  623. << ":int = " << (*m_iter)->get_key() << ";" << endl;
  624. }
  625. out << endl;
  626. // Inner Isset class
  627. if (members.size() > 0) {
  628. for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
  629. if (!type_can_be_null((*m_iter)->get_type())) {
  630. indent(out) << "private var __isset_" << (*m_iter)->get_name() << ":Boolean = false;"
  631. << endl;
  632. }
  633. }
  634. }
  635. out << endl;
  636. generate_as3_meta_data_map(out, tstruct);
  637. // Static initializer to populate global class to struct metadata map
  638. indent(out) << "{" << endl;
  639. indent_up();
  640. indent(out) << "FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ", metaDataMap);"
  641. << endl;
  642. indent_down();
  643. indent(out) << "}" << endl << endl;
  644. // Default constructor
  645. indent(out) << "public function " << tstruct->get_name() << "() {" << endl;
  646. indent_up();
  647. for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
  648. if ((*m_iter)->get_value() != NULL) {
  649. indent(out) << "this._" << (*m_iter)->get_name() << " = "
  650. << (*m_iter)->get_value()->get_integer() << ";" << endl;
  651. }
  652. }
  653. indent_down();
  654. indent(out) << "}" << endl << endl;
  655. generate_as3_bean_boilerplate(out, tstruct, bindable);
  656. generate_generic_field_getters_setters(out, tstruct);
  657. generate_generic_isset_method(out, tstruct);
  658. generate_as3_struct_reader(out, tstruct);
  659. if (is_result) {
  660. generate_as3_struct_result_writer(out, tstruct);
  661. } else {
  662. generate_as3_struct_writer(out, tstruct);
  663. }
  664. generate_as3_struct_tostring(out, tstruct, bindable);
  665. generate_as3_validator(out, tstruct);
  666. scope_down(out);
  667. out << endl;
  668. }
  669. /**
  670. * Generates a function to read all the fields of the struct.
  671. *
  672. * @param tstruct The struct definition
  673. */
  674. void t_as3_generator::generate_as3_struct_reader(ostream& out, t_struct* tstruct) {
  675. out << indent() << "public function read(iprot:TProtocol):void {" << endl;
  676. indent_up();
  677. const vector<t_field*>& fields = tstruct->get_members();
  678. vector<t_field*>::const_iterator f_iter;
  679. // Declare stack tmp variables and read struct header
  680. out << indent() << "var field:TField;" << endl << indent() << "iprot.readStructBegin();" << endl;
  681. // Loop over reading in fields
  682. indent(out) << "while (true)" << endl;
  683. scope_up(out);
  684. // Read beginning field marker
  685. indent(out) << "field = iprot.readFieldBegin();" << endl;
  686. // Check for field STOP marker and break
  687. indent(out) << "if (field.type == TType.STOP) { " << endl;
  688. indent_up();
  689. indent(out) << "break;" << endl;
  690. indent_down();
  691. indent(out) << "}" << endl;
  692. // Switch statement on the field we are reading
  693. indent(out) << "switch (field.id)" << endl;
  694. scope_up(out);
  695. // Generate deserialization code for known cases
  696. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  697. indent(out) << "case " << upcase_string((*f_iter)->get_name()) << ":" << endl;
  698. indent_up();
  699. indent(out) << "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
  700. indent_up();
  701. generate_deserialize_field(out, *f_iter, "this.");
  702. generate_isset_set(out, *f_iter);
  703. indent_down();
  704. out << indent() << "} else { " << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
  705. << endl << indent() << "}" << endl << indent() << "break;" << endl;
  706. indent_down();
  707. }
  708. // In the default case we skip the field
  709. out << indent() << "default:" << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
  710. << endl << indent() << " break;" << endl;
  711. scope_down(out);
  712. // Read field end marker
  713. indent(out) << "iprot.readFieldEnd();" << endl;
  714. scope_down(out);
  715. out << indent() << "iprot.readStructEnd();" << endl << endl;
  716. // in non-beans style, check for required fields of primitive type
  717. // (which can be checked here but not in the general validate method)
  718. out << endl << indent() << "// check for required fields of primitive type, which can't be "
  719. "checked in the validate method" << endl;
  720. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  721. if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) {
  722. out << indent() << "if (!__isset_" << (*f_iter)->get_name() << ") {" << endl << indent()
  723. << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '"
  724. << (*f_iter)->get_name()
  725. << "' was not found in serialized data! Struct: \" + toString());" << endl << indent()
  726. << "}" << endl;
  727. }
  728. }
  729. // performs various checks (e.g. check that all required fields are set)
  730. indent(out) << "validate();" << endl;
  731. indent_down();
  732. out << indent() << "}" << endl << endl;
  733. }
  734. // generates as3 method to perform various checks
  735. // (e.g. check that all required fields are set)
  736. void t_as3_generator::generate_as3_validator(ostream& out, t_struct* tstruct) {
  737. indent(out) << "public function validate():void {" << endl;
  738. indent_up();
  739. const vector<t_field*>& fields = tstruct->get_members();
  740. vector<t_field*>::const_iterator f_iter;
  741. out << indent() << "// check for required fields" << endl;
  742. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  743. if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
  744. if (type_can_be_null((*f_iter)->get_type())) {
  745. indent(out) << "if (" << (*f_iter)->get_name() << " == null) {" << endl;
  746. indent(out) << " throw new TProtocolError(TProtocolError.UNKNOWN, \"Required field '"
  747. << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());"
  748. << endl;
  749. indent(out) << "}" << endl;
  750. } else {
  751. indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name()
  752. << "' because it's a primitive and you chose the non-beans generator." << endl;
  753. }
  754. }
  755. }
  756. // check that fields of type enum have valid values
  757. out << indent() << "// check that fields of type enum have valid values" << endl;
  758. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  759. t_field* field = (*f_iter);
  760. t_type* type = field->get_type();
  761. // if field is an enum, check that its value is valid
  762. if (type->is_enum()) {
  763. indent(out) << "if (" << generate_isset_check(field) << " && !" << get_enum_class_name(type)
  764. << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
  765. indent_up();
  766. indent(out) << "throw new TProtocolError(TProtocolError.UNKNOWN, \"The field '"
  767. << field->get_name() << "' has been assigned the invalid value \" + "
  768. << field->get_name() << ");" << endl;
  769. indent_down();
  770. indent(out) << "}" << endl;
  771. }
  772. }
  773. indent_down();
  774. indent(out) << "}" << endl << endl;
  775. }
  776. /**
  777. * Generates a function to write all the fields of the struct
  778. *
  779. * @param tstruct The struct definition
  780. */
  781. void t_as3_generator::generate_as3_struct_writer(ostream& out, t_struct* tstruct) {
  782. out << indent() << "public function write(oprot:TProtocol):void {" << endl;
  783. indent_up();
  784. string name = tstruct->get_name();
  785. const vector<t_field*>& fields = tstruct->get_sorted_members();
  786. vector<t_field*>::const_iterator f_iter;
  787. // performs various checks (e.g. check that all required fields are set)
  788. indent(out) << "validate();" << endl << endl;
  789. indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
  790. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  791. bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
  792. if (could_be_unset) {
  793. indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
  794. indent_up();
  795. }
  796. bool null_allowed = type_can_be_null((*f_iter)->get_type());
  797. if (null_allowed) {
  798. out << indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
  799. indent_up();
  800. }
  801. indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
  802. << "_FIELD_DESC);" << endl;
  803. // Write field contents
  804. generate_serialize_field(out, *f_iter, "this.");
  805. // Write field closer
  806. indent(out) << "oprot.writeFieldEnd();" << endl;
  807. if (null_allowed) {
  808. indent_down();
  809. indent(out) << "}" << endl;
  810. }
  811. if (could_be_unset) {
  812. indent_down();
  813. indent(out) << "}" << endl;
  814. }
  815. }
  816. // Write the struct map
  817. out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
  818. << endl;
  819. indent_down();
  820. out << indent() << "}" << endl << endl;
  821. }
  822. /**
  823. * Generates a function to write all the fields of the struct,
  824. * which is a function result. These fields are only written
  825. * if they are set in the Isset array, and only one of them
  826. * can be set at a time.
  827. *
  828. * @param tstruct The struct definition
  829. */
  830. void t_as3_generator::generate_as3_struct_result_writer(ostream& out, t_struct* tstruct) {
  831. out << indent() << "public function write(oprot:TProtocol):void {" << endl;
  832. indent_up();
  833. string name = tstruct->get_name();
  834. const vector<t_field*>& fields = tstruct->get_sorted_members();
  835. vector<t_field*>::const_iterator f_iter;
  836. indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
  837. bool first = true;
  838. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  839. if (first) {
  840. first = false;
  841. out << endl << indent() << "if ";
  842. } else {
  843. out << " else if ";
  844. }
  845. out << "(this." << generate_isset_check(*f_iter) << ") {" << endl;
  846. indent_up();
  847. indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
  848. << "_FIELD_DESC);" << endl;
  849. // Write field contents
  850. generate_serialize_field(out, *f_iter, "this.");
  851. // Write field closer
  852. indent(out) << "oprot.writeFieldEnd();" << endl;
  853. indent_down();
  854. indent(out) << "}";
  855. }
  856. // Write the struct map
  857. out << endl << indent() << "oprot.writeFieldStop();" << endl << indent()
  858. << "oprot.writeStructEnd();" << endl;
  859. indent_down();
  860. out << indent() << "}" << endl << endl;
  861. }
  862. void t_as3_generator::generate_reflection_getters(ostringstream& out,
  863. t_type* type,
  864. string field_name,
  865. string cap_name) {
  866. (void)type;
  867. (void)cap_name;
  868. indent(out) << "case " << upcase_string(field_name) << ":" << endl;
  869. indent_up();
  870. indent(out) << "return this." << field_name << ";" << endl;
  871. indent_down();
  872. }
  873. void t_as3_generator::generate_reflection_setters(ostringstream& out,
  874. t_type* type,
  875. string field_name,
  876. string cap_name) {
  877. (void)type;
  878. (void)cap_name;
  879. indent(out) << "case " << upcase_string(field_name) << ":" << endl;
  880. indent_up();
  881. indent(out) << "if (value == null) {" << endl;
  882. indent(out) << " unset" << get_cap_name(field_name) << "();" << endl;
  883. indent(out) << "} else {" << endl;
  884. indent(out) << " this." << field_name << " = value;" << endl;
  885. indent(out) << "}" << endl;
  886. indent(out) << "break;" << endl << endl;
  887. indent_down();
  888. }
  889. void t_as3_generator::generate_generic_field_getters_setters(std::ostream& out,
  890. t_struct* tstruct) {
  891. std::ostringstream getter_stream;
  892. std::ostringstream setter_stream;
  893. // build up the bodies of both the getter and setter at once
  894. const vector<t_field*>& fields = tstruct->get_members();
  895. vector<t_field*>::const_iterator f_iter;
  896. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  897. t_field* field = *f_iter;
  898. t_type* type = get_true_type(field->get_type());
  899. std::string field_name = field->get_name();
  900. std::string cap_name = get_cap_name(field_name);
  901. indent_up();
  902. generate_reflection_setters(setter_stream, type, field_name, cap_name);
  903. generate_reflection_getters(getter_stream, type, field_name, cap_name);
  904. indent_down();
  905. }
  906. // create the setter
  907. indent(out) << "public function setFieldValue(fieldID:int, value:*):void {" << endl;
  908. indent_up();
  909. indent(out) << "switch (fieldID) {" << endl;
  910. out << setter_stream.str();
  911. indent(out) << "default:" << endl;
  912. indent(out) << " throw new ArgumentError(\"Field \" + fieldID + \" doesn't exist!\");" << endl;
  913. indent(out) << "}" << endl;
  914. indent_down();
  915. indent(out) << "}" << endl << endl;
  916. // create the getter
  917. indent(out) << "public function getFieldValue(fieldID:int):* {" << endl;
  918. indent_up();
  919. indent(out) << "switch (fieldID) {" << endl;
  920. out << getter_stream.str();
  921. indent(out) << "default:" << endl;
  922. indent(out) << " throw new ArgumentError(\"Field \" + fieldID + \" doesn't exist!\");" << endl;
  923. indent(out) << "}" << endl;
  924. indent_down();
  925. indent(out) << "}" << endl << endl;
  926. }
  927. // Creates a generic isSet method that takes the field number as argument
  928. void t_as3_generator::generate_generic_isset_method(std::ostream& out, t_struct* tstruct) {
  929. const vector<t_field*>& fields = tstruct->get_members();
  930. vector<t_field*>::const_iterator f_iter;
  931. // create the isSet method
  932. indent(out) << "// Returns true if field corresponding to fieldID is set (has been assigned a "
  933. "value) and false otherwise" << endl;
  934. indent(out) << "public function isSet(fieldID:int):Boolean {" << endl;
  935. indent_up();
  936. indent(out) << "switch (fieldID) {" << endl;
  937. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  938. t_field* field = *f_iter;
  939. indent(out) << "case " << upcase_string(field->get_name()) << ":" << endl;
  940. indent_up();
  941. indent(out) << "return " << generate_isset_check(field) << ";" << endl;
  942. indent_down();
  943. }
  944. indent(out) << "default:" << endl;
  945. indent(out) << " throw new ArgumentError(\"Field \" + fieldID + \" doesn't exist!\");" << endl;
  946. indent(out) << "}" << endl;
  947. indent_down();
  948. indent(out) << "}" << endl << endl;
  949. }
  950. /**
  951. * Generates a set of As3 Bean boilerplate functions (setters, getters, etc.)
  952. * for the given struct.
  953. *
  954. * @param tstruct The struct definition
  955. */
  956. void t_as3_generator::generate_as3_bean_boilerplate(ostream& out,
  957. t_struct* tstruct,
  958. bool bindable) {
  959. const vector<t_field*>& fields = tstruct->get_members();
  960. vector<t_field*>::const_iterator f_iter;
  961. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  962. t_field* field = *f_iter;
  963. t_type* type = get_true_type(field->get_type());
  964. std::string field_name = field->get_name();
  965. std::string cap_name = get_cap_name(field_name);
  966. // Simple getter
  967. generate_as3_doc(out, field);
  968. indent(out) << "public function get " << field_name << "():" << type_name(type) << " {" << endl;
  969. indent_up();
  970. indent(out) << "return this._" << field_name << ";" << endl;
  971. indent_down();
  972. indent(out) << "}" << endl << endl;
  973. // Simple setter
  974. generate_as3_doc(out, field);
  975. std::string propName = tmp("thriftPropertyChange");
  976. if (bindable) {
  977. indent(out) << "[Bindable(event=\"" << propName << "\")]" << endl;
  978. }
  979. indent(out) << "public function set " << field_name << "(" << field_name << ":"
  980. << type_name(type) << "):void {" << endl;
  981. indent_up();
  982. indent(out) << "this._" << field_name << " = " << field_name << ";" << endl;
  983. generate_isset_set(out, field);
  984. if (bindable) {
  985. // We have to use a custom event rather than the default, because if you use the default,
  986. // the setter only gets called if the value has changed - this means calling
  987. // foo.setIntValue(0)
  988. // will not cause foo.isIntValueSet() to return true since the value of foo._intValue wasn't
  989. // changed
  990. // so the setter was never called.
  991. indent(out) << "dispatchEvent(new Event(\"" << propName << "\"));" << endl;
  992. // However, if you just use a custom event, then collections won't be able to detect when
  993. // elements
  994. // in the collections have changed since they listed for PropertyChangeEvents. So, we
  995. // dispatch both.
  996. indent(out) << "dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE));"
  997. << endl;
  998. }
  999. indent_down();
  1000. indent(out) << "}" << endl << endl;
  1001. // Unsetter
  1002. indent(out) << "public function unset" << cap_name << "():void {" << endl;
  1003. indent_up();
  1004. if (type_can_be_null(type)) {
  1005. indent(out) << "this." << field_name << " = null;" << endl;
  1006. } else {
  1007. indent(out) << "this.__isset_" << field_name << " = false;" << endl;
  1008. }
  1009. indent_down();
  1010. indent(out) << "}" << endl << endl;
  1011. // isSet method
  1012. indent(out) << "// Returns true if field " << field_name
  1013. << " is set (has been assigned a value) and false otherwise" << endl;
  1014. indent(out) << "public function is" << get_cap_name("set") << cap_name << "():Boolean {"
  1015. << endl;
  1016. indent_up();
  1017. if (type_can_be_null(type)) {
  1018. indent(out) << "return this." << field_name << " != null;" << endl;
  1019. } else {
  1020. indent(out) << "return this.__isset_" << field_name << ";" << endl;
  1021. }
  1022. indent_down();
  1023. indent(out) << "}" << endl << endl;
  1024. }
  1025. }
  1026. /**
  1027. * Generates a toString() method for the given struct
  1028. *
  1029. * @param tstruct The struct definition
  1030. */
  1031. void t_as3_generator::generate_as3_struct_tostring(ostream& out,
  1032. t_struct* tstruct,
  1033. bool bindable) {
  1034. // If it's bindable, it extends EventDispatcher so toString is an override.
  1035. out << indent() << "public " << (bindable ? "override " : "") << "function toString():String {"
  1036. << endl;
  1037. indent_up();
  1038. out << indent() << "var ret:String = new String(\"" << tstruct->get_name() << "(\");" << endl;
  1039. out << indent() << "var first:Boolean = true;" << endl << endl;
  1040. const vector<t_field*>& fields = tstruct->get_members();
  1041. vector<t_field*>::const_iterator f_iter;
  1042. bool first = true;
  1043. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  1044. bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
  1045. if (could_be_unset) {
  1046. indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
  1047. indent_up();
  1048. }
  1049. t_field* field = (*f_iter);
  1050. if (!first) {
  1051. indent(out) << "if (!first) ret += \", \";" << endl;
  1052. }
  1053. indent(out) << "ret += \"" << (*f_iter)->get_name() << ":\";" << endl;
  1054. bool can_be_null = type_can_be_null(field->get_type());
  1055. if (can_be_null) {
  1056. indent(out) << "if (this." << (*f_iter)->get_name() << " == null) {" << endl;
  1057. indent(out) << " ret += \"null\";" << endl;
  1058. indent(out) << "} else {" << endl;
  1059. indent_up();
  1060. }
  1061. if (field->get_type()->is_binary()) {
  1062. indent(out) << " ret += \"BINARY\";" << endl;
  1063. } else if (field->get_type()->is_enum()) {
  1064. indent(out) << "var " << field->get_name()
  1065. << "_name:String = " << get_enum_class_name(field->get_type())
  1066. << ".VALUES_TO_NAMES[this." << (*f_iter)->get_name() << "];" << endl;
  1067. indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
  1068. indent(out) << " ret += " << field->get_name() << "_name;" << endl;
  1069. indent(out) << " ret += \" (\";" << endl;
  1070. indent(out) << "}" << endl;
  1071. indent(out) << "ret += this." << field->get_name() << ";" << endl;
  1072. indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
  1073. indent(out) << " ret += \")\";" << endl;
  1074. indent(out) << "}" << endl;
  1075. } else {
  1076. indent(out) << "ret += this." << (*f_iter)->get_name() << ";" << endl;
  1077. }
  1078. if (can_be_null) {
  1079. indent_down();
  1080. indent(out) << "}" << endl;
  1081. }
  1082. indent(out) << "first = false;" << endl;
  1083. if (could_be_unset) {
  1084. indent_down();
  1085. indent(out) << "}" << endl;
  1086. }
  1087. first = false;
  1088. }
  1089. out << indent() << "ret += \")\";" << endl << indent() << "return ret;" << endl;
  1090. indent_down();
  1091. indent(out) << "}" << endl << endl;
  1092. }
  1093. /**
  1094. * Generates a static map with meta data to store information such as fieldID to
  1095. * fieldName mapping
  1096. *
  1097. * @param tstruct The struct definition
  1098. */
  1099. void t_as3_generator::generate_as3_meta_data_map(ostream& out, t_struct* tstruct) {
  1100. const vector<t_field*>& fields = tstruct->get_members();
  1101. vector<t_field*>::const_iterator f_iter;
  1102. // Static Map with fieldID -> FieldMetaData mappings
  1103. indent(out) << "public static const metaDataMap:Dictionary = new Dictionary();" << endl;
  1104. if (fields.size() > 0) {
  1105. // Populate map
  1106. scope_up(out);
  1107. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  1108. t_field* field = *f_iter;
  1109. std::string field_name = field->get_name();
  1110. indent(out) << "metaDataMap[" << upcase_string(field_name) << "] = new FieldMetaData(\""
  1111. << field_name << "\", ";
  1112. // Set field requirement type (required, optional, etc.)
  1113. if (field->get_req() == t_field::T_REQUIRED) {
  1114. out << "TFieldRequirementType.REQUIRED, ";
  1115. } else if (field->get_req() == t_field::T_OPTIONAL) {
  1116. out << "TFieldRequirementType.OPTIONAL, ";
  1117. } else {
  1118. out << "TFieldRequirementType.DEFAULT, ";
  1119. }
  1120. // Create value meta data
  1121. generate_field_value_meta_data(out, field->get_type());
  1122. out << ");" << endl;
  1123. }
  1124. scope_down(out);
  1125. }
  1126. }
  1127. /**
  1128. * Returns a string with the as3 representation of the given thrift type
  1129. * (e.g. for the type struct it returns "TType.STRUCT")
  1130. */
  1131. std::string t_as3_generator::get_as3_type_string(t_type* type) {
  1132. if (type->is_list()) {
  1133. return "TType.LIST";
  1134. } else if (type->is_map()) {
  1135. return "TType.MAP";
  1136. } else if (type->is_set()) {
  1137. return "TType.SET";
  1138. } else if (type->is_struct() || type->is_xception()) {
  1139. return "TType.STRUCT";
  1140. } else if (type->is_enum()) {
  1141. return "TType.I32";
  1142. } else if (type->is_typedef()) {
  1143. return get_as3_type_string(((t_typedef*)type)->get_type());
  1144. } else if (type->is_base_type()) {
  1145. switch (((t_base_type*)type)->get_base()) {
  1146. case t_base_type::TYPE_VOID:
  1147. return "TType.VOID";
  1148. break;
  1149. case t_base_type::TYPE_STRING:
  1150. return "TType.STRING";
  1151. break;
  1152. case t_base_type::TYPE_BOOL:
  1153. return "TType.BOOL";
  1154. break;
  1155. case t_base_type::TYPE_I8:
  1156. return "TType.BYTE";
  1157. break;
  1158. case t_base_type::TYPE_I16:
  1159. return "TType.I16";
  1160. break;
  1161. case t_base_type::TYPE_I32:
  1162. return "TType.I32";
  1163. break;
  1164. case t_base_type::TYPE_I64:
  1165. return "TType.I64";
  1166. break;
  1167. case t_base_type::TYPE_DOUBLE:
  1168. return "TType.DOUBLE";
  1169. break;
  1170. default:
  1171. throw std::runtime_error("Unknown thrift type \"" + type->get_name()
  1172. + "\" passed to t_as3_generator::get_as3_type_string!");
  1173. break; // This should never happen!
  1174. }
  1175. } else {
  1176. throw std::runtime_error(
  1177. "Unknown thrift type \"" + type->get_name()
  1178. + "\" passed to t_as3_generator::get_as3_type_string!"); // This should never happen!
  1179. }
  1180. }
  1181. void t_as3_generator::generate_field_value_meta_data(std::ostream& out, t_type* type) {
  1182. out << endl;
  1183. indent_up();
  1184. indent_up();
  1185. if (type->is_struct() || type->is_xception()) {
  1186. indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type);
  1187. } else if (type->is_container()) {
  1188. if (type->is_list()) {
  1189. indent(out) << "new ListMetaData(TType.LIST, ";
  1190. t_type* elem_type = ((t_list*)type)->get_elem_type();
  1191. generate_field_value_meta_data(out, elem_type);
  1192. } else if (type->is_set()) {
  1193. indent(out) << "new SetMetaData(TType.SET, ";
  1194. t_type* elem_type = ((t_list*)type)->get_elem_type();
  1195. generate_field_value_meta_data(out, elem_type);
  1196. } else { // map
  1197. indent(out) << "new MapMetaData(TType.MAP, ";
  1198. t_type* key_type = ((t_map*)type)->get_key_type();
  1199. t_type* val_type = ((t_map*)type)->get_val_type();
  1200. generate_field_value_meta_data(out, key_type);
  1201. out << ", ";
  1202. generate_field_value_meta_data(out, val_type);
  1203. }
  1204. } else {
  1205. indent(out) << "new FieldValueMetaData(" << get_as3_type_string(type);
  1206. }
  1207. out << ")";
  1208. indent_down();
  1209. indent_down();
  1210. }
  1211. /**
  1212. * Generates a thrift service. In C++, this comprises an entirely separate
  1213. * header and source file. The header file defines the methods and includes
  1214. * the data types defined in the main header file, and the implementation
  1215. * file contains implementations of the basic printer and default interfaces.
  1216. *
  1217. * @param tservice The service definition
  1218. */
  1219. void t_as3_generator::generate_service(t_service* tservice) {
  1220. // Make interface file
  1221. string f_service_name = package_dir_ + "/" + service_name_ + ".as";
  1222. f_service_.open(f_service_name.c_str());
  1223. f_service_ << autogen_comment() << as3_package();
  1224. scope_up(f_service_);
  1225. f_service_ << endl << as3_type_imports() << as3_thrift_imports()
  1226. << as3_thrift_gen_imports(tservice);
  1227. if (tservice->get_extends() != NULL) {
  1228. t_type* parent = tservice->get_extends();
  1229. string parent_namespace = parent->get_program()->get_namespace("as3");
  1230. if (!parent_namespace.empty() && parent_namespace != package_name_) {
  1231. f_service_ << "import " << type_name(parent) << ";" << endl;
  1232. }
  1233. }
  1234. f_service_ << endl;
  1235. generate_service_interface(tservice);
  1236. scope_down(f_service_);
  1237. f_service_.close();
  1238. // Now make the implementation/client file
  1239. f_service_name = package_dir_ + "/" + service_name_ + "Impl.as";
  1240. f_service_.open(f_service_name.c_str());
  1241. f_service_ << autogen_comment() << as3_package();
  1242. scope_up(f_service_);
  1243. f_service_ << endl << as3_type_imports() << as3_thrift_imports()
  1244. << as3_thrift_gen_imports(tservice);
  1245. if (tservice->get_extends() != NULL) {
  1246. t_type* parent = tservice->get_extends();
  1247. string parent_namespace = parent->get_program()->get_namespace("as3");
  1248. if (!parent_namespace.empty() && parent_namespace != package_name_) {
  1249. f_service_ << "import " << type_name(parent) << "Impl;" << endl;
  1250. }
  1251. }
  1252. f_service_ << endl;
  1253. generate_service_client(tservice);
  1254. scope_down(f_service_);
  1255. f_service_ << as3_type_imports();
  1256. f_service_ << as3_thrift_imports();
  1257. f_service_ << as3_thrift_gen_imports(tservice);
  1258. if (!package_name_.empty()) {
  1259. f_service_ << "import " << package_name_ << ".*;" << endl;
  1260. }
  1261. generate_service_helpers(tservice);
  1262. f_service_.close();
  1263. // Now make the processor/server file
  1264. f_service_name = package_dir_ + "/" + service_name_ + "Processor.as";
  1265. f_service_.open(f_service_name.c_str());
  1266. f_service_ << autogen_comment() << as3_package();
  1267. scope_up(f_service_);
  1268. f_service_ << endl << as3_type_imports() << as3_thrift_imports()
  1269. << as3_thrift_gen_imports(tservice) << endl;
  1270. generate_service_server(tservice);
  1271. scope_down(f_service_);
  1272. f_service_ << as3_type_imports();
  1273. f_service_ << as3_thrift_imports();
  1274. f_service_ << as3_thrift_gen_imports(tservice) << endl;
  1275. if (!package_name_.empty()) {
  1276. f_service_ << "import " << package_name_ << ".*;" << endl;
  1277. }
  1278. generate_service_helpers(tservice);
  1279. f_service_.close();
  1280. }
  1281. /**
  1282. * Generates a service interface definition.
  1283. *
  1284. * @param tservice The service to generate a header definition for
  1285. */
  1286. void t_as3_generator::generate_service_interface(t_service* tservice) {
  1287. string extends_iface = "";
  1288. if (tservice->get_extends() != NULL) {
  1289. extends_iface = " extends " + tservice->get_extends()->get_name();
  1290. }
  1291. generate_as3_doc(f_service_, tservice);
  1292. f_service_ << indent() << "public interface " << service_name_ << extends_iface << " {" << endl
  1293. << endl;
  1294. indent_up();
  1295. vector<t_function*> functions = tservice->get_functions();
  1296. vector<t_function*>::iterator f_iter;
  1297. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  1298. generate_as3_doc(f_service_, *f_iter);
  1299. if (!(*f_iter)->is_oneway()) {
  1300. if ((*f_iter)->get_returntype()->is_void()) {
  1301. indent(f_service_) << "//function onError(Error):void;" << endl;
  1302. indent(f_service_) << "//function onSuccess():void;" << endl;
  1303. } else {
  1304. indent(f_service_) << "//function onError(Error):void;" << endl;
  1305. indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype())
  1306. << "):void;" << endl;
  1307. }
  1308. }
  1309. indent(f_service_) << function_signature(*f_iter) << ";" << endl << endl;
  1310. }
  1311. indent_down();
  1312. f_service_ << indent() << "}" << endl << endl;
  1313. }
  1314. /**
  1315. * Generates structs for all the service args and return types
  1316. *
  1317. * @param tservice The service
  1318. */
  1319. void t_as3_generator::generate_service_helpers(t_service* tservice) {
  1320. vector<t_function*> functions = tservice->get_functions();
  1321. vector<t_function*>::iterator f_iter;
  1322. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  1323. t_struct* ts = (*f_iter)->get_arglist();
  1324. generate_as3_struct_definition(f_service_, ts, false, true);
  1325. generate_function_helpers(*f_iter);
  1326. }
  1327. }
  1328. /**
  1329. * Generates a service client definition.
  1330. *
  1331. * @param tservice The service to generate a server for.
  1332. */
  1333. void t_as3_generator::generate_service_client(t_service* tservice) {
  1334. string extends = "";
  1335. string extends_client = "";
  1336. if (tservice->get_extends() != NULL) {
  1337. extends = tservice->get_extends()->get_name();
  1338. extends_client = " extends " + extends + "Impl";
  1339. }
  1340. indent(f_service_) << "public class " << service_name_ << "Impl" << extends_client
  1341. << " implements " << service_name_ << " {" << endl;
  1342. indent_up();
  1343. indent(f_service_) << "public function " << service_name_ << "Impl"
  1344. << "(iprot:TProtocol, oprot:TProtocol=null)" << endl;
  1345. scope_up(f_service_);
  1346. if (extends.empty()) {
  1347. f_service_ << indent() << "iprot_ = iprot;" << endl;
  1348. f_service_ << indent() << "if (oprot == null) {" << endl;
  1349. indent_up();
  1350. f_service_ << indent() << "oprot_ = iprot;" << endl;
  1351. indent_down();
  1352. f_service_ << indent() << "} else {" << endl;
  1353. indent_up();
  1354. f_service_ << indent() << "oprot_ = oprot;" << endl;
  1355. indent_down();
  1356. f_service_ << indent() << "}";
  1357. } else {
  1358. f_service_ << indent() << "super(iprot, oprot);" << endl;
  1359. }
  1360. scope_down(f_service_);
  1361. f_service_ << endl;
  1362. if (extends.empty()) {
  1363. f_service_ << indent() << "protected var iprot_:TProtocol;" << endl << indent()
  1364. << "protected var oprot_:TProtocol;" << endl << endl << indent()
  1365. << "protected var seqid_:int;" << endl << endl;
  1366. indent(f_service_) << "public function getInputProtocol():TProtocol" << endl;
  1367. scope_up(f_service_);
  1368. indent(f_service_) << "return this.iprot_;" << endl;
  1369. scope_down(f_service_);
  1370. f_service_ << endl;
  1371. indent(f_service_) << "public function getOutputProtocol():TProtocol" << endl;
  1372. scope_up(f_service_);
  1373. indent(f_service_) << "return this.oprot_;" << endl;
  1374. scope_down(f_service_);
  1375. f_service_ << endl;
  1376. }
  1377. // Generate client method implementations
  1378. vector<t_function*> functions = tservice->get_functions();
  1379. vector<t_function*>::const_iterator f_iter;
  1380. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  1381. string funname = (*f_iter)->get_name();
  1382. // Open function
  1383. if (!(*f_iter)->is_oneway()) {
  1384. if ((*f_iter)->get_returntype()->is_void()) {
  1385. indent(f_service_) << "//function onError(Error):void;" << endl;
  1386. indent(f_service_) << "//function onSuccess():void;" << endl;
  1387. } else {
  1388. indent(f_service_) << "//function onError(Error):void;" << endl;
  1389. indent(f_service_) << "//function onSuccess(" << type_name((*f_iter)->get_returntype())
  1390. << "):void;" << endl;
  1391. }
  1392. }
  1393. indent(f_service_) << "public " << function_signature(*f_iter) << endl;
  1394. scope_up(f_service_);
  1395. // Get the struct of function call params
  1396. t_struct* arg_struct = (*f_iter)->get_arglist();
  1397. string argsname = (*f_iter)->get_name() + "_args";
  1398. vector<t_field*>::const_iterator fld_iter;
  1399. const vector<t_field*>& fields = arg_struct->get_members();
  1400. // Serialize the request
  1401. f_service_ << indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", "
  1402. << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
  1403. << ", seqid_));" << endl << indent() << "var args:" << argsname << " = new "
  1404. << argsname << "();" << endl;
  1405. for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
  1406. f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = "
  1407. << (*fld_iter)->get_name() << ";" << endl;
  1408. }
  1409. f_service_ << indent() << "args.write(oprot_);" << endl << indent()
  1410. << "oprot_.writeMessageEnd();" << endl;
  1411. if ((*f_iter)->is_oneway()) {
  1412. f_service_ << indent() << "oprot_.getTransport().flush();" << endl;
  1413. } else {
  1414. f_service_ << indent() << "oprot_.getTransport().flush(function(error:Error):void {" << endl;
  1415. indent_up();
  1416. f_service_ << indent() << "try {" << endl;
  1417. indent_up();
  1418. string resultname = (*f_iter)->get_name() + "_result";
  1419. f_service_ << indent() << "if (error != null) {" << endl << indent()
  1420. << " if (onError != null) onError(error);" << endl << indent() << " return;"
  1421. << endl << indent() << "}" << endl << indent()
  1422. << "var msg:TMessage = iprot_.readMessageBegin();" << endl << indent()
  1423. << "if (msg.type == TMessageType.EXCEPTION) {" << endl << indent()
  1424. << " var x:TApplicationError = TApplicationError.read(iprot_);" << endl
  1425. << indent() << " iprot_.readMessageEnd();" << endl << indent()
  1426. << " if (onError != null) onError(x);" << endl << indent() << " return;" << endl
  1427. << indent() << "}" << endl << indent() << "var result :" << resultname << " = new "
  1428. << resultname << "();" << endl << indent() << "result.read(iprot_);" << endl
  1429. << indent() << "iprot_.readMessageEnd();" << endl;
  1430. // Careful, only return _result if not a void function
  1431. if (!(*f_iter)->get_returntype()->is_void()) {
  1432. f_service_ << indent() << "if (result." << generate_isset_check("success") << ") {" << endl
  1433. << indent() << " if (onSuccess != null) onSuccess(result.success);" << endl
  1434. << indent() << " return;" << endl << indent() << "}" << endl;
  1435. }
  1436. t_struct* xs = (*f_iter)->get_xceptions();
  1437. const std::vector<t_field*>& xceptions = xs->get_members();
  1438. vector<t_field*>::const_iterator x_iter;
  1439. for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
  1440. f_service_ << indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl
  1441. << indent() << " if (onError != null) onError(result." << (*x_iter)->get_name()
  1442. << ");" << endl << indent() << " return;" << endl << indent() << "}" << endl;
  1443. }
  1444. // If you get here it's an exception, unless a void function
  1445. if ((*f_iter)->get_returntype()->is_void()) {
  1446. f_service_ << indent() << "if (onSuccess != null) onSuccess();" << endl << indent()
  1447. << "return;" << endl;
  1448. } else {
  1449. f_service_ << indent() << "if (onError != null) onError(new "
  1450. "TApplicationError(TApplicationError.MISSING_RESULT, \""
  1451. << (*f_iter)->get_name() << " failed: unknown result\"));" << endl;
  1452. }
  1453. indent_down();
  1454. f_service_ << indent() << "} catch (e:TError) {" << endl << indent()
  1455. << " if (onError != null) onError(e);" << endl << indent() << "}" << endl;
  1456. indent_down();
  1457. indent(f_service_) << "});" << endl;
  1458. }
  1459. // Close function
  1460. scope_down(f_service_);
  1461. f_service_ << endl;
  1462. }
  1463. indent_down();
  1464. indent(f_service_) << "}" << endl;
  1465. }
  1466. /**
  1467. * Generates a service server definition.
  1468. *
  1469. * @param tservice The service to generate a server for.
  1470. */
  1471. void t_as3_generator::generate_service_server(t_service* tservice) {
  1472. // Generate the dispatch methods
  1473. vector<t_function*> functions = tservice->get_functions();
  1474. vector<t_function*>::iterator f_iter;
  1475. // Extends stuff
  1476. string extends = "";
  1477. string extends_processor = "";
  1478. if (tservice->get_extends() != NULL) {
  1479. extends = type_name(tservice->get_extends());
  1480. extends_processor = " extends " + extends + "Processor";
  1481. }
  1482. // Generate the header portion
  1483. indent(f_service_) << "public class " << service_name_ << "Processor" << extends_processor
  1484. << " implements TProcessor {" << endl;
  1485. indent_up();
  1486. indent(f_service_) << "public function " << service_name_ << "Processor(iface:" << service_name_
  1487. << ")" << endl;
  1488. scope_up(f_service_);
  1489. if (!extends.empty()) {
  1490. f_service_ << indent() << "super(iface);" << endl;
  1491. }
  1492. f_service_ << indent() << "iface_ = iface;" << endl;
  1493. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  1494. f_service_ << indent() << "PROCESS_MAP[\"" << (*f_iter)->get_name()
  1495. << "\"] = " << (*f_iter)->get_name() << "();" << endl;
  1496. }
  1497. scope_down(f_service_);
  1498. f_service_ << endl;
  1499. f_service_ << indent() << "private var iface_:" << service_name_ << ";" << endl;
  1500. if (extends.empty()) {
  1501. f_service_ << indent() << "protected const PROCESS_MAP:Dictionary = new Dictionary();" << endl;
  1502. }
  1503. f_service_ << endl;
  1504. // Generate the server implementation
  1505. string override = "";
  1506. if (tservice->get_extends() != NULL) {
  1507. override = "override ";
  1508. }
  1509. indent(f_service_) << override
  1510. << "public function process(iprot:TProtocol, oprot:TProtocol):Boolean" << endl;
  1511. scope_up(f_service_);
  1512. f_service_ << indent() << "var msg:TMessage = iprot.readMessageBegin();" << endl;
  1513. // TODO(mcslee): validate message, was the seqid etc. legit?
  1514. // AS- If all method is oneway:
  1515. // do you have an oprot?
  1516. // do you you need nullcheck?
  1517. f_service_
  1518. << indent() << "var fn:Function = PROCESS_MAP[msg.name];" << endl << indent()
  1519. << "if (fn == null) {" << endl << indent() << " TProtocolUtil.skip(iprot, TType.STRUCT);"
  1520. << endl << indent() << " iprot.readMessageEnd();" << endl << indent()
  1521. << " var x:TApplicationError = new TApplicationError(TApplicationError.UNKNOWN_METHOD, "
  1522. "\"Invalid method name: '\"+msg.name+\"'\");" << endl << indent()
  1523. << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));"
  1524. << endl << indent() << " x.write(oprot);" << endl << indent() << " oprot.writeMessageEnd();"
  1525. << endl << indent() << " oprot.getTransport().flush();" << endl << indent()
  1526. << " return true;" << endl << indent() << "}" << endl << indent()
  1527. << "fn.call(this,msg.seqid, iprot, oprot);" << endl;
  1528. f_service_ << indent() << "return true;" << endl;
  1529. scope_down(f_service_);
  1530. f_service_ << endl;
  1531. // Generate the process subfunctions
  1532. for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
  1533. generate_process_function(tservice, *f_iter);
  1534. }
  1535. indent_down();
  1536. indent(f_service_) << "}" << endl << endl;
  1537. }
  1538. /**
  1539. * Generates a struct and helpers for a function.
  1540. *
  1541. * @param tfunction The function
  1542. */
  1543. void t_as3_generator::generate_function_helpers(t_function* tfunction) {
  1544. if (tfunction->is_oneway()) {
  1545. return;
  1546. }
  1547. t_struct result(program_, tfunction->get_name() + "_result");
  1548. t_field success(tfunction->get_returntype(), "success", 0);
  1549. if (!tfunction->get_returntype()->is_void()) {
  1550. result.append(&success);
  1551. }
  1552. t_struct* xs = tfunction->get_xceptions();
  1553. const vector<t_field*>& fields = xs->get_members();
  1554. vector<t_field*>::const_iterator f_iter;
  1555. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  1556. result.append(*f_iter);
  1557. }
  1558. generate_as3_struct_definition(f_service_, &result, false, true, true);
  1559. }
  1560. /**
  1561. * Generates a process function definition.
  1562. *
  1563. * @param tfunction The function to write a dispatcher for
  1564. */
  1565. void t_as3_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
  1566. (void)tservice;
  1567. // Open class
  1568. indent(f_service_) << "private function " << tfunction->get_name() << "():Function {" << endl;
  1569. indent_up();
  1570. // Open function
  1571. indent(f_service_) << "return function(seqid:int, iprot:TProtocol, oprot:TProtocol):void" << endl;
  1572. scope_up(f_service_);
  1573. string argsname = tfunction->get_name() + "_args";
  1574. string resultname = tfunction->get_name() + "_result";
  1575. f_service_ << indent() << "var args:" << argsname << " = new " << argsname << "();" << endl
  1576. << indent() << "args.read(iprot);" << endl << indent() << "iprot.readMessageEnd();"
  1577. << endl;
  1578. t_struct* xs = tfunction->get_xceptions();
  1579. const std::vector<t_field*>& xceptions = xs->get_members();
  1580. vector<t_field*>::const_iterator x_iter;
  1581. // Declare result for non oneway function
  1582. if (!tfunction->is_oneway()) {
  1583. f_service_ << indent() << "var result:" << resultname << " = new " << resultname << "();"
  1584. << endl;
  1585. }
  1586. // Try block for a function with exceptions
  1587. if (xceptions.size() > 0) {
  1588. f_service_ << indent() << "try {" << endl;
  1589. indent_up();
  1590. }
  1591. // Generate the function call
  1592. t_struct* arg_struct = tfunction->get_arglist();
  1593. const std::vector<t_field*>& fields = arg_struct->get_members();
  1594. vector<t_field*>::const_iterator f_iter;
  1595. f_service_ << indent();
  1596. if (tfunction->is_oneway()) {
  1597. f_service_ << "iface_." << tfunction->get_name() << "(";
  1598. bool first = true;
  1599. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  1600. if (first) {
  1601. first = false;
  1602. } else {
  1603. f_service_ << ", ";
  1604. }
  1605. f_service_ << "args." << (*f_iter)->get_name();
  1606. }
  1607. f_service_ << ");" << endl;
  1608. } else {
  1609. f_service_ << "// sorry this operation is not supported yet" << endl;
  1610. f_service_ << indent() << "throw new Error(\"This is not yet supported\");" << endl;
  1611. }
  1612. // Set isset on success field
  1613. if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()
  1614. && !type_can_be_null(tfunction->get_returntype())) {
  1615. f_service_ << indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet")
  1616. << "(true);" << endl;
  1617. }
  1618. if (!tfunction->is_oneway() && xceptions.size() > 0) {
  1619. indent_down();
  1620. f_service_ << indent() << "}";
  1621. for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
  1622. f_service_ << " catch (" << (*x_iter)->get_name() << ":"
  1623. << type_name((*x_iter)->get_type(), false, false) << ") {" << endl;
  1624. if (!tfunction->is_oneway()) {
  1625. indent_up();
  1626. f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
  1627. << (*x_iter)->get_name() << ";" << endl;
  1628. indent_down();
  1629. f_service_ << indent() << "}";
  1630. } else {
  1631. f_service_ << "}";
  1632. }
  1633. }
  1634. f_service_ << " catch (th:Error) {" << endl;
  1635. indent_up();
  1636. f_service_ << indent() << "trace(\"Internal error processing " << tfunction->get_name()
  1637. << "\", th);" << endl << indent()
  1638. << "var x:TApplicationError = new "
  1639. "TApplicationError(TApplicationError.INTERNAL_ERROR, \"Internal error processing "
  1640. << tfunction->get_name() << "\");" << endl << indent()
  1641. << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
  1642. << "\", TMessageType.EXCEPTION, seqid));" << endl << indent() << "x.write(oprot);"
  1643. << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
  1644. << "oprot.getTransport().flush();" << endl << indent() << "return;" << endl;
  1645. indent_down();
  1646. f_service_ << indent() << "}" << endl;
  1647. }
  1648. // Shortcut out here for oneway functions
  1649. if (tfunction->is_oneway()) {
  1650. f_service_ << indent() << "return;" << endl;
  1651. scope_down(f_service_);
  1652. // Close class
  1653. indent_down();
  1654. f_service_ << indent() << "}" << endl << endl;
  1655. return;
  1656. }
  1657. f_service_ << indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
  1658. << "\", TMessageType.REPLY, seqid));" << endl << indent() << "result.write(oprot);"
  1659. << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
  1660. << "oprot.getTransport().flush();" << endl;
  1661. // Close function
  1662. scope_down(f_service_);
  1663. f_service_ << endl;
  1664. // Close class
  1665. indent_down();
  1666. f_service_ << indent() << "}" << endl << endl;
  1667. }
  1668. /**
  1669. * Deserializes a field of any type.
  1670. *
  1671. * @param tfield The field
  1672. * @param prefix The variable name or container for this field
  1673. */
  1674. void t_as3_generator::generate_deserialize_field(ostream& out, t_field* tfield, string prefix) {
  1675. t_type* type = get_true_type(tfield->get_type());
  1676. if (type->is_void()) {
  1677. throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
  1678. }
  1679. string name = prefix + tfield->get_name();
  1680. if (type->is_struct() || type->is_xception()) {
  1681. generate_deserialize_struct(out, (t_struct*)type, name);
  1682. } else if (type->is_container()) {
  1683. generate_deserialize_container(out, type, name);
  1684. } else if (type->is_base_type() || type->is_enum()) {
  1685. indent(out) << name << " = iprot.";
  1686. if (type->is_base_type()) {
  1687. t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
  1688. switch (tbase) {
  1689. case t_base_type::TYPE_VOID:
  1690. throw "compiler error: cannot serialize void field in a struct: " + name;
  1691. break;
  1692. case t_base_type::TYPE_STRING:
  1693. if (type->is_binary()) {
  1694. out << "readBinary();";
  1695. } else {
  1696. out << "readString();";
  1697. }
  1698. break;
  1699. case t_base_type::TYPE_BOOL:
  1700. out << "readBool();";
  1701. break;
  1702. case t_base_type::TYPE_I8:
  1703. out << "readByte();";
  1704. break;
  1705. case t_base_type::TYPE_I16:
  1706. out << "readI16();";
  1707. break;
  1708. case t_base_type::TYPE_I32:
  1709. out << "readI32();";
  1710. break;
  1711. case t_base_type::TYPE_I64:
  1712. out << "readI64();";
  1713. break;
  1714. case t_base_type::TYPE_DOUBLE:
  1715. out << "readDouble();";
  1716. break;
  1717. default:
  1718. throw "compiler error: no As3 name for base type " + t_base_type::t_base_name(tbase);
  1719. }
  1720. } else if (type->is_enum()) {
  1721. out << "readI32();";
  1722. }
  1723. out << endl;
  1724. } else {
  1725. printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
  1726. tfield->get_name().c_str(),
  1727. type_name(type).c_str());
  1728. }
  1729. }
  1730. /**
  1731. * Generates an unserializer for a struct, invokes read()
  1732. */
  1733. void t_as3_generator::generate_deserialize_struct(ostream& out, t_struct* tstruct, string prefix) {
  1734. out << indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << indent()
  1735. << prefix << ".read(iprot);" << endl;
  1736. }
  1737. /**
  1738. * Deserializes a container by reading its size and then iterating
  1739. */
  1740. void t_as3_generator::generate_deserialize_container(ostream& out, t_type* ttype, string prefix) {
  1741. scope_up(out);
  1742. string obj;
  1743. if (ttype->is_map()) {
  1744. obj = tmp("_map");
  1745. } else if (ttype->is_set()) {
  1746. obj = tmp("_set");
  1747. } else if (ttype->is_list()) {
  1748. obj = tmp("_list");
  1749. }
  1750. // Declare variables, read header
  1751. if (ttype->is_map()) {
  1752. indent(out) << "var " << obj << ":TMap = iprot.readMapBegin();" << endl;
  1753. } else if (ttype->is_set()) {
  1754. indent(out) << "var " << obj << ":TSet = iprot.readSetBegin();" << endl;
  1755. } else if (ttype->is_list()) {
  1756. indent(out) << "var " << obj << ":TList = iprot.readListBegin();" << endl;
  1757. }
  1758. indent(out) << prefix << " = new " << type_name(ttype, false, true)
  1759. // size the collection correctly
  1760. << "("
  1761. << ");" << endl;
  1762. // For loop iterates over elements
  1763. string i = tmp("_i");
  1764. indent(out) << "for (var " << i << ":int = 0; " << i << " < " << obj << ".size"
  1765. << "; "
  1766. << "++" << i << ")" << endl;
  1767. scope_up(out);
  1768. if (ttype->is_map()) {
  1769. generate_deserialize_map_element(out, (t_map*)ttype, prefix);
  1770. } else if (ttype->is_set()) {
  1771. generate_deserialize_set_element(out, (t_set*)ttype, prefix);
  1772. } else if (ttype->is_list()) {
  1773. generate_deserialize_list_element(out, (t_list*)ttype, prefix);
  1774. }
  1775. scope_down(out);
  1776. // Read container end
  1777. if (ttype->is_map()) {
  1778. indent(out) << "iprot.readMapEnd();" << endl;
  1779. } else if (ttype->is_set()) {
  1780. indent(out) << "iprot.readSetEnd();" << endl;
  1781. } else if (ttype->is_list()) {
  1782. indent(out) << "iprot.readListEnd();" << endl;
  1783. }
  1784. scope_down(out);
  1785. }
  1786. /**
  1787. * Generates code to deserialize a map
  1788. */
  1789. void t_as3_generator::generate_deserialize_map_element(ostream& out, t_map* tmap, string prefix) {
  1790. string key = tmp("_key");
  1791. string val = tmp("_val");
  1792. t_field fkey(tmap->get_key_type(), key);
  1793. t_field fval(tmap->get_val_type(), val);
  1794. indent(out) << declare_field(&fkey) << endl;
  1795. indent(out) << declare_field(&fval) << endl;
  1796. generate_deserialize_field(out, &fkey);
  1797. generate_deserialize_field(out, &fval);
  1798. indent(out) << prefix << "[" << key << "] = " << val << ";" << endl;
  1799. }
  1800. /**
  1801. * Deserializes a set element
  1802. */
  1803. void t_as3_generator::generate_deserialize_set_element(ostream& out, t_set* tset, string prefix) {
  1804. string elem = tmp("_elem");
  1805. t_field felem(tset->get_elem_type(), elem);
  1806. indent(out) << declare_field(&felem) << endl;
  1807. generate_deserialize_field(out, &felem);
  1808. indent(out) << prefix << ".add(" << elem << ");" << endl;
  1809. }
  1810. /**
  1811. * Deserializes a list element
  1812. */
  1813. void t_as3_generator::generate_deserialize_list_element(ostream& out,
  1814. t_list* tlist,
  1815. string prefix) {
  1816. string elem = tmp("_elem");
  1817. t_field felem(tlist->get_elem_type(), elem);
  1818. indent(out) << declare_field(&felem) << endl;
  1819. generate_deserialize_field(out, &felem);
  1820. indent(out) << prefix << ".push(" << elem << ");" << endl;
  1821. }
  1822. /**
  1823. * Serializes a field of any type.
  1824. *
  1825. * @param tfield The field to serialize
  1826. * @param prefix Name to prepend to field name
  1827. */
  1828. void t_as3_generator::generate_serialize_field(ostream& out, t_field* tfield, string prefix) {
  1829. t_type* type = get_true_type(tfield->get_type());
  1830. // Do nothing for void types
  1831. if (type->is_void()) {
  1832. throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
  1833. }
  1834. if (type->is_struct() || type->is_xception()) {
  1835. generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
  1836. } else if (type->is_container()) {
  1837. generate_serialize_container(out, type, prefix + tfield->get_name());
  1838. } else if (type->is_base_type() || type->is_enum()) {
  1839. string name = prefix + tfield->get_name();
  1840. indent(out) << "oprot.";
  1841. if (type->is_base_type()) {
  1842. t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
  1843. switch (tbase) {
  1844. case t_base_type::TYPE_VOID:
  1845. throw "compiler error: cannot serialize void field in a struct: " + name;
  1846. break;
  1847. case t_base_type::TYPE_STRING:
  1848. if (type->is_binary()) {
  1849. out << "writeBinary(" << name << ");";
  1850. } else {
  1851. out << "writeString(" << name << ");";
  1852. }
  1853. break;
  1854. case t_base_type::TYPE_BOOL:
  1855. out << "writeBool(" << name << ");";
  1856. break;
  1857. case t_base_type::TYPE_I8:
  1858. out << "writeByte(" << name << ");";
  1859. break;
  1860. case t_base_type::TYPE_I16:
  1861. out << "writeI16(" << name << ");";
  1862. break;
  1863. case t_base_type::TYPE_I32:
  1864. out << "writeI32(" << name << ");";
  1865. break;
  1866. case t_base_type::TYPE_I64:
  1867. out << "writeI64(" << name << ");";
  1868. break;
  1869. case t_base_type::TYPE_DOUBLE:
  1870. out << "writeDouble(" << name << ");";
  1871. break;
  1872. default:
  1873. throw "compiler error: no As3 name for base type " + t_base_type::t_base_name(tbase);
  1874. }
  1875. } else if (type->is_enum()) {
  1876. out << "writeI32(" << name << ");";
  1877. }
  1878. out << endl;
  1879. } else {
  1880. printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
  1881. prefix.c_str(),
  1882. tfield->get_name().c_str(),
  1883. type_name(type).c_str());
  1884. }
  1885. }
  1886. /**
  1887. * Serializes all the members of a struct.
  1888. *
  1889. * @param tstruct The struct to serialize
  1890. * @param prefix String prefix to attach to all fields
  1891. */
  1892. void t_as3_generator::generate_serialize_struct(ostream& out, t_struct* tstruct, string prefix) {
  1893. (void)tstruct;
  1894. out << indent() << prefix << ".write(oprot);" << endl;
  1895. }
  1896. /**
  1897. * Serializes a container by writing its size then the elements.
  1898. *
  1899. * @param ttype The type of container
  1900. * @param prefix String prefix for fields
  1901. */
  1902. void t_as3_generator::generate_serialize_container(ostream& out, t_type* ttype, string prefix) {
  1903. scope_up(out);
  1904. if (ttype->is_map()) {
  1905. string iter = tmp("_key");
  1906. string counter = tmp("_sizeCounter");
  1907. indent(out) << "var " << counter << ":int = 0;" << endl;
  1908. indent(out) << "for (var " << iter << ":* in " << prefix << ") {" << endl;
  1909. indent(out) << " " << counter << +"++;" << endl;
  1910. indent(out) << "}" << endl;
  1911. indent(out) << "oprot.writeMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
  1912. << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << counter << "));"
  1913. << endl;
  1914. } else if (ttype->is_set()) {
  1915. indent(out) << "oprot.writeSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
  1916. << ", " << prefix << ".size));" << endl;
  1917. } else if (ttype->is_list()) {
  1918. indent(out) << "oprot.writeListBegin(new TList("
  1919. << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".length));"
  1920. << endl;
  1921. }
  1922. string iter = tmp("elem");
  1923. if (ttype->is_map()) {
  1924. indent(out) << "for (var " << iter << ":* in " << prefix << ")";
  1925. } else if (ttype->is_set()) {
  1926. indent(out) << "for each (var " << iter << ":* in " << prefix << ".toArray())";
  1927. } else if (ttype->is_list()) {
  1928. indent(out) << "for each (var " << iter << ":* in " << prefix << ")";
  1929. }
  1930. scope_up(out);
  1931. if (ttype->is_map()) {
  1932. generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
  1933. } else if (ttype->is_set()) {
  1934. generate_serialize_set_element(out, (t_set*)ttype, iter);
  1935. } else if (ttype->is_list()) {
  1936. generate_serialize_list_element(out, (t_list*)ttype, iter);
  1937. }
  1938. scope_down(out);
  1939. if (ttype->is_map()) {
  1940. indent(out) << "oprot.writeMapEnd();" << endl;
  1941. } else if (ttype->is_set()) {
  1942. indent(out) << "oprot.writeSetEnd();" << endl;
  1943. } else if (ttype->is_list()) {
  1944. indent(out) << "oprot.writeListEnd();" << endl;
  1945. }
  1946. scope_down(out);
  1947. }
  1948. /**
  1949. * Serializes the members of a map.
  1950. */
  1951. void t_as3_generator::generate_serialize_map_element(ostream& out,
  1952. t_map* tmap,
  1953. string iter,
  1954. string map) {
  1955. t_field kfield(tmap->get_key_type(), iter);
  1956. generate_serialize_field(out, &kfield, "");
  1957. t_field vfield(tmap->get_val_type(), map + "[" + iter + "]");
  1958. generate_serialize_field(out, &vfield, "");
  1959. }
  1960. /**
  1961. * Serializes the members of a set.
  1962. */
  1963. void t_as3_generator::generate_serialize_set_element(ostream& out, t_set* tset, string iter) {
  1964. t_field efield(tset->get_elem_type(), iter);
  1965. generate_serialize_field(out, &efield, "");
  1966. }
  1967. /**
  1968. * Serializes the members of a list.
  1969. */
  1970. void t_as3_generator::generate_serialize_list_element(ostream& out, t_list* tlist, string iter) {
  1971. t_field efield(tlist->get_elem_type(), iter);
  1972. generate_serialize_field(out, &efield, "");
  1973. }
  1974. /**
  1975. * Returns a As3 type name
  1976. *
  1977. * @param ttype The type
  1978. * @param container Is the type going inside a container?
  1979. * @return As3 type name, i.e. HashMap<Key,Value>
  1980. */
  1981. string t_as3_generator::type_name(t_type* ttype, bool in_container, bool in_init) {
  1982. (void)in_init;
  1983. // In As3 typedefs are just resolved to their real type
  1984. ttype = get_true_type(ttype);
  1985. string prefix;
  1986. if (ttype->is_base_type()) {
  1987. return base_type_name((t_base_type*)ttype, in_container);
  1988. } else if (ttype->is_enum()) {
  1989. return "int";
  1990. } else if (ttype->is_map()) {
  1991. return "Dictionary";
  1992. } else if (ttype->is_set()) {
  1993. return "Set";
  1994. } else if (ttype->is_list()) {
  1995. return "Array";
  1996. }
  1997. // Check for namespacing
  1998. t_program* program = ttype->get_program();
  1999. if (program != NULL && program != program_) {
  2000. string package = program->get_namespace("as3");
  2001. if (!package.empty()) {
  2002. return package + "." + ttype->get_name();
  2003. }
  2004. }
  2005. return ttype->get_name();
  2006. }
  2007. /**
  2008. * Returns the AS3 type that corresponds to the thrift type.
  2009. *
  2010. * @param tbase The base type
  2011. * @param container Is it going in a As3 container?
  2012. */
  2013. string t_as3_generator::base_type_name(t_base_type* type, bool in_container) {
  2014. (void)in_container;
  2015. t_base_type::t_base tbase = type->get_base();
  2016. switch (tbase) {
  2017. case t_base_type::TYPE_VOID:
  2018. return "void";
  2019. case t_base_type::TYPE_STRING:
  2020. if (type->is_binary()) {
  2021. return "ByteArray";
  2022. } else {
  2023. return "String";
  2024. }
  2025. case t_base_type::TYPE_BOOL:
  2026. return "Boolean";
  2027. case t_base_type::TYPE_I8:
  2028. case t_base_type::TYPE_I16:
  2029. case t_base_type::TYPE_I32:
  2030. return "int";
  2031. case t_base_type::TYPE_I64:
  2032. throw "i64 is not yet supported in as3";
  2033. case t_base_type::TYPE_DOUBLE:
  2034. return "Number";
  2035. default:
  2036. throw "compiler error: no As3 name for base type " + t_base_type::t_base_name(tbase);
  2037. }
  2038. }
  2039. /**
  2040. * Declares a field, which may include initialization as necessary.
  2041. *
  2042. * @param ttype The type
  2043. */
  2044. string t_as3_generator::declare_field(t_field* tfield, bool init) {
  2045. // TODO(mcslee): do we ever need to initialize the field?
  2046. string result = "var " + tfield->get_name() + ":" + type_name(tfield->get_type());
  2047. if (init) {
  2048. t_type* ttype = get_true_type(tfield->get_type());
  2049. if (ttype->is_base_type() && tfield->get_value() != NULL) {
  2050. std::ofstream dummy;
  2051. result += " = " + render_const_value(dummy, tfield->get_name(), ttype, tfield->get_value());
  2052. } else if (ttype->is_base_type()) {
  2053. t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
  2054. switch (tbase) {
  2055. case t_base_type::TYPE_VOID:
  2056. throw "NO T_VOID CONSTRUCT";
  2057. case t_base_type::TYPE_STRING:
  2058. result += " = null";
  2059. break;
  2060. case t_base_type::TYPE_BOOL:
  2061. result += " = false";
  2062. break;
  2063. case t_base_type::TYPE_I8:
  2064. case t_base_type::TYPE_I16:
  2065. case t_base_type::TYPE_I32:
  2066. case t_base_type::TYPE_I64:
  2067. result += " = 0";
  2068. break;
  2069. case t_base_type::TYPE_DOUBLE:
  2070. result += " = (double)0";
  2071. break;
  2072. }
  2073. } else if (ttype->is_enum()) {
  2074. result += " = 0";
  2075. } else if (ttype->is_container()) {
  2076. result += " = new " + type_name(ttype, false, true) + "()";
  2077. } else {
  2078. result += " = new " + type_name(ttype, false, true) + "()";
  2079. ;
  2080. }
  2081. }
  2082. return result + ";";
  2083. }
  2084. /**
  2085. * Renders a function signature of the form 'type name(args)'
  2086. *
  2087. * @param tfunction Function definition
  2088. * @return String of rendered function definition
  2089. */
  2090. string t_as3_generator::function_signature(t_function* tfunction, string prefix) {
  2091. std::string arguments = argument_list(tfunction->get_arglist());
  2092. if (!tfunction->is_oneway()) {
  2093. if (arguments != "") {
  2094. arguments += ", ";
  2095. }
  2096. arguments += "onError:Function, onSuccess:Function";
  2097. }
  2098. std::string result = "function " + prefix + tfunction->get_name() + "(" + arguments + "):void";
  2099. return result;
  2100. }
  2101. /**
  2102. * Renders a comma separated field list, with type names
  2103. */
  2104. string t_as3_generator::argument_list(t_struct* tstruct) {
  2105. string result = "";
  2106. const vector<t_field*>& fields = tstruct->get_members();
  2107. vector<t_field*>::const_iterator f_iter;
  2108. bool first = true;
  2109. for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
  2110. if (first) {
  2111. first = false;
  2112. } else {
  2113. result += ", ";
  2114. }
  2115. result += (*f_iter)->get_name() + ":" + type_name((*f_iter)->get_type());
  2116. }
  2117. return result;
  2118. }
  2119. /**
  2120. * Converts the parse type to a C++ enum string for the given type.
  2121. */
  2122. string t_as3_generator::type_to_enum(t_type* type) {
  2123. type = get_true_type(type);
  2124. if (type->is_base_type()) {
  2125. t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
  2126. switch (tbase) {
  2127. case t_base_type::TYPE_VOID:
  2128. throw "NO T_VOID CONSTRUCT";
  2129. case t_base_type::TYPE_STRING:
  2130. return "TType.STRING";
  2131. case t_base_type::TYPE_BOOL:
  2132. return "TType.BOOL";
  2133. case t_base_type::TYPE_I8:
  2134. return "TType.BYTE";
  2135. case t_base_type::TYPE_I16:
  2136. return "TType.I16";
  2137. case t_base_type::TYPE_I32:
  2138. return "TType.I32";
  2139. case t_base_type::TYPE_I64:
  2140. return "TType.I64";
  2141. case t_base_type::TYPE_DOUBLE:
  2142. return "TType.DOUBLE";
  2143. }
  2144. } else if (type->is_enum()) {
  2145. return "TType.I32";
  2146. } else if (type->is_struct() || type->is_xception()) {
  2147. return "TType.STRUCT";
  2148. } else if (type->is_map()) {
  2149. return "TType.MAP";
  2150. } else if (type->is_set()) {
  2151. return "TType.SET";
  2152. } else if (type->is_list()) {
  2153. return "TType.LIST";
  2154. }
  2155. throw "INVALID TYPE IN type_to_enum: " + type->get_name();
  2156. }
  2157. /**
  2158. * Applies the correct style to a string based on the value of nocamel_style_
  2159. */
  2160. std::string t_as3_generator::get_cap_name(std::string name) {
  2161. name[0] = toupper(name[0]);
  2162. return name;
  2163. }
  2164. string t_as3_generator::constant_name(string name) {
  2165. string constant_name;
  2166. bool is_first = true;
  2167. bool was_previous_char_upper = false;
  2168. for (string::iterator iter = name.begin(); iter != name.end(); ++iter) {
  2169. string::value_type character = (*iter);
  2170. bool is_upper = isupper(character);
  2171. if (is_upper && !is_first && !was_previous_char_upper) {
  2172. constant_name += '_';
  2173. }
  2174. constant_name += toupper(character);
  2175. is_first = false;
  2176. was_previous_char_upper = is_upper;
  2177. }
  2178. return constant_name;
  2179. }
  2180. /**
  2181. * Emits a As3Doc comment if the provided object has a doc in Thrift
  2182. */
  2183. void t_as3_generator::generate_as3_doc(ostream& out, t_doc* tdoc) {
  2184. if (tdoc->has_doc()) {
  2185. generate_docstring_comment(out, "/**\n", " * ", tdoc->get_doc(), " */\n");
  2186. }
  2187. }
  2188. /**
  2189. * Emits a As3Doc comment if the provided function object has a doc in Thrift
  2190. */
  2191. void t_as3_generator::generate_as3_doc(ostream& out, t_function* tfunction) {
  2192. if (tfunction->has_doc()) {
  2193. stringstream ss;
  2194. ss << tfunction->get_doc();
  2195. const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
  2196. vector<t_field*>::const_iterator p_iter;
  2197. for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
  2198. t_field* p = *p_iter;
  2199. ss << "\n@param " << p->get_name();
  2200. if (p->has_doc()) {
  2201. ss << " " << p->get_doc();
  2202. }
  2203. }
  2204. generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
  2205. }
  2206. }
  2207. std::string t_as3_generator::generate_isset_check(t_field* field) {
  2208. return generate_isset_check(field->get_name());
  2209. }
  2210. std::string t_as3_generator::generate_isset_check(std::string field_name) {
  2211. return "is" + get_cap_name("set") + get_cap_name(field_name) + "()";
  2212. }
  2213. void t_as3_generator::generate_isset_set(ostream& out, t_field* field) {
  2214. if (!type_can_be_null(field->get_type())) {
  2215. indent(out) << "this.__isset_" << field->get_name() << " = true;" << endl;
  2216. }
  2217. }
  2218. std::string t_as3_generator::get_enum_class_name(t_type* type) {
  2219. string package = "";
  2220. t_program* program = type->get_program();
  2221. if (program != NULL && program != program_) {
  2222. package = program->get_namespace("as3") + ".";
  2223. }
  2224. return package + type->get_name();
  2225. }
  2226. THRIFT_REGISTER_GENERATOR(
  2227. as3,
  2228. "AS3",
  2229. " bindable: Add [bindable] metadata to all the struct classes.\n")