Skip to content

feat(wasm-dpp): implement function to produce generics from JsValue #712

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

Merged
merged 2 commits into from
Jan 13, 2023
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
43 changes: 22 additions & 21 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/wasm-dpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ The WASM JavaScript binding of the Rust implementation of the [Dash Platform Pro
In order for this binding to work, you have to have a rs-platform cloned
alongside platform repo, so you can have access to the rust dpp.

## IMPORTANT! Build on a Mac
## IMPORTANT!
### Build on a Mac

To build on a mac, you need to perform two steps. First, install `clang`
from the homebrew. XCode's `clang` doesn't ship with the WASM support. Second,
Expand All @@ -24,6 +25,9 @@ instead.

Alternatively, you can add the following to the `yarn workspace @dashevo/wasm-dpp build:node:mac` instead.

### Class names minification
Library consumers must ignore class names minification for `@dashevo/wasm-dpp` library in their bundlers.

## Table of Contents

- [Install](#install)
Expand Down
1 change: 1 addition & 0 deletions packages/wasm-dpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"string_decoder": "^1.3.0",
"terser-webpack-plugin": "^5.3.1",
"ts-loader": "^8.0.2",
"typescript": "^3.9.5",
"url": "^0.11.0",
Expand Down
31 changes: 31 additions & 0 deletions packages/wasm-dpp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use dpp::{
use js_sys::Function;
use serde::de::DeserializeOwned;
use serde_json::Value;
use wasm_bindgen::convert::RefFromWasmAbi;
use wasm_bindgen::prelude::*;

use crate::errors::{from_dpp_err, RustConversionError};
Expand Down Expand Up @@ -121,3 +122,33 @@ impl<T> WithJsError<T> for Result<T, serde_json::Error> {
}
}
}

pub fn generic_of_js_val<T: RefFromWasmAbi<Abi = u32>>(
js_value: &JsValue,
class_name: &str,
) -> Result<T::Anchor, JsValue> {
if !js_value.is_object() {
return Err(JsValue::from_str(
format!("Value supplied as {} is not an object", class_name).as_str(),
));
}

let ctor_name = js_sys::Object::get_prototype_of(js_value)
.constructor()
.name();

if ctor_name == class_name {
let ptr = js_sys::Reflect::get(js_value, &JsValue::from_str("ptr"))?;
let ptr_u32: u32 = ptr
.as_f64()
.ok_or(JsValue::from("Invalid JS object pointer"))? as u32;
let reference = unsafe { T::ref_from_abi(ptr_u32) };
Ok(reference)
} else {
let error_string = format!(
"JS object constructor name mismatch. Expected {}, provided {}.",
class_name, ctor_name
);
Err(JsValue::from(&error_string))
}
}
9 changes: 9 additions & 0 deletions packages/wasm-dpp/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
entry: './lib/index.ts',
Expand All @@ -14,6 +15,14 @@ module.exports = {
// as webpack names global object "self" for some reason
globalObject: 'this',
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
terserOptions: {
keep_classnames: true,
},
})],
},
mode: 'production',
module: {
rules: [
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ __metadata:
stream-browserify: ^3.0.0
stream-http: ^3.2.0
string_decoder: ^1.3.0
terser-webpack-plugin: ^5.3.1
ts-loader: ^8.0.2
typescript: ^3.9.5
url: ^0.11.0
Expand Down