2022-08-23 10:12:36 +08:00
|
|
|
|
# FScript 脚本引擎
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 1. 介绍
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
FScript 是一个极简的脚本引擎,借鉴了函数语言中一些思路,主要用于低端嵌入式系统,让用户轻松扩展现有系统,而不需要重新编译和下载固件。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
* 特色:
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2020-11-13 17:57:06 +08:00
|
|
|
|
* 小内存。最低开销小于 400 字节。
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 小巧。核心代码 1500 行,基本扩展函数 1000 行。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
* 灵活。支持多条语句、函数嵌套调用和变量定义。
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 简单。熟悉任何一种编程语言的人,5 分钟内即可学会。
|
2021-01-09 17:48:48 +08:00
|
|
|
|
* 强大。超过 60 个内置函数,支持类似 C 语言的条件语句、循环语句和注释方式,支持复杂的表达式,支持用 C 语言扩展函数。
|
2020-11-13 17:57:06 +08:00
|
|
|
|
|
|
|
|
|
> keil -O1 编译结果:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```bash
|
2020-11-13 17:57:06 +08:00
|
|
|
|
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
|
2021-01-09 23:19:05 +08:00
|
|
|
|
10380 1580 1909 4 0 86893 fscript.o
|
2020-11-13 17:57:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-12-20 17:18:31 +08:00
|
|
|
|
实际内存使用与场景有关,请参考 [内存需求评估](fscript_ram_req.md)
|
2021-11-16 17:50:22 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 2. 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print("hello fscript")
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 在 PC 上测试运行:
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
如:
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```bash
|
2020-11-10 22:06:48 +08:00
|
|
|
|
./bin/runFScript 'print("hello fscript")'
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 在 PC 上测试运行指定文件:
|
|
|
|
|
|
|
|
|
|
> 第一个命令行参数以 @ 开头表示它是一个脚本文件,读取其内容执行。
|
|
|
|
|
|
|
|
|
|
如:
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```bash
|
2021-01-10 09:58:41 +08:00
|
|
|
|
./bin/runFScript @tests/fscripts/demo_while1.fs
|
2021-01-02 12:36:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
## 3. 语法
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.1 数据类型
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 15:25:37 +08:00
|
|
|
|
* 浮点数类型 (double)。
|
|
|
|
|
* 整数类型 (int32)。支持十进制、二进制和十六进制。
|
|
|
|
|
* 字符串类型 (string)。UTF-8 字符串,用英文双引号扩起来。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
* 布尔类型 (bool)。标准取值为:true 和 false,非 0 的数值视为 true。
|
2021-01-10 09:58:41 +08:00
|
|
|
|
* 可以通过扩展实现混合数据类型的数组、固定类型的数组和 map。
|
2024-01-02 16:48:28 +08:00
|
|
|
|
* 可以通过类型转换函数得到各种基本类型。如 int8/int16/int32/int64/uint8/uint16/uint32/uint64/float/double。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.2 注释
|
2021-01-07 11:34:05 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
* 格式:
|
2021-01-07 11:34:05 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-07 11:34:05 +08:00
|
|
|
|
//line comment
|
|
|
|
|
/*block comment*/
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
* 示例:
|
2021-01-07 11:34:05 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-07 11:34:05 +08:00
|
|
|
|
//this is line comment
|
|
|
|
|
/*this is block comment*/
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.3 函数调用
|
2020-11-11 15:25:37 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-11 15:25:37 +08:00
|
|
|
|
print(0xff)
|
|
|
|
|
print(100)
|
|
|
|
|
print(0b1010)
|
|
|
|
|
print("hello fscript")
|
|
|
|
|
print("hello", 123)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 函数之间可以用空格、换行和英文分号分隔。
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.4 定义变量
|
2020-11-11 15:25:37 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 通过 set 函数设置
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
/* 系统变量:一个 AWTK 应用程序只有一个 global 对象,在程序初始化时创建,程序退出时销毁 */
|
2022-08-23 18:27:17 +08:00
|
|
|
|
set(global.a, 123)
|
|
|
|
|
|
|
|
|
|
/* 全局变量:保存在脚本执行的上下文对象中,通常由外部控制生命周期 */
|
2020-11-11 15:25:37 +08:00
|
|
|
|
set(a, 123)
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 通过=赋值
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
/* 系统变量:一个 AWTK 应用程序只有一个 global 对象,在程序初始化时创建,程序退出时销毁 */
|
2022-08-23 18:27:17 +08:00
|
|
|
|
global.a = 123
|
|
|
|
|
global.b = "abc"
|
|
|
|
|
global.c = true
|
|
|
|
|
|
|
|
|
|
/* 全局变量:保存在脚本执行的上下文对象中,通常由外部控制生命周期 */
|
2021-01-02 12:36:30 +08:00
|
|
|
|
a = 123
|
|
|
|
|
b = "abc"
|
|
|
|
|
c = true
|
2022-08-23 18:27:17 +08:00
|
|
|
|
|
2024-04-08 07:40:07 +08:00
|
|
|
|
/* 局部变量:又称临时变量,在脚本程序执行完毕或函数调用完成就销毁(同一个脚本程序/函数只有一个作用域)*/
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a = 123
|
|
|
|
|
var b = "abc"
|
|
|
|
|
var c = true
|
2021-01-02 12:36:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 18:27:17 +08:00
|
|
|
|
> 备注:
|
|
|
|
|
>
|
|
|
|
|
> 1. global. 开头的变量为系统变量,可以在同一进程的多个脚本中共享数据(这并非是好的做法,尽量避免使用)
|
|
|
|
|
> 2. 在 FScript 脚本中定义的系统变量和全局变量在执行完一段脚本后通常不会被清空,因此使用这些变量前需要初始化。
|
2021-09-09 15:10:12 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 3.4.1 变量名命名规则:
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
|
|
|
|
* 以字母开头,后面可用数字、英文下划线和英文点。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 12:36:30 +08:00
|
|
|
|
age=123
|
|
|
|
|
name="fscript"
|
|
|
|
|
file_name="test.txt"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 英文点"."可以用来访问对象的成员,如果对应的对象不存在则当作普通变量。
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 12:36:30 +08:00
|
|
|
|
msg.payload = "hello"
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
> 在这里,如果对象 msg 存在,msg.payload 为 msg 对象的 payload 成员,否则 msg.payload 则是一个普通变量。
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.5 获取变量
|
2020-11-11 15:25:37 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-10-19 11:06:46 +08:00
|
|
|
|
abc
|
|
|
|
|
get(abc)
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 11:06:46 +08:00
|
|
|
|
> * 获取变量时,如果变量不存在,自动当成字符串处理,并打印获取变量失败的警告。
|
2021-12-20 17:18:31 +08:00
|
|
|
|
> * 如果不希望变量被当成字符串,可以加上$前缀,或者使用 get 函数。
|
2022-08-23 18:27:17 +08:00
|
|
|
|
> * FScript 不支持将函数返回值当做变量名使用并通过英文点"."访问子变量,比如 array_create().size,此时会将 .size 当做字符串处理。
|
2021-11-08 17:41:35 +08:00
|
|
|
|
>
|
2022-08-23 18:27:17 +08:00
|
|
|
|
> 注意:FScript 不提倡直接使用变量名当做字符串,**如需要如果使用字符串常量,请用双引号包起来**。
|
2020-12-06 11:41:52 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
如:
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 12:36:30 +08:00
|
|
|
|
./bin/runFScript 'print($abc)'
|
|
|
|
|
|
|
|
|
|
get var abc failed
|
|
|
|
|
(null)
|
|
|
|
|
result:true
|
|
|
|
|
cost: 112 us
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 12:36:30 +08:00
|
|
|
|
./bin/runFScript 'print(abc)'
|
|
|
|
|
|
|
|
|
|
get var abc failed
|
|
|
|
|
abc
|
|
|
|
|
result:true
|
|
|
|
|
cost: 112 us
|
|
|
|
|
```
|
|
|
|
|
|
2021-12-20 17:18:31 +08:00
|
|
|
|
判断一个变量是否有效,需要使用 get 函数:
|
2021-10-19 11:06:46 +08:00
|
|
|
|
|
2022-08-23 18:27:17 +08:00
|
|
|
|
> 注意:如果不使用 get 函数获取变量,那么当变量不存在时,FScript 会自动将其当成字符串处理,此时 value_is_valid 函数一定返回 true。
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-10-19 11:06:46 +08:00
|
|
|
|
print("abc is valid? ", value_is_valid(get(abc)))
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var abc=123
|
2021-10-19 11:06:46 +08:00
|
|
|
|
print("abc is valid? ", value_is_valid(get(abc)))
|
|
|
|
|
print(get(abc) + 321)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.6 函数嵌套调用
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(join(",", 1, 2, 3))
|
2021-01-02 12:36:30 +08:00
|
|
|
|
print(join(",",1,2,3,4), join(";",5,6,7,8))
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.7 提前返回 (return)
|
2021-02-19 10:12:15 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-02-19 10:12:15 +08:00
|
|
|
|
var a = 1;
|
|
|
|
|
if(a == 1) {
|
|
|
|
|
print("return");
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
print("end");
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.8 条件执行
|
2020-11-11 15:25:37 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 语句方式
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
|
|
|
|
/* if else */
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=random(1, 100)
|
|
|
|
|
var b=random(1, 100)
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
|
|
|
|
print("a is ", a);
|
|
|
|
|
print("b is ", b);
|
|
|
|
|
|
|
|
|
|
if(a < b) {
|
|
|
|
|
print(a, "<", b)
|
|
|
|
|
} else {
|
|
|
|
|
print(a, ">=", b)
|
|
|
|
|
}
|
2020-11-11 15:25:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
|
|
|
|
/* else if */
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=random(1, 6);
|
2021-02-19 09:37:34 +08:00
|
|
|
|
var b = "";
|
|
|
|
|
|
|
|
|
|
if(a == 1) {
|
|
|
|
|
b = "a"
|
|
|
|
|
} else if(a == 2) {
|
|
|
|
|
b = "b"
|
|
|
|
|
} else if(a == 3) {
|
|
|
|
|
b = "c"
|
|
|
|
|
} else if(a == 4) {
|
|
|
|
|
b = "d"
|
|
|
|
|
} else {
|
|
|
|
|
b = "other"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> 代码块需要用 {} 扩起来,else 语句可以省略。
|
|
|
|
|
|
|
|
|
|
* 函数方式
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-11 15:25:37 +08:00
|
|
|
|
if(true, print("a"), print("b"))
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.9 while 循环
|
2020-11-23 16:10:23 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 语法
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
while (条件) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 示例
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=0
|
|
|
|
|
var b=0
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
|
|
|
|
while(a < 100) {
|
|
|
|
|
a=a+1
|
|
|
|
|
b=b+a
|
|
|
|
|
print(a, b)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.10 until 循环
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
> 重复执行直到条件成立
|
|
|
|
|
|
|
|
|
|
* 语法
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
until(条件) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 示例
|
2021-01-02 12:36:30 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=0
|
|
|
|
|
var b=0
|
2022-01-07 17:56:21 +08:00
|
|
|
|
|
|
|
|
|
until(a >= 100) {
|
|
|
|
|
a=a+1
|
|
|
|
|
b=b+a
|
|
|
|
|
print(a, b)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.11 for 循环
|
2022-01-07 17:56:21 +08:00
|
|
|
|
|
|
|
|
|
* 语法
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
for(初始化表达式;结束条件;迭代表达式) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-01-07 17:56:21 +08:00
|
|
|
|
for(var a = 0; a < 100; a = a + 1) {
|
|
|
|
|
print(a)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.12 for_in 循环
|
2022-01-07 17:56:21 +08:00
|
|
|
|
|
|
|
|
|
> 用于遍历数组对象
|
|
|
|
|
|
|
|
|
|
* 语法
|
|
|
|
|
|
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
for_in(迭代变量,数组对象) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var sum = 0;
|
|
|
|
|
var a = array_create(11, 22, 33, 44, 55)
|
|
|
|
|
for_in(i, a) {
|
|
|
|
|
sum = sum + i
|
|
|
|
|
print(i)
|
|
|
|
|
}
|
|
|
|
|
print(sum)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.13 repeat 循环
|
2022-01-07 17:56:21 +08:00
|
|
|
|
|
|
|
|
|
* 语法
|
|
|
|
|
|
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
repeat(迭代变量,开始值,结束值,增量) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
repeat(i, 0, 10, 1) {
|
|
|
|
|
print(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var times = 10
|
|
|
|
|
repeat(i, 0, times, 2) {
|
|
|
|
|
print(i)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var times = 10
|
|
|
|
|
repeat(i, 20, times, -2) {
|
|
|
|
|
print(i)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.14 repeat_times 循环
|
2022-01-07 17:56:21 +08:00
|
|
|
|
|
|
|
|
|
* 语法
|
|
|
|
|
|
|
|
|
|
```js
|
2022-12-03 16:26:51 +08:00
|
|
|
|
repeat_times(重复次数) {
|
2022-01-07 17:56:21 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
* 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
repeat_times(100) {
|
|
|
|
|
print("hello")
|
|
|
|
|
}
|
2020-11-23 16:10:23 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 3.15 表达式
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
为了确保优先级正确,尽量用括号明确优先级。
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 3.15.1 支持的操作符
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
|
|
|
|
* 四则运算:+ - * / %
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 逻辑运算:|| && !
|
2020-11-29 10:55:12 +08:00
|
|
|
|
* 位运算:| & ~
|
|
|
|
|
* 比较运算:< <= > >= ==
|
2021-01-02 12:36:30 +08:00
|
|
|
|
* 条件运算:(p) ? a : b
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> 对于加法运算 (+),参数中有字符串时,进行字符串连接。
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 3.15.2 示例
|
2020-11-29 10:55:12 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a = 10
|
|
|
|
|
var b = 20
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-29 10:20:50 +08:00
|
|
|
|
a<b
|
|
|
|
|
1+2
|
|
|
|
|
1-2
|
|
|
|
|
1*2
|
|
|
|
|
1/2
|
|
|
|
|
3%2
|
|
|
|
|
a||b
|
|
|
|
|
a&&b
|
|
|
|
|
a|b
|
|
|
|
|
a&b
|
|
|
|
|
!a
|
|
|
|
|
~a
|
|
|
|
|
a<b
|
|
|
|
|
a<=b
|
|
|
|
|
a>b
|
|
|
|
|
a>=b
|
|
|
|
|
(1+2)*(2+3)
|
|
|
|
|
sin(a) + sin(b)
|
|
|
|
|
a<b ? print(a) : print(b)
|
|
|
|
|
set(a, a+1)
|
|
|
|
|
set(a, a+b)
|
|
|
|
|
"abc" + "123"
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-13 17:57:06 +08:00
|
|
|
|
## 4. 函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.1 基本函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.1 assert
|
2021-01-04 10:48:13 +08:00
|
|
|
|
|
|
|
|
|
> 断言
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
assert(condition, crash_if_fail) => void
|
2021-01-04 10:48:13 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 14:40:14 +08:00
|
|
|
|
* crash\_if\_fail 可选。为 true 时,调用系统的 assert,否则只是打印警告。
|
2021-01-13 13:48:59 +08:00
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-04 10:48:13 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-04 10:48:13 +08:00
|
|
|
|
assert(1<2)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.2 eval
|
2021-01-12 11:38:33 +08:00
|
|
|
|
|
|
|
|
|
> 执行另外一段脚本。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
eval(condition) => value
|
2021-01-12 11:38:33 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-12 11:38:33 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-12 11:38:33 +08:00
|
|
|
|
eval("1+2")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.3 has_error
|
2021-01-02 18:34:09 +08:00
|
|
|
|
|
|
|
|
|
> 检查是否之前遇到错误
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
has_error() => bool
|
2021-01-02 18:34:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-02 18:34:09 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 18:34:09 +08:00
|
|
|
|
print(has_error())
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.4 get_last_error
|
|
|
|
|
|
2021-10-19 15:35:30 +08:00
|
|
|
|
> 获取前一个错误
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-10-19 15:35:30 +08:00
|
|
|
|
get_last_error() => object | null
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 15:51:24 +08:00
|
|
|
|
如果有错,返回对象,其成员如下:
|
2021-10-19 15:35:30 +08:00
|
|
|
|
|
|
|
|
|
* message 错误消息
|
|
|
|
|
* code 错误码
|
|
|
|
|
* line 出错的行
|
|
|
|
|
* col 出错的列
|
|
|
|
|
|
|
|
|
|
没有错误,返回无效值。
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-10-19 15:35:30 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-10-19 15:35:30 +08:00
|
|
|
|
print(len())
|
|
|
|
|
var error = get_last_error();
|
|
|
|
|
print("XXX:", error.message, error.code, error.line, error.col);
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.5 clear_error
|
2021-01-02 18:34:09 +08:00
|
|
|
|
|
|
|
|
|
> 清除之前遇到错误
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
clear_error() => void
|
2021-01-02 18:34:09 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-02 18:34:09 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-02 18:34:09 +08:00
|
|
|
|
clear_error()
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.6 print
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 打印调试信息到控制台。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
print(str,...) => void
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print("hello fscript")
|
2020-11-11 15:25:37 +08:00
|
|
|
|
print("hello", "fscript")
|
|
|
|
|
print(1)
|
|
|
|
|
print(true)
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=100
|
|
|
|
|
var b=200
|
2020-11-29 10:20:50 +08:00
|
|
|
|
print(a+b)
|
|
|
|
|
print(join(",", a, b))
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.7 sleep_ms
|
2022-01-01 18:47:50 +08:00
|
|
|
|
|
2022-01-07 17:56:21 +08:00
|
|
|
|
> 睡眠指定时间长(毫秒)
|
2022-01-01 18:47:50 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-01-01 18:47:50 +08:00
|
|
|
|
sleep_ms(time) => bool
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-01-01 18:47:50 +08:00
|
|
|
|
sleep_ms(100);
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.8 set
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-02 18:34:09 +08:00
|
|
|
|
> 设置变量的值。建议使用=赋值。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
set(var, value) => bool
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a=1
|
2020-11-10 22:06:48 +08:00
|
|
|
|
set(a, 1)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.9 unset
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 清除变量。
|
|
|
|
|
|
2021-01-13 13:48:59 +08:00
|
|
|
|
对于不再使用的变量,特别是对象和字符串,请调用本函数清除掉。
|
|
|
|
|
|
|
|
|
|
当一个变量重复赋值时,会自动清除之前的数据。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
unset(var) => void
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
var a = 10;
|
2020-11-10 22:06:48 +08:00
|
|
|
|
unset(a)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.10 int
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 转换为整数类型。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
int(var) => int32_t
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
int("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.11 i8
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int8_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i8(var) => int8_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
i8("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.12 i16
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int16_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i16(var) => int16_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
i16("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.13 i32
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 int32_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i32(var) => int32_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
i32("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.14 i64
|
2021-01-04 17:22:32 +08:00
|
|
|
|
|
|
|
|
|
> 转换为 int64_t 类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
i64(var) => int64_t
|
2021-01-04 17:22:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-04 17:22:32 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-04 17:22:32 +08:00
|
|
|
|
i64("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.15 u8
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt8_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u8(var) => uint8_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
u8("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.16 u16
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt16_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u16(var) => value(uint16)
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
u16("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.17 u32
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2020-11-23 11:01:46 +08:00
|
|
|
|
> 转换为 unt32_t 类型。
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u32(var) => uint32_t
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
u32("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.18 u64
|
2021-01-04 17:22:32 +08:00
|
|
|
|
|
|
|
|
|
> 转换为 unt64_t 类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
u64(var) => uint64_t
|
2021-01-04 17:22:32 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-01-04 17:22:32 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-04 17:22:32 +08:00
|
|
|
|
u64("123")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.19 f32
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
> 转换为单精度类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
f32(var) => float
|
2020-11-22 12:23:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-22 12:23:46 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-22 12:23:46 +08:00
|
|
|
|
f32("123")
|
|
|
|
|
```
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.20 f64
|
|
|
|
|
|
|
|
|
|
> 转换为双精度类型。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
|
|
|
|
f64(var) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
|
|
|
|
f64("123")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.21 str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 转换为字符串类型。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
str(var [,force_pointer_as_str]) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-02 12:36:30 +08:00
|
|
|
|
> force\_pointer\_as\_str 如果输入参数是 POINTER 类型,是否将强制转换成字符串。
|
2020-12-10 15:57:12 +08:00
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
str(int(123))
|
2020-12-10 15:57:12 +08:00
|
|
|
|
str(msg.payload, true)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.22 binary
|
2022-05-19 17:21:04 +08:00
|
|
|
|
|
|
|
|
|
> 转换为 binary_data_t 类型。
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-05-19 17:21:04 +08:00
|
|
|
|
binary(var) => binary_data_t
|
|
|
|
|
binary(var, size) => binary_data_t
|
|
|
|
|
binary(var, size, clone) => binary_data_t
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* var 是常见类型的变量。如:i8/i16/i32/i64/u8/u16/u32/u64/float/double/string/wstring/pointer 等。
|
|
|
|
|
* size 如果类型为 pointer 时,size 必须指定,其它类型可以不指定,此时用默认长度。
|
|
|
|
|
* clone 是否拷贝数据。基本数据类型 i8/i16/i32/i64/u8/u16/u32/u64/float/double 始终拷贝。string/wstring/pointer 由本参数决定是否拷贝。
|
|
|
|
|
|
|
|
|
|
> 如果不拷贝,请确保引用的变量的生命周期大于新的变量。
|
|
|
|
|
|
|
|
|
|
##### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-05-19 17:21:04 +08:00
|
|
|
|
binary(100)
|
|
|
|
|
binary("123")
|
|
|
|
|
binary("123", 2)
|
|
|
|
|
binary("123", 2, true)
|
|
|
|
|
binary(data, 4)
|
|
|
|
|
binary(data, 8, true)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.23 iformat
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
> 对整数进行格式化(生成的字符串长度不超过 63)
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
iformat(format, value) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
iformat("hello:%d", 123)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.24 fformat
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
> 对浮点数进行格式化(生成的字符串长度不超过 63)
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
fformat(format, value) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
fformat("hello:%lf", 123)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.1.25 exec
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 执行 object 里的一个命令。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
exec(cmd, arg) => bool
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
exec("clear", "all")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 具体功能与 object 的实现有关。
|
|
|
|
|
|
2024-02-21 17:56:34 +08:00
|
|
|
|
#### 4.1.26 can_exec
|
|
|
|
|
|
|
|
|
|
> 判断 object 里的一个命令是否可执行。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
can_exec(cmd, arg) => bool
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
if (can_exec("clear", "all")) {
|
|
|
|
|
exec("clear", "all")
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 具体功能与 object 的实现有关。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.2 字符串函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.1 join
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-10 09:58:41 +08:00
|
|
|
|
> 将多个变量用指定的分隔符拼接起来,形成一个字符串。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
join(seperator, s1, s2, s3...) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
join(",", 1, 2, 3, "abc")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.2 one_of
|
2021-02-20 12:42:20 +08:00
|
|
|
|
|
2021-12-20 17:18:31 +08:00
|
|
|
|
> 从字符串数组中取出第 N 个字符串
|
2021-02-20 12:42:20 +08:00
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-02-20 12:42:20 +08:00
|
|
|
|
one_of(str_array, index, sep) => str
|
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
2021-12-20 17:18:31 +08:00
|
|
|
|
> sep 为分隔符,默认为英文分号 (;)。
|
2021-02-20 12:42:20 +08:00
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2021-02-20 12:42:20 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
|
one_of("aa;bb;cc", 0) // => aa
|
|
|
|
|
one_of("aa;bb;cc", 1) // => bb
|
|
|
|
|
one_of("aa;bb;cc", 2) // => cc
|
|
|
|
|
one_of("aa.bb.cc", 0, ".") // ==> aa
|
2021-02-20 12:42:20 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.3 len
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-07-19 17:11:39 +08:00
|
|
|
|
> 取字符串/数组的长度。
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
len(str) => uint32_t
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
len("abc")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.4 toupper
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 将字符串转换成大写。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
toupper(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2022-09-20 18:01:37 +08:00
|
|
|
|
toupper("abc")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.5 tolower
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 将字符串转换成小写。
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
|
|
|
|
|
2020-11-10 22:06:48 +08:00
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
tolower(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
tolower("ABC")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.6 trim
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2021-01-10 09:58:41 +08:00
|
|
|
|
> 去掉字符串两端的空白字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
trim(str) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
|
|
|
|
trim(" abc ")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.7 substr
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 取子字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
substr(str, from, len) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
substr("abcd", 1, 2)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.8 replace
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 替换子字符串。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
replace(str, old, new) => str
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-11 09:33:33 +08:00
|
|
|
|
replace("ab cd", "ab", "hello")
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.2.9 contains
|
2020-11-11 09:33:33 +08:00
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
> 检查是否包含指定的子字符串。
|
|
|
|
|
----------------------------
|
2020-11-11 09:33:33 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
contains(str, substr) => bool
|
2020-11-11 09:33:33 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-11 09:33:33 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-11 09:33:33 +08:00
|
|
|
|
contains("ab cd", "ab")
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 09:56:04 +08:00
|
|
|
|
### 4.3 运算函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.1 sum
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 对多个数值类型的参数求和。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
sum(n1,n2...)
|
|
|
|
|
+(n1,n2...)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1+n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(sum(1, 2, 3))
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.2 sub
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 计算两个数之差。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
sub(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1-n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(sub(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2 - 1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.3 mul
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 计算两个数之积。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
mul(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1*n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(mul(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2 * 1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.4 div
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 计算两个数之商。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
div(n1,n2)
|
|
|
|
|
或
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1/n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(div(2, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(2/1)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.5 %
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 计算两个数的模。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1%n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(23%7)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.6 and
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 逻辑与运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
and(n1,n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1 && n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(true && false)
|
|
|
|
|
print(true && true)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.7 or
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 逻辑或运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
or(n1,n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
|
n1 || n2
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-10 09:58:41 +08:00
|
|
|
|
print(a || b)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.3.8 not
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 逻辑非运算。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
not(n1)
|
|
|
|
|
!(n1)
|
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
print(not(true))
|
|
|
|
|
print(!(false))
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 4.4 比较函数
|
2020-12-01 18:11:44 +08:00
|
|
|
|
|
|
|
|
|
> 支持字符串比较。
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.1 <
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 小于。
|
2022-08-23 10:12:36 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
<(a,b)
|
|
|
|
|
less(a,b)
|
|
|
|
|
a<b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.2 <=
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 小于等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
<=(a,b)
|
|
|
|
|
le(a,b)
|
|
|
|
|
a<=b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.3 >
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 大于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
>(a,b)
|
|
|
|
|
great(a,b)
|
|
|
|
|
a>b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.4 >=
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 大于等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
>=(a,b)
|
|
|
|
|
ge(a,b)
|
|
|
|
|
a>=b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.5 ==
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 等于。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
==(a,b)
|
|
|
|
|
eq(a,b)
|
|
|
|
|
a==b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.4.5 !=
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-12-01 18:11:44 +08:00
|
|
|
|
> 不等于。
|
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2020-12-01 18:11:44 +08:00
|
|
|
|
##### 原型
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-12-01 18:11:44 +08:00
|
|
|
|
!=(a,b)
|
|
|
|
|
a != b
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 4.5 数学函数
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.1 random
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> 产生随机数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
random() => int
|
|
|
|
|
random(min, max) => int
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
##### 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
a = random();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 4.5.2 round
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
|
|
|
|
> 四舍五入。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
round(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-29 10:20:50 +08:00
|
|
|
|
round(4.5)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.3 floor
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
|
|
|
|
> 返回小于等于指定数值的最大整数。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
floor(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-29 10:20:50 +08:00
|
|
|
|
floor(4.5)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.4 ceil
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
|
|
|
|
> 返回大于等于指定数值的最小整数。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
ceil(v) => double
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-29 10:20:50 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
ceil(4.5)
|
2020-11-29 10:20:50 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.5 abs
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> abs 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
abs(a) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
abs(1)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.6 min
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> min 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
min(a, b) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-11-22 16:39:37 +08:00
|
|
|
|
min(1, 2)
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.6 max
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> max 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
max(a, b) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
max(1, 2)
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
#### 4.5.7 clamp
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
> clamp 函数。
|
2020-11-11 09:56:04 +08:00
|
|
|
|
----------------------------
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-01-13 13:48:59 +08:00
|
|
|
|
clamp(a, min, max) => double
|
2020-11-10 22:06:48 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
|
##### 示例
|
2020-11-10 22:06:48 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2020-11-10 22:06:48 +08:00
|
|
|
|
clamp(2, 1, 3)
|
|
|
|
|
```
|
|
|
|
|
|
2023-12-04 18:02:20 +08:00
|
|
|
|
#### 4.5.8 levelize
|
|
|
|
|
|
|
|
|
|
> levelize 函数。
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
##### 原型
|
|
|
|
|
|
|
|
|
|
按指定的区间对值进行等级化。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
levelize(levels, value) => int32_t
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
##### 示例
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
levelize('0-20;21-40;41-60;61-80;81-100', 10) => 0
|
|
|
|
|
levelize('0-20;21-40;41-60;61-80;81-100', 30) => 1
|
|
|
|
|
levelize('0-20;21-40;41-60;61-80;81-100', 50) => 2
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
### 4.6 时间函数
|
|
|
|
|
|
2021-01-13 15:42:09 +08:00
|
|
|
|
> 已经移动到扩展模块。
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
## 5. 自定义函数
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
### 5.1 定义函数
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
static ret_t func_foo(object_t* obj, fscript_args_t* args, value_t* v) {
|
|
|
|
|
value_set_int(v, 123);
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
### 5.2 注册和使用私有函数
|
2020-11-11 22:18:50 +08:00
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
value_t v;
|
|
|
|
|
object_t* obj = object_default_create();
|
|
|
|
|
object_set_prop_pointer(obj, "function.foo", (void*)func_foo);
|
|
|
|
|
|
|
|
|
|
fscript_eval(obj, "foo()", &v);
|
|
|
|
|
value_reset(&v);
|
|
|
|
|
|
|
|
|
|
OBJECT_UNREF(obj);
|
|
|
|
|
```
|
2020-11-23 11:01:46 +08:00
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
### 5.3 注册全局函数
|
2020-12-04 08:29:06 +08:00
|
|
|
|
|
|
|
|
|
* 初始化时调用
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```c
|
2020-12-04 08:29:06 +08:00
|
|
|
|
fscript_register_func("foo", func_foo);
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
### 5.4 定义脚本函数
|
2021-08-26 07:40:37 +08:00
|
|
|
|
|
|
|
|
|
* 函数定义
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-08-26 07:40:37 +08:00
|
|
|
|
function foo1(v1, v2) {
|
|
|
|
|
return v1 + v2;
|
|
|
|
|
}
|
|
|
|
|
assert(foo1(100, 200) == 300)
|
|
|
|
|
```
|
|
|
|
|
|
2021-12-20 17:18:31 +08:00
|
|
|
|
* 使用 var 定义局部变量。
|
2021-08-26 07:40:37 +08:00
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-08-26 07:40:37 +08:00
|
|
|
|
function foo2(v1, v2) {
|
|
|
|
|
var v3 = v1 + v2;
|
|
|
|
|
return v3
|
|
|
|
|
}
|
|
|
|
|
assert(foo2(100, 200) == 300)
|
|
|
|
|
|
|
|
|
|
function foo3(v1, v2) {
|
|
|
|
|
var v3 = v1 + v2;
|
|
|
|
|
if(v3 < 100) {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(foo3(10, 20))
|
|
|
|
|
assert(!foo3(100, 200))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 函数内的临时变量,无论在哪里定义,一旦定义,在该函数内都可以使用。
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
```js
|
2021-08-26 07:40:37 +08:00
|
|
|
|
function foo4 (v1, v2) {
|
|
|
|
|
var v3 = v1 + v2;
|
|
|
|
|
if(v3 < 100) {
|
|
|
|
|
var name = "awtk";
|
|
|
|
|
} else {
|
|
|
|
|
var name = "react-awtk";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(foo4(10, 20) == 'awtk')
|
|
|
|
|
assert(foo4(100, 200) == 'react-awtk')
|
|
|
|
|
```
|
2020-12-04 08:29:06 +08:00
|
|
|
|
|
2022-07-05 17:10:18 +08:00
|
|
|
|
## 6. 性能测量与优化
|
2020-11-23 11:01:46 +08:00
|
|
|
|
|
|
|
|
|
runFScript 的第二个参数可以指定运行次数,方便测量某个函数的运行时间。
|
|
|
|
|
|
|
|
|
|
比如:
|
|
|
|
|
|
2022-08-23 18:27:17 +08:00
|
|
|
|
```bash
|
2020-12-01 18:11:44 +08:00
|
|
|
|
./bin/runFScript '123+234' 100000
|
2020-11-23 11:01:46 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-07-06 09:59:38 +08:00
|
|
|
|
## 7. 内置常量
|
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
|
| 名称 | 说明 |
|
|
|
|
|
| -------------------- | ---------------------- |
|
|
|
|
|
| RET\_OK | 成功。 |
|
|
|
|
|
| RET\_OOM | Out of memory。 |
|
|
|
|
|
| RET\_FAIL | 失败。 |
|
|
|
|
|
| RET\_NOT\_IMPL | 没有实现/不支持。 |
|
|
|
|
|
| RET\_QUIT | 退出。通常用于主循环。 |
|
|
|
|
|
| RET\_FOUND | 找到。 |
|
|
|
|
|
| RET\_BUSY | 对象忙。 |
|
|
|
|
|
| RET\_REMOVE | 移出。通常用于定时器。 |
|
|
|
|
|
| RET\_REPEAT | 重复。通常用于定时器。 |
|
|
|
|
|
| RET\_NOT\_FOUND | 没找到。 |
|
|
|
|
|
| RET\_DONE | 操作完成。 |
|
|
|
|
|
| RET\_STOP | 停止后续操作。 |
|
|
|
|
|
| RET\_SKIP | 跳过当前项。 |
|
|
|
|
|
| RET\_CONTINUE | 继续后续操作。 |
|
|
|
|
|
| RET\_OBJECT\_CHANGED | 对象属性变化。 |
|
|
|
|
|
| RET\_ITEMS\_CHANGED | 集合数目变化。 |
|
|
|
|
|
| RET\_BAD\_PARAMS | 无效参数。 |
|
|
|
|
|
| RET\_TIMEOUT | 超时。 |
|
2022-12-03 16:26:51 +08:00
|
|
|
|
| RET\_CRC | CRC 错误。 |
|
|
|
|
|
| RET\_IO | IO 错误。 |
|
2022-08-23 10:12:36 +08:00
|
|
|
|
| RET\_EOS | End of Stream |
|
|
|
|
|
| RET\_NOT\_MODIFIED | 没有改变。 |
|
2022-12-03 16:26:51 +08:00
|
|
|
|
|
|
|
|
|
示例:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
return RET_OK;
|
|
|
|
|
```
|
2022-07-06 09:59:38 +08:00
|
|
|
|
|
2022-12-03 16:26:51 +08:00
|
|
|
|
## 8. 注册常量
|
|
|
|
|
|
2022-12-03 16:53:37 +08:00
|
|
|
|
fscript 本身只是一个胶水语言,它负责把各个原生扩展模块组合起来。扩展模块中往往有一些常量,有些常量是了方便记忆(比如 PI),有的常量是降低变化带来的影响(比如 TK\_NAME\_LEN) ,有的常量用于表示特定的意义(比如一些枚举)。为了保持这些常量在 fscript 和原生代码中的一致性,和函数一样,fscript 提供了一种注册机制,让原生模块可以注册常量,这些常量在 fscript 中就可以访问了。
|
2022-12-03 16:26:51 +08:00
|
|
|
|
|
|
|
|
|
* 注册常量
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/**
|
|
|
|
|
* @method fscript_register_const_value
|
|
|
|
|
* 注册常量。
|
|
|
|
|
* @param {const char*} name 常量名。
|
|
|
|
|
* @param {const value_t*} value 数据。
|
|
|
|
|
*
|
|
|
|
|
* @return {ret_t} 返回 RET_OK 表示成功,否则表示失败。
|
|
|
|
|
*/
|
|
|
|
|
ret_t fscript_register_const_value(const char* name, const value_t* value);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 注册整数常量(只是为了使用方便而提供的包装函数)
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/**
|
|
|
|
|
* @method fscript_register_const_int
|
|
|
|
|
* 注册整数常量。
|
|
|
|
|
* @param {const char*} name 常量名。
|
|
|
|
|
* @param {int} value 数据。
|
|
|
|
|
*
|
|
|
|
|
* @return {ret_t} 返回 RET_OK 表示成功,否则表示失败。
|
|
|
|
|
*/
|
|
|
|
|
ret_t fscript_register_const_int(const char* name, int value);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 注册浮点数常量(只是为了使用方便而提供的包装函数)
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
/**
|
|
|
|
|
* @method fscript_register_const_double
|
|
|
|
|
* 注册浮点数常量。
|
|
|
|
|
* @param {const char*} name 常量名。
|
|
|
|
|
* @param {double} value 数据。
|
|
|
|
|
*
|
|
|
|
|
* @return {ret_t} 返回 RET_OK 表示成功,否则表示失败。
|
|
|
|
|
*/
|
|
|
|
|
ret_t fscript_register_const_double(const char* name, double value);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
示例:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
fscript_register_const_double("PI", 3.1415926);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 在 fscript 中引用常量。引用常量时需要加 fconsts. 前缀。
|
|
|
|
|
|
|
|
|
|
示例:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
print(fconsts.PI)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 9. 扩展模块
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
2021-07-16 07:31:17 +08:00
|
|
|
|
* [字符串扩展模块](fscript_str.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
* [位操作扩展模块](fscript_bits.md)
|
|
|
|
|
* [数学扩展模块](fscript_math.md)
|
|
|
|
|
* [CRC 扩展模块](fscript_crc.md)
|
|
|
|
|
* [value 扩展模块](fscript_value.md)
|
|
|
|
|
* [widget 扩展模块](fscript_widget.md)
|
|
|
|
|
* [object 扩展模块](fscript_object.md)
|
2021-01-13 13:48:59 +08:00
|
|
|
|
* [文件系统扩展模块](fscript_fs.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
* [app\_conf 扩展模块](fscript_app_conf.md)
|
|
|
|
|
* [日期和时间扩展模块](fscript_date_time.md)
|
|
|
|
|
* [JSON 扩展模块](fscript_json.md)
|
|
|
|
|
* [大端小端扩展模块](fscript_endian.md)
|
|
|
|
|
* [istream 扩展模块](fscript_istream.md)
|
|
|
|
|
* [ostream 扩展模块](fscript_ostream.md)
|
|
|
|
|
* [iostream 扩展模块](fscript_iostream.md)
|
|
|
|
|
* [TCP/UDP 流扩展模块](fscript_iostream_inet.md)
|
|
|
|
|
* [文件流扩展模块](fscript_iostream_file.md)
|
|
|
|
|
* [串口流扩展模块](fscript_iostream_serial.md)
|
2021-01-14 10:00:45 +08:00
|
|
|
|
* [动态数组扩展模块](fscript_typed_array.md)
|
|
|
|
|
* [单一类型的动态数组扩展模块](fscript_typed_array.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
* [写缓冲区扩展模块](fscript_wbuffer.md)
|
|
|
|
|
* [读缓冲区扩展模块](fscript_rbuffer.md)
|
2022-12-03 16:26:51 +08:00
|
|
|
|
* [module 扩展模块](fscript_module.md)
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
2022-07-06 09:59:38 +08:00
|
|
|
|
## 9. 更多示例
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
|
|
|
|
请参考 tests/fscripts 目录。
|