You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.2 KiB

  1. From 4c5a08b9e01ebfce5c8914dd82c1722737bbecab Mon Sep 17 00:00:00 2001
  2. From: Muh Muhten <muh.muhten@gmail.com>
  3. Date: Tue, 19 Feb 2019 00:39:34 -0500
  4. Subject: [PATCH 8/9] Load ~/.jq as a library instead of with builtins
  5. Remove the special code which loads ~/.jq in builtin.c, and instead glue
  6. an optional include which points to the same file onto the main program
  7. in linker.c.
  8. Fixes a minor bug where errors in ~/.jq would be labelled <builtin>.
  9. ---
  10. src/builtin.c | 44 +++++++-------------------------------------
  11. src/linker.c | 10 ++++++++++
  12. 2 files changed, 17 insertions(+), 37 deletions(-)
  13. --- a/src/builtin.c
  14. +++ b/src/builtin.c
  15. @@ -1706,9 +1706,7 @@ static block bind_bytecoded_builtins(blo
  16. return block_bind(builtins, b, OP_IS_CALL_PSEUDO);
  17. }
  18. -
  19. -
  20. -static const char* const jq_builtins =
  21. +static const char jq_builtins[] =
  22. /* Include jq-coded builtins */
  23. #include "src/builtin.inc"
  24. @@ -1744,45 +1742,17 @@ static block gen_builtin_list(block buil
  25. return BLOCK(builtins, gen_function("builtins", gen_noop(), gen_const(list)));
  26. }
  27. -static int builtins_bind_one(jq_state *jq, block* bb, const char* code) {
  28. - struct locfile* src;
  29. - src = locfile_init(jq, "<builtin>", code, strlen(code));
  30. - block funcs;
  31. - int nerrors = jq_parse_library(src, &funcs);
  32. - if (nerrors == 0) {
  33. - *bb = block_bind(funcs, *bb, OP_IS_CALL_PSEUDO);
  34. - }
  35. - locfile_free(src);
  36. - return nerrors;
  37. -}
  38. -
  39. -static int slurp_lib(jq_state *jq, block* bb) {
  40. - int nerrors = 0;
  41. - char* home = getenv("HOME");
  42. - if (home) { // silently ignore no $HOME
  43. - jv filename = jv_string_append_str(jv_string(home), "/.jq");
  44. - jv data = jv_load_file(jv_string_value(filename), 1);
  45. - if (jv_is_valid(data)) {
  46. - nerrors = builtins_bind_one(jq, bb, jv_string_value(data) );
  47. - }
  48. - jv_free(filename);
  49. - jv_free(data);
  50. - }
  51. - return nerrors;
  52. -}
  53. -
  54. int builtins_bind(jq_state *jq, block* bb) {
  55. - block builtins = gen_noop();
  56. - int nerrors = slurp_lib(jq, bb);
  57. - if (nerrors) {
  58. - block_free(*bb);
  59. - return nerrors;
  60. - }
  61. - nerrors = builtins_bind_one(jq, &builtins, jq_builtins);
  62. + block builtins;
  63. + struct locfile* src = locfile_init(jq, "<builtin>", jq_builtins, sizeof(jq_builtins)-1);
  64. + int nerrors = jq_parse_library(src, &builtins);
  65. assert(!nerrors);
  66. + locfile_free(src);
  67. +
  68. builtins = bind_bytecoded_builtins(builtins);
  69. builtins = gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), builtins);
  70. builtins = gen_builtin_list(builtins);
  71. +
  72. *bb = block_bind(builtins, *bb, OP_IS_CALL_PSEUDO);
  73. *bb = block_drop_unreferenced(*bb);
  74. return nerrors;
  75. --- a/src/linker.c
  76. +++ b/src/linker.c
  77. @@ -387,6 +387,16 @@ int load_program(jq_state *jq, struct lo
  78. if (nerrors)
  79. return nerrors;
  80. + char* home = getenv("HOME");
  81. + if (home) { // silently ignore no $HOME
  82. + /* Import ~/.jq as a library named "" found in $HOME */
  83. + block import = gen_import_meta(gen_import("", NULL, 0),
  84. + gen_const(JV_OBJECT(
  85. + jv_string("optional"), jv_true(),
  86. + jv_string("search"), jv_string(home))));
  87. + program = BLOCK(import, program);
  88. + }
  89. +
  90. nerrors = process_dependencies(jq, jq_get_jq_origin(jq), jq_get_prog_origin(jq), &program, &lib_state);
  91. block libs = gen_noop();
  92. for (uint64_t i = 0; i < lib_state.ct; ++i) {