diff --git a/JS/jsonnet/src/jsonnet_wasm.d.ts b/JS/jsonnet/src/jsonnet_wasm.d.ts index 8eb8f753c..01f7635ab 100644 --- a/JS/jsonnet/src/jsonnet_wasm.d.ts +++ b/JS/jsonnet/src/jsonnet_wasm.d.ts @@ -1,29 +1,29 @@ /* tslint:disable */ /* eslint-disable */ /** - * @returns {number} - */ +* @returns {number} +*/ export function jsonnet_make(): number; /** - * @param {number} vm - */ +* @param {number} vm +*/ export function jsonnet_destroy(vm: number): void; /** - * @param {number} vm - * @param {string} filename - * @param {string} snippet - * @returns {string} - */ +* @param {number} vm +* @param {string} filename +* @param {string} snippet +* @returns {string} +*/ export function jsonnet_evaluate_snippet(vm: number, filename: string, snippet: string): string; /** - * @param {number} vm - * @param {string} filename - * @returns {string} - */ +* @param {number} vm +* @param {string} filename +* @returns {string} +*/ export function jsonnet_evaluate_file(vm: number, filename: string): string; /** - * @param {number} vm - * @param {string} key - * @param {string} value - */ +* @param {number} vm +* @param {string} key +* @param {string} value +*/ export function ext_string(vm: number, key: string, value: string): void; diff --git a/JS/jsonnet/src/jsonnet_wasm.js b/JS/jsonnet/src/jsonnet_wasm.js index 9c893b553..e9cd70f58 100644 --- a/JS/jsonnet/src/jsonnet_wasm.js +++ b/JS/jsonnet/src/jsonnet_wasm.js @@ -3,7 +3,7 @@ import * as imports from "./jsonnet_wasm_bg.js"; // switch between both syntax for Node.js and for workers (Cloudflare Workers) import * as wkmod from "./jsonnet_wasm_bg.wasm"; import * as nodemod from "./jsonnet_wasm_bg.wasm"; -if (typeof process !== "undefined" && process.release.name === "node") { +if ((typeof process !== 'undefined') && (process.release.name === 'node')) { imports.__wbg_set_wasm(nodemod); } else { const instance = new WebAssembly.Instance(wkmod.default, { "./jsonnet_wasm_bg.js": imports }); diff --git a/JS/jsonnet/src/jsonnet_wasm_bg.js b/JS/jsonnet/src/jsonnet_wasm_bg.js index 193b3402e..91d3999e1 100644 --- a/JS/jsonnet/src/jsonnet_wasm_bg.js +++ b/JS/jsonnet/src/jsonnet_wasm_bg.js @@ -1,17 +1,16 @@ -import { read_file } from "./snippets/arakoo-07ad5af4ed8e3fe0/read-file.js"; +import { read_file } from './snippets/arakoo-07ad5af4ed8e3fe0/read-file.js'; let wasm; export function __wbg_set_wasm(val) { wasm = val; } + const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); -function getObject(idx) { - return heap[idx]; -} +function getObject(idx) { return heap[idx]; } let WASM_VECTOR_LEN = 0; @@ -24,32 +23,29 @@ function getUint8Memory0() { return cachedUint8Memory0; } -const lTextEncoder = - typeof TextEncoder === "undefined" ? (0, module.require)("util").TextEncoder : TextEncoder; +const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; -let cachedTextEncoder = new lTextEncoder("utf-8"); +let cachedTextEncoder = new lTextEncoder('utf-8'); -const encodeString = - typeof cachedTextEncoder.encodeInto === "function" - ? function (arg, view) { - return cachedTextEncoder.encodeInto(arg, view); - } - : function (arg, view) { - const buf = cachedTextEncoder.encode(arg); - view.set(buf); - return { - read: arg.length, - written: buf.length, - }; - }; +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); function passStringToWasm0(arg, malloc, realloc) { + if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length, 1) >>> 0; - getUint8Memory0() - .subarray(ptr, ptr + buf.length) - .set(buf); + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } @@ -63,7 +59,7 @@ function passStringToWasm0(arg, malloc, realloc) { for (; offset < len; offset++) { const code = arg.charCodeAt(offset); - if (code > 0x7f) break; + if (code > 0x7F) break; mem[ptr + offset] = code; } @@ -71,7 +67,7 @@ function passStringToWasm0(arg, malloc, realloc) { if (offset !== 0) { arg = arg.slice(offset); } - ptr = realloc(ptr, len, (len = offset + arg.length * 3), 1) >>> 0; + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8Memory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); @@ -110,10 +106,9 @@ function takeObject(idx) { return ret; } -const lTextDecoder = - typeof TextDecoder === "undefined" ? (0, module.require)("util").TextDecoder : TextDecoder; +const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; -let cachedTextDecoder = new lTextDecoder("utf-8", { ignoreBOM: true, fatal: true }); +let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); @@ -139,26 +134,26 @@ function handleError(f, args) { } } /** - * @returns {number} - */ +* @returns {number} +*/ export function jsonnet_make() { const ret = wasm.jsonnet_make(); return ret >>> 0; } /** - * @param {number} vm - */ +* @param {number} vm +*/ export function jsonnet_destroy(vm) { wasm.jsonnet_destroy(vm); } /** - * @param {number} vm - * @param {string} filename - * @param {string} snippet - * @returns {string} - */ +* @param {number} vm +* @param {string} filename +* @param {string} snippet +* @returns {string} +*/ export function jsonnet_evaluate_snippet(vm, filename, snippet) { let deferred3_0; let deferred3_1; @@ -181,10 +176,10 @@ export function jsonnet_evaluate_snippet(vm, filename, snippet) { } /** - * @param {number} vm - * @param {string} filename - * @returns {string} - */ +* @param {number} vm +* @param {string} filename +* @returns {string} +*/ export function jsonnet_evaluate_file(vm, filename) { let deferred2_0; let deferred2_1; @@ -205,10 +200,10 @@ export function jsonnet_evaluate_file(vm, filename) { } /** - * @param {number} vm - * @param {string} key - * @param {string} value - */ +* @param {number} vm +* @param {string} key +* @param {string} value +*/ export function ext_string(vm, key, value) { const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; @@ -219,29 +214,22 @@ export function ext_string(vm, key, value) { export function __wbindgen_string_get(arg0, arg1) { const obj = getObject(arg1); - const ret = typeof obj === "string" ? obj : undefined; - var ptr1 = isLikeNone(ret) - ? 0 - : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const ret = typeof(obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); var len1 = WASM_VECTOR_LEN; getInt32Memory0()[arg0 / 4 + 1] = len1; getInt32Memory0()[arg0 / 4 + 0] = ptr1; -} +}; -export function __wbg_readfile_3df9f1d22ad880df() { - return handleError(function (arg0, arg1, arg2) { - const ret = read_file(getStringFromWasm0(arg1, arg2)); - const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len1; - getInt32Memory0()[arg0 / 4 + 0] = ptr1; - }, arguments); -} +export function __wbg_readfile_3df9f1d22ad880df() { return handleError(function (arg0, arg1, arg2) { + const ret = read_file(getStringFromWasm0(arg1, arg2)); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + getInt32Memory0()[arg0 / 4 + 1] = len1; + getInt32Memory0()[arg0 / 4 + 0] = ptr1; +}, arguments) }; export function __wbindgen_object_drop_ref(arg0) { takeObject(arg0); -} +}; -export function __wbg_log_50ac1182ae1d639d(arg0, arg1) { - console.log(getStringFromWasm0(arg0, arg1)); -} diff --git a/JS/jsonnet/src/jsonnet_wasm_bg.wasm b/JS/jsonnet/src/jsonnet_wasm_bg.wasm index 5bf0bffb0..8c937b007 100644 Binary files a/JS/jsonnet/src/jsonnet_wasm_bg.wasm and b/JS/jsonnet/src/jsonnet_wasm_bg.wasm differ diff --git a/JS/jsonnet/src/jsonnet_wasm_bg.wasm.d.ts b/JS/jsonnet/src/jsonnet_wasm_bg.wasm.d.ts index e1b0aab10..c29cb9267 100644 --- a/JS/jsonnet/src/jsonnet_wasm_bg.wasm.d.ts +++ b/JS/jsonnet/src/jsonnet_wasm_bg.wasm.d.ts @@ -3,14 +3,7 @@ export const memory: WebAssembly.Memory; export function jsonnet_make(): number; export function jsonnet_destroy(a: number): void; -export function jsonnet_evaluate_snippet( - a: number, - b: number, - c: number, - d: number, - e: number, - f: number -): void; +export function jsonnet_evaluate_snippet(a: number, b: number, c: number, d: number, e: number, f: number): void; export function jsonnet_evaluate_file(a: number, b: number, c: number, d: number): void; export function ext_string(a: number, b: number, c: number, d: number, e: number): void; export function __wbindgen_malloc(a: number, b: number): number; diff --git a/JS/jsonnet/src/lib.rs b/JS/jsonnet/src/lib.rs index a57fa04e3..7c2ed6fd5 100755 --- a/JS/jsonnet/src/lib.rs +++ b/JS/jsonnet/src/lib.rs @@ -38,7 +38,7 @@ pub fn jsonnet_make() -> *mut VM { state.clone(), PathResolver::new_cwd_fallback(), )); - add_namespace(&state); + // add_namespace(&state); Box::into_raw(Box::new(VM { state, manifest_format: Box::new(JsonFormat::default()), @@ -105,7 +105,7 @@ pub fn ext_string(vm: *mut VM, key: &str, value: &str) { any_initializer .as_any() .downcast_ref::() - .unwrap() + .expect("only stdlib context initializer supported") .add_ext_var(key.into(), Val::Str(value.into())); } @@ -145,7 +145,39 @@ fn regex_match(a: String, b: String) -> Vec { #[cfg(test)] mod test { + use super::*; use regex::Regex; + #[test] + // #[wasm_bindgen] + pub fn test() { + let vm = jsonnet_make(); + // let filename = CString::new("filename").unwrap(); + let filename = "filename"; + + let snippet = r#" + local username = std.extVar('name'); + local Person(name='Alice') = { + name: name, + welcome: 'Hello ' + name + '!', + }; + { + person1: Person(username), + person2: Person('Bob'), + }"#; + + // .unwrap(); + unsafe { + ext_string( + &mut *vm, // name.as_ptr() as *const c_char, + "name", // value.as_ptr() as *const c_char, + "afshan", + ); + } + + let result = unsafe { jsonnet_evaluate_snippet(&mut *vm, filename, snippet) }; + println!("{}", result); + // } + } #[test] fn do_regex_test() { @@ -166,6 +198,7 @@ mod test { search.push(cap[1].to_string()); } - println!("{:?}", search); + println!("pattern found {:?}", search); } + } diff --git a/JS/jsonnet/test/package.json b/JS/jsonnet/test/package.json new file mode 100644 index 000000000..953deece6 --- /dev/null +++ b/JS/jsonnet/test/package.json @@ -0,0 +1,17 @@ +{ + "name": "test", + "version": "1.0.0", + "description": "", + "main": "test.js", + "type": "module", + "scripts": { + "test": "node --experimental-wasm-modules ./node_modules/mocha/bin/mocha" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^5.1.0", + "mocha": "^10.4.0" + } +} diff --git a/JS/jsonnet/test/test.js b/JS/jsonnet/test/test.js new file mode 100644 index 000000000..599e7d96f --- /dev/null +++ b/JS/jsonnet/test/test.js @@ -0,0 +1,120 @@ +import Jsonnet from "../src/jsonnet.js"; +import { expect } from "chai"; + +let jsonnet = new Jsonnet(); + +describe("Testing evaluateSnippet function of jsonnet library", () => { + it("self reference", () => { + let result = JSON.parse(jsonnet.evaluateSnippet(`{ + Martini: { + local drink = self, + ingredients: [ + { kind: "Farmer's Gin", qty: 1 }, + { + kind: 'Dry White Vermouth', + qty: drink.ingredients[0].qty, + }, + ], + garnish: 'Olive', + served: 'Straight Up', + }, + }`)); + let expected = JSON.parse(`{ + "Martini": { + "garnish": "Olive", + "ingredients": [ + { + "kind": "Farmer's Gin", + "qty": 1 + }, + { + "kind": "Dry White Vermouth", + "qty": 1 + } + ], + "served": "Straight Up" + } + }`); + // expect(JSON.stringify(result)).to.equal(JSON.stringify(expected)); + expect(result).to.eql(expected); + }); + + it("math operations", () => { + let result = JSON.parse(jsonnet.evaluateSnippet(`{ + a: 1 + 2, + b: 3 * 4, + c: 5 / 6, + d: 7 % 8, + e: 9 - 10, + }`)); + let expected = JSON.parse(`{ + "a": 3, + "b": 12, + "c": 0.8333333333333334, + "d": 7, + "e": -1 + }`); + // expect(JSON.stringify(result)).to.equal(JSON.stringify(expected)); + expect(result).to.eql(expected); + }) +}); + + +describe("Testing evaluateFile function of jsonnet library", () => { + it("Read File and evaluate", () => { + // let result = jsonnet.extString("name", "Alice"); + let result = JSON.parse(jsonnet.evaluateFile("./test.jsonnet")); + let expected = JSON.parse(`{ + "concat_array": [ + 1, + 2, + 3, + 4 + ], + "concat_string": "1234", + "equality1": false, + "equality2": true, + "ex1": 1.6666666666666665, + "ex2": 3, + "ex3": 1.6666666666666665, + "ex4": true, + "obj": { + "a": 1, + "b": 3, + "c": 4 + }, + "obj_member": true, + "str1": "The value of self.ex2 is 3.", + "str2": "The value of self.ex2 is 3.", + "str3": "ex1=1.67, ex2=3.00", + "str4": "ex1=1.67, ex2=3.00", + "str5": "ex1=1.67\\nex2=3.00\\n" + }`); + expect(result).to.eql(expected); + }); +}); + +describe("Testing extString function of jsonnet library", () => { + it("Test extString function", () => { + let result = JSON.parse(jsonnet.extString("name", "Alice").evaluateSnippet(`local username = std.extVar('name'); + local Person(name='Alice') = { + name: name, + welcome: 'Hello ' + name + '!', + }; + { + person1: Person(username), + person2: Person('Bob'), + }`)) + let expected = JSON.parse(`{ + "person1": { + "name": "Alice", + "welcome": "Hello Alice!" + }, + "person2": { + "name": "Bob", + "welcome": "Hello Bob!" + } + }`); + expect(result).to.eql(expected); + }); +}) \ No newline at end of file diff --git a/JS/jsonnet/test/test.jsonnet b/JS/jsonnet/test/test.jsonnet new file mode 100644 index 000000000..f23fd7e4b --- /dev/null +++ b/JS/jsonnet/test/test.jsonnet @@ -0,0 +1,25 @@ +{ + concat_array: [1, 2, 3] + [4], + concat_string: '123' + 4, + equality1: 1 == '1', + equality2: [{}, { x: 3 - 1 }] + == [{}, { x: 2 }], + ex1: 1 + 2 * 3 / (4 + 5), + ex2: self.ex1 | 3, + ex3: self.ex1 % 2, + ex4: (4 > 3) && (1 <= 3) || false, + obj: { a: 1, b: 2 } + { b: 3, c: 4 }, + obj_member: 'foo' in { foo: 1 }, + str1: 'The value of self.ex2 is ' + + self.ex2 + '.', + str2: 'The value of self.ex2 is %g.' + % self.ex2, + str3: 'ex1=%0.2f, ex2=%0.2f' + % [self.ex1, self.ex2], + str4: 'ex1=%(ex1)0.2f, ex2=%(ex2)0.2f' + % self, + str5: ||| + ex1=%(ex1)0.2f + ex2=%(ex2)0.2f + ||| % self, +} \ No newline at end of file diff --git a/JS/wasm/crates/cli/Cargo.toml b/JS/wasm/crates/cli/Cargo.toml index 41270e885..fe47acc48 100644 --- a/JS/wasm/crates/cli/Cargo.toml +++ b/JS/wasm/crates/cli/Cargo.toml @@ -17,7 +17,8 @@ experimental_event_loop = [] wizer = { workspace = true } structopt = "0.3" anyhow = { workspace = true } -binaryen = { git = "https://github.com/pepyakin/binaryen-rs", rev = "00c98174843f957681ba0bc5cdcc9d15f5d0cb23" } +binaryen = "0.13.0" +# binaryen = { git = "https://github.com/pepyakin/binaryen-rs", rev = "00c98174843f957681ba0bc5cdcc9d15f5d0cb23" } brotli = "3.4.0" wasmprinter = { version = "0.2.75", optional = true } wasmtime = { workspace = true }