-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend plugins to rust contracts
- Loading branch information
1 parent
4f4647b
commit a69e2fc
Showing
7 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
crates/pst/contract/implementation/src/actions/the_answer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_wasm_bindgen::from_value; | ||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(js_name = "theAnswer")] | ||
pub fn the_answer() -> u8; | ||
|
||
#[wasm_bindgen(js_name = "multiplyTheAnswer")] | ||
pub fn multiply_the_answer(times: u8) -> u8; | ||
|
||
#[wasm_bindgen(js_name = "concatenateTheAnswer")] | ||
pub fn concatenate_the_answer(prefix: String) -> String; | ||
|
||
#[wasm_bindgen(js_name = "wrapTheAnswer")] | ||
pub fn the_answer_wrapped(wrapper: JsValue) -> JsValue; | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct TheAnswerWrapper { | ||
pub context: String, | ||
pub answer: u8, | ||
} | ||
|
||
// convenient rust-typed wrapper for JsValue -> JsValue method | ||
pub fn wrap_the_answer(context: &str) -> TheAnswerWrapper { | ||
from_value::<TheAnswerWrapper>(the_answer_wrapped(JsValue::from_str(context))) | ||
.unwrap_or_default() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { WarpPlugin, WarpPluginType } from '../../../lib/types'; | ||
|
||
// complicated logic of our plugin | ||
const theAnswer = () => 42; | ||
const multiplyTheAnswer = (multiplier: number) => multiplier * theAnswer(); | ||
const concatenateTheAnswer = (prefix: string) => prefix + theAnswer(); | ||
const wrapTheAnswer = (context: unknown) => { | ||
return { answer: theAnswer(), context }; | ||
}; | ||
|
||
// ugly rust imports | ||
const rustImports = (helpers) => { | ||
return { | ||
__wbg_theAnswer: typeof theAnswer == 'function' ? theAnswer : helpers.notDefined('theAnswer'), | ||
__wbg_multiplyTheAnswer: | ||
typeof multiplyTheAnswer == 'function' ? multiplyTheAnswer : helpers.notDefined('multiplyTheAnswer'), | ||
__wbg_concatenateTheAnswer: function () { | ||
return helpers.logError(function (arg0, arg1, arg2) { | ||
try { | ||
const ret = concatenateTheAnswer(helpers.getStringFromWasm0(arg1, arg2)); | ||
const ptr0 = helpers.passStringToWasm0( | ||
ret, | ||
helpers.wasm().__wbindgen_malloc, | ||
helpers.wasm().__wbindgen_realloc | ||
); | ||
const len0 = helpers.WASM_VECTOR_LEN(); | ||
helpers.getInt32Memory0()[arg0 / 4 + 1] = len0; | ||
helpers.getInt32Memory0()[arg0 / 4 + 0] = ptr0; | ||
} finally { | ||
helpers.wasm().__wbindgen_free(arg1, arg2); | ||
} | ||
// eslint-disable-next-line | ||
}, arguments); | ||
}, | ||
wrapTheAnswer | ||
}; | ||
}; | ||
|
||
export class TheAnswerExtension implements WarpPlugin<unknown, void> { | ||
process(input): void { | ||
// pick our namespace and expose our plugin logic to JS contracts | ||
input.theAnswer = { | ||
theAnswer, | ||
multiplyTheAnswer, | ||
concatenateTheAnswer, | ||
wrapTheAnswer, | ||
// the following line effectively exposes your glue code imports to WASM module | ||
rustImports | ||
}; | ||
} | ||
|
||
type(): WarpPluginType { | ||
return 'smartweave-extension-the-answer'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters