Skip to content

Commit cffda8e

Browse files
committed
batch() returns err on any null/undef key/value
fixes facebook#19
1 parent 932f3c3 commit cffda8e

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

src/database.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ v8::Handle<v8::Value> Database::Put (const v8::Arguments& args) {
214214

215215
LD_METHOD_SETUP_COMMON(put, 2, 3)
216216

217-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
218-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(1, Value)
217+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)
218+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(1, Value)
219219

220220
v8::Local<v8::Value> keyBufferV = args[0];
221221
v8::Local<v8::Value> valueBufferV = args[1];
@@ -248,7 +248,7 @@ v8::Handle<v8::Value> Database::Get (const v8::Arguments& args) {
248248

249249
LD_METHOD_SETUP_COMMON(put, 1, 2)
250250

251-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
251+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)
252252

253253
v8::Local<v8::Value> keyBufferV = args[0];
254254
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
@@ -276,7 +276,7 @@ v8::Handle<v8::Value> Database::Delete (const v8::Arguments& args) {
276276

277277
LD_METHOD_SETUP_COMMON(put, 1, 2)
278278

279-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
279+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Key)
280280

281281
v8::Local<v8::Value> keyBufferV = args[0];
282282
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
@@ -320,10 +320,11 @@ v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
320320
continue;
321321

322322
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(array->Get(i));
323-
if (!obj->Has(str_type) || !obj->Has(str_key))
324-
continue;
323+
324+
LD_CB_ERR_IF_NULL_OR_UNDEFINED(obj->Get(str_type), type)
325325

326326
v8::Local<v8::Value> keyBuffer = obj->Get(str_key);
327+
LD_CB_ERR_IF_NULL_OR_UNDEFINED(keyBuffer, key)
327328

328329
if (obj->Get(str_type)->StrictEquals(str_del)) {
329330
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, Key)
@@ -332,8 +333,9 @@ v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
332333
key
333334
, v8::Persistent<v8::Value>::New(keyBuffer)
334335
));
335-
} else if (obj->Get(str_type)->StrictEquals(str_put) && obj->Has(str_value)) {
336+
} else if (obj->Get(str_type)->StrictEquals(str_put)) {
336337
v8::Local<v8::Value> valueBuffer = obj->Get(str_value);
338+
LD_CB_ERR_IF_NULL_OR_UNDEFINED(valueBuffer, value)
337339

338340
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, Key)
339341
LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, Value)
@@ -377,8 +379,8 @@ v8::Handle<v8::Value> Database::ApproximateSize (const v8::Arguments& args) {
377379

378380
LD_METHOD_SETUP_COMMON(approximateSize, -1, 2)
379381

380-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(0, Start)
381-
LD_CB_ERR_IF_NULL_OR_UNDEFINED(1, End)
382+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(0, Start)
383+
LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(1, End)
382384

383385
LD_STRING_OR_BUFFER_TO_SLICE(start, startBufferV, Start)
384386
LD_STRING_OR_BUFFER_TO_SLICE(end, endBufferV, End)

src/leveldown.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@
1212
#define LD_V8_METHOD(name) \
1313
static v8::Handle<v8::Value> name (const v8::Arguments& args);
1414

15-
#define LD_CB_ERR_IF_NULL_OR_UNDEFINED(index, name) \
16-
if (args[index]->IsNull() || args[index]->IsUndefined()) { \
15+
#define LD_CB_ERR_IF_NULL_OR_UNDEFINED(thing, name) \
16+
if (thing->IsNull() || thing->IsUndefined()) { \
1717
v8::Local<v8::Value> argv[] = { \
1818
v8::Local<v8::Value>::New(v8::Exception::Error( \
19-
v8::String::New(#name " argument cannot be `null` or `undefined`")) \
19+
v8::String::New(#name " cannot be `null` or `undefined`")) \
2020
) \
2121
}; \
2222
LD_RUN_CALLBACK(callback, argv, 1); \
2323
return v8::Undefined(); \
2424
}
2525

26+
#define LD_CB_ERR_IF_OPTION_NULL_OR_UNDEFINED(index, name) \
27+
LD_CB_ERR_IF_NULL_OR_UNDEFINED(args[index], name argument)
28+
2629
#define LD_FROM_V8_STRING(to, from) \
2730
size_t to ## Sz_; \
2831
char* to; \

test/batch-test.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,35 @@ test('test simple batch()', function (t) {
5252

5353
test('test batch() with missing `value`', function (t) {
5454
db.batch([{ type: 'put', key: 'foo1' }], function (err) {
55-
t.notOk(err, 'no error')
55+
t.like(err.message, /value cannot be `null` or `undefined`/, 'correct error message')
56+
t.end()
57+
})
58+
})
5659

57-
db.get('foo1', function (err, value) {
58-
t.ok(err, 'entry not found')
59-
t.notOk(value, 'value not returned')
60-
t.like(err.message, /NotFound/)
61-
t.end()
62-
})
60+
test('test batch() with null `value`', function (t) {
61+
db.batch([{ type: 'put', key: 'foo1', value: null }], function (err) {
62+
t.like(err.message, /value cannot be `null` or `undefined`/, 'correct error message')
63+
t.end()
6364
})
6465
})
6566

6667
test('test batch() with missing `key`', function (t) {
6768
db.batch([{ type: 'put', value: 'foo1' }], function (err) {
68-
t.notOk(err, 'no error')
69+
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
70+
t.end()
71+
})
72+
})
73+
74+
test('test batch() with null `key`', function (t) {
75+
db.batch([{ type: 'put', key: null, value: 'foo1' }], function (err) {
76+
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
6977
t.end()
7078
})
7179
})
7280

7381
test('test batch() with missing `key` and `value`', function (t) {
7482
db.batch([{ type: 'put' }], function (err) {
75-
t.notOk(err, 'no error')
83+
t.like(err.message, /key cannot be `null` or `undefined`/, 'correct error message')
7684
t.end()
7785
})
7886
})

0 commit comments

Comments
 (0)