Skip to content

Commit

Permalink
Add script to introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Oct 12, 2023
1 parent 8e446b0 commit 0f5831f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ make all

## Examples

* [Demo Script: Simple UDT](./tests/ckb_js_tests/test_data/simple_udt.js)
* [Fibonacci Number](./tests/examples/fib.js)
* [Calculate PI](./tests/examples/pi_bigint.js)

Expand Down
12 changes: 12 additions & 0 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ binary. Finally, it is written as `hello.bc`.

`ckb-js-vm` can transparently run JavaScript bytecode or source files, which can also
be in file systems.

## Script
A ckb-js-vm script contains following data structure:

```
code_hash: <code_hash to ckb-js-vm cell>
hash_type: <hash_type>
args: <ckb-js-vm args, 2 bytes> <code_hash to JavaScript code cell, 32 bytes> <hash_type to JavaScript code cell, 1 byte> <JavaScript code args, variable length>
```

The tailing bytes are JavaScript code arguments which can be used by JavaScript.
Note: 2 bytes ckb-js-vm args are reserved for further use.
34 changes: 0 additions & 34 deletions tests/ckb_js_tests/test_data/simple_udt.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function compare_array(a, b) {
return true;
}


function unpack_script(buf) {
let script = new Uint32Array(buf);
let raw_data = new Uint8Array(buf);
Expand All @@ -36,39 +35,6 @@ function unpack_script(buf) {
return {'code_hash': code_hash, 'hash_type': hash_type, 'args': args};
}

function test() {
function assert_equal(a, b) {
if (a.byteLength != b.byteLength) {
throw Error(`not equal: ${a}\n${b}`);
}
for (let i = 0; i < a.byteLength; i++) {
if (a[i] != b[i]) {
throw Error(`not equal: ${a}\n${b}`);
}
}
}
let buf = new Uint8Array([
88, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 223, 151, 119, 120, 8, 155,
243, 63, 197, 31, 34, 69, 250, 109, 183, 250, 24, 25, 213, 3, 17, 49, 168, 61, 78, 203, 203, 108,
186, 7, 206, 145, 1, 35, 0, 0, 0, 0, 0, 145, 188, 29, 31, 200, 193, 146, 137, 231, 45, 21,
0, 77, 213, 6, 183, 98, 70, 191, 234, 209, 51, 145, 165, 179, 81, 121, 207, 124, 143, 55, 239, 1
]);
let script = unpack_script(buf.buffer);
let expect_code_hash = new Uint8Array([
223, 151, 119, 120, 8, 155, 243, 63, 197, 31, 34, 69, 250, 109, 183, 250,
24, 25, 213, 3, 17, 49, 168, 61, 78, 203, 203, 108, 186, 7, 206, 145,
]);
let code_hash = new Uint8Array(script.code_hash);
assert_equal(expect_code_hash == code_hash, 'code_hash mismatched');
assert(script.hash_type == 1, 'hash_type mismatched');
let expect_args = new Uint8Array([
0, 0, 145, 188, 29, 31, 200, 193, 146, 137, 231, 45, 21, 0, 77, 213, 6, 183,
98, 70, 191, 234, 209, 51, 145, 165, 179, 81, 121, 207, 124, 143, 55, 239, 1
])
let args = new Uint8Array(buf.args);
assert_equal(script.args == expect_args, 'args mismatched');
}

function* iterate_field(source, field) {
let index = 0;
while (true) {
Expand Down

0 comments on commit 0f5831f

Please sign in to comment.