Class: Jsonnet::VM
- Inherits:
-
Data
- Object
- Data
- Jsonnet::VM
- Defined in:
- lib/jsonnet/vm.rb,
ext/jsonnet/vm.c
Defined Under Namespace
Classes: UnsupportedOptionError
Class Method Summary collapse
-
.evaluate(snippet, options = {}) ⇒ String
Convenient method to evaluate a Jsonnet snippet.
-
.evaluate_file(filename, options = {}) ⇒ String
Convenient method to evaluate a Jsonnet file.
- .new(*args) ⇒ Object
Instance Method Summary collapse
-
#define_function(name, body = nil) { ... } ⇒ Object
Define a function (native extension) in the VM and let the given block handle the invocation of the function.
-
#evaluate(jsonnet, filename: "(jsonnet)", multi: false) ⇒ String
Evaluates Jsonnet source.
-
#evaluate_file(filename, encoding: Encoding.default_external, multi: false) ⇒ String
Evaluates Jsonnet file.
-
#ext_code(key, code) ⇒ Object
Binds an external variable to a code fragment.
-
#ext_var(key, val) ⇒ Object
Binds an external variable to a value.
- #gc_growth_trigger=(val) ⇒ Object
- #gc_min_objects=(val) ⇒ Object
-
#handle_import {|base, rel| ... } ⇒ Object
Lets the given block handle “import” expression of Jsonnet.
-
#import_callback=(callback) ⇒ Object
Sets a custom way to resolve “import” expression.
-
#initialize(options = {}) ⇒ VM
constructor
initializes a new VM with the given configuration.
-
#jpath_add(*args) ⇒ Object
Adds library search paths.
- #max_stack=(val) ⇒ Object
- #max_trace=(val) ⇒ Object
-
#string_output=(val) ⇒ Object
Let #evaluate and #evaluate_file return a raw String instead of JSON-encoded string if val is true.
-
#tla_code(key, code) ⇒ Object
Binds a top-level argument to a code fragment.
-
#tla_var(key, val) ⇒ Object
Binds a top-level argument to a value.
Constructor Details
#initialize(options = {}) ⇒ VM
initializes a new VM with the given configuration.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jsonnet/vm.rb', line 45 def initialize( = {}) .each do |key, value| method = "#{key}=" if respond_to?(method) public_send(method, value) else raise UnsupportedOptionError.new("Jsonnet VM does not support #{key} option") end end self end |
Class Method Details
.evaluate(snippet, options = {}) ⇒ String
Convenient method to evaluate a Jsonnet snippet.
It implicitly instantiates a VM and then evaluate Jsonnet with the VM.
15 16 17 18 19 20 |
# File 'lib/jsonnet/vm.rb', line 15 def evaluate(snippet, = {}) snippet_check = ->(key, value) { key.to_s.match(/^filename|multi$/) } = .select &snippet_check = .reject &snippet_check new().evaluate(snippet, ) end |
.evaluate_file(filename, options = {}) ⇒ String
Convenient method to evaluate a Jsonnet file.
It implicitly instantiates a VM and then evaluates Jsonnet with the VM.
31 32 33 34 35 36 |
# File 'lib/jsonnet/vm.rb', line 31 def evaluate_file(filename, = {}) file_check = ->(key, value) { key.to_s.match(/^encoding|multi$/) } = .select &file_check = .reject &file_check new().evaluate_file(filename, ) end |
.new(*args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'ext/jsonnet/vm.c', line 52 static VALUE vm_s_new(int argc, const VALUE *argv, VALUE klass) { struct jsonnet_vm_wrap *vm; VALUE self = TypedData_Make_Struct(cVM, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm); vm->vm = jsonnet_make(); vm->import_callback = Qnil; vm->native_callbacks.len = 0; vm->native_callbacks.contexts = NULL; rb_obj_call_init(self, argc, argv); return self; } |
Instance Method Details
#define_function(name, body = nil) { ... } ⇒ Object
Currently it cannot define keyword or optional paramters in Jsonnet. Also all the positional optional parameters of the body are interpreted as required parameters. And the body cannot have keyword, rest or keyword rest paramters.
Define a function (native extension) in the VM and let the given block handle the invocation of the function.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/jsonnet/vm.rb', line 115 def define_function(name, body = nil) body = body ? body.to_proc : Proc.new params = body.parameters.map.with_index do |(type, name), i| raise ArgumentError, "rest or keyword parameters are not allowed: #{type}" \ unless [:req, :opt].include? type name || "p#{i}" end register_native_callback(name.to_sym, to_method(body), params); end |
#evaluate(jsonnet, filename: "(jsonnet)", multi: false) ⇒ String
It is recommended to encode the source string in UTF-8 because Jsonnet expects it is ASCII-compatible, the result JSON string shall be UTF-8,16,32 according to RFC 7159 thus the only intersection between the requirements is UTF-8.
Evaluates Jsonnet source.
72 73 74 |
# File 'lib/jsonnet/vm.rb', line 72 def evaluate(jsonnet, filename: "(jsonnet)", multi: false) eval_snippet(jsonnet, filename, multi) end |
#evaluate_file(filename, encoding: Encoding.default_external, multi: false) ⇒ String
It is recommended to encode the source file in UTF-8 because Jsonnet expects it is ASCII-compatible, the result JSON string shall be UTF-8,16,32 according to RFC 7159 thus the only intersection between the requirements is UTF-8.
Evaluates Jsonnet file.
87 88 89 |
# File 'lib/jsonnet/vm.rb', line 87 def evaluate_file(filename, encoding: Encoding.default_external, multi: false) eval_file(filename, encoding, multi) end |
#ext_code(key, code) ⇒ Object
Binds an external variable to a code fragment.
165 166 167 168 169 170 |
# File 'ext/jsonnet/vm.c', line 165 static VALUE vm_ext_code(VALUE self, VALUE key, VALUE code) { vm_bind_variable(ext_code, self, key, code); return Qnil; } |
#ext_var(key, val) ⇒ Object
Binds an external variable to a value.
153 154 155 156 157 158 |
# File 'ext/jsonnet/vm.c', line 153 static VALUE vm_ext_var(VALUE self, VALUE key, VALUE val) { vm_bind_variable(ext_var, self, key, val); return Qnil; } |
#gc_growth_trigger=(val) ⇒ Object
229 230 231 232 233 234 235 |
# File 'ext/jsonnet/vm.c', line 229 static VALUE vm_set_gc_growth_trigger(VALUE self, VALUE val) { struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); jsonnet_gc_growth_trigger(vm->vm, NUM2DBL(val)); return Qnil; } |
#gc_min_objects=(val) ⇒ Object
221 222 223 224 225 226 227 |
# File 'ext/jsonnet/vm.c', line 221 static VALUE vm_set_gc_min_objects(VALUE self, VALUE val) { struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); jsonnet_gc_min_objects(vm->vm, NUM2UINT(val)); return Qnil; } |
#handle_import {|base, rel| ... } ⇒ Object
Lets the given block handle “import” expression of Jsonnet.
97 98 99 100 |
# File 'lib/jsonnet/vm.rb', line 97 def handle_import self.import_callback = to_method(Proc.new) nil end |
#import_callback=(callback) ⇒ Object
Sets a custom way to resolve “import” expression.
138 139 140 141 142 143 144 145 146 147 |
# File 'ext/jsonnet/callbacks.c', line 138 static VALUE vm_set_import_callback(VALUE self, VALUE callback) { struct jsonnet_vm_wrap *const vm = rubyjsonnet_obj_to_vm(self); vm->import_callback = callback; jsonnet_import_callback(vm->vm, import_callback_entrypoint, vm); return callback; } |
#jpath_add(*args) ⇒ Object
Adds library search paths
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'ext/jsonnet/vm.c', line 199 static VALUE vm_jpath_add_m(int argc, const VALUE *argv, VALUE self) { int i; struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); for (i = 0; i < argc; ++i) { VALUE jpath = argv[i]; FilePathValue(jpath); jsonnet_jpath_add(vm->vm, StringValueCStr(jpath)); } return Qnil; } |
#max_stack=(val) ⇒ Object
213 214 215 216 217 218 219 |
# File 'ext/jsonnet/vm.c', line 213 static VALUE vm_set_max_stack(VALUE self, VALUE val) { struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); jsonnet_max_stack(vm->vm, NUM2UINT(val)); return Qnil; } |
#max_trace=(val) ⇒ Object
250 251 252 253 254 255 256 |
# File 'ext/jsonnet/vm.c', line 250 static VALUE vm_set_max_trace(VALUE self, VALUE val) { struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); jsonnet_max_trace(vm->vm, NUM2UINT(val)); return Qnil; } |
#string_output=(val) ⇒ Object
Let #evaluate and #evaluate_file return a raw String instead of JSON-encoded string if val is true
242 243 244 245 246 247 248 |
# File 'ext/jsonnet/vm.c', line 242 static VALUE vm_set_string_output(VALUE self, VALUE val) { struct jsonnet_vm_wrap *vm = rubyjsonnet_obj_to_vm(self); jsonnet_string_output(vm->vm, RTEST(val)); return Qnil; } |
#tla_code(key, code) ⇒ Object
Binds a top-level argument to a code fragment.
189 190 191 192 193 194 |
# File 'ext/jsonnet/vm.c', line 189 static VALUE vm_tla_code(VALUE self, VALUE key, VALUE code) { vm_bind_variable(tla_code, self, key, code); return Qnil; } |
#tla_var(key, val) ⇒ Object
Binds a top-level argument to a value.
177 178 179 180 181 182 |
# File 'ext/jsonnet/vm.c', line 177 static VALUE vm_tla_var(VALUE self, VALUE key, VALUE val) { vm_bind_variable(tla_var, self, key, val); return Qnil; } |