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 2 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())
}
70 changes: 37 additions & 33 deletions zenoh-ts/src/key_expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ██ ███████ ██ ███████ ██ ██ ██ ██ ██

// 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;

Expand All @@ -41,8 +41,8 @@ export class KeyExpr {
// `new_key_expr` calls the `key_expr::OwnedKeyExpr::new` in Rust
// if this function fails, the keyexpr is invalid, and an exception is thrown in Wasm and propagated here
// else the Key Expression is valid and we can store the string it represents in the class
// TODO: Add back in When bundling WASM is fixed
// new_key_expr(ke);

new_key_expr(ke);
this._inner = ke;
}

Expand All @@ -54,48 +54,52 @@ export class KeyExpr {
* Joins both sides, inserting a / in between them.
* This should be your preferred method when concatenating path segments.
*/
// TODO: Add back in When bundling WASM is fixed
// join(other: IntoKeyExpr): KeyExpr {
// const key_expr = this.call_wasm<string>(other, join)
// return new KeyExpr(key_expr)
// }
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.
*/
// TODO: Add back in When bundling WASM is fixed
// concat(other: IntoKeyExpr): KeyExpr {
// const key_expr = this.call_wasm<string>(other, concat)
// return new KeyExpr(key_expr)
// }
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.
*/
// TODO: Add back in When bundling WASM is fixed
// includes(other: IntoKeyExpr): boolean {
// return this.call_wasm<boolean>(other, includes)
// }
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.
*/
// TODO: Add back in When bundling WASM is fixed
// intersects(other: IntoKeyExpr): boolean {
// return this.call_wasm<boolean>(other, intersects)
// }
intersects(other: IntoKeyExpr): boolean {
return this.call_wasm<boolean>(other, intersects)
}

// TODO: Add back in When bundling WASM is fixed
// private call_wasm<T>(other: IntoKeyExpr, fn: (expr1: string, expr2: string) => T): T {
// let ke;
// if (other instanceof KeyExpr) {
// ke = other._inner;
// } else if (other instanceof String) {
// ke = other.toString();
// } else {
// ke = other;
// }
// return fn(this._inner, ke)
// }
/*
* 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.
*/
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) {
ke = other._inner;
} else if (other instanceof String) {
ke = other.toString();
} else {
ke = other;
}
return fn(this._inner, ke)
}

}
28 changes: 28 additions & 0 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {string} key_expr_str
*/
export function new_key_expr(key_expr_str: string): void;
/**
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
export function join(ke1: string, ke2: string): string;
/**
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
export function concat(ke1: string, ke2: string): string;
/**
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
export function includes(ke1: string, ke2: string): boolean;
/**
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
export function intersects(ke1: string, ke2: string): boolean;
/**
* @param {string} ke
* @returns {string}
*/
export function autocanonize(ke: string): string;
5 changes: 3 additions & 2 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import * as wasm from "./zenoh_keyexpr_wrapper_bg.wasm";
export * from "./zenoh_keyexpr_wrapper_bg.js";
import { __wbg_set_wasm } from "./zenoh_keyexpr_wrapper_bg.js";
__wbg_set_wasm(wasm);
__wbg_set_wasm(wasm);
export * from "./zenoh_keyexpr_wrapper_bg.js";
67 changes: 49 additions & 18 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ function takeObject(idx) {
return ret;
}
/**
* @param {string} key_expr_str
*/
* @param {string} key_expr_str
*/
export function new_key_expr(key_expr_str) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand All @@ -139,10 +139,10 @@ export function new_key_expr(key_expr_str) {
}

/**
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
export function join(ke1, ke2) {
let deferred4_0;
let deferred4_1;
Expand Down Expand Up @@ -173,10 +173,10 @@ export function join(ke1, ke2) {
}

/**
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
* @param {string} ke1
* @param {string} ke2
* @returns {string}
*/
export function concat(ke1, ke2) {
let deferred4_0;
let deferred4_1;
Expand Down Expand Up @@ -207,10 +207,10 @@ export function concat(ke1, ke2) {
}

/**
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
export function includes(ke1, ke2) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand All @@ -232,10 +232,10 @@ export function includes(ke1, ke2) {
}

/**
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
* @param {string} ke1
* @param {string} ke2
* @returns {boolean}
*/
export function intersects(ke1, ke2) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
Expand All @@ -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.
19 changes: 10 additions & 9 deletions zenoh-ts/src/key_expr/zenoh_keyexpr_wrapper_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export const new_key_expr: (a: number, b: number, c: number) => void;
export const join: (a: number, b: number, c: number, d: number, e: number) => void;
export const concat: (a: number, b: number, c: number, d: number, e: number) => void;
export const includes: (a: number, b: number, c: number, d: number, e: number) => void;
export const intersects: (a: number, b: number, c: number, d: number, e: number) => void;
export const __wbindgen_add_to_stack_pointer: (a: number) => number;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __wbindgen_free: (a: number, b: number, c: number) => void;
export function new_key_expr(a: number, b: number, c: number): void;
export function join(a: number, b: number, c: number, d: number, e: number): void;
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;
export function autocanonize(a: number, b: number, c: number): void;
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;
export function __wbindgen_free(a: number, b: number, c: number): void;
Loading