Skip to content

Commit

Permalink
Added toJSON() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaMineJP committed May 11, 2024
1 parent a0834e8 commit d5949fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ This version is compatible with [`960f8d139940fa0814d3fac44da9a2975642f5d3`](htt
- Exposed `run_chia_program`
- Exposed `run_clvm`
- Exposed `LazyNode`
- Added `toJSON()` method to `LazyNode` (This is not implemented in `clvm_wasm`)
- Exposed `Flag`
- Exposed `serialized_length`
- Exposed `node_from_bytes`
- Added `asUint8Array()` method to `Stream`
- Added `toJSON()` method to `Bytes`
- Added `toJSON()` method to `SExp`
- Added tests

## [2.0.1]
Expand Down
10 changes: 10 additions & 0 deletions src/SExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ export class SExp implements CLVMType {
return this.as_bin().hex();
}

public toJSON(){
if(this.pair){
return this.pair;
}
if(this.atom){
return this.atom.hex();
}
throw new EvalError("Invalid object", this);
}

public __repr__(){
return `SExp(${this.as_bin().hex()})`;
}
Expand Down
12 changes: 12 additions & 0 deletions src/__clvm_wasm__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* 4. Annotate typings, fix lint issues
* 5. Paste loader code preserved in the previous procedure
* 6. Add `__wb*` functions to the `imports` object.
* 7. Add `toJSON()` method to `LazyNode`.
*/
import {Word32Array} from "jscrypto/Word32Array";

type ClvmWasmExports = {
memory: WebAssembly.Memory;
Expand Down Expand Up @@ -397,6 +399,16 @@ export class LazyNode {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

toJSON() {
if(this.pair){
return this.pair;
}
if(this.atom){
return (new Word32Array(this.atom)).toString();
}
throw new Error("Invalid object");
}
}

export function __wbg_lazynode_new(arg0: number) {
Expand Down
4 changes: 4 additions & 0 deletions src/__type_compatibility__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ export class Bytes {

return 0;
}

public toJSON(){
return this.hex();
}
}

export function b(utf8Str: string, type:"utf8"|"hex" = "utf8"){
Expand Down

0 comments on commit d5949fe

Please sign in to comment.