Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support jsonnet >= v0.19.0 #38

Merged
merged 1 commit into from
Sep 7, 2024
Merged

Support jsonnet >= v0.19.0 #38

merged 1 commit into from
Sep 7, 2024

Conversation

eagletmt
Copy link
Collaborator

jsonnet v0.19.0 changed the C API for import callbacks. https://github.com/google/jsonnet/releases/tag/v0.19.0

This PR should fix #37

@eagletmt eagletmt force-pushed the jsonnet-0-20 branch 2 times, most recently from cceee4b to 204db86 Compare August 27, 2024 03:35
*found_here = rubyjsonnet_str_to_cstr(vm->vm, rb_ary_entry(result, 1));
#ifdef HAVE_JSONNET_IMPORT_CALLBACK_0_19
*buf = rubyjsonnet_str_to_cstr(vm->vm, rb_ary_entry(result, 0));
Copy link
Collaborator

@kaorimatz kaorimatz Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are expected to return arbitrary bytes instead of c string (null terminated) with buf because importbin may import data containing null bytes, so we may need another helper function like below.

char *
rubyjsonnet_str_to_ptr(struct JsonnetVm *vm, VALUE str)
{
    const char *const ptr = StringValuePtr(str);
    char *const buf = jsonnet_realloc(vm, NULL, RSTRING_LEN(str));
    memcpy(buf, ptr, RSTRING_LEN(str));
    return buf;
}

https://github.com/google/jsonnet/blob/v0.19.0/include/libjsonnet.h#L72

before:

irb(main):001:0> vm = Jsonnet::VM.new
=> #<Jsonnet::VM:0x0000753e9e129b50>
irb(main):002:0> vm.import_callback = ->(base, rel) { ["\x0\x1", base + rel] }
=> #<Proc:0x0000753e9f670310 (irb):2 (lambda)>
irb(main):003:0> vm.evaluate('importbin "foo"')
/path/to/ruby-jsonnet/lib/jsonnet/vm.rb:73:in `eval_snippet': string contains null byte (ArgumentError)

after:

irb(main):001:0> vm = Jsonnet::VM.new
=> #<Jsonnet::VM:0x00007690148dc280>
irb(main):002:0> vm.import_callback = ->(base, rel) { ["\x0\x1", base + rel] }
=> #<Proc:0x0000769016a248e8 (irb):2 (lambda)>
irb(main):003:0> vm.evaluate('importbin "foo"')
=> "[\n   0,\n   1\n]\n"

@yugui yugui merged commit e26ca9b into yugui:master Sep 7, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot install jsonnet gem
3 participants