Skip to content

Commit

Permalink
0.3.24
Browse files Browse the repository at this point in the history
  • Loading branch information
andreespirela committed Oct 6, 2023
1 parent 5c79b6f commit 080bba8
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 8 deletions.
6 changes: 6 additions & 0 deletions crates/exm/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@
Object.defineProperty(window, "EXM", {
get: () => {
const isEXM = Deno.core.opSync("op_get_executor_settings", "EXM");
const preKv = (globalThis?.exmContext?.kv || {});
if(Object.values(preKv).length > 0) {
Object.entries(preKv).forEach(([key, val]) => {
baseIns.putKv(key, val);
});
}
if (!window[ExmSymbol]) {
Object.defineProperty(window, ExmSymbol, {
value: isEXM ? baseIns : {
Expand Down
4 changes: 2 additions & 2 deletions crates/js/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ impl Runtime {
let context = scope.get_current_context();

if maybe_exm_context.is_some() {
let exm_context = maybe_exm_context.unwrap();
let inner_scope = &mut v8::ContextScope::new(scope, context);

let global = context.global(inner_scope);
let v8_key = serde_v8::to_v8(inner_scope, "exmContext").unwrap();
let v8_val =
serde_v8::to_v8(inner_scope, maybe_exm_context.unwrap()).unwrap();
let v8_val = serde_v8::to_v8(inner_scope, exm_context).unwrap();
global.set(inner_scope, v8_key, v8_val);
}
};
Expand Down
3 changes: 3 additions & 0 deletions crates/smartweave/smartweave.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@
},
del(key) {
globalThis.EXM.delKv(key);
},
getAll() {
return globalThis.EXM.kv;
}
};
}
Expand Down
43 changes: 43 additions & 0 deletions js/napi/napi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,47 @@ export async function handle(state, action) {
expect(simulate.state).toEqual({"8f39fb4940c084460da00a876a521ef2ba84ad6ea8d2f5628c9f1f8aeb395342":1595580000000000});
});

test("KV Test", async () => {
const buffer = new TextEncoder().encode(`
/**
*
* @param state is the current state your application holds
* @param action is an object containing { input, caller } . Most of the times you will only use action.input which contains the input passed as a write operation
* @returns {Promise<{ users: Array<{ username: string}> }>}
*/
export async function handle(state, action) {
const { username } = action.input; SmartWeave.kv.put(username, "world");
state.users.push({ username });
return { state, result: 'Hello World' };
}
`);

const simulate = await simulateContract({
contractId: "",
maybeContractSource: {
contractType: SimulateContractType.JAVASCRIPT,
contractSrc: buffer
},
interactions: [{
id: "ABCD",
owner: "2asdaskdsapdk012",
quantity: "1000",
reward: "203123921",
target: "none",
tags: [],
input: JSON.stringify({"username":"Andres"})
}],
contractInitState: JSON.stringify({}),
maybeSettings: {
},
maybeExmContext: JSON.stringify({
requests: {},
kv: {
'PreviouslyAddedKey': 'Buccees'
}
})
});
console.log(simulate);
});

})
2 changes: 1 addition & 1 deletion js/napi/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@three-em/node-darwin-arm64",
"version": "0.3.23",
"version": "0.3.24",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion js/napi/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@three-em/node-darwin-x64",
"version": "0.3.23",
"version": "0.3.24",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion js/napi/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@three-em/node-linux-x64-gnu",
"version": "0.3.23",
"version": "0.3.24",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion js/napi/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@three-em/node-win32-x64-msvc",
"version": "0.3.23",
"version": "0.3.24",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion js/napi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@three-em/node",
"version": "0.3.23",
"version": "0.3.24",
"author": "Divy Srivastava <[email protected]>",
"license": "MIT",
"napi": {
Expand Down
39 changes: 39 additions & 0 deletions js/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,43 @@ mod tests {
let contract = simulate_contract(execution_context).await.unwrap();
println!("{}", contract.state);
}

#[tokio::test]
pub async fn simulate_kv() {
let contract_source_bytes =
include_bytes!("../../../testdata/contracts/kv.js");
let contract_source_vec = contract_source_bytes.to_vec();
let execution_context: SimulateExecutionContext =
SimulateExecutionContext {
contract_id: String::new(),
interactions: vec![SimulateInput {
id: String::from("abcd"),
owner: String::from("210392sdaspd-asdm-asd_sa0d1293-lc"),
quantity: String::from("12301"),
reward: String::from("12931293"),
target: None,
tags: vec![],
block: None,
input: serde_json::json!({
"key": "Name",
"value": "Andres"
})
.to_string(),
}],
contract_init_state: Some(r#"{"users": []}"#.into()),
maybe_config: None,
maybe_cache: Some(false),
maybe_bundled_contract: None,
maybe_settings: None,
maybe_exm_context: Some(r#"{"requests": {}, "kv": {"Pre-key": "prevalue"}}"#.into()),
maybe_contract_source: Some(ContractSource {
contract_src: contract_source_vec.into(),
contract_type: SimulateContractType::JAVASCRIPT,
}),
};

let contract = simulate_contract(execution_context).await.unwrap();
println!("{}", contract.exm_context);
assert_eq!(contract.exm_context.to_string(), r#"{"requests":{},"kv":{"Name":"Andres","Pre-key":"prevalue"}}"#);
}
}
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@three-em/js",
"type": "module",
"version": "0.3.17",
"version": "0.3.18",
"main": "index.js",
"license": "MIT"
}
6 changes: 6 additions & 0 deletions testdata/contracts/kv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export async function handle(state, action) {
const input = action.input;
const { key, value } = input;
SmartWeave.kv.put(key, value);
return { result: JSON.stringify(SmartWeave.kv.getAll()) }
}

0 comments on commit 080bba8

Please sign in to comment.