jsonnet 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b21144880989556e02da3a182e6d527b7038aa090917a9f52563c77533348736
4
- data.tar.gz: 5326728e4f910249f23cd7f26ccebab4630da2f13dc3f1a9d1bb15e000aaffc2
3
+ metadata.gz: d4340172a08a949087e54c213842fe9a3ee445ec9149bc39f8dc56371d2199f6
4
+ data.tar.gz: 1e068cb049eda5218e0a49984bd581e22147f3d98f4db11a2d0fb082490e5001
5
5
  SHA512:
6
- metadata.gz: 8423047526b7bcda8f25501bfa8b937cf585786982d477053445230877b4dc7ea0cff3d398abcc6cdacddbc8387611a187f9760ae7290034dbb4b157768cd833
7
- data.tar.gz: 36f6a67d475dbddbc356fe42a9d164af6dabdf7d9be49dfb19b127413f24e0458aa7c8ae3306100a9f1f031b933a0346874137fb1589555bc35cc6625d291728
6
+ metadata.gz: 9d532a2079abd6d4612f0f14bed63ed245302478251dedff199f02572a5e7f5400c07c76f9bf649da67607ad1c904cc49089a3c481614a90d36163427dd91e01
7
+ data.tar.gz: d2d41cca8cab085b6fe38fda48886706bb2f8876ff1f4e557e9310fd955b11f5784001f11c93c3bb509bfb6df542fced240733cd64478e3bf69c5ae21cbf094d
@@ -14,7 +14,7 @@ jobs:
14
14
  name: Test
15
15
  strategy:
16
16
  matrix:
17
- ruby-version: ['2.7', '3.0', '3.1']
17
+ ruby-version: ['3.0', '3.1', '3.2', '3.3']
18
18
  os: ['ubuntu-latest', 'macos-latest']
19
19
  runs-on: ${{ matrix.os }}
20
20
 
data/README.md CHANGED
@@ -63,7 +63,7 @@ gem install jsonnet -- --use-system-libraries
63
63
 
64
64
  Load the library with `require "jsonnet"`
65
65
 
66
- You can evaluate a string of Jsonnet using `Jsonnet.parse`
66
+ You can evaluate a string of Jsonnet using `Jsonnet.evaluate`
67
67
 
