概述里提到过,ASCH 智能合约的实现最终采用了 Node.js 的 VM(ECMAScript 运行环境) + TypeScript 子集(智能合约语言,最终编译成 js)的方案。下面讲解一下哪些语言特性是合约内可以使用的,哪些是不可以使用的。
首先,我们得明确 ECMAScript 是语言标准,而 Node.js 是 JavaScript(ECMAScript 的主要实现)的运行时环境,并且提供了额外的底层功能,如:文件操作、网络 IO 等等。Node.js 的 VM 是 ECMAScript/JavaScript 的运行环境(基于 V8),并不能使用 Node.js 中独有的 API,即 ASCH 智能合约中也只能使用 ECMAScript 的部分特性。
运算符
- 一元运算符:+、-、!、~、typeof、void
- 自增减运算符:++、--
- 二元运算符:==、!=、===、!==、<、<=、>、>=、<<、>>、>>>、+、-、*、/、%、|、^、&、in、instanceof
- 赋值运算符:=、+=、-=、*=、/=、%=、<<=、>>=、>>>=、|=、^=、&=
- 逻辑运算符:&&、||
- 三元运算符:?:
- new 操作符
全局变量
-
Object
- 静态方法:assign、entries、keys、values、is
-
Array
- 静态方法:from、isArray、of
- 实例方法:concat、copyWithin、every、fill、filter、find、findIndex、forEach、includes、indexOf、join、lastIndexOf、map、pop、push、reduce、reduceRight、reverse、shift、unshift、slice、some、sort、splice、toString
-
String
- 静态方法:fromCharCode
- 实例方法:charAt、charCodeAt、startsWith、endsWith、includes、indexOf、lastIndexOf、padStart、padEnd、repeat、slice、substr、substring、split、toLowerCase、toUpperCase、trim、trimLeft、trimRight、toString
-
Number
- 静态方法:isNaN、parseFloat、parseInt
- 实例方法:toExponential、toFixed、toPrecision、toString
-
Math
- 静态方法:abs、acos、acosh、asin、asinh、atan、atan2、atanh、cbrt、ceil、clz32、cos、cosh、exp、expm1、floor、fround、hypot、imul、log、log10、log1p、log2、max、min、pow、round、sign、sin、sinh、sqrt、tan、tanh、trunc
-
BigInt
- 构造函数。如:BigInt(100000000000000)
- 实例方法:toString
-
assert。ASCH 智能合约添加的,断言函数
-
Mapping。ASCH 智能合约添加的,需要使用 new 实例化,key-value 型对象
-
静态方法:fromItems、isMapping、keysOf、sizeOf
-
Vector。ASCH 智能合约添加的,容器类,需要使用 new 实例化,类似数组
- 静态方法:isVector
- 实例方法:push、pop、size
-
Crypto。ASCH 智能合约添加的,常用哈希函数
-
Crypto.sha256.hash
-
Crypto.ripemd160.hash
-
Crypto.ed25519.sign
-
Crypto.ed25519.verify
-
ByteBuffer。ASCH 智能合约添加的,二进制操作
- 静态方法:wrap、isByteBuffer
- 实例方法:readInt8、readUint8、readInt16、readUint16、readInt32、readUint32、readInt64、readUint64、readFloat32、readFloat64、readString、readCString、readIString、writeInt8、writeUint8、writeInt16、writeUint16、writeInt32、writeUint32、writeInt64、writeUint64、writeFloat32、writeFloat64、writeString、writeCString、writeIString、toString、toArrayBuffer、flip
-
Util。ASCH 智能合约添加的,工具函数集
- Util.Address.generateNormalAddress
-
修饰器
- @payable。转账接口,如果有
isDefault: true
参数,即表示向该合约地址转账时会自动调用这个方法。其他添加 @payable 修饰器的函数,给合约转账时需要指明函数名,格式 {contract_name}/{pay_mothod_name}。 - @constant。只读接口,可以通过 HTTP 接口调用,该函数有以下使用限制:
- 不能修改合约的状态,即 this 上的属性
- 不能使用 this.transfer,即不能转账
- 不能使用 this.context,因为没有区块信息
- @payable。转账接口,如果有
关键字
- delete
- debugger
- with
- throw
- try-catch
- AsyncFunction + await
- GeneratorFunction + yield
- import
全局变量
- Date
- Function
- Boolean
- Symbol
- Error + EvalError + RangeError + ReferenceError + SyntaxError + TypeError + URIError
- RegExp
- Int8Array + Uint8Array + Uint8ClampedArray + Int16Array + Uint16Array + Int32Array + Uint32Array + Float32Array + Float64Array
- Map + WeakMap
- Set + WeakSet
- JSON
- Promise
- Reflect
- Proxy
- Intl
- WebAssembly
全局方法
- eval + uneval
- isFinite
- isNaN
- parseFloat + parseInt
- encodeURI + decodeURI
- encodeURIComponent + decodeURIComponent
- escape + unescape
- setTimeout + clearTimeout
- setInterval + clearInterval
- setImmediate + clearImmediate
因为 ASCH 智能合约支持了 BigInt 类型,所以 Node.js 版本应当是 v10.13.0+。