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

expose autocannonize func for keyexpr wrapper #101

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion zenoh-keyexpr-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::TryFrom;

use wasm_bindgen::prelude::*;
use zenoh_keyexpr::key_expr;
use zenoh_keyexpr::{canon::Canonize, key_expr};

#[wasm_bindgen]
pub fn new_key_expr(key_expr_str: String) -> Result<(), String> {
Expand Down Expand Up @@ -47,3 +47,9 @@ pub fn intersects(ke1: String, ke2: String) -> Result<bool, String> {
let b = key_expr::OwnedKeyExpr::new(ke2).map_err(|x| x.to_string())?;
Ok(a.intersects(&b))
}

#[wasm_bindgen]
pub fn autocanonize(ke: String) -> Result<String, String> {
let keyexpr = key_expr::OwnedKeyExpr::autocanonize(ke).map_err(|x| x.to_string())?;
Ok(keyexpr.to_string())
}
25 changes: 20 additions & 5 deletions zenoh-ts/src/key_expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ███████ ██ ██ ██ ██ ██

import { new_key_expr, join, concat, includes, intersects } from "./key_expr/zenoh_keyexpr_wrapper.js"
import { new_key_expr, join, concat, includes, intersects, autocanonize} from "./key_expr/zenoh_keyexpr_wrapper.js"

export type IntoKeyExpr = KeyExpr | String | string;

export class KeyExpr {
/**
* Class to represent a Key Expression in Zenoh
*/

// not to be used directly
private _inner: string;

Expand All @@ -49,37 +50,51 @@ export class KeyExpr {
return this._inner;
}

/*
/**
* Joins both sides, inserting a / in between them.
* This should be your preferred method when concatenating path segments.
* @returns KeyExpr
*/
join(other: IntoKeyExpr): KeyExpr {
const key_expr = this.call_wasm<string>(other, join)
return new KeyExpr(key_expr)
}

/*
/**
* Performs string concatenation and returns the result as a KeyExpr if possible.
* @returns KeyExpr
*/
concat(other: IntoKeyExpr): KeyExpr {
const key_expr = this.call_wasm<string>(other, concat)
return new KeyExpr(key_expr)
}

/*
/**
* Returns true if this includes other, i.e. the set defined by this contains every key belonging to the set defined by other.
* @returns KeyExpr
*/
includes(other: IntoKeyExpr): boolean {
return this.call_wasm<boolean>(other, includes)
}

/*
/**
* Returns true if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the sets defined by self and other.
* @returns KeyExpr
*/
intersects(other: IntoKeyExpr): boolean {
return this.call_wasm<boolean>(other, intersects)
}

/**
* Returns the canon form of a key_expr
* @returns KeyExpr
*/
autocanonize(other: IntoKeyExpr): KeyExpr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is incorrect. Also this should be free function, it doesn't use "this".
Probably this require to make "call_wasm" free function too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or make this method static, ts allows this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_wasm is just a function that is part of the keyexpr class, to abstract away return types and function calls.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering why autocanonize is a method of already created KeyExpr? This function should accept string and create key new expression. I.e. this is a constructor. It can be implemented like this:

class KeyExpr {
...
	public static autocanonize(s: string): KeyExpr | null {
		const key_expr = ??? call_wasm<String>(s, autocanonize);
                // we should be able to call wasm without `this`
		if key_expr.is_ok() { // not sure how to really check it to ok
                   return new KeyExpr(key_expr)
                } else {
                   return null; // TODO: it is the way how we fail in zenoh_ts? Need to check
                }
	}
}

const key_expr = this.call_wasm<String>(other, autocanonize)
return new KeyExpr(key_expr)
}


private call_wasm<T>(other: IntoKeyExpr, fn: (expr1: string, expr2: string) => T): T {
let ke;
if (other instanceof KeyExpr) {
Expand Down
5 changes: 5 additions & 0 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ export function includes(ke1: string, ke2: string): boolean;
* @returns {boolean}
*/
export function intersects(ke1: string, ke2: string): boolean;
/**
* @param {string} ke
* @returns {string}
*/
export function autocanonize(ke: string): string;
31 changes: 31 additions & 0 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,37 @@ export function intersects(ke1, ke2) {
}
}

/**
* @param {string} ke
* @returns {string}
*/
export function autocanonize(ke) {
let deferred3_0;
let deferred3_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(ke, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.autocanonize(retptr, ptr0, len0);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
var ptr2 = r0;
var len2 = r1;
if (r3) {
ptr2 = 0; len2 = 0;
throw takeObject(r2);
}
deferred3_0 = ptr2;
deferred3_1 = len2;
return getStringFromWasm0(ptr2, len2);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
}
}

export function __wbindgen_string_new(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
Expand Down
Binary file modified zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper_bg.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function join(a: number, b: number, c: number, d: number, e: number): voi
export function concat(a: number, b: number, c: number, d: number, e: number): void;
export function includes(a: number, b: number, c: number, d: number, e: number): void;
export function intersects(a: number, b: number, c: number, d: number, e: number): void;
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

garbage in file, build is passed only because this file is regenerated on build

export function autocanonize(a: number, b: number, c: number): void;
=======
>>>>>>> main
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
Expand Down
Loading