Skip to content

Commit

Permalink
wip: update ckb-js-vm bin waitting for spawn ckb-std
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu committed Nov 18, 2024
1 parent 7009ad0 commit 2a4ab1a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
8 changes: 7 additions & 1 deletion examples/ckb-js-script/deps/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
ckb-js-vm is a binary built for javascript engine on CKB-VM, you can rebuild it from https://github.com/nervosnetwork/ckb-js-vm
# ckb-js-vm

`ckb-js-vm` is a binary built for javascript engine on CKB-VM, you can rebuild it from https://github.com/nervosnetwork/ckb-js-vm

This binary is locked with the commit hash [c158de314783151b6fb023420dd5d5bff2bc2772](https://github.com/nervosnetwork/ckb-js-vm/commit/c158de314783151b6fb023420dd5d5bff2bc2772)

`compile.awk` is copied from https://github.com/nervosnetwork/ckb-js-vm/blob/c158de314783151b6fb023420dd5d5bff2bc2772/tools/compile.awk and used to compile js source code into binary for CKB-VM.
Binary file modified examples/ckb-js-script/deps/ckb-js-vm
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/ckb-js-script/deps/compile.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/awk -f

{
# Check if the line starts with "Script log: "
if ($0 ~ /^Script log: /) {
# Remove "Script log: " from the line
line_data = gensub(/^Script log: /, "", "g")

# If the remaining line is not empty, print it
if (line_data != "") {
print line_data
}
}
}
6 changes: 0 additions & 6 deletions website/docs/script/_script-in-js.mdx

This file was deleted.

26 changes: 18 additions & 8 deletions website/docs/script/js-script.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ The `ckb-js-vm` is a binary that can be used both in the CLI and in the on-chain
binary and give it a try to see if it works as expected.

:::info
<<<<<<< HEAD
You will need `Clang ≥v18` to build the `ckb-js-vm` binary:
=======
You will need `clang 18+` to build the `ckb-js-vm` binary:
>>>>>>> 97de069 (wip: update ckb-js-vm bin waitting for spawn ckb-std)
:::

```sh
Expand Down Expand Up @@ -101,11 +105,10 @@ ckb-debugger --read-file tests/examples/hello.js --bin build/ckb-js-vm -- -r
```

```bash
Run from file, local access enabled. For testing only.
hello, world
Script log: Run from file, local access enabled. For Testing only.
Script log: hello, world
Run result: 0
Total cycles consumed: 30081070(2.9m)
Transfer cycles: 125121(122.2k), running cycles: 2955949(2.8m)
All cycles: 3016765(2.9M)
```

```mdx-code-block
Expand All @@ -116,7 +119,7 @@ Transfer cycles: 125121(122.2k), running cycles: 2955949(2.8m)
With the `-r` option, `ckb-js-vm` will read a local JS file via CKB-Debugger. This function is
intended for testing purposes and does not function in a production environment. However, we can see the
running output, which includes a `hello, world` message. The run result is 0, indicating that the `hello.js` Script executes successfully.
Also, you can see how many `cycles`(the overhead required to execute a Script) are needed to run the JS Script in the output as well.
Also, you can see how many [cycles](/docs/script/vm-cycle-limits)(the overhead required to execute a Script) are needed to run the JS Script in the output as well.

## Integrate ckb-js-vm

Expand Down Expand Up @@ -205,12 +208,13 @@ cd my-first-script-workspace
mkdir deps
```

Copy the `ckb-js-vm` binary we built before into the `deps` folder. When you're done, it should look like this:
Copy `ckb-js-vm/tools/compile.awk` and the `ckb-js-vm` binary we built before into the `deps` folder. When you're done, it should look like this:

```sh
--build
--contracts
--deps
--compile.awk
--ckb-js-vm
...
```
Expand Down Expand Up @@ -252,7 +256,13 @@ console.log("hello, ckb-js-script!");
#### Compile the `hello.js` into binary with CKB-Debugger

```sh
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | awk '/Run result: 0/{exit} {print}' | xxd -r -p > js/build/hello.bc
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | awk -f deps/compile.awk | xxd -r -p > js/build/hello.bc
```

If you are using Mac, change `awk` to `gawk` which can be installed by `brew install gawk`:

```sh
ckb-debugger --read-file js/hello.js --bin deps/ckb-js-vm -- -c | gawk -f deps/compile.awk | xxd -r -p > js/build/hello.bc
```

#### Write tests for the `hello.js` Script
Expand Down Expand Up @@ -443,7 +453,7 @@ cargo test -- --nocapture
```sh
running 1 test
[contract debug] hello, ckb-js-script!
consume cycles: 3070458
consume cycles: 2996560
test tests::hello_script ... ok
```

Expand Down

0 comments on commit 2a4ab1a

Please sign in to comment.