Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make module context-aware #11

Merged
merged 1 commit into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/lzo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ lzo_uint decompress(const unsigned char *input, unsigned char *output, lzo_uint
}

void js_compress(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);

Handle<Object> inputBuffer = args[0]->ToObject();
Expand All @@ -54,7 +54,7 @@ void js_compress(const v8::FunctionCallbackInfo<Value>& args) {
}

void js_decompress(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);

Handle<Object> inputBuffer = args[0]->ToObject();
Expand All @@ -81,8 +81,8 @@ void js_decompress(const v8::FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}

void Init(Handle<Object> exports) {
Isolate *isolate = Isolate::GetCurrent();
void Init(Local<Object> exports, Local<Context> context) {
Isolate* isolate = context->GetIsolate();

int init_result = lzo_init();

Expand Down Expand Up @@ -116,4 +116,7 @@ void Init(Handle<Object> exports) {
String::NewFromUtf8(isolate, lzo_version_date()));
}

NODE_MODULE(node_lzo, Init);
// Initialize this addon to be context-aware.
NODE_MODULE_INIT(/* exports, module, context */) {
Init(exports, context);
}
19 changes: 14 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const randChar = () => String.fromCharCode(Math.random() * 1000);
const toBuf = a => Buffer.from(a);

describe('Compression/Decompression', () => {
it('lzo.compress should throw if nothing is passed', () =>
it('lzo.compress should throw if nothing is passed', () =>
expect(() => lzo.decompress()).to.throw() );

it('lzo.decompress throw if nothing is passed', () =>
it('lzo.decompress throw if nothing is passed', () =>
expect(() => lzo.decompress()).to.throw() );

it('Decompressed date should be the same as the initial input', () => {
Expand All @@ -30,12 +30,21 @@ describe('Compression/Decompression', () => {
});

describe('Properties', () => {
it('Should have property \'version\'', () =>
it('Should have property \'version\'', () =>
expect(lzo).to.have.ownProperty('version') );

it('Should have property \'versionDate\'', () =>
it('Should have property \'versionDate\'', () =>
expect(lzo).to.have.ownProperty('versionDate') );

it('Should have property \'errors\' (lzo error codes)', () =>
expect(lzo).to.have.ownProperty('errors') );
});
});

describe('Module loaded in multiple contexts', () => {
it('Should not throw an error when module is loaded multiple times', () => {
delete require.cache[require.resolve('..')];
delete require.cache[require.resolve('../build/Release/node_lzo.node')];
const lzo2 = require('..');
expect(() => lzo2.compress(toBuf(arr(sampleSize)))).to.not.throw();
});
});