From 90443f4e11d883fba6abb23562a159e8a8b0b78e Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sat, 22 Jan 2011 21:35:48 +0000 Subject: [PATCH] THRIFT-1044 Fix JavaScript inheritance 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 | 35 ++++++++------------- lib/js/thrift.js | 8 ++++- lib/nodejs/lib/thrift/thrift.js | 4 +++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc index bbf436dc..8d3c4401 100644 --- a/compiler/cpp/src/generate/t_js_generator.cc +++ b/compiler/cpp/src/generate/t_js_generator.cc @@ -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)"<get_program())<get_name()<<"[property] = Thrift.TException[property]"<get_program())<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())<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 "<get_program()) << service_name_<<"Client[property] = "<get_program()) << + service_name_ << "Client, " << + tservice->get_extends()->get_name() << ".Client)" << endl; + } else { + //init prototype + indent(f_service_) << js_namespace(tservice->get_program())<get_program())< functions = tservice->get_functions(); vector::const_iterator f_iter; diff --git a/lib/js/thrift.js b/lib/js/thrift.js index fbdc809a..c06e27a0 100644 --- a/lib/js/thrift.js +++ b/lib/js/thrift.js @@ -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(); +} diff --git a/lib/nodejs/lib/thrift/thrift.js b/lib/nodejs/lib/thrift/thrift.js index aee5a544..87acc6f7 100644 --- a/lib/nodejs/lib/thrift/thrift.js +++ b/lib/nodejs/lib/thrift/thrift.js @@ -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); +} -- 2.17.1