Skip to content

Commit 7713782

Browse files
authored
Merge pull request #382 from Peefy/doc-wasm-api
docs: add wasm api reference documents
2 parents c07b1cf + e6b51aa commit 7713782

File tree

6 files changed

+362
-2
lines changed

6 files changed

+362
-2
lines changed

docs/reference/xlang-api/wasm-api.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# WASM API
6+
7+
The KCL core is written by Rust and can be compiled to the `wasm-wasi` target using toolchains such as cargo. With the help of WASM, we can also easily achieve multilingual and browser integration. Here is how we can use the KCL WASM module in node environment and rust.
8+
9+
## Quick Start
10+
11+
We can find and download KCL WASM module from [here](https://github.com/kcl-lang/lib/tree/main/wasm)
12+
13+
## Node.js
14+
15+
Install the dependency
16+
17+
```shell
18+
npm install @kcl-lang/wasm-lib
19+
```
20+
21+
Write the code
22+
23+
```typescript
24+
import { load, invokeKCLRun } from '@kcl-lang/wasm-lib'
25+
26+
async function main() {
27+
const inst = await load();
28+
const result = invokeKCLRun(inst, {
29+
filename: "test.k",
30+
source: `
31+
schema Person:
32+
name: str
33+
34+
p = Person {name = "Alice"}`,
35+
});
36+
console.log(result)
37+
}
38+
39+
main()
40+
```
41+
42+
The output is
43+
44+
```yaml
45+
p:
46+
name: Alice
47+
```
48+
49+
## Rust
50+
51+
In Rust, we use `wasmtime` as an example, and of course, you can also use other runtime that supports WASI to accomplish this.
52+
53+
Install the dependency
54+
55+
```shell
56+
cargo add kcl-wasm-lib --git https://github.com/kcl-lang/lib
57+
cargo add anyhow
58+
```
59+
60+
Write the code
61+
62+
```rust
63+
use anyhow::Result;
64+
use kcl_wasm_lib::{KCLModule, RunOptions};
65+
66+
fn main() -> Result<()> {
67+
let opts = RunOptions {
68+
filename: "test.k".to_string(),
69+
source: "a = 1".to_string(),
70+
};
71+
// Note replace your KCL wasm module path.
72+
let mut module = KCLModule::from_path("path/to/kcl.wasm")?;
73+
let result = module.run(&opts)?;
74+
println!("{}", result);
75+
Ok(())
76+
}
77+
```
78+
79+
The output is
80+
81+
```yaml
82+
a: 1
83+
```

docs/user_docs/support/faq-kcl.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,7 @@ p:
26832683
fullName: Alice White
26842684
```
26852685

2686-
## 67. Does the use of mixed-in attributes outisde of mixin requires casting to `any` type ?
2686+
## 67. Does the use of mixed-in attributes outside of mixin requires casting to `any` type?
26872687

26882688
You need to add specifically the type into the schema. An example code shown below:
26892689

@@ -2711,4 +2711,3 @@ returns the output:
27112711
```bash
27122712
foobar: foo.bar
27132713
```
2714-
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# WASM API
6+
7+
KCL 核心用 Rust 编写,可以使用 cargo 等工具链编译到 `wasm-wasi` 目标。在 WASM 的帮助下,也可以轻松实现多语言和浏览器集成。以下是在 Node.js 环境和 Rust 中使用 KCL WASM 模块的方法。
8+
9+
## 快速开始
10+
11+
可以从[这里](https://github.com/kcl-lang/lib/tree/main/wasm)找到 KCL WASM 模块。
12+
13+
## Node.js
14+
15+
安装依赖:
16+
17+
```shell
18+
npm install @kcl-lang/wasm-lib
19+
```
20+
21+
编写代码:
22+
23+
```typescript
24+
import { load, invokeKCLRun } from '@kcl-lang/wasm-lib'
25+
26+
async function main() {
27+
const inst = await load();
28+
const result = invokeKCLRun(inst, {
29+
filename: "test.k",
30+
source: `
31+
schema Person:
32+
name: str
33+
34+
p = Person {name = "Alice"}`,
35+
});
36+
console.log(result)
37+
}
38+
39+
main()
40+
```
41+
42+
输出结果为:
43+
44+
```yaml
45+
p:
46+
name: Alice
47+
```
48+
49+
## Rust
50+
51+
在 Rust 中,我们以 `wasmtime` 为例,当然你也可以使用其他支持 WASI 的运行时来完成这一任务。
52+
53+
安装依赖:
54+
55+
```shell
56+
cargo add kcl-wasm-lib --git https://github.com/kcl-lang/lib
57+
cargo add anyhow
58+
```
59+
60+
编写代码:
61+
62+
```rust
63+
use anyhow::Result;
64+
use kcl_wasm_lib::{KCLModule, RunOptions};
65+
66+
fn main() -> Result<()> {
67+
let opts = RunOptions {
68+
filename: "test.k".to_string(),
69+
source: "a = 1".to_string(),
70+
};
71+
// 注意修改路径为你的 kcl.wasm 路径
72+
let mut module = KCLModule::from_path("path/to/kcl.wasm")?;
73+
let result = module.run(&opts)?;
74+
println!("{}", result);
75+
Ok(())
76+
}
77+
```
78+
79+
输出结果为:
80+
81+
```yaml
82+
a: 1
83+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# WASM API
6+
7+
KCL 核心用 Rust 编写,可以使用 cargo 等工具链编译到 `wasm-wasi` 目标。在 WASM 的帮助下,也可以轻松实现多语言和浏览器集成。以下是在 Node.js 环境和 Rust 中使用 KCL WASM 模块的方法。
8+
9+
## 快速开始
10+
11+
可以从[这里](https://github.com/kcl-lang/lib/tree/main/wasm)找到 KCL WASM 模块。
12+
13+
## Node.js
14+
15+
安装依赖:
16+
17+
```shell
18+
npm install @kcl-lang/wasm-lib
19+
```
20+
21+
编写代码:
22+
23+
```typescript
24+
import { load, invokeKCLRun } from '@kcl-lang/wasm-lib'
25+
26+
async function main() {
27+
const inst = await load();
28+
const result = invokeKCLRun(inst, {
29+
filename: "test.k",
30+
source: `
31+
schema Person:
32+
name: str
33+
34+
p = Person {name = "Alice"}`,
35+
});
36+
console.log(result)
37+
}
38+
39+
main()
40+
```
41+
42+
输出结果为:
43+
44+
```yaml
45+
p:
46+
name: Alice
47+
```
48+
49+
## Rust
50+
51+
在 Rust 中,我们以 `wasmtime` 为例,当然你也可以使用其他支持 WASI 的运行时来完成这一任务。
52+
53+
安装依赖:
54+
55+
```shell
56+
cargo add kcl-wasm-lib --git https://github.com/kcl-lang/lib
57+
cargo add anyhow
58+
```
59+
60+
编写代码:
61+
62+
```rust
63+
use anyhow::Result;
64+
use kcl_wasm_lib::{KCLModule, RunOptions};
65+
66+
fn main() -> Result<()> {
67+
let opts = RunOptions {
68+
filename: "test.k".to_string(),
69+
source: "a = 1".to_string(),
70+
};
71+
// 注意修改路径为你的 kcl.wasm 路径
72+
let mut module = KCLModule::from_path("path/to/kcl.wasm")?;
73+
let result = module.run(&opts)?;
74+
println!("{}", result);
75+
Ok(())
76+
}
77+
```
78+
79+
输出结果为:
80+
81+
```yaml
82+
a: 1
83+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# WASM API
6+
7+
The KCL core is written by Rust and can be compiled to the `wasm-wasi` target using toolchains such as cargo. With the help of WASM, we can also easily achieve multilingual and browser integration. Here is how we can use the KCL WASM module in node environment and rust.
8+
9+
## Quick Start
10+
11+
We can find and download KCL WASM module from [here](https://github.com/kcl-lang/lib/tree/main/wasm)
12+
13+
## Node.js
14+
15+
Install the dependency
16+
17+
```shell
18+
npm install @kcl-lang/wasm-lib
19+
```
20+
21+
Write the code
22+
23+
```typescript
24+
import { load, invokeKCLRun } from '@kcl-lang/wasm-lib'
25+
26+
async function main() {
27+
const inst = await load();
28+
const result = invokeKCLRun(inst, {
29+
filename: "test.k",
30+
source: `
31+
schema Person:
32+
name: str
33+
34+
p = Person {name = "Alice"}`,
35+
});
36+
console.log(result)
37+
}
38+
39+
main()
40+
```
41+
42+
The output is
43+
44+
```yaml
45+
p:
46+
name: Alice
47+
```
48+
49+
## Rust
50+
51+
In Rust, we use `wasmtime` as an example, and of course, you can also use other runtime that supports WASI to accomplish this.
52+
53+
Install the dependency
54+
55+
```shell
56+
cargo add kcl-wasm-lib --git https://github.com/kcl-lang/lib
57+
cargo add anyhow
58+
```
59+
60+
Write the code
61+
62+
```rust
63+
use anyhow::Result;
64+
use kcl_wasm_lib::{KCLModule, RunOptions};
65+
66+
fn main() -> Result<()> {
67+
let opts = RunOptions {
68+
filename: "test.k".to_string(),
69+
source: "a = 1".to_string(),
70+
};
71+
// Note replace your KCL wasm module path.
72+
let mut module = KCLModule::from_path("path/to/kcl.wasm")?;
73+
let result = module.run(&opts)?;
74+
println!("{}", result);
75+
Ok(())
76+
}
77+
```
78+
79+
The output is
80+
81+
```yaml
82+
a: 1
83+
```

versioned_docs/version-0.9/user_docs/support/faq-kcl.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,3 +2683,32 @@ p:
26832683
fullName: Alice White
26842684
```
26852685

2686+
## 67. Does the use of mixed-in attributes outside of mixin requires casting to `any` type?
2687+
2688+
You need to add specifically the type into the schema. An example code shown below:
2689+
2690+
```KCL
2691+
schema FooBar:
2692+
mixin [
2693+
FooBarMixin
2694+
]
2695+
foo: str = 'foo'
2696+
bar: str = 'bar'
2697+
2698+
protocol FooBarProtocol:
2699+
foo: str
2700+
bar: str
2701+
2702+
mixin FooBarMixin for FooBarProtocol:
2703+
foobar: str = "${foo}.${bar}" # Attribute with the annotation can be accessed outside the schema.
2704+
2705+
_c = FooBar {}
2706+
foobar = _c.foobar
2707+
```
2708+
2709+
returns the output:
2710+
2711+
```bash
2712+
foobar: foo.bar
2713+
```
2714+

0 commit comments

Comments
 (0)