Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Created March 19, 2019 02:26
Show Gist options
  • Save gabrielschulhof/a21afc24e8c78e86e6296ea32ff0075b to your computer and use it in GitHub Desktop.
Save gabrielschulhof/a21afc24e8c78e86e6296ea32ff0075b to your computer and use it in GitHub Desktop.
Crash on OSX because of symbols exposed by default
#include <assert.h>
#include <node.h>
#include "common.h"
namespace {
void Init(v8::Local<v8::Object> exports,
v8::Local<v8::Object> module,
v8::Local<v8::Context> context) {
v8::Local<v8::Name> propName =
v8::String::NewFromUtf8(context->GetIsolate(), "getModuleName",
v8::NewStringType::kNormal).ToLocalChecked();
v8::Local<v8::Value> propValue =
ReturnModuleName::New(context, NODE_STRINGIFY(NODE_GYP_MODULE_NAME));
exports->Set(context, propName, propValue).FromJust();
}
} // end of anonymous namespace
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Init)
{
'variables': {
'apply_fix': '<!(echo $npm_config_apply_fix)'
},
'target_defaults': {
'sources': [ 'binding.cc' ],
'conditions': [
[
'apply_fix == "true"', {
# The snippet we want to add to addon.gypi
'conditions': [
[
'OS=="mac"', {
'cflags': [
'-fvisibility=hidden'
],
'xcode_settings': {
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES' # -fvisibility=hidden
}
}
]
]
##########################################
}
]
]
},
'targets': [
{
'target_name': 'binding1',
},
{
'target_name': 'binding2',
},
]
}
#ifndef COMMON_INL_H_
#define COMMON_INL_H_
#include "common.h"
inline v8::Local<v8::Function>
ReturnModuleName::New(v8::Local<v8::Context> context, const char* string) {
v8::Local<v8::External> ext =
v8::External::New(context->GetIsolate(),
static_cast<void*>(const_cast<char*>(string)));
ref = new v8::Persistent<v8::External>(context->GetIsolate(), ext);
return v8::Function::New(context, Binding).ToLocalChecked();
}
inline void
ReturnModuleName::Binding(const v8::FunctionCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(
v8::String::NewFromUtf8(info.GetIsolate(),
static_cast<const char*>(
v8::Local<v8::External>::New(info.GetIsolate(), *ref)->Value()),
v8::NewStringType::kNormal).ToLocalChecked());
ref->Reset();
delete ref;
ref = nullptr;
}
v8::Persistent<v8::External>* ReturnModuleName::ref = nullptr;
#endif // COMMON_INL_H_
#ifndef COMMON_H_
#define COMMON_H_
#include <node.h>
class ReturnModuleName {
public:
static v8::Persistent<v8::External>* ref;
static v8::Local<v8::Function> New(v8::Local<v8::Context> context,
const char* string);
private:
static void Binding(const v8::FunctionCallbackInfo<v8::Value>& info);
};
#include "common-inl.h"
#endif // COMMON_H_
const bindings = require('bindings');
const binding1 = bindings('binding1');
const binding2 = bindings('binding2');
console.log(binding1.getModuleName());
console.log(binding2.getModuleName());
{
"name": "napi-pr-407",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"bindings": "^1.4.0"
},
"author": "",
"license": "Apache-2.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment