Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ext str #322

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions JS/jsonnet/src/jsonnet_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion JS/jsonnet/src/jsonnet_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
114 changes: 51 additions & 63 deletions JS/jsonnet/src/jsonnet_wasm_bg.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
Expand All @@ -63,15 +59,15 @@ 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;
}

if (offset !== len) {
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);

Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
Binary file modified JS/jsonnet/src/jsonnet_wasm_bg.wasm
Binary file not shown.
9 changes: 1 addition & 8 deletions JS/jsonnet/src/jsonnet_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 36 additions & 3 deletions JS/jsonnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn ext_string(vm: *mut VM, key: &str, value: &str) {
any_initializer
.as_any()
.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
.unwrap()
.expect("only stdlib context initializer supported")
.add_ext_var(key.into(), Val::Str(value.into()));
}

Expand Down Expand Up @@ -145,7 +145,39 @@ fn regex_match(a: String, b: String) -> Vec<String> {

#[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() {
Expand All @@ -166,6 +198,7 @@ mod test {
search.push(cap[1].to_string());
}

println!("{:?}", search);
println!("pattern found {:?}", search);
}

}
17 changes: 17 additions & 0 deletions JS/jsonnet/test/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading
Loading