Skip to content

Commit

Permalink
Add function in jsonnet (#358)
Browse files Browse the repository at this point in the history
* Adding error panic hook for better debugging

* Added functionality to check if a string contains a pattern

* Support both cjs require and es6 imports for jsonnet library
  • Loading branch information
redoC-A2k authored May 13, 2024
1 parent 691c599 commit dbd06e6
Show file tree
Hide file tree
Showing 19 changed files with 519 additions and 506 deletions.
1 change: 1 addition & 0 deletions JS/jsonnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 6 additions & 6 deletions JS/jsonnet/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 <<EOF >"$OUTPUT_FILE"
import * as imports from "./${BG_FILE}";
Expand All @@ -59,8 +59,8 @@ if ((typeof process !== 'undefined') && (process.release.name === 'node')) {
export * from "./${BG_FILE}";
EOF

cat <<EOF >"$OUT_FOLDER/index.js"
import Jsonnet from "./jsonnet.js";
cat <<EOF >"$OUT_FOLDER/index.mjs"
const {Jsonnet} = await import("./jsonnet.js")
export default Jsonnet;
EOF
Expand Down Expand Up @@ -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
13 changes: 10 additions & 3 deletions JS/jsonnet/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
7 changes: 5 additions & 2 deletions JS/jsonnet/read-file.js
Original file line number Diff line number Diff line change
@@ -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 }
11 changes: 11 additions & 0 deletions JS/jsonnet/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// log(&a);
Expand Down Expand Up @@ -50,6 +60,7 @@ fn arakoolib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
);
builder.method("join", join::INST);
builder.method("regexMatch", regex_match::INST);
builder.method("includes", includes::INST);
builder.build()
}

Expand Down
10 changes: 10 additions & 0 deletions JS/jsonnet/src/index.d.mts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 2 additions & 3 deletions JS/jsonnet/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
import Jsonnet from "./jsonnet.js";

export default Jsonnet;
const Jsonnet = require("./jsonnet.js")
module.exports = Jsonnet;
3 changes: 3 additions & 0 deletions JS/jsonnet/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Jsonnet from "./jsonnet.js";

export default Jsonnet;
6 changes: 3 additions & 3 deletions JS/jsonnet/src/jsonnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,7 +13,7 @@ if (!isArakoo) {
get_func,
set_func,
register_native_callback,
} = await module;
} = module;
Jsonnet = class Jsonnet {
constructor() {
this.vm = jsonnet_make();
Expand Down Expand Up @@ -76,4 +76,4 @@ if (!isArakoo) {
};
}

export default Jsonnet;
module.exports = Jsonnet;
54 changes: 27 additions & 27 deletions JS/jsonnet/src/jsonnet_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit dbd06e6

Please sign in to comment.