Class: Jsonnet::VM

Inherits:
Data
  • Object
show all
Defined in:
lib/jsonnet/vm.rb,
ext/jsonnet/jsonnet.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



138
139
140
141
142
143
144
145
146
# File 'ext/jsonnet/jsonnet.c', line 138

static VALUE
vm_s_new(VALUE mod)
{
    struct jsonnet_vm_wrap *vm;
    VALUE self = TypedData_Make_Struct(cVM, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    vm->vm = jsonnet_make();
    vm->callback = Qnil;
    return self;
}

Instance Method Details

#debug_ast=(val) ⇒ Object



346
347
348
349
350
351
352
353
# File 'ext/jsonnet/jsonnet.c', line 346

static VALUE
vm_set_debug_ast(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_debug_ast(vm->vm, RTEST(val));
    return Qnil;
}

#evaluate(jsonnet, filename: "(jsonnet)", multi: false) ⇒ String

Note:

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.

Parameters:

  • jsonnet (String)

    Jsonnet source string. Must be encoded in an ASCII-compatible encoding.

  • filename (String) (defaults to: "(jsonnet)")

    filename of the source. Used in stacktrace.

  • multi (Boolean) (defaults to: false)

    enables multi-mode

Returns:

  • (String)

    a JSON representation of the evaluation result

Raises:



18
19
20
# File 'lib/jsonnet/vm.rb', line 18

def evaluate(jsonnet, filename: "(jsonnet)", multi: false)
  eval_snippet(jsonnet, filename, multi)
end

#evaluate_file(filename, encoding: Encoding.default_external, multi: false) ⇒ String

Note:

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.

Parameters:

  • filename (String)

    filename of a Jsonnet source file.

  • multi (Boolean) (defaults to: false)

    enables multi-mode

Returns:

  • (String)

    a JSON representation of the evaluation result

Raises:



33
34
35
# File 'lib/jsonnet/vm.rb', line 33

def evaluate_file(filename, encoding: Encoding.default_external, multi: false)
  eval_file(filename, encoding, multi)
end

#ext_var(key, val) ⇒ Object

Binds an external variable to a value.

Parameters:

  • key (String)

    name of the variable

  • val (String)

    the value



285
286
287
288
289
290
291
292
293
294
295
# File 'ext/jsonnet/jsonnet.c', line 285

static VALUE
vm_ext_var(VALUE self, VALUE key, VALUE val)
{
    struct jsonnet_vm_wrap *vm;

    enc_assert_asciicompat(StringValue(key));
    enc_assert_asciicompat(StringValue(val));
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_ext_var(vm->vm, StringValueCStr(key), StringValueCStr(val));
    return Qnil;
}

#gc_growth_trigger=(val) ⇒ Object



315
316
317
318
319
320
321
322
# File 'ext/jsonnet/jsonnet.c', line 315

static VALUE
vm_set_gc_growth_trigger(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_gc_growth_trigger(vm->vm, NUM2DBL(val));
    return Qnil;
}

#gc_min_objects=(val) ⇒ Object



306
307
308
309
310
311
312
313
# File 'ext/jsonnet/jsonnet.c', line 306

static VALUE
vm_set_gc_min_objects(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_gc_min_objects(vm->vm, NUM2UINT(val));
    return Qnil;
}

#handle_import {|base, rel| ... } ⇒ Object

Lets the given block handle “import” expression of Jsonnet.

Yield Parameters:

  • base (String)

    base path to resolve “rel” from.

  • rel (String)

    a relative or absolute path to the file to be imported

Yield Returns:

  • (Array<String>)

    a pair of the content of the imported file and its path.



43
44
45
46
# File 'lib/jsonnet/vm.rb', line 43

def handle_import
  self.import_callback = Proc.new
  nil
end

#import_callback=(callback) ⇒ Object

Sets a custom way to resolve “import” expression.

Parameters:

  • callback (#call)

    receives two parameters and returns two values. The first parameter “base” is a base directory to resolve “rel” from. The second parameter “rel” is an absolute or a relative path to the file to import. The first return value is the content of the imported file. The second return value is the resolved path of the imported file.



269
270
271
272
273
274
275
276
277
278
# File 'ext/jsonnet/jsonnet.c', line 269

static VALUE
vm_set_import_callback(VALUE self, VALUE callback)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);

    vm->callback = callback;
    jsonnet_import_callback(vm->vm, import_callback_thunk, vm);
    return callback;
}

#max_stack=(val) ⇒ Object



297
298
299
300
301
302
303
304
# File 'ext/jsonnet/jsonnet.c', line 297

static VALUE
vm_set_max_stack(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_max_stack(vm->vm, NUM2UINT(val));
    return Qnil;
}

#max_trace=(val) ⇒ Object



337
338
339
340
341
342
343
344
# File 'ext/jsonnet/jsonnet.c', line 337

static VALUE
vm_set_max_trace(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_max_trace(vm->vm, NUM2UINT(val));
    return Qnil;
}

#string_output=(val) ⇒ Object

Let #evalutae and #evaluate_file return a raw String instead of JSON-encoded string if val is true

Parameters:

  • val (Boolean)


328
329
330
331
332
333
334
335
# File 'ext/jsonnet/jsonnet.c', line 328

static VALUE
vm_set_string_output(VALUE self, VALUE val)
{
    struct jsonnet_vm_wrap *vm;
    TypedData_Get_Struct(self, struct jsonnet_vm_wrap, &jsonnet_vm_type, vm);
    jsonnet_string_output(vm->vm, RTEST(val));
    return Qnil;
}