68
68
  ```
69
69
  irb(main):002:0> Jsonnet.evaluate('{ foo: "bar" }')
@@ -11,6 +11,14 @@
11
11
  */
12
12
  #define RUBYJSONNET_GLOBAL_ESCAPE_MAGIC "\x07\x03\x0c:rubytag:\x07\x03\x0c:"
13
13
 
14
+ /* Copied from vm_core.h in Ruby. State variables in global escapes have
15
+ * this value when an exception is raised.
16
+ *
17
+ * TODO(yugui) Find a better way to distinguish "raise" from "throw".
18
+ * It is not a very good idea to depend on the implementation details of Ruby.
19
+ */
20
+ #define RUBY_TAG_RAISE 0x6
21
+
14
22
  /*
15
23
  * callback support in VM
16
24
  */
@@ -44,9 +52,9 @@ invoke_callback(VALUE args)
44
52
  static VALUE
45
53
  rescue_callback(int state, const char *fmt, ...)
46
54
  {
47
- VALUE err = rb_errinfo();
48
- if (rb_obj_is_kind_of(err, rb_eException)) {
49
- VALUE msg = rb_protect(rubyjsonnet_format_exception, rb_errinfo(), NULL);
55
+ if (state == RUBY_TAG_RAISE) {
56
+ VALUE err = rb_errinfo();
57
+ VALUE msg = rb_protect(rubyjsonnet_format_exception, err, NULL);
50
58
  if (msg == Qnil) {
51
59
  va_list ap;
52
60
  va_start(ap, fmt);
@@ -96,9 +104,15 @@ rubyjsonnet_jump_tag(const char *exc_mesg)
96
104
  return 0;
97
105
  }
98
106
 
107
+ #ifdef HAVE_JSONNET_IMPORT_CALLBACK_0_19
108
+ static int
109
+ import_callback_entrypoint(void *ctx, const char *base, const char *rel, char **found_here,
110
+ char **buf, size_t *buflen)
111
+ #else
99
112
  static char *
100
113
  import_callback_entrypoint(void *ctx, const char *base, const char *rel, char **found_here,
101
114
  int *success)
115
+ #endif
102
116
  {
103
117
  struct jsonnet_vm_wrap *const vm = (struct jsonnet_vm_wrap *)ctx;
104
118
  int state;
@@ -115,14 +129,24 @@ import_callback_entrypoint(void *ctx, const char *base, const char *rel, char **
115
129
 
116
130
  if (state) {
117
131
  VALUE msg = rescue_callback(state, "cannot import %s from %s", rel, base);
132
+ #ifdef HAVE_JSONNET_IMPORT_CALLBACK_0_19
133
+ *buf = rubyjsonnet_str_to_ptr(vm->vm, msg, buflen);
134
+ return 1;
135
+ #else
118
136
  *success = 0;
119
137
  return rubyjsonnet_str_to_cstr(vm->vm, msg);
138
+ #endif
120
139
  }
121
140
 
122
141
  result = rb_Array(result);
123
- *success = 1;
124
142
  *found_here = rubyjsonnet_str_to_cstr(vm->vm, rb_ary_entry(result, 1));
143
+ #ifdef HAVE_JSONNET_IMPORT_CALLBACK_0_19
144
+ *buf = rubyjsonnet_str_to_ptr(vm->vm, rb_ary_entry(result, 0), buflen);
145
+ return 0;
146
+ #else
147
+ *success = 1;
125
148
  return rubyjsonnet_str_to_cstr(vm->vm, rb_ary_entry(result, 0));
149
+ #endif
126
150
  }
127
151
 
128
152
  /*
@@ -13,8 +13,8 @@ unless using_system_libraries?
13
13
  require 'mini_portile2'
14
14
  message "Using mini_portile version #{MiniPortile::VERSION}\n"
15
15
 
16
- recipe = MiniPortile.new('jsonnet', 'v0.18.0')
17
- recipe.files = ['https://github.com/google/jsonnet/archive/v0.18.0.tar.gz']
16
+ recipe = MiniPortile.new('jsonnet', 'v0.20.0')
17
+ recipe.files = ['https://github.com/google/jsonnet/archive/v0.20.0.tar.gz']
18
18
  class << recipe
19
19
  CORE_OBJS = %w[
20
20
  desugarer.o formatter.o lexer.o libjsonnet.o parser.o pass.o static_analysis.o string_utils.o vm.o
@@ -89,4 +89,21 @@ end
89
89
  abort 'libjsonnet.h not found' unless have_header('libjsonnet.h')
90
90
  abort 'libjsonnet not found' unless have_library('jsonnet')
91
91
  have_header('libjsonnet_fmt.h')
92
+
93
+ import_callback_0_19 = checking_for checking_message('JsonnetImportCallback >= v0.19.0') do
94
+ try_compile(<<SRC, '-Werror=incompatible-pointer-types')
95
+ #include <libjsonnet.h>
96
+
97
+ int f(void *ctx, const char *base, const char *rel, char **found_here, char **buf, size_t *buflen);
98
+
99
+ int main() {
100
+ jsonnet_import_callback(NULL, f, NULL);
101
+ return 0;
102
+ }
103
+ SRC
104
+ end
105
+ if import_callback_0_19
106
+ $defs.push('-DHAVE_JSONNET_IMPORT_CALLBACK_0_19')
107
+ end
108
+
92
109
  create_makefile('jsonnet/jsonnet_wrap')
@@ -34,6 +34,13 @@ rubyjsonnet_assert_asciicompat(VALUE str)
34
34
 
35
35
  /**
36
36
  * Allocates a C string whose content is equal to \c str with jsonnet_realloc.
37
+ *
38
+ * Note that this function does not allow NUL characters in the string.
39
+ * You should use rubyjsonnet_str_to_ptr() if you want to handle NUL characters.
40
+ *
41
+ * @param[in] vm a Jsonnet VM
42
+ * @param[in] str a String-like object
43
+ * @return the allocated C string
37
44
  */
38
45
  char *
39
46
  rubyjsonnet_str_to_cstr(struct JsonnetVm *vm, VALUE str)
@@ -44,6 +51,25 @@ rubyjsonnet_str_to_cstr(struct JsonnetVm *vm, VALUE str)
44
51
  return buf;
45
52
  }
46
53
 
54
+ /**
55
+ * Allocates a byte sequence whose content is equal to \c str with jsonnet_realloc.
56
+ *
57
+ * @param[in] vm a Jsonnet VM
58
+ * @param[in] str a String-like object
59
+ * @param[out] buflen the length of the allocated buffer
60
+ * @return the allocated buffer
61
+ */
62
+ char *
63
+ rubyjsonnet_str_to_ptr(struct JsonnetVm *vm, VALUE str, size_t *buflen)
64
+ {
65
+ StringValue(str);
66
+ size_t len = RSTRING_LEN(str);
67
+ char *buf = jsonnet_realloc(vm, NULL, len);
68
+ memcpy(buf, RSTRING_PTR(str), len);
69
+ *buflen = len;
70
+ return buf;
71
+ }
72
+
47
73
  /**
48
74
  * @return a human readable string which contains the class name of the
49
75
  * exception and its message. It might be nil on failure
@@ -33,6 +33,7 @@ struct JsonnetJsonValue *rubyjsonnet_obj_to_json(struct JsonnetVm *vm, VALUE obj
33
33
 
34
34
  rb_encoding *rubyjsonnet_assert_asciicompat(VALUE str);
35
35
  char *rubyjsonnet_str_to_cstr(struct JsonnetVm *vm, VALUE str);
36
+ char *rubyjsonnet_str_to_ptr(struct JsonnetVm *vm, VALUE str, size_t *buflen);
36
37
  VALUE rubyjsonnet_format_exception(VALUE exc);
37
38
  int rubyjsonnet_jump_tag(const char *exc_mesg);
38
39
 
data/ext/jsonnet/vm.c CHANGED
@@ -59,16 +59,15 @@ rubyjsonnet_obj_to_vm(VALUE wrap)
59
59
  }
60
60
 
61
61
  static VALUE
62
- vm_s_new(int argc, const VALUE *argv, VALUE klass)
62
+ vm_s_allocate(VALUE klass)
63
63
  {
64
64
  struct jsonnet_vm_wrap *vm;
65
- VALUE self = TypedData_Make_Struct(cVM, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
65
+ VALUE self = TypedData_Make_Struct(klass, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
66
66
  vm->vm = jsonnet_make();
67
67
  vm->import_callback = Qnil;
68
68
  vm->native_callbacks.len = 0;
69
69
  vm->native_callbacks.contexts = NULL;
70
70
 
71
- rb_obj_call_init(self, argc, argv);
72
71
  return self;
73
72
  }
74
73
 
@@ -81,11 +80,10 @@ vm_free(void *ptr)
81
80
 
82
81
  for (i = 0; i < vm->native_callbacks.len; ++i) {
83
82
  struct native_callback_ctx *ctx = vm->native_callbacks.contexts[i];
84
- RB_REALLOC_N(ctx, struct native_callback_ctx, 0);
83
+ xfree(ctx);
85
84
  }
86
- RB_REALLOC_N(vm->native_callbacks.contexts, struct native_callback_ctx *, 0);
87
-
88
- RB_REALLOC_N(vm, struct jsonnet_vm_wrap, 0);
85
+ xfree(vm->native_callbacks.contexts);
86
+ xfree(vm);
89
87
  }
90
88
 
91
89
  static void
@@ -390,7 +388,7 @@ void
390
388
  rubyjsonnet_init_vm(VALUE mJsonnet)
391
389
  {
392
390
  cVM = rb_define_class_under(mJsonnet, "VM", rb_cObject);
393
- rb_define_singleton_method(cVM, "new", vm_s_new, -1);
391
+ rb_define_alloc_func(cVM, vm_s_allocate);
394
392
  rb_define_private_method(cVM, "eval_file", vm_evaluate_file, 3);
395
393
  rb_define_private_method(cVM, "eval_snippet", vm_evaluate, 3);
396
394
  rb_define_private_method(cVM, "fmt_file", vm_fmt_file, 2);
@@ -1,3 +1,3 @@
1
1
  module Jsonnet
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.0"
3
3
  end
data/test/test_vm.rb CHANGED
@@ -54,7 +54,6 @@ class TestVM < Test::Unit::TestCase
54
54
  end
55
55
  end
56
56
 
57
-
58
57
  test 'Jsonnet::VM#evaluate evaluates snippet' do
59
58
  vm = Jsonnet::VM.new
60
59
  result = vm.evaluate(<<-EOS, filename: 'example.snippet')
@@ -288,6 +287,26 @@ class TestVM < Test::Unit::TestCase
288
287
  assert_equal expected, JSON.parse(result)
289
288
  end
290
289
 
290
+ test "Jsonnet::VM#import_callback allows NUL char in Jsonnet v0.19 or later" do
291
+ return unless Jsonnet.libversion >= "v0.19"
292
+
293
+ example_str = "\x0\x1".force_encoding(Encoding::BINARY)
294
+
295
+ vm = Jsonnet::VM.new
296
+ vm.import_callback = ->(base, rel) {
297
+ case [base, rel]
298
+ when ['/path/to/base/', 'foo.bin']
299
+ return "\x0\x1".force_encoding(Encoding::BINARY), '/path/to/base/foo.bin'
300
+ else
301
+ raise Errno::ENOENT, "#{rel} at #{base}"
302
+ end
303
+ }
304
+ result = vm.evaluate(<<-EOS, filename: "/path/to/base/example.jsonnet")
305
+ importbin "foo.bin"
306
+ EOS
307
+ assert_equal [0, 1], JSON.parse(result)
308
+ end
309
+
291
310
  test "Jsonnet::VM#evaluate returns an error if customized import callback raises an exception" do
292
311
  vm = Jsonnet::VM.new
293
312
  called = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Yugui Sonoda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-24 00:00:00.000000000 Z
11
+ date: 2024-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_portile2
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  requirements: []
133
- rubygems_version: 3.3.7
133
+ rubygems_version: 3.4.1
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Jsonnet library