Skip to content

Commit

Permalink
Merge pull request #15 from bauman/duk-module
Browse files Browse the repository at this point in the history
duktape javascript module tests
  • Loading branch information
bauman authored Jan 25, 2020
2 parents 7b9b482 + c7b5cad commit 56936e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/matcher-module-duk.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static mongoc_matcher_module_callback_t
duk_make_the_matches_call(matcher_container_duk_holder_t *md){
mongoc_matcher_module_callback_t cb = MATCHER_MODULE_CALLBACK_CONTINUE;
if (duk_pcall(md->ctx, 1 /*nargs*/) != 0) {
// I don't like introducing writing stderr.
// no better ideas though :-(
fprintf(stderr, "%s\n", duk_safe_to_string(md->ctx, -1));
cb = MATCHER_MODULE_CALLBACK_CONTINUE;
} else {
Expand Down Expand Up @@ -76,6 +78,19 @@ matcher_module_duk_prep(mongoc_matcher_op_t *op){
return (void*)ud;
}


static void
matcher_module_duk_free_son(matcher_container_duk_usermem_t* ud){
if (ud->bson_data){
bson_free(ud->bson_data);
ud->bson_data = NULL;
}
if (ud->json_data){
bson_free(ud->json_data);
ud->json_data = NULL;
}
}

mongoc_matcher_module_callback_t
matcher_module_duk_search(mongoc_matcher_op_t * op, bson_iter_t * iter, void * usermem){
mongoc_matcher_module_callback_t cb = MATCHER_MODULE_CALLBACK_CONTINUE;
Expand All @@ -87,7 +102,6 @@ matcher_module_duk_search(mongoc_matcher_op_t * op, bson_iter_t * iter, void * u
ud = (matcher_container_duk_usermem_t*) usermem;

// DO THE SEARCH
// usrmem ddb_entry is exactly like bson_binary any binary string and a length
if (iter){
bson_type_t btype = bson_iter_type(iter);
duk_push_global_object(md->ctx);
Expand Down Expand Up @@ -115,7 +129,7 @@ matcher_module_duk_search(mongoc_matcher_op_t * op, bson_iter_t * iter, void * u
break;
}
cb = duk_make_the_matches_call(md);

matcher_module_duk_free_son(ud);
// --Consider move to a "value_only" static function?
// ----------------------------------------------------------------------
} else {
Expand All @@ -132,14 +146,7 @@ matcher_module_duk_cleanup(mongoc_matcher_op_t *op, void * usermem){
mongoc_matcher_module_callback_t cb = MATCHER_MODULE_CALLBACK_CONTINUE;
matcher_container_duk_usermem_t *ud;
ud = (matcher_container_duk_usermem_t*) usermem;
if (ud->bson_data){
bson_free(ud->bson_data);
ud->bson_data = NULL;
}
if (ud->json_data){
bson_free(ud->json_data);
ud->json_data = NULL;
}
matcher_module_duk_free_son(ud);
bson_free(usermem);
return cb;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/tests/bsonsearch_duk.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,24 @@ main (int argc,
BSON_ASSERT(compare_json("{\"hello\": {\"world\":{\"a\": \"variable\"}}}",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n d = JSON.parse(data);\\n return d.a == 'variable' ;\\n}\\n\"}}}}}"));

BSON_ASSERT(compare_json("{\"hello\": [{\"world\":\"not the right string variable\"}, {\"world\":\"variable\"}]}",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n return data == 'variable' ;\\n}\\n\"}}}}}"));

BSON_ASSERT(compare_json("{\"hello\": [ {\"world\":{\"a\": \"not the right variable\"}}, {\"world\":{\"a\": \"variable\"}} ] }",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n d = JSON.parse(data);\\n return d.a == 'variable' ;\\n}\\n\"}}}}}"));

BSON_ASSERT(!compare_json("{\"hello\": [ {\"world\":{\"a\": \"not the right variable\"}}, {\"nottherightkeybutgoodvalue\":{\"a\": \"variable\"}}, {\"world\":{\"a\": \"another wrong value\"}} ] }",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n d = JSON.parse(data);\\n return d.a == 'variable' ;\\n}\\n\"}}}}}"));


/////////////////////////////////
// This one is arguable, for now, leaving this to the matcher author to deal with.
BSON_ASSERT(!compare_json("{\"hello\": [ {\"world\":{\"a\": \"not the right variable\"}}, {\"world\": \"variable\"} ] }",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n d = JSON.parse(data);\\n return d.a == 'variable' ;\\n}\\n\"}}}}}"));
// Notice the actual javascript change that accounts for both cases and successfully matches
BSON_ASSERT(compare_json("{\"hello\": [ {\"world\":{\"a\": \"not the right variable\"}}, {\"world\": \"variable\"} ] }",
"{\"hello.world\":{\"$module\":{\"name\":\"dukjs\", \"config\":{\"entrypoint\": \"matches\", \"code\": {\"$code\": \"\\nfunction matches(data) {\\n try { d = JSON.parse(data); return d.a == 'variable'; } catch (e) { return data == 'variable'; }\\n}\\n\"}}}}}"));
/////////////////////////////////
rounds--;
} while (rounds > 0);

Expand Down

0 comments on commit 56936e7

Please sign in to comment.