diff --git a/examples/ckb-js-script/deps/README.md b/examples/ckb-js-script/deps/README.md index fdde6e71..de72fcd6 100644 --- a/examples/ckb-js-script/deps/README.md +++ b/examples/ckb-js-script/deps/README.md @@ -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. diff --git a/examples/ckb-js-script/deps/ckb-js-vm b/examples/ckb-js-script/deps/ckb-js-vm index 8e62cb3c..bef946df 100755 Binary files a/examples/ckb-js-script/deps/ckb-js-vm and b/examples/ckb-js-script/deps/ckb-js-vm differ diff --git a/examples/ckb-js-script/deps/compile.awk b/examples/ckb-js-script/deps/compile.awk new file mode 100644 index 00000000..bacc338c --- /dev/null +++ b/examples/ckb-js-script/deps/compile.awk @@ -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 + } + } +} diff --git a/website/docs/script/ScriptHeaders.tsx b/website/docs/script/ScriptHeaders.tsx index 613b1689..8c7e596e 100644 --- a/website/docs/script/ScriptHeaders.tsx +++ b/website/docs/script/ScriptHeaders.tsx @@ -107,6 +107,120 @@ export const ScriptHeaders: ScriptHeadersType = { , ], }, + js: { + time: "5 - 7 min", + topics: [ + { label: "Script", link: "/docs/tech-explanation/script" }, + { + label: "CKB-VM", + link: "/docs/tech-explanation/ckb-vm", + }, + { label: "Cell Model", link: "/docs/tech-explanation/cell-model" }, + { + label: "Transaction", + link: "/docs/tech-explanation/glossary#transaction", + }, + ], + tools: [ +
+ + git + + , + + make + + , + + sed + + , + + bash + + , + + sha256sum + + {" and others Unix utilities."} +
, +
+ Rust + {" and riscv64 target: "}{" "} + rustup target add riscv64imac-unknown-none-elf +
, +
+ + Clang 18+ + + + +
, +
+ + cargo-generate + +
, +
+ + {"ckb-debugger >= v0.118.0"} + + +
, + ], + }, typeid: { time: "5 - 7 min", topics: [ diff --git a/website/docs/script/_script-in-js.mdx b/website/docs/script/_script-in-js.mdx deleted file mode 100644 index 42cd17f8..00000000 --- a/website/docs/script/_script-in-js.mdx +++ /dev/null @@ -1,6 +0,0 @@ ---- -id: script-in-js -title: Run Script on Top of JS runtime (Coming soon...) ---- - -# Coming soon... diff --git a/website/docs/script/js-script.mdx b/website/docs/script/js-script.mdx index 0cefc328..19ff43cb 100644 --- a/website/docs/script/js-script.mdx +++ b/website/docs/script/js-script.mdx @@ -13,9 +13,9 @@ import ScriptTools from "./_ScriptTools.mdx"; # Tutorial: Run JavaScript Code on CKB ## The High-Level Idea @@ -40,7 +40,7 @@ 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 -You will need `clang 16+` to build the `ckb-js-vm` binary: +You will need `clang 18+` to build the `ckb-js-vm` binary: ::: ```sh @@ -87,11 +87,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 @@ -102,7 +101,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 @@ -191,12 +190,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 ... ``` @@ -238,7 +238,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 @@ -429,7 +435,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 ```