THRIFT-1200. js: JS compiler generates code that clobbers existing namespaces
authorBryan Duxbury <bryanduxbury@apache.org>
Wed, 8 Jun 2011 23:51:24 +0000 (23:51 +0000)
committerBryan Duxbury <bryanduxbury@apache.org>
Wed, 8 Jun 2011 23:51:24 +0000 (23:51 +0000)
Patch: Ilya Maykov

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1133606 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_js_generator.cc

index 6c61890..f925a4d 100644 (file)
@@ -275,15 +275,10 @@ void t_js_generator::init_generator() {
   // TODO should the namespace just be in the directory structure for node?
   vector<string> ns_pieces = js_namespace_pieces( program_ );
   if( ns_pieces.size() > 0){
-      f_types_ << "var " << ns_pieces[0] << " = {};"<<endl;
-
-      pns = ns_pieces[0];
-
-      for(size_t i=1; i<ns_pieces.size(); i++){
-          pns += "." + ns_pieces[i];
-
-          f_types_ << pns << " = {}"<<endl;
-      }
+    for(size_t i = 0; i < ns_pieces.size(); ++i) {
+      pns += ((i == 0) ? "" : ".") + ns_pieces[i];
+      f_types_ << "if (typeof " << pns << " === 'undefined') " << pns << " = {};" << endl;
+    }
   }
 
 }