Skip to content

Commit

Permalink
src,build: improve the native module subsystem
Browse files Browse the repository at this point in the history
* Split `jsrs-impl.cc` into separate modules.
* Make some refactoring.
* Rename the native addon to `jstp` since there already is a function
  that is not a part of JSRS.
* Fix `binding.gyp`: make `cflags` not ignored on macOS (as it appeared
  they used to be) and do not use `-O3` in Debug configuration.
* Use a macro to throw V8 exceptions to avoid boilerplate code.

Backport-of: #36
  • Loading branch information
aqrln committed Apr 2, 2017
1 parent 6bea82a commit 98c13ce
Show file tree
Hide file tree
Showing 14 changed files with 1,394 additions and 254 deletions.
34 changes: 31 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
{
'variables': {
'jstp_base_cflags': ['-Wall', '-Wextra', '-Wno-unused-parameter'],
'jstp_debug_cflags': ['-g', '-O0'],
'jstp_release_cflags': ['-O3']
},
'targets': [
{
'target_name': 'jsrs',
'cflags': ['-O3'],
'sources': ['src/jsrs-impl.cc']
'target_name': 'jstp',
'sources': [
'src/node_bindings.cc',
'src/jsrs_serializer.cc',
'src/jsrs_parser.cc',
'src/packet_parser.cc',
'src/unicode_utils.cc'
],
'configurations': {
'Debug': {
'cflags': ['<@(jstp_debug_cflags)'],
'xcode_settings': {
'OTHER_CFLAGS': ['<@(jstp_debug_cflags)']
}
},
'Release': {
'cflags': ['<@(jstp_release_cflags)'],
'xcode_settings': {
'OTHER_CFLAGS': ['<@(jstp_release_cflags)']
}
}
},
'cflags': ['<@(jstp_base_cflags)'],
'xcode_settings': {
'OTHER_CFLAGS': ['<@(jstp_base_cflags)']
}
}
]
}
20 changes: 8 additions & 12 deletions lib/record-serialization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var common = require('./common');
var serializerFactory = require('./serializer-factory');

var jsrs = {};
Expand All @@ -11,31 +12,26 @@ module.exports = jsrs;
// one of our priorities to optimize it.
var USE_NATIVE_SERIALIZER = false;

var jsrsNative;
var jstpNative;

try {
jsrsNative = require('../build/Release/jsrs');
jstpNative = require('../build/Release/jstp');
} catch (e) {
try {
jsrsNative = require('../build/Debug/jsrs');
jstpNative = require('../build/Debug/jstp');
} catch (e) {
console.warn(
'JSTP Record Serialization native module is not built. ' +
'JSTP native addon is not built. ' +
'Run `npm install` in order to build it, otherwise you will get ' +
'poor server performance under load.'
);
module.exports = require('./record-serialization-fallback');
}
}

if (jsrsNative) {
jsrs.parse = jsrsNative.parse;

if (USE_NATIVE_SERIALIZER) {
jsrs.stringify = jsrsNative.stringify;
} else {
if (jstpNative) {
common.extend(jsrs, jstpNative);
if (!USE_NATIVE_SERIALIZER) {
jsrs.stringify = serializerFactory.createSerializer();
}

jsrs.parseNetworkPackets = jsrsNative.parseNetworkPackets;
}
11 changes: 11 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2016-2017 JSTP project authors. Use of this source code is
// governed by the MIT license that can be found in the LICENSE file.

#ifndef SRC_COMMON_H_
#define SRC_COMMON_H_

#define THROW_EXCEPTION(ex_type, ex_msg) \
isolate->ThrowException(v8::Exception::ex_type( \
v8::String::NewFromUtf8(isolate, ex_msg)))

#endif // SRC_COMMON_H_
192 changes: 0 additions & 192 deletions src/jsrs-impl.h

This file was deleted.

47 changes: 0 additions & 47 deletions src/jsrs.h

This file was deleted.

Loading

0 comments on commit 98c13ce

Please sign in to comment.