diff --git a/JS/jsonnet/Cargo.toml b/JS/jsonnet/Cargo.toml index 03f856311..09d71046e 100644 --- a/JS/jsonnet/Cargo.toml +++ b/JS/jsonnet/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1.0.111" wasm-bindgen-file-reader = "1" regex = "1.10.3" js-sys = "0.3.69" +console_error_panic_hook = "0.1.7" [dev-dependencies] wasm-bindgen-test = "0.3.34" diff --git a/JS/jsonnet/build.sh b/JS/jsonnet/build.sh index eb03afcec..674e61cb4 100755 --- a/JS/jsonnet/build.sh +++ b/JS/jsonnet/build.sh @@ -14,7 +14,7 @@ if [ "$1" != "false" ]; then fi OUT_FOLDER="jsonnet" OUT_JSON="${OUT_FOLDER}/package.json" -OUT_TARGET="bundler" +OUT_TARGET="nodejs" OUT_NPM_NAME="arakoo" WASM_BUILD_PROFILE="release" @@ -40,8 +40,8 @@ enable_cf_in_bindings() { # - Cloudflare Workers / Miniflare local FILE="$1" # e.g., `query_engine.js` - local BG_FILE="jsonnet_wasm_bg.js" - local OUTPUT_FILE="${OUT_FOLDER}/jsonnet_wasm.js" + local BG_FILE="jsonnet_wasm.js" + local OUTPUT_FILE="${OUT_FOLDER}/jsonnet_wasm_binding.js" cat <"$OUTPUT_FILE" import * as imports from "./${BG_FILE}"; @@ -59,8 +59,8 @@ if ((typeof process !== 'undefined') && (process.release.name === 'node')) { export * from "./${BG_FILE}"; EOF - cat <"$OUT_FOLDER/index.js" -import Jsonnet from "./jsonnet.js"; + cat <"$OUT_FOLDER/index.mjs" +const {Jsonnet} = await import("./jsonnet.js") export default Jsonnet; EOF @@ -96,6 +96,6 @@ move_jsonnet_to_src() { rm -rf jsonnet } -enable_cf_in_bindings "jsonnet_wasm_bg.js" +# enable_cf_in_bindings "jsonnet_wasm_bg.js" update_package_json "index.js" move_jsonnet_to_src diff --git a/JS/jsonnet/package.json b/JS/jsonnet/package.json index bd3af7e37..852cd7f39 100644 --- a/JS/jsonnet/package.json +++ b/JS/jsonnet/package.json @@ -1,7 +1,14 @@ { "name": "@arakoodev/jsonnet", "version": "0.2.0", - "main": "src/index.js", - "type": "module", - "types": "src/index.d.ts" + "exports":{ + "import":{ + "default":"./src/index.mjs", + "types":"./src/index.d.mts" + }, + "require":{ + "default":"./src/index.js", + "types":"./src/index.d.ts" + } + } } diff --git a/JS/jsonnet/read-file.js b/JS/jsonnet/read-file.js index 218671dff..920c10000 100644 --- a/JS/jsonnet/read-file.js +++ b/JS/jsonnet/read-file.js @@ -1,5 +1,8 @@ -import fs from "fs"; +// import fs from "fs"; +const fs = require("fs"); -export function read_file(path) { +function read_file(path) { return fs.readFileSync(path, { encoding: "utf8" }); } + +module.exports = { read_file } \ No newline at end of file diff --git a/JS/jsonnet/src/context.rs b/JS/jsonnet/src/context.rs index ff5e52eba..a9b69a0ee 100644 --- a/JS/jsonnet/src/context.rs +++ b/JS/jsonnet/src/context.rs @@ -18,6 +18,16 @@ fn join(a: String, b: String) -> String { format!("{}{}", a, b) } +#[builtin] +fn includes(a: String, b: String) -> bool { + if a.contains(&b) { + return true; + } + else { + return false; + } +} + #[builtin] fn regex_match(a: String, b: String) -> Vec { // log(&a); @@ -50,6 +60,7 @@ fn arakoolib_uncached(settings: Rc>) -> ObjValue { ); builder.method("join", join::INST); builder.method("regexMatch", regex_match::INST); + builder.method("includes", includes::INST); builder.build() } diff --git a/JS/jsonnet/src/index.d.mts b/JS/jsonnet/src/index.d.mts new file mode 100644 index 000000000..6d4fe8d6a --- /dev/null +++ b/JS/jsonnet/src/index.d.mts @@ -0,0 +1,10 @@ +declare class Jsonnet { + constructor(); + evaluateSnippet(snippet: string): string; + destroy(): void; + extString(key: string, value: string): this; + evaluateFile(filename: string): string; + javascriptCallback(name: string, func: Function): this; +} + +export default Jsonnet; diff --git a/JS/jsonnet/src/index.js b/JS/jsonnet/src/index.js index d6d0a7061..87cf96b46 100644 --- a/JS/jsonnet/src/index.js +++ b/JS/jsonnet/src/index.js @@ -1,3 +1,2 @@ -import Jsonnet from "./jsonnet.js"; - -export default Jsonnet; +const Jsonnet = require("./jsonnet.js") +module.exports = Jsonnet; \ No newline at end of file diff --git a/JS/jsonnet/src/index.mjs b/JS/jsonnet/src/index.mjs new file mode 100644 index 000000000..9ade7c274 --- /dev/null +++ b/JS/jsonnet/src/index.mjs @@ -0,0 +1,3 @@ +import Jsonnet from "./jsonnet.js"; + +export default Jsonnet; \ No newline at end of file diff --git a/JS/jsonnet/src/jsonnet.js b/JS/jsonnet/src/jsonnet.js index ce39f758d..988c4a737 100644 --- a/JS/jsonnet/src/jsonnet.js +++ b/JS/jsonnet/src/jsonnet.js @@ -3,7 +3,7 @@ const isArakoo = process.env.arakoo; let Jsonnet; if (!isArakoo) { - let module = import("./jsonnet_wasm.js"); + let module = require("./jsonnet_wasm.js"); let { jsonnet_evaluate_snippet, jsonnet_destroy, @@ -13,7 +13,7 @@ if (!isArakoo) { get_func, set_func, register_native_callback, - } = await module; + } = module; Jsonnet = class Jsonnet { constructor() { this.vm = jsonnet_make(); @@ -76,4 +76,4 @@ if (!isArakoo) { }; } -export default Jsonnet; +module.exports = Jsonnet; diff --git a/JS/jsonnet/src/jsonnet_wasm.d.ts b/JS/jsonnet/src/jsonnet_wasm.d.ts index b0876700d..c81f6ecda 100644 --- a/JS/jsonnet/src/jsonnet_wasm.d.ts +++ b/JS/jsonnet/src/jsonnet_wasm.d.ts @@ -1,45 +1,45 @@ /* 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; /** - * @param {string} name - * @returns {Function | undefined} - */ +* @param {string} name +* @returns {Function | undefined} +*/ export function get_func(name: string): Function | undefined; /** - * @param {string} name - * @param {Function} func - */ +* @param {string} name +* @param {Function} func +*/ export function set_func(name: string, func: Function): void; /** - * @param {number} vm - * @param {string} name - * @param {number} args_num - */ +* @param {number} vm +* @param {string} name +* @param {number} args_num +*/ export function register_native_callback(vm: number, name: string, args_num: number): void; diff --git a/JS/jsonnet/src/jsonnet_wasm.js b/JS/jsonnet/src/jsonnet_wasm.js index 9c893b553..21de9fd82 100644 --- a/JS/jsonnet/src/jsonnet_wasm.js +++ b/JS/jsonnet/src/jsonnet_wasm.js @@ -1,13 +1,391 @@ -import * as imports from "./jsonnet_wasm_bg.js"; +let imports = {}; +imports['__wbindgen_placeholder__'] = module.exports; +let wasm; +const { read_file } = require(String.raw`./snippets/arakoo-jsonnet-17c737407ebd2d3c/read-file.js`); +const { TextEncoder, TextDecoder } = require(`util`); -// 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") { - imports.__wbg_set_wasm(nodemod); -} else { - const instance = new WebAssembly.Instance(wkmod.default, { "./jsonnet_wasm_bg.js": imports }); - imports.__wbg_set_wasm(instance.exports); +const heap = new Array(128).fill(undefined); + +heap.push(undefined, null, true, false); + +function getObject(idx) { return heap[idx]; } + +let WASM_VECTOR_LEN = 0; + +let cachedUint8Memory0 = null; + +function getUint8Memory0() { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; +} + +let cachedTextEncoder = new TextEncoder('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 + }; +}); + +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); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +function isLikeNone(x) { + return x === undefined || x === null; +} + +let cachedInt32Memory0 = null; + +function getInt32Memory0() { + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachedInt32Memory0; +} + +let heap_next = heap.length; + +function dropObject(idx) { + if (idx < 132) return; + heap[idx] = heap_next; + heap_next = idx; +} + +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; +} + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} + +function addHeapObject(obj) { + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; + + heap[idx] = obj; + return idx; +} + +function debugString(val) { + // primitive types + const type = typeof val; + if (type == 'number' || type == 'boolean' || val == null) { + return `${val}`; + } + if (type == 'string') { + return `"${val}"`; + } + if (type == 'symbol') { + const description = val.description; + if (description == null) { + return 'Symbol'; + } else { + return `Symbol(${description})`; + } + } + if (type == 'function') { + const name = val.name; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } + } + // objects + if (Array.isArray(val)) { + const length = val.length; + let debug = '['; + if (length > 0) { + debug += debugString(val[0]); + } + for(let i = 1; i < length; i++) { + debug += ', ' + debugString(val[i]); + } + debug += ']'; + return debug; + } + // Test for built-in + const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); + let className; + if (builtInMatches.length > 1) { + className = builtInMatches[1]; + } else { + // Failed to match the standard '[object ClassName]' + return toString.call(val); + } + if (className == 'Object') { + // we're a user defined class or Object + // JSON.stringify avoids problems with cycles, and is generally much + // easier than looping through ownProperties of `val`. + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } + } + // errors + if (val instanceof Error) { + return `${val.name}: ${val.message}\n${val.stack}`; + } + // TODO we could test for more things here, like `Set`s and `Map`s. + return className; } -export * from "./jsonnet_wasm_bg.js"; +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + wasm.__wbindgen_exn_store(addHeapObject(e)); + } +} +/** +* @returns {number} +*/ +module.exports.jsonnet_make = function() { + const ret = wasm.jsonnet_make(); + return ret >>> 0; +}; + +/** +* @param {number} vm +*/ +module.exports.jsonnet_destroy = function(vm) { + wasm.jsonnet_destroy(vm); +}; + +/** +* @param {number} vm +* @param {string} filename +* @param {string} snippet +* @returns {string} +*/ +module.exports.jsonnet_evaluate_snippet = function(vm, filename, snippet) { + let deferred3_0; + let deferred3_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(snippet, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + wasm.jsonnet_evaluate_snippet(retptr, vm, ptr0, len0, ptr1, len1); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred3_0 = r0; + deferred3_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +}; + +/** +* @param {number} vm +* @param {string} filename +* @returns {string} +*/ +module.exports.jsonnet_evaluate_file = function(vm, filename) { + let deferred2_0; + let deferred2_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + wasm.jsonnet_evaluate_file(retptr, vm, ptr0, len0); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred2_0 = r0; + deferred2_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +}; + +/** +* @param {number} vm +* @param {string} key +* @param {string} value +*/ +module.exports.ext_string = function(vm, key, value) { + const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + wasm.ext_string(vm, ptr0, len0, ptr1, len1); +}; + +/** +* @param {string} name +* @returns {Function | undefined} +*/ +module.exports.get_func = function(name) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.get_func(ptr0, len0); + return takeObject(ret); +}; + +/** +* @param {string} name +* @param {Function} func +*/ +module.exports.set_func = function(name, func) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + wasm.set_func(ptr0, len0, addHeapObject(func)); +}; + +/** +* @param {number} vm +* @param {string} name +* @param {number} args_num +*/ +module.exports.register_native_callback = function(vm, name, args_num) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + wasm.register_native_callback(vm, ptr0, len0, args_num); +}; + +module.exports.__wbindgen_string_get = function(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); + var len1 = WASM_VECTOR_LEN; + getInt32Memory0()[arg0 / 4 + 1] = len1; + getInt32Memory0()[arg0 / 4 + 0] = ptr1; +}; + +module.exports.__wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); +}; + +module.exports.__wbindgen_string_new = function(arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); +}; + +module.exports.__wbg_readfile_5b48d0f7e3518df2 = function() { 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) }; + +module.exports.__wbindgen_object_clone_ref = function(arg0) { + const ret = getObject(arg0); + return addHeapObject(ret); +}; + +module.exports.__wbg_call_27c0f87801dedf93 = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).call(getObject(arg1)); + return addHeapObject(ret); +}, arguments) }; + +module.exports.__wbg_call_b3ca7c6051f9bec1 = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).call(getObject(arg1), getObject(arg2)); + return addHeapObject(ret); +}, arguments) }; + +module.exports.__wbg_new_abda76e883ba8a5f = function() { + const ret = new Error(); + return addHeapObject(ret); +}; + +module.exports.__wbg_stack_658279fe44541cf6 = function(arg0, arg1) { + const ret = getObject(arg1).stack; + 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; +}; + +module.exports.__wbg_error_f851667af71bcfc6 = function(arg0, arg1) { + let deferred0_0; + let deferred0_1; + try { + deferred0_0 = arg0; + deferred0_1 = arg1; + console.error(getStringFromWasm0(arg0, arg1)); + } finally { + wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); + } +}; + +module.exports.__wbindgen_debug_string = function(arg0, arg1) { + const ret = debugString(getObject(arg1)); + 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; +}; + +module.exports.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + +const path = require('path').join(__dirname, 'jsonnet_wasm_bg.wasm'); +const bytes = require('fs').readFileSync(path); + +const wasmModule = new WebAssembly.Module(bytes); +const wasmInstance = new WebAssembly.Instance(wasmModule, imports); +wasm = wasmInstance.exports; +module.exports.__wasm = wasm; + diff --git a/JS/jsonnet/src/jsonnet_wasm_bg.js b/JS/jsonnet/src/jsonnet_wasm_bg.js deleted file mode 100644 index 773da80e3..000000000 --- a/JS/jsonnet/src/jsonnet_wasm_bg.js +++ /dev/null @@ -1,376 +0,0 @@ -import { read_file } from "./snippets/arakoo-jsonnet-17c737407ebd2d3c/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]; -} - -let WASM_VECTOR_LEN = 0; - -let cachedUint8Memory0 = null; - -function getUint8Memory0() { - if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { - cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8Memory0; -} - -const lTextEncoder = - typeof TextEncoder === "undefined" ? (0, module.require)("util").TextEncoder : TextEncoder; - -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, - }; - }; - -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); - WASM_VECTOR_LEN = buf.length; - return ptr; - } - - let len = arg.length; - let ptr = malloc(len, 1) >>> 0; - - const mem = getUint8Memory0(); - - let offset = 0; - - for (; offset < len; offset++) { - const code = arg.charCodeAt(offset); - if (code > 0x7f) break; - mem[ptr + offset] = code; - } - - if (offset !== len) { - if (offset !== 0) { - arg = arg.slice(offset); - } - ptr = realloc(ptr, len, (len = offset + arg.length * 3), 1) >>> 0; - const view = getUint8Memory0().subarray(ptr + offset, ptr + len); - const ret = encodeString(arg, view); - - offset += ret.written; - ptr = realloc(ptr, len, offset, 1) >>> 0; - } - - WASM_VECTOR_LEN = offset; - return ptr; -} - -function isLikeNone(x) { - return x === undefined || x === null; -} - -let cachedInt32Memory0 = null; - -function getInt32Memory0() { - if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { - cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachedInt32Memory0; -} - -let heap_next = heap.length; - -function dropObject(idx) { - if (idx < 132) return; - heap[idx] = heap_next; - heap_next = idx; -} - -function takeObject(idx) { - const ret = getObject(idx); - dropObject(idx); - return ret; -} - -const lTextDecoder = - typeof TextDecoder === "undefined" ? (0, module.require)("util").TextDecoder : TextDecoder; - -let cachedTextDecoder = new lTextDecoder("utf-8", { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); -} - -function addHeapObject(obj) { - if (heap_next === heap.length) heap.push(heap.length + 1); - const idx = heap_next; - heap_next = heap[idx]; - - heap[idx] = obj; - return idx; -} - -function debugString(val) { - // primitive types - const type = typeof val; - if (type == "number" || type == "boolean" || val == null) { - return `${val}`; - } - if (type == "string") { - return `"${val}"`; - } - if (type == "symbol") { - const description = val.description; - if (description == null) { - return "Symbol"; - } else { - return `Symbol(${description})`; - } - } - if (type == "function") { - const name = val.name; - if (typeof name == "string" && name.length > 0) { - return `Function(${name})`; - } else { - return "Function"; - } - } - // objects - if (Array.isArray(val)) { - const length = val.length; - let debug = "["; - if (length > 0) { - debug += debugString(val[0]); - } - for (let i = 1; i < length; i++) { - debug += ", " + debugString(val[i]); - } - debug += "]"; - return debug; - } - // Test for built-in - const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); - let className; - if (builtInMatches.length > 1) { - className = builtInMatches[1]; - } else { - // Failed to match the standard '[object ClassName]' - return toString.call(val); - } - if (className == "Object") { - // we're a user defined class or Object - // JSON.stringify avoids problems with cycles, and is generally much - // easier than looping through ownProperties of `val`. - try { - return "Object(" + JSON.stringify(val) + ")"; - } catch (_) { - return "Object"; - } - } - // errors - if (val instanceof Error) { - return `${val.name}: ${val.message}\n${val.stack}`; - } - // TODO we could test for more things here, like `Set`s and `Map`s. - return className; -} - -function handleError(f, args) { - try { - return f.apply(this, args); - } catch (e) { - wasm.__wbindgen_exn_store(addHeapObject(e)); - } -} -/** - * @returns {number} - */ -export function jsonnet_make() { - const ret = wasm.jsonnet_make(); - return ret >>> 0; -} - -/** - * @param {number} vm - */ -export function jsonnet_destroy(vm) { - wasm.jsonnet_destroy(vm); -} - -/** - * @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; - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - const ptr0 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passStringToWasm0(snippet, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - wasm.jsonnet_evaluate_snippet(retptr, vm, ptr0, len0, ptr1, len1); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - deferred3_0 = r0; - deferred3_1 = r1; - return getStringFromWasm0(r0, r1); - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); - wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); - } -} - -/** - * @param {number} vm - * @param {string} filename - * @returns {string} - */ -export function jsonnet_evaluate_file(vm, filename) { - let deferred2_0; - let deferred2_1; - try { - const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); - const ptr0 = passStringToWasm0(filename, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - wasm.jsonnet_evaluate_file(retptr, vm, ptr0, len0); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - deferred2_0 = r0; - deferred2_1 = r1; - return getStringFromWasm0(r0, r1); - } finally { - wasm.__wbindgen_add_to_stack_pointer(16); - wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); - } -} - -/** - * @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; - const ptr1 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len1 = WASM_VECTOR_LEN; - wasm.ext_string(vm, ptr0, len0, ptr1, len1); -} - -/** - * @param {string} name - * @returns {Function | undefined} - */ -export function get_func(name) { - const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.get_func(ptr0, len0); - return takeObject(ret); -} - -/** - * @param {string} name - * @param {Function} func - */ -export function set_func(name, func) { - const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - wasm.set_func(ptr0, len0, addHeapObject(func)); -} - -/** - * @param {number} vm - * @param {string} name - * @param {number} args_num - */ -export function register_native_callback(vm, name, args_num) { - const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - wasm.register_native_callback(vm, ptr0, len0, args_num); -} - -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); - var len1 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len1; - getInt32Memory0()[arg0 / 4 + 0] = ptr1; -} - -export function __wbindgen_object_drop_ref(arg0) { - takeObject(arg0); -} - -export function __wbindgen_string_new(arg0, arg1) { - const ret = getStringFromWasm0(arg0, arg1); - return addHeapObject(ret); -} - -export function __wbg_readfile_5b48d0f7e3518df2() { - 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_clone_ref(arg0) { - const ret = getObject(arg0); - return addHeapObject(ret); -} - -export function __wbg_call_27c0f87801dedf93() { - return handleError(function (arg0, arg1) { - const ret = getObject(arg0).call(getObject(arg1)); - return addHeapObject(ret); - }, arguments); -} - -export function __wbg_call_b3ca7c6051f9bec1() { - return handleError(function (arg0, arg1, arg2) { - const ret = getObject(arg0).call(getObject(arg1), getObject(arg2)); - return addHeapObject(ret); - }, arguments); -} - -export function __wbindgen_debug_string(arg0, arg1) { - const ret = debugString(getObject(arg1)); - 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; -} - -export function __wbindgen_throw(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -} diff --git a/JS/jsonnet/src/jsonnet_wasm_bg.wasm b/JS/jsonnet/src/jsonnet_wasm_bg.wasm index 6385ee2bf..0a1c7abb7 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 b134fd153..ae3eaa690 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 get_func(a: number, b: number): number; diff --git a/JS/jsonnet/src/lib.rs b/JS/jsonnet/src/lib.rs index 66cf3d3e0..04cbbb96a 100755 --- a/JS/jsonnet/src/lib.rs +++ b/JS/jsonnet/src/lib.rs @@ -17,6 +17,7 @@ use jrsonnet_gcmodule::Trace; use jrsonnet_parser::IStr; use std::alloc; use wasm_bindgen::prelude::*; +use console_error_panic_hook; mod context; @@ -59,6 +60,7 @@ pub fn jsonnet_make() -> *mut VM { }); } let state = State::default(); + console_error_panic_hook::set_once(); state.settings_mut().import_resolver = tb!(FileImportResolver::default()); state.set_context_initializer(context::ArakooContextInitializer::new( state.clone(), @@ -139,43 +141,6 @@ pub fn ext_string(vm: *mut VM, key: &str, value: &str) { .add_ext_var(key.into(), Val::Str(value.into())); } -// #[wasm_bindgen] -// pub struct CallBackClass { -// arg: String, -// func: js_sys::Function -// } - -// #[wasm_bindgen] -// impl CallBackClass { -// #[wasm_bindgen(constructor)] -// pub extern "C" fn new(f: &js_sys::Function) -> CallBackClass { -// CallBackClass { -// arg: String::from(""), -// func: f.clone(), -// } -// } - -// pub extern "C" fn call_native_js_func(&self) -> Result { -// let this = JsValue::null(); -// // for x in self.args { -// // let x = JsValue::from(x); -// let result = self.func.call1(&this, &JsValue::from_str(&self.arg)); -// println!("Result of calling JS function: {:?}", result); -// return result; -// // } -// } - -// #[wasm_bindgen(getter)] -// pub extern "C" fn arg(&self) -> String { -// self.arg.clone() -// } - -// #[wasm_bindgen(setter)] -// pub extern "C" fn set_arg(&mut self, arg: String) { -// self.arg = arg; -// } -// } - #[wasm_bindgen] pub fn get_func(name: &str) -> Option { unsafe { diff --git a/JS/jsonnet/src/snippets/arakoo-jsonnet-17c737407ebd2d3c/read-file.js b/JS/jsonnet/src/snippets/arakoo-jsonnet-17c737407ebd2d3c/read-file.js index 218671dff..920c10000 100644 --- a/JS/jsonnet/src/snippets/arakoo-jsonnet-17c737407ebd2d3c/read-file.js +++ b/JS/jsonnet/src/snippets/arakoo-jsonnet-17c737407ebd2d3c/read-file.js @@ -1,5 +1,8 @@ -import fs from "fs"; +// import fs from "fs"; +const fs = require("fs"); -export function read_file(path) { +function read_file(path) { return fs.readFileSync(path, { encoding: "utf8" }); } + +module.exports = { read_file } \ No newline at end of file diff --git a/JS/jsonnet/test/dist/test.js b/JS/jsonnet/test/dist/test.js index 88e610871..7bb5d5564 100644 --- a/JS/jsonnet/test/dist/test.js +++ b/JS/jsonnet/test/dist/test.js @@ -1,11 +1,12 @@ import Jsonnet from "@arakoodev/jsonnet"; +// import Jsonnet from "../src/index.js" +// import Jsonnet from "../src/index.mjs" import { expect } from "chai"; import { describe, it } from "mocha"; let jsonnet = new Jsonnet(); describe("Testing evaluateSnippet function of jsonnet library", () => { it("self reference", () => { - let result = JSON.parse( - jsonnet.evaluateSnippet(`{ + let result = JSON.parse(jsonnet.evaluateSnippet(`{ Martini: { local drink = self, ingredients: [ @@ -18,8 +19,7 @@ describe("Testing evaluateSnippet function of jsonnet library", () => { garnish: 'Olive', served: 'Straight Up', }, - }`) - ); + }`)); let expected = { Martini: { garnish: "Olive", @@ -40,15 +40,13 @@ describe("Testing evaluateSnippet function of jsonnet library", () => { expect(result).to.eql(expected); }); it("math operations", () => { - let result = JSON.parse( - jsonnet.evaluateSnippet(`{ + let result = JSON.parse(jsonnet.evaluateSnippet(`{ a: 1 + 2, b: 3 * 4, c: 5 / 6, d: 7 % 8, e: 9 - 10, - }`) - ); + }`)); let expected = { a: 3, b: 12, @@ -90,8 +88,7 @@ describe("Testing evaluateFile function of jsonnet library", () => { }); 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'); + let result = JSON.parse(jsonnet.extString("name", "Alice").evaluateSnippet(`local username = std.extVar('name'); local Person(name='Alice') = { name: name, welcome: 'Hello ' + name + '!', @@ -99,8 +96,7 @@ describe("Testing extString function of jsonnet library", () => { { person1: Person(username), person2: Person('Bob'), - }`) - ); + }`)); let expected = { person1: { name: "Alice", @@ -133,13 +129,11 @@ describe("Testing regex function of jsonnet library", () => { }); describe("Testing join function of jsonnet library", () => { it("Test join function", () => { - let result = JSON.parse( - jsonnet.evaluateSnippet(`local a = "open"; + let result = JSON.parse(jsonnet.evaluateSnippet(`local a = "open"; local b = "source"; { "joined string":arakoo.join(a,b) - }`) - ); + }`)); let expected = { "joined string": "opensource", }; @@ -151,12 +145,10 @@ describe("Testing javascript native function of jsonnet library", () => { function add(a, b, c) { return a + b + c; } - let result = JSON.parse( - jsonnet.javascriptCallback("add", add).evaluateSnippet(`{ + let result = JSON.parse(jsonnet.javascriptCallback("add", add).evaluateSnippet(`{ "result": "Output "+arakoo.native("add")(1,2,3), "name":"Alice" - }`) - ); + }`)); expect(result).to.eql({ result: "Output 6", name: "Alice", @@ -171,12 +163,10 @@ describe("Testing javascript native function of jsonnet library", () => { } return sum; } - let result = JSON.parse( - jsonnet.javascriptCallback("arrsum", calcSum).evaluateSnippet(`{ + let result = JSON.parse(jsonnet.javascriptCallback("arrsum", calcSum).evaluateSnippet(`{ "result": "Output "+arakoo.native("arrsum")(${JSON.stringify(numArr)}), "name":"Alice" - }`) - ); + }`)); expect(result).to.eql({ result: "Output 15", name: "Alice", @@ -186,15 +176,24 @@ describe("Testing javascript native function of jsonnet library", () => { function concat(a, b) { return a + b; } - let result = JSON.parse( - jsonnet.javascriptCallback("concat", concat).evaluateSnippet(`{ + let result = JSON.parse(jsonnet.javascriptCallback("concat", concat).evaluateSnippet(`{ "result": "Output "+arakoo.native("concat")("Hello ","World"), "name":"Alice" - }`) - ); + }`)); expect(result).to.eql({ result: "Output Hello World", name: "Alice", }); }); }); +describe("Testing includes function of jsonnet library", () => { + it("Test includes function", () => { + let result = JSON.parse(jsonnet.evaluateSnippet(`{ + "result":arakoo.includes("open source is awesome","source") + }`)); + let expected = { + "result": true, + }; + expect(result).to.eql(expected); + }); +}); diff --git a/JS/jsonnet/test/package.json b/JS/jsonnet/test/package.json index 8f30c2088..0838edcce 100644 --- a/JS/jsonnet/test/package.json +++ b/JS/jsonnet/test/package.json @@ -11,11 +11,14 @@ "author": "", "license": "ISC", "dependencies": { - "@arakoodev/jsonnet": "file:../" + "synchronized-promise": "^0.3.1", + "util": "^0.12.5" }, "devDependencies": { "@types/chai": "^4.3.14", + "@arakoodev/jsonnet":"file:../", "@types/mocha": "^10.0.6", + "@types/node": "^20.12.7", "chai": "^5.1.0", "mocha": "^10.4.0", "typescript": "^5.4.3" diff --git a/JS/jsonnet/test/test.ts b/JS/jsonnet/test/test.ts index 5f86c8c7e..7216ff091 100644 --- a/JS/jsonnet/test/test.ts +++ b/JS/jsonnet/test/test.ts @@ -1,4 +1,6 @@ import Jsonnet from "@arakoodev/jsonnet"; +// import Jsonnet from "../src/index.js" +// import Jsonnet from "../src/index.mjs" import { expect } from "chai"; import { describe, it } from "mocha"; @@ -206,3 +208,15 @@ describe("Testing javascript native function of jsonnet library", () => { }); }); }); + +describe("Testing includes function of jsonnet library", () => { + it("Test includes function", () => { + let result = JSON.parse(jsonnet.evaluateSnippet(`{ + "result":arakoo.includes("open source is awesome","source") + }`)); + let expected = { + "result": true, + }; + expect(result).to.eql(expected); + }); +})