Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  1. 10.Sway应用(Sway Reference)

10.1 编译器内部函数(Compiler Intrinsics)

The Sway compiler supports a list of intrinsics that perform various low level operations that are useful for building libraries. Compiler intrinsics should rarely be used but are preferred over asm blocks because they are type-checked and are safer overall. Below is a list of all available compiler intrinsics:

Sway 编译器支持一系列内部函数,这些内部函数执行各种对构建库有用的低级操作。编译器内部函数应该很少使用,但比 asm 块更受欢迎,因为它们经过类型检查并且总体上更安全。下面是所有可用的编译器内在函数的列表:


__size_of_val<T>(val: T) -> u64

Description: Return the size of type T in bytes.

Constraints: None.

说明: 返回类型 T 的大小(以字节为单位)。

约束: 无。


__size_of<T>() -> u64

Description: Return the size of type T in bytes.

Constraints: None.

说明: 返回类型 T 的大小(以字节为单位)。

约束: 无。


__is_reference_type<T>() -> bool

Description: Returns true if T is a reference type and false otherwise.

Constraints: None.

说明: 如果 T 是 引用类型 ,则返回 true,否则返回 false。

约束: 无。


__eq<T>(lhs: T, rhs: T) -> bool

Description: Returns whether lhs and rhs are equal.

Constraints: T is bool, u8, u16, u32, u64, or raw_ptr.

说明: 返回 lhs 和 rhs 是否相等。

约束: T 是bool、u8、u16、u32、u64 或 raw_ptr。


__gt<T>(lhs: T, rhs: T) -> bool

Description: Returns whether lhs is greater than rhs.

Constraints: T is u8, u16, u32, u64.

说明: 返回 lhs 是否大于 rhs。

约束: T 是 u8、u16、u32、u64。


__lt<T>(lhs: T, rhs: T) -> bool

Description: Returns whether lhs is less than rhs.

Constraints: T is u8, u16, u32, u64.

说明: 返回 lhs 是否小于 rhs。

约束: T 是 u8、u16、u32、u64。


__gtf<T>(index: u64, tx_field_id: u64) -> T

Constraints: None.

约束: 无。


__addr_of<T>(val: T) -> raw_ptr

Description: Returns the address in memory where val is stored.

Constraints: T is a reference type.

说明: 返回内存中存储 val 的地址。

约束: T 是引用类型。


__state_load_word(key: b256) -> u64

Description: Reads and returns a single word from storage at key key.

Constraints: None.

描述: 从存储中的键 key 读取并返回单个单词。

约束: 无。


__state_load_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

Description: Reads slots number of slots (b256 each) from storage starting at key key and stores them in memory starting at address ptr. Returns a Boolean describing whether all the storage slots were previously set.

Constraints: None.

描述: 从键 key 开始的存储中读取 slots 槽数(每个 b256),并将它们存储在从地址 ptr 开始的内存中。之后返回一个布尔值,描述是否所有存储槽都已预先设置。

约束: 无。


__state_store_word(key: b256, val: u64) -> bool

Description: Stores a single word val into storage at key key. Returns a Boolean describing whether the store slot was previously set.

Constraints: None.

描述: 将单个单词 val 存储到键 key 处的存储中。返回一个布尔值,描述是否先前设置了存储槽。

约束: 无。


__state_store_quad(key: b256, ptr: raw_ptr, slots: u64) -> bool

Description: Stores slots number of slots (b256 each) starting at address ptr in memory into storage starting at key key. Returns a Boolean describing whether the first storage slot was previously set.

Constraints: None.

描述: 将内存中从地址 ptr 开始的插槽数(每个 b256)存储到以键 key 开始的存储中。返回一个布尔值,描述第一个存储槽是否已设置。

约束: 无。


__log<T>(val: T)

Description: Logs value val.

Constraints: None.

说明: 记录值val。

约束: 无。


__add<T>(lhs: T, rhs: T) -> T

Description: Adds lhs and rhs and returns the result.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 添加 lhs 和 rhs 并返回结果。

约束:T 是整数类型,即u8、u16、u32、u64。


__sub<T>(lhs: T, rhs: T) -> T

Description: Subtracts rhs from lhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 从 lhs 中减去 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


__mul<T>(lhs: T, rhs: T) -> T

Description: Multiplies lhs by rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 将 lhs 乘以 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


__div<T>(lhs: T, rhs: T) -> T

Description: Divides lhs by rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 将 lhs 除以 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


__and<T>(lhs: T, rhs: T) -> T

Description: Bitwise AND lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位与 lhs 和 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


or<T>(lhs: T, rhs: T) -> T

Description: Bitwise OR lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位或 lhs 和 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


__xor<T>(lhs: T, rhs: T) -> T

Description: Bitwise XOR lhs and rhs.

Constraints: T is an integer type, i.e. u8, u16, u32, u64.

说明: 按位异或 lhs 和 rhs。

约束:T 是整数类型,即u8、u16、u32、u64。


__revert(code: u64)

Description: Reverts with error code code.

Constraints: None.

说明: 返回错误代码为 code。

约束: 无。


__ptr_add(ptr: raw_ptr, offset: u64)

Description: Adds offset to the raw value of pointer ptr.

Constraints: None.

说明: 将 offset 添加到指针 ptr 的原始值。

约束: 无。


__ptr_sub(ptr: raw_ptr, offset: u64)

Description: Subtracts offset to the raw value of pointer ptr.

Constraints: None.

说明: 将 offset 减去指针 ptr 的原始值。

约束: 无。


__smo<T>(recipient: b256, data: T, output_index: u64, coins: u64)

Description: Sends a message data of arbitrary type T and coins amount of the base asset to address recipient. This intrinsic assumes that an OutputMessage is available at index output_index.

Constraints: None.

描述: 发送任意类型的消息data和基础资产的coins数量到地址recipient。此内在假设 OutputMessage 在索引 output_index 处可用。

约束: 无。

Previous10.Sway应用(Sway Reference)Next10.2 属性(Attributes)

Last updated 1 year ago

Description: Returns transaction field with ID tx_field_id at index index, if applicable. This is a wrapper around FuelVM's . The resuting field is cast to T.

说明: 返回索引为 index 的 ID 为 tx_field_id 的交易字段(如果适用)。这是 FuelVM 的 的封装器。结果字段被转换为T。

gtf instruction
gtf 指令