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

Add urlencode in jsonnet and use console panic hook only in nodejs case #388

Merged
merged 2 commits into from
Jun 14, 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
1 change: 1 addition & 0 deletions JS/jsonnet/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
Cargo.lock
package-lock.json
yarn.lock
dist/
6 changes: 6 additions & 0 deletions JS/jsonnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ wasm-bindgen-file-reader = "1"
regex = "1.10.3"
js-sys = "0.3.69"
console_error_panic_hook = "0.1.7"
urlencoding = "2.1.3"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"

[features]
nodejs = []
default = ["nodejs"]

8 changes: 4 additions & 4 deletions JS/jsonnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"version": "0.3.1",
"exports": {
"import": {
"default": "./src/index.mjs",
"types": "./src/index.d.mts"
"types": "./src/index.d.mts",
"default": "./src/index.mjs"
},
"require": {
"default": "./src/index.js",
"types": "./src/index.d.ts"
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
}
}
100 changes: 77 additions & 23 deletions JS/jsonnet/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use jrsonnet_stdlib::{builtin_type, Settings};
use std::{cell::{Ref, RefCell, RefMut}, collections::HashMap, rc::Rc};
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashMap,
rc::Rc,
};

use jrsonnet_evaluator::function::builtin;
use jrsonnet_evaluator::{
error::ErrorKind::{self, ImportSyntaxError}, function::{builtin::{ NativeCallback, NativeCallbackHandler}, FuncVal, TlaArg}, stdlib, tb, trace::PathResolver, ContextBuilder, ContextInitializer, Error, ObjValue, ObjValueBuilder, Result, State, Thunk, Val,
function::builtin,
error::ErrorKind::{self, ImportSyntaxError},
function::{
builtin::{NativeCallback, NativeCallbackHandler},
FuncVal, TlaArg,
},
stdlib, tb,
trace::PathResolver,
typed::{ComplexValType, Typed, ValType},
ContextBuilder, ContextInitializer, Error, ObjValue, ObjValueBuilder, ObjectLike, Result,
State, Thunk, Val,
};
use jrsonnet_gcmodule::Trace;
use jrsonnet_parser::{IStr, Source};
use jrsonnet_stdlib::{StdTracePrinter, TracePrinter};



// Implement NativeCallbackHandler for your native function

#[builtin]
Expand All @@ -22,8 +33,7 @@ fn join(a: String, b: String) -> String {
fn includes(a: String, b: String) -> bool {
if a.contains(&b) {
return true;
}
else {
} else {
return false;
}
}
Expand All @@ -50,17 +60,61 @@ fn regex_match(a: String, b: String) -> Vec<String> {
matches
}

// #[derive(Debug)]
// struct DateTimeFormatOptions {
// hour: String,
// minute: String,
// hour12: bool,
// }

// #[builtin]
// fn format_date(
// hour: Option<String>,
// minute: Option<String>,
// hour12: bool,
// timeZone: Option<String>,
// ) -> String {
// super::log(&format!("Debug : {:?}", hour));
// super::log(&format!("Debug : {:?}", minute));
// super::log(&format!("Debug : {:?}", hour12));
// println!("Debug : {:?}", timeZone);
// String::from("Formatted date")
// }

/// Returns current date and time in utc
// #[builtin]
// fn date(
// dateTime: Option<String>,
// timeStamp: Option<u32>,
// year: Option<u16>,
// monthIndex: Option<u16>,
// day: Option<u8>,
// hours: Option<u8>,
// minutes: Option<u8>,
// seconds: Option<u8>,
// milliseconds: Option<u32>,
// ) -> String {
// String::from("Good datetime")
// }

#[builtin]
fn url_encode(a: String) -> String {
urlencoding::encode(&a).into_owned()
}

fn arakoolib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
let mut builder = ObjValueBuilder::new();
builder.method(
"native",
builtin_native {
settings: settings.clone(),
},
);
"native",
builtin_native {
settings: settings.clone(),
},
);
builder.method("join", join::INST);
builder.method("regexMatch", regex_match::INST);
builder.method("includes", includes::INST);
// builder.method("formatDate", format_date::INST);
builder.method("urlEncode", url_encode::INST);
builder.build()
}

Expand All @@ -75,20 +129,20 @@ pub struct ArakooContextInitializer {
}

fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
let source_name = format!("<extvar:{name}>");
Source::new_virtual(source_name.into(), code.into())
let source_name = format!("<extvar:{name}>");
Source::new_virtual(source_name.into(), code.into())
}

#[builtin(fields(
settings: Rc<RefCell<Settings>>,
))]
pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {
this.settings
.borrow()
.ext_natives
.get(&x)
.cloned()
.map_or(Val::Null, Val::Func)
this.settings
.borrow()
.ext_natives
.get(&x)
.cloned()
.map_or(Val::Null, Val::Func)
}

impl ArakooContextInitializer {
Expand All @@ -101,7 +155,7 @@ impl ArakooContextInitializer {
};
let settings = Rc::new(RefCell::new(settings));
let stdlib_obj = jrsonnet_stdlib::stdlib_uncached(settings.clone());

let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));
let arakoolib_obj = arakoolib_uncached(settings.clone());
let arakoolib_thunk = Thunk::evaluated(Val::Obj(arakoolib_obj));
Expand Down Expand Up @@ -163,11 +217,11 @@ impl jrsonnet_evaluator::ContextInitializer for ArakooContextInitializer {
fn reserve_vars(&self) -> usize {
1
}

fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
self.context.clone()
}

fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
builder.bind("std", self.stdlib_thunk.clone());
builder.bind("arakoo", self.arakoolib_thunk.clone());
Expand Down
4 changes: 2 additions & 2 deletions JS/jsonnet/src/jsonnet.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const isArakoo = process.env.arakoo;
// const isArakoo = process.env.arakoo;

let Jsonnet;

if (!isArakoo) {
if (!process.env.arakoo) {
let module = require("./jsonnet_wasm.js");
let {
jsonnet_evaluate_snippet,
Expand Down
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 jsonnet_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
Loading