THRIFT-1044 Fix JavaScript inheritance
authorRoger Meier <roger@apache.org>
Sat, 22 Jan 2011 21:35:48 +0000 (21:35 +0000)
committerRoger Meier <roger@apache.org>
Sat, 22 Jan 2011 21:35:48 +0000 (21:35 +0000)
Patch Wade Simmons

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

compiler/cpp/src/generate/t_js_generator.cc
lib/js/thrift.js
lib/nodejs/lib/thrift/thrift.js

index bbf436d..8d3c440 100644 (file)
@@ -541,19 +541,12 @@ void t_js_generator::generate_js_struct_definition(ofstream& out,
   out << "}\n";
 
   if (is_exception) {
-      if (gen_node_) {
-          out << "require('util').inherits(" <<
-              js_namespace(tstruct->get_program()) <<
-              tstruct->get_name() << ", Thrift.TException)" << endl;
-      } else {
-          out << "for (var property in Thrift.TException)"<<endl<<
-              js_namespace(tstruct->get_program())<<tstruct->get_name()<<"[property] = Thrift.TException[property]"<<endl;
-      }
-  }
-
-  if (!gen_node_) {
-      //init prototype
-      out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {}\n";
+    out << "Thrift.inherits(" <<
+        js_namespace(tstruct->get_program()) <<
+        tstruct->get_name() << ", Thrift.TException)" << endl;
+  } else {
+    //init prototype
+    out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {}\n";
   }
 
 
@@ -935,17 +928,15 @@ void t_js_generator::generate_service_client(t_service* tservice) {
 
 
   if (tservice->get_extends() != NULL) {
-      extends = tservice->get_extends()->get_name();
-
-      f_service_ << "for (var property in "<<extends<<"Client)"<<endl<<
-          js_namespace(tservice->get_program()) << service_name_<<"Client[property] = "<<extends<<"Client[property]"<<endl;
-
+    indent(f_service_) << "Thrift.inherits(" <<
+        js_namespace(tservice->get_program()) <<
+        service_name_ << "Client, " <<
+        tservice->get_extends()->get_name() << ".Client)" << endl;
+  } else {
+      //init prototype
+      indent(f_service_) <<  js_namespace(tservice->get_program())<<service_name_ << "Client.prototype = {}"<<endl;
   }
 
-  //init prototype
-  f_service_ <<  js_namespace(tservice->get_program())<<service_name_ << "Client.prototype = {}"<<endl;
-
-
   // Generate client method implementations
   vector<t_function*> functions = tservice->get_functions();
   vector<t_function*>::const_iterator f_iter;
index fbdc809..c06e27a 100644 (file)
@@ -723,4 +723,10 @@ Thrift.objectLength = function(obj) {
   return length;
 }
 
-
+Thirft.inherits = function(constructor, superConstructor) {
+  // Prototypal Inheritance
+  // http://javascript.crockford.com/prototypal.html
+  function F() {}
+  F.prototype = superConstructor.prototype;
+  constructor.prototype = new F();
+}
index aee5a54..87acc6f 100644 (file)
@@ -132,3 +132,7 @@ TApplicationException.prototype.write = function(output){
 exports.objectLength = function(obj) {
   return Object.keys(obj).length;
 }
+
+exports.inherits = function(constructor, superConstructor) {
+  sys.inherits(constructor, superConstructor);
+}