forked from yosuke-furukawa/node-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (23 loc) · 1.04 KB
/
index.js
File metadata and controls
27 lines (23 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const Module = require('./lib/libjsonnet');
const Jsonnet = function Jsonnet() {
this.libjsonnet = Module();
this.jsonnet_make = this.libjsonnet.cwrap('jsonnet_make', 'number', []);
this.jsonnet_realloc = this.libjsonnet.cwrap('jsonnet_realloc', 'number', ['number', 'number', 'number']);
this.jsonnet_evaluate_snippet = this.libjsonnet.cwrap('jsonnet_evaluate_snippet', 'number', ['number', 'string', 'string', 'number']);
this.jsonnet_destroy = this.libjsonnet.cwrap('jsonnet_destroy', 'number', ['number']);
};
module.exports = Jsonnet;
Jsonnet.prototype.eval = function ev(code) {
const vm = this.jsonnet_make();
const error_ptr = this.libjsonnet._malloc(4);
const output_ptr = this.jsonnet_evaluate_snippet(vm, 'snippet', code, error_ptr);
const error = this.libjsonnet.getValue(error_ptr, 'i32*');
this.libjsonnet._free(error_ptr);
const result = this.libjsonnet.UTF8ToString(output_ptr);
this.jsonnet_realloc(vm, output_ptr, 0);
this.jsonnet_destroy(vm);
if (error) {
throw result;
}
return JSON.parse(result);
};