Skip to content

Commit

Permalink
THRIFT-4675: Generate Int64 constants for js
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafa-cosar authored and jeking3 committed Jan 15, 2019
1 parent 010ccf0 commit f86845e
Show file tree
Hide file tree
Showing 44 changed files with 11,418 additions and 67 deletions.
2,924 changes: 2,924 additions & 0 deletions CHANGES

Large diffs are not rendered by default.

69 changes: 58 additions & 11 deletions compiler/cpp/src/thrift/generate/t_js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ using std::stringstream;
using std::vector;

static const string endl = "\n"; // avoid ostream << std::endl flushes
// largest consecutive integer representable by a double (2 ^ 53 - 1)
static const long max_safe_integer = 0x1fffffffffffff;
// smallest consecutive number representable by a double (-2 ^ 53 + 1)
static const long min_safe_integer = -max_safe_integer;

#include "thrift/generate/t_oop_generator.h"

Expand Down Expand Up @@ -200,6 +204,7 @@ class t_js_generator : public t_oop_generator {

std::string js_includes();
std::string ts_includes();
std::string ts_service_includes();
std::string render_includes();
std::string declare_field(t_field* tfield, bool init = false, bool obj = false);
std::string function_signature(t_function* tfunction,
Expand Down Expand Up @@ -296,7 +301,7 @@ class t_js_generator : public t_oop_generator {
* Returns "declare " if no module was defined.
* @return string
*/
string ts_declare() { return (ts_module_.empty() ? "declare " : ""); }
string ts_declare() { return (ts_module_.empty() ? (gen_node_ ? "declare " : "export declare ") : ""); }

/**
* Returns "?" if the given field is optional or has a default value.
Expand Down Expand Up @@ -430,6 +435,8 @@ void t_js_generator::init_generator() {
f_types_ << "if (typeof " << pns << " === 'undefined') {" << endl;
f_types_ << " " << pns << " = {};" << endl;
f_types_ << "}" << endl;
f_types_ << "" << "if (typeof module !== 'undefined' && module.exports) {" << endl;
f_types_ << " module.exports." << pns << " = " << pns << ";" << endl << "}" << endl;
}
if (gen_ts_) {
ts_module_ = pns;
Expand All @@ -448,10 +455,11 @@ string t_js_generator::js_includes() {
if (!gen_es6_) {
result += js_const_type_ + "Q = thrift.Q;\n";
}
result += js_const_type_ + "Int64 = require('node-int64');\n";
return result;
}

return "";
string result = "if (typeof Int64 === 'undefined' && typeof require === 'function') {\n " + js_const_type_ + "Int64 = require('node-int64');\n}\n";
return result;
}

/**
Expand All @@ -462,10 +470,24 @@ string t_js_generator::ts_includes() {
return string(
"import thrift = require('thrift');\n"
"import Thrift = thrift.Thrift;\n"
"import Q = thrift.Q;\n");
"import Q = thrift.Q;\n"
"import Int64 = require('node-int64');");
}
return string("import Int64 = require('node-int64');");
}

return "";
/**
* Prints service ts imports
*/
string t_js_generator::ts_service_includes() {
if (gen_node_) {
return string(
"import thrift = require('thrift');\n"
"import Thrift = thrift.Thrift;\n"
"import Q = thrift.Q;\n"
"import Int64 = require('node-int64');");
}
return string("import Int64 = require('node-int64');");
}

/**
Expand Down Expand Up @@ -593,9 +615,18 @@ string t_js_generator::render_const_value(t_type* type, t_const_value* value) {
case t_base_type::TYPE_I8:
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
case t_base_type::TYPE_I64:
out << value->get_integer();
break;
case t_base_type::TYPE_I64:
{
int64_t const& integer_value = value->get_integer();
if (integer_value <= max_safe_integer && integer_value >= min_safe_integer) {
out << "new Int64(" << integer_value << ")";
} else {
out << "new Int64('" << std::hex << integer_value << std::dec << "')";
}
}
break;
case t_base_type::TYPE_DOUBLE:
if (value->get_type() == t_const_value::CV_INTEGER) {
out << value->get_integer();
Expand Down Expand Up @@ -1086,9 +1117,8 @@ void t_js_generator::generate_service(t_service* tservice) {
f_service_ts_ << "/// <reference path=\"" << tservice->get_extends()->get_name()
<< ".d.ts\" />" << endl;
}
f_service_ts_ << autogen_comment() << endl;
f_service_ts_ << autogen_comment() << endl << ts_includes() << endl << endl;
if (gen_node_) {
f_service_ts_ << ts_includes() << endl;
f_service_ts_ << "import ttypes = require('./" + program_->get_name() + "_types');" << endl;
// Generate type aliases
// enum
Expand Down Expand Up @@ -1119,9 +1149,18 @@ void t_js_generator::generate_service(t_service* tservice) {
f_service_ts_ << "import " << (*s_iter)->get_name() << " = ttypes."
<< js_namespace(program_) << (*s_iter)->get_name() << endl;
}
} else {
f_service_ts_ << "import { " << program_->get_name() << " } from \"./" << program_->get_name() << "_types\";" << endl << endl;
}
if (!ts_module_.empty()) {
f_service_ts_ << "declare module " << ts_module_ << " {";
if (gen_node_) {
f_service_ts_ << "declare module " << ts_module_ << " {";
} else {
f_service_ts_ << "declare module \"./" << program_->get_name() << "_types\" {" << endl;
indent_up();
f_service_ts_ << ts_indent() << "module " << program_->get_name() << " {" << endl;
indent_up();
}
}
}

Expand Down Expand Up @@ -1149,7 +1188,13 @@ void t_js_generator::generate_service(t_service* tservice) {
f_service_.close();
if (gen_ts_) {
if (!ts_module_.empty()) {
f_service_ts_ << "}";
if (gen_node_) {
f_service_ts_ << "}" << endl;
} else {
indent_down();
f_service_ts_ << ts_indent() << "}" << endl;
f_service_ts_ << "}" << endl;
}
}
f_service_ts_.close();
}
Expand Down Expand Up @@ -2572,10 +2617,12 @@ string t_js_generator::ts_get_type(t_type* type) {
break;
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
case t_base_type::TYPE_I64:
case t_base_type::TYPE_DOUBLE:
ts_type = "number";
break;
case t_base_type::TYPE_I64:
ts_type = "Int64";
break;
case t_base_type::TYPE_VOID:
ts_type = "void";
}
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ AC_CONFIG_FILES([
lib/rs/test/Makefile
lib/lua/Makefile
lib/swift/Makefile
lib/ts/Makefile
lib/xml/Makefile
lib/xml/test/Makefile
test/Makefile
Expand Down
54 changes: 44 additions & 10 deletions lib/js/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ module.exports = function(grunt) {
ThriftGenDeepConstructor: {
command: '../../compiler/cpp/thrift -gen js -o test ../../test/JsDeepConstructorTest.thrift'
},
ThriftBrowserifyNodeInt64: {
command: [
'./node_modules/browserify/bin/cmd.js ./node_modules/node-int64/Int64.js -s Int64 -o test/build/js/lib/Int64.js',
'./node_modules/browserify/bin/cmd.js ../nodejs/lib/thrift/int64_util.js -s Int64Util -o test/build/js/lib/Int64Util.js',
'./node_modules/browserify/bin/cmd.js ./node_modules/json-int64/index.js -s JSONInt64 -o test/build/js/lib/JSONInt64.js'
].join(' && ')
},
ThriftGenInt64: {
command: '../../compiler/cpp/thrift -gen js -o test ../../test/Int64Test.thrift'
},
ThriftGenDoubleConstants: {
command: '../../compiler/cpp/thrift -gen js -o test ../../test/DoubleConstantsTest.thrift'
},
Expand Down Expand Up @@ -147,6 +157,18 @@ module.exports = function(grunt) {
},
}
},
ThriftJS_Int64: {
options: {
urls: [
'http://localhost:8089/test-int64.html'
],
puppeteer: {
headless: true,
args: ['--no-sandbox'],
ignoreHTTPSErrors: true,
},
}
},
ThriftWS: {
options: {
urls: [
Expand Down Expand Up @@ -218,7 +240,7 @@ module.exports = function(grunt) {
}
},
jshint: {
// The main thrift library file. not es6 yet :(
// The main Thrift library file. not es6 yet :(
lib: {
src: ['src/**/*.js'],
},
Expand Down Expand Up @@ -261,33 +283,45 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-shell-spawn');

grunt.registerTask('wait', 'Wait just one second for server to start', function () {
grunt.registerTask('wait', 'Wait just one second for the server to start', function () {
var done = this.async();
setTimeout(function() {
done(true);
}, 1000);
});

grunt.registerTask('installAndGenerate', [
'shell:InstallThriftJS', 'shell:InstallThriftNodeJSDep', 'shell:ThriftGen',
'shell:ThriftGenDeepConstructor', 'shell:ThriftGenDoubleConstants',
'shell:InstallThriftJS',
'shell:InstallThriftNodeJSDep',
'shell:ThriftGen',
'shell:ThriftGenDeepConstructor',
'shell:ThriftGenDoubleConstants',
'shell:InstallTestLibs',
'shell:ThriftBrowserifyNodeInt64',
'shell:ThriftGenInt64'
]);

grunt.registerTask('test', [
'installAndGenerate',
'jshint',
'shell:ThriftTestServer', 'shell:ThriftTestServer_TLS',
'shell:ThriftTestServerES6', 'shell:ThriftTestServerES6_TLS',
'shell:ThriftTestServer',
'shell:ThriftTestServer_TLS',
'shell:ThriftTestServerES6',
'shell:ThriftTestServerES6_TLS',
'wait',
'qunit:ThriftDeepConstructor',
'qunit:ThriftJS', 'qunit:ThriftJS_TLS',
'qunit:ThriftJS',
'qunit:ThriftJS_TLS',
'qunit:ThriftJS_DoubleRendering',
'qunit:ThriftWS',
'qunit:ThriftJSJQ', 'qunit:ThriftJSJQ_TLS',
'qunit:ThriftJSJQ',
'qunit:ThriftJSJQ_TLS',
'qunit:ThriftWSES6',
'shell:ThriftTestServer:kill', 'shell:ThriftTestServer_TLS:kill',
'shell:ThriftTestServerES6:kill', 'shell:ThriftTestServerES6_TLS:kill',
'qunit:ThriftJS_Int64',
'shell:ThriftTestServer:kill',
'shell:ThriftTestServer_TLS:kill',
'shell:ThriftTestServerES6:kill',
'shell:ThriftTestServerES6_TLS:kill',
]);
grunt.registerTask('default', ['test', 'concat', 'uglify', 'jsdoc']);
};
5 changes: 5 additions & 0 deletions lib/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ TypeScript definition files can also be generated by running:

thrift --gen js:ts file.thrift

# Breaking Changes

## 0.13.0

1. 64-bit integer constants are now generatd using node-int64 e.g.: var x = new Int64("7fffffffffffffff");
Loading

0 comments on commit f86845e

Please sign in to comment.