Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Compile ncpy3 in cpp land; require both, use it conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
eljefedelrodeodeljefe committed Nov 30, 2015
1 parent d4e7a35 commit dd88d74
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 185 deletions.
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
},
"targets": [
{
"target_name": "node-cpython-2X",
"target_name": "node-cpython",
"sources": [
"src/addon.cc",
"src/node-cpython-2X.cc",
"src/node-cpython-3X.cc",
"src/ffi.cc"
],
"include_dirs": [
Expand Down
3 changes: 2 additions & 1 deletion lib/bindings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var addon = require('bindings')('node-cpython-2X')
var addon = require('bindings')('node-cpython')

module.exports = {
NodeCPython2X: addon.NodeCPython2X,
NodeCPython3X: addon.NodeCPython3X,
FFI: new addon.FFI()
}
43 changes: 25 additions & 18 deletions lib/node-cpython.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const EventEmitter = require('events')
const util = require('util')
const glob = require('glob')
const NCPY2 = require('./bindings.js').NodeCPython2X
const NCPY3 = require('./bindings.js').NodeCPython3X
var NCPy2 = new NCPY2()
var NCPy3 = new NCPY2()
var NCPy = NCPy2 // use 2.7 as default, overwrite in constructor
const FFI = require('./ffi.js')
const path = require('path')

Expand All @@ -25,6 +28,10 @@ function Ncpy (options) {
}
}
}
// override cpp dep if so desired
if (this.opts.version === '3.x') {
NCPy = NCPy3
}

this.programs = []
}
Expand Down Expand Up @@ -56,9 +63,9 @@ Ncpy.prototype.init = function (options) {
*/
Ncpy.prototype.repl = function () {
// http://stackoverflow.com/a/9541411/3580261 for below
NCPy2.initialize()
NCPy2.addModule()
NCPy2.getDict()
NCPy.initialize()
NCPy.addModule()
NCPy.getDict()

process.stdin.setEncoding('utf8')

Expand All @@ -67,19 +74,19 @@ Ncpy.prototype.repl = function () {
process.stdout.write('> ')
if (chunk !== null) {
// TODO: implement pass-down to interactiveLoop
NCPy2.runString(chunk)
NCPy.runString(chunk)
}
})

process.stdin.on('end', function () {
process.stdout.write('-------\nPyREPL connection end.\n')
NCPy2.finalize()
NCPy.finalize()
})

process.on('SIGINT', function () {
process.stdout.write('\n-------\nPyREPL closed.\n')

NCPy2.finalize()
NCPy.finalize()
process.exit(0)
})
}
Expand Down Expand Up @@ -170,7 +177,7 @@ Ncpy.prototype.runSync = function (glob, argv, cb) {
Ncpy.prototype.runString = function () {
let args = Array.prototype.slice.call(arguments)
// http://stackoverflow.com/a/9541411/3580261 for above repl implementation
return NCPy2.runString(args[0])
return NCPy.runString(args[0])
}

/**
Expand Down Expand Up @@ -201,9 +208,9 @@ Ncpy.prototype.simpleString = function (str, cb) {
return cb(err)
}

NCPy2.initialize()
NCPy2.simpleString(args[0])
NCPy2.finalize()
NCPy.initialize()
NCPy.simpleString(args[0])
NCPy.finalize()

cb = (typeof cb === 'function') ? cb : function () {}

Expand Down Expand Up @@ -267,7 +274,7 @@ Ncpy.prototype.ffi = function (file, functionname, argList, cb) {
return
}

NCPy2.setPath(path.join(process.cwd(), path.dirname(file)))
NCPy.setPath(path.join(process.cwd(), path.dirname(file)))

cb = (typeof cb === 'function') ? cb : function () {}

Expand All @@ -291,11 +298,11 @@ Ncpy.prototype.ffi.require = function (file, options) {

Ncpy.prototype.ffi._setPath = function () {
// go directly down to cc-land
return NCPy2.setPath(this.fileParam[1])
return NCPy.setPath(this.fileParam[1])
}

Ncpy.prototype.ffi.init = function (stream) {
NCPy2.initialize()
NCPy.initialize()

this._setPath()

Expand All @@ -305,7 +312,7 @@ Ncpy.prototype.ffi.init = function (stream) {
// listen for SIGINT (and sorts)
// REVIEW: for memory leaks
FFI._init(stream, function () {
NCPy2.finalize()
NCPy.finalize()
})
return this
}
Expand Down Expand Up @@ -337,31 +344,31 @@ Ncpy.prototype.eval = function () {
*
*/
Ncpy.prototype.initialize = function () {
return NCPy2.initialize()
return NCPy.initialize()
}

/**
* is-check for the interpreter not running
* @return {Boolean} returns true if Py_isInitialized is ecplictely not 0
*/
Ncpy.prototype.isInitialized = function () {
return NCPy2.isInitialized()
return NCPy.isInitialized()
}

/**
* Finalize python context, clear memory.
* @param {callback} callback for completion of py context
*/
Ncpy.prototype.finalize = function (ref) {
return NCPy2.finalize(ref)
return NCPy.finalize(ref)
}

/**
* is-check for the interpreter not running
* @return {Boolean} return true if Py_isInitialized explictely is 0
*/
Ncpy.prototype.isFinalized = function () {
return NCPy2.isFinalized()
return NCPy.isFinalized()
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <nan.h>
#include "node-cpython-2X.h"
#include "node-cpython-3X.h"
#include "ffi.h"

void InitAll(v8::Local<v8::Object> exports) {
NodeCPython2X::Init(exports);
NodeCPython3X::Init(exports);
FFI::Init(exports);
}

Expand Down
Loading

0 comments on commit dd88d74

Please sign in to comment.