react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / contextify / node_modules / bindings / bindings.js
81146 views1/**2* Module dependencies.3*/45var fs = require('fs')6, path = require('path')7, join = path.join8, dirname = path.dirname9, exists = fs.existsSync || path.existsSync10, defaults = {11arrow: process.env.NODE_BINDINGS_ARROW || ' → '12, compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled'13, platform: process.platform14, arch: process.arch15, version: process.versions.node16, bindings: 'bindings.node'17, try: [18// node-gyp's linked version in the "build" dir19[ 'module_root', 'build', 'bindings' ]20// node-waf and gyp_addon (a.k.a node-gyp)21, [ 'module_root', 'build', 'Debug', 'bindings' ]22, [ 'module_root', 'build', 'Release', 'bindings' ]23// Debug files, for development (legacy behavior, remove for node v0.9)24, [ 'module_root', 'out', 'Debug', 'bindings' ]25, [ 'module_root', 'Debug', 'bindings' ]26// Release files, but manually compiled (legacy behavior, remove for node v0.9)27, [ 'module_root', 'out', 'Release', 'bindings' ]28, [ 'module_root', 'Release', 'bindings' ]29// Legacy from node-waf, node <= 0.4.x30, [ 'module_root', 'build', 'default', 'bindings' ]31// Production "Release" buildtype binary (meh...)32, [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ]33]34}3536/**37* The main `bindings()` function loads the compiled bindings for a given module.38* It uses V8's Error API to determine the parent filename that this function is39* being invoked from, which is then used to find the root directory.40*/4142function bindings (opts) {4344// Argument surgery45if (typeof opts == 'string') {46opts = { bindings: opts }47} else if (!opts) {48opts = {}49}50opts.__proto__ = defaults5152// Get the module root53if (!opts.module_root) {54opts.module_root = exports.getRoot(exports.getFileName())55}5657// Ensure the given bindings name ends with .node58if (path.extname(opts.bindings) != '.node') {59opts.bindings += '.node'60}6162var tries = []63, i = 064, l = opts.try.length65, n66, b67, err6869for (; i<l; i++) {70n = join.apply(null, opts.try[i].map(function (p) {71return opts[p] || p72}))73tries.push(n)74try {75b = opts.path ? require.resolve(n) : require(n)76if (!opts.path) {77b.path = n78}79return b80} catch (e) {81if (!/not find/i.test(e.message)) {82throw e83}84}85}8687err = new Error('Could not locate the bindings file. Tried:\n'88+ tries.map(function (a) { return opts.arrow + a }).join('\n'))89err.tries = tries90throw err91}92module.exports = exports = bindings939495/**96* Gets the filename of the JavaScript file that invokes this function.97* Used to help find the root directory of a module.98* Optionally accepts an filename argument to skip when searching for the invoking filename99*/100101exports.getFileName = function getFileName (calling_file) {102var origPST = Error.prepareStackTrace103, origSTL = Error.stackTraceLimit104, dummy = {}105, fileName106107Error.stackTraceLimit = 10108109Error.prepareStackTrace = function (e, st) {110for (var i=0, l=st.length; i<l; i++) {111fileName = st[i].getFileName()112if (fileName !== __filename) {113if (calling_file) {114if (fileName !== calling_file) {115return116}117} else {118return119}120}121}122}123124// run the 'prepareStackTrace' function above125Error.captureStackTrace(dummy)126dummy.stack127128// cleanup129Error.prepareStackTrace = origPST130Error.stackTraceLimit = origSTL131132return fileName133}134135/**136* Gets the root directory of a module, given an arbitrary filename137* somewhere in the module tree. The "root directory" is the directory138* containing the `package.json` file.139*140* In: /home/nate/node-native-module/lib/index.js141* Out: /home/nate/node-native-module142*/143144exports.getRoot = function getRoot (file) {145var dir = dirname(file)146, prev147while (true) {148if (dir === '.') {149// Avoids an infinite loop in rare cases, like the REPL150dir = process.cwd()151}152if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {153// Found the 'package.json' file or 'node_modules' dir; we're done154return dir155}156if (prev === dir) {157// Got to the top158throw new Error('Could not find module root given file: "' + file159+ '". Do you have a `package.json` file? ')160}161// Try the parent dir next162prev = dir163dir = join(dir, '..')164}165}166167168