-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Builds, and works, now to: - write tests - add support for builtin modules so we can `include "jq/prelude"` and so on I've tested this with the following in a file named jqmod.jq: module {"cfunctions":"jqmod"}; def add1: _add1; and this in a file named jqmod.c built like so: $ cc -shared -o jqmod.so -fPIC -O0 -ggdb3 jqmod.c -L $PWD/.libs -ljq -Wl,-rpath,$PWD/.libs $ cat jqmod.c \#include <stddef.h> \#include <stdlib.h> \#include <stdint.h> \#include "src/jv.h" \#include "src/jq.h" \#include "src/jq_plugin.h" \#define JQ_ABI 10700 /* 1.7.0 */ static jv f_add1(jq_state *jq, jv input) { if (jv_get_kind(input) == JV_KIND_NUMBER) return jv_number(jv_number_value(input) + 1); if (jv_get_kind(input) == JV_KIND_STRING) return jv_string_concat(input, jv_string("1")); if (jv_get_kind(input) == JV_KIND_ARRAY) return jv_array_append(input, jv_number(1)); return jv_invalid_with_msg(jv_string("add1 only works for numbers, strings, and arrays")); } struct cfunction my_cfuncs[] = { { (cfunction_ptr)f_add1, "_add1", 1 }, }; int jq_plugin_init(int abi, const char **err, const char **contents, size_t *contentssz, struct cfunction **cf, size_t *ncf) { *contents = *err = 0; *ncf = *contentssz = 0; *cf = 0; if (abi != JQ_ABI) { *err = "Wrong ABI version"; return 1; } *cf = my_cfuncs; *ncf = sizeof(my_cfuncs) / sizeof(my_cfuncs[0]); return 0; }
- Loading branch information
1 parent
9a0d5be
commit 83cc14b
Showing
5 changed files
with
228 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#ifndef JQ_PLUGIN_H | ||
#define JQ_PLUGIN_H | ||
|
||
#include <stddef.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
#define MAX_CFUNCTION_ARGS 10 | ||
typedef void (*cfunction_ptr)(); | ||
struct cfunction { | ||
cfunction_ptr fptr; | ||
const char* name; | ||
int nargs; | ||
}; | ||
|
||
typedef int (*jq_plugin_init_f) | ||
(int, /* jq plugin ABI version */ | ||
const char **, /* error string */ | ||
const char **, /* jq-coded module contents */ | ||
size_t *, /* jq-coded module contents size */ | ||
struct cfunction **, /* array of C-coded function descriptors */ | ||
size_t *); | ||
|
||
#define JQ_ABI 10700 /* 1.7.0 */ | ||
|
||
#endif /* JQ_PLUGIN_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters