Have Ruby interop call JSON.parse

Thanks to @besquared for showing me how to do this!

https://mobile.twitter.com/besquared/status/1588363108606541824
This commit is contained in:
Richard Feldman 2022-11-03 22:58:07 -04:00
parent a06ba9ad23
commit 8ebdc44a23
No known key found for this signature in database
GPG Key ID: F1F21AA5B1D9E43B

View File

@ -220,22 +220,9 @@ VALUE call_roc(VALUE self, VALUE rb_arg)
// Now that we've created our Ruby JSON string, we're no longer referencing the RocBytes.
decref((void *)&ret, alignof(uint8_t *));
// return the JSON string, so the Ruby code can call JSON.parse on it.
// I'd love to call JSON.parse on it here, but after a couple hours of trying unsuccessfully
// to call JSON.parse from C, I decided it's better to just require that the caller do it.
// This JSON approach is temporary anyway; it will all be replaced by proper glue in the future.
return returned_json;
VALUE json_module = rb_define_module("JSON");
// Call JSON.parse on the returned JSON, so we return a normal Ruby value - most likely a hash.
// return rb_funcall(rb_mJSON, rb_intern("parse"), 1, returned_json);
// TODO this doesn't work because rb_mJSON is not defined. Neither is mJSON, although
// it gets defined here - https://github.com/ruby/ruby/blob/62849b337988268b4209c58b6de02d0002c78988/ext/json/parser/parser.rl#L935
// if I could only figure out how to include parser.h in here!
//
// Seems like some combination of GET_PARSER, which creates a `json` variable - https://github.com/ruby/ruby/blob/1d170fdc6d0af128c9e5ea2d6082790d5885a4ae/ext/json/parser/parser.h#L53
// - and JSON_parse_value - https://github.com/ruby/ruby/blob/1d170fdc6d0af128c9e5ea2d6082790d5885a4ae/ext/json/parser/parser.h#L66
// wich is implemented in https://github.com/ruby/ruby/blob/62849b337988268b4209c58b6de02d0002c78988/ext/json/parser/parser.rl
// should work...but I can't figure out how to include parser.h here.
return rb_funcall(json_module, rb_intern("parse"), 1, returned_json);
}
void Init_demo